1 ! Copyright (C) 2011-2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors assocs calendar combinators environment fonts
5 formatting fry io io.streams.string kernel literals locals make
6 math math.order math.ranges pdf.canvas pdf.values pdf.wrap
7 sequences sequences.extras sorting splitting ui.text
9 FROM: assocs => change-at ;
10 FROM: sequences => change-nth ;
11 FROM: pdf.canvas => draw-text ;
19 ! margin-left, margin-right += inset-width
22 ! margin-left, margin-right -= inset-width
27 ! TUPLE: spacer width height ;
30 ! TUPLE: image < span ;
33 ! Outlines (add to catalog):
35 ! /PageMode /UseOutlines
41 ! FIXME: spacing oddities if run multiple times
42 ! FIXME: make sure highlights text "in order"
43 ! FIXME: don't modify layout objects in pdf-render
44 ! FIXME: make sure unicode "works"
45 ! FIXME: only set style differences to reduce size?
46 ! FIXME: gadget. to take a "screenshot" into a pdf?
47 ! FIXME: compress each pdf object to reduce file size?
50 GENERIC: pdf-render ( canvas obj -- remain/f )
52 M: f pdf-render 2drop f ;
54 GENERIC: pdf-width ( canvas obj -- n )
58 : (pdf-layout) ( page obj -- page )
60 dupd [ pdf-render ] with-string-writer
61 '[ _ append ] [ change-stream ] curry dip
62 [ [ , <canvas> ] when ] keep
67 : pdf-layout ( seq -- pages )
70 dup stream>> empty? [ drop ] [ , ] if
74 TUPLE: div items style ;
79 [ style>> set-style ] keep
80 swap '[ _ pdf-render drop ] each f ;
83 [ style>> set-style ] keep
84 items>> [ dupd pdf-width ] map nip supremum ;
89 : convert-string ( str -- str' )
93 } escape-string-by [ 256 < ] filter ;
98 TUPLE: p string style ;
100 : <p> ( string style -- p )
101 [ convert-string ] dip p boa ;
104 [ style>> set-style ] keep
107 over [ font>> ] [ avail-width ] bi visual-wrap
108 over avail-lines short cut
109 [ draw-text ] [ "" concat-as ] bi*
110 ] change-string dup string>> empty? [ drop f ] when ;
113 [ style>> set-style ] keep
114 [ font>> ] [ string>> ] bi* string-lines
115 [ dupd text-width ] map nip supremum ;
118 TUPLE: text string style ;
120 : <text> ( string style -- text )
121 [ convert-string ] dip text boa ;
124 [ style>> set-style ] keep
128 over [ font>> ] [ avail-width ] bi visual-wrap
129 unclip [ "" concat-as ] dip
130 ] [ over line-break f ] if
133 [ { } ] [ over [ font>> ] [ width ] bi visual-wrap ]
135 ] dip [ prefix ] when*
136 over avail-lines short cut
137 [ draw-text ] [ "" concat-as ] bi*
138 ] change-string dup string>> empty? [ drop f ] when ;
141 [ style>> set-style ] keep
142 [ font>> ] [ string>> ] bi* string-lines
143 [ dupd text-width ] map nip supremum ;
153 [ dup 0 > pick avail-lines 0 > and ] [
154 over avail-width over min [ - ] keep [
155 [ over ] dip [ draw-line ] [ inc-x ] 2bi
156 ] unless-zero dup 0 > [ over line-break ] when
158 ] change-width nip dup width>> 0 > [ drop f ] unless ;
170 over avail-lines 0 > [ drop ?break f ] [ nip ] if ;
178 : <pb> ( -- pb ) f pb boa ;
181 dup used?>> [ f >>used? drop f ] [ t >>used? ] if nip ;
188 CONSTANT: table-cell-padding 5
190 TUPLE: table-cell contents width ;
192 : <table-cell> ( contents -- table-cell )
195 M: table-cell pdf-render
197 [ width>> >>col-width 0 >>x drop ]
199 [ [ dupd pdf-render ] map nip ] change-contents
200 dup contents>> [ ] any? [ drop f ] unless
203 width>> table-cell-padding +
204 swap margin>> [ + ] change-left drop
208 TUPLE: table-row cells ;
210 C: <table-row> table-row
212 ! save y before rendering each cell
213 ! set y to max y after all renders
215 M: table-row pdf-render
226 over y>> max-y max max-y!
227 ] map swap max-y >>y drop
230 dup cells>> [ ] any? [ drop f ] unless
233 [ drop margin>> 54 >>left drop ]
235 drop dup width>> >>col-width
236 [ ?line-break ] [ table-cell-padding inc-y ] bi
240 : col-widths ( canvas cells -- widths )
244 [ [ dupd pdf-width ] [ 0 ] if* ] map supremum
249 :: max-col-widths ( canvas rows -- widths )
252 cells>> canvas swap col-widths
253 [ widths [ 0 or max ] change-at ] each-index
254 ] each widths >alist sort-keys values
256 ! make last cell larger
257 dup sum 400 swap [-] [ + ] curry dupd change-last
259 ! size down each column
260 dup sum dup 400 > [ 400 swap / [ * ] curry map ] [ drop ] if ;
262 : set-col-widths ( canvas rows -- )
263 [ max-col-widths ] keep [
265 [ swap >>width drop ] [ drop ] if*
269 TUPLE: table rows widths? ;
271 : <table> ( rows -- table )
277 dup widths?>> [ 2drop ] [
278 t >>widths? rows>> set-col-widths
283 dup rows>> empty? [ t ] [
284 [ rows>> first dupd pdf-render ] keep swap
286 ] [ [ rest ] change-rows ] until nip
287 dup rows>> [ drop f ] [ drop ] if-empty
292 2drop 400 ; ! FIXME: hardcoded max-width
295 : pdf-object ( str n -- str' )
296 "%d 0 obj\n" sprintf "\nendobj" surround ;
298 : pdf-stream ( str -- str' )
299 [ length 1 + "<<\n/Length %d\n>>" sprintf ]
300 [ "\nstream\n" "\nendstream" surround ] bi append ;
302 : pdf-catalog ( -- str )
310 : pdf-pages ( n -- str )
314 "/MediaBox [ 0 0 612 792 ]" ,
315 [ "/Count %d" sprintf , ]
318 [ "%d 0 R " sprintf ] map concat
319 "/Kids [ " "]" surround ,
322 ] { } make "\n" join ;
324 : pdf-page ( n -- page )
329 1 + "/Contents %d 0 R" sprintf ,
330 "/Resources << /Font <<" ,
331 "/F1 3 0 R /F2 4 0 R /F3 5 0 R" ,
332 "/F4 6 0 R /F5 7 0 R /F6 8 0 R" ,
333 "/F7 9 0 R /F8 10 0 R /F9 11 0 R" ,
334 "/F10 12 0 R /F11 13 0 R /F12 14 0 R" ,
337 ] { } make "\n" join ;
339 : pdf-trailer ( objects -- str )
342 dup length 1 + "0 %d" sprintf ,
343 "0000000000 65535 f" ,
345 over "%010X 00000 n" sprintf , length 1 + +
349 dup length 1 + "/Size %d" sprintf ,
354 [ length 1 + ] map-sum 9 + "%d" sprintf ,
356 ] { } make "\n" join ;
358 TUPLE: pdf-info title timestamp producer author creator ;
360 : <pdf-info> ( -- pdf-info )
364 "USER" os-env "unknown" or >>author
365 "created with Factor" >>creator ;
367 M: pdf-info pdf-value
370 [ timestamp>> [ "/CreationDate " write pdf-write nl ] when* ]
371 [ producer>> [ "/Producer " write pdf-write nl ] when* ]
372 [ author>> [ "/Author " write pdf-write nl ] when* ]
373 [ title>> [ "/Title " write pdf-write nl ] when* ]
374 [ creator>> [ "/Creator " write pdf-write nl ] when* ]
376 ] with-string-writer ;
379 TUPLE: pdf-ref object revision ;
384 [ object>> ] [ revision>> ] bi "%d %d R" sprintf ;
387 TUPLE: pdf info pages fonts ;
395 :: pages>objects ( pdf -- objects )
397 pdf info>> pdf-value ,
399 { $ sans-serif-font $ serif-font $ monospace-font } {
400 [ [ f >>bold? f >>italic? pdf-value , ] each ]
401 [ [ t >>bold? f >>italic? pdf-value , ] each ]
402 [ [ f >>bold? t >>italic? pdf-value , ] each ]
403 [ [ t >>bold? t >>italic? pdf-value , ] each ]
405 pdf pages>> length pdf-pages ,
407 dup length 16 swap 2 range boa zip
408 [ pdf-page , , ] assoc-each
410 dup length [1,b] zip [ first2 pdf-object ] map ;
412 : objects>pdf ( objects -- str )
413 [ "\n" join "\n" append "%PDF-1.4\n" ]
414 [ pdf-trailer ] bi surround ;
416 ! Rename to pdf>string, have it take a <pdf> object?
418 : pdf>string ( seq -- pdf )
419 <pdf> swap pdf-layout [
420 stream>> pdf-stream over pages>> push
421 ] each pages>objects objects>pdf ;
423 : write-pdf ( seq -- )