@@ -80,6 +80,7 @@ var getSupplementalGlyphMapForArialBlack =
8080 coreStandardFonts . getSupplementalGlyphMapForArialBlack ;
8181var getUnicodeRangeFor = coreUnicode . getUnicodeRangeFor ;
8282var mapSpecialUnicodeValues = coreUnicode . mapSpecialUnicodeValues ;
83+ var getUnicodeForGlyph = coreUnicode . getUnicodeForGlyph ;
8384
8485// Unicode Private Use Area
8586var PRIVATE_USE_OFFSET_START = 0xE000 ;
@@ -465,7 +466,7 @@ var ProblematicCharRanges = new Int32Array([
465466 */
466467var Font = ( function FontClosure ( ) {
467468 function Font ( name , file , properties ) {
468- var charCode , glyphName , fontChar ;
469+ var charCode , glyphName , unicode , fontChar ;
469470
470471 this . name = name ;
471472 this . loadedName = properties . loadedName ;
@@ -609,21 +610,25 @@ var Font = (function FontClosure() {
609610 this . toFontChar [ charCode ] = fontChar ;
610611 }
611612 } else if ( isStandardFont ) {
612- this . toFontChar = [ ] ;
613613 glyphsUnicodeMap = getGlyphsUnicode ( ) ;
614614 for ( charCode in properties . defaultEncoding ) {
615615 glyphName = ( properties . differences [ charCode ] ||
616616 properties . defaultEncoding [ charCode ] ) ;
617- this . toFontChar [ charCode ] = glyphsUnicodeMap [ glyphName ] ;
617+ unicode = getUnicodeForGlyph ( glyphName , glyphsUnicodeMap ) ;
618+ if ( unicode !== - 1 ) {
619+ this . toFontChar [ charCode ] = unicode ;
620+ }
618621 }
619622 } else {
620- var unicodeCharCode , notCidFont = ( type . indexOf ( 'CIDFontType' ) === - 1 ) ;
621623 glyphsUnicodeMap = getGlyphsUnicode ( ) ;
622624 this . toUnicode . forEach ( function ( charCode , unicodeCharCode ) {
623- if ( notCidFont ) {
625+ if ( ! this . composite ) {
624626 glyphName = ( properties . differences [ charCode ] ||
625627 properties . defaultEncoding [ charCode ] ) ;
626- unicodeCharCode = ( glyphsUnicodeMap [ glyphName ] || unicodeCharCode ) ;
628+ unicode = getUnicodeForGlyph ( glyphName , glyphsUnicodeMap ) ;
629+ if ( unicode !== - 1 ) {
630+ unicodeCharCode = unicode ;
631+ }
627632 }
628633 this . toFontChar [ charCode ] = unicodeCharCode ;
629634 } . bind ( this ) ) ;
@@ -722,7 +727,7 @@ var Font = (function FontClosure() {
722727 function int16 ( b0 , b1 ) {
723728 return ( b0 << 8 ) + b1 ;
724729 }
725-
730+
726731 function signedInt16 ( b0 , b1 ) {
727732 var value = ( b0 << 8 ) + b1 ;
728733 return value & ( 1 << 15 ) ? value - 0x10000 : value ;
@@ -2283,6 +2288,26 @@ var Font = (function FontClosure() {
22832288 return false ;
22842289 }
22852290
2291+ // Some bad PDF generators, e.g. Scribus PDF, include glyph names
2292+ // in a 'uniXXXX' format -- attempting to recover proper ones.
2293+ function recoverGlyphName ( name , glyphsUnicodeMap ) {
2294+ if ( glyphsUnicodeMap [ name ] !== undefined ) {
2295+ return name ;
2296+ }
2297+ // The glyph name is non-standard, trying to recover.
2298+ var unicode = getUnicodeForGlyph ( name , glyphsUnicodeMap ) ;
2299+ if ( unicode !== - 1 ) {
2300+ for ( var key in glyphsUnicodeMap ) {
2301+ if ( glyphsUnicodeMap [ key ] === unicode ) {
2302+ return key ;
2303+ }
2304+ }
2305+ }
2306+ warn ( 'Unable to recover a standard glyph name for: ' + name ) ;
2307+ return name ;
2308+ }
2309+
2310+
22862311 if ( properties . type === 'CIDFontType2' ) {
22872312 var cidToGidMap = properties . cidToGidMap || [ ] ;
22882313 var isCidToGidMapEmpty = cidToGidMap . length === 0 ;
@@ -2337,7 +2362,7 @@ var Font = (function FontClosure() {
23372362 }
23382363 var glyphsUnicodeMap = getGlyphsUnicode ( ) ;
23392364 for ( charCode = 0 ; charCode < 256 ; charCode ++ ) {
2340- var glyphName ;
2365+ var glyphName , standardGlyphName ;
23412366 if ( this . differences && charCode in this . differences ) {
23422367 glyphName = this . differences [ charCode ] ;
23432368 } else if ( charCode in baseEncoding &&
@@ -2349,13 +2374,16 @@ var Font = (function FontClosure() {
23492374 if ( ! glyphName ) {
23502375 continue ;
23512376 }
2377+ // Ensure that non-standard glyph names are resolved to valid ones.
2378+ standardGlyphName = recoverGlyphName ( glyphName , glyphsUnicodeMap ) ;
2379+
23522380 var unicodeOrCharCode , isUnicode = false ;
23532381 if ( cmapPlatformId === 3 && cmapEncodingId === 1 ) {
2354- unicodeOrCharCode = glyphsUnicodeMap [ glyphName ] ;
2382+ unicodeOrCharCode = glyphsUnicodeMap [ standardGlyphName ] ;
23552383 isUnicode = true ;
23562384 } else if ( cmapPlatformId === 1 && cmapEncodingId === 0 ) {
23572385 // TODO: the encoding needs to be updated with mac os table.
2358- unicodeOrCharCode = MacRomanEncoding . indexOf ( glyphName ) ;
2386+ unicodeOrCharCode = MacRomanEncoding . indexOf ( standardGlyphName ) ;
23592387 }
23602388
23612389 var found = false ;
@@ -2373,6 +2401,11 @@ var Font = (function FontClosure() {
23732401 if ( ! found && properties . glyphNames ) {
23742402 // Try to map using the post table.
23752403 var glyphId = properties . glyphNames . indexOf ( glyphName ) ;
2404+ // The post table ought to use the same kind of glyph names as the
2405+ // `differences` array, but check the standard ones as a fallback.
2406+ if ( glyphId === - 1 && standardGlyphName !== glyphName ) {
2407+ glyphId = properties . glyphNames . indexOf ( standardGlyphName ) ;
2408+ }
23762409 if ( glyphId > 0 && hasGlyph ( glyphId , - 1 , - 1 ) ) {
23772410 charCodeToGlyphId [ charCode ] = glyphId ;
23782411 found = true ;
@@ -2686,6 +2719,12 @@ var Font = (function FontClosure() {
26862719 code = + glyphName . substr ( 1 ) ;
26872720 }
26882721 break ;
2722+ default :
2723+ // 'uniXXXX'/'uXXXX{XX}' glyphs
2724+ var unicode = getUnicodeForGlyph ( glyphName , glyphsUnicodeMap ) ;
2725+ if ( unicode !== - 1 ) {
2726+ code = unicode ;
2727+ }
26892728 }
26902729 if ( code ) {
26912730 // If |baseEncodingName| is one the predefined encodings,
0 commit comments