Skip to content

Commit 7ade4c1

Browse files
committed
Record drawing into owned frames and pages
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. SVG, PDF and EPS write them as 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 stops on a transformed frame instead of ignoring it. * 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, and 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 returns a status, for the file writes. The PDF backend writes several pages (new_page), sharing their fonts, images and transparency states, and keeps any number of images. * 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, and the backends reset their state when a canvas is opened again. Invalid handles, sizes, transforms and unbalanced frames break the rendering contract and stop with a message, like the other checks in fplotlib_render_checks. The PNG and SVG 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, and transformed and clipped frames in PNG.
1 parent 688e10b commit 7ade4c1

22 files changed

Lines changed: 2287 additions & 100 deletions

‎README.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,20 @@ written, are returned, through a required `status_t` from `fplotlib_status`.
261261

262262
`fplotlib_render:renderer_t` is a drawing target, not a file writer; only
263263
`encoded_renderer_t`, which the SVG, PNG, PDF and EPS backends extend, adds
264-
`bytes`.
264+
`bytes`. A `recording_renderer_t` from `fplotlib_scene` records the drawing
265+
into an owned `frame_t` instead, with no file or format.
266+
`fplotlib_draw:render_frame(frame)` captures the current plot that
267+
way, and `fplotlib_mathtext:math_frame` a formula. A frame's
268+
`import_frame(child, handle)` imports an owned copy;
269+
`place(handle, matrix, clip)` can reuse it at several positions. The
270+
six matrix entries are `[a,b,c,d,e,f]`, mapping a point to
271+
`(a*x+c*y+e, b*x+d*y+f)`, and the optional rectangular clip is in the child's
272+
local coordinates. A `paged_document_t` holds pages of frames, which may have
273+
different sizes; `fplotlib_document` exports it to bytes in memory
274+
(`export_document`) or to files (`save_document`), one PDF or an SVG, PNG or
275+
EPS per page. Glyph runs keep their logical UTF-8 text and the source byte
276+
range of every glyph, and PDF text carries it as `ActualText`; a formula keeps
277+
its TeX source, which is not used as `ActualText`.
265278

266279
The pixi tasks below are for developing fplotlib itself, with pinned compilers.
267280

