Skip to content

Commit 56cc7dc

Browse files
committed
Merge pull request phaserjs#2519 from TadejZupancic/patch-1
Fixing measurement of text width that produced too narrow canvas
2 parents d18c591 + 317c85d commit 56cc7dc

1 file changed

Lines changed: 91 additions & 3 deletions

File tree

src/gameobjects/Text.js

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,23 @@ Phaser.Text.prototype.updateText = function () {
390390
drawnLines = this.style.maxLines;
391391
}
392392

393+
this._charCount = 0;
394+
393395
for (var i = 0; i < drawnLines; i++)
394396
{
395397
if (tabs === 0)
396398
{
397399
// Simple layout (no tabs)
398-
var lineWidth = this.context.measureText(lines[i]).width + this.style.strokeThickness + this.padding.x;
400+
var lineWidth = this.style.strokeThickness + this.padding.x;
401+
402+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
403+
{
404+
lineWidth += this.measureLine(lines[i]);
405+
}
406+
else
407+
{
408+
lineWidth += this.context.measureText(lines[i]).width;
409+
}
399410

400411
// Adjust for wrapped text
401412
if (this.style.wordWrap)
@@ -415,7 +426,16 @@ Phaser.Text.prototype.updateText = function () {
415426

416427
for (var c = 0; c < line.length; c++)
417428
{
418-
var section = Math.ceil(this.context.measureText(line[c]).width);
429+
var section = 0;
430+
431+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
432+
{
433+
section = this.measureLine(line[c]);
434+
}
435+
else
436+
{
437+
section = Math.ceil(this.context.measureText(line[c]).width);
438+
}
419439

420440
if (c > 0)
421441
{
@@ -430,7 +450,14 @@ Phaser.Text.prototype.updateText = function () {
430450
for (var c = 0; c < line.length; c++)
431451
{
432452
// How far to the next tab?
433-
lineWidth += Math.ceil(this.context.measureText(line[c]).width);
453+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
454+
{
455+
lineWidth += this.measureLine(line[c]);
456+
}
457+
else
458+
{
459+
lineWidth += Math.ceil(this.context.measureText(line[c]).width);
460+
}
434461

435462
var diff = this.game.math.snapToCeil(lineWidth, tabs) - lineWidth;
436463

@@ -648,6 +675,67 @@ Phaser.Text.prototype.updateShadow = function (state) {
648675

649676
};
650677

678+
/**
679+
* Measures a line of text character by character taking into the account the specified character styles.
680+
*
681+
* @method Phaser.Text#measureLine
682+
* @private
683+
* @param {string} line - The line of text to measure.
684+
* @return {integer} length of the line.
685+
*/
686+
Phaser.Text.prototype.measureLine = function (line) {
687+
688+
var lineLength = 0;
689+
690+
for (var i = 0; i < line.length; i++)
691+
{
692+
var letter = line[i];
693+
694+
if (this.fontWeights.length > 0 || this.fontStyles.length > 0)
695+
{
696+
var components = this.fontToComponents(this.context.font);
697+
698+
if (this.fontStyles[this._charCount])
699+
{
700+
components.fontStyle = this.fontStyles[this._charCount];
701+
}
702+
703+
if (this.fontWeights[this._charCount])
704+
{
705+
components.fontWeight = this.fontWeights[this._charCount];
706+
}
707+
708+
this.context.font = this.componentsToFont(components);
709+
}
710+
711+
if (this.style.stroke && this.style.strokeThickness)
712+
{
713+
if (this.strokeColors[this._charCount])
714+
{
715+
this.context.strokeStyle = this.strokeColors[this._charCount];
716+
}
717+
718+
this.updateShadow(this.style.shadowStroke);
719+
}
720+
721+
if (this.style.fill)
722+
{
723+
if (this.colors[this._charCount])
724+
{
725+
this.context.fillStyle = this.colors[this._charCount];
726+
}
727+
728+
this.updateShadow(this.style.shadowFill);
729+
}
730+
731+
lineLength += this.context.measureText(letter).width;
732+
733+
this._charCount++;
734+
}
735+
736+
return Math.ceil(lineLength);
737+
};
738+
651739
/**
652740
* Updates a line of text, applying fill and stroke per-character colors or style and weight per-character font if applicable.
653741
*

0 commit comments

Comments
 (0)