Skip to content

Commit

Permalink
Merge pull request #9536 from ahocevar/font-cache-hits
Browse files Browse the repository at this point in the history
More font cache hits
  • Loading branch information
ahocevar committed May 15, 2019
2 parents 39de245 + 41e958e commit 11607ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
38 changes: 10 additions & 28 deletions src/ol/geom/flat/textpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {lerp} from '../../math.js';
* @param {function(string, string, Object<string, number>):number} measureAndCacheTextWidth Measure and cache text width.
* @param {string} font The font.
* @param {Object<string, number>} cache A cache of measured widths.
* @return {Array<Array<*>>} The result array of null if `maxAngle` was
* exceeded. Entries of the array are x, y, anchorX, angle, chunk.
* @return {Array<Array<*>>} The result array (or null if `maxAngle` was
* exceeded). Entries of the array are x, y, anchorX, angle, chunk.
*/
export function drawTextOnPath(
flatCoordinates, offset, end, stride, text, startM, maxAngle, scale, measureAndCacheTextWidth, font, cache) {
Expand All @@ -35,16 +35,13 @@ export function drawTextOnPath(
let y2 = flatCoordinates[offset + 1];
let segmentM = 0;
let segmentLength = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
let angleChanged = false;

let chunk = '';
let chunkLength = 0;
let data, index, previousAngle;
let index, previousAngle;
for (let i = 0; i < numChars; ++i) {
index = reverse ? numChars - i - 1 : i;
const char = text.charAt(index);
chunk = reverse ? char + chunk : chunk + char;
const charLength = scale * measureAndCacheTextWidth(font, chunk, cache) - chunkLength;
chunkLength += charLength;
const char = text[index];
const charLength = scale * measureAndCacheTextWidth(font, char, cache);
const charM = startM + charLength / 2;
while (offset < end - stride && segmentM + segmentLength < charM) {
x1 = x2;
Expand All @@ -62,33 +59,18 @@ export function drawTextOnPath(
}
if (previousAngle !== undefined) {
let delta = angle - previousAngle;
angleChanged = angleChanged || delta !== 0;
delta += (delta > Math.PI) ? -2 * Math.PI : (delta < -Math.PI) ? 2 * Math.PI : 0;
if (Math.abs(delta) > maxAngle) {
return null;
}
}
previousAngle = angle;
const interpolate = segmentPos / segmentLength;
const x = lerp(x1, x2, interpolate);
const y = lerp(y1, y2, interpolate);
if (previousAngle == angle) {
if (reverse) {
data[0] = x;
data[1] = y;
data[2] = charLength / 2;
}
data[4] = chunk;
} else {
chunk = char;
chunkLength = charLength;
data = [x, y, charLength / 2, angle, chunk];
if (reverse) {
result.unshift(data);
} else {
result.push(data);
}
previousAngle = angle;
}
result[index] = [x, y, charLength / 2, angle, char];
startM += charLength;
}
return result;
return angleChanged ? result : [[result[0][0], result[0][1], result[0][2], result[0][3], text]];
}
6 changes: 4 additions & 2 deletions test/spec/ol/geom/flat/textpath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ describe('ol.geom.flat.drawTextOnPath', function() {
const instructions = drawTextOnPath(
angled, 0, angled.length, 2, 'foo', startM, Infinity, 1, measureAndCacheTextWidth, '', {});
expect(instructions[0][3]).to.eql(45 * Math.PI / 180);
expect(instructions[0][4]).to.be('fo');
expect(instructions[1][3]).to.eql(-45 * Math.PI / 180);
expect(instructions[0][4]).to.be('f');
expect(instructions[1][3]).to.eql(45 * Math.PI / 180);
expect(instructions[1][4]).to.be('o');
expect(instructions[2][3]).to.eql(-45 * Math.PI / 180);
expect(instructions[2][4]).to.be('o');
});

it('respects maxAngle', function() {
Expand Down

0 comments on commit 11607ca

Please sign in to comment.