Skip to content

Commit 427018d

Browse files
committed
You can now provide the TextMetrics as part of the config object, to avoid the expensive canvas scanning needed.
Text.getTextMetrics and TextStyle.getTextMetrics added to faciliate this. Plus added to toJSON.
1 parent e2576fe commit 427018d

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'bbed2b40-2a8c-11e7-9394-818efff01abb'
2+
build: '9ef13fb0-2a8f-11e7-ae43-2f2e7e8ba14e'
33
};
44
module.exports = CHECKSUM;

v3/src/gameobjects/text/TextStyle.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Class = require('../../utils/Class');
2+
var GetObjectValue = require('../../utils/object/GetObjectValue');
23
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
34
var MeasureText = require('./MeasureText');
45

@@ -59,7 +60,22 @@ var TextStyle = new Class({
5960
}
6061
}
6162

62-
this.metrics = MeasureText(this);
63+
var metrics = GetObjectValue(style, 'metrics', false);
64+
65+
// Provide optional TextMetrics in the style object to avoid the canvas look-up / scanning
66+
// Doing this is un-done if you then change the font of this TextStyle after creation
67+
if (metrics)
68+
{
69+
this.metrics = {
70+
ascent: GetObjectValue(metrics, 'ascent', 0),
71+
descent: GetObjectValue(metrics, 'descent', 0),
72+
fontSize: GetObjectValue(metrics, 'fontSize', 0)
73+
};
74+
}
75+
else
76+
{
77+
this.metrics = MeasureText(this);
78+
}
6379
},
6480

6581
reset: function ()
@@ -265,6 +281,17 @@ var TextStyle = new Class({
265281
return this.update(false);
266282
},
267283

284+
getTextMetrics: function ()
285+
{
286+
var metrics = this.metrics;
287+
288+
return {
289+
ascent: metrics.ascent,
290+
descent: metrics.descent,
291+
fontSize: metrics.fontSize
292+
};
293+
},
294+
268295
toJSON: function ()
269296
{
270297
var output = {};
@@ -274,6 +301,8 @@ var TextStyle = new Class({
274301
output[key] = this[key];
275302
}
276303

304+
output.metrics = this.getTextMetrics();
305+
277306
return output;
278307
},
279308

v3/src/gameobjects/text/static/Text.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ var Text = new Class({
279279
return this;
280280
},
281281

282+
getTextMetrics: function ()
283+
{
284+
return this.style.getTextMetrics();
285+
},
286+
282287
toJSON: function ()
283288
{
284289
var out = Components.ToJSON(this);

0 commit comments

Comments
 (0)