Commit 55d20cf
authored
update: 2.6.18 fix: patch opentype.js cmap Format 4 table overflow bug
fix: resolve cmap Format 4 table overflow in large font generation
opentype.js exhibits a critical overflow bug when processing fonts with
8000+ drawing glyphs. The cmap Format 4 segment generator lacks consecutive
segment merging logic, creating isolated segments for each Unicode codepoint.
When character count exceeds approximately 8192, the cumulative table length
calculation produces values exceeding the uint16 maximum (65535 bytes). The
Format 4 length field, defined as uint16, silently truncates on overflow.
Windows font parsers detect the length mismatch between the declared header
value and actual physical data offset, treating the entire font as corrupted
and rejecting it during parsing.
The root cause lies in the cmap.make algorithm operating within a closure,
making direct external patching unfeasible. Segment sorting occurs prior to
binary serialization but provides no opportunity for deduplication.
Implement segment merging via Array.prototype.sort interception during the
Font.prototype.toTables lifecycle. This technique:
- Intercepts sort operations on the segment array post-sort, pre-serialization
- Merges consecutive segments where curr.start equals prev.end + 1 and deltas
are equal, reducing overall segment count proportionally to run lengths
- Restores original Array.sort after completion, avoiding side effects
- Operates at the optimal closure boundary without library modification
Testing confirms fonts with 8000+ glyphs now load correctly on Windows
platforms while maintaining backward compatibility with smaller fonts.1 parent 796b9f7 commit 55d20cf
2 files changed
Lines changed: 33 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2208 | 2208 | | |
2209 | 2209 | | |
2210 | 2210 | | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
| 2230 | + | |
| 2231 | + | |
| 2232 | + | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
| 2238 | + | |
| 2239 | + | |
| 2240 | + | |
| 2241 | + | |
| 2242 | + | |
2211 | 2243 | | |
2212 | 2244 | | |
2213 | 2245 | | |
| |||
0 commit comments