+static subfont_map_entry *encode_glyph_at(glyph g, wchar_t u,
+ font_encoding *fe, int pos)
+{
+ subfont_map_entry *sme = snew(subfont_map_entry);
+
+ sme->subfont = fe;
+ sme->position = pos;
+ fe->vector[pos] = g;
+ fe->to_unicode[pos] = u;
+ add234(fe->font->subfont_map, sme);
+ return sme;
+}
+
+static int new_sfmap_cmp(void *a, void *b)
+{
+ glyph ga = *(glyph *)a;
+ subfont_map_entry *sb = b;
+ glyph gb = sb->subfont->vector[sb->position];
+
+ if (ga < gb) return -1;
+ if (ga > gb) return 1;
+ return 0;
+}
+
+static subfont_map_entry *encode_glyph(glyph g, wchar_t u, font_data *font)
+{
+ subfont_map_entry *sme;
+ int c;
+
+ sme = find234(font->subfont_map, &g, new_sfmap_cmp);
+ if (sme) return sme;
+
+ /*
+ * This character is not yet in a subfont. Assign one.
+ */
+ if (font->latest_subfont->free_pos >= 0x100)
+ font->latest_subfont = new_font_encoding(font);
+
+ c = font->latest_subfont->free_pos++;
+ if (font->latest_subfont->free_pos == 0x7F)
+ font->latest_subfont->free_pos = 0xA1;
+
+ return encode_glyph_at(g, u, font->latest_subfont, c);
+}
+
+static int sfmap_cmp(void *a, void *b)
+{
+ subfont_map_entry *sa = a, *sb = b;
+ glyph ga = sa->subfont->vector[sa->position];
+ glyph gb = sb->subfont->vector[sb->position];
+
+ if (ga < gb) return -1;
+ if (ga > gb) return 1;
+ return 0;
+}
+
+int width_cmp(void *a, void *b)
+{
+ glyph_width const *wa = a, *wb = b;
+
+ if (wa->glyph < wb->glyph)
+ return -1;
+ if (wa->glyph > wb->glyph)
+ return 1;
+ return 0;
+}
+
+int kern_cmp(void *a, void *b)
+{
+ kern_pair const *ka = a, *kb = b;
+
+ if (ka->left < kb->left)
+ return -1;
+ if (ka->left > kb->left)
+ return 1;
+ if (ka->right < kb->right)
+ return -1;
+ if (ka->right > kb->right)
+ return 1;
+ return 0;
+}
+
+int lig_cmp(void *a, void *b)
+{
+ ligature const *la = a, *lb = b;
+
+ if (la->left < lb->left)
+ return -1;
+ if (la->left > lb->left)
+ return 1;
+ if (la->right < lb->right)
+ return -1;
+ if (la->right > lb->right)
+ return 1;
+ return 0;
+}
+
+static int utoglyph(font_info const *fi, wchar_t u) {
+ return (u < 0 || u > 0xFFFF ? NOGLYPH : fi->bmp[u]);
+}
+