452d562e9b091a6f6cf035bb6a459a5ef758f4c9
2 * PostScript backend for Halibut
10 /* Ideal number of characters per line, for use in PostScript code */
12 /* Absolute maxiumum characters per line, for use in DSC comments */
13 #define PS_MAXWIDTH 255
15 static void ps_comment(FILE *fp
, char const *leader
, word
*words
);
16 static void ps_string_len(FILE *fp
, int *cc
, char const *str
, int len
);
17 static void ps_string(FILE *fp
, int *cc
, char const *str
);
19 paragraph
*ps_config_filename(char *filename
)
21 return cmdline_cfg_simple("ps-filename", filename
, NULL
);
24 void ps_backend(paragraph
*sourceform
, keywordlist
*keywords
,
25 indexdata
*idx
, void *vdoc
) {
26 document
*doc
= (document
*)vdoc
;
36 int cc
; /* Character count on current line */
41 filename
= dupstr("output.ps");
42 for (p
= sourceform
; p
; p
= p
->next
) {
43 if (p
->type
== para_Config
) {
44 if (!ustricmp(p
->keyword
, L
"ps-filename")) {
46 filename
= dupstr(adv(p
->origkeyword
));
51 fp
= fopen(filename
, "w");
53 error(err_cantopenw
, filename
);
57 fprintf(fp
, "%%!PS-Adobe-3.0\n");
58 fprintf(fp
, "%%%%Creator: Halibut, %s\n", version
);
59 fprintf(fp
, "%%%%DocumentData: Clean7Bit\n");
60 fprintf(fp
, "%%%%LanguageLevel: 1\n");
61 for (pageno
= 0, page
= doc
->pages
; page
; page
= page
->next
)
63 fprintf(fp
, "%%%%Pages: %d\n", pageno
);
64 for (p
= sourceform
; p
; p
= p
->next
)
65 if (p
->type
== para_Title
)
66 ps_comment(fp
, "%%Title: ", p
->words
);
67 fprintf(fp
, "%%%%DocumentNeededResources:\n");
68 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
)
69 /* XXX This may request the same font multiple times. */
70 if (!fe
->font
->info
->fontfile
)
71 fprintf(fp
, "%%%%+ font %s\n", fe
->font
->info
->name
);
72 fprintf(fp
, "%%%%DocumentSuppliedResources: procset Halibut 0 3\n");
73 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
)
74 /* XXX This may request the same font multiple times. */
75 if (fe
->font
->info
->fontfile
)
76 fprintf(fp
, "%%%%+ font %s\n", fe
->font
->info
->name
);
77 fprintf(fp
, "%%%%EndComments\n");
79 fprintf(fp
, "%%%%BeginProlog\n");
80 fprintf(fp
, "%%%%BeginResource: procset Halibut 0 3\n");
82 * Supply a prologue function which allows a reasonably
83 * compressed representation of the text on the pages.
85 * "t" expects two arguments: a y-coordinate, and then an array.
86 * Elements of the array are processed sequentially as follows:
88 * - a number is treated as an x-coordinate
89 * - an array is treated as a (font, size) pair
92 * "r" takes four arguments, and behaves like "rectfill".
95 "/tdict 4 dict dup begin\n"
96 " /arraytype {aload pop scalefont setfont} bind def\n"
97 " /realtype {1 index moveto} bind def\n"
98 " /integertype /realtype load def\n"
99 " /stringtype {show} bind def\n"
101 "/t { tdict begin {dup type exec} forall end pop } bind def\n"
102 "/r { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
103 " neg 0 rlineto closepath fill } bind def\n");
107 * "p" generates a named destination referencing this page.
108 * "x" generates a link to a named destination.
109 * "u" generates a link to a URI.
110 * "o" generates an outline entry.
111 * "m" generates a general pdfmark.
113 * They all do nothing if pdfmark is undefined.
116 "/pdfmark where { pop\n"
117 " /p { [ /Dest 3 -1 roll /View [ /XYZ null null null ]\n"
118 " /DEST pdfmark } bind def\n"
119 " /x { [ /Dest 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
120 " /Subtype /Link /ANN pdfmark } bind def\n"
121 " /u { 2 dict dup /Subtype /URI put dup /URI 4 -1 roll put\n"
122 " [ /Action 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
123 " /Subtype /Link /ANN pdfmark } bind def\n"
124 " /o { [ /Count 3 -1 roll /Dest 5 -1 roll /Title 7 -1 roll\n"
125 " /OUT pdfmark } bind def\n"
126 " /m /pdfmark load def\n"
129 " /p { pop } bind def\n"
130 " /x { pop pop } bind def\n"
132 " /o { pop pop pop } bind def\n"
133 " /m /cleartomark load def\n"
136 fprintf(fp
, "%%%%EndResource\n");
137 fprintf(fp
, "%%%%EndProlog\n");
139 fprintf(fp
, "%%%%BeginSetup\n");
142 * Assign a destination name to each page for pdfmark purposes.
145 for (page
= doc
->pages
; page
; page
= page
->next
) {
148 buf
= snewn(12, char);
149 sprintf(buf
, "/p%d", pageno
);
154 * This is as good a place as any to put version IDs.
156 for (p
= sourceform
; p
; p
= p
->next
)
157 if (p
->type
== para_VersionID
)
158 ps_comment(fp
, "% ", p
->words
);
162 * Request the correct page size. We might want to bracket this
163 * with "%%BeginFeature: *PageSize A4" or similar, and "%%EndFeature",
164 * but that would require us to have a way of getting the name of
165 * the page size given its dimensions.
167 ps_token(fp
, &cc
, "/setpagedevice where {\n");
168 ps_token(fp
, &cc
, " pop 2 dict dup /PageSize [%g %g] put setpagedevice\n",
169 doc
->paper_width
/ FUNITS_PER_PT
,
170 doc
->paper_height
/ FUNITS_PER_PT
);
171 ps_token(fp
, &cc
, "} if\n");
173 ps_token(fp
, &cc
, "[/PageMode/UseOutlines/DOCVIEW m\n");
174 noe
= doc
->n_outline_elements
;
175 for (oe
= doc
->outline_elements
; noe
; oe
++, noe
--) {
177 int titlelen
, count
, i
;
179 title
= pdf_outline_convert(oe
->pdata
->outline_title
, &titlelen
);
180 if (oe
->level
== 0) {
181 ps_token(fp
, &cc
, "[/Title");
182 ps_string_len(fp
, &cc
, title
, titlelen
);
183 ps_token(fp
, &cc
, "/DOCINFO m\n");
187 for (i
= 1; i
< noe
&& oe
[i
].level
> oe
->level
; i
++)
188 if (oe
[i
].level
== oe
->level
+ 1)
190 if (oe
->level
> 0) count
= -count
;
192 ps_string_len(fp
, &cc
, title
, titlelen
);
194 ps_token(fp
, &cc
, "%s %d o\n",
195 (char *)oe
->pdata
->first
->page
->spare
, count
);
198 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
199 /* XXX This may request the same font multiple times. */
200 if (fe
->font
->info
->fontfile
) {
201 fprintf(fp
, "%%%%BeginResource: font %s\n", fe
->font
->info
->name
);
202 if (fe
->font
->info
->filetype
== TYPE1
)
203 pf_writeps(fe
->font
->info
, fp
);
205 sfnt_writeps(fe
->font
->info
, fp
);
206 fprintf(fp
, "%%%%EndResource\n");
208 fprintf(fp
, "%%%%IncludeResource: font %s\n",
209 fe
->font
->info
->name
);
214 * Re-encode the fonts.
217 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
221 sprintf(fname
, "f%d", font_index
++);
222 fe
->name
= dupstr(fname
);
224 ps_token(fp
, &cc
, "/%s findfont dup length dict begin\n",
225 fe
->font
->info
->name
);
226 ps_token(fp
, &cc
, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
227 ps_token(fp
, &cc
, "/Encoding [\n");
228 for (i
= 0; i
< 256; i
++)
229 ps_token(fp
, &cc
, "/%s", glyph_extern(fe
->vector
[i
]));
230 ps_token(fp
, &cc
, "] def\n");
231 ps_token(fp
, &cc
, "currentdict end\n");
232 ps_token(fp
, &cc
, "/fontname-%s exch definefont /%s exch def\n",
235 fprintf(fp
, "%%%%EndSetup\n");
238 * Output the text and graphics.
241 for (page
= doc
->pages
; page
; page
= page
->next
) {
242 text_fragment
*frag
, *frag_end
;
249 fprintf(fp
, "%%%%Page: %d %d\n", pageno
, pageno
);
251 ps_token(fp
, &cc
, "save %s p\n", (char *)page
->spare
);
253 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
254 ps_token(fp
, &cc
, "[%g %g %g %g]",
255 xr
->lx
/FUNITS_PER_PT
, xr
->by
/FUNITS_PER_PT
,
256 xr
->rx
/FUNITS_PER_PT
, xr
->ty
/FUNITS_PER_PT
);
257 if (xr
->dest
.type
== PAGE
) {
258 ps_token(fp
, &cc
, "%s x\n", (char *)xr
->dest
.page
->spare
);
260 ps_string(fp
, &cc
, xr
->dest
.url
);
261 ps_token(fp
, &cc
, "u\n");
265 for (r
= page
->first_rect
; r
; r
= r
->next
) {
266 ps_token(fp
, &cc
, "%g %g %g %g r\n",
267 r
->x
/ FUNITS_PER_PT
, r
->y
/ FUNITS_PER_PT
,
268 r
->w
/ FUNITS_PER_PT
, r
->h
/ FUNITS_PER_PT
);
271 frag
= page
->first_text
;
276 * Collect all the adjacent text fragments with the
279 for (frag_end
= frag
;
280 frag_end
&& frag_end
->y
== frag
->y
;
281 frag_end
= frag_end
->next
);
283 ps_token(fp
, &cc
, "%g[", frag
->y
/ FUNITS_PER_PT
);
285 while (frag
&& frag
!= frag_end
) {
287 if (frag
->fe
!= fe
|| frag
->fontsize
!= fs
)
288 ps_token(fp
, &cc
, "[%s %d]",
289 frag
->fe
->name
, frag
->fontsize
);
293 ps_token(fp
, &cc
, "%g", frag
->x
/FUNITS_PER_PT
);
294 ps_string(fp
, &cc
, frag
->text
);
299 ps_token(fp
, &cc
, "]t\n");
302 ps_token(fp
, &cc
, "restore showpage\n");
305 fprintf(fp
, "%%%%EOF\n");
312 static void ps_comment(FILE *fp
, char const *leader
, word
*words
) {
315 cc
+= fprintf(fp
, "%s", leader
);
317 for (; words
; words
= words
->next
) {
321 switch (words
->type
) {
331 type
= removeattr(words
->type
);
335 text
= utoa_dup(words
->text
, CS_ASCII
);
337 case word_WhiteSpace
:
345 if (cc
+ strlen(text
) > PS_MAXWIDTH
)
346 text
[PS_MAXWIDTH
- cc
] = 0;
347 cc
+= fprintf(fp
, "%s", text
);
354 void ps_token(FILE *fp
, int *cc
, char const *fmt
, ...) {
358 if (*cc
>= PS_WIDTH
- 10) {
362 *cc
+= vfprintf(fp
, fmt
, ap
);
363 /* Assume that \n only occurs at the end of a string */
364 if (fmt
[strlen(fmt
) - 1] == '\n')
368 static void ps_string_len(FILE *fp
, int *cc
, char const *str
, int len
) {
372 for (c
= str
; c
< str
+len
; c
++) {
373 if (*c
< ' ' || *c
> '~')
375 else if (*c
== '(' || *c
== ')' || *c
== '\\')
381 ps_token(fp
, cc
, "<");
382 for (c
= str
; c
< str
+len
; c
++) {
383 ps_token(fp
, cc
, "%02X", 0xFF & (int)*c
);
385 ps_token(fp
, cc
, ">");
387 *cc
+= fprintf(fp
, "(");
388 for (c
= str
; c
< str
+len
; c
++) {
389 if (*cc
>= PS_WIDTH
- 4) {
393 if (*c
< ' ' || *c
> '~') {
394 *cc
+= fprintf(fp
, "\\%03o", 0xFF & (int)*c
);
396 if (*c
== '(' || *c
== ')' || *c
== '\\') {
404 *cc
+= fprintf(fp
, ")");
408 static void ps_string(FILE *fp
, int *cc
, char const *str
) {
409 ps_string_len(fp
, cc
, str
, strlen(str
));