glyph_motion.lib package

Submodules

glyph_motion.lib.font_alphabet_stroke_builder module

K.NAKADA, kengo.nakada@gmail.com

アルファベット文字列から筆順付き stroke データを生成する。

このモジュールは、通常フォントのアウトラインを使わない。 最初からロボットでなぞるための single-line stroke を生成する。

既存のアウトラインフォント処理とは別系統の入力経路である。 既存の outline -> filled -> centerline -> motion の処理はそのまま残す。

このモジュールが出力する FontStrokeGlyph は、筆順を持つ。 FontStrokeGlyph の各 FontStrokePath は、一回ペンを下ろして書く単位である。

stroke 内部は、粗い制御点をそのまま直線で結ぶだけではなく、 stroke_division に従って細かい点列へ分割する。

これにより、FontMotionBuilder は変更せずに、 より細かいロボット移動命令を生成できる。

class FontAlphabetStrokeBuilder(height, spacing, stroke_division=20)[source]

Bases: object

アルファベット文字列から筆順付き stroke データを作るクラスである。

Parameters:
_build_glyph_table()[source]

内蔵 stroke 定義を返す。

座標は 0.0 から 1.0 の正規化座標で定義する。 path の順序が筆順である。

ここで定義する点は制御点に近い粗い点である。 実際に返す FontStrokeGlyph では、stroke_division に従って 各線分を細かい点列へ分割する。

Return type:

Dict[str, List[List[Tuple[float, float]]]]

_subdivide_raw_stroke(raw_stroke)[source]

stroke 内の各線分を細かい点列へ分割する。

Parameters:

raw_stroke (List[Tuple[float, float]])

Return type:

List[Tuple[float, float]]

_subdivide_segment(start_point, end_point)[source]

2点間の線分を stroke_division 個の小区間へ分割する。

返す点列には始点を含めない。 終点は含める。

Parameters:
Return type:

List[Tuple[float, float]]

build_text(text)[source]

文字列を筆順付き stroke データへ変換する。

Parameters:

text (str)

Return type:

FontStrokeGlyph

main()[source]

アルファベット stroke 生成を単体確認する。

Return type:

None

glyph_motion.lib.font_centerline_cleaner module

K.NAKADA, kengo.nakada@gmail.com

中心線抽出後の線分を、ロボット移動命令へ渡しやすい点列へ整理する。

FontCenterlineExtractor が出力する FontCenterlineGlyph は、画像処理で得た 生の中心線である。生の中心線には、短い枝、重複点、細かすぎる点間隔、 孤立した短線分が含まれる可能性がある。

このモジュールは、生の FontCenterlineGlyph を入力として受け取り、 短すぎる線分の除去、連続重複点の除去、点間隔の再サンプリングを行った FontCenterlineGlyph を返す。

ここではフォント読み込み、輪郭抽出、塗りつぶし、中心線抽出、表示、 ロボット固有API呼び出しは行わない。

class FontCenterlineCleaner(min_path_length, point_spacing)[source]

Bases: object

生の中心線をロボット向けの中心線へ整理するクラスである。

Parameters:
_calculate_distance(point_a, point_b)[source]

2点間距離を返す。

Parameters:
Return type:

float

_calculate_path_length(centerline_path)[source]

中心線パス全体の長さを計算する。

Parameters:

centerline_path (FontCenterlinePath)

Return type:

float

_remove_consecutive_duplicate_points(centerline_path)[source]

連続する同一点を除去する。

Parameters:

centerline_path (FontCenterlinePath)

Return type:

FontCenterlinePath

_resample_path(centerline_path)[source]

中心線パスを指定間隔に近い点列へ再サンプリングする。

Parameters:

centerline_path (FontCenterlinePath)

Return type:

FontCenterlinePath

clean(centerline_glyph)[source]

中心線群を整理する。

Parameters:

centerline_glyph (FontCenterlineGlyph) – 整理対象の中心線群である。

Returns:

整理後の中心線群である。

Return type:

FontCenterlineGlyph

main()[source]

中心線整理を単体確認する。

Return type:

None

glyph_motion.lib.font_centerline_extractor module

K.NAKADA, kengo.nakada@gmail.com

塗りつぶし済み文字領域から中心線を抽出する。

FontFilledGlyph を入力として受け取り、FontCenterlineGlyph を出力する。 太さを持つ文字領域を、ロボットがなぞる候補になる細い線へ変換する。

class FontCenterlineExtractor[source]

