14
14
*/
15
15
16
16
import {
17
- bytesToString , FormatError , unreachable , Util
17
+ bytesToString , FONT_IDENTITY_MATRIX , FormatError , unreachable , Util , warn
18
18
} from '../shared/util' ;
19
19
import { CFFParser } from './cff_parser' ;
20
20
import { getGlyphsUnicode } from './glyphlist' ;
@@ -91,6 +91,9 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
91
91
subrs : ( cff . topDict . privateDict && cff . topDict . privateDict . subrsIndex &&
92
92
cff . topDict . privateDict . subrsIndex . objects ) ,
93
93
gsubrs : cff . globalSubrIndex && cff . globalSubrIndex . objects ,
94
+ isCFFCIDFont : cff . isCIDFont ,
95
+ fdSelect : cff . fdSelect ,
96
+ fdArray : cff . fdArray ,
94
97
} ;
95
98
}
96
99
@@ -293,7 +296,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
293
296
}
294
297
}
295
298
296
- function compileCharString ( code , cmds , font ) {
299
+ function compileCharString ( code , cmds , font , glyphId ) {
297
300
var stack = [ ] ;
298
301
var x = 0 , y = 0 ;
299
302
var stems = 0 ;
@@ -366,8 +369,28 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
366
369
}
367
370
break ;
368
371
case 10 : // callsubr
369
- n = stack . pop ( ) + font . subrsBias ;
370
- subrCode = font . subrs [ n ] ;
372
+ n = stack . pop ( ) ;
373
+ subrCode = null ;
374
+ if ( font . isCFFCIDFont ) {
375
+ let fdIndex = font . fdSelect . getFDIndex ( glyphId ) ;
376
+ if ( fdIndex >= 0 && fdIndex < font . fdArray . length ) {
377
+ let fontDict = font . fdArray [ fdIndex ] , subrs ;
378
+ if ( fontDict . privateDict && fontDict . privateDict . subrsIndex ) {
379
+ subrs = fontDict . privateDict . subrsIndex . objects ;
380
+ }
381
+ if ( subrs ) {
382
+ let numSubrs = subrs . length ;
383
+ // Add subroutine bias.
384
+ n += numSubrs < 1240 ? 107 :
385
+ ( numSubrs < 33900 ? 1131 : 32768 ) ;
386
+ subrCode = subrs [ n ] ;
387
+ }
388
+ } else {
389
+ warn ( 'Invalid fd index for glyph index.' ) ;
390
+ }
391
+ } else {
392
+ subrCode = font . subrs [ n + font . subrsBias ] ;
393
+ }
371
394
if ( subrCode ) {
372
395
parse ( subrCode ) ;
373
396
}
@@ -438,12 +461,14 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
438
461
cmds . push ( { cmd : 'translate' , args : [ x , y ] , } ) ;
439
462
var cmap = lookupCmap ( font . cmap , String . fromCharCode (
440
463
font . glyphNameMap [ StandardEncoding [ achar ] ] ) ) ;
441
- compileCharString ( font . glyphs [ cmap . glyphId ] , cmds , font ) ;
464
+ compileCharString ( font . glyphs [ cmap . glyphId ] , cmds , font ,
465
+ cmap . glyphId ) ;
442
466
cmds . push ( { cmd : 'restore' , } ) ;
443
467
444
468
cmap = lookupCmap ( font . cmap , String . fromCharCode (
445
469
font . glyphNameMap [ StandardEncoding [ bchar ] ] ) ) ;
446
- compileCharString ( font . glyphs [ cmap . glyphId ] , cmds , font ) ;
470
+ compileCharString ( font . glyphs [ cmap . glyphId ] , cmds , font ,
471
+ cmap . glyphId ) ;
447
472
}
448
473
return ;
449
474
case 18 : // hstemhm
@@ -603,7 +628,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
603
628
var cmap = lookupCmap ( this . cmap , unicode ) ;
604
629
var fn = this . compiledGlyphs [ cmap . glyphId ] ;
605
630
if ( ! fn ) {
606
- fn = this . compileGlyph ( this . glyphs [ cmap . glyphId ] ) ;
631
+ fn = this . compileGlyph ( this . glyphs [ cmap . glyphId ] , cmap . glyphId ) ;
607
632
this . compiledGlyphs [ cmap . glyphId ] = fn ;
608
633
}
609
634
if ( this . compiledCharCodeToGlyphId [ cmap . charCode ] === undefined ) {
@@ -612,17 +637,30 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
612
637
return fn ;
613
638
} ,
614
639
615
- compileGlyph ( code ) {
640
+ compileGlyph ( code , glyphId ) {
616
641
if ( ! code || code . length === 0 || code [ 0 ] === 14 ) {
617
642
return noop ;
618
643
}
619
644
645
+ let fontMatrix = this . fontMatrix ;
646
+ if ( this . isCFFCIDFont ) {
647
+ // Top DICT's FontMatrix can be ignored because CFFCompiler always
648
+ // removes it and copies to FDArray DICTs.
649
+ let fdIndex = this . fdSelect . getFDIndex ( glyphId ) ;
650
+ if ( fdIndex >= 0 && fdIndex < this . fdArray . length ) {
651
+ let fontDict = this . fdArray [ fdIndex ] ;
652
+ fontMatrix = fontDict . getByName ( 'FontMatrix' ) || FONT_IDENTITY_MATRIX ;
653
+ } else {
654
+ warn ( 'Invalid fd index for glyph index.' ) ;
655
+ }
656
+ }
657
+
620
658
var cmds = [ ] ;
621
659
cmds . push ( { cmd : 'save' , } ) ;
622
- cmds . push ( { cmd : 'transform' , args : this . fontMatrix . slice ( ) , } ) ;
660
+ cmds . push ( { cmd : 'transform' , args : fontMatrix . slice ( ) , } ) ;
623
661
cmds . push ( { cmd : 'scale' , args : [ 'size' , '-size' ] , } ) ;
624
662
625
- this . compileGlyphImpl ( code , cmds ) ;
663
+ this . compileGlyphImpl ( code , cmds , glyphId ) ;
626
664
627
665
cmds . push ( { cmd : 'restore' , } ) ;
628
666
@@ -668,11 +706,15 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
668
706
107 : ( this . gsubrs . length < 33900 ? 1131 : 32768 ) ) ;
669
707
this . subrsBias = ( this . subrs . length < 1240 ?
670
708
107 : ( this . subrs . length < 33900 ? 1131 : 32768 ) ) ;
709
+
710
+ this . isCFFCIDFont = cffInfo . isCFFCIDFont ;
711
+ this . fdSelect = cffInfo . fdSelect ;
712
+ this . fdArray = cffInfo . fdArray ;
671
713
}
672
714
673
715
Util . inherit ( Type2Compiled , CompiledFont , {
674
- compileGlyphImpl ( code , cmds ) {
675
- compileCharString ( code , cmds , this ) ;
716
+ compileGlyphImpl ( code , cmds , glyphId ) {
717
+ compileCharString ( code , cmds , this , glyphId ) ;
676
718
} ,
677
719
} ) ;
678
720
0 commit comments