@@ -8,12 +8,6 @@ class CanvasConstructor {
88
99 this . width = width ;
1010 this . height = height ;
11-
12- this . font = {
13- style : '' ,
14- size : 21.33 ,
15- font : ''
16- } ;
1711 }
1812
1913 /**
@@ -204,12 +198,12 @@ class CanvasConstructor {
204198 * .toBuffer();
205199 */
206200 addResponsiveText ( text , x , y , maxWidth ) {
207- const { style = '' , size, font } = this . font ;
208- if ( isNaN ( size ) ) throw new TypeError ( 'The parameter size must be a valid number.' ) ;
201+ const [ , style = '' , size , font ] = / ( \w + ) ? ( \d + ) ( . + ) / . exec ( this . context . font ) ;
202+ const currentSize = parseInt ( size ) ;
209203 const { width } = this . measureText ( text ) ;
210- const newLength = maxWidth > width ? size : ( maxWidth / width ) * size ;
204+ const newLength = maxWidth > width ? currentSize : ( maxWidth / width ) * currentSize ;
211205 return this
212- . setTextFont ( ` ${ style } ${ newLength } px ${ font } ` )
206+ . setTextFont ( style + newLength + font )
213207 . addText ( text , x , y ) ;
214208 }
215209
@@ -771,45 +765,6 @@ class CanvasConstructor {
771765 return this ;
772766 }
773767
774- /**
775- * Parses the font.
776- * @param {string } string A string.
777- * @returns {void }
778- * @private
779- */
780- _parseFontString ( string ) {
781- const data = / ( [ ^ \d ] + ) ? ( [ \d \w ] + ) ( .+ ) ? / . exec ( string ) ;
782- if ( data === null ) return ;
783- this . font . style = data [ 1 ] || '' ;
784- this . font . size = this . _parseFontSize ( data [ 2 ] ) ;
785- this . font . font = data [ 3 ] || '' ;
786- }
787-
788- /**
789- * Parses the font's size
790- * @param {string } string The string with a number and a unit.
791- * @returns {number }
792- * @private
793- */
794- _parseFontSize ( string ) {
795- const data = / ( \d + ) ( \w + ) / . exec ( string ) ;
796- if ( data === null ) return 21.33 ;
797- let size = parseFloat ( data [ 1 ] ) ;
798- const unit = data [ 2 ] ;
799- switch ( unit ) {
800- case 'pt' : size /= 0.75 ; break ;
801- case 'pc' : size *= 16 ; break ;
802- case 'in' : size *= 96 ; break ;
803- case 'cm' : size *= 96.0 / 2.54 ; break ;
804- case 'mm' : size *= 96.0 / 25.4 ; break ;
805- case 'em' : size /= 0.75 ; break ;
806- case 'rem' : size *= 21.33 / 0.75 ; break ;
807- case 'q' : size *= 96 / 25.4 / 4 ; break ;
808- }
809-
810- return size ;
811- }
812-
813768}
814769
815770module . exports = CanvasConstructor ;
0 commit comments