<!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 &gt; main.ps
troff: main.tr:12: warning: can&#39;t find special character &#39;u53CD&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u5012&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u8BF4&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u662F&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u5F97&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u4E86&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u6D41&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u884C&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u6027&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u611F&#39;
troff: main.tr:12: warning: can&#39;t find special character &#39;u5192&#39;</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(&quot;/usr/share/texlive/texmf-dist/fonts/opentype/public/fandol/FandolSong-Regular.otf&quot;)
sel = myfont.selection.all()

for glyph in sel.byGlyphs:
 if 0x4e00 &lt;= glyph.unicode &lt;= 0x9fff:
   glyph.glyphname = f&quot;uni{glyph.unicode:04X}&quot;

myfont.save(&quot;/tmp/FandolSongR.ttf&quot;)</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>