+ /*
+ * It's possible for fonts to specify the same name for multiple
+ * glyphs, which would make one of them inaccessible. Check for
+ * that, and rename all but one of each set.
+ *
+ * To ensure that we don't clash with any existing glyph names,
+ * our renaming involves appending the glyph number formatted with
+ * enough leading zeroes to make it longer than any all-digit
+ * suffix that already exists in the font.
+ */
+ suflen = 4;
+ for (i = 0; i < sf->nglyphs; i++) {
+ char const *p;
+ p = strrchr(glyph_extern(sfnt_indextoglyph(sf, i)), '.');
+ if (p && !(p+1)[strspn(p+1, "0123456789")] && strlen(p+1) > suflen)
+ suflen = strlen(p+1);
+ }
+ suflen++;
+ prev = sfnt_indextoglyph(sf, sf->glyphsbyname[0]);
+ for (i = 1; i < sf->nglyphs; i++) {
+ if (prev == (this = sfnt_indextoglyph(sf, sf->glyphsbyname[i]))) {
+ char const *basename;
+ char *buf;
+ basename = glyph_extern(this);
+ buf = snewn(strlen(basename) + 2 + suflen, char);
+ strcpy(buf, basename);
+ sprintf(buf + strlen(basename), ".%0*hu", suflen,
+ sf->glyphsbyname[i]);
+ sf->glyphsbyindex[sf->glyphsbyname[i]] = glyph_intern(buf);
+ sfree(buf);
+ }
+ prev = this;
+ }
+ /* We may have renamed some glyphs, so re-sort the array. */
+ qsort(sf->glyphsbyname, sf->nglyphs, sizeof(*sf->glyphsbyname),
+ glyphsbyname_cmp);