<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mass renaming glyphs in fontforge with python</title>
<link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
<header>
<h1 class="title">Mass renaming glyphs in fontforge with python</h1>
<p class="date">2023-09-02</p>
</header>
<p>I've got a book that I've roughly typeset in XeTeX, and out of curiosity I decided to port it to Groff. I wanted to see how similar I could get it to look and I started typesetting in MS Song since that looked like what XeTeX had picked (for this project my tex file didn't specify a font).</p>
<p>I soon noticed that XeTeX had used a very similar but slightly different font, Fandol Song. TeXlive had installed an OTF copy of Fandol Song at /usr/share/texlive/texmf-dist/fonts/opentype/public/fandol/FandolSong-Regular.otf, so I used the <a href="
https://www.schaffter.ca/mom/mom-05.html#install-font">install-font.sh script from the creator of the groff mom macros</a> to prepare it for use with Groff, but Groff complained that all of my CJK glyphs couldn't be found:</p>
<pre><code>groff -mzh -Kutf8 -Tps main.tr > main.ps
troff: main.tr:12: warning: can't find special character 'u53CD'
troff: main.tr:12: warning: can't find special character 'u5012'
troff: main.tr:12: warning: can't find special character 'u8BF4'
troff: main.tr:12: warning: can't find special character 'u662F'
troff: main.tr:12: warning: can't find special character 'u5F97'
troff: main.tr:12: warning: can't find special character 'u4E86'
troff: main.tr:12: warning: can't find special character 'u6D41'
troff: main.tr:12: warning: can't find special character 'u884C'
troff: main.tr:12: warning: can't find special character 'u6027'
troff: main.tr:12: warning: can't find special character 'u611F'
troff: main.tr:12: warning: can't find special character 'u5192'</code></pre>
<p>Eventually I traced the problem down to the format of the glyph names: my other CJK fonts seem to use a glyph name like "uni4E86" for the glyph representing unicode codepoint U+4E86 (了), whereas TeX's FandolSong file had a glyph name like "GB1.2580", with 2580 corresponding to the Character ID used for 了 in the Adobe-GB1-6 character collection.</p>
<p>I'm sure this isn't the proper way to fix this, but I opted to just run a fontforge script using the python API to explicitly reset the glyph name for each character in the CJK Unified Ideographs unicode block which spans U+4E00 - U+9FFF:</p>
<pre><code>myfont = fontforge.open("/usr/share/texlive/texmf-dist/fonts/opentype/public/fandol/FandolSong-Regular.otf")
sel = myfont.selection.all()
for glyph in sel.byGlyphs:
if 0x4e00 <= glyph.unicode <= 0x9fff:
glyph.glyphname = f"uni{glyph.unicode:04X}"
myfont.save("/tmp/FandolSongR.ttf")</code></pre>
<p>Useful links:</p>
<ul>
<li><a href="
https://support.stmdocs.in/wiki/index.php?title=Using_TrueType_fonts_with_pdfTeX" class="uri">
https://support.stmdocs.in/wiki/index.php?title=Using_TrueType_fonts_with_pdfTeX</a></li>
<li><a href="
https://kleshwong.com/blog/post/composing-pdf-on-linux-happily-as-an-non-latin-coder/" class="uri">
https://kleshwong.com/blog/post/composing-pdf-on-linux-happily-as-an-non-latin-coder/</a></li>
<li><a href="
https://silnrsi.github.io/FDBP/en-US/Adobe_Glyph_List.html" class="uri">
https://silnrsi.github.io/FDBP/en-US/Adobe_Glyph_List.html</a></li>
</ul>
</body>
</html>