]> gitweb.factorcode.org Git - factor.git/blob - extra/text-to-pdf/text-to-pdf.pdf
factor: Move math.ranges => ranges.
[factor.git] / extra / text-to-pdf / text-to-pdf.pdf
1 %PDF-1.4
2 1 0 obj
3 <<
4 /CreationDate D:20101016095802
5 /Producer (Factor)
6 /Author (jbenedik)
7 /Creator (created with Factor)
8 >>
9 endobj
10 2 0 obj
11 <<
12 /Type /Catalog
13 /Pages 4 0 R
14 >>
15 endobj
16 3 0 obj
17 <<
18 /Type /Font
19 /Subtype /Type1
20 /BaseFont /Courier
21 >>
22 endobj
23 4 0 obj
24 <<
25 /Type /Pages
26 /MediaBox [ 0 0 612 792 ]
27 /Count 3
28 /Kids [ 5 0 R 7 0 R 9 0 R ]
29 >>
30 endobj
31 5 0 obj
32 <<
33 /Type /Page
34 /Parent 4 0 R
35 /Contents 6 0 R
36 /Resources << /Font << /F1 3 0 R >> >>
37 >>
38 endobj
39 6 0 obj
40 <<
41 /Length 1633
42 >>
43 stream
44 BT
45 54 738 Td
46 /F1 10 Tf
47 12 TL
48 (! Copyright \(C\) 2010 John Benediktsson)'
49 (! See http://factorcode.org/license.txt for BSD license)'
50 ( )'
51 (USING: assocs calendar combinators environment formatting)'
52 (grouping io io.files kernel make math ranges sequences)'
53 (splitting xml.entities ;)'
54 ( )'
55 (IN: text-to-pdf)'
56 ( )'
57 (<PRIVATE)'
58 ( )'
59 (: pdf-string \( str -- str' \))'
60 (    H{)'
61 (        { HEX: 08    "\\\\b"  })'
62 (        { HEX: 0c    "\\\\f"  })'
63 (        { CHAR: \\n   "\\\\n"  })'
64 (        { CHAR: \\r   "\\\\r"  })'
65 (        { CHAR: \\t   "\\\\t"  })'
66 (        { CHAR: \\\\   "\\\\\\\\" })'
67 (        { CHAR: \(    "\\\\\("  })'
68 (        { CHAR: \)    "\\\\\)"  })'
69 (    } escape-string-by "\(" "\)" surround ;)'
70 ( )'
71 (: pdf-object \( str n -- str' \))'
72 (    "%d 0 obj\\n" sprintf "\\nendobj" surround ;)'
73 ( )'
74 (: pdf-stream \( str -- str' \))'
75 (    [ length 1 + "<<\\n/Length %d\\n>>" sprintf ])'
76 (    [ "\\nstream\\n" "\\nendstream" surround ] bi append ;)'
77 ( )'
78 (: pdf-info \( -- str \))'
79 (    [)'
80 (        "<<" ,)'
81 (        "/CreationDate D:" now "%Y%m%d%H%M%S" strftime append ,)'
82 (        "/Producer \(Factor\)" ,)'
83 (        "/Author " "USER" os-env "unknown" or pdf-string append ,)'
84 (        "/Creator \(created with Factor\)" ,)'
85 (        ">>" ,)'
86 (    ] { } make "\\n" join ;)'
87 ( )'
88 (: pdf-catalog \( -- str \))'
89 (    {)'
90 (        "<<")'
91 (        "/Type /Catalog")'
92 (        "/Pages 4 0 R")'
93 (        ">>")'
94 (    } "\\n" join ;)'
95 ( )'
96 (: pdf-font \( -- str \))'
97 (    {)'
98 (        "<<")'
99 (        "/Type /Font")'
100 (        "/Subtype /Type1")'
101 (        "/BaseFont /Courier")'
102 (        ">>")'
103 (    } "\\n" join ;)'
104 ( )'
105 ET
106 endstream
107 endobj
108 7 0 obj
109 <<
110 /Type /Page
111 /Parent 4 0 R
112 /Contents 8 0 R
113 /Resources << /Font << /F1 3 0 R >> >>
114 >>
115 endobj
116 8 0 obj
117 <<
118 /Length 1569
119 >>
120 stream
121 BT
122 54 738 Td
123 /F1 10 Tf
124 12 TL
125 (: pdf-pages \( n -- str \))'
126 (    [)'
127 (        "<<" ,)'
128 (        "/Type /Pages" ,)'
129 (        "/MediaBox [ 0 0 612 792 ]" ,)'
130 (        [ "/Count %d" sprintf , ])'
131 (        [)'
132 (            5 swap 2 range boa)'
133 (            [ "%d 0 R " sprintf ] map concat)'
134 (            "/Kids [ " "]" surround ,)'
135 (        ] bi)'
136 (        ">>" ,)'
137 (    ] { } make "\\n" join ;)'
138 ( )'
139 (: pdf-text \( lines -- str \))'
140 (    [)'
141 (        "BT" ,)'
142 (        "54 738 Td" ,)'
143 (        "/F1 10 Tf" ,)'
144 (        "12 TL" ,)'
145 (        [ pdf-string "'" append , ] each)'
146 (        "ET" ,)'
147 (    ] { } make "\\n" join pdf-stream ;)'
148 ( )'
149 (: pdf-page \( n -- page \))'
150 (    [)'
151 (        "<<" ,)'
152 (        "/Type /Page" ,)'
153 (        "/Parent 4 0 R" ,)'
154 (        1 + "/Contents %d 0 R" sprintf ,)'
155 (        "/Resources << /Font << /F1 3 0 R >> >>" ,)'
156 (        ">>" ,)'
157 (    ] { } make "\\n" join ;)'
158 ( )'
159 (: pdf-trailer \( objects -- str \))'
160 (    [)'
161 (        "xref" ,)'
162 (        dup length 1 + "0 %d" sprintf ,)'
163 (        "0000000000 65535 f" ,)'
164 (        9 over [)'
165 (            over "%010X 00000 n" sprintf , length 1 + +)'
166 (        ] each drop)'
167 (        "trailer" ,)'
168 (        "<<" ,)'
169 (        dup length 1 + "/Size %d" sprintf ,)'
170 (        "/Info 1 0 R" ,)'
171 (        "/Root 2 0 R" ,)'
172 (        ">>" ,)'
173 (        "startxref" ,)'
174 (        [ length 1 + ] map-sum 9 + "%d" sprintf ,)'
175 (        "%%EOF" ,)'
176 (    ] { } make "\\n" join ;)'
177 ( )'
178 (: string>lines \( str -- lines \))'
179 (    "\\t" split "    " join lines)'
180 (    [ [ " " ] when-empty ] map ;)'
181 ( )'
182 ET
183 endstream
184 endobj
185 9 0 obj
186 <<
187 /Type /Page
188 /Parent 4 0 R
189 /Contents 10 0 R
190 /Resources << /Font << /F1 3 0 R >> >>
191 >>
192 endobj
193 10 0 obj
194 <<
195 /Length 839
196 >>
197 stream
198 BT
199 54 738 Td
200 /F1 10 Tf
201 12 TL
202 (: lines>pages \( lines -- pages \))'
203 (    [ 84 <groups> ] map concat 57 <groups> ;)'
204 ( )'
205 (: pages>objects \( pages -- objects \))'
206 (    [)'
207 (        pdf-info ,)'
208 (        pdf-catalog ,)'
209 (        pdf-font ,)'
210 (        dup length pdf-pages ,)'
211 (        dup length 5 swap 2 range boa zip)'
212 (        [ pdf-page , pdf-text , ] assoc-each)'
213 (    ] { } make)'
214 (    dup length [1,b] zip [ first2 pdf-object ] map ;)'
215 ( )'
216 (: objects>pdf \( objects -- str \))'
217 (    [ "\\n" join "\\n" append "%PDF-1.4\\n" ])'
218 (    [ pdf-trailer ] bi surround ;)'
219 ( )'
220 (PRIVATE>)'
221 ( )'
222 (: text-to-pdf \( str -- str' \))'
223 (    string>lines lines>pages pages>objects objects>pdf ;)'
224 ( )'
225 (: file-to-pdf \( path encoding -- \))'
226 (    [ file-contents text-to-pdf ])'
227 (    [ [ ".pdf" append ] dip set-file-contents ] 2bi ;)'
228 ( )'
229 ( )'
230 ET
231 endstream
232 endobj
233 xref
234 0 11
235 0000000000 65535 f
236 0000000009 00000 n
237 0000000082 00000 n
238 00000000B3 00000 n
239 00000000F7 00000 n
240 0000000158 00000 n
241 00000001BE 00000 n
242 0000000852 00000 n
243 00000008B8 00000 n
244 0000000F0C 00000 n
245 0000000F73 00000 n
246 trailer
247 <<
248 /Size 11
249 /Info 1 0 R
250 /Root 2 0 R
251 >>
252 startxref
253 4845
254 %%EOF