@@ -293,7 +293,7 @@ export default (function () {
293
293
294
294
/**
295
295
* Log
296
- *
296
+ *
297
297
* @private
298
298
*/
299
299
Context . prototype . __debug = function ( ...data ) {
@@ -380,15 +380,15 @@ export default (function () {
380
380
* @private
381
381
*/
382
382
Context . prototype . __applyStyleToCurrentElement = function ( type ) {
383
- var currentElement = this . __currentElement ;
384
- var currentStyleGroup = this . __currentElementsToStyle ;
385
- if ( currentStyleGroup ) {
386
- currentElement . setAttribute ( type , "" ) ;
387
- currentElement = currentStyleGroup . element ;
388
- currentStyleGroup . children . forEach ( function ( node ) {
389
- node . setAttribute ( type , "" ) ;
390
- } )
391
- }
383
+ var currentElement = this . __currentElement ;
384
+ var currentStyleGroup = this . __currentElementsToStyle ;
385
+ if ( currentStyleGroup ) {
386
+ currentElement . setAttribute ( type , "" ) ;
387
+ currentElement = currentStyleGroup . element ;
388
+ currentStyleGroup . children . forEach ( function ( node ) {
389
+ node . setAttribute ( type , "" ) ;
390
+ } )
391
+ }
392
392
393
393
var keys = Object . keys ( STYLES ) , i , style , value , regex , matches , id , nodeIndex , node ;
394
394
for ( i = 0 ; i < keys . length ; i ++ ) {
@@ -531,7 +531,7 @@ export default (function () {
531
531
if ( this . __transformMatrixStack && this . __transformMatrixStack . length > 0 ) {
532
532
this . setTransform ( this . __transformMatrixStack . pop ( ) )
533
533
}
534
-
534
+
535
535
} ;
536
536
537
537
/**
@@ -556,11 +556,11 @@ export default (function () {
556
556
* @private
557
557
*/
558
558
Context . prototype . __applyCurrentDefaultPath = function ( ) {
559
- var currentElement = this . __currentElement ;
559
+ var currentElement = this . __currentElement ;
560
560
if ( currentElement . nodeName === "path" ) {
561
- currentElement . setAttribute ( "d" , this . __currentDefaultPath ) ;
561
+ currentElement . setAttribute ( "d" , this . __currentDefaultPath ) ;
562
562
} else {
563
- console . error ( "Attempted to apply path command to node" , currentElement . nodeName ) ;
563
+ console . error ( "Attempted to apply path command to node" , currentElement . nodeName ) ;
564
564
}
565
565
} ;
566
566
@@ -585,7 +585,7 @@ export default (function () {
585
585
// creates a new subpath with the given point
586
586
this . __currentPosition = { x : x , y : y } ;
587
587
this . __addPathCommand ( format ( "M {x} {y}" , {
588
- x : this . __matrixTransform ( x , y ) . x ,
588
+ x : this . __matrixTransform ( x , y ) . x ,
589
589
y : this . __matrixTransform ( x , y ) . y
590
590
} ) ) ;
591
591
} ;
@@ -606,12 +606,12 @@ export default (function () {
606
606
this . __currentPosition = { x : x , y : y } ;
607
607
if ( this . __currentDefaultPath . indexOf ( 'M' ) > - 1 ) {
608
608
this . __addPathCommand ( format ( "L {x} {y}" , {
609
- x : this . __matrixTransform ( x , y ) . x ,
609
+ x : this . __matrixTransform ( x , y ) . x ,
610
610
y : this . __matrixTransform ( x , y ) . y
611
611
} ) ) ;
612
612
} else {
613
613
this . __addPathCommand ( format ( "M {x} {y}" , {
614
- x : this . __matrixTransform ( x , y ) . x ,
614
+ x : this . __matrixTransform ( x , y ) . x ,
615
615
y : this . __matrixTransform ( x , y ) . y
616
616
} ) ) ;
617
617
}
@@ -628,7 +628,7 @@ export default (function () {
628
628
cp1y : this . __matrixTransform ( cp1x , cp1y ) . y ,
629
629
cp2x : this . __matrixTransform ( cp2x , cp2y ) . x ,
630
630
cp2y : this . __matrixTransform ( cp2x , cp2y ) . y ,
631
- x : this . __matrixTransform ( x , y ) . x ,
631
+ x : this . __matrixTransform ( x , y ) . x ,
632
632
y : this . __matrixTransform ( x , y ) . y
633
633
} ) ) ;
634
634
} ;
@@ -639,9 +639,9 @@ export default (function () {
639
639
Context . prototype . quadraticCurveTo = function ( cpx , cpy , x , y ) {
640
640
this . __currentPosition = { x : x , y : y } ;
641
641
this . __addPathCommand ( format ( "Q {cpx} {cpy} {x} {y}" , {
642
- cpx : this . __matrixTransform ( cpx , cpy ) . x ,
642
+ cpx : this . __matrixTransform ( cpx , cpy ) . x ,
643
643
cpy : this . __matrixTransform ( cpx , cpy ) . y ,
644
- x : this . __matrixTransform ( x , y ) . x ,
644
+ x : this . __matrixTransform ( x , y ) . x ,
645
645
y : this . __matrixTransform ( x , y ) . y
646
646
} ) ) ;
647
647
} ;
@@ -908,52 +908,6 @@ export default (function () {
908
908
909
909
} ;
910
910
911
- /**
912
- * Parses the font string and returns svg mapping
913
- * @private
914
- */
915
- Context . prototype . __parseFont = function ( ) {
916
- var regex = / ^ \s * (? = (?: (?: [ - a - z ] + \s * ) { 0 , 2 } ( i t a l i c | o b l i q u e ) ) ? ) (? = (?: (?: [ - a - z ] + \s * ) { 0 , 2 } ( s m a l l - c a p s ) ) ? ) (? = (?: (?: [ - a - z ] + \s * ) { 0 , 2 } ( b o l d (?: e r ) ? | l i g h t e r | [ 1 - 9 ] 0 0 ) ) ? ) (?: (?: n o r m a l | \1| \2| \3) \s * ) { 0 , 3 } ( (?: x x ? - ) ? (?: s m a l l | l a r g e ) | m e d i u m | s m a l l e r | l a r g e r | [ . \d ] + (?: \% | i n | [ c e m ] m | e x | p [ c t x ] ) ) (?: \s * \/ \s * ( n o r m a l | [ . \d ] + (?: \% | i n | [ c e m ] m | e x | p [ c t x ] ) ) ) ? \s * ( [ - , \' \" \s a - z 0 - 9 ] + ?) \s * $ / i;
917
- var fontPart = regex . exec ( this . font ) ;
918
- var data = {
919
- style : fontPart [ 1 ] || 'normal' ,
920
- size : fontPart [ 4 ] || '10px' ,
921
- family : fontPart [ 6 ] || 'sans-serif' ,
922
- weight : fontPart [ 3 ] || 'normal' ,
923
- decoration : fontPart [ 2 ] || 'normal' ,
924
- href : null
925
- } ;
926
-
927
- //canvas doesn't support underline natively, but we can pass this attribute
928
- if ( this . __fontUnderline === "underline" ) {
929
- data . decoration = "underline" ;
930
- }
931
-
932
- //canvas also doesn't support linking, but we can pass this as well
933
- if ( this . __fontHref ) {
934
- data . href = this . __fontHref ;
935
- }
936
-
937
- return data ;
938
- } ;
939
-
940
- /**
941
- * Helper to link text fragments
942
- * @param font
943
- * @param element
944
- * @return {* }
945
- * @private
946
- */
947
- Context . prototype . __wrapTextLink = function ( font , element ) {
948
- if ( font . href ) {
949
- var a = this . __createElement ( "a" ) ;
950
- a . setAttributeNS ( "http://www.w3.org/1999/xlink" , "xlink:href" , font . href ) ;
951
- a . appendChild ( element ) ;
952
- return a ;
953
- }
954
- return element ;
955
- } ;
956
-
957
911
/**
958
912
* Fills or strokes text
959
913
* @param text
@@ -963,16 +917,21 @@ export default (function () {
963
917
* @private
964
918
*/
965
919
Context . prototype . __applyText = function ( text , x , y , action ) {
966
- var font = this . __parseFont ( ) ,
920
+ var el = document . createElement ( "span" ) ;
921
+ el . setAttribute ( "style" , 'font:' + this . font ) ;
922
+
923
+ var style = el . style , // CSSStyleDeclaration object
967
924
parent = this . __closestGroupOrSvg ( ) ,
968
925
textElement = this . __createElement ( "text" , {
969
- "font-family" : font . family ,
970
- "font-size" : font . size ,
971
- "font-style" : font . style ,
972
- "font-weight" : font . weight ,
973
- "text-decoration" : font . decoration ,
974
- "x" : x ,
975
- "y" : y ,
926
+ "font-family" : style . fontFamily ,
927
+ "font-size" : style . fontSize ,
928
+ "font-style" : style . fontStyle ,
929
+ "font-weight" : style . fontWeight ,
930
+
931
+ // canvas doesn't support underline natively, but we do :)
932
+ "text-decoration" : this . __fontUnderline ,
933
+ "x" : x ,
934
+ "y" : y ,
976
935
"text-anchor" : getTextAnchor ( this . textAlign ) ,
977
936
"dominant-baseline" : getDominantBaseline ( this . textBaseline )
978
937
} , true ) ;
@@ -981,7 +940,16 @@ export default (function () {
981
940
this . __currentElement = textElement ;
982
941
this . __applyTransformation ( textElement ) ;
983
942
this . __applyStyleToCurrentElement ( action ) ;
984
- parent . appendChild ( this . __wrapTextLink ( font , textElement ) ) ;
943
+
944
+ if ( this . __fontHref ) {
945
+ var a = this . __createElement ( "a" ) ;
946
+ // canvas doesn't natively support linking, but we do :)
947
+ a . setAttributeNS ( "http://www.w3.org/1999/xlink" , "xlink:href" , this . __fontHref ) ;
948
+ a . appendChild ( textElement ) ;
949
+ textElement = a ;
950
+ }
951
+
952
+ parent . appendChild ( textElement ) ;
985
953
} ;
986
954
987
955
/**
@@ -1055,7 +1023,7 @@ export default (function () {
1055
1023
xAxisRotation :0 ,
1056
1024
largeArcFlag :largeArcFlag ,
1057
1025
sweepFlag :sweepFlag ,
1058
- endX : this . __matrixTransform ( endX , endY ) . x ,
1026
+ endX : this . __matrixTransform ( endX , endY ) . x ,
1059
1027
endY : this . __matrixTransform ( endX , endY ) . y
1060
1028
} ) ) ;
1061
1029
@@ -1209,9 +1177,9 @@ export default (function () {
1209
1177
} ;
1210
1178
1211
1179
/**
1212
- * SetTransform changes the current transformation matrix to
1180
+ * SetTransform changes the current transformation matrix to
1213
1181
* the matrix given by the arguments as described below.
1214
- *
1182
+ *
1215
1183
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
1216
1184
*/
1217
1185
Context . prototype . setTransform = function ( a , b , c , d , e , f ) {
@@ -1225,7 +1193,7 @@ export default (function () {
1225
1193
/**
1226
1194
* GetTransform Returns a copy of the current transformation matrix,
1227
1195
* as a newly created DOMMAtrix Object
1228
- *
1196
+ *
1229
1197
* @returns A DOMMatrix Object
1230
1198
*/
1231
1199
Context . prototype . getTransform = function ( ) {
@@ -1235,21 +1203,21 @@ export default (function () {
1235
1203
1236
1204
/**
1237
1205
* ResetTransform resets the current transformation matrix to the identity matrix
1238
- *
1206
+ *
1239
1207
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/resetTransform
1240
1208
*/
1241
1209
Context . prototype . resetTransform = function ( ) {
1242
1210
this . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
1243
1211
} ;
1244
1212
1245
1213
/**
1246
- * Add the scaling transformation described by the arguments to the current transformation matrix.
1247
- *
1248
- * @param x The x argument represents the scale factor in the horizontal direction
1214
+ * Add the scaling transformation described by the arguments to the current transformation matrix.
1215
+ *
1216
+ * @param x The x argument represents the scale factor in the horizontal direction
1249
1217
* @param y The y argument represents the scale factor in the vertical direction.
1250
1218
* @see https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-scale
1251
1219
*/
1252
- Context . prototype . scale = function ( x , y ) {
1220
+ Context . prototype . scale = function ( x , y ) {
1253
1221
if ( y === undefined ) {
1254
1222
y = x ;
1255
1223
}
@@ -1263,7 +1231,7 @@ export default (function () {
1263
1231
1264
1232
/**
1265
1233
* Rotate adds a rotation to the transformation matrix.
1266
- *
1234
+ *
1267
1235
* @param angle The rotation angle, clockwise in radians. You can use degree * Math.PI / 180 to calculate a radian from a degree.
1268
1236
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rotate
1269
1237
* @see https://www.w3.org/TR/css-transforms-1
@@ -1282,7 +1250,7 @@ export default (function () {
1282
1250
1283
1251
/**
1284
1252
* Translate adds a translation transformation to the current matrix.
1285
- *
1253
+ *
1286
1254
* @param x Distance to move in the horizontal direction. Positive values are to the right, and negative to the left.
1287
1255
* @param y Distance to move in the vertical direction. Positive values are down, and negative are up.
1288
1256
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/translate
@@ -1293,9 +1261,9 @@ export default (function () {
1293
1261
} ;
1294
1262
1295
1263
/**
1296
- * Transform multiplies the current transformation with the matrix described by the arguments of this method.
1264
+ * Transform multiplies the current transformation with the matrix described by the arguments of this method.
1297
1265
* This lets you scale, rotate, translate (move), and skew the context.
1298
- *
1266
+ *
1299
1267
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
1300
1268
*/
1301
1269
Context . prototype . transform = function ( a , b , c , d , e , f ) {
@@ -1308,7 +1276,7 @@ export default (function () {
1308
1276
}
1309
1277
1310
1278
/**
1311
- *
1279
+ *
1312
1280
* @param {* } sx The x-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.
1313
1281
* @param {* } sy The y-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.
1314
1282
* @param {* } sw The width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left.
0 commit comments