Bases: object

塗りつぶし済み文字領域から中心線を抽出する。

_build_graph(skeleton)[source]

スケルトン画像を8近傍グラフへ変換する。

Return type:

Dict[Tuple[int, int], List[Tuple[int, int]]]

_collect_neighbors(skeleton, row_index, column_index)[source]

指定ピクセルに隣接するスケルトンピクセルを集める。

Parameters:
  • row_index (int)

  • column_index (int)

Return type:

List[Tuple[int, int]]

_collect_start_pixels(graph)[source]

線分追跡の始点候補を集める。

Parameters:

graph (Dict[Tuple[int, int], List[Tuple[int, int]]])

Return type:

List[Tuple[int, int]]

_make_edge(pixel_a, pixel_b)[source]

無向エッジを一意な順序で表す。

Parameters:
Return type:

Tuple[Tuple[int, int], Tuple[int, int]]

_trace_from_edge(graph, start_pixel, next_pixel, visited_edges)[source]

始点と次点から1本の中心線パスを追跡する。

Parameters:
Return type:

List[Tuple[int, int]]

_trace_pixel_paths(graph)[source]

スケルトングラフを線分パス群へ分解する。

Parameters:

graph (Dict[Tuple[int, int], List[Tuple[int, int]]])

Return type:

List[List[Tuple[int, int]]]

extract(filled_glyph)[source]

文字領域から中心線群を抽出する。

Parameters:

filled_glyph (FontFilledGlyph)

Return type:

FontCenterlineGlyph

main()[source]

中心線抽出を単体確認する。

Return type:

None

glyph_motion.lib.font_fill_glyph module

glyph_motion.lib.font_geometry module

K.NAKADA, kengo.nakada@gmail.com

フォント文字を中心線へ変換する処理で共有する幾何データ型を定義する。

このモジュールは、処理段階の入出力データだけを保持する。 フォントの読み込み、輪郭抽出、塗りつぶし、中心線抽出、表示、移動命令生成は それぞれ別モジュールで行う。

class FontCenterlineGlyph(text, paths)[source]

Bases: object

指定文字列から得た中心線群を表す。

Parameters:
paths: List[FontCenterlinePath]
text: str
class FontCenterlinePath(points)[source]

Bases: object

中心線一本を表す。

Parameters:

points (List[FontPoint])

points: List[FontPoint]
class FontFilledGlyph(text, mask, x_min, y_min, pixel_size)[source]

Bases: object

輪郭を格子上に塗りつぶした文字領域を表す。

Parameters:
index_to_point(row_index, column_index)[source]

配列インデックスをXY座標へ変換する。

Parameters:
  • row_index (int)

  • column_index (int)

Return type:

FontPoint

mask: ndarray
pixel_size: float
text: str
x_min: float
y_min: float
class FontMotionCommand(pen, dx, dy)[source]

Bases: object

中心線または筆画から作成したXY相対移動命令を表す。

Parameters:
dx: float
dy: float
pen: float
class FontOutlineGlyph(text, paths)[source]

Bases: object

指定文字列から取り出した輪郭線群を表す。

Parameters:
paths: List[FontOutlinePath]
scaled(scale)[source]

輪郭線群全体を拡大縮小した文字輪郭を返す。

Parameters:

scale (float)

Return type:

FontOutlineGlyph

shifted(x_offset, y_offset)[source]

輪郭線群全体を平行移動した文字輪郭を返す。

Parameters:
Return type:

FontOutlineGlyph

text: str
class FontOutlinePath(points, closed)[source]

Bases: object

フォントから取り出した一本の輪郭線を表す。

Parameters:
closed: bool
points: List[FontPoint]
scaled(scale)[source]

輪郭全体を拡大縮小した輪郭を返す。

Parameters:

scale (float)

Return type:

FontOutlinePath

shifted(x_offset, y_offset)[source]

輪郭全体を平行移動した輪郭を返す。

Parameters:
Return type:

FontOutlinePath

class FontPoint(x, y)[source]

Bases: object

XY平面上の一点を表す。

Parameters:
as_tuple()[source]

座標をタプルとして返す。

Return type:

Tuple[float, float]

scaled(scale)[source]

原点基準で拡大縮小した点を返す。

Parameters:

scale (float)

Return type:

FontPoint

shifted(x_offset, y_offset)[source]

指定量だけ平行移動した点を返す。

Parameters:
Return type:

FontPoint

