Repository navigation
Conversation
This was referenced Nov 14, 2026
certik
marked this pull request as ready for review
November 14, 2026 18:28
certik
force-pushed
the
recoverable-errors
branch
from
November 14, 2026 21:22
55bbddf to
9371c51
Compare
certik
force-pushed
the
recoverable-errors
branch
from
November 14, 2026 21:57
9371c51 to
688e10b
Compare
jalvesz
reviewed
Nov 15, 2026
| c = glyph_recompose(cps(n), code) | ||
| if (c > 0) then | ||
| cps(n) = c | ||
| if (present(last)) last(n) = i - 1 |
Collaborator
There was a problem hiding this comment.
I would advise against using if(present( )) clauses within the body of procedures. I would find safer to use an internal allocatable working array which's memory is move_alloc(ed) to the optional output arrays if they are present. That way the core of the body remains independent of a conditional check.
I would typically try to have all if(present( )) checks and allocations at the start of the procedure body, and the rest of the body just the actual work.
A plot could only be drawn straight into an encoder, so nothing could hold a finished drawing, place it inside another or put several on pages. * renderer_t gains begin_frame(matrix, clip) and end_frame, which scope an affine transform and a rectangular clip. Like every other method they are non_overridable and checked once, in renderer_t: an open canvas, a finite matrix within MAX_COORD, a clip of nonnegative size. Frames and groups nest, each ending inside whatever began after it, and none may be open at close_canvas. Backends implement begin_frame_impl and end_frame_impl: SVG, PDF and EPS write a group with a transform, cm and concat; the PNG rasterizer applies the matrix to the outlines before filling and clips them to the frame rectangles. A target that cannot transform keeps the defaults, which accept only the identity without a clip and stop on any other frame instead of ignoring it. * In the PNG rasterizer, a frame's outlines are mapped onto the canvas before the far-off cut, which then works in canvas pixels as it does without a frame. A dashed stroke is still built in the frame's own coordinates, but what the walk may skip is decided on the canvas: a segment is clipped to the canvas there, and a segment whose pattern is finer than half a pixel on the canvas is drawn solid at the cover the pattern gives, as a whole stroke that fine already is. So a frame that shrinks, shears or stretches unevenly neither changes what the shortcuts draw nor makes a walk long. Curves and discs are flattened as finely as the frame magnifies them, up to the most they get. * fplotlib_scene has frame_t, an owned recording of drawing calls with a size, a baseline and ink bounds; import_frame copies a child into a frame once and place draws it, transformed and clipped, as often as wanted. recording_renderer_t records any drawing into a frame, implementing only the *_impl methods, so renderer_t checks every call it records; paged_document_t holds pages of frames, of any sizes. Frames own their data: the inputs, and an imported child, may change or go away. * fplotlib_draw:render_frame captures the current figure as a frame, and fplotlib_mathtext:math_frame a formula. * fplotlib_document exports a paged document to bytes in memory, one PDF or one SVG, PNG or EPS per page, or saves it to files. Only save_document can fail at run time; its status is optional, as savefig's is, and without it a file that cannot be written stops with exit code 2. The PDF backend writes several pages (new_page, which no group or frame may run across), sharing their fonts, images and transparency states. * A glyph run keeps the font of every glyph, its logical UTF-8 text and the byte range each glyph came from; text_layout fills them in, and a formula maps its glyphs to its TeX source. PDF text carries its source as ActualText; a formula's TeX is not used as ActualText. * EPS images are clipped like everything else. The SVG and PNG test pictures are unchanged; the PDFs gain ActualText and the EPS files of images their clip. tests/test_frames.f90 checks that frames own what they record, the source mapping, and a captured plot replayed after clf; tests/test_frame_backends.f90 checks PDF resources and pages, text, transformed and clipped frames in PNG, and dashed strokes reaching 1e5 points off in seven transformed frames, from a scale of 1e-4 to a shear and an uneven stretch: they finish, and wherever the pattern is no finer than half a pixel on the canvas they draw what walking every dash draws. tests/test_contract.f90 checks the stops of six broken frame calls (41 in all).
The first review of the port found that a dashed stroke in a frame could still walk for minutes. The box on the canvas was widened by the pen times the most the frame stretches anything, in every direction, so a line along an axis the frame shrinks was walked over all of that: a frame of 1e9 along x and 1e-3 along y took 29 s and 20 GB, then overflowed. And the canvas cut was taken back into the frame through the inverse matrix, which for a shear all but singular multiplied a fifth of a pixel of rounding by 1e16: 60 s and 3.5 GB, and an integer overflow. A segment is now cut in the frame's own coordinates: each side of the canvas is a line there, matrix(1)*x + matrix(3)*y + matrix(5) = bx0 and so on, widened by the pen's reach as the matrix stretches that axis, and where the segment crosses it is a fraction along the segment, nothing taken back through an inverse. The walk is then as short as without a frame: those two cases take 0.01 s and 0.9 s. Where a segment's pattern is at least half a pixel on the canvas the dashes are still what walking every one draws; the review's sweep of six frames, three paths, three caps and three widths finds no other difference. Also from the review: - Frames nested so deep that together they map past the largest double took the PNG rasterizer to NaN and an integer conversion of it; they now draw nothing, as a shape whose place is past the largest double. - A frame placed twice wrote its SVG group ids twice; a repeated name is now written name-2, name-3 and so on. - test_frame_backends skipped comparing a frame with the full walk when any direction was too fine, so the uneven frames were never compared; it now skips only a path that has a segment too fine to walk, and adds the two cases above and the deep nesting, which have to finish. - The docs no longer say an uneven frame changes nothing the shortcuts draw without saying how, nor that ActualText carries each glyph's range, nor that save_document checks dpi for any format but PNG.
The second review of the port found two ways a dashed stroke in a frame still walked without bound: - A frame that stretches the pen far more than the line, a shear of 1e8 say, makes a thin line's pen a sliver far longer than the canvas, so every dash along it really reaches the canvas: walked exactly, that took 19 s and 1.2 GB at 1e8, and ran out of memory at 1e10. Each pixel the pen sweeps then sees more dashes than the canvas has pixels, and the pattern shows as its cover; a segment whose visible piece holds more periods than twice the canvas's width and height in pixels is now drawn solid at that cover, as a pattern finer than half a pixel is. That bounds every walk by the canvas. - Frames nested to 1e300, finite together, still mapped a point of 1e9 past the largest double, and the cut became NaN, which walked the whole segment or ran out of memory, depending on the compiler. Frames that together stretch or move by more than 1e100 now place nothing, as a shape past the largest double is left out; below that every point a frame can hold maps to a finite one. Those cases now take 0.01 s, and the review's sweep of 2286 frames, paths, caps and widths still finds no difference from the full walk where the pattern is at least half a pixel on the canvas. Also from the review: - The recorder kept every point it was given, not only those the verbs use, so ink bounds could reach a point no part of the path; it now keeps only those. - SVG group ids are unique even against a name given as name-2. - The test's rule for a segment too fine to walk is the rasterizer's, the PNG's dpi and even patterns included, and its check that the degenerate frames finish comes after drawing them, which now include the shear of 1e8 and a dashed line in frames nested to 1e300. - The docs say what bounds the walk, that new_page is the PDF backend's, and that a PNG page too large to rasterize is a caller's check, as its size is known before it is exported.
…inite The third review of the port found that a34c815's period bound drew visible dashes solid. It counted periods over the piece frame_cut keeps, whose sides are widened by the pen as the matrix stretches each canvas axis, which overstates by far how far along the line a pen turned at an angle reaches: a frame turned 45 degrees and stretched 1e4 across the line drew 4 px stripes 11 px apart as flat grey, and a plain move with a 10000 pt butt pen 5.5 px stripes. In a frame, the piece is now narrowed as the plain path narrows a wide butt pen, for every cap: to the dashes whose place along the line falls within the span of the canvas's corners projected onto it, and a cap's length and two pixels beyond. Only those are counted and walked, so more of them than twice the canvas's width and height in pixels means stripes less than about half a pixel apart, where the solid cover is the documented approximation. Both cases are now what the full walk draws, and the review's sweep of 3755 random frames finds no difference; a shear of 1e8 still takes 0.03 s. Also from the review: - blit_affine_image divided by det times the scale, which underflows where neither does, and 0/0 then passed the range test as NaN into an integer conversion: it now divides by each in turn, and the test fails a NaN. - SVG group ids took a scan of every id written per suffix tried, cubic in the number of repeats: 4000 groups of one name took 79 s. Each name now keeps its next suffix: 0.07 s. - The degenerate frames are checked to finish within 20 s of CPU, not only to finish, and the transformed-dash test adds the turned stretch, the plain move and a 10000 pt pen. - The docs say what is counted, and that the 1e100 cap applies to each frame with those around it, and to the frames inside.
…switch The fourth review of the port found visible dashes still drawn solid: in a frame squeezed 5000 times across a line, a 10000 pt butt pen with dashes 4 px long was drawn as a grey band. a9778da narrowed each segment to the canvas corners' span along it, but widened that span by two canvas pixels measured along the direction the matrix shrinks most, and frame_cut by a round pen's reach a butt end does not have, so it counted 5000 periods where 33 cross the canvas. That count was the wrong test anyway: what the eye sees is how far apart the dashes' stripes lie on the canvas. - A segment is drawn solid exactly where its stripes are less than half a pixel apart. The stripes are the images of the lines where the pattern stands still, the segment's normal n mapped by the matrix, a period apart along the segment, so |det| / |M n| of a period apart across them. That is never more than the spacing along the segment, the old test, and the same without a frame. A frame that squeezes across a line makes stripes almost along it, which are sub-pixel even where each dash is long; the old test drew those by walking, which the review's case shows is right, and a path whose segments overlap so differs from the walk only where the documented approximation already did. - The span along the line is that of the canvas widened by two pixels, its corners mapped into the frame, which holds every point that touches a pixel; only a round or square cap's length is added. Stripes at least half a pixel apart then cross it at most 2(W + H + 8) + 2 times, and a cap adds at most 100 periods at either end, so the count of periods walked is kept only as a guard that no such segment exceeds. local_canvas no longer returns a margin, and an infinite corner leaves the segment as frame_cut cuts it. - Where a walked segment meets one drawn solid, the dash running through the corner ends or starts with the join, through a capless stub as at a canvas cut; the review's case lost the mitre wedge there. A dash ending exactly at the corner is a tie that rounding decides, as in the walk. - The review's own sweeps, with their skip rule moved to the new test, compare 3329 random frames three ways and 1962 sweep cases, and find no difference from the full walk; the timing fuzz's worst case takes 0.02 s. - Tests: the squeezed frame joins the transformed-dash check; a line along it, with each cap, is compared on its own; and the joins at a switch between walked and solid segments, in both orders, with each cap, join and six phases, are compared outside the solid segment's pen. - IDENTITY in fplotlib_scene is now IDENTITY_MATRIX, and export_document takes 100 dpi by default, as savefig does.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The last of the API refactoring taken out of #68, now ported onto main. It was first built on #83's first commit; #83 has since landed as #85–#89, so this PR is one commit on top of main.
A plot could only be drawn straight into an encoder. Nothing could hold a finished drawing, place it inside another, or put several drawings on pages.
renderer_tgainsbegin_frame(matrix, clip)andend_frame(), which scope an affine transform and a rectangular clip.non_overridableand checked once, inrenderer_t. The canvas must be open, the matrix finite and withinMAX_COORD, and the clip a box of nonnegative size.close_canvas.begin_frame_implandend_frame_impl. SVG, PDF and EPS write them natively. The PNG rasterizer transforms outlines before filling and clips them to the frame rectangles.period·|det M| / |M n|, withnthe segment's normal. This is never more than the spacing along the segment, and the same without a frame. It extends the documented approximation and bounds every walk by the canvas, as stripes at least half a pixel apart cross it at most about2(W+H)times. Everywhere else the dashes are what walking every one draws, and the join where a walked segment meets a solid one is kept.fplotlib_sceneadds:frame_t: an owned recording with a size, baseline and ink bounds.import_framecopies a child in once, andplacedraws it, transformed and clipped, as often as wanted.recording_renderer_t: records any drawing into a frame. It implements only the*_implmethods, so every call it records is checked.paged_document_t: holds pages of frames, which can have different sizes.fplotlib_draw:render_framecaptures the current figure as a frame;fplotlib_mathtext:math_framedoes the same for a formula.fplotlib_documentexports a paged document to bytes in memory (export_document) or to files (save_document): one PDF, or one SVG, PNG or EPS per page.save_document'sstatusis optional, assavefig's is: without it, a file that cannot be written stops with exit code 2.new_page, which no group or frame may run across) that share their resources.ActualText; formula TeX is not used asActualText.SVG and PNG test pictures are byte-identical to main's. PDFs gain
ActualText, and the EPS files of image plots gain their clip.Tests. They pass with gfortran, gfortran
-O3 -march=native, flang, flang-O3 -march=nativeand LFortran, under the sanitizers withfloat-cast-overflow, and the docs build.tests/test_frames.f90: frames owning their inputs, source mapping, and a captured plot replayed afterclf.tests/test_frame_backends.f90:tests/test_contract.f90: six broken frame calls stop with their rule (41 in all).tests/test_frame_backends.f90also draws the degenerate cases the first review found (a frame stretched 1e9 by 1e-3, a shear all but singular with a pen of 1e6, a shear of 1e8, and dashed lines and images in frames nested to 1e300 and past it), which have to finish.After this, the remaining differences from #68 are even-odd PNG filling and matrices/cases/aligned/
\binomin math (separate PRs to come), and the error policy: #68 returns astatus_tfrom every call, while main stops on broken preconditions.