16
16
import {
17
17
bytesToString ,
18
18
FONT_IDENTITY_MATRIX ,
19
+ FontRenderOps ,
19
20
FormatError ,
20
21
unreachable ,
21
22
warn ,
@@ -180,13 +181,13 @@ function lookupCmap(ranges, unicode) {
180
181
181
182
function compileGlyf ( code , cmds , font ) {
182
183
function moveTo ( x , y ) {
183
- cmds . push ( { cmd : "moveTo" , args : [ x , y ] } ) ;
184
+ cmds . add ( FontRenderOps . MOVE_TO , [ x , y ] ) ;
184
185
}
185
186
function lineTo ( x , y ) {
186
- cmds . push ( { cmd : "lineTo" , args : [ x , y ] } ) ;
187
+ cmds . add ( FontRenderOps . LINE_TO , [ x , y ] ) ;
187
188
}
188
189
function quadraticCurveTo ( xa , ya , x , y ) {
189
- cmds . push ( { cmd : "quadraticCurveTo" , args : [ xa , ya , x , y ] } ) ;
190
+ cmds . add ( FontRenderOps . QUADRATIC_CURVE_TO , [ xa , ya , x , y ] ) ;
190
191
}
191
192
192
193
let i = 0 ;
@@ -247,20 +248,22 @@ function compileGlyf(code, cmds, font) {
247
248
if ( subglyph ) {
248
249
// TODO: the transform should be applied only if there is a scale:
249
250
// https://github.com/freetype/freetype/blob/edd4fedc5427cf1cf1f4b045e53ff91eb282e9d4/src/truetype/ttgload.c#L1205
250
- cmds . push (
251
- { cmd : "save" } ,
252
- {
253
- cmd : "transform" ,
254
- args : [ scaleX , scale01 , scale10 , scaleY , x , y ] ,
255
- }
256
- ) ;
251
+ cmds . add ( FontRenderOps . SAVE ) ;
252
+ cmds . add ( FontRenderOps . TRANSFORM , [
253
+ scaleX ,
254
+ scale01 ,
255
+ scale10 ,
256
+ scaleY ,
257
+ x ,
258
+ y ,
259
+ ] ) ;
257
260
258
261
if ( ! ( flags & 0x02 ) ) {
259
262
// TODO: we must use arg1 and arg2 to make something similar to:
260
263
// https://github.com/freetype/freetype/blob/edd4fedc5427cf1cf1f4b045e53ff91eb282e9d4/src/truetype/ttgload.c#L1209
261
264
}
262
265
compileGlyf ( subglyph , cmds , font ) ;
263
- cmds . push ( { cmd : "restore" } ) ;
266
+ cmds . add ( FontRenderOps . RESTORE ) ;
264
267
}
265
268
} while ( flags & 0x20 ) ;
266
269
} else {
@@ -365,13 +368,13 @@ function compileGlyf(code, cmds, font) {
365
368
366
369
function compileCharString ( charStringCode , cmds , font , glyphId ) {
367
370
function moveTo ( x , y ) {
368
- cmds . push ( { cmd : "moveTo" , args : [ x , y ] } ) ;
371
+ cmds . add ( FontRenderOps . MOVE_TO , [ x , y ] ) ;
369
372
}
370
373
function lineTo ( x , y ) {
371
- cmds . push ( { cmd : "lineTo" , args : [ x , y ] } ) ;
374
+ cmds . add ( FontRenderOps . LINE_TO , [ x , y ] ) ;
372
375
}
373
376
function bezierCurveTo ( x1 , y1 , x2 , y2 , x , y ) {
374
- cmds . push ( { cmd : "bezierCurveTo" , args : [ x1 , y1 , x2 , y2 , x , y ] } ) ;
377
+ cmds . add ( FontRenderOps . BEZIER_CURVE_TO , [ x1 , y1 , x2 , y2 , x , y ] ) ;
375
378
}
376
379
377
380
const stack = [ ] ;
@@ -544,7 +547,8 @@ function compileCharString(charStringCode, cmds, font, glyphId) {
544
547
const bchar = stack . pop ( ) ;
545
548
y = stack . pop ( ) ;
546
549
x = stack . pop ( ) ;
547
- cmds . push ( { cmd : "save" } , { cmd : "translate" , args : [ x , y ] } ) ;
550
+ cmds . add ( FontRenderOps . SAVE ) ;
551
+ cmds . add ( FontRenderOps . TRANSLATE , [ x , y ] ) ;
548
552
let cmap = lookupCmap (
549
553
font . cmap ,
550
554
String . fromCharCode ( font . glyphNameMap [ StandardEncoding [ achar ] ] )
@@ -555,7 +559,7 @@ function compileCharString(charStringCode, cmds, font, glyphId) {
555
559
font ,
556
560
cmap . glyphId
557
561
) ;
558
- cmds . push ( { cmd : "restore" } ) ;
562
+ cmds . add ( FontRenderOps . RESTORE ) ;
559
563
560
564
cmap = lookupCmap (
561
565
font . cmap ,
@@ -741,6 +745,27 @@ function compileCharString(charStringCode, cmds, font, glyphId) {
741
745
742
746
const NOOP = [ ] ;
743
747
748
+ class Commands {
749
+ cmds = [ ] ;
750
+
751
+ add ( cmd , args ) {
752
+ if ( args ) {
753
+ if ( args . some ( arg => typeof arg !== "number" ) ) {
754
+ warn (
755
+ `Commands.add - "${ cmd } " has at least one non-number arg: "${ args } ".`
756
+ ) ;
757
+ // "Fix" the wrong args by replacing them with 0.
758
+ const newArgs = args . map ( arg => ( typeof arg === "number" ? arg : 0 ) ) ;
759
+ this . cmds . push ( cmd , ...newArgs ) ;
760
+ } else {
761
+ this . cmds . push ( cmd , ...args ) ;
762
+ }
763
+ } else {
764
+ this . cmds . push ( cmd ) ;
765
+ }
766
+ }
767
+ }
768
+
744
769
class CompiledFont {
745
770
constructor ( fontMatrix ) {
746
771
if ( this . constructor === CompiledFont ) {
@@ -757,8 +782,10 @@ class CompiledFont {
757
782
let fn = this . compiledGlyphs [ glyphId ] ;
758
783
if ( ! fn ) {
759
784
try {
760
- fn = this . compileGlyph ( this . glyphs [ glyphId ] , glyphId ) ;
761
- this . compiledGlyphs [ glyphId ] = fn ;
785
+ fn = this . compiledGlyphs [ glyphId ] = this . compileGlyph (
786
+ this . glyphs [ glyphId ] ,
787
+ glyphId
788
+ ) ;
762
789
} catch ( ex ) {
763
790
// Avoid attempting to re-compile a corrupt glyph.
764
791
this . compiledGlyphs [ glyphId ] = NOOP ;
@@ -793,16 +820,14 @@ class CompiledFont {
793
820
}
794
821
}
795
822
796
- const cmds = [
797
- { cmd : "save" } ,
798
- { cmd : "transform" , args : fontMatrix . slice ( ) } ,
799
- { cmd : "scale" , args : [ "size" , "-size" ] } ,
800
- ] ;
823
+ const cmds = new Commands ( ) ;
824
+ cmds . add ( FontRenderOps . SAVE ) ;
825
+ cmds . add ( FontRenderOps . TRANSFORM , fontMatrix . slice ( ) ) ;
826
+ cmds . add ( FontRenderOps . SCALE ) ;
801
827
this . compileGlyphImpl ( code , cmds , glyphId ) ;
828
+ cmds . add ( FontRenderOps . RESTORE ) ;
802
829
803
- cmds . push ( { cmd : "restore" } ) ;
804
-
805
- return cmds ;
830
+ return cmds . cmds ;
806
831
}
807
832
808
833
compileGlyphImpl ( ) {
0 commit comments