2 * PostScript backend for Halibut
9 static void ps_comment(FILE *fp
, char const *leader
, word
*words
);
11 paragraph
*ps_config_filename(char *filename
)
13 return cmdline_cfg_simple("ps-filename", filename
, NULL
);
16 void ps_backend(paragraph
*sourceform
, keywordlist
*keywords
,
17 indexdata
*idx
, void *vdoc
) {
18 document
*doc
= (document
*)vdoc
;
30 filename
= dupstr("output.ps");
31 for (p
= sourceform
; p
; p
= p
->next
) {
32 if (p
->type
== para_Config
) {
33 if (!ustricmp(p
->keyword
, L
"ps-filename")) {
35 filename
= dupstr(adv(p
->origkeyword
));
40 fp
= fopen(filename
, "w");
42 error(err_cantopenw
, filename
);
46 fprintf(fp
, "%%!PS-Adobe-3.0\n");
47 fprintf(fp
, "%%%%Creator: Halibut, %s\n", version
);
48 fprintf(fp
, "%%%%DocumentData: Clean8Bit\n");
49 fprintf(fp
, "%%%%LanguageLevel: 1\n");
50 for (pageno
= 0, page
= doc
->pages
; page
; page
= page
->next
)
52 fprintf(fp
, "%%%%Pages: %d\n", pageno
);
53 for (p
= sourceform
; p
; p
= p
->next
)
54 if (p
->type
== para_Title
)
55 ps_comment(fp
, "%%Title: ", p
->words
);
56 fprintf(fp
, "%%%%DocumentNeededResources:\n");
57 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
)
58 /* XXX This may request the same font multiple times. */
59 fprintf(fp
, "%%%%+ font %s\n", fe
->font
->info
->name
);
60 fprintf(fp
, "%%%%DocumentSuppliedResources: procset Halibut 0 0\n");
61 fprintf(fp
, "%%%%EndComments\n");
63 fprintf(fp
, "%%%%BeginProlog\n");
64 fprintf(fp
, "%%%%BeginResource: procset Halibut 0 0\n");
66 * Supply a prologue function which allows a reasonably
67 * compressed representation of the text on the pages.
69 * Expects two arguments: a y-coordinate, and then an array.
70 * Elements of the array are processed sequentially as follows:
72 * - a number is treated as an x-coordinate
73 * - an array is treated as a (font, size) pair
77 "/tdict 4 dict dup begin\n"
78 " /arraytype {aload pop scalefont setfont} bind def\n"
79 " /realtype {1 index moveto} bind def\n"
80 " /integertype /realtype load def\n"
81 " /stringtype {show} bind def\n"
83 "/t { tdict begin {dup type exec} forall end pop } bind def\n");
85 fprintf(fp
, "%%%%EndResource\n");
86 fprintf(fp
, "%%%%EndProlog\n");
88 fprintf(fp
, "%%%%BeginSetup\n");
91 * This is as good a place as any to put version IDs.
93 for (p
= sourceform
; p
; p
= p
->next
)
94 if (p
->type
== para_VersionID
)
95 ps_comment(fp
, "% ", p
->words
);
97 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
)
98 /* XXX This may request the same font multiple times. */
99 fprintf(fp
, "%%%%IncludeResource: font %s\n", fe
->font
->info
->name
);
102 * Re-encode the fonts.
105 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
109 sprintf(fname
, "f%d", font_index
++);
110 fe
->name
= dupstr(fname
);
112 fprintf(fp
, "/%s findfont dup length dict begin\n",
113 fe
->font
->info
->name
);
114 fprintf(fp
, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
115 fprintf(fp
, "/Encoding [\n");
116 for (i
= 0; i
< 256; i
++)
117 fprintf(fp
, "/%s%c", fe
->vector
[i
] ? fe
->vector
[i
] : ".notdef",
118 i
% 4 == 3 ?
'\n' : ' ');
119 fprintf(fp
, "] def\n");
120 fprintf(fp
, "currentdict end\n");
121 fprintf(fp
, "/fontname-%s exch definefont /%s exch def\n\n",
124 fprintf(fp
, "%%%%EndSetup\n");
127 * Output the text and graphics.
130 for (page
= doc
->pages
; page
; page
= page
->next
) {
131 text_fragment
*frag
, *frag_end
;
135 fprintf(fp
, "%%%%Page: %d %d\n", pageno
, pageno
);
136 fprintf(fp
, "save\n");
142 * I used this diagnostic briefly to ensure that
143 * cross-reference rectangles were being put where they
146 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
147 fprintf(fp
, "gsave 0.7 setgray %g %g moveto",
148 xr
->lx
/FUNITS_PER_PT
, xr
->ty
/FUNITS_PER_PT
);
149 fprintf(fp
, " %g %g lineto %g %g lineto",
150 xr
->lx
/FUNITS_PER_PT
, xr
->by
/FUNITS_PER_PT
,
151 xr
->rx
/FUNITS_PER_PT
, xr
->by
/FUNITS_PER_PT
);
152 fprintf(fp
, " %g %g lineto closepath fill grestore\n",
153 xr
->rx
/FUNITS_PER_PT
, xr
->ty
/FUNITS_PER_PT
);
158 for (r
= page
->first_rect
; r
; r
= r
->next
) {
159 fprintf(fp
, "%g %g moveto %g 0 rlineto 0 %g rlineto "
160 "-%g 0 rlineto closepath fill\n",
161 r
->x
/ FUNITS_PER_PT
, r
->y
/ FUNITS_PER_PT
,
162 r
->w
/ FUNITS_PER_PT
, r
->h
/ FUNITS_PER_PT
,
163 r
->w
/ FUNITS_PER_PT
);
166 frag
= page
->first_text
;
173 * Collect all the adjacent text fragments with the
176 for (frag_end
= frag
;
177 frag_end
&& frag_end
->y
== frag
->y
;
178 frag_end
= frag_end
->next
);
180 fprintf(fp
, "%g[", frag
->y
/ FUNITS_PER_PT
);
185 while (frag
&& frag
!= frag_end
) {
187 if (frag
->fe
!= fe
|| frag
->fontsize
!= fs
)
188 fprintf(fp
, "[%s %d]", frag
->fe
->name
, frag
->fontsize
);
192 fprintf(fp
, "%g(", frag
->x
/FUNITS_PER_PT
);
193 for (c
= frag
->text
; *c
; c
++) {
194 if (*c
== '(' || *c
== ')' || *c
== '\\')
206 fprintf(fp
, "restore showpage\n");
209 fprintf(fp
, "%%%%EOF\n");
216 static void ps_comment(FILE *fp
, char const *leader
, word
*words
)
218 fprintf(fp
, "%s", leader
);
220 for (; words
; words
= words
->next
) {
224 switch (words
->type
) {
234 type
= removeattr(words
->type
);
238 text
= utoa_dup(words
->text
, CS_ASCII
);
240 case word_WhiteSpace
: