Don't iterate each chracter if entire string fits in single segment
This commit is contained in:
@@ -60,7 +60,7 @@ export function drawTextOnPath(
|
|||||||
|
|
||||||
const startOffset = offset - stride;
|
const startOffset = offset - stride;
|
||||||
const startLength = segmentM;
|
const startLength = segmentM;
|
||||||
const endM = startM + measureAndCacheTextWidth(font, text, cache);
|
const endM = startM + scale * measureAndCacheTextWidth(font, text, cache);
|
||||||
while (offset < end - stride && segmentM + segmentLength < endM) {
|
while (offset < end - stride && segmentM + segmentLength < endM) {
|
||||||
advance();
|
advance();
|
||||||
}
|
}
|
||||||
@@ -78,13 +78,14 @@ export function drawTextOnPath(
|
|||||||
reverse = beginX > endX;
|
reverse = beginX > endX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const singleSegment = startOffset + stride === offset;
|
||||||
|
|
||||||
offset = startOffset;
|
offset = startOffset;
|
||||||
segmentLength = 0;
|
segmentLength = 0;
|
||||||
segmentM = startLength;
|
segmentM = startLength;
|
||||||
x2 = flatCoordinates[offset];
|
x2 = flatCoordinates[offset];
|
||||||
y2 = flatCoordinates[offset + 1];
|
y2 = flatCoordinates[offset + 1];
|
||||||
advance();
|
advance();
|
||||||
let angleChanged = false;
|
|
||||||
|
|
||||||
const PI = Math.PI;
|
const PI = Math.PI;
|
||||||
const result = [];
|
const result = [];
|
||||||
@@ -92,24 +93,33 @@ export function drawTextOnPath(
|
|||||||
if (reverse) {
|
if (reverse) {
|
||||||
previousAngle += previousAngle > 0 ? -PI : PI;
|
previousAngle += previousAngle > 0 ? -PI : PI;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All on the same segment
|
||||||
|
if (singleSegment) {
|
||||||
|
const x = (endX + beginX) / 2;
|
||||||
|
const y = (endY + beginY) / 2;
|
||||||
|
result[0] = [x, y, (endM - startM) / 2, previousAngle, text];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0, ii = text.length; i < ii; ++i) {
|
for (let i = 0, ii = text.length; i < ii; ++i) {
|
||||||
const index = reverse ? ii - i - 1 : i;
|
const index = reverse ? ii - i - 1 : i;
|
||||||
const char = text[index];
|
const char = text[index];
|
||||||
const charLength = scale * measureAndCacheTextWidth(font, char, cache);
|
const charLength = scale * measureAndCacheTextWidth(font, char, cache);
|
||||||
const charM = startM + charLength / 2;
|
const charM = startM + charLength / 2;
|
||||||
|
let angle;
|
||||||
while (offset < end - stride && segmentM + segmentLength < charM) {
|
while (offset < end - stride && segmentM + segmentLength < charM) {
|
||||||
advance();
|
advance();
|
||||||
let angle = Math.atan2(y2 - y1, x2 - x1);
|
angle = Math.atan2(y2 - y1, x2 - x1);
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
angle += angle > 0 ? -PI : PI;
|
angle += angle > 0 ? -PI : PI;
|
||||||
}
|
}
|
||||||
if (previousAngle !== undefined && angle !== previousAngle) {
|
}
|
||||||
let delta = angle - previousAngle;
|
if (angle !== undefined) {
|
||||||
delta += delta > PI ? -2 * PI : delta < -PI ? 2 * PI : 0;
|
let delta = angle - previousAngle;
|
||||||
if (Math.abs(delta) > maxAngle) {
|
delta += delta > PI ? -2 * PI : delta < -PI ? 2 * PI : 0;
|
||||||
return null;
|
if (Math.abs(delta) > maxAngle) {
|
||||||
}
|
return null;
|
||||||
angleChanged = true;
|
|
||||||
}
|
}
|
||||||
previousAngle = angle;
|
previousAngle = angle;
|
||||||
}
|
}
|
||||||
@@ -119,7 +129,5 @@ export function drawTextOnPath(
|
|||||||
result[index] = [x, y, charLength / 2, previousAngle, char];
|
result[index] = [x, y, charLength / 2, previousAngle, char];
|
||||||
startM += charLength;
|
startM += charLength;
|
||||||
}
|
}
|
||||||
return angleChanged
|
return result;
|
||||||
? result
|
|
||||||
: [[result[0][0], result[0][1], result[0][2], result[0][3], text]];
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user