@@ -29,13 +29,26 @@ module fplot_backend_pdf
2929 use fplot_render
3030 use fplot_svg, only: svg_builder, builder_init, builder_append, &
3131 builder_get, fmt_num
32+ use fplot_png, only: zlib_compress
3233 implicit none
3334 private
3435
3536 public :: pdf_renderer_t
3637
3738 integer , parameter :: MAX_OBJ = 64
3839 integer , parameter :: MAX_ALPHA = 64
40+ integer , parameter :: MAX_IMG = 16
41+
42+ ! An image already compressed and ready to become an XObject. The samples
43+ ! are kept as the deflated stream rather than as pixels because that is
44+ ! the only form the file wants them in.
45+ type :: pdf_image_t
46+ integer :: w = 0 , h = 0
47+ character (len= :), allocatable :: rgb
48+ ! Empty unless some sample is transparent, since a fully opaque
49+ ! image needs no soft mask.
50+ character (len= :), allocatable :: alpha
51+ end type pdf_image_t
3952
4053 type, extends(renderer_t) :: pdf_renderer_t
4154 ! The page content stream. The file itself is assembled in
@@ -50,6 +63,8 @@ module fplot_backend_pdf
5063 real (dp) :: alpha_stroke(MAX_ALPHA) = 1.0_dp
5164 logical :: clip_open = .false.
5265 type (clip_t) :: cur_clip
66+ integer :: n_img = 0
67+ type (pdf_image_t) :: img(MAX_IMG)
5368 contains
5469 procedure :: open_canvas = > pdf_open_canvas
5570 procedure :: close_canvas = > pdf_close_canvas
@@ -247,6 +262,7 @@ subroutine pdf_open_canvas(self, width, height, bg_rgb, bg_alpha, x0, y0)
247262 if (present (x0)) self% x0 = x0
248263 if (present (y0)) self% y0 = y0
249264 self% n_alpha = 0
265+ self% n_img = 0
250266 call builder_init(self% c)
251267
252268 ! One matrix for the whole page turns the API's top-left origin with
@@ -411,23 +427,66 @@ subroutine pdf_draw_text(self, x, y, s, font, paint, anchor, baseline, angle)
411427 call put(self, " Q" // new_line(" a" ))
412428 end subroutine pdf_draw_text
413429
414- ! imshow currently reaches every backend as a grid of rectangles, so no
415- ! figure fplot can draw needs this yet.
430+ ! The image itself is registered for later; what goes in the content
431+ ! stream is a matrix and one Do. PDF draws an image into the unit square
432+ ! with the first row at the top of it, so the matrix that lands it on the
433+ ! API's rectangle is a flip: v = 1 is the top edge, and the page is
434+ ! already flipped underneath.
416435 subroutine pdf_draw_image (self , x , y , w , h , rgba , nx , ny , paint )
417436 class(pdf_renderer_t), intent (inout ) :: self
418437 real (dp), intent (in ) :: x, y, w, h
419438 integer , intent (in ) :: nx, ny
420439 integer , intent (in ) :: rgba(4 , nx, ny)
421440 type (paint_t), intent (in ) :: paint
441+ integer , allocatable :: rgb(:), a(:)
442+ integer :: i, j, k, id
443+ logical :: opaque
444+
445+ if (nx < 1 .or. ny < 1 .or. self% n_img >= MAX_IMG) return
446+
447+ allocate (rgb(3 * nx* ny), a(nx* ny))
448+ k = 0
449+ opaque = .true.
450+ do j = 1 , ny
451+ do i = 1 , nx
452+ rgb(3 * k + 1 :3 * k + 3 ) = rgba(1 :3 , i, j)
453+ a(k + 1 ) = rgba(4 , i, j)
454+ if (rgba(4 , i, j) < 255 ) opaque = .false.
455+ k = k + 1
456+ end do
457+ end do
458+
459+ self% n_img = self% n_img + 1
460+ id = self% n_img
461+ self% img(id)% w = nx
462+ self% img(id)% h = ny
463+ self% img(id)% rgb = zlib_compress(rgb)
464+ if (opaque) then
465+ self% img(id)% alpha = " "
466+ else
467+ self% img(id)% alpha = zlib_compress(a)
468+ end if
469+
470+ call begin_paint(self, paint)
471+ call put_num(self, w)
472+ call put_num(self, 0.0_dp )
473+ call put_num(self, 0.0_dp )
474+ call put_num(self, - h)
475+ call put_num(self, x)
476+ call put_num(self, y + h)
477+ call put(self, " cm" // new_line(" a" ))
478+ call put(self, " /Im" // int_str(id)// " Do" // new_line(" a" ))
479+ call put(self, " Q" // new_line(" a" ))
422480 end subroutine pdf_draw_image
423481
424482 ! Assemble the file. Objects have to be byte-counted as they are written
425483 ! because the trailing xref table records where each one starts.
426484 subroutine pdf_close_canvas (self )
427485 class(pdf_renderer_t), intent (inout ) :: self
428486 type (svg_builder) :: f
429- character (len= :), allocatable :: content, res, gs
487+ character (len= :), allocatable :: content, res, gs, xo
430488 integer :: off(MAX_OBJ), nobj, i, pos, xref_pos
489+ integer :: img_obj(MAX_IMG), mask_obj(MAX_IMG)
431490 character (len= 1 ) :: eol
432491
433492 eol = new_line(" a" )
@@ -440,13 +499,29 @@ subroutine pdf_close_canvas(self)
440499 // num_str(self% alpha_fill(i))// " /CA " &
441500 // num_str(self% alpha_stroke(i))// " >> "
442501 end do
502+
503+ ! Images follow the five fixed objects, one object each and a second
504+ ! for a soft mask where one is needed.
505+ nobj = 5
506+ xo = " "
507+ do i = 1 , self% n_img
508+ nobj = nobj + 1
509+ img_obj(i) = nobj
510+ mask_obj(i) = 0
511+ if (len (self% img(i)% alpha) > 0 ) then
512+ nobj = nobj + 1
513+ mask_obj(i) = nobj
514+ end if
515+ xo = xo// " /Im" // int_str(i)// " " // int_str(img_obj(i))// " 0 R "
516+ end do
517+
443518 res = " << /Font << /F1 5 0 R >>"
444519 if (len (gs) > 0 ) res = res// " /ExtGState << " // gs// " >>"
520+ if (len (xo) > 0 ) res = res// " /XObject << " // xo// " >>"
445521 res = res// " >>"
446522
447523 call builder_init(f)
448524 pos = 0
449- nobj = 5
450525
451526 call emit(f, pos, " %PDF-1.4" // eol)
452527 ! A comment with high bytes marks the file as binary for tools that
@@ -477,6 +552,36 @@ subroutine pdf_close_canvas(self)
477552 // " /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>" // eol &
478553 // " endobj" // eol)
479554
555+ do i = 1 , self% n_img
556+ off(img_obj(i)) = pos
557+ call emit(f, pos, int_str(img_obj(i))// " 0 obj" // eol &
558+ // " << /Type /XObject /Subtype /Image /Width " &
559+ // int_str(self% img(i)% w)// " /Height " // int_str(self% img(i)% h) &
560+ // " /ColorSpace /DeviceRGB /BitsPerComponent 8" &
561+ // " /Filter /FlateDecode" )
562+ if (mask_obj(i) > 0 ) then
563+ call emit(f, pos, " /SMask " // int_str(mask_obj(i))// " 0 R" )
564+ end if
565+ call emit(f, pos, " /Length " // int_str(len (self% img(i)% rgb))// " >>" &
566+ // eol// " stream" // eol)
567+ call emit(f, pos, self% img(i)% rgb)
568+ call emit(f, pos, eol// " endstream" // eol// " endobj" // eol)
569+
570+ ! The mask is the alpha channel as a greyscale image of the same
571+ ! size, which is how PDF spells per-sample transparency.
572+ if (mask_obj(i) > 0 ) then
573+ off(mask_obj(i)) = pos
574+ call emit(f, pos, int_str(mask_obj(i))// " 0 obj" // eol &
575+ // " << /Type /XObject /Subtype /Image /Width " &
576+ // int_str(self% img(i)% w)// " /Height " // int_str(self% img(i)% h) &
577+ // " /ColorSpace /DeviceGray /BitsPerComponent 8" &
578+ // " /Filter /FlateDecode /Length " &
579+ // int_str(len (self% img(i)% alpha))// " >>" // eol// " stream" // eol)
580+ call emit(f, pos, self% img(i)% alpha)
581+ call emit(f, pos, eol// " endstream" // eol// " endobj" // eol)
582+ end if
583+ end do
584+
480585 xref_pos = pos
481586 call emit(f, pos, " xref" // eol// " 0 " // int_str(nobj + 1 )// eol)
482587 call emit(f, pos, " 0000000000 65535 f " // eol)
0 commit comments