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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
var lineSpacing = text._lineSpacing || 0;
|
|
|
|
|
|
|
|
if (lineSpacing < 0 && Math.abs(lineSpacing) > lineHeight)
|
|
|
|
{
|
|
|
|
lineSpacing = -lineHeight;
|
|
|
|
}
|
|
|
|
|
2017-04-26 14:34:15 +00:00
|
|
|
// Adjust for line spacing
|
2017-03-15 01:07:58 +00:00
|
|
|
if (lineSpacing !== 0)
|
|
|
|
{
|
|
|
|
height += (lineSpacing > 0) ? lineSpacing * lines.length : lineSpacing * (lines.length - 1);
|
|
|
|
}
|
|
|
|
|
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;
|