fonttoolsでSVGのuseからglyphが作れないか試す
前回まで
特に<use>した図形に対してtransform="rotate(180,128,128)"などの変換をかけた図形のパスデータを生成し、そのまま<glyph>にしたい。
今回はそれが出来そうな方法を探って試してみる。そのための準備。
情報源
以前ググっていたら以下を発見した。
その時、以下が気になっていた。
SVG からグリフにする
コードを見てみる。
def svg2glif(svg, name, width=0, height=0, unicodes=None, transform=None, version=2): """Convert an SVG outline to a UFO glyph with given 'name', advance 'width' and 'height' (int), and 'unicodes' (list of int). Return the resulting string in GLIF format (default: version 2). If 'transform' is provided, apply a transformation matrix before the conversion (must be tuple of 6 floats, or a FontTools Transform object). """ glyph = SimpleNamespace(width=width, height=height, unicodes=unicodes) outline = SVGPath.fromstring(svg, transform=transform) # writeGlyphToString takes a callable (usually a glyph's drawPoints # method) that accepts a PointPen, however SVGPath currently only has # a draw method that accepts a segment pen. We need to wrap the call # with a converter pen. def drawPoints(pointPen): pen = SegmentToPointPen(pointPen) outline.draw(pen) return writeGlyphToString( name, glyphObject=glyph, drawPointsFunc=drawPoints, formatVersion=version )
引数transformがある。もしやSVGのuseに渡していたtransform属性値が使える? rotate(180, 128, 128)等が使えるのか? 以下のことができなかった件が解決できる?
コードのコメント文を見ると望みは薄そうだが、まずはインストールしてみる。
インストール
pip install fonttools
成功。出力ログは以下。バージョン4.57.0。
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting fonttools Downloading fonttools-4.57.0-py3-none-any.whl.metadata (102 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 102.5/102.5 kB 1.0 MB/s eta 0:00:00 Downloading fonttools-4.57.0-py3-none-any.whl (1.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 2.0 MB/s eta 0:00:00 Installing collected packages: fonttools Successfully installed fonttools-4.57.0 [notice] A new release of pip is available: 23.3.2 -> 25.0.1 [notice] To update, run: pip install --upgrade pip
ヘルプ
$ fonttools --help fonttools v4.57.0 fonttools cffLib.CFF2ToCFF Convert CFF OTF font to CFF2 OTF font fonttools cffLib.CFFToCFF2 Convert CFF OTF font to CFF2 OTF font fonttools cffLib.width Calculate optimum defaultWidthX/nominalWidthX values fonttools cu2qu Convert a UFO font from cubic to quadratic curves fonttools designspaceLib Roundtrip .designspace file through the DesignSpaceDocument class fonttools feaLib Add features from a feature file (.fea) into an OTF font fonttools help Show this help fonttools merge Merge multiple fonts into one fonttools mtiLib Convert a FontDame OTL file to TTX XML fonttools otlLib.optimize Optimize the layout tables of an existing font fonttools pens.statisticsPen Report font glyph shape geometricsl statistics fonttools pens.svgPathPen Generate per-character SVG from font and text fonttools qu2cu Convert an OpenType font from quadratic to cubic curves fonttools subset OpenType font subsetter and optimizer fonttools ttLib Open/save fonts with TTFont() or TTCollection() fonttools ttLib.scaleUpem Change the units-per-EM of fonts fonttools ttLib.woff2 Compress and decompress WOFF2 fonts fonttools ttx Convert OpenType fonts to XML and back fonttools varLib Build variable fonts from a designspace file and masters fonttools varLib.avar Add `avar` table from designspace file to variable font. fonttools varLib.avarPlanner Plan the standard axis mappings for a variable font fonttools varLib.hvar Add `HVAR` table to variable font. fonttools varLib.instancer Partially instantiate a variable font fonttools varLib.interpolatable Test for interpolatability issues between fonts fonttools varLib.interpolate_layout Interpolate GDEF/GPOS/GSUB tables for a point on a designspace fonttools varLib.models Normalize locations on a given designspace fonttools varLib.mutator Instantiate a variation font fonttools varLib.varStore Optimize a font's GDEF variation store fonttools voltLib.voltToFea Convert MS VOLT to AFDKO feature files.
svg2glif
svg2glifを丸ごとパクって実行してみる。
$ python svg2glif.py Traceback (most recent call last): File "/tmp/work/svg2glif.py", line 11, in <module> from fontTools.ufoLib.glifLib import writeGlyphToString File "/home/pi/.anyenv/envs/pyenv/versions/3.10.5/lib/python3.10/site-packages/fontTools/ufoLib/__init__.py", line 42, in <module> import fs ModuleNotFoundError: No module named 'fs'
エラーが出た。
再びインストール
READMEによると、完全インストールコマンドは以下だったらしい。最初から言ってよ。
pip install fonttools[ufo,lxml,woff,unicode]
実行したらエラーが出た。
$ pip install fonttools[ufo,lxml,woff,unicode] Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: fonttools[lxml,ufo,unicode,woff] in /home/pi/.anyenv/envs/pyenv/versions/3.10.5/lib/python3.10/site-packages (4.57.0) Collecting brotli>=1.0.1 (from fonttools[lxml,ufo,unicode,woff]) Downloading Brotli-1.1.0.tar.gz (7.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.4/7.4 MB 216.1 kB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Collecting zopfli>=0.1.4 (from fonttools[lxml,ufo,unicode,woff]) Downloading zopfli-0.2.3.post1.tar.gz (175 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 175.6/175.6 kB 225.6 kB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... done Collecting unicodedata2>=15.1.0 (from fonttools[lxml,ufo,unicode,woff]) Downloading unicodedata2-16.0.0.tar.gz (672 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 672.2/672.2 kB 147.0 kB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Collecting fs<3,>=2.2.0 (from fonttools[lxml,ufo,unicode,woff]) Downloading https://www.piwheels.org/simple/fs/fs-2.4.16-py2.py3-none-any.whl (138 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 138.2/138.2 kB 180.5 kB/s eta 0:00:00 Collecting lxml>=4.0 (from fonttools[lxml,ufo,unicode,woff]) Downloading lxml-5.3.2.tar.gz (3.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.7/3.7 MB 179.2 kB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [4 lines of output] <string>:67: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html Building lxml version 5.3.2. Building without Cython. Error: Please make sure the libxml2 and libxslt development packages are installed. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. [notice] A new release of pip is available: 23.3.2 -> 25.0.1 [notice] To update, run: pip install --upgrade pip
ググったら以下がヒット。
先にlxmlより先に以下が必要らしい。(教えてよ。最初に言ってよ)
sudo apt install -y libxml2-dev libxslt-dev python3-dev
再試行。
pip install fonttools[ufo,lxml,woff,unicode]
長い時間を経て終了。
svg2glif.py 起動確認
python svg2glif.py
usage: svg2glif.py [-h] [-n NAME] [-w WIDTH] [-H HEIGHT] [-u UNICODES] [-t TRANSFORM] [-f {1,2}] INPUT.svg [OUTPUT.glif] svg2glif.py: error: the following arguments are required: INPUT.svg
起動は確認できた。あとは正しく引数を渡してやれば試せそう。
今回はここまで。