CFF hflex and hflex1: second curve doesn't return to the starting y

Open Beginner friendly
#437 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Start in src/glyph/CFFGlyph.js and inspect the hflex and hflex1 operator handling, using the Type 2 charstring spec cited in the issue to confirm the endpoint rules. Compare the affected glyph path commands with fontTools' RecordingPen output for a reproducing CFF OTF. Done when both operators return to their starting y and the resulting paths match the reference.

Written by the indexing model from the issue text.

Description

In src/glyph/CFFGlyph.js (still on master, and in 2.0.4), the hflex (12 34) and hflex1 (12 36) operators leave the second curve at the raised y. The Type 2 charstring spec (Adobe TN #5177) says both end at the y they started from. Every point after the flex is shifted by the flex's dy, which shows up as steps or misplaced contours when drawing the path.

hflex dx1 dx2 dy2 dx3 dx4 dx5 dx6: the second curve is (dx4, 0) (dx5, -dy2) (dx6, 0), so c5y should drop back to the start. Currently:

c5y = c4y;   // should be: c5y = y;
c6y = c5y;

hflex1 dx1 dy1 dx2 dy2 dx3 dx4 dx5 dy5 dx6: dy6 is implied so the curve ends at the starting y. Currently:

c5y = c4y + stack.shift();
c6x = c5x + stack.shift();
c6y = c5y;   // should be: c6y = y;

(y is still the start point here because it's only updated after c6y.)

Repro: any CFF OTF whose charstrings use hflex/hflex1. Compare font.glyphForCodePoint(cp).path.commands with fontTools' RecordingPen output for the same glyph. In one font, a glyph whose contour should close at (415, -10) ends at (415, 0) in fontkit (hflex with dy2 = 10). In another, an hflex1 glyph's last point comes out at y 147 where fontTools gives 171. With the two one-line changes above, fontkit matches fontTools.

flex (12 35) and flex1 (12 37) look correct.

Dominant language
JavaScript
Stars
1.7k
Forks
254
Avg merge
1d 13h
Merged PRs (30d)
22

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from foliojs/fontkit

All issues in foliojs/fontkit

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.