Add lineSpacing property to BitmapText

This commit is contained in:
arbassic 2022-12-05 22:10:45 +01:00
parent 83cf8d12fc
commit aa0d9be861
3 changed files with 66 additions and 3 deletions

View file

@ -73,6 +73,7 @@ var GetBitmapTextSize = function (src, round, updateOrigin, out)
var chars = src.fontData.chars; var chars = src.fontData.chars;
var lineHeight = src.fontData.lineHeight; var lineHeight = src.fontData.lineHeight;
var letterSpacing = src.letterSpacing; var letterSpacing = src.letterSpacing;
var lineSpacing = src.lineSpacing;
var xAdvance = 0; var xAdvance = 0;
var yAdvance = 0; var yAdvance = 0;
@ -128,7 +129,7 @@ var GetBitmapTextSize = function (src, round, updateOrigin, out)
} }
xAdvance = 0; xAdvance = 0;
yAdvance += lineHeight; yAdvance += lineHeight + lineSpacing;
lastGlyph = null; lastGlyph = null;
continue; continue;
@ -291,7 +292,7 @@ var GetBitmapTextSize = function (src, round, updateOrigin, out)
} }
xAdvance = 0; xAdvance = 0;
yAdvance += lineHeight; yAdvance += lineHeight + lineSpacing;
lastGlyph = null; lastGlyph = null;
lineWidths[currentLine] = currentLineWidth; lineWidths[currentLine] = currentLineWidth;

View file

@ -153,6 +153,18 @@ var BitmapText = new Class({
*/ */
this._letterSpacing = 0; this._letterSpacing = 0;
/**
* Adds / Removes spacing lines in a multiline BitmapText object.
*
* Can be a negative or positive number.
*
* @name Phaser.GameObjects.BitmapText#_lineSpacing
* @type {number}
* @private
* @since 3.60.0
*/
this._lineSpacing = 0;
/** /**
* Controls the alignment of each line of text in this BitmapText object. * Controls the alignment of each line of text in this BitmapText object.
* Only has any effect when this BitmapText contains multiple lines of text, split with carriage-returns. * Only has any effect when this BitmapText contains multiple lines of text, split with carriage-returns.
@ -381,6 +393,28 @@ var BitmapText = new Class({
return this; return this;
}, },
/**
* The line spacing value.
* This value is added to the font height to calculate the overall line height.
* Only has an effect if this Text object contains multiple lines of text.
*
*
* @name Phaser.GameObjects.Text#lineSpacing
* @type {number}
* @since 3.60.0
*/
setLineSpacing: function (spacing)
{
if (spacing === undefined) { spacing = 0; }
this._lineSpacing = spacing;
this._dirty = true;
return this;
},
/** /**
* Set the textual content of this BitmapText. * Set the textual content of this BitmapText.
* *
@ -922,6 +956,32 @@ var BitmapText = new Class({
}, },
/**
* Adds / Removes spacing between lines.
*
* Can be a negative or positive number.
*
* You can also use the method `setLineSpacing` if you want a chainable way to change the line spacing.
*
* @name Phaser.GameObjects.BitmapText#lineSpacing
* @type {number}
* @since 3.60.0
*/
lineSpacing: {
set: function (value)
{
this._lineSpacing = value;
this._dirty = true;
},
get: function ()
{
return this._lineSpacing;
}
},
/** /**
* The maximum display width of this BitmapText in pixels. * The maximum display width of this BitmapText in pixels.
* *

View file

@ -41,6 +41,7 @@ var BitmapTextCanvasRenderer = function (renderer, src, camera, parentMatrix)
var chars = src.fontData.chars; var chars = src.fontData.chars;
var lineHeight = src.fontData.lineHeight; var lineHeight = src.fontData.lineHeight;
var letterSpacing = src._letterSpacing; var letterSpacing = src._letterSpacing;
var lineSpacing = src._lineSpacing;
var xAdvance = 0; var xAdvance = 0;
var yAdvance = 0; var yAdvance = 0;
@ -113,7 +114,8 @@ var BitmapTextCanvasRenderer = function (renderer, src, camera, parentMatrix)
} }
xAdvance = 0; xAdvance = 0;
yAdvance += lineHeight; yAdvance += lineHeight + lineSpacing;
lastGlyph = null; lastGlyph = null;
continue; continue;