1 ! Copyright (C) 2011-2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3 USING: accessors assocs calendar combinators environment fonts
4 formatting fry io io.streams.string kernel literals locals make
5 math math.order math.ranges namespaces pdf.canvas pdf.values
6 pdf.wrap sequences sequences.extras sorting splitting ui.text
8 FROM: pdf.canvas => draw-text ;
16 ! margin-left, margin-right += inset-width
19 ! margin-left, margin-right -= inset-width
24 ! TUPLE: spacer width height ;
27 ! TUPLE: image < span ;
30 ! Outlines (add to catalog):
32 ! /PageMode /UseOutlines
38 ! FIXME: spacing oddities if run multiple times
39 ! FIXME: make sure highlights text "in order"
40 ! FIXME: don't modify layout objects in pdf-render
41 ! FIXME: make sure unicode "works"
42 ! FIXME: only set style differences to reduce size?
43 ! FIXME: gadget. to take a "screenshot" into a pdf?
44 ! FIXME: compress each pdf object to reduce file size?
47 GENERIC: pdf-render ( canvas obj -- remain/f )
49 M: f pdf-render 2drop f ;
51 GENERIC: pdf-width ( canvas obj -- n )
55 : (pdf-layout) ( page obj -- page )
57 dupd [ pdf-render ] with-string-writer
58 '[ _ append ] [ change-stream ] curry dip
59 [ [ , <canvas> ] when ] keep
64 : pdf-layout ( seq -- pages )
67 dup stream>> empty? [ drop ] [ , ] if
71 TUPLE: div items style ;
76 [ style>> set-style ] keep
77 swap '[ _ pdf-render drop ] each f ;
80 [ style>> set-style ] keep
81 items>> [ dupd pdf-width ] map nip supremum ;
86 : convert-string ( str -- str' )
90 } escape-string-by [ 256 < ] filter ;
95 TUPLE: p string style ;
97 : <p> ( string style -- p )
98 [ convert-string ] dip p boa ;
101 [ style>> set-style ] keep
104 over [ font>> ] [ avail-width ] bi visual-wrap
105 over avail-lines short cut
106 [ draw-text ] [ "" concat-as ] bi*
107 ] change-string dup string>> empty? [ drop f ] when ;
110 [ style>> set-style ] keep
111 [ font>> ] [ string>> ] bi* lines
112 [ dupd text-width ] map nip supremum ;
115 TUPLE: text string style ;
117 : <text> ( string style -- text )
118 [ convert-string ] dip text boa ;
120 ! FIXME: need to make links clickable, render text first, draw
121 ! box over text that is "link"
123 ! https://www.w3.org/WAI/WCAG21/Techniques/pdf/PDF11.html
126 [ style>> set-style ] keep
130 over [ font>> ] [ avail-width ] bi visual-wrap
131 unclip [ "" concat-as ] dip
132 ] [ over line-break f ] if
135 [ { } ] [ over [ font>> ] [ width ] bi visual-wrap ]
137 ] dip [ prefix ] when*
138 over avail-lines short cut
139 [ draw-text ] [ "" concat-as ] bi*
140 ] change-string dup string>> empty? [ drop f ] when ;
143 [ style>> set-style ] keep
144 [ font>> ] [ string>> ] bi* lines
145 [ dupd text-width ] map nip supremum ;
155 [ dup 0 > pick avail-lines 0 > and ] [
156 over avail-width over min [ - ] keep [
157 overd [ draw-line ] [ inc-x ] 2bi
158 ] unless-zero dup 0 > [ over line-break ] when
160 ] change-width nip dup width>> 0 > [ drop f ] unless ;
172 over avail-lines 0 > [ drop ?break f ] [ nip ] if ;
180 : <pb> ( -- pb ) f pb boa ;
183 dup used?>> [ f >>used? drop f ] [ t >>used? ] if nip ;
190 CONSTANT: table-cell-padding 5
192 TUPLE: table-cell contents width ;
194 : <table-cell> ( contents -- table-cell )
197 M: table-cell pdf-render
199 [ width>> >>col-width 0 >>x drop ]
201 [ [ dupd pdf-render ] map nip ] change-contents
202 dup contents>> [ ] any? [ drop f ] unless
205 width>> table-cell-padding +
206 swap margin>> [ + ] change-left drop
210 TUPLE: table-row cells ;
212 C: <table-row> table-row
214 ! save y before rendering each cell
215 ! set y to max y after all renders
217 M: table-row pdf-render
228 over y>> max-y max max-y!
229 ] map swap max-y >>y drop
232 dup cells>> [ ] any? [ drop f ] unless
235 [ drop margin>> 54 >>left drop ]
237 drop dup width>> >>col-width
238 [ ?line-break ] [ table-cell-padding inc-y ] bi
242 : col-widths ( canvas cells -- widths )
246 [ [ dupd pdf-width ] [ 0 ] if* ] map supremum
251 :: max-col-widths ( canvas rows -- widths )
254 cells>> canvas swap col-widths
255 [ widths [ 0 or max ] change-at ] each-index
256 ] each widths >alist sort-keys values
261 ! special-case small first column
262 drop dup unclip-slice over sum swap
263 450 swap - swap / [ * ] curry map! drop
265 ! size down all columns
266 450 swap / [ * ] curry map
270 ! make last cell larger
271 450 swap [-] [ + ] curry dupd
272 sequences.extras:change-last
275 : set-col-widths ( canvas rows -- )
276 [ max-col-widths ] keep [
278 [ swap >>width drop ] [ drop ] if*
282 TUPLE: table rows widths? ;
284 : <table> ( rows -- table )
290 dup widths?>> [ 2drop ] [
291 t >>widths? rows>> set-col-widths
296 dup rows>> empty? [ t ] [
297 [ rows>> first dupd pdf-render ] keep swap
299 ] [ [ rest ] change-rows ] until nip
300 dup rows>> [ drop f ] [ drop ] if-empty
305 2drop 450 ; ! FIXME: hardcoded max-width
308 : pdf-object ( str n -- str' )
309 "%d 0 obj\n" sprintf "\nendobj" surround ;
311 : pdf-stream ( str -- str' )
312 [ length 1 + "<<\n/Length %d\n>>" sprintf ]
313 [ "\nstream\n" "\nendstream" surround ] bi append ;
315 : pdf-catalog ( -- str )
323 : pdf-pages ( n -- str )
327 "/MediaBox [ 0 0 612 792 ]" ,
328 [ "/Count %d" sprintf , ]
331 [ "%d 0 R " sprintf ] map concat
332 "/Kids [ " "]" surround ,
337 : pdf-page ( n -- page )
342 1 + "/Contents %d 0 R" sprintf ,
343 "/Resources << /Font <<" ,
344 "/F1 3 0 R /F2 4 0 R /F3 5 0 R" ,
345 "/F4 6 0 R /F5 7 0 R /F6 8 0 R" ,
346 "/F7 9 0 R /F8 10 0 R /F9 11 0 R" ,
347 "/F10 12 0 R /F11 13 0 R /F12 14 0 R" ,
352 : pdf-trailer ( objects -- str )
355 dup length 1 + "0 %d" sprintf ,
356 "0000000000 65535 f" ,
358 over "%010X 00000 n" sprintf , length 1 + +
362 dup length 1 + "/Size %d" sprintf ,
367 [ length 1 + ] map-sum 9 + "%d" sprintf ,
371 SYMBOLS: pdf-producer pdf-author pdf-creator ;
373 TUPLE: pdf-info title timestamp producer author creator ;
375 : <pdf-info> ( -- pdf-info )
378 pdf-producer get >>producer
379 pdf-author get >>author
380 pdf-creator get >>creator ;
382 M: pdf-info pdf-value
385 [ timestamp>> [ "/CreationDate " write pdf-write nl ] when* ]
386 [ producer>> [ "/Producer " write pdf-write nl ] when* ]
387 [ author>> [ "/Author " write pdf-write nl ] when* ]
388 [ title>> [ "/Title " write pdf-write nl ] when* ]
389 [ creator>> [ "/Creator " write pdf-write nl ] when* ]
391 ] with-string-writer ;
394 TUPLE: pdf-ref object revision ;
399 [ object>> ] [ revision>> ] bi "%d %d R" sprintf ;
402 TUPLE: pdf info pages fonts ;
410 :: pages>objects ( pdf -- objects )
412 pdf info>> pdf-value ,
414 { $ sans-serif-font $ serif-font $ monospace-font } {
415 [ [ f >>bold? f >>italic? pdf-value , ] each ]
416 [ [ t >>bold? f >>italic? pdf-value , ] each ]
417 [ [ f >>bold? t >>italic? pdf-value , ] each ]
418 [ [ t >>bold? t >>italic? pdf-value , ] each ]
420 pdf pages>> length pdf-pages ,
422 dup length 16 swap 2 range boa zip
423 [ pdf-page , , ] assoc-each
425 dup length [1,b] zip [ first2 pdf-object ] map ;
427 : objects>pdf ( objects -- str )
428 [ unlines "\n" append "%PDF-1.4\n" ]
429 [ pdf-trailer ] bi surround ;
431 ! Rename to pdf>string, have it take a <pdf> object?
433 : pdf>string ( seq -- pdf )
434 <pdf> swap pdf-layout [
435 stream>> pdf-stream over pages>> push
436 ] each pages>objects objects>pdf ;
438 : write-pdf ( seq -- )