MCPcopy Create free account
hub / github.com/phaserjs/phaser / GetTextSize

Function GetTextSize

src/gameobjects/text/GetTextSize.js:19–92  ·  view source on GitHub ↗
(text, size, lines)

Source from the content-addressed store, hash-verified

17 * @return {Phaser.Types.GameObjects.Text.GetTextSizeObject} An object containing dimensions of the Text object.
18 */
19var GetTextSize = function (text, size, lines)
20{
21 var canvas = text.canvas;
22 var context = text.context;
23 var style = text.style;
24
25 var lineWidths = [];
26 var maxLineWidth = 0;
27 var drawnLines = lines.length;
28
29 if (style.maxLines > 0 && style.maxLines < lines.length)
30 {
31 drawnLines = style.maxLines;
32 }
33
34 style.syncFont(canvas, context);
35
36 // Text Width
37 var letterSpacing = text.letterSpacing;
38
39 for (var i = 0; i < drawnLines; i++)
40 {
41 var lineWidth = style.strokeThickness;
42
43 if (letterSpacing === 0)
44 {
45 lineWidth += context.measureText(lines[i]).width;
46 }
47 else
48 {
49 var line = lines[i];
50
51 for (var j = 0; j < line.length; j++)
52 {
53 lineWidth += context.measureText(line[j]).width;
54 }
55
56 if (line.length > 1)
57 {
58 lineWidth += letterSpacing * (line.length - 1);
59 }
60 }
61
62 // Adjust for wrapped text
63 if (style.wordWrap)
64 {
65 lineWidth -= context.measureText(' ').width;
66 }
67
68 lineWidths[i] = Math.ceil(lineWidth);
69 maxLineWidth = Math.max(maxLineWidth, lineWidths[i]);
70 }
71
72 // Text Height
73
74 var lineHeight = size.fontSize + style.strokeThickness;
75 var height = lineHeight * drawnLines;
76 var lineSpacing = text.lineSpacing;

Callers 2

Text.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…