foliojs/fontkit

Bug in Path.transform breaks Path rotation

hauswirth opened this issue · 0 comments

When rotating a Path with Path.rotate(angle), the resulting path is distorted.

fontkit/src/glyph/Path.js

Lines 202 to 208 in 417af0c

transform(m0, m1, m2, m3, m4, m5) {
return this.mapPoints((x, y) => {
x = m0 * x + m2 * y + m4;
y = m1 * x + m3 * y + m5;
return [x, y];
});
}

The cause is that the arrow function which Point.transform passes to Point.mapPoints modifies the value of its parameter x (which should hold the x-coordinate of the original point), and then uses the modified x (instead of using the original x) to compute the y-coordinate of the transformed point in its second line.

The fix is straightforward: e.g., introduce variables tx and ty for the transformed point:

    return this.mapPoints((x, y) => {
      const tx = m0 * x + m2 * y + m4;
      const ty = m1 * x + m3 * y + m5;
      return [tx, ty];
    });