function adjustMapping(charCodeToGlyphId, hasGlyph, newGlyphZeroId, toUnicode) {
  const newMap = Object.create(null);
  const toUnicodeExtraMap = new Map();
  const toFontChar = [];
  const usedGlyphIds = new Set();
  let privateUseAreaIndex = 0;
  const privateUseOffetStart = PRIVATE_USE_AREAS[privateUseAreaIndex][0];
  let nextAvailableFontCharCode = privateUseOffetStart;
  let privateUseOffetEnd = PRIVATE_USE_AREAS[privateUseAreaIndex][1];
  const isInPrivateArea = code =>
    (PRIVATE_USE_AREAS[0][0] <= code && code <= PRIVATE_USE_AREAS[0][1]) ||
    (PRIVATE_USE_AREAS[1][0] <= code && code <= PRIVATE_USE_AREAS[1][1]);
  let LIGATURE_TO_UNICODE = null;

  for (const originalCharCode in charCodeToGlyphId) {
    let glyphId = charCodeToGlyphId[originalCharCode];
    // For missing glyphs don't create the mappings so the glyph isn't
    // drawn.
    if (!hasGlyph(glyphId)) {
      continue;
    }
    if (nextAvailableFontCharCode > privateUseOffetEnd) {
      privateUseAreaIndex++;
      if (privateUseAreaIndex >= PRIVATE_USE_AREAS.length) {
        warn("Ran out of space in font private use area.");
        break;
      }
      nextAvailableFontCharCode = PRIVATE_USE_AREAS[privateUseAreaIndex][0];
      privateUseOffetEnd = PRIVATE_USE_AREAS[privateUseAreaIndex][1];
    }
    const fontCharCode = nextAvailableFontCharCode++;
    if (glyphId === 0) {
      glyphId = newGlyphZeroId;
    }

    // Fix for bug 1778484:
    // The charcodes are moved into a private use area to fix some rendering
    // issues (https://github.com/mozilla/pdf.js/pull/9340) but when printing
    // to PDF the generated font will contain wrong chars. We can avoid that by
    // adding the unicode to the cmap and the print backend will then map the
    // glyph ids to the correct unicode.
    let unicode = toUnicode.get(originalCharCode);
    if (typeof unicode === "string") {
      if (unicode.length === 1) {
        unicode = unicode.codePointAt(0);
      } else {
        if (!LIGATURE_TO_UNICODE) {
          LIGATURE_TO_UNICODE = new Map();
          // The code range [0xfb00, 0xfb4f] contains some ligature characters
          // but not all.
          // See https://www.compart.com/en/unicode/block/U+FB00.
          for (let i = 0xfb00; i <= 0xfb4f; i++) {
            const normalized = String.fromCharCode(i).normalize("NFKD");
            if (normalized.length > 1) {
              LIGATURE_TO_UNICODE.set(normalized, i);
            }
          }
        }
        unicode = LIGATURE_TO_UNICODE.get(unicode) || unicode.codePointAt(0);
      }
    }
    if (unicode && !isInPrivateArea(unicode) && !usedGlyphIds.has(glyphId)) {
      toUnicodeExtraMap.set(unicode, glyphId);
      usedGlyphIds.add(glyphId);
    }

    newMap[fontCharCode] = glyphId;
    toFontChar[originalCharCode] = fontCharCode;
  }
  return {
    toFontChar,
    charCodeToGlyphId: newMap,
    toUnicodeExtraMap,
    nextAvailableFontCharCode,
  };
}
Rebuilds the char code to glyph ID map by moving all char codes to the
private use area. This is done to avoid issues with various problematic
unicode areas where either a glyph won't be drawn or is deformed by a
shaper.
@returns {Object} Two properties:
'toFontChar' - maps original char codes(the value that will be read
from commands such as show text) to the char codes that will be used in the
font that we build
'charCodeToGlyphId' - maps the new font char codes to glyph ids