Fix the loca table length when there is enough space for it

It fixes #13425.
This commit is contained in:
calixteman 2025-12-27 22:20:15 +01:00
parent 33e857995c
commit 22932f7b68
No known key found for this signature in database
GPG Key ID: 0C5442631EE0691F
3 changed files with 38 additions and 10 deletions

View File

@ -2702,13 +2702,36 @@ class Font {
writeUint32(tables.maxp.data, 0, version);
}
let isGlyphLocationsLong = int16(
tables.head.data[50],
tables.head.data[51]
);
if (tables.loca) {
const locaLength = isGlyphLocationsLong
? (numGlyphs + 1) * 4
: (numGlyphs + 1) * 2;
if (tables.loca.length !== locaLength) {
warn("Incorrect 'loca' table length -- attempting to fix it.");
// The length of the loca table is wrong (see #13425), so we check if we
// have enough space to fix it.
const sortedTables = Object.values(tables)
.filter(Boolean)
.sort((a, b) => a.offset - b.offset);
const locaIndex = sortedTables.indexOf(tables.loca);
const nextTable = sortedTables[locaIndex + 1] || null;
if (nextTable && tables.loca.offset + locaLength < nextTable.offset) {
const previousPos = font.pos;
font.pos = font.start || 0;
font.skip(tables.loca.offset);
tables.loca.data = font.getBytes(locaLength);
tables.loca.length = locaLength;
font.pos = previousPos;
}
}
}
if (properties.scaleFactors?.length === numGlyphs && isTrueType) {
const { scaleFactors } = properties;
const isGlyphLocationsLong = int16(
tables.head.data[50],
tables.head.data[51]
);
const glyphs = new GlyfTable({
glyfTable: tables.glyf.data,
isGlyphLocationsLong,
@ -2723,7 +2746,7 @@ class Font {
if (isLocationLong !== !!isGlyphLocationsLong) {
tables.head.data[50] = 0;
tables.head.data[51] = isLocationLong ? 1 : 0;
isGlyphLocationsLong = tables.head.data[51] = isLocationLong ? 1 : 0;
}
const metrics = tables.hmtx.data;
@ -2801,10 +2824,6 @@ class Font {
let missingGlyphs = Object.create(null);
if (isTrueType) {
const isGlyphLocationsLong = int16(
tables.head.data[50],
tables.head.data[51]
);
const glyphsInfo = sanitizeGlyphLocations(
tables.loca,
tables.glyf,

View File

@ -0,0 +1 @@
https://github.com/mozilla/pdf.js/files/6529459/20200927_204903_509.pdf

View File

@ -13115,5 +13115,13 @@
"md5": "b85c798b9a4cc2cd4337d335321cc612",
"rounds": 1,
"type": "eq"
},
{
"id": "issue13425",
"file": "pdfs/issue13425.pdf",
"md5": "36854e6ad43b8e0446d3d64e8f2950bf",
"rounds": 1,
"link": true,
"type": "eq"
}
]