x: float
y: float
class FontStrokeGlyph(text, paths)[source]

Bases: object

筆順付き文字データを表す。

paths の順序が、そのままロボットに書かせる順序である。 各 FontStrokePath は、一回ペンを下ろして書く単位である。

Parameters:
paths: List[FontStrokePath]
text: str
class FontStrokePath(points)[source]

Bases: object

一回ペンを下ろして書く一本の筆画を表す。

FontCenterlinePath は、画像処理で抽出された中心線である。 FontStrokePath は、ロボットが実際に一筆でなぞる単位である。

paths の分割は、ペンアップ・ペンダウンの単位に対応する。

Parameters:

points (List[FontPoint])

points: List[FontPoint]
main()[source]

共通データ型の生成を確認する。

Return type:

None

glyph_motion.lib.font_kanjivg_stroke_loader module

K.NAKADA, kengo.nakada@gmail.com

KanjiVG の SVG ファイルから筆順付き stroke データを読み込む。

[oai_citation:1‡KanjiVG](https://kanjivg.tagaini.net/svg-format.html?utm_source=chatgpt.com)つ SVG path を読み取り、FontStrokeGlyph に変換する。

KanjiVG の StrokePaths グループには、漢字の各筆画が SVG path として 筆順順に格納されている。この順序をそのままロボット筆記順として使う。

このモジュールは、漢字用の本命ルートである。

KanjiVG SVG -> FontStrokeGlyph -> FontMotionBuilder

という流れで使う。

class FontKanjiVGStrokeLoader(kanjivg_directory, height, spacing, curve_division=16)[source]

Bases: object

KanjiVG SVG から筆順付き FontStrokeGlyph を生成するクラスである。

Parameters:
_collect_stroke_paths(root)[source]

StrokePaths グループ内の path d 属性を筆順順に集める。

Return type:

List[str]

_convert_svg_point(point, view_box, scale)[source]

SVG 座標を表示・ロボット処理用のXY座標へ変換する。

Parameters:
Return type:

FontPoint

_find_stroke_paths_group(root)[source]

id が kvg:StrokePaths で始まる SVG group を探す。

_find_svg_file(character)[source]

文字に対応する KanjiVG SVG ファイルを探す。

Parameters:

character (str)

Return type:

Path

_read_view_box(root)[source]

SVG の viewBox を読み取る。

Return type:

Tuple[float, float, float, float]

_strip_namespace(tag)[source]

XML 名前空間を除いたタグ名を返す。

Parameters:

tag (str)

Return type:

str

load_character(character)[source]

一文字の KanjiVG SVG を読み込み、筆順付き stroke データを返す。

Parameters:

character (str)

Return type:

Tuple[FontStrokeGlyph, float]

load_text(text)[source]

文字列を KanjiVG 由来の筆順付き stroke データとして読み込む。

Parameters:

text (str)

Return type:

FontStrokeGlyph

class SvgPathSampler(curve_division)[source]

Bases: object

SVG path の M/L/H/V/Q/T/C/S/Z 命令を点列へサンプリングする。

Parameters:

curve_division (int)

_has_number(tokens, index)[source]

現在位置に数値 token があるか判定する。

Parameters:
Return type:

bool

_is_command_token(token)[source]

token が SVG path command か判定する。

Parameters:

token (str)

Return type:

bool

_make_point(x, y, relative, current_point)[source]

絶対または相対座標から FontPoint を作る。

Parameters:
Return type:

FontPoint

_reflect_point(point, center)[source]

制御点を現在点に対して反転する。

Parameters:
Return type:

FontPoint

_sample_cubic_bezier(p0, p1, p2, p3)[source]

3次ベジェ曲線を点列化する。

Parameters:
Return type:

List[FontPoint]

_sample_quadratic_bezier(p0, p1, p2)[source]

2次ベジェ曲線を点列化する。

Parameters:
Return type:

List[FontPoint]

_tokenize_path(path_data)[source]

SVG path data を command と数値 token に分解する。

Parameters:

path_data (str)

Return type:

List[str]

sample_path(path_data)[source]

SVG path data を点列へ変換する。

Parameters:

path_data (str)

Return type:

List[FontPoint]

main()[source]

KanjiVG stroke 読み込みを単体確認する。

Return type:

None

glyph_motion.lib.font_outline_extractor module

glyph_motion.lib.font_viewer module

glyph_motion.lib.run_font_kanjivg_stroke_view module

glyph_motion.lib.run_font_view module