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 namespaces pdf.canvas pdf.values
7 pdf.wrap sequences sequences.extras sorting splitting ui.text
9 FROM: pdf.canvas => draw-text ;
17 ! margin-left, margin-right += inset-width
20 ! margin-left, margin-right -= inset-width
25 ! TUPLE: spacer width height ;
28 ! TUPLE: image < span ;
31 ! Outlines (add to catalog):
33 ! /PageMode /UseOutlines
39 ! FIXME: spacing oddities if run multiple times
40 ! FIXME: make sure highlights text "in order"
41 ! FIXME: don't modify layout objects in pdf-render
42 ! FIXME: make sure unicode "works"
43 ! FIXME: only set style differences to reduce size?
44 ! FIXME: gadget. to take a "screenshot" into a pdf?
45 ! FIXME: compress each pdf object to reduce file size?
48 GENERIC: pdf-render ( canvas obj -- remain/f )
50 M: f pdf-render 2drop f ;
52 GENERIC: pdf-width ( canvas obj -- n )
56 : (pdf-layout) ( page obj -- page )
58 dupd [ pdf-render ] with-string-writer
59 '[ _ append ] [ change-stream ] curry dip
60 [ [ , <canvas> ] when ] keep
65 : pdf-layout ( seq -- pages )
68 dup stream>> empty? [ drop ] [ , ] if
72 TUPLE: div items style ;
77 [ style>> set-style ] keep
78 swap '[ _ pdf-render drop ] each f ;
81 [ style>> set-style ] keep
82 items>> [ dupd pdf-width ] map nip supremum ;
87 : convert-string ( str -- str' )
91 } escape-string-by [ 256 < ] filter ;
96 TUPLE: p string style ;
98 : <p> ( string style -- p )
99 [ convert-string ] dip p boa ;
102 [ style>> set-style ] keep
105 over [ font>> ] [ avail-width ] bi visual-wrap
106 over avail-lines short cut
107 [ draw-text ] [ "" concat-as ] bi*
108 ] change-string dup string>> empty? [ drop f ] when ;
111 [ style>> set-style ] keep
112 [ font>> ] [ string>> ] bi* string-lines
113 [ dupd text-width ] map nip supremum ;
116 TUPLE: text string style ;
118 : <text> ( string style -- text )
119 [ convert-string ] dip text boa ;
122 [ style>> set-style ] keep
126 over [ font>> ] [ avail-width ] bi visual-wrap
127 unclip [ "" concat-as ] dip
128 ] [ over line-break f ] if
131 [ { } ] [ over [ font>> ] [ width ] bi visual-wrap ]
133 ] dip [ prefix ] when*
134 over avail-lines short cut
135 [ draw-text ] [ "" concat-as ] bi*
136 ] change-string dup string>> empty? [ drop f ] when ;
139 [ style>> set-style ] keep
140 [ font>> ] [ string>> ] bi* string-lines
141 [ dupd text-width ] map nip supremum ;
151 [ dup 0 > pick avail-lines 0 > and ] [
152 over avail-width over min [ - ] keep [
153 [ over ] dip [ draw-line ] [ inc-x ] 2bi
154 ] unless-zero dup 0 > [ over line-break ] when
156 ] change-width nip dup width>> 0 > [ drop f ] unless ;
168 over avail-lines 0 > [ drop ?break f ] [ nip ] if ;
176 : <pb> ( -- pb ) f pb boa ;
179 dup used?>> [ f >>used? drop f ] [ t >>used? ] if nip ;
186 CONSTANT: table-cell-padding 5
188 TUPLE: table-cell contents width ;
190 : <table-cell> ( contents -- table-cell )
193 M: table-cell pdf-render
195 [ width>> >>col-width 0 >>x drop ]
197 [ [ dupd pdf-render ] map nip ] change-contents
198 dup contents>> [ ] any? [ drop f ] unless
201 width>> table-cell-padding +
202 swap margin>> [ + ] change-left drop
206 TUPLE: table-row cells ;
208 C: <table-row> table-row
210 ! save y before rendering each cell
211 ! set y to max y after all renders
213 M: table-row pdf-render
224 over y>> max-y max max-y!
225 ] map swap max-y >>y drop
228 dup cells>> [ ] any? [ drop f ] unless
231 [ drop margin>> 54 >>left drop ]
233 drop dup width>> >>col-width
234 [ ?line-break ] [ table-cell-padding inc-y ] bi
238 : col-widths ( canvas cells -- widths )
242 [ [ dupd pdf-width ] [ 0 ] if* ] map supremum
247 :: max-col-widths ( canvas rows -- widths )
250 cells>> canvas swap col-widths
251 [ widths [ 0 or max ] change-at ] each-index
252 ] each widths >alist sort-keys values
254 ! make last cell larger
255 dup sum 400 swap [-] [ + ] curry dupd change-last
257 ! size down each column
258 dup sum dup 400 > [ 400 swap / [ * ] curry map ] [ drop ] if ;
260 : set-col-widths ( canvas rows -- )
261 [ max-col-widths ] keep [
263 [ swap >>width drop ] [ drop ] if*
267 TUPLE: table rows widths? ;
269 : <table> ( rows -- table )
275 dup widths?>> [ 2drop ] [
276 t >>widths? rows>> set-col-widths
281 dup rows>> empty? [ t ] [
282 [ rows>> first dupd pdf-render ] keep swap
284 ] [ [ rest ] change-rows ] until nip
285 dup rows>> [ drop f ] [ drop ] if-empty
290 2drop 400 ; ! FIXME: hardcoded max-width
293 : pdf-object ( str n -- str' )
294 "%d 0 obj\n" sprintf "\nendobj" surround ;
296 : pdf-stream ( str -- str' )
297 [ length 1 + "<<\n/Length %d\n>>" sprintf ]
298 [ "\nstream\n" "\nendstream" surround ] bi append ;
300 : pdf-catalog ( -- str )
308 : pdf-pages ( n -- str )
312 "/MediaBox [ 0 0 612 792 ]" ,
313 [ "/Count %d" sprintf , ]
316 [ "%d 0 R " sprintf ] map concat
317 "/Kids [ " "]" surround ,
320 ] { } make "\n" join ;
322 : pdf-page ( n -- page )
327 1 + "/Contents %d 0 R" sprintf ,
328 "/Resources << /Font <<" ,
329 "/F1 3 0 R /F2 4 0 R /F3 5 0 R" ,
330 "/F4 6 0 R /F5 7 0 R /F6 8 0 R" ,
331 "/F7 9 0 R /F8 10 0 R /F9 11 0 R" ,
332 "/F10 12 0 R /F11 13 0 R /F12 14 0 R" ,
335 ] { } make "\n" join ;
337 : pdf-trailer ( objects -- str )
340 dup length 1 + "0 %d" sprintf ,
341 "0000000000 65535 f" ,
343 over "%010X 00000 n" sprintf , length 1 + +
347 dup length 1 + "/Size %d" sprintf ,
352 [ length 1 + ] map-sum 9 + "%d" sprintf ,
354 ] { } make "\n" join ;
356 SYMBOLS: pdf-producer pdf-author pdf-creator ;
358 TUPLE: pdf-info title timestamp producer author creator ;
360 : <pdf-info> ( -- pdf-info )
363 pdf-producer get >>producer
364 pdf-author get >>author
365 pdf-creator get >>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 -- )