Skip to content

Commit 9af10ab

Browse files
fmalitaskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[demos] Improved text-on-path glyph alignment
Instead of aligning the bottom-left corner, align the bottom-mid point with the path tangent. Change-Id: I0274f20adafd8de4e1ccc141535005bd7e00083b Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1294776 Commit-Queue: Kaylee Lubick <kjlubick@google.com> Auto-Submit: Florin Malita <fmalita@google.com> Reviewed-by: Kaylee Lubick <kjlubick@google.com>
1 parent 87411a6 commit 9af10ab

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

demos.skia.org/demos/canvas_edit/editor.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,23 @@ export class CanvasPathEditor {
376376
if (clusters.length > 0) {
377377
for (let i = 0; i < clusters.length; i++) {
378378
const cluster = clusters[i];
379-
const pt = this.path.getPointAtLength(cluster.x, this.textAlign, metrics.width || 0);
379+
const nextX = i + 1 < clusters.length ? clusters[i + 1].x
380+
: (cluster.width ? cluster.x + cluster.width
381+
: (metrics.width || 0));
382+
const clusterWidth = nextX - cluster.x;
383+
const centerS = cluster.x + clusterWidth * 0.5;
384+
const pt = this.path.getPointAtLength(centerS, this.textAlign, metrics.width || 0);
380385

381386
ctx.save();
382387
ctx.translate(pt.x, pt.y);
383388
ctx.rotate(pt.angle);
384389

385390
if (this.hasFillTextCluster(ctx)) {
386-
ctx.fillTextCluster(cluster, -cluster.x, -cluster.y);
391+
ctx.fillTextCluster(cluster, -centerS, -cluster.y);
387392
} else {
388393
// Fallback rendering
389-
ctx.fillText(cluster.text || this.text.slice(cluster.start, cluster.end), 0, 0);
394+
ctx.fillText(cluster.text || this.text.slice(cluster.start, cluster.end),
395+
-clusterWidth * 0.5, 0);
390396
}
391397
ctx.restore();
392398
}

0 commit comments

Comments
 (0)