1-
1+ var AddToDOM = require ( '../../../dom/AddToDOM' ) ;
2+ var CanvasPool = require ( '../../../display/canvas/CanvasPool' ) ;
23var Class = require ( '../../../utils/Class' ) ;
3- var GameObject = require ( '../../GameObject' ) ;
44var Components = require ( '../../components' ) ;
5- var CanvasPool = require ( '../../../display/canvas/CanvasPool' ) ;
5+ var GameObject = require ( '../../GameObject' ) ;
6+ var GetTextSize = require ( '../GetTextSize' ) ;
7+ var RemoveFromDOM = require ( '../../../dom/RemoveFromDOM' ) ;
68var TextRender = require ( './TextRender' ) ;
79var TextStyle = require ( '../TextStyle' ) ;
8- var GetTextSize = require ( '../GetTextSize' ) ;
910
1011var Text = new Class ( {
1112
@@ -32,10 +33,6 @@ var Text = new Class({
3233 {
3334 if ( x === undefined ) { x = 0 ; }
3435 if ( y === undefined ) { y = 0 ; }
35- if ( text === undefined ) { text = '' ; }
36-
37- // Cast to string whatever our value may be (numeric, etc)
38- text = text . toString ( ) ;
3936
4037 GameObject . call ( this , scene , 'Text' ) ;
4138
@@ -64,7 +61,8 @@ var Text = new Class({
6461 */
6562 this . splitRegExp = / (?: \r \n | \r | \n ) / ;
6663
67- this . text = ( Array . isArray ( text ) ) ? text . join ( '\n' ) : text ;
64+ // This is populated in this.setText
65+ this . text = '' ;
6866
6967 this . resolution = 1 ;
7068
@@ -81,10 +79,9 @@ var Text = new Class({
8179 this . canvasTexture = null ;
8280 this . dirty = false ;
8381
84- if ( text !== '' )
85- {
86- this . updateText ( ) ;
87- }
82+ this . initRTL ( ) ;
83+
84+ this . setText ( text ) ;
8885
8986 var _this = this ;
9087
@@ -95,6 +92,32 @@ var Text = new Class({
9592 } ) ;
9693 } ,
9794
95+ initRTL : function ( )
96+ {
97+ if ( ! this . style . rtl )
98+ {
99+ return ;
100+ }
101+
102+ // Here is where the crazy starts.
103+ //
104+ // Due to browser implementation issues, you cannot fillText BiDi text to a canvas
105+ // that is not part of the DOM. It just completely ignores the direction property.
106+
107+ this . canvas . dir = 'rtl' ;
108+
109+ // Experimental atm, but one day ...
110+ this . context . direction = 'rtl' ;
111+
112+ // Add it to the DOM, but hidden within the parent canvas.
113+ this . canvas . style . display = 'none' ;
114+
115+ AddToDOM ( this . canvas , this . scene . sys . canvas ) ;
116+
117+ // And finally we set the x origin
118+ this . originX = 1 ;
119+ } ,
120+
98121 setText : function ( value )
99122 {
100123 if ( Array . isArray ( value ) )
@@ -255,13 +278,20 @@ var Text = new Class({
255278 linePositionY += ( textSize . lineSpacing * i ) ;
256279 }
257280
258- if ( style . align === 'right' )
281+ if ( style . rtl )
259282 {
260- linePositionX += textSize . width - textSize . lineWidths [ i ] ;
283+ linePositionX = w - linePositionX ;
261284 }
262- else if ( style . align === 'center' )
285+ else
263286 {
264- linePositionX += ( textSize . width - textSize . lineWidths [ i ] ) / 2 ;
287+ if ( style . align === 'right' )
288+ {
289+ linePositionX += textSize . width - textSize . lineWidths [ i ] ;
290+ }
291+ else if ( style . align === 'center' )
292+ {
293+ linePositionX += ( textSize . width - textSize . lineWidths [ i ] ) / 2 ;
294+ }
265295 }
266296
267297 if ( this . autoRound )
@@ -315,7 +345,18 @@ var Text = new Class({
315345 out . data = data ;
316346
317347 return out ;
348+ } ,
349+
350+ preDestroy : function ( )
351+ {
352+ if ( this . style . rtl )
353+ {
354+ RemoveFromDOM ( this . canvas ) ;
355+ }
356+
357+ CanvasPool . remove ( this . canvas ) ;
318358 }
359+
319360} ) ;
320361
321362module . exports = Text ;
0 commit comments