2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-07 02:46:11 +00:00
|
|
|
/**
|
|
|
|
* Returns an object containing dimensions of the Text object.
|
2018-03-16 17:29:39 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.GameObjects.Text.GetTextSize
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-20 06:28:02 +00:00
|
|
|
* @param {Phaser.GameObjects.Text} text - The Text object to calculate the size from.
|
2019-05-09 10:59:10 +00:00
|
|
|
* @param {Phaser.Types.GameObjects.Text.TextMetrics} size - The Text metrics to use when calculating the size.
|
2020-09-01 17:38:44 +00:00
|
|
|
* @param {string[]} lines - The lines of text to calculate the size from.
|
2018-03-16 17:29:39 +00:00
|
|
|
*
|
2020-09-01 17:38:44 +00:00
|
|
|
* @return {Phaser.Types.GameObjects.Text.GetTextSizeObject} An object containing dimensions of the Text object.
|
2018-02-07 02:46:11 +00:00
|
|
|
*/
|
2017-03-15 01:07:58 +00:00
|
|
|
var GetTextSize = function (text, size, lines)
|
|
|
|
{
|
|
|
|
var canvas = text.canvas;
|
|
|
|
var context = text.context;
|
|
|
|
var style = text.style;
|
|
|
|
|
|
|
|
var lineWidths = [];
|
|
|
|
var maxLineWidth = 0;
|
|
|
|
var drawnLines = lines.length;
|
2018-06-20 06:28:02 +00:00
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
if (style.maxLines > 0 && style.maxLines < lines.length)
|
|
|
|
{
|
|
|
|
drawnLines = style.maxLines;
|
|
|
|
}
|
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
style.syncFont(canvas, context);
|
2017-03-15 01:07:58 +00:00
|
|
|
|
|
|
|
// Text Width
|
|
|
|
|
|
|
|
for (var i = 0; i < drawnLines; i++)
|
|
|
|
{
|
2017-04-25 18:46:13 +00:00
|
|
|
var lineWidth = style.strokeThickness;
|
2017-03-15 01:07:58 +00:00
|
|
|
|
|
|
|
lineWidth += context.measureText(lines[i]).width;
|
|
|
|
|
|
|
|
// Adjust for wrapped text
|
|
|
|
if (style.wordWrap)
|
|
|
|
{
|
|
|
|
lineWidth -= context.measureText(' ').width;
|
|
|
|
}
|
|
|
|
|
|
|
|
lineWidths[i] = Math.ceil(lineWidth);
|
|
|
|
maxLineWidth = Math.max(maxLineWidth, lineWidths[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Text Height
|
|
|
|
|
2017-04-25 18:46:13 +00:00
|
|
|
var lineHeight = size.fontSize + style.strokeThickness;
|
2017-03-15 01:07:58 +00:00
|
|
|
var height = lineHeight * drawnLines;
|
2018-09-12 15:29:38 +00:00
|
|
|
var lineSpacing = text.lineSpacing;
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-04-26 14:34:15 +00:00
|
|
|
// Adjust for line spacing
|
2019-03-15 06:31:05 +00:00
|
|
|
if (drawnLines > 1)
|
2017-03-15 01:07:58 +00:00
|
|
|
{
|
2019-03-15 06:31:05 +00:00
|
|
|
height += lineSpacing * (drawnLines - 1);
|
2017-03-15 01:07:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:34:15 +00:00
|
|
|
return {
|
|
|
|
width: maxLineWidth,
|
|
|
|
height: height,
|
|
|
|
lines: drawnLines,
|
|
|
|
lineWidths: lineWidths,
|
|
|
|
lineSpacing: lineSpacing,
|
|
|
|
lineHeight: lineHeight
|
|
|
|
};
|
2017-03-15 01:07:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetTextSize;
|