@@ -12,89 +12,77 @@ var Glyph = {
12
12
return path . match ( / ( [ a - z ] ) | ( - ? \d + (?: \. \d + ) ? ) / ig) ;
13
13
} ,
14
14
15
- addContourToPath : function ( ctx , points , startIndex , count ) {
16
- ctx . beginPath ( ) ;
15
+ drawSVGPath : function ( ctx , path ) {
16
+ var p = Glyph . splitPath ( path ) ;
17
+
18
+ if ( ! p ) {
19
+ return ;
20
+ }
17
21
18
- var offset = 0 ;
22
+ var l = p . length ;
23
+ var i = 0 ;
19
24
20
- while ( offset < count ) {
21
- var point_m1 = points [ ( offset == 0 ) ? ( startIndex + count - 1 ) : startIndex + ( offset - 1 ) % count ] ;
22
- var point = points [ startIndex + offset % count ] ;
23
- var point_p1 = points [ startIndex + ( offset + 1 ) % count ] ;
24
- var point_p2 = points [ startIndex + ( offset + 2 ) % count ] ;
25
-
26
- if ( offset == 0 ) {
27
- ctx . moveTo ( point . x , point . y ) ;
28
- }
25
+ while ( i < l ) {
26
+ var v = p [ i ] ;
29
27
30
- if ( point . onCurve && point_p1 . onCurve ) {
31
- ctx . lineTo ( point_p1 . x , point_p1 . y ) ;
32
- offset ++ ;
33
- }
34
- else if ( point . onCurve && ! point_p1 . onCurve && point_p2 . onCurve ) {
35
- ctx . quadraticCurveTo ( point_p1 . x , point_p1 . y , point_p2 . x , point_p2 . y ) ;
36
- offset += 2 ;
37
- }
38
- else if ( point . onCurve && ! point_p1 . onCurve && ! point_p2 . onCurve ) {
39
- ctx . quadraticCurveTo ( point_p1 . x , point_p1 . y , Glyph . midValue ( point_p1 . x , point_p2 . x ) , Glyph . midValue ( point_p1 . y , point_p2 . y ) ) ;
40
- offset += 2 ;
41
- }
42
- else if ( ! point . onCurve && ! point_p1 . onCurve ) {
43
- ctx . quadraticCurveTo ( point . x , point . y , Glyph . midValue ( point . x , point_p1 . x ) , Glyph . midValue ( point . y , point_p1 . y ) ) ;
44
- offset ++ ;
45
- }
46
- else if ( ! point . onCurve && point_p1 . onCurve ) {
47
- ctx . quadraticCurveTo ( point . x , point . y , point_p1 . x , point_p1 . y ) ;
48
- offset ++ ;
49
- }
50
- else {
51
- console . error ( "error" ) ;
52
- break ;
28
+ switch ( v ) {
29
+ case "M" :
30
+ ctx . beginPath ( ) ;
31
+ ctx . moveTo ( parseFloat ( p [ ++ i ] ) , parseFloat ( p [ ++ i ] ) ) ;
32
+ break ;
33
+
34
+ case "L" :
35
+ ctx . lineTo ( parseFloat ( p [ ++ i ] ) , parseFloat ( p [ ++ i ] ) ) ;
36
+ break ;
37
+
38
+ case "Q" :
39
+ ctx . quadraticCurveTo ( parseFloat ( p [ ++ i ] ) , parseFloat ( p [ ++ i ] ) , parseFloat ( p [ ++ i ] ) , parseFloat ( p [ ++ i ] ) ) ;
40
+ break ;
41
+
42
+ case "z" :
43
+ ctx . closePath ( ) ;
44
+ ctx . fill ( ) ;
45
+ i ++ ;
46
+ break ;
47
+
48
+ default :
49
+ i ++ ;
53
50
}
54
51
}
55
-
52
+ } ,
53
+
54
+ drawHorizLine : function ( ctx , y , color ) {
55
+ ctx . beginPath ( ) ;
56
+ ctx . strokeStyle = color ;
57
+ ctx . moveTo ( 0 , y ) ;
58
+ ctx . lineTo ( Glyph . width * Glyph . ratio , y ) ;
56
59
ctx . closePath ( ) ;
57
- ctx . fill ( ) ;
60
+ ctx . stroke ( ) ;
58
61
} ,
59
62
60
63
draw : function ( canvas , shape , gid ) {
61
- var element = canvas [ 0 ] ;
62
- var ctx = element . getContext ( "2d" ) ;
63
- var width = element . width ;
64
- var height = element . height ;
65
- var ratio = Glyph . ratio ;
64
+ var element = canvas [ 0 ] ;
65
+ var ctx = element . getContext ( "2d" ) ;
66
+ var ratio = Glyph . ratio ;
67
+ Glyph . width = element . width ;
68
+ Glyph . height = element . height ;
66
69
67
70
ctx . lineWidth = ratio ;
68
71
69
72
// Invert axis
70
- ctx . translate ( 0 , height ) ;
73
+ ctx . translate ( 0 , Glyph . height ) ;
71
74
ctx . scale ( 1 / ratio , - ( 1 / ratio ) ) ;
72
75
73
76
ctx . translate ( 0 , - Glyph . head . yMin ) ;
74
77
75
78
// baseline
76
- ctx . beginPath ( ) ;
77
- ctx . strokeStyle = "rgba(0,255,0,0.2)" ;
78
- ctx . moveTo ( 0 , 0 ) ;
79
- ctx . lineTo ( width * ratio , 0 ) ;
80
- ctx . closePath ( ) ;
81
- ctx . stroke ( ) ;
79
+ Glyph . drawHorizLine ( ctx , 0 , "rgba(0,255,0,0.2)" ) ;
82
80
83
81
// ascender
84
- ctx . beginPath ( ) ;
85
- ctx . strokeStyle = "rgba(255,0,0,0.2)" ;
86
- ctx . moveTo ( 0 , Glyph . os2 . typoAscender ) ;
87
- ctx . lineTo ( width * ratio , Glyph . os2 . typoAscender ) ;
88
- ctx . closePath ( ) ;
89
- ctx . stroke ( ) ;
82
+ Glyph . drawHorizLine ( ctx , Glyph . os2 . typoAscender , "rgba(255,0,0,0.2)" ) ;
90
83
91
84
// descender
92
- ctx . beginPath ( ) ;
93
- ctx . strokeStyle = "rgba(255,0,0,0.2)" ;
94
- ctx . moveTo ( 0 , - Math . abs ( Glyph . os2 . typoDescender ) ) ;
95
- ctx . lineTo ( width * ratio , - Math . abs ( Glyph . os2 . typoDescender ) ) ;
96
- ctx . closePath ( ) ;
97
- ctx . stroke ( ) ;
85
+ Glyph . drawHorizLine ( ctx , - Math . abs ( Glyph . os2 . typoDescender ) , "rgba(255,0,0,0.2)" ) ;
98
86
99
87
ctx . translate ( - Glyph . head . xMin , 0 ) ;
100
88
@@ -138,19 +126,6 @@ var Glyph = {
138
126
ctx . strokeStyle = "black" ;
139
127
ctx . globalCompositeOperation = "xor" ;
140
128
141
- var points = shape . points ;
142
- var length = points . length ;
143
- var firstIndex = 0 ;
144
- var count = 0 ;
145
-
146
- for ( var i = 0 ; i < length ; i ++ ) {
147
- count ++ ;
148
-
149
- if ( points [ i ] . endOfContour ) {
150
- Glyph . addContourToPath ( ctx , points , firstIndex , count ) ;
151
- firstIndex = i + 1 ;
152
- count = 0 ;
153
- }
154
- }
129
+ Glyph . drawSVGPath ( ctx , shape . SVGPath ) ;
155
130
}
156
131
} ;
0 commit comments