terrastruct / d2

D2 is a modern diagram scripting language that turns text to diagrams.
https://d2lang.com
Mozilla Public License 2.0
16.68k stars 417 forks source link

Fonts sometimes have an incorrect cmap subtable #1252

Closed eiswind closed 1 year ago

eiswind commented 1 year ago

I found that when fonts are embedded, they are not always embedded the same way. Sometimes the font is correct, sometimes it has an incorrect cmap subtable.

Browsers seem to ignore that but other tools I use to render the svg cannot handle that.

I'll attach two files, one (injection) has the font correctly, the other (boot) one is broken.

01_boot_d2 01_injection_d2

mikeday commented 1 year ago

The issue here appears to be that the cmap subtable (format 4) has an odd length count?

alixander commented 1 year ago

oh interestingg, we do roll out own cmap font thing for optimized subsetting. very possible to have missed something.

will test with the "Allsorts" that @mikeday mentioned in his other issue.

other tools I use to render the svg cannot handle that

@eiswind what's a tool we can test on to confirm we have the right fix?

mikeday commented 1 year ago

I think it might be this line 611 of generateCMAPTable in utf8fontfile.go in gofpdf:

length := 16 + (8 * segCount) + (numSymbols + 1)

The length field is the size in bytes of the cmap format 4 subtable as defined here:

https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-4-segment-mapping-to-delta-values

Since this subtable is entirely comprised of 16-bit values it is impossible for it to be odd; the 16 + (8 * segCount) part is correct but the numSymbols + 1 is highly suspect!

mikeday commented 1 year ago

It appears that the original PHP fpdf does not have this bug, it just takes the length of the subtable in bytes directly:

$data .= pack('nnn', 4, 6+strlen($cmap), 0); // format, length, language
eiswind commented 1 year ago

@alixander please try prince xml https://www.princexml.com/ @mikeday thanks for your assistance!

eiswind commented 1 year ago

Are there chances that this will get fixed? Sadly I don't get what going on there, so I can't provide a patch.

alixander commented 1 year ago

@berniexie can you look into this on Tuesday please?

berniexie commented 1 year ago

@eiswind can you give me the exact d2 code of the failed Spring Boot example

alixander commented 1 year ago

can you not reproduce with some simple "asdf" text? @berniexie

berniexie commented 1 year ago

@alixander not with the latest version of d2. I suspect the issue may already fixed but I wanted to confirm with the exact case that can generate an invalid subset.

alixander commented 1 year ago

how would it have been fixed? i don't think there's any PR that touched this part since the issue was created

berniexie commented 1 year ago

Nevermind. I was able to reproduce the issue with an example.