@@ -25,13 +25,13 @@ export class Text extends Mark {
2525 rotate
2626 } = options ;
2727 const [ vrotate , crotate ] = maybeNumberChannel ( rotate , 0 ) ;
28- const [ vfontSize , cfontSize ] = maybeNumberChannel ( fontSize ) ;
28+ const [ vfontSize , cfontSize ] = maybeFontSizeChannel ( fontSize ) ;
2929 super (
3030 data ,
3131 [
3232 { name : "x" , value : x , scale : "x" , optional : true } ,
3333 { name : "y" , value : y , scale : "y" , optional : true } ,
34- { name : "fontSize" , value : numberChannel ( vfontSize ) , optional : true } ,
34+ { name : "fontSize" , value : vfontSize , optional : true } ,
3535 { name : "rotate" , value : numberChannel ( vrotate ) , optional : true } ,
3636 { name : "text" , value : text , filter : nonempty }
3737 ] ,
@@ -105,3 +105,39 @@ function applyDirectTextStyles(selection, mark) {
105105 applyAttr ( selection , "dx" , mark . dx ) ;
106106 applyAttr ( selection , "dy" , mark . dy ) ;
107107}
108+
109+ // https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
110+ const fontSizes = new Set ( [
111+ // global keywords
112+ "inherit" ,
113+ "initial" ,
114+ "revert" ,
115+ "unset" ,
116+ // absolute keywords
117+ "xx-small" ,
118+ "x-small" ,
119+ "small" ,
120+ "medium" ,
121+ "large" ,
122+ "x-large" ,
123+ "xx-large" ,
124+ "xxx-large" ,
125+ // relative keywords
126+ "larger" ,
127+ "smaller"
128+ ] ) ;
129+
130+ // The font size may be expressed as a constant in the following forms:
131+ // - number in pixels
132+ // - string keyword: see above
133+ // - string <length>: e.g., "12px"
134+ // - string <percentage>: e.g., "80%"
135+ // Anything else is assumed to be a channel definition.
136+ function maybeFontSizeChannel ( fontSize ) {
137+ if ( fontSize == null || typeof fontSize === "number" ) return [ undefined , fontSize ] ;
138+ if ( typeof fontSize !== "string" ) return [ fontSize , undefined ] ;
139+ fontSize = fontSize . trim ( ) . toLowerCase ( ) ;
140+ return fontSizes . has ( fontSize ) || / ^ [ + - ] ? \d * \. ? \d + ( e [ + - ] ? \d + ) ? ( \w * | % ) $ / . test ( fontSize )
141+ ? [ undefined , fontSize ]
142+ : [ fontSize , undefined ] ;
143+ }
0 commit comments