8000 fix(plugin-print)!: keep blank lines in printed text by hipstersmoothie · Pull Request #1505 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

fix(plugin-print)!: keep blank lines in printed text - #1505

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/jimp-2-0-print-keeps-blank-lines
Nov 23, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/jimp-2-0-print-keeps-blank-lines

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

What was wrong

splitLines replaced every run of line breaks with a single break (/[\r\n]+/g), so blank lines disappeared:

measureTextHeight(font, "a\n\nb"); // 2 lines, should be 3
image.print({ font, x: 0, y: 0, text: "a\n\nb" }); // "b" printed right under "a"

"a\r\n\r\nb", "\n\na" and "a\n\n" were all collapsed the same way. That made print, measureTextHeight, vertical alignment and the cb position disagree with the text. (measureText already split at every break, so its result doesn't change.)

Two related layout bugs in the same code:

  • A long word after a line break wasn't split correctly. With "a\nxxxxxxxxxx…" and a small maxWidth, the newline stayed inside the word, so the first piece of it was printed on the same line as a ([["a", "\nxxxxx"], …]).
  • Spaces at a wrap point made phantom blank lines. For example, "aaaa bbbb" wrapped between the words gave [["aaaa"], [""], ["bbbb"]]: three lines with an empty one in the middle. Leading and trailing runs of spaces did the same.

What changed

splitLines now splits the text at line breaks (\n, \r\n, \r, the same regex measureText uses) and wraps each line separately (wrapLine). The wrapping itself is the same as #1498: whole words, splitting words wider than maxWidth, kerning, and incremental measuring.

  • Blank lines are kept. Every line break starts a new line, including leading and trailing ones. "a\n\nb" is three lines, "\n" is two and "" is still one. measureTextHeight, alignmentY and the cb's y all count empty lines.
  • measureText still returns the widest line. Empty lines are 0 wide.
  • Spaces (unchanged): runs of spaces and lines of only spaces are printed as they are. Leading spaces indent a line, and a whitespace-only line is a line. The only change is at a wrap point: the spaces where maxWidth wraps the text are dropped, so wrapping never creates a line of only spaces. A space-only or empty line is only kept when the source text had one.
  • cb still gets { x: x + widestLine, y: bottomOfLastLine }. Trailing empty lines move y down, since they're lines.
  • README and the print/measureText/measureTextHeight JSDoc describe the new behaviour.

Tests and snapshots

  • No existing snapshot changed. Every existing snapshot has at most one line break in a row, and no spaces at a wrap point.
  • New snapshot text with blank lines prints "One\n\nTwo\r\n\r\n\r\nThree" with SANS_16_BLACK. It shows a one-line gap after "One" and a two-line gap after "Two".
  • New blank lines and spaces tests run on a 4px test font. They cover:
    • splitLines with \n, \r\n and \r, and with leading, trailing and empty text
    • measureTextHeight with and without wrapping
    • measureText
    • printed rows
    • bottom alignment
    • cb positions
    • kept runs of spaces and space-only lines
    • spaces at a wrap
    • l 8000 ong words after a break
  • 8 of the 10 new tests fail on main. The other 2 cover behaviour that stays the same.

BREAKING CHANGE

Every line break starts a new line, so "a\n\nb" prints three lines (an empty one between a and b) instead of two. Leading and trailing line breaks add empty lines. measureTextHeight, vertical alignment and the y passed to cb count those lines. Spaces at a point where text wraps at maxWidth are dropped instead of being left on a line of their own.

🤖 Generated with Claude Code

Every line break (\n, \r\n or \r) now starts a new line, so blank lines,
including leading and trailing ones, are printed and counted by
measureTextHeight instead of being collapsed. splitLines splits at line
breaks first and wraps each line on its own, which also splits a long
word that follows a line break at maxWidth.

Spaces are still printed as they are, but the spaces where a line wraps
no longer end up on a line of their own.

BREAKING CHANGE: "a\n\nb" prints three lines instead of two, and
measureTextHeight, vertical alignment and the cb's y count empty lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added bug there is a bug in the way jimp behaves major Increment the major version when merged labels Nov 23, 2026
@hipstersmoothie
hipstersmoothie merged commit 8f1f4ca into main Nov 23, 2026
5 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/jimp-2-0-print-keeps-blank-lines branch November 23, 2026 18:29
hipstersmoothie added a commit that referenced this pull request Nov 24, 2026
* docs: Kid Studio redesign with full method reference and playgrounds (WIP)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: register sharpen, toColorSpace, otsuThreshold and the Color Spaces guide

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: register skew, affine, exposure, vibrance and the Migrate to v2 guide

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: update effects and compositing pages for 2.0

Add the sharpen page and playground op. Document dither's type and
palette options, convolute/convolution divisor, bias, preserveAlpha
and even-sized kernels, blur/gaussian radius changes, region clamping
in pixelate, convolute and blit, composite across Jimp classes with
clamped opacity and the new blend-mode error, mask luminance and alpha,
centered shadow size, and invalid boxShadow colors. Remove the cautions
for bugs fixed in 2.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: replace obsolete gotchas in WRITING.md with version-note guidance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: update size, shape and transform pages for 2.0; add skew and affine

- New skew and affine method pages with playgrounds (#1515)
- resize/scale/scaleToFit: LANCZOS, LANCZOS2, MITCHELL (#1481),
  premultiplyAlpha (#1502), default resizer premultiplies (#1499),
  rounding fix
- cover/contain: { x, y } fractional align (#1513), missing axis centered
  and stricter flag validation (#1499), premultiplyAlpha
- crop: clamps to the image and throws for empty regions (#1501, #1511)
- autocrop/autocropRect: ignoreSides/leaveBorder with cropOnlyFrames,
  transparent pixels match (#1499)
- rotate: centered rotation and new canvas size (#1490), ResizeStrategy
  modes interpolate (#1508)
- circle radius 0, border invalid colors throw, displace alpha/stretched
  map and offset { x, y } (#1499, #1497, #1514)
- Playground: rotate interpolation modes, fractional align for
  cover/contain, displace y offset, new skew and affine ops

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: update color, pixel and constants pages for 2.0

- color(): multiply action, CSS colors in mix/multiply/xor, greyscale action
  matching greyscale(), ColorActionName exported, changed saturate/lighten maths
- new pages: exposure(), vibrance(), otsuThreshold()
- sepia(amount), threshold max "otsu" and per-channel autoGreyscale:false,
  contrast around 127.5, posterize nearest level, normalize flat channels,
  greyscale rounding, quantize colors validation
- pixels: setPixelColor ignores off-image points, NaN throws, Edge.WRAP any
  distance, Edge as a runtime export, scan/scanIterator region clamping
- concepts: color spaces section, Edge and ColorActionName, new
  ResizeStrategy members, JimpMime.ico, align rules, exported type names
- playground: exposure, vibrance and otsuThreshold ops, multiply action,
  sepia amount, threshold otsu toggle, getPixelIndex uses Edge

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: update text, comparison and errors pages for 2.0

- print: box layout from x/y, alignment without maxWidth/maxHeight,
  whole-word wrapping, kept blank lines, cb position (#1498, #1505)
- loadFont: HTTP/fetch errors, non-PNG page errors, bundled font coverage
  (#1475, #1498, #1509)
- measureText/measureTextHeight: widest line, optional maxWidth, empty lines
- hash/pHash/distance/distanceFromHash: options added in 2.0, hashes
  differ from 1.x (#1466, #1472)
- compareHashes: base argument, decodes hash() output (#1499)
- diff: options object, DiffColor, dark diff example (#1510)
- errors-and-validation: re-verified every message; colors, regions,
  ENOENT, ICO/BMP/SVG errors (#1497, #1501, #1503, #1506, #1518, #1522)
- playground: alignment without a box, blank-line preset, diffMask,
  diffColor and alpha controls

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: update loading, saving, formats and color space pages for 2.0

- read/fromBuffer: Uint8Array/Blob input, ENOENT for missing files,
  exifRotate and ReadOptions
- fromBitmap/new Jimp: data is copied and length-checked, colorSpace,
  invalid colors throw
- getBuffer/write: icc option, BMP bitPP, ICO, per-extension option
  typing; remove dead BMP/PNG options; PNG bitDepth 16 is broken
- clone copies mime, background and ICC profile
- New toColorSpace page; cssColorToHex throws and takes color()
- Formats: ICO, JPEG 2000, SVG, strict WASM options, community
  plugins (jimp-heic)
- Guides: Color Spaces restyle, exifRotate, JPEG 2000 and SVG sections
  with bundler notes, corrected JPEG memory limits, browser fonts path
  and fromBitmap copying, ICO in defaults, Node 22
- Playground encode op gains ICO; webp-example casts around the
  wasm-webp declaration bug

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: write the Migrate to v2 guide and update guides and home for 2.0

- guides/migrate-to-v2: every breaking change merged for 2.0 (Node 22,
  errors that now throw, behavior changes, removed options, types), with
  before/after code checked against 1.7.0 and the 2.0 build, plus a
  "New in 2.0" section linking the new methods, options and formats
- guides/migrate-to-v1: point to the v2 guide; note Edge is a runtime
  export only since 2.0 and that opaque PNGs drop alpha automatically
- guides/getting-started: Node.js 22, Jimp.read path vs URL rules and
  input types, ENOENT for missing files, new WASM formats
- guides/writing-plugins: the format detect() hook, color spaces in
  plugins (CSS colors, keeping iccProfile on new canvases), 2.0 versions
- home and methods index: sharpen and roundCorners in the playground,
  ICO and the new WASM formats in the feature copy, 2.0 methods in the
  section intros

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: flag stored-hash incompatibility and add #1357 and morphology to the v2 guide

- Caution near the top: pHash/hash values differ for every image in 2.0,
  so 1.x hashes can't be compared with 2.0 hashes; re-hash stored images
- Hashes section explains the degenerate 1.x hashes and cross-version
  compareHashes/distanceFromHash
- Jimp.read now passes decode options for file paths and buffers (#1357)
- New in 2.0: erode, dilate, open and close (#1523)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: add erode, dilate, open and close method pages

Document @jimp/plugin-morphology (#1523): four method pages with
scanned-document cleanup, mask, outline and ink examples, playground
ops with radius and shape controls, and method entries under Blur &
effects. Link the new pages from the v2 migration guide, threshold and
dither.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.0.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 24, 2026
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 major Increment the major 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

0