55
66import { Renderer } from '../core/p5.Renderer' ;
77
8- function textCore ( p5 , fn ) {
9-
10- // additional constants
11- fn . IDEOGRAPHIC = 'ideographic' ;
12- fn . RIGHT_TO_LEFT = 'rtl' ;
13- fn . LEFT_TO_RIGHT = 'ltr' ;
14- fn . _CTX_MIDDLE = 'middle' ;
15- fn . _TEXT_BOUNDS = '_textBoundsSingle' ;
16- fn . _FONT_BOUNDS = '_fontBoundsSingle' ;
17- fn . HANGING = 'hanging' ;
18- fn . START = 'start' ;
19- fn . END = 'end' ;
8+ export const textCoreConstants = {
9+ IDEOGRAPHIC : 'ideographic' ,
10+ RIGHT_TO_LEFT : 'rtl' ,
11+ LEFT_TO_RIGHT : 'ltr' ,
12+ _CTX_MIDDLE : 'middle' ,
13+ _TEXT_BOUNDS : '_textBoundsSingle' ,
14+ _FONT_BOUNDS : '_fontBoundsSingle' ,
15+ HANGING : 'hanging' ,
16+ START : 'start' ,
17+ END : 'end' ,
18+ }
2019
20+ function textCore ( p5 , fn ) {
2121 const LeadingScale = 1.275 ;
2222 const DefaultFill = '#000000' ;
2323 const LinebreakRe = / \r ? \n / g;
@@ -265,7 +265,7 @@ function textCore(p5, fn) {
265265 */
266266 Renderer . prototype . textBounds = function ( str , x , y , width , height ) {
267267 // delegate to _textBoundsSingle for measuring
268- return this . _computeBounds ( fn . _TEXT_BOUNDS , str , x , y , width , height ) . bounds ;
268+ return this . _computeBounds ( textCoreConstants . _TEXT_BOUNDS , str , x , y , width , height ) . bounds ;
269269 } ;
270270
271271 /**
@@ -280,7 +280,7 @@ function textCore(p5, fn) {
280280 */
281281 Renderer . prototype . fontBounds = function ( str , x , y , width , height ) {
282282 // delegate to _fontBoundsSingle for measuring
283- return this . _computeBounds ( fn . _FONT_BOUNDS , str , x , y , width , height ) . bounds ;
283+ return this . _computeBounds ( textCoreConstants . _FONT_BOUNDS , str , x , y , width , height ) . bounds ;
284284 } ;
285285
286286 /**
@@ -355,7 +355,7 @@ function textCore(p5, fn) {
355355 this . states . setValue ( 'textAlign' , h ) ;
356356 if ( typeof v !== 'undefined' ) {
357357 if ( v === fn . CENTER ) {
358- v = fn . _CTX_MIDDLE ;
358+ v = textCoreConstants . _CTX_MIDDLE ;
359359 }
360360 this . states . setValue ( 'textBaseline' , v ) ;
361361 }
@@ -970,9 +970,9 @@ function textCore(p5, fn) {
970970 return width / 2 ;
971971 case fn . RIGHT :
972972 return width ;
973- case fn . START :
973+ case textCoreConstants . START :
974974 return 0 ;
975- case fn . END :
975+ case textCoreConstants . END :
976976 throw new Error ( 'textBounds: END not yet supported for textAlign' ) ;
977977 default :
978978 return 0 ;
@@ -1282,7 +1282,7 @@ function textCore(p5, fn) {
12821282
12831283 for ( let i = 0 ; i < lines . length ; i ++ ) {
12841284 switch ( textAlign ) {
1285- case fn . START :
1285+ case textCoreConstants . START :
12861286 throw new Error ( 'textBounds: START not yet supported for textAlign' ) ; // default to LEFT
12871287 case fn . LEFT :
12881288 adjustedX = x ;
@@ -1293,7 +1293,7 @@ function textCore(p5, fn) {
12931293 case fn . RIGHT :
12941294 adjustedX = x + adjustedW ;
12951295 break ;
1296- case fn . END :
1296+ case textCoreConstants . END :
12971297 throw new Error ( 'textBounds: END not yet supported for textAlign' ) ;
12981298 }
12991299 lineData [ i ] = { text : lines [ i ] , x : adjustedX , y : y + i * textLeading } ;
@@ -1319,16 +1319,16 @@ function textCore(p5, fn) {
13191319 break ; // ??
13201320 case fn . BASELINE :
13211321 break ;
1322- case fn . _CTX_MIDDLE :
1322+ case textCoreConstants . _CTX_MIDDLE :
13231323 yOff = ydiff / 2 ;
13241324 break ;
13251325 case fn . BOTTOM :
13261326 yOff = ydiff ;
13271327 break ;
1328- case fn . IDEOGRAPHIC :
1328+ case textCoreConstants . IDEOGRAPHIC :
13291329 console . warn ( 'textBounds: IDEOGRAPHIC not yet supported for textBaseline' ) ; // FES?
13301330 break ;
1331- case fn . HANGING :
1331+ case textCoreConstants . HANGING :
13321332 console . warn ( 'textBounds: HANGING not yet supported for textBaseline' ) ; // FES?
13331333 break ;
13341334 }
@@ -1358,7 +1358,7 @@ function textCore(p5, fn) {
13581358
13591359 for ( let i = 0 ; i < lines . length ; i ++ ) {
13601360 switch ( textAlign ) {
1361- case fn . START :
1361+ case textCoreConstants . START :
13621362 throw new Error ( 'textBounds: START not yet supported for textAlign' ) ; // default to LEFT
13631363 case fn . LEFT :
13641364 adjustedX = x ;
@@ -1369,7 +1369,7 @@ function textCore(p5, fn) {
13691369 case fn . RIGHT :
13701370 adjustedX = x + adjustedW - widths [ i ] - adjustedW + ( width || 0 ) ;
13711371 break ;
1372- case fn . END :
1372+ case textCoreConstants . END :
13731373 throw new Error ( 'textBounds: END not yet supported for textAlign' ) ;
13741374 }
13751375 lineData [ i ] = { text : lines [ i ] , x : adjustedX , y : y + i * textLeading } ;
@@ -1393,7 +1393,7 @@ function textCore(p5, fn) {
13931393 break ;
13941394 case fn . BASELINE :
13951395 break ;
1396- case fn . _CTX_MIDDLE :
1396+ case textCoreConstants . _CTX_MIDDLE :
13971397 yOff = - totalHeight / 2 + textSize + ( height || 0 ) / 2 ;
13981398 break ;
13991399 case fn . BOTTOM :
0 commit comments