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
@@ -78,6 +79,8 @@ var Text = new Class({
7879 this . canvasTexture = null ;
7980 this . dirty = false ;
8081
82+ this . initRTL ( ) ;
83+
8184 this . setText ( text ) ;
8285
8386 var _this = this ;
@@ -89,6 +92,32 @@ var Text = new Class({
8992 } ) ;
9093 } ,
9194
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+
92121 setText : function ( value )
93122 {
94123 if ( Array . isArray ( value ) )
@@ -249,13 +278,20 @@ var Text = new Class({
249278 linePositionY += ( textSize . lineSpacing * i ) ;
250279 }
251280
252- if ( style . align === 'right' )
281+ if ( style . rtl )
253282 {
254- linePositionX += textSize . width - textSize . lineWidths [ i ] ;
283+ linePositionX = w - linePositionX ;
255284 }
256- else if ( style . align === 'center' )
285+ else
257286 {
258- 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+ }
259295 }
260296
261297 if ( this . autoRound )
@@ -309,7 +345,18 @@ var Text = new Class({
309345 out . data = data ;
310346
311347 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 ) ;
312358 }
359+
313360} ) ;
314361
315362module . exports = Text ;
0 commit comments