Skip to content

Commit 3964c7a

Browse files
committed
Added in support for RTL text in the Text Game Object.
1 parent 3c8a5b4 commit 3964c7a

2 files changed

Lines changed: 55 additions & 13 deletions

File tree

v3/src/gameobjects/text/TextStyle.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ var TextStyle = new Class({
9090

9191
syncFont: function (canvas, context)
9292
{
93-
if (this.rtl)
94-
{
95-
canvas.dir = 'rtl';
96-
}
97-
9893
context.font = this.font;
9994
context.textBaseline = 'alphabetic';
10095

v3/src/gameobjects/text/static/Text.js

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
1+
var AddToDOM = require('../../../dom/AddToDOM');
2+
var CanvasPool = require('../../../display/canvas/CanvasPool');
23
var Class = require('../../../utils/Class');
3-
var GameObject = require('../../GameObject');
44
var 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');
68
var TextRender = require('./TextRender');
79
var TextStyle = require('../TextStyle');
8-
var GetTextSize = require('../GetTextSize');
910

1011
var 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

315362
module.exports = Text;

0 commit comments

Comments
 (0)