Skip to content

Commit 2fe60ff

Browse files
committed
Add wrapping to Text & ensure context's font stays in sync
1 parent f6bd7ec commit 2fe60ff

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

v3/src/gameobjects/text/TextStyle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ var TextStyle = new Class({
137137
syncFont: function (canvas, context)
138138
{
139139
context.font = this._font;
140+
},
141+
142+
syncStyle: function (canvas, context)
143+
{
140144
context.textBaseline = 'alphabetic';
141145

142146
context.fillStyle = this.color;

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var Text = new Class({
106106
}
107107

108108
// Here is where the crazy starts.
109-
//
109+
//
110110
// Due to browser implementation issues, you cannot fillText BiDi text to a canvas
111111
// that is not part of the DOM. It just completely ignores the direction property.
112112

@@ -420,6 +420,18 @@ var Text = new Class({
420420
return this.style.setShadowFill(enabled);
421421
},
422422

423+
// Set to null to remove
424+
setWordWrapWidth: function (width, useAdvancedWrap)
425+
{
426+
return this.style.setWordWrapWidth(width, useAdvancedWrap);
427+
},
428+
429+
// Set to null to remove
430+
setWordWrapCallback: function (callback, scope)
431+
{
432+
return this.style.setWordWrapCallback(callback, scope);
433+
},
434+
423435
setAlign: function (align)
424436
{
425437
return this.style.setAlign(align);
@@ -488,12 +500,14 @@ var Text = new Class({
488500
var style = this.style;
489501
var size = style.metrics;
490502

503+
style.syncFont(canvas, context);
504+
491505
var outputText = this.text;
492506

493-
// if (style.wordWrap)
494-
// {
495-
// outputText = this.runWordWrap(this.text);
496-
// }
507+
if (style.wordWrapWidth || style.wordWrapCallback)
508+
{
509+
outputText = this.runWordWrap(this.text);
510+
}
497511

498512
// Split text into lines
499513
var lines = outputText.split(this.splitRegExp);
@@ -524,6 +538,7 @@ var Text = new Class({
524538
{
525539
canvas.width = w;
526540
canvas.height = h;
541+
style.syncFont(canvas, context); // Resizing resets the context
527542
}
528543
else
529544
{
@@ -538,7 +553,7 @@ var Text = new Class({
538553
context.fillRect(0, 0, w, h);
539554
}
540555

541-
style.syncFont(canvas, context);
556+
style.syncStyle(canvas, context);
542557

543558
context.textBaseline = 'alphabetic';
544559

0 commit comments

Comments
 (0)