Skip to content

fix(plugin-print): don't throw on fonts opentype.js can't shape - #1573

Merged
hipstersmoothie merged 1 commit into
mainfrom
fix/print-unshapeable-fonts
Nov 27, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
fix/print-unshapeable-fonts

Conversation

@hipstersmoothie

Copy link
Copy Markdown
Collaborator

The bug

print, measureText and @jimp/layout's Text shape each line with opentype.js's stringToGlyphs. opentype.js applies the font's GSUB substitutions there, and it throws on lookup kinds it doesn't implement. One such lookup in an otherwise fine font made that font unusable for text.

Repro font: Hanken Grotesk (OFL, on Google Fonts). Its ccmp feature has a class-based chained-context substitution (lookup type 6, format 2), so any line of two or more characters throws:

error: substitutionType : 62 lookupType: 6 - substFormat: 2 is not yet supported
  at ccmpReplacementLigatures / applyCcmpReplacement (opentype.js)
  at shapeLine (@jimp/plugin-print/dist/esm/vector-layout.js)
const font = await loadTTF("HankenGrotesk-Regular.ttf");
image.print({ font, size: 40, x: 0, y: 0, text: "Roomy" }); // throws

Turning features off doesn't help: opentype.js applies ccmp to every line regardless of the features option, which only filters liga/rlig for Latin.

The fix

shapeLine (in vector-layout.ts) still calls stringToGlyphs first. If that throws, the line falls back to one glyph per code point via charToGlyph, without substitutions. Variation selectors are dropped, as opentype.js does. Kerning (GPOS or kern) and letter spacing are applied the same way as before, so the text is drawn without ligatures but still kerned.

  • Measuring, wrapping (splitLines) and drawing all go through shapeLine, so wrapped lines and drawn widths agree.
  • The fallback is per call: the font object isn't modified. If opentype.js later supports the lookup, the font gets full shaping again without any change here.
  • Fonts opentype.js can shape take the same path as before. Nothing changes for them.

Tests

  • New fixture fixtures/HankenGrotesk-Regular.ttf: a 20 KB subset of Hanken Grotesk Regular, plus its HankenGrotesk-OFL.txt (OFL 1.1, no Reserved Font Name). It's generated by scripts/subset-fonts.sh like the other fixtures. The subset keeps the combining marks and dotless i/j so the offending ccmp lookup survives subsetting. Checked that opentype.js still throws on it.

  • vector-font.test.ts (Node and browser) checks:

    • opentype.js throws on the fixture, even with features: {};
    • the fallback measures one glyph per code point with kerning and without the fi ligature;
    • letter spacing still applies;
    • variation selectors are dropped;
    • wrapping and the print callback's end position match measurement;
    • print draws ink inside the measured width.

    Without the fix, 5 of these fail with the error above.

  • New image snapshot of wrapped Hanken text in vector-font.node.test.ts.

  • Also checked by hand with the full, unsubsetted Hanken Grotesk Regular.

@jimp/plugin-print: 134 Node tests and 45 browser tests pass. @jimp/layout: 103 tests pass. Build, lint and typecheck of src are clean for both.

Docs

The README has a new "Substitutions" section and lists the new test font. The loadTTF docs page notes the fallback.

Not included: a features option

I left out an option to choose shaping features. opentype.js's features option only toggles liga/rlig for Latin and can't turn off ccmp, so it wouldn't help with this bug. That makes it a separate, smaller feature. It could be a follow-up if there's demand. It would thread through VectorTextLayoutSchema to stringToGlyphs.

🤖 Generated with Claude Code

opentype.js throws on GSUB lookups it doesn't implement, and applies
ccmp to every line whatever features it's given. With Hanken Grotesk,
whose ccmp has a chained-context substitution (lookup type 6, format 2),
every line threw, so print, measureText and @jimp/layout's Text couldn't
use the font.

When stringToGlyphs throws, shapeLine now maps each code point to its
glyph with charToGlyph (dropping variation selectors, as opentype.js
does), keeping kerning and letter spacing. Measuring, wrapping and
drawing all go through shapeLine, so they fall back the same way. The
font isn't changed.

Adds a subset of Hanken Grotesk (OFL) as a test fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added bug there is a bug in the way jimp behaves patch Increment the patch version when merged labels Nov 27, 2026
@hipstersmoothie
hipstersmoothie merged commit eac78c3 into main Nov 27, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the fix/print-unshapeable-fonts branch November 27, 2026 20:47
@hipstersmoothie

Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.2.1 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 27, 2026
hipstersmoothie added a commit to hipstersmoothie/roomy that referenced this pull request Nov 27, 2026
jimp 2.2.1 (jimp-dev/jimp#1573) falls back to per-character glyphs when
opentype.js can't shape a line, so Hanken Grotesk renders without
renaming its `ccmp` GSUB feature after loading. The only visible change is
that ligatures (the "ffi" in "official") draw as separate letters until
opentype.js ships type 6 format 2 (opentypejs/opentype.js#861).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug there is a bug in the way jimp behaves patch Increment the patch version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant