{"record":{"id":"fb170e84d5b13d23","repo":"mozilla/pdf.js","slug":"max-size-of-cid-is-65-535","errorCode":null,"errorMessage":"Max size of CID is 65,535","messagePattern":"Max size of CID is 65,535","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/evaluator.js","lineNumber":3988,"sourceCode":"      // by the font’s CMap (for example, Adobe and Japan1) from its\n      // CIDSystemInfo dictionary.\n      const { registry, ordering } = properties.cidSystemInfo;\n      // c) Construct a second CMap name by concatenating the registry and\n      // ordering obtained in step (b) in the format registry–ordering–UCS2\n      // (for example, Adobe–Japan1–UCS2).\n      const ucs2CMapName = Name.get(`${registry}-${ordering}-UCS2`);\n      // d) Obtain the CMap with the name constructed in step (c) (available\n      // from the ASN Web site; see the Bibliography).\n      const ucs2CMap = await CMapFactory.create({\n        encoding: ucs2CMapName,\n        fetchBuiltInCMap: this._fetchBuiltInCMapBound,\n        useCMap: null,\n      });\n      const toUnicode = [],\n        buf = [];\n      properties.cMap.forEach(function (charcode, cid) {\n        if (cid > 0xffff) {\n          throw new FormatError(\"Max size of CID is 65,535\");\n        }\n        // e) Map the CID obtained in step (a) according to the CMap\n        // obtained in step (d), producing a Unicode value.\n        const ucs2 = ucs2CMap.lookup(cid);\n        if (ucs2) {\n          buf.length = 0;\n          // Support multi-byte entries (fixes issue16176.pdf).\n          for (let i = 0, ii = ucs2.length; i < ii; i += 2) {\n            buf.push((ucs2.charCodeAt(i) << 8) + ucs2.charCodeAt(i + 1));\n          }\n          toUnicode[charcode] = String.fromCharCode(...buf);\n        }\n      });\n      return new ToUnicodeMap(toUnicode);\n    }\n\n    // The viewer's choice, just use an identity map.\n    return new IdentityToUnicodeMap(properties.firstChar, properties.lastChar);","sourceCodeStart":3970,"sourceCodeEnd":4006,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/evaluator.js#L3970-L4006","documentation":"Thrown while building the ToUnicode map for a CID font when iterating the font's CMap and encountering a character identifier (CID) greater than 0xFFFF (65,535). CID values are 16-bit by definition; a larger value indicates a corrupt CMap or a CIDSystemInfo/registry mismatch. The check guards against array-index overflow in the toUnicode lookup table.","triggerScenarios":"A composite (Type0/CID) font whose CMap yields a CID > 65535 during the registry-ordering-UCS2 mapping step. Fires in buildToUnicode() inside PartialEvaluator, only for fonts whose CIDSystemInfo is one of the standard Adobe collections (GB1, CNS1, Japan1, Korea1).","commonSituations":"Corrupt or hand-built CMap data, a CID font whose CMap was concatenated with a wrong/use CMap, or a generator that emitted non-truncated CID ranges. Rare in the wild but seen in PDFs produced by experimental or academic CJK typesetting tools.","solutions":["Repair or regenerate the CMap data in the offending font; ensure CID values fit in 16 bits.","Re-export the PDF through Ghostscript (gs -sDEVICE=pdfwrite) which normalizes CID/CMap structures.","Catch the error per-page and continue rendering with an incomplete ToUnicode map (search/copy fidelity will suffer).","If authoring CJK content with a custom tool, validate CID ranges against the relevant Adobe CMap resource before embedding."],"exampleFix":"// before\nawait page.getTextContent(); // throws if a CID > 0xFFFF\n\n// after\ntry {\n  await page.getTextContent();\n} catch (e) {\n  if (/Max size of CID/.test(e.message)) {\n    console.warn('CID font corrupt, text extraction will be partial');\n  } else throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await page.getTextContent();\n} catch (e) {\n  if (e.name === 'FormatError' && /Max size of CID/.test(e.message)) {\n    // CID font corrupt; accept partial text extraction\n  } else throw e;\n}","preventionTips":["Validate embedded CMap data against the relevant Adobe CMap spec for CID ranges before publishing CJK PDFs.","Re-export CJK PDFs through Ghostscript to normalize CID/CMap structures.","Isolate per-page operations so one corrupt CID font does not block other pages."],"tags":["pdf","font","cid","cmap","cjk","format-error"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}