@@ -23,7 +23,7 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
23
23
} ;
24
24
25
25
_context . lineWidth = s . distance ( _context . lineWidth ) ;
26
- // FIXME text default size
26
+ _context . font = ` ${ s . distance ( 10 ) } px sans-serif` ;
27
27
28
28
const notSupported = ( name ) => {
29
29
throw new Error ( `${ name } not supported` ) ;
@@ -32,9 +32,8 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
32
32
throw new Error ( `${ name } not implemented yet` ) ;
33
33
} ;
34
34
35
- /* FIXME
36
- - font
37
- - all text stuff
35
+ /* TODO
36
+ - measureText
38
37
- Path2d, farContext.Path2D ?
39
38
*/
40
39
@@ -67,10 +66,32 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
67
66
notImplementedYet ( "filter" ) ;
68
67
} ,
69
68
get font ( ) {
70
- notImplementedYet ( "font" ) ;
69
+ const font_ = _context . font . split ( " " ) . filter ( ( a ) => a . trim ( ) ) ;
70
+
71
+ if ( ! [ 2 , 3 ] . includes ( font_ . length ) ) {
72
+ notSupported ( "font(!'[<style> ]<size> <face>')" ) ;
73
+ } else {
74
+ const [ style , size , face ] = font_ . length == 3 ? font_ : [ "" , ...font_ ] ;
75
+
76
+ const sizeValue = parseFloat ( size . match ( / [ 0 - 9 \. ] / g) . join ( "" ) ) ;
77
+ const sizeUnit = size . match ( / [ A - Z a - z ] / g) . join ( "" ) ;
78
+
79
+ return `${ style } ${ s . inv . distance ( sizeValue ) } ${ sizeUnit } ${ face } ` ;
80
+ }
71
81
} ,
72
82
set font ( font ) {
73
- notImplementedYet ( "font" ) ;
83
+ const font_ = font . split ( " " ) . filter ( ( a ) => a . trim ( ) ) ;
84
+
85
+ if ( ! [ 2 , 3 ] . includes ( font_ . length ) ) {
86
+ notSupported ( "font(!'[<style> ]<size> <face>')" ) ;
87
+ } else {
88
+ const [ style , size , face ] = font_ . length == 3 ? font_ : [ "" , ...font_ ] ;
89
+
90
+ const sizeValue = parseFloat ( size . match ( / [ 0 - 9 \. ] / g) . join ( "" ) ) ;
91
+ const sizeUnit = size . match ( / [ A - Z a - z ] / g) . join ( "" ) ;
92
+
93
+ _context . font = `${ style } ${ s . distance ( sizeValue ) } ${ sizeUnit } ${ face } ` ;
94
+ }
74
95
} ,
75
96
get fontKerning ( ) {
76
97
return _context . fontKerning ;
@@ -228,7 +249,7 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
228
249
if ( args . length === 0 ) {
229
250
return _context . clip ( ) ;
230
251
} else if ( typeof args [ 0 ] === "object" ) {
231
- // FIXME Path2D
252
+ // TODO Path2D
232
253
notImplementedYet ( "clip(Path2D, .)" ) ;
233
254
} else {
234
255
return _context . clip ( ...args ) ;
@@ -316,7 +337,7 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
316
337
if ( args . length === 0 ) {
317
338
return _context . fill ( ) ;
318
339
} else if ( typeof args [ 0 ] === "object" ) {
319
- // FIXME Path2D
340
+ // TODO Path2D
320
341
notImplementedYet ( "fill(Path2D, .)" ) ;
321
342
} else {
322
343
return _context . fill ( ...args ) ;
@@ -330,8 +351,13 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
330
351
s . distance ( height )
331
352
) ;
332
353
} ,
333
- fillText ( text , x , y , maxWidth ) {
334
- return _context . fillText ( text , s . x ( x ) , s . y ( y ) , s . distance ( maxWidth ) ) ;
354
+ fillText ( text , x , y , maxWidth = undefined ) {
355
+ return _context . fillText (
356
+ text ,
357
+ s . x ( x ) ,
358
+ s . y ( y ) ,
359
+ isDefined ( maxWidth ) ? s . distance ( maxWidth ) : undefined
360
+ ) ;
335
361
} ,
336
362
getContextAttributes ( ) {
337
363
return _context . getContextAttributes ( ) ;
@@ -420,8 +446,13 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
420
446
s . distance ( height )
421
447
) ;
422
448
} ,
423
- strokeText ( text , x , y , maxWidth ) {
424
- return _context . strokeText ( text , s . x ( x ) , s . y ( y ) , s . distance ( maxWidth ) ) ;
449
+ strokeText ( text , x , y , maxWidth = undefined ) {
450
+ return _context . strokeText (
451
+ text ,
452
+ s . x ( x ) ,
453
+ s . y ( y ) ,
454
+ isDefined ( maxWidth ) ? s . distance ( maxWidth ) : undefined
455
+ ) ;
425
456
} ,
426
457
transform ( a , b , c , d , e , f ) {
427
458
notSupported ( "transform" ) ;
0 commit comments