@@ -433,8 +433,9 @@ function convertCidString(charCode, cid, shouldThrow = false) {
433
433
* font that we build
434
434
* 'charCodeToGlyphId' - maps the new font char codes to glyph ids
435
435
*/
436
- function adjustMapping ( charCodeToGlyphId , hasGlyph , newGlyphZeroId ) {
436
+ function adjustMapping ( charCodeToGlyphId , hasGlyph , newGlyphZeroId , toUnicode ) {
437
437
const newMap = Object . create ( null ) ;
438
+ const completeMap = Object . create ( null ) ;
438
439
const toFontChar = [ ] ;
439
440
let privateUseAreaIndex = 0 ;
440
441
let nextAvailableFontCharCode = PRIVATE_USE_AREAS [ privateUseAreaIndex ] [ 0 ] ;
@@ -461,12 +462,27 @@ function adjustMapping(charCodeToGlyphId, hasGlyph, newGlyphZeroId) {
461
462
glyphId = newGlyphZeroId ;
462
463
}
463
464
464
- newMap [ fontCharCode ] = glyphId ;
465
+ // Fix for bug 1778484:
466
+ // The charcodes are moved into the private area in to fix some rendering
467
+ // issues (https://github.com/mozilla/pdf.js/pull/9340) but when printing
468
+ // into pdf the generated will contains wrong chars. We can avoid that in
469
+ // adding the unicode to the cmap and the print backend will then map the
470
+ // glyph ids to the correct unicode.
471
+ let unicode = toUnicode . get ( originalCharCode ) ;
472
+ if ( typeof unicode === "string" ) {
473
+ unicode = unicode . codePointAt ( 0 ) ;
474
+ }
475
+ if ( unicode ) {
476
+ completeMap [ unicode ] = glyphId ;
477
+ }
478
+
479
+ newMap [ fontCharCode ] = completeMap [ fontCharCode ] = glyphId ;
465
480
toFontChar [ originalCharCode ] = fontCharCode ;
466
481
}
467
482
return {
468
483
toFontChar,
469
484
charCodeToGlyphId : newMap ,
485
+ completeCharCodeToGlyphId : completeMap ,
470
486
nextAvailableFontCharCode,
471
487
} ;
472
488
}
@@ -2914,12 +2930,16 @@ class Font {
2914
2930
const newMapping = adjustMapping (
2915
2931
charCodeToGlyphId ,
2916
2932
hasGlyph ,
2917
- glyphZeroId
2933
+ glyphZeroId ,
2934
+ this . toUnicode
2918
2935
) ;
2919
2936
this . toFontChar = newMapping . toFontChar ;
2920
2937
tables . cmap = {
2921
2938
tag : "cmap" ,
2922
- data : createCmapTable ( newMapping . charCodeToGlyphId , numGlyphsOut ) ,
2939
+ data : createCmapTable (
2940
+ newMapping . completeCharCodeToGlyphId ,
2941
+ numGlyphsOut
2942
+ ) ,
2923
2943
} ;
2924
2944
2925
2945
if ( ! tables [ "OS/2" ] || ! validateOS2Table ( tables [ "OS/2" ] , font ) ) {
@@ -2992,17 +3012,20 @@ class Font {
2992
3012
const mapping = font . getGlyphMapping ( properties ) ;
2993
3013
let newMapping = null ;
2994
3014
let newCharCodeToGlyphId = mapping ;
3015
+ let newCompleteCharCodeToGlyphId = mapping ;
2995
3016
2996
3017
// When `cssFontInfo` is set, the font is used to render text in the HTML
2997
3018
// view (e.g. with Xfa) so nothing must be moved in the private use area.
2998
3019
if ( ! properties . cssFontInfo ) {
2999
3020
newMapping = adjustMapping (
3000
3021
mapping ,
3001
3022
font . hasGlyphId . bind ( font ) ,
3002
- glyphZeroId
3023
+ glyphZeroId ,
3024
+ this . toUnicode
3003
3025
) ;
3004
3026
this . toFontChar = newMapping . toFontChar ;
3005
3027
newCharCodeToGlyphId = newMapping . charCodeToGlyphId ;
3028
+ newCompleteCharCodeToGlyphId = newMapping . completeCharCodeToGlyphId ;
3006
3029
}
3007
3030
const numGlyphs = font . numGlyphs ;
3008
3031
@@ -3087,7 +3110,10 @@ class Font {
3087
3110
// OS/2 and Windows Specific metrics
3088
3111
builder . addTable ( "OS/2" , createOS2Table ( properties , newCharCodeToGlyphId ) ) ;
3089
3112
// Character to glyphs mapping
3090
- builder . addTable ( "cmap" , createCmapTable ( newCharCodeToGlyphId , numGlyphs ) ) ;
3113
+ builder . addTable (
3114
+ "cmap" ,
3115
+ createCmapTable ( newCompleteCharCodeToGlyphId , numGlyphs )
3116
+ ) ;
3091
3117
// Font header
3092
3118
builder . addTable (
3093
3119
"head" ,
0 commit comments