Skip to content

Commit 92a329f

Browse files
authored
Merge pull request #753 from Akurn/feat/new-text-styles
2 parents 30b1791 + 08eaaec commit 92a329f

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/gameobjects/Text.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
3636
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
3737
* @param {number} [style.maxLines=0] - The maximum number of lines to be shown for wrapped text.
38-
* @param {number} [style.tabs=0] - The size (in pixels) of the tabs, for when text includes tab characters. 0 disables. Can be an array of varying tab sizes, one per tab stop.
38+
* @param {string} [style.letterSpacing='0px'] - Additional spacing between letters in CSS [length]{@link https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/length} units.
39+
* @param {number|array} [style.tabs=0] - The size (in pixels) of the tabs, for when text includes tab characters. 0 disables. Can be an array of varying tab sizes, one per tab stop.
3940
* @param {object} [style.fontProperties=null] - `ascent`, `descent`, and `fontSize` lengths for a given style. You can get these from {@link Phaser.Text#determineFontProperties}.
4041
* @param {string} [style.testString='|MÂÉQfjq_'] - The text to use to measure the font width and height.
4142
*/
@@ -315,6 +316,7 @@ Phaser.Text.prototype.setShadow = function (x, y, color, blur, shadowStroke, sha
315316
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
316317
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
317318
* @param {number} [style.maxLines=0] - The maximum number of lines to be shown for wrapped text.
319+
* @param {string} [style.letterSpacing='0px'] - Additional spacing between letters in CSS [length]{@link https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/length} units.
318320
* @param {number|array} [style.tabs=0] - The size (in pixels) of the tabs, for when text includes tab characters. 0 disables. Can be an array of varying tab sizes, one per tab stop.
319321
* @param {object} [style.fontProperties=null] - `ascent`, `descent`, and `fontSize` lengths for a given style. You can get these from {@link Phaser.Text#determineFontProperties}.
320322
* @param {string} [style.testString='|MÂÉQfjq_'] - The text to use to measure the font width and height.
@@ -337,6 +339,7 @@ Phaser.Text.prototype.setStyle = function (style, update)
337339
newStyle.wordWrap = style.wordWrap || false;
338340
newStyle.wordWrapWidth = style.wordWrapWidth || 100;
339341
newStyle.maxLines = style.maxLines || 0;
342+
newStyle.letterSpacing = style.letterSpacing || '0px';
340343
newStyle.shadowOffsetX = style.shadowOffsetX || 0;
341344
newStyle.shadowOffsetY = style.shadowOffsetY || 0;
342345
newStyle.shadowColor = style.shadowColor || 'rgba(0,0,0,0)';
@@ -440,7 +443,7 @@ Phaser.Text.prototype.updateText = function ()
440443
// Simple layout (no tabs)
441444
var lineWidth = this.style.strokeThickness + this.padding.x;
442445

443-
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
446+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0 || this.letterSpacing !== '0px')
444447
{
445448
lineWidth += this.measureLine(lines[i]);
446449
}
@@ -469,7 +472,7 @@ Phaser.Text.prototype.updateText = function ()
469472
{
470473
var section = 0;
471474

472-
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
475+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0 || this.letterSpacing !== '0px')
473476
{
474477
section = this.measureLine(line[c]);
475478
}
@@ -491,7 +494,7 @@ Phaser.Text.prototype.updateText = function ()
491494
for (var c = 0; c < line.length; c++)
492495
{
493496
// How far to the next tab?
494-
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
497+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0 || this.letterSpacing !== '0px')
495498
{
496499
lineWidth += this.measureLine(line[c]);
497500
}
@@ -548,6 +551,7 @@ Phaser.Text.prototype.updateText = function ()
548551
this.context.font = this.style.font;
549552
this.context.strokeStyle = this.style.stroke;
550553
this.context.textBaseline = 'alphabetic';
554+
this.context.letterSpacing = this.style.letterSpacing;
551555

552556
this.context.lineWidth = this.style.strokeThickness;
553557
this.context.lineCap = 'round';
@@ -768,6 +772,11 @@ Phaser.Text.prototype.measureLine = function (line)
768772
this.updateShadow(this.style.shadowFill);
769773
}
770774

775+
if (this.letterSpacing && this.letterSpacing !== '0px')
776+
{
777+
this.context.letterSpacing = this.letterSpacing;
778+
}
779+
771780
lineLength += this.context.measureText(letter).width;
772781

773782
this._charCount++;
@@ -2087,6 +2096,29 @@ Object.defineProperty(Phaser.Text.prototype, 'lineSpacing', {
20872096

20882097
});
20892098

2099+
/**
2100+
* @name Phaser.Text#letterSpacing
2101+
* @property {string} letterSpacing - Additional spacing between letters in CSS [length]{@link https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/length} units.
2102+
* Uses [CanvasRedneringContext2D.letterSpacing]{@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/letterSpacing} property.
2103+
*/
2104+
Object.defineProperty(Phaser.Text.prototype, 'letterSpacing', {
2105+
2106+
get: function ()
2107+
{
2108+
return this.style.letterSpacing;
2109+
},
2110+
2111+
set: function (value)
2112+
{
2113+
if (value !== this.style.letterSpacing)
2114+
{
2115+
this.style.letterSpacing = value;
2116+
this.dirty = true;
2117+
}
2118+
}
2119+
2120+
});
2121+
20902122
/**
20912123
* @name Phaser.Text#shadowOffsetX
20922124
* @property {number} shadowOffsetX - The shadowOffsetX value in pixels. This is how far offset horizontally the shadow effect will be.

typescript/phaser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5140,6 +5140,7 @@ declare module Phaser {
51405140
wordWrap?: boolean;
51415141
wordWrapWidth?: number;
51425142
maxLines?: number;
5143+
letterSpacing?: string;
51435144
shadowOffsetX?: number;
51445145
shadowOffsetY?: number;
51455146
shadowColor?: string;
@@ -5197,6 +5198,7 @@ declare module Phaser {
51975198
game: Phaser.Game;
51985199
input: Phaser.InputHandler;
51995200
inputEnabled: boolean;
5201+
letterSpacing: string;
52005202
lineSpacing: number;
52015203
name: string;
52025204
padding: Phaser.Point;

0 commit comments

Comments
 (0)