{"record":{"id":"bda7fe077d71fd72","repo":"mozilla/pdf.js","slug":"could-not-fix-indextolocformat-indextolocformat","errorCode":null,"errorMessage":"Could not fix indexToLocFormat: ${indexToLocFormat}","messagePattern":"Could not fix indexToLocFormat: (.+?)","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/fonts.js","lineNumber":2159,"sourceCode":"        // consists of long offsets.\n        //\n        // The number of entries in the loca table should be numGlyphs + 1.\n        //\n        // Using this information, we can work backwards to deduce if the\n        // size of each offset in the loca table, and thus figure out the\n        // appropriate value for indexToLocFormat.\n\n        const numGlyphsPlusOne = numGlyphs + 1;\n        if (locaLength === numGlyphsPlusOne << 1) {\n          // 0x0000 indicates the loca table consists of short offsets\n          data[50] = 0;\n          data[51] = 0;\n        } else if (locaLength === numGlyphsPlusOne << 2) {\n          // 0x0001 indicates the loca table consists of long offsets\n          data[50] = 0;\n          data[51] = 1;\n        } else {\n          throw new FormatError(\n            \"Could not fix indexToLocFormat: \" + indexToLocFormat\n          );\n        }\n      }\n    }\n\n    function sanitizeGlyphLocations(\n      loca,\n      glyf,\n      numGlyphs,\n      isGlyphLocationsLong,\n      hintsValid,\n      dupFirstEntry,\n      maxSizeOfInstructions\n    ) {\n      let itemSize, itemDecode, itemEncode;\n      if (isGlyphLocationsLong) {\n        itemSize = 4;","sourceCodeStart":2141,"sourceCodeEnd":2177,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/fonts.js#L2141-L2177","documentation":"Thrown by sanitizeHead() when the head table's indexToLocFormat field is invalid (not 0 or 1) and the code cannot deduce the correct value from the loca table length. The loca table should be either 2*(numGlyphs+1) bytes (short format, indexToLocFormat=0) or 4*(numGlyphs+1) bytes (long format, indexToLocFormat=1); if neither matches, recovery is impossible.","triggerScenarios":"sanitizeHead detects indexToLocFormat outside [0,1], then checks locaLength against numGlyphsPlusOne<<1 and numGlyphsPlusOne<<2. If neither matches, the throw fires. This means both the head table's indexToLocFormat field and the loca table's actual size are inconsistent with numGlyphs.","commonSituations":"A TrueType font where the head, loca, and maxp tables are mutually inconsistent (e.g., numGlyphs was changed but loca wasn't rebuilt). Fonts edited by hex-editing tools without rebuilding table offsets. Corrupt fonts where the loca table was truncated. Fonts with a deliberately malformed indexToLocFormat that the sanitizer cannot auto-fix.","solutions":["Rebuild the font with fonttools: load and re-save to regenerate consistent head/loca/maxp tables.","Run ots-sanitize or fontbakery to identify and report the specific table inconsistency.","If numGlyphs is wrong, fix the maxp table's numGlyphs value to match the actual loca entry count.","Replace the corrupt embedded font with a clean copy."],"exampleFix":"# rebuild font to fix head/loca/maxp consistency\nfrom fontTools import ttLib\nfont = ttLib.TTFont('broken.ttf')\n# fontTools recalculates loca/head on save\nfont.save('fixed.ttf')","handlingStrategy":"validation","validationCode":"// Server-side: verify head.indexToLocFormat consistency with loca length\n// from fontTools import ttLib\n// font = ttLib.TTFont('font.ttf')\n// numGlyphs = font['maxp'].numGlyphs\n// locaLen = len(font['loca'])  # entries\n// fmt = font['head'].indexToLocFormat\n// if fmt not in (0, 1):\n//     if locaLen == numGlyphs + 1: pass  # can infer short\n//     elif locaLen == (numGlyphs + 1) * 2: pass  # can infer long\n//     else: print('Cannot fix indexToLocFormat')\n","typeGuard":null,"tryCatchPattern":"try {\n  await page.render(renderParams).promise;\n} catch (err) {\n  if (err.message?.includes('Could not fix indexToLocFormat')) {\n    console.error('Font head/loca/maxp tables are inconsistent; rebuild the font.');\n  } else { throw err; }\n}","preventionTips":["Always rebuild TrueType fonts with fonttools after editing to regenerate consistent head/loca/maxp.","Never hex-edit numGlyphs without rebuilding the loca table.","Validate fonts with ots-sanitize before embedding."],"tags":["font","head-table","loca-table","truetype","font-sanitization","format-error"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}