@@ -357,6 +370,7 @@ src/ library modules
357370
fplotlib_draw.f90 layout, axes decoration, one renderer per plot type
358371
fplotlib.f90 the plotting API
359372
fplotlib_status.f90 the status_t of calls that can fail at run time
373+
fplotlib_scene.f90 owned frames, pages and a recording renderer
360374
examples/ demo.f90
361375
tests/ Fortran test plots, fingerprints, matplotlib refs, compare scripts
362376
```

‎docs/api/fplotlib_document.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# fplotlib_document module
2+
3+
`export_document(document, format, assets, dpi)` exports finished pages
4+
without plotting state or a filesystem. It returns one byte asset for PDF,
5+
or one per page for SVG, PNG and EPS. `dpi` affects PNG only.
6+
`save_document(document, filename, status, dpi)` writes the same assets
7+
directly to files. Both live here, not in the high-level plotting facade.
8+
9+
Writing a file is the one step that can fail at run time, so
10+
`save_document` alone takes a required `status_t`. Its message names the
11+
file. A multi-file export is not a filesystem transaction: the pages before
12+
the file that failed have been written. An unknown format, a document
13+
without pages or a nonpositive `dpi` is a bug in the caller and stops the
14+
program.
15+
16+
The exporters do not lay out text or math. Glyph source mappings
17+
are preserved in frames; ordinary PDF text carries `ActualText`, while
18+
TeX formula source is not presented as a Unicode replacement string.
19+
20+
```{f:module} fplotlib_document
21+
```

‎docs/api/fplotlib_mathtext.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
The layout keeps no state between calls. An optional `math_context_t` reuses
44
scratch storage; otherwise the call owns local scratch.
5+
`math_frame` returns an independently owned frame.
56

67
The input is TeX math source, not a public math-tree API. A programmatic math
78
representation, broader shaping/font selection and complete TeX compatibility

‎docs/api/fplotlib_render.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
`renderer_t` is the format-independent drawing contract. Drawing does not
44
require a file, an encoded format, or a `bytes` method. `encoded_renderer_t`
5-
extends it with `bytes` for PDF/SVG/PNG/EPS output; other targets can
6-
implement drawing alone.
5+
extends it with `bytes` for PDF/SVG/PNG/EPS output; recording and interactive
6+
targets can implement drawing alone.
77

88
The built-in text family has size, weight and slant styles. There is no
99
serif/monospace family selector that silently substitutes the sans font.
10+
Finished glyph runs identify the actual built-in glyphs they use.
1011

1112
No renderer call returns an error. A call that breaks the contract, such as
1213
drawing before `open_canvas` or asking for `bytes` before `close_canvas`,
1314
stops the program with a message (see
1415
[`fplotlib_render_checks`](fplotlib_render_checks.md)).
1516

17+
`begin_frame(matrix, clip)` and `end_frame()` scope an affine transform and a
18+
rectangular clip. Every built-in backend implements them. A custom target that
19+
does not override them accepts only the identity transform without a clip,
20+
and stops on any other rather than ignoring it.
21+
1622
```{f:module} fplotlib_render
1723
```

‎docs/api/fplotlib_scene.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# fplotlib_scene module
2+
3+
Owned frames and pages are the boundary between drawing and output.
4+
Capture a plot with `fplotlib_draw:render_frame`, obtain an equation with
5+
`fplotlib_mathtext:math_frame`, or record any drawing with a
6+
`recording_renderer_t`. Each produces the same `frame_t`, with local point
7+
coordinates, a baseline and ink bounds.
8+
9+
`import_frame` copies a child into the receiving frame's arena and returns an
10+
owner-local handle. Repeated `place` calls share that copy. The original child
11+
does not need to remain alive. The affine matrix is `[a,b,c,d,e,f]`; an
12+
optional clip is in the child's coordinates.
13+
14+
`paged_document_t` owns its pages and all captured drawing data. Built-in
15+
font data is shared read-only rather than copied into each document.
16+
17+
`source_text()` concatenates stored glyph-run source in drawing order.
18+
It includes raw TeX, so it is neither plain-text extraction nor an accessibility
19+
representation. Importing a frame does not place it; `place` controls each
20+
occurrence in the drawing order.
21+
22+
An invalid handle, size, transform or scope operation is a bug in the caller
23+
and stops the program with a message, like any other broken precondition of
24+
the rendering contract (see
25+
[`fplotlib_render_checks`](fplotlib_render_checks.md)).
26+
27+
```{f:module} fplotlib_scene
28+
```

‎docs/api/index.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fplotlib_cmap
1313
fplotlib_colors
1414
fplotlib_contour
1515
fplotlib_dates
16+
fplotlib_document
1617
fplotlib_draw
1718
fplotlib_gif
1819
fplotlib_glyphs
@@ -26,6 +27,7 @@ fplotlib_raster
2627
fplotlib_render
2728
fplotlib_render_checks
2829
fplotlib_scale
30+
fplotlib_scene
2931
fplotlib_state
3032
fplotlib_status
3133
fplotlib_style

‎src/fplotlib_artist.f90‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ subroutine append_text(b, x, y, s, anchor, fontsize, color, rot, weight, &
406406
if (math_is(s)) then
407407
call append_math(b, x, y, s, f, brush(color), an, bl, ang)
408408
else
409-
run%text = s
410409
call text_layout(s, f%size, text_face(f%weight, f%slant), 0.0_dp, &
411410
0.0_dp, run, w)
412411
call align_run(run, w, f, an, bl)
@@ -499,7 +498,6 @@ subroutine append_math(b, x, y, s, f, p, anchor, baseline, ang)
499498
real(dp) :: w, x0, y0, ca, sa, rad, rx(4), ry(4)
500499

501500
call math_layout(s, f%size, run, rules, w, text_face(f%weight, f%slant))
502-
run%text = s
503501
! The text around the math takes the weight and slant asked for, and
504502
! the math is set in its regular font whatever they are; the faces
505503
! share their metrics, so the run is aligned by the regular one.

‎src/fplotlib_backend_eps.f90‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module fplotlib_backend_eps
4242
character(len=:), allocatable :: out
4343
real(dp) :: w = 0.0_dp, h = 0.0_dp
4444
real(dp) :: x0 = 0.0_dp, y0 = 0.0_dp
45+
integer :: n_frame = 0
4546
! What a translucent color is composited against.
4647
integer :: page_rgb(3) = [255, 255, 255]
4748
! The glyphs the page has drawn, and the fonts they went into.
@@ -56,6 +57,8 @@ module fplotlib_backend_eps
5657
procedure :: draw_markers => eps_draw_markers
5758
procedure :: draw_glyphs => eps_draw_glyphs
5859
procedure :: draw_image => eps_draw_image
60+
procedure :: begin_frame => eps_begin_frame
61+
procedure :: end_frame => eps_end_frame
5962
end type eps_renderer_t
6063

6164

@@ -193,6 +196,8 @@ subroutine eps_open_canvas(self, width, height, bg_rgb, bg_alpha, x0, y0)
193196
self%y0 = 0.0_dp
194197
if (present(x0)) self%x0 = x0
195198
if (present(y0)) self%y0 = y0
199+
self%page_rgb = [255, 255, 255]
200+
if (allocated(self%out)) deallocate (self%out)
196201
call t3_reset(self%t3)
197202
call builder_init(self%c)
198203

@@ -211,6 +216,38 @@ subroutine eps_open_canvas(self, width, height, bg_rgb, bg_alpha, x0, y0)
211216
end if
212217
end subroutine eps_open_canvas
213218

219+
subroutine eps_begin_frame(self, matrix, clip)
220+
class(eps_renderer_t), intent(inout) :: self
221+
real(dp), intent(in) :: matrix(6)
222+
type(clip_t), intent(in) :: clip
223+
integer :: i
224+
225+
call check_open(self)
226+
call check_transform(matrix, clip)
227+
call put(self, "gsave"//new_line("a")//"[")
228+
do i = 1, 6
229+
call put_num(self, matrix(i))
230+
end do
231+
call put(self, "] concat"//new_line("a"))
232+
if (clip%on) then
233+
call put_num(self, clip%x)
234+
call put_num(self, clip%y)
235+
call put_num(self, clip%w)
236+
call put_num(self, clip%h)
237+
call put(self, "rectclip"//new_line("a"))
238+
end if
239+
self%n_frame = self%n_frame + 1
240+
end subroutine eps_begin_frame
241+
242+
subroutine eps_end_frame(self)
243+
class(eps_renderer_t), intent(inout) :: self
244+
245+
call check_open(self)
246+
if (self%n_frame == 0) call contract_error("eps: unmatched end_frame")
247+
call put(self, "grestore"//new_line("a"))
248+
self%n_frame = self%n_frame - 1
249+
end subroutine eps_end_frame
250+
214251
subroutine emit_path(self, x, y, verbs, n_verb)
215252
class(eps_renderer_t), intent(inout) :: self
216253
real(dp), intent(in) :: x(:), y(:)
@@ -406,6 +443,13 @@ subroutine eps_draw_image(self, x, y, w, h, rgba, nx, ny, paint)
406443
if (nx < 1 .or. ny < 1) return
407444

408445
call put(self, "gsave"//new_line("a"))
446+
if (paint%clip%on) then
447+
call put_num(self, paint%clip%x)
448+
call put_num(self, paint%clip%y)
449+
call put_num(self, paint%clip%w)
450+
call put_num(self, paint%clip%h)
451+
call put(self, "rectclip"//new_line("a"))
452+
end if
409453
call put_num(self, x)
410454
call put_num(self, y + h)
411455
call put(self, "translate"//new_line("a"))
@@ -449,6 +493,7 @@ subroutine eps_close_canvas(self)
449493
integer :: i
450494

451495
call check_open(self)
496+
if (self%n_frame /= 0) call contract_error("eps: unclosed frame at close_canvas")
452497
eol = new_line("a")
453498
call builder_init(f)
454499
call builder_append(f, "%!PS-Adobe-3.0 EPSF-3.0"//eol)

0 commit comments

Comments
 (0)