Skip to content

Commit b81acfc

Browse files
committed
Add unicode mapping in the font cmap to have correct chars when printing in pdf (bug 1778484)
It aims to fix https://bugzilla.mozilla.org/show_bug.cgi?id=1778484.
1 parent 220f980 commit b81acfc

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/core/fonts.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,11 @@ function convertCidString(charCode, cid, shouldThrow = false) {
433433
* font that we build
434434
* 'charCodeToGlyphId' - maps the new font char codes to glyph ids
435435
*/
436-
function adjustMapping(charCodeToGlyphId, hasGlyph, newGlyphZeroId) {
436+
function adjustMapping(charCodeToGlyphId, hasGlyph, newGlyphZeroId, toUnicode) {
437437
const newMap = Object.create(null);
438+
const completeMap = Object.create(null);
438439
const toFontChar = [];
440+
const usedGlyphIds = new Set();
439441
let privateUseAreaIndex = 0;
440442
let nextAvailableFontCharCode = PRIVATE_USE_AREAS[privateUseAreaIndex][0];
441443
let privateUseOffetEnd = PRIVATE_USE_AREAS[privateUseAreaIndex][1];
@@ -461,12 +463,28 @@ function adjustMapping(charCodeToGlyphId, hasGlyph, newGlyphZeroId) {
461463
glyphId = newGlyphZeroId;
462464
}
463465

464-
newMap[fontCharCode] = glyphId;
466+
// Fix for bug 1778484:
467+
// The charcodes are moved into the private area in to fix some rendering
468+
// issues (https://github.com/mozilla/pdf.js/pull/9340) but when printing
469+
// into pdf the generated will contains wrong chars. We can avoid that in
470+
// adding the unicode to the cmap and the print backend will then map the
471+
// glyph ids to the correct unicode.
472+
let unicode = toUnicode.get(originalCharCode);
473+
if (typeof unicode === "string") {
474+
unicode = unicode.codePointAt(0);
475+
}
476+
if (unicode && !usedGlyphIds.has(glyphId)) {
477+
completeMap[unicode] = glyphId;
478+
usedGlyphIds.add(glyphId);
479+
}
480+
481+
newMap[fontCharCode] = completeMap[fontCharCode] = glyphId;
465482
toFontChar[originalCharCode] = fontCharCode;
466483
}
467484
return {
468485
toFontChar,
469486
charCodeToGlyphId: newMap,
487+
completeCharCodeToGlyphId: completeMap,
470488
nextAvailableFontCharCode,
471489
};
472490
}
@@ -2914,12 +2932,16 @@ class Font {
29142932
const newMapping = adjustMapping(
29152933
charCodeToGlyphId,
29162934
hasGlyph,
2917-
glyphZeroId
2935+
glyphZeroId,
2936+
this.toUnicode
29182937
);
29192938
this.toFontChar = newMapping.toFontChar;
29202939
tables.cmap = {
29212940
tag: "cmap",
2922-
data: createCmapTable(newMapping.charCodeToGlyphId, numGlyphsOut),
2941+
data: createCmapTable(
2942+
newMapping.completeCharCodeToGlyphId,
2943+
numGlyphsOut
2944+
),
29232945
};
29242946

29252947
if (!tables["OS/2"] || !validateOS2Table(tables["OS/2"], font)) {
@@ -2992,17 +3014,20 @@ class Font {
29923014
const mapping = font.getGlyphMapping(properties);
29933015
let newMapping = null;
29943016
let newCharCodeToGlyphId = mapping;
3017+
let newCompleteCharCodeToGlyphId = mapping;
29953018

29963019
// When `cssFontInfo` is set, the font is used to render text in the HTML
29973020
// view (e.g. with Xfa) so nothing must be moved in the private use area.
29983021
if (!properties.cssFontInfo) {
29993022
newMapping = adjustMapping(
30003023
mapping,
30013024
font.hasGlyphId.bind(font),
3002-
glyphZeroId
3025+
glyphZeroId,
3026+
this.toUnicode
30033027
);
30043028
this.toFontChar = newMapping.toFontChar;
30053029
newCharCodeToGlyphId = newMapping.charCodeToGlyphId;
3030+
newCompleteCharCodeToGlyphId = newMapping.completeCharCodeToGlyphId;
30063031
}
30073032
const numGlyphs = font.numGlyphs;
30083033

@@ -3087,7 +3112,10 @@ class Font {
30873112
// OS/2 and Windows Specific metrics
30883113
builder.addTable("OS/2", createOS2Table(properties, newCharCodeToGlyphId));
30893114
// Character to glyphs mapping
3090-
builder.addTable("cmap", createCmapTable(newCharCodeToGlyphId, numGlyphs));
3115+
builder.addTable(
3116+
"cmap",
3117+
createCmapTable(newCompleteCharCodeToGlyphId, numGlyphs)
3118+
);
30913119
// Font header
30923120
builder.addTable(
30933121
"head",

0 commit comments

Comments
 (0)