Skip to content

Commit

Permalink
Replace measureText with helpers.measureText
Browse files Browse the repository at this point in the history
  • Loading branch information
nagix committed Jan 26, 2019
1 parent caec142 commit 21a6bb6
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/core/core.scale.js
Expand Up @@ -83,17 +83,6 @@ function getPixelForGridLine(scale, index, offsetGridLines) {
return lineValue;
}

function measureText(ctx, data, gc, string) {
var key = ctx.font + ';' + string;
var textWidth = data[key];

if (!textWidth) {
textWidth = data[key] = ctx.measureText(string).width;
gc.push(key);
}
return textWidth;
}

/**
* Returns {width, height, offset} objects for the first, last, widest, highest tick
* labels where offset indicates the anchor point offset from the top in pixels.
Expand All @@ -103,43 +92,50 @@ function computeLabelSizes(ctx, tickFonts, ticks, cache) {
var widths = [];
var heights = [];
var offsets = [];
var data = cache.data = cache.data || {};
var gc = cache.garbageCollect = cache.garbageCollect || [];
var gcLen, i, j, jlen, tick, label, tickFont, width, height, nestedLabel, widest, highest;
var i, j, jlen, label, tickFont, fontString, data, gc, lineHeight, width, height, nestedLabel, widest, highest;

helpers.each(tickFonts, function(font) {
cache[font.string] = cache[font.string] || {data: {}, garbageCollect: []};
});

for (i = 0; i < length; ++i) {
tick = ticks[i];
label = tick.label;
tickFont = tick.major ? tickFonts.major : tickFonts.minor;
label = ticks[i].label;
tickFont = ticks[i].major ? tickFonts.major : tickFonts.minor;
ctx.font = fontString = tickFont.string;
data = cache[fontString].data;
gc = cache[fontString].garbageCollect;
lineHeight = tickFont.lineHeight;
width = height = 0;
ctx.font = tickFont.string;
// Undefined labels and arrays should not be measured
if (!helpers.isNullOrUndef(label) && !helpers.isArray(label)) {
width = measureText(ctx, data, gc, label);
height = tickFont.lineHeight;
width = helpers.measureText(ctx, data, gc, width, label);
height = lineHeight;
} else if (helpers.isArray(label)) {
// if it is an array lets measure each element
for (j = 0, jlen = label.length; j < jlen; ++j) {
nestedLabel = label[j];
// Undefined labels and arrays should not be measured
if (!helpers.isNullOrUndef(nestedLabel) && !helpers.isArray(nestedLabel)) {
width = Math.max(width, measureText(ctx, data, gc, nestedLabel));
height += tickFont.lineHeight;
width = helpers.measureText(ctx, data, gc, width, nestedLabel);
height += lineHeight;
}
}
}
widths.push(width);
heights.push(height);
offsets.push(tickFont.lineHeight / 2);
offsets.push(lineHeight / 2);
}

gcLen = gc.length / 2;
if (gcLen > length) {
for (i = 0; i < gcLen; ++i) {
delete data[gc[i]];
helpers.each(cache, function(fontCache) {
var garbageCollect = fontCache.garbageCollect;
var gcLen = garbageCollect.length / 2;
if (gcLen > length) {
for (i = 0; i < gcLen; ++i) {
delete fontCache.data[garbageCollect[i]];
}
garbageCollect.splice(0, gcLen);
}
gc.splice(0, gcLen);
}
});

widest = widths.indexOf(Math.max.apply(null, widths));
highest = heights.indexOf(Math.max.apply(null, heights));
Expand Down

0 comments on commit 21a6bb6

Please sign in to comment.