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* string-lines
112 [ dupd text-width ] map nip supremum ;
115 TUPLE: text string style ;
117 : <text> ( string style -- text )
118 [ convert-string ] dip text boa ;
121 [ style>> set-style ] keep
125 over [ font>> ] [ avail-width ] bi visual-wrap
126 unclip [ "" concat-as ] dip
127 ] [ over line-break f ] if
130 [ { } ] [ over [ font>> ] [ width ] bi visual-wrap ]
132 ] dip [ prefix ] when*
133 over avail-lines short cut
134 [ draw-text ] [ "" concat-as ] bi*
135 ] change-string dup string>> empty? [ drop f ] when ;
138 [ style>> set-style ] keep
139 [ font>> ] [ string>> ] bi* string-lines
140 [ dupd text-width ] map nip supremum ;
150 [ dup 0 > pick avail-lines 0 > and ] [
151 over avail-width over min [ - ] keep [
152 [ over ] dip [ draw-line ] [ inc-x ] 2bi
153 ] unless-zero dup 0 > [ over line-break ] when
155 ] change-width nip dup width>> 0 > [ drop f ] unless ;
167 over avail-lines 0 > [ drop ?break f ] [ nip ] if ;
175 : <pb> ( -- pb ) f pb boa ;
178 dup used?>> [ f >>used? drop f ] [ t >>used? ] if nip ;
185 CONSTANT: table-cell-padding 5
187 TUPLE: table-cell contents width ;
189 : <table-cell> ( contents -- table-cell )
192 M: table-cell pdf-render
194 [ width>> >>col-width 0 >>x drop ]
196 [ [ dupd pdf-render ] map nip ] change-contents
197 dup contents>> [ ] any? [ drop f ] unless
200 width>> table-cell-padding +
201 swap margin>> [ + ] change-left drop
205 TUPLE: table-row cells ;
207 C: <table-row> table-row
209 ! save y before rendering each cell
210 ! set y to max y after all renders
212 M: table-row pdf-render
223 over y>> max-y max max-y!
224 ] map swap max-y >>y drop
227 dup cells>> [ ] any? [ drop f ] unless
230 [ drop margin>> 54 >>left drop ]
232 drop dup width>> >>col-width
233 [ ?line-break ] [ table-cell-padding inc-y ] bi
237 : col-widths ( canvas cells -- widths )
241 [ [ dupd pdf-width ] [ 0 ] if* ] map supremum
246 :: max-col-widths ( canvas rows -- widths )
249 cells>> canvas swap col-widths
250 [ widths [ 0 or max ] change-at ] each-index
251 ] each widths >alist sort-keys values
253 ! make last cell larger
254 dup sum 400 swap [-] [ + ] curry dupd sequences.extras:change-last
256 ! size down each column
257 dup sum dup 400 > [ 400 swap / [ * ] curry map ] [ drop ] if ;
259 : set-col-widths ( canvas rows -- )
260 [ max-col-widths ] keep [
262 [ swap >>width drop ] [ drop ] if*
266 TUPLE: table rows widths? ;
268 : <table> ( rows -- table )
274 dup widths?>> [ 2drop ] [
275 t >>widths? rows>> set-col-widths
280 dup rows>> empty? [ t ] [
281 [ rows>> first dupd pdf-render ] keep swap
283 ] [ [ rest ] change-rows ] until nip
284 dup rows>> [ drop f ] [ drop ] if-empty
289 2drop 400 ; ! FIXME: hardcoded max-width
292 : pdf-object ( str n -- str' )
293 "%d 0 obj\n" sprintf "\nendobj" surround ;
295 : pdf-stream ( str -- str' )
296 [ length 1 + "<<\n/Length %d\n>>" sprintf ]
297 [ "\nstream\n" "\nendstream" surround ] bi append ;
299 : pdf-catalog ( -- str )
307 : pdf-pages ( n -- str )
311 "/MediaBox [ 0 0 612 792 ]" ,
312 [ "/Count %d" sprintf , ]
315 [ "%d 0 R " sprintf ] map concat
316 "/Kids [ " "]" surround ,
319 ] { } make "\n" join ;
321 : pdf-page ( n -- page )
326 1 + "/Contents %d 0 R" sprintf ,
327 "/Resources << /Font <<" ,
328 "/F1 3 0 R /F2 4 0 R /F3 5 0 R" ,
329 "/F4 6 0 R /F5 7 0 R /F6 8 0 R" ,
330 "/F7 9 0 R /F8 10 0 R /F9 11 0 R" ,
331 "/F10 12 0 R /F11 13 0 R /F12 14 0 R" ,
334 ] { } make "\n" join ;
336 : pdf-trailer ( objects -- str )
339 dup length 1 + "0 %d" sprintf ,
340 "0000000000 65535 f" ,
342 over "%010X 00000 n" sprintf , length 1 + +
346 dup length 1 + "/Size %d" sprintf ,
351 [ length 1 + ] map-sum 9 + "%d" sprintf ,
353 ] { } make "\n" join ;
355 SYMBOLS: pdf-producer pdf-author pdf-creator ;
357 TUPLE: pdf-info title timestamp producer author creator ;
359 : <pdf-info> ( -- pdf-info )
362 pdf-producer get >>producer
363 pdf-author get >>author
364 pdf-creator get >>creator ;
366 M: pdf-info pdf-value
369 [ timestamp>> [ "/CreationDate " write pdf-write nl ] when* ]
370 [ producer>> [ "/Producer " write pdf-write nl ] when* ]
371 [ author>> [ "/Author " write pdf-write nl ] when* ]
372 [ title>> [ "/Title " write pdf-write nl ] when* ]
373 [ creator>> [ "/Creator " write pdf-write nl ] when* ]
375 ] with-string-writer ;
378 TUPLE: pdf-ref object revision ;
383 [ object>> ] [ revision>> ] bi "%d %d R" sprintf ;
386 TUPLE: pdf info pages fonts ;
394 :: pages>objects ( pdf -- objects )
396 pdf info>> pdf-value ,
398 { $ sans-serif-font $ serif-font $ monospace-font } {
399 [ [ f >>bold? f >>italic? pdf-value , ] each ]
400 [ [ t >>bold? f >>italic? pdf-value , ] each ]
401 [ [ f >>bold? t >>italic? pdf-value , ] each ]
402 [ [ t >>bold? t >>italic? pdf-value , ] each ]
404 pdf pages>> length pdf-pages ,
406 dup length 16 swap 2 range boa zip
407 [ pdf-page , , ] assoc-each
409 dup length [1,b] zip [ first2 pdf-object ] map ;
411 : objects>pdf ( objects -- str )
412 [ "\n" join "\n" append "%PDF-1.4\n" ]
413 [ pdf-trailer ] bi surround ;
415 ! Rename to pdf>string, have it take a <pdf> object?
417 : pdf>string ( seq -- pdf )
418 <pdf> swap pdf-layout [
419 stream>> pdf-stream over pages>> push
420 ] each pages>objects objects>pdf ;
422 : write-pdf ( seq -- )