Skip to content

PNG rasterizer: exact and bounded on far-off geometry - #85

Merged
certik merged 1 commit into
mainfrom
split-1-raster
Nov 22, 2026
Merged

certik merged 1 commit into
mainfrom
split-1-raster

Conversation

@certik

@certik certik commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

PNG rasterizer: exact and bounded on far-off geometry

Geometry far outside the canvas could make the PNG rasterizer walk a dash pattern for ever, overflow its integer pixel arithmetic or crash: a dashed line a million points long, a pen a trillion points wide, a path out at 1e15. This PR changes only src/fplotlib_raster.f90, and adds its tests.

  • Clipping. clip_to_box and clip_segment cut a path to a box about the canvas before it is filled or stroked. They interpolate from the near end, and bisect where both ends are far, so nothing far off reaches the pixel arithmetic. (The plotting layer uses them in the next PR.)
  • Dash walking. stroke_dashed walks a pattern only over the part of a segment whose dashes can reach the canvas, and lands exactly on the segment's far end. For a butt-ended pen wider than the canvas, it walks only over the canvas's span along the segment. Joins at skipped vertices come back as capless stubs, only where a dash runs through.
  • One documented approximation. A pattern shorter than half a pixel, whose dashes can't be told apart, is drawn as a solid line at the share of the line it would cover; walking it dash by dash could take without end. Every other shortcut draws exactly what the full walk of every dash draws, up to the rounding of where along a very long segment the pattern stands.

full_walk, a test-only switch, turns every shortcut off. The new tests/test_missing.f90 compares the two on fixed cases, sweeps of offsets and caps, and 400 random strokes, and requires them to agree to the pixel. Every test picture is unchanged.

This is part of a stack of five PRs that together are exactly #83 (the final tree of the last one is byte for byte #83's), split so that each can be reviewed and merged on its own. Each one builds and passes the whole test suite with gfortran, gfortran -O3 -march=native, flang, flang -O3 -march=native and LFortran, and the docs build.

  1. PNG rasterizer: exact and bounded on far-off geometry #85 PNG rasterizer: exact and bounded on far-off geometry
  2. Leave missing data out of the picture; clip far-off geometry #86 Missing data and far-off geometry in the plotting layer
  3. Stop on broken preconditions; return run-time failures in status_t #87 Contract stops and status_t (the error API)
  4. Check every plotting argument from one generated table #88 Argument checks from one generated table
  5. Keep far-off data finite in ticks, dates, contours and axis limits #89 Far-off data in ticks, dates, contours and axis limits; sanitizer CI job

Merge them in order; each PR's base is the one before it. #83 went through 30 rounds of independent review, the last with no blockers.

Geometry far outside the canvas could make the PNG rasterizer walk a
dash pattern for ever, overflow its integer pixel arithmetic or crash:
a dashed line a million points long, a pen a trillion points wide, a
path out at 1e15.

- clip_to_box and clip_segment cut a path to a box about the canvas
  before it is filled or stroked, interpolating from the near end and
  bisecting where both ends are far, so that nothing far off reaches the
  pixel arithmetic.
- stroke_dashed walks a dash pattern only over the part of a segment
  whose dashes can reach the canvas, and lands on its far end exactly;
  where a wide pen with butt ends is wider than the canvas, only over the
  canvas's span along the segment. Joins at skipped vertices come back as
  capless stubs, only where a dash runs through.
- A dash pattern shorter than half a pixel, whose dashes cannot be told
  apart, is drawn as a solid line at the share of the line it would
  cover; walking it dash by dash could take without end. This is the one
  approximation, and the source documents it. Every other shortcut draws
  exactly what the full walk of every dash draws, up to the rounding of
  where along a very long segment the pattern stands.

full_walk, a switch for the tests, turns every shortcut off; the new
tests/test_missing.f90 compares the two on fixed cases, sweeps of
offsets and caps, and 400 random strokes, and requires them to agree to
the pixel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant