{"record":{"id":"d8d9e328d043cb66","repo":"mozilla/pdf.js","slug":"cmap-was-not-found","errorCode":null,"errorMessage":"cmap was not found","messagePattern":"cmap was not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"external/cmapscompress/parse.mjs","lineNumber":19,"sourceCode":"/* Copyright 2014 Mozilla Foundation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nfunction parseAdobeCMap(content) {\n  let m = /(\\bbegincmap\\b[\\s\\S]+?)\\bendcmap\\b/.exec(content);\n  if (!m) {\n    throw new Error(\"cmap was not found\");\n  }\n\n  const body = m[1].replaceAll(/\\r\\n?/g, \"\\n\");\n  const result = {\n    type: 1,\n    wmode: 0,\n    comment:\n      \"Copyright 1990-2009 Adobe Systems Incorporated.\\nAll rights reserved.\\nSee ./LICENSE\",\n    usecmap: null,\n    body: [],\n  };\n  m = /\\/CMapType\\s+(\\d+)\\s+def\\b/.exec(body);\n  result.type = +m[1];\n  m = /\\/WMode\\s+(\\d+)\\s+def\\b/.exec(body);\n  result.wmode = +m[1];\n  m = /\\/([\\w-]+)\\s+usecmap\\b/.exec(body);\n  if (m) {\n    result.usecmap = m[1];","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/external/cmapscompress/parse.mjs#L1-L37","documentation":"parseAdobeCMap() expects Adobe's text CMap format, which must contain a `begincmap` ... `endcmap` block. The regex `/\\bbegincmap\\b[\\s\\S]+?\\bendcmap\\b/` returning null means the file is not a CMap at all, is truncated before the markers, or uses a different dialect. The parser cannot proceed because everything it extracts (CMapType, WMode, body) lives inside that block. This is the text-side counterpart of the bcmap type error and is the first integrity check on CMap source files.","triggerScenarios":"Passing a PostScript fragment, a CIDFont wrapper, an empty file, or a binary bcmap to parseAdobeCMap; a CMap file whose `begincmap`/`endcmap` keywords were altered or stripped; reading a file with the wrong encoding that defeats the word-boundary regex.","commonSituations":"Pointing `gulp cmaps` (or a custom compress run) at a directory containing non-CMap text files; a hand-downloaded CMap resource that was truncated; line-ending or BOM issues that break the regex (note the function normalizes CRLF but a leading BOM before `begincmap` could still matter).","solutions":["Confirm the input is an Adobe text CMap: it should literally contain the tokens `begincmap` and `endcmap`.","Redownload the CMap resource from https://github.com/adobe-type-tools/cmap-resources and re-extract cleanly.","If processing a batch, skip non-CMap files (filter by a quick `includes('begincmap')` test) before calling parseAdobeCMap.","Check the file is not empty or truncated (`ls -l`, `wc -l`)."],"exampleFix":"// before\nconst parsed = parseAdobeCMap(fs.readFileSync('external/cmaps/somefile', 'utf8'));\n// after\nconst content = fs.readFileSync('external/cmaps/UniJIS-UCS2-H', 'utf8');\nif (!/\\bbegincmap\\b[\\s\\S]+\\bendcmap\\b/.test(content)) {\n  throw new Error(`${file} is not a valid Adobe CMap text file`);\n}\nconst parsed = parseAdobeCMap(content);","handlingStrategy":"validation","validationCode":"function parseCMapSafe(content, name = '<cmap>') {\n  if (!/\\bbegincmap\\b[\\s\\S]+?\\bendcmap\\b/.test(content)) {\n    throw new Error(`${name} is not an Adobe CMap (no begincmap/endcmap block)`);\n  }\n  return parseAdobeCMap(content);\n}","typeGuard":"function isCMapText(content) {\n  return typeof content === 'string' && /\\bbegincmap\\b/.test(content) && /\\bendcmap\\b/.test(content);\n}","tryCatchPattern":"try {\n  parsed = parseAdobeCMap(content);\n} catch (e) {\n  if (/cmap was not found/.test(e.message)) {\n    console.warn(`skipping non-CMap file ${file}`);\n    continue;\n  }\n  throw e;\n}","preventionTips":["When batch-processing a directory, filter files by a `begincmap` presence test first.","Always source CMaps from adobe-type-tools/cmap-resources; don't hand-author.","Check file size before parsing — a near-empty file is never a valid CMap."],"tags":["cmaps","parsing","validation","build"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}