decktape icon indicating copy to clipboard operation
decktape copied to clipboard

Fix merging embedded fonts

Open jkub6 opened this issue 1 month ago • 3 comments

I have been using d2 to create diagrams in my revealjs presentations, which are embedded as svg files. The svg files have embedded fonts with the same name, but different glyphs. This has been causing issues when attempting to merge the fonts.

Error: Repeat unicode, glyph index: 9
    at Object.raise (/nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/node_modules/fonteditor-core/lib/ttf/error.js:45:17)
    at /nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/node_modules/fonteditor-core/lib/ttf/ttfwriter.js:78:33
    at Array.forEach (<anonymous>)
    at /nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/node_modules/fonteditor-core/lib/ttf/ttfwriter.js:76:24
    at Array.forEach (<anonymous>)
    at TTFWriter.resolveTTF (/nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/node_modules/fonteditor-core/lib/ttf/ttfwriter.js:73:16)
    at TTFWriter.write (/nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/node_modules/fonteditor-core/lib/ttf/ttfwriter.js:221:12)
    at Font.write (/nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/node_modules/fonteditor-core/lib/ttf/font.js:156:53)
    at file:///nix/store/0l2sgyrb6ggd4nv5balc2svq8g83dry4-decktape/lib/node_modules/decktape/decktape.js:402:58
    at Array.forEach (<anonymous>) {
  number: 10200,
  data: 9
}

This change will hash each font and only attempt to merge them if the fonts are identical (hashes match). This fully resolves the problem for me.

jkub6 avatar Oct 28 '25 17:10 jkub6

@jkub6 thanks a lot for opening this up. Would you be able to share your presentation, I'd like to check whether it's not voiding the merging of the fonts as I suspect the SHA might become different for each slide.

astefanutti avatar Oct 28 '25 17:10 astefanutti

Here is a minimal self-contained example that exibits the error: index.html

jkub6 avatar Oct 28 '25 19:10 jkub6

@jkub6 thanks. I've debugged it and what happens is that duplicated glyphs exist at different indices. This is the first time I see this case.

I think the correct fix would be to lookup the glyph index by matches glyphs by unicode value before calling the mergeGlyph function and use the index from the source font: https://github.com/astefanutti/decktape/blob/eb90f7fdd5a5252047c3532317013d8b6b918c37/decktape.js#L517-L527

astefanutti avatar Nov 12 '25 21:11 astefanutti