43341922 |
1 | /* |
2 | * Paper printing pre-backend for Halibut. |
3 | * |
4 | * This module does all the processing common to both PostScript |
5 | * and PDF output: selecting fonts, line wrapping and page breaking |
6 | * in accordance with font metrics, laying out the contents and |
7 | * index pages, generally doing all the page layout. After this, |
8 | * bk_ps.c and bk_pdf.c should only need to do linear translations |
9 | * into their literal output format. |
10 | */ |
11 | |
12 | /* |
9a8dc6b1 |
13 | * TODO in future work: |
43341922 |
14 | * |
15 | * - linearised PDF, perhaps? |
16 | * |
17 | * - I'm uncertain of whether I need to include a ToUnicode CMap |
18 | * in each of my font definitions in PDF. Currently things (by |
19 | * which I mean cut and paste out of acroread) seem to be |
20 | * working fairly happily without it, but I don't know. |
21 | * |
9a8dc6b1 |
22 | * - rather than the ugly aux_text mechanism for rendering chapter |
23 | * titles, we could actually build the correct word list and |
24 | * wrap it as a whole. |
25 | * |
26 | * - get vertical font metrics and use them to position the PDF |
27 | * xref boxes more pleasantly |
28 | * |
43341922 |
29 | * - configurability |
9a8dc6b1 |
30 | * * page header and footer should be configurable; we should |
31 | * be able to shift the page number elsewhere, and add other |
32 | * things such as the current chapter/section title and fixed |
33 | * text |
34 | * * remove the fixed mapping from heading levels to heading |
35 | * styles; offer a menu of styles from which the user can |
36 | * choose at every heading level |
37 | * * first-line indent in paragraphs |
dd567011 |
38 | * * fixed text: `Contents', `Index', the colon-space and full |
39 | * stop in chapter title constructions |
9a8dc6b1 |
40 | * * configurable location of contents? |
41 | * * certainly configurably _remove_ the contents, and possibly |
42 | * also the index |
43 | * * double-sided document switch? |
44 | * + means you have two header/footer formats which |
45 | * alternate |
46 | * + and means that mandatory page breaks before chapter |
47 | * titles should include a blank page if necessary to |
48 | * start the next section to a right-hand page |
43341922 |
49 | * |
50 | * - title pages |
9a8dc6b1 |
51 | * |
52 | * - ability to import other Type 1 fonts |
53 | * * we need to parse the font to extract its metrics |
54 | * * then we pass the font bodily to both PS and PDF so it can |
55 | * be included in the output file |
266a539f |
56 | * |
57 | * - character substitution for better typography? |
58 | * * fi, fl, ffi, ffl ligatures |
59 | * * use real ellipsis rather than ... |
60 | * * a hyphen in a word by itself might prefer to be an en-dash |
61 | * * (Americans might even want a convenient way to use an |
62 | * em-dash) |
09358aa7 |
63 | * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw! |
266a539f |
64 | * * substituting `minus' for `hyphen' in the standard encoding |
65 | * is probably preferable in Courier, though certainly not in |
66 | * the main text font |
67 | * * if I do do this lot, I'm rather inclined to at least try |
68 | * to think up a configurable way to do it so that Americans |
69 | * can do em-dash tricks without my intervention and other |
70 | * people can do other odd things too. |
43341922 |
71 | */ |
72 | |
73 | #include <assert.h> |
74 | #include <stdio.h> |
dd567011 |
75 | #include <stdarg.h> |
ba0fe3ec |
76 | #include <stdlib.h> |
43341922 |
77 | |
78 | #include "halibut.h" |
79 | #include "paper.h" |
80 | |
be76d597 |
81 | typedef struct paper_conf_Tag paper_conf; |
c6536773 |
82 | typedef struct paper_idx_Tag paper_idx; |
be76d597 |
83 | |
c419cb97 |
84 | typedef struct { |
85 | font_data *fonts[NFONTS]; |
86 | int font_size; |
87 | } font_cfg; |
88 | |
be76d597 |
89 | struct paper_conf_Tag { |
90 | int paper_width; |
91 | int paper_height; |
92 | int left_margin; |
93 | int top_margin; |
94 | int right_margin; |
95 | int bottom_margin; |
96 | int indent_list_bullet; |
dd567011 |
97 | int indent_list_after; |
be76d597 |
98 | int indent_list; |
99 | int indent_quote; |
100 | int base_leading; |
101 | int base_para_spacing; |
102 | int chapter_top_space; |
103 | int sect_num_left_space; |
104 | int chapter_underline_depth; |
105 | int chapter_underline_thickness; |
106 | int rule_thickness; |
c419cb97 |
107 | font_cfg fbase, fcode, ftitle, fchapter, *fsect; |
108 | int nfsect; |
2bfd1b76 |
109 | int contents_indent_step; |
110 | int contents_margin; |
111 | int leader_separation; |
c6536773 |
112 | int index_gutter; |
113 | int index_cols; |
114 | int index_minsep; |
9a8dc6b1 |
115 | int pagenum_fontsize; |
116 | int footer_distance; |
dd567011 |
117 | wchar_t *lquote, *rquote, *bullet; |
f6220253 |
118 | wchar_t *contents_text, *index_text; |
be76d597 |
119 | /* These are derived from the above */ |
120 | int base_width; |
121 | int page_height; |
c6536773 |
122 | int index_colwidth; |
be76d597 |
123 | }; |
124 | |
c6536773 |
125 | struct paper_idx_Tag { |
126 | /* |
127 | * Word list giving the page numbers on which this index entry |
128 | * appears. Also the last word in the list, for ease of |
129 | * construction. |
130 | */ |
131 | word *words; |
132 | word *lastword; |
133 | /* |
134 | * The last page added to the list (so we can ensure we don't |
135 | * add one twice). |
136 | */ |
137 | page_data *lastpage; |
138 | }; |
139 | |
3f3d1acc |
140 | enum { |
141 | word_PageXref = word_NotWordType + 1 |
142 | }; |
143 | |
b5232689 |
144 | /* Flags for render_string() */ |
145 | #define RS_NOLIG 1 |
146 | |
43341922 |
147 | static font_data *make_std_font(font_list *fontlist, char const *name); |
148 | static void wrap_paragraph(para_data *pdata, word *words, |
dd567011 |
149 | int w, int i1, int i2, paper_conf *conf); |
43341922 |
150 | static page_data *page_breaks(line_data *first, line_data *last, |
c6536773 |
151 | int page_height, int ncols, int headspace); |
2bfd1b76 |
152 | static int render_string(page_data *page, font_data *font, int fontsize, |
b5232689 |
153 | int x, int y, wchar_t *str, unsigned flags); |
2bfd1b76 |
154 | static int render_line(line_data *ldata, int left_x, int top_y, |
dd567011 |
155 | xref_dest *dest, keywordlist *keywords, indexdata *idx, |
156 | paper_conf *conf); |
c6536773 |
157 | static void render_para(para_data *pdata, paper_conf *conf, |
158 | keywordlist *keywords, indexdata *idx, |
159 | paragraph *index_placeholder, page_data *index_page); |
b5232689 |
160 | static int string_width(font_data *font, wchar_t const *string, int *errs, |
161 | unsigned flags); |
dd567011 |
162 | static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf); |
be76d597 |
163 | static para_data *code_paragraph(int indent, word *words, paper_conf *conf); |
164 | static para_data *rule_paragraph(int indent, paper_conf *conf); |
23765aeb |
165 | static void add_rect_to_page(page_data *page, int x, int y, int w, int h); |
2bfd1b76 |
166 | static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, |
be76d597 |
167 | word *pkwtext, word *pkwtext2, word *pwords, |
168 | paper_conf *conf); |
169 | static void standard_line_spacing(para_data *pdata, paper_conf *conf); |
170 | static wchar_t *prepare_outline_title(word *first, wchar_t *separator, |
171 | word *second); |
2bfd1b76 |
172 | static word *fake_word(wchar_t *text); |
c6536773 |
173 | static word *fake_space_word(void); |
3f3d1acc |
174 | static word *fake_page_ref(page_data *page); |
175 | static word *fake_end_ref(void); |
2bfd1b76 |
176 | static word *prepare_contents_title(word *first, wchar_t *separator, |
177 | word *second); |
c6536773 |
178 | static void fold_into_page(page_data *dest, page_data *src, int right_shift); |
43341922 |
179 | |
dd567011 |
180 | static int fonts_ok(wchar_t *string, ...) |
181 | { |
182 | font_data *font; |
183 | va_list ap; |
184 | int ret = TRUE; |
185 | |
186 | va_start(ap, string); |
187 | while ( (font = va_arg(ap, font_data *)) != NULL) { |
188 | int errs; |
b5232689 |
189 | (void) string_width(font, string, &errs, 0); |
dd567011 |
190 | if (errs) { |
191 | ret = FALSE; |
192 | break; |
193 | } |
194 | } |
195 | va_end(ap); |
196 | |
197 | return ret; |
198 | } |
199 | |
c419cb97 |
200 | static void paper_cfg_fonts(font_data **fonts, font_list *fontlist, |
201 | wchar_t *wp, filepos *fpos) { |
202 | font_data *f; |
203 | char *fn; |
204 | int i; |
205 | |
206 | for (i = 0; i < NFONTS && *wp; i++, wp = uadv(wp)) { |
207 | fn = utoa_dup(wp, CS_ASCII); |
208 | f = make_std_font(fontlist, fn); |
209 | if (f) |
210 | fonts[i] = f; |
211 | else |
212 | /* FIXME: proper error */ |
213 | error(err_nofont, fpos, wp); |
214 | } |
215 | } |
216 | |
dd567011 |
217 | static paper_conf paper_configure(paragraph *source, font_list *fontlist) { |
218 | paragraph *p; |
219 | paper_conf ret; |
220 | |
221 | /* |
222 | * Defaults. |
223 | */ |
17c71b41 |
224 | ret.paper_width = 595 * UNITS_PER_PT; |
30ddbdc1 |
225 | ret.paper_height = 842 * UNITS_PER_PT; |
17c71b41 |
226 | ret.left_margin = 72 * UNITS_PER_PT; |
227 | ret.top_margin = 72 * UNITS_PER_PT; |
228 | ret.right_margin = 72 * UNITS_PER_PT; |
229 | ret.bottom_margin = 108 * UNITS_PER_PT; |
230 | ret.indent_list_bullet = 6 * UNITS_PER_PT; |
231 | ret.indent_list_after = 18 * UNITS_PER_PT; |
232 | ret.indent_quote = 18 * UNITS_PER_PT; |
233 | ret.base_leading = UNITS_PER_PT; |
234 | ret.base_para_spacing = 10 * UNITS_PER_PT; |
235 | ret.chapter_top_space = 72 * UNITS_PER_PT; |
236 | ret.sect_num_left_space = 12 * UNITS_PER_PT; |
237 | ret.chapter_underline_depth = 14 * UNITS_PER_PT; |
238 | ret.chapter_underline_thickness = 3 * UNITS_PER_PT; |
239 | ret.rule_thickness = 1 * UNITS_PER_PT; |
c419cb97 |
240 | ret.fbase.font_size = 12; |
241 | ret.fbase.fonts[FONT_NORMAL] = make_std_font(fontlist, "Times-Roman"); |
242 | ret.fbase.fonts[FONT_EMPH] = make_std_font(fontlist, "Times-Italic"); |
243 | ret.fbase.fonts[FONT_CODE] = make_std_font(fontlist, "Courier"); |
244 | ret.fcode.font_size = 12; |
245 | ret.fcode.fonts[FONT_NORMAL] = make_std_font(fontlist, "Courier-Bold"); |
246 | ret.fcode.fonts[FONT_EMPH] = make_std_font(fontlist, "Courier-Oblique"); |
247 | ret.fcode.fonts[FONT_CODE] = make_std_font(fontlist, "Courier"); |
248 | ret.ftitle.font_size = 24; |
249 | ret.ftitle.fonts[FONT_NORMAL] = make_std_font(fontlist, "Helvetica-Bold"); |
250 | ret.ftitle.fonts[FONT_EMPH] = |
251 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
252 | ret.ftitle.fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
253 | ret.fchapter.font_size = 20; |
254 | ret.fchapter.fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
255 | ret.fchapter.fonts[FONT_EMPH] = |
256 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
257 | ret.fchapter.fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
258 | ret.nfsect = 3; |
259 | ret.fsect = snewn(ret.nfsect, font_cfg); |
260 | ret.fsect[0].font_size = 16; |
261 | ret.fsect[0].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
262 | ret.fsect[0].fonts[FONT_EMPH] = |
263 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
264 | ret.fsect[0].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
265 | ret.fsect[1].font_size = 14; |
266 | ret.fsect[1].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
267 | ret.fsect[1].fonts[FONT_EMPH] = |
268 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
269 | ret.fsect[1].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
270 | ret.fsect[2].font_size = 13; |
271 | ret.fsect[2].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
272 | ret.fsect[2].fonts[FONT_EMPH] = |
273 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
274 | ret.fsect[2].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
17c71b41 |
275 | ret.contents_indent_step = 24 * UNITS_PER_PT; |
276 | ret.contents_margin = 84 * UNITS_PER_PT; |
277 | ret.leader_separation = 12 * UNITS_PER_PT; |
278 | ret.index_gutter = 36 * UNITS_PER_PT; |
dd567011 |
279 | ret.index_cols = 2; |
17c71b41 |
280 | ret.index_minsep = 18 * UNITS_PER_PT; |
dd567011 |
281 | ret.pagenum_fontsize = 12; |
17c71b41 |
282 | ret.footer_distance = 32 * UNITS_PER_PT; |
dd567011 |
283 | ret.lquote = L"\x2018\0\x2019\0'\0'\0\0"; |
284 | ret.rquote = uadv(ret.lquote); |
285 | ret.bullet = L"\x2022\0-\0\0"; |
f6220253 |
286 | ret.contents_text = L"Contents"; |
287 | ret.index_text = L"Index"; |
dd567011 |
288 | |
289 | /* |
290 | * Two-pass configuration so that we can pick up global config |
291 | * (e.g. `quotes') before having it overridden by specific |
292 | * config (`paper-quotes'), irrespective of the order in which |
293 | * they occur. |
294 | */ |
295 | for (p = source; p; p = p->next) { |
296 | if (p->type == para_Config) { |
297 | if (!ustricmp(p->keyword, L"quotes")) { |
298 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
299 | ret.lquote = uadv(p->keyword); |
300 | ret.rquote = uadv(ret.lquote); |
301 | } |
302 | } |
303 | } |
304 | } |
305 | |
306 | for (p = source; p; p = p->next) { |
307 | p->private_data = NULL; |
308 | if (p->type == para_Config) { |
309 | if (!ustricmp(p->keyword, L"paper-quotes")) { |
310 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
311 | ret.lquote = uadv(p->keyword); |
312 | ret.rquote = uadv(ret.lquote); |
313 | } |
f6220253 |
314 | } else if (!ustricmp(p->keyword, L"contents")) { |
315 | ret.contents_text = uadv(p->keyword); |
316 | } else if (!ustricmp(p->keyword, L"index")) { |
317 | ret.index_text = uadv(p->keyword); |
dd567011 |
318 | } else if (!ustricmp(p->keyword, L"paper-bullet")) { |
319 | ret.bullet = uadv(p->keyword); |
320 | } else if (!ustricmp(p->keyword, L"paper-page-width")) { |
321 | ret.paper_width = |
17c71b41 |
322 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
323 | } else if (!ustricmp(p->keyword, L"paper-page-height")) { |
324 | ret.paper_height = |
17c71b41 |
325 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
326 | } else if (!ustricmp(p->keyword, L"paper-left-margin")) { |
327 | ret.left_margin = |
17c71b41 |
328 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
329 | } else if (!ustricmp(p->keyword, L"paper-top-margin")) { |
330 | ret.top_margin = |
17c71b41 |
331 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
332 | } else if (!ustricmp(p->keyword, L"paper-right-margin")) { |
333 | ret.right_margin = |
17c71b41 |
334 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
335 | } else if (!ustricmp(p->keyword, L"paper-bottom-margin")) { |
336 | ret.bottom_margin = |
17c71b41 |
337 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
338 | } else if (!ustricmp(p->keyword, L"paper-list-indent")) { |
339 | ret.indent_list_bullet = |
17c71b41 |
340 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
341 | } else if (!ustricmp(p->keyword, L"paper-listitem-indent")) { |
342 | ret.indent_list = |
17c71b41 |
343 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
344 | } else if (!ustricmp(p->keyword, L"paper-quote-indent")) { |
345 | ret.indent_quote = |
17c71b41 |
346 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
347 | } else if (!ustricmp(p->keyword, L"paper-base-leading")) { |
348 | ret.base_leading = |
17c71b41 |
349 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
350 | } else if (!ustricmp(p->keyword, L"paper-base-para-spacing")) { |
351 | ret.base_para_spacing = |
17c71b41 |
352 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
353 | } else if (!ustricmp(p->keyword, L"paper-chapter-top-space")) { |
354 | ret.chapter_top_space = |
17c71b41 |
355 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
356 | } else if (!ustricmp(p->keyword, L"paper-sect-num-left-space")) { |
357 | ret.sect_num_left_space = |
17c71b41 |
358 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
359 | } else if (!ustricmp(p->keyword, L"paper-chapter-underline-depth")) { |
360 | ret.chapter_underline_depth = |
17c71b41 |
361 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
362 | } else if (!ustricmp(p->keyword, L"paper-chapter-underline-thickness")) { |
363 | ret.chapter_underline_thickness = |
17c71b41 |
364 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
365 | } else if (!ustricmp(p->keyword, L"paper-rule-thickness")) { |
366 | ret.rule_thickness = |
17c71b41 |
367 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
368 | } else if (!ustricmp(p->keyword, L"paper-contents-indent-step")) { |
369 | ret.contents_indent_step = |
17c71b41 |
370 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
371 | } else if (!ustricmp(p->keyword, L"paper-contents-margin")) { |
372 | ret.contents_margin = |
17c71b41 |
373 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
374 | } else if (!ustricmp(p->keyword, L"paper-leader-separation")) { |
375 | ret.leader_separation = |
17c71b41 |
376 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
377 | } else if (!ustricmp(p->keyword, L"paper-index-gutter")) { |
378 | ret.index_gutter = |
17c71b41 |
379 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
380 | } else if (!ustricmp(p->keyword, L"paper-index-minsep")) { |
381 | ret.index_minsep = |
17c71b41 |
382 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
383 | } else if (!ustricmp(p->keyword, L"paper-footer-distance")) { |
384 | ret.footer_distance = |
17c71b41 |
385 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
dd567011 |
386 | } else if (!ustricmp(p->keyword, L"paper-base-font-size")) { |
c419cb97 |
387 | ret.fbase.font_size = utoi(uadv(p->keyword)); |
dd567011 |
388 | } else if (!ustricmp(p->keyword, L"paper-index-columns")) { |
c419cb97 |
389 | ret.index_cols = utoi(uadv(p->keyword)); |
dd567011 |
390 | } else if (!ustricmp(p->keyword, L"paper-pagenum-font-size")) { |
c419cb97 |
391 | ret.pagenum_fontsize = utoi(uadv(p->keyword)); |
392 | } else if (!ustricmp(p->keyword, L"paper-base-fonts")) { |
393 | paper_cfg_fonts(ret.fbase.fonts, fontlist, uadv(p->keyword), |
394 | &p->fpos); |
395 | } else if (!ustricmp(p->keyword, L"paper-code-font-size")) { |
396 | ret.fcode.font_size = utoi(uadv(p->keyword)); |
397 | } else if (!ustricmp(p->keyword, L"paper-code-fonts")) { |
398 | paper_cfg_fonts(ret.fcode.fonts, fontlist, uadv(p->keyword), |
399 | &p->fpos); |
400 | } else if (!ustricmp(p->keyword, L"paper-title-font-size")) { |
401 | ret.ftitle.font_size = utoi(uadv(p->keyword)); |
402 | } else if (!ustricmp(p->keyword, L"paper-title-fonts")) { |
403 | paper_cfg_fonts(ret.ftitle.fonts, fontlist, uadv(p->keyword), |
404 | &p->fpos); |
405 | } else if (!ustricmp(p->keyword, L"paper-chapter-font-size")) { |
ba0fe3ec |
406 | ret.fchapter.font_size = utoi(uadv(p->keyword)); |
c419cb97 |
407 | } else if (!ustricmp(p->keyword, L"paper-chapter-fonts")) { |
ba0fe3ec |
408 | paper_cfg_fonts(ret.fchapter.fonts, fontlist, uadv(p->keyword), |
c419cb97 |
409 | &p->fpos); |
410 | } else if (!ustricmp(p->keyword, L"paper-section-font-size")) { |
411 | wchar_t *q = uadv(p->keyword); |
412 | int n = 0; |
413 | if (uisdigit(*q)) { |
414 | n = utoi(q); |
415 | q = uadv(q); |
416 | } |
417 | if (n >= ret.nfsect) { |
418 | int i; |
419 | ret.fsect = sresize(ret.fsect, n+1, font_cfg); |
420 | for (i = ret.nfsect; i <= n; i++) |
421 | ret.fsect[i] = ret.fsect[ret.nfsect-1]; |
422 | ret.nfsect = n+1; |
423 | } |
424 | ret.fsect[n].font_size = utoi(q); |
425 | } else if (!ustricmp(p->keyword, L"paper-section-fonts")) { |
426 | wchar_t *q = uadv(p->keyword); |
427 | int n = 0; |
428 | if (uisdigit(*q)) { |
429 | n = utoi(q); |
430 | q = uadv(q); |
431 | } |
432 | if (n >= ret.nfsect) { |
433 | int i; |
434 | ret.fsect = sresize(ret.fsect, n+1, font_cfg); |
435 | for (i = ret.nfsect; i <= n; i++) |
436 | ret.fsect[i] = ret.fsect[ret.nfsect-1]; |
437 | ret.nfsect = n+1; |
438 | } |
439 | paper_cfg_fonts(ret.fsect[n].fonts, fontlist, q, &p->fpos); |
440 | } |
dd567011 |
441 | } |
442 | } |
443 | |
444 | /* |
445 | * Set up the derived fields in the conf structure. |
446 | */ |
447 | |
448 | ret.base_width = |
449 | ret.paper_width - ret.left_margin - ret.right_margin; |
450 | ret.page_height = |
451 | ret.paper_height - ret.top_margin - ret.bottom_margin; |
452 | ret.indent_list = ret.indent_list_bullet + ret.indent_list_after; |
453 | ret.index_colwidth = |
454 | (ret.base_width - (ret.index_cols-1) * ret.index_gutter) |
455 | / ret.index_cols; |
456 | |
457 | /* |
dd567011 |
458 | * Now process fallbacks on quote characters and bullets. We |
459 | * use string_width() to determine whether all of the relevant |
460 | * fonts contain the same character, and fall back whenever we |
461 | * find a character which not all of them support. |
462 | */ |
463 | |
464 | /* Quote characters need not be supported in the fixed code fonts, |
465 | * but must be in the title and body fonts. */ |
c419cb97 |
466 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote))) { |
467 | int n; |
ba0fe3ec |
468 | if (fonts_ok(ret.lquote, |
469 | ret.fbase.fonts[FONT_NORMAL], |
470 | ret.fbase.fonts[FONT_EMPH], |
471 | ret.ftitle.fonts[FONT_NORMAL], |
472 | ret.ftitle.fonts[FONT_EMPH], |
473 | ret.fchapter.fonts[FONT_NORMAL], |
474 | ret.fchapter.fonts[FONT_EMPH], NULL) && |
475 | fonts_ok(ret.rquote, |
476 | ret.fbase.fonts[FONT_NORMAL], |
477 | ret.fbase.fonts[FONT_EMPH], |
478 | ret.ftitle.fonts[FONT_NORMAL], |
479 | ret.ftitle.fonts[FONT_EMPH], |
480 | ret.fchapter.fonts[FONT_NORMAL], |
481 | ret.fchapter.fonts[FONT_EMPH], NULL)) { |
482 | for (n = 0; n < ret.nfsect; n++) |
483 | if (!fonts_ok(ret.lquote, |
484 | ret.fsect[n].fonts[FONT_NORMAL], |
485 | ret.fsect[n].fonts[FONT_EMPH], NULL) || |
486 | !fonts_ok(ret.rquote, |
487 | ret.fsect[n].fonts[FONT_NORMAL], |
488 | ret.fsect[n].fonts[FONT_EMPH], NULL)) |
489 | break; |
490 | if (n == ret.nfsect) |
c419cb97 |
491 | break; |
ba0fe3ec |
492 | } |
dd567011 |
493 | ret.lquote = uadv(ret.rquote); |
494 | ret.rquote = uadv(ret.lquote); |
495 | } |
496 | |
497 | /* The bullet character only needs to be supported in the normal body |
498 | * font (not even in italics). */ |
499 | while (*ret.bullet && *uadv(ret.bullet) && |
c419cb97 |
500 | !fonts_ok(ret.bullet, ret.fbase.fonts[FONT_NORMAL], NULL)) |
dd567011 |
501 | ret.bullet = uadv(ret.bullet); |
502 | |
503 | return ret; |
504 | } |
505 | |
43341922 |
506 | void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, |
507 | indexdata *idx) { |
508 | paragraph *p; |
509 | document *doc; |
2bfd1b76 |
510 | int indent, used_contents; |
be76d597 |
511 | para_data *pdata, *firstpara = NULL, *lastpara = NULL; |
2bfd1b76 |
512 | para_data *firstcont, *lastcont; |
c6536773 |
513 | line_data *firstline, *lastline, *firstcontline, *lastcontline; |
43341922 |
514 | page_data *pages; |
515 | font_list *fontlist; |
dd567011 |
516 | paper_conf *conf, ourconf; |
c6536773 |
517 | int has_index; |
518 | int pagenum; |
519 | paragraph index_placeholder_para; |
520 | page_data *first_index_page; |
43341922 |
521 | |
ba0fe3ec |
522 | init_std_fonts(); |
f1530049 |
523 | fontlist = snew(font_list); |
43341922 |
524 | fontlist->head = fontlist->tail = NULL; |
dd567011 |
525 | |
526 | ourconf = paper_configure(sourceform, fontlist); |
527 | conf = &ourconf; |
43341922 |
528 | |
529 | /* |
c6536773 |
530 | * Set up a data structure to collect page numbers for each |
531 | * index entry. |
532 | */ |
533 | { |
534 | int i; |
535 | indexentry *entry; |
536 | |
537 | has_index = FALSE; |
538 | |
539 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
f1530049 |
540 | paper_idx *pi = snew(paper_idx); |
c6536773 |
541 | |
542 | has_index = TRUE; |
543 | |
544 | pi->words = pi->lastword = NULL; |
545 | pi->lastpage = NULL; |
546 | |
547 | entry->backend_data = pi; |
548 | } |
549 | } |
550 | |
551 | /* |
2bfd1b76 |
552 | * Format the contents entry for each heading. |
553 | */ |
554 | { |
555 | word *contents_title; |
f6220253 |
556 | contents_title = fake_word(conf->contents_text); |
2bfd1b76 |
557 | |
558 | firstcont = make_para_data(para_UnnumberedChapter, 0, 0, 0, |
559 | NULL, NULL, contents_title, conf); |
560 | lastcont = firstcont; |
561 | lastcont->next = NULL; |
562 | firstcontline = firstcont->first; |
563 | lastcontline = lastcont->last; |
564 | for (p = sourceform; p; p = p->next) { |
565 | word *words; |
566 | int indent; |
567 | |
568 | switch (p->type) { |
569 | case para_Chapter: |
570 | case para_Appendix: |
571 | case para_UnnumberedChapter: |
572 | case para_Heading: |
573 | case para_Subsect: |
574 | switch (p->type) { |
575 | case para_Chapter: |
576 | case para_Appendix: |
577 | words = prepare_contents_title(p->kwtext, L": ", p->words); |
578 | indent = 0; |
579 | break; |
580 | case para_UnnumberedChapter: |
581 | words = prepare_contents_title(NULL, NULL, p->words); |
582 | indent = 0; |
583 | break; |
584 | case para_Heading: |
585 | case para_Subsect: |
586 | words = prepare_contents_title(p->kwtext2, L" ", p->words); |
587 | indent = (p->aux + 1) * conf->contents_indent_step; |
588 | break; |
589 | } |
590 | pdata = make_para_data(para_Normal, p->aux, indent, |
591 | conf->contents_margin, |
592 | NULL, NULL, words, conf); |
593 | pdata->next = NULL; |
594 | pdata->contents_entry = p; |
595 | lastcont->next = pdata; |
596 | lastcont = pdata; |
597 | |
598 | /* |
599 | * Link all contents line structures together into |
600 | * a big list. |
601 | */ |
602 | if (pdata->first) { |
603 | if (lastcontline) { |
604 | lastcontline->next = pdata->first; |
605 | pdata->first->prev = lastcontline; |
606 | } else { |
607 | firstcontline = pdata->first; |
608 | pdata->first->prev = NULL; |
609 | } |
610 | lastcontline = pdata->last; |
611 | lastcontline->next = NULL; |
612 | } |
613 | |
614 | break; |
615 | } |
616 | } |
c6536773 |
617 | |
618 | /* |
619 | * And one extra one, for the index. |
620 | */ |
621 | if (has_index) { |
622 | pdata = make_para_data(para_Normal, 0, 0, |
623 | conf->contents_margin, |
f6220253 |
624 | NULL, NULL, |
625 | fake_word(conf->index_text), conf); |
c6536773 |
626 | pdata->next = NULL; |
627 | pdata->contents_entry = &index_placeholder_para; |
628 | lastcont->next = pdata; |
629 | lastcont = pdata; |
630 | |
631 | if (pdata->first) { |
632 | if (lastcontline) { |
633 | lastcontline->next = pdata->first; |
634 | pdata->first->prev = lastcontline; |
635 | } else { |
636 | firstcontline = pdata->first; |
637 | pdata->first->prev = NULL; |
638 | } |
639 | lastcontline = pdata->last; |
640 | lastcontline->next = NULL; |
641 | } |
642 | } |
2bfd1b76 |
643 | } |
644 | |
645 | /* |
646 | * Do the main paragraph formatting. |
43341922 |
647 | */ |
648 | indent = 0; |
2bfd1b76 |
649 | used_contents = FALSE; |
43341922 |
650 | firstline = lastline = NULL; |
651 | for (p = sourceform; p; p = p->next) { |
652 | p->private_data = NULL; |
653 | |
654 | switch (p->type) { |
655 | /* |
656 | * These paragraph types are either invisible or don't |
657 | * define text in the normal sense. Either way, they |
658 | * don't require wrapping. |
659 | */ |
660 | case para_IM: |
661 | case para_BR: |
43341922 |
662 | case para_Biblio: |
663 | case para_NotParaType: |
664 | case para_Config: |
665 | case para_VersionID: |
666 | case para_NoCite: |
667 | break; |
668 | |
669 | /* |
670 | * These paragraph types don't require wrapping, but |
671 | * they do affect the line width to which we wrap the |
672 | * rest of the paragraphs, so we need to pay attention. |
673 | */ |
674 | case para_LcontPush: |
be76d597 |
675 | indent += conf->indent_list; break; |
43341922 |
676 | case para_LcontPop: |
be76d597 |
677 | indent -= conf->indent_list; assert(indent >= 0); break; |
43341922 |
678 | case para_QuotePush: |
be76d597 |
679 | indent += conf->indent_quote; break; |
43341922 |
680 | case para_QuotePop: |
be76d597 |
681 | indent -= conf->indent_quote; assert(indent >= 0); break; |
43341922 |
682 | |
683 | /* |
684 | * This paragraph type is special. Process it |
685 | * specially. |
686 | */ |
687 | case para_Code: |
be76d597 |
688 | pdata = code_paragraph(indent, p->words, conf); |
515d216b |
689 | p->private_data = pdata; |
39a0cfb9 |
690 | if (pdata->first != pdata->last) { |
691 | pdata->first->penalty_after += 100000; |
692 | pdata->last->penalty_before += 100000; |
693 | } |
43341922 |
694 | break; |
695 | |
696 | /* |
87bd6353 |
697 | * This paragraph is also special. |
698 | */ |
699 | case para_Rule: |
be76d597 |
700 | pdata = rule_paragraph(indent, conf); |
87bd6353 |
701 | p->private_data = pdata; |
702 | break; |
703 | |
704 | /* |
43341922 |
705 | * All of these paragraph types require wrapping in the |
706 | * ordinary way. So we must supply a set of fonts, a |
707 | * line width and auxiliary information (e.g. bullet |
708 | * text) for each one. |
709 | */ |
710 | case para_Chapter: |
711 | case para_Appendix: |
712 | case para_UnnumberedChapter: |
713 | case para_Heading: |
714 | case para_Subsect: |
715 | case para_Normal: |
716 | case para_BiblioCited: |
717 | case para_Bullet: |
718 | case para_NumberedList: |
719 | case para_DescribedThing: |
720 | case para_Description: |
721 | case para_Copyright: |
722 | case para_Title: |
2bfd1b76 |
723 | pdata = make_para_data(p->type, p->aux, indent, 0, |
be76d597 |
724 | p->kwtext, p->kwtext2, p->words, conf); |
43341922 |
725 | |
43341922 |
726 | p->private_data = pdata; |
727 | |
515d216b |
728 | break; |
729 | } |
730 | |
731 | if (p->private_data) { |
732 | pdata = (para_data *)p->private_data; |
733 | |
43341922 |
734 | /* |
2bfd1b76 |
735 | * If this is the first non-title heading, we link the |
736 | * contents section in before it. |
737 | */ |
738 | if (!used_contents && pdata->outline_level > 0) { |
739 | used_contents = TRUE; |
740 | if (lastpara) |
741 | lastpara->next = firstcont; |
742 | else |
743 | firstpara = firstcont; |
744 | lastpara = lastcont; |
745 | assert(lastpara->next == NULL); |
746 | |
747 | if (lastline) { |
748 | lastline->next = firstcontline; |
749 | firstcontline->prev = lastline; |
750 | } else { |
751 | firstline = firstcontline; |
752 | firstcontline->prev = NULL; |
753 | } |
754 | assert(lastcontline != NULL); |
755 | lastline = lastcontline; |
756 | lastline->next = NULL; |
757 | } |
758 | |
759 | /* |
515d216b |
760 | * Link all line structures together into a big list. |
761 | */ |
43341922 |
762 | if (pdata->first) { |
763 | if (lastline) { |
764 | lastline->next = pdata->first; |
765 | pdata->first->prev = lastline; |
766 | } else { |
767 | firstline = pdata->first; |
768 | pdata->first->prev = NULL; |
769 | } |
770 | lastline = pdata->last; |
2bfd1b76 |
771 | lastline->next = NULL; |
43341922 |
772 | } |
be76d597 |
773 | |
774 | /* |
775 | * Link all paragraph structures together similarly. |
776 | */ |
777 | pdata->next = NULL; |
778 | if (lastpara) |
779 | lastpara->next = pdata; |
780 | else |
781 | firstpara = pdata; |
782 | lastpara = pdata; |
43341922 |
783 | } |
784 | } |
785 | |
786 | /* |
787 | * Now we have an enormous linked list of every line of text in |
788 | * the document. Break it up into pages. |
789 | */ |
c6536773 |
790 | pages = page_breaks(firstline, lastline, conf->page_height, 0, 0); |
43341922 |
791 | |
792 | /* |
2bfd1b76 |
793 | * Number the pages. |
794 | */ |
795 | { |
c6536773 |
796 | char buf[40]; |
2bfd1b76 |
797 | page_data *page; |
c6536773 |
798 | |
799 | pagenum = 0; |
800 | |
2bfd1b76 |
801 | for (page = pages; page; page = page->next) { |
c6536773 |
802 | sprintf(buf, "%d", ++pagenum); |
e4ea58f8 |
803 | page->number = ufroma_dup(buf, CS_ASCII); |
2bfd1b76 |
804 | } |
c6536773 |
805 | |
806 | if (has_index) { |
f1530049 |
807 | first_index_page = snew(page_data); |
c6536773 |
808 | first_index_page->next = first_index_page->prev = NULL; |
809 | first_index_page->first_line = NULL; |
810 | first_index_page->last_line = NULL; |
811 | first_index_page->first_text = first_index_page->last_text = NULL; |
812 | first_index_page->first_xref = first_index_page->last_xref = NULL; |
813 | first_index_page->first_rect = first_index_page->last_rect = NULL; |
814 | |
815 | /* And don't forget the as-yet-uncreated index. */ |
816 | sprintf(buf, "%d", ++pagenum); |
e4ea58f8 |
817 | first_index_page->number = ufroma_dup(buf, CS_ASCII); |
c6536773 |
818 | } |
2bfd1b76 |
819 | } |
820 | |
821 | /* |
43341922 |
822 | * Now we're ready to actually lay out the pages. We do this by |
823 | * looping over _paragraphs_, since we may need to track cross- |
824 | * references between lines and even across pages. |
825 | */ |
c6536773 |
826 | for (pdata = firstpara; pdata; pdata = pdata->next) |
827 | render_para(pdata, conf, keywords, idx, |
828 | &index_placeholder_para, first_index_page); |
829 | |
830 | /* |
831 | * Now we've laid out the main body pages, we should have |
832 | * acquired a full set of page numbers for the index. |
833 | */ |
834 | if (has_index) { |
835 | int i; |
836 | indexentry *entry; |
837 | word *index_title; |
838 | para_data *firstidx, *lastidx; |
839 | line_data *firstidxline, *lastidxline, *ldata; |
840 | page_data *ipages, *ipages2, *page; |
a0768d17 |
841 | |
c6536773 |
842 | /* |
843 | * Create a set of paragraphs for the index. |
844 | */ |
f6220253 |
845 | index_title = fake_word(conf->index_text); |
c6536773 |
846 | |
847 | firstidx = make_para_data(para_UnnumberedChapter, 0, 0, 0, |
848 | NULL, NULL, index_title, conf); |
849 | lastidx = firstidx; |
850 | lastidx->next = NULL; |
851 | firstidxline = firstidx->first; |
852 | lastidxline = lastidx->last; |
853 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
854 | paper_idx *pi = (paper_idx *)entry->backend_data; |
855 | para_data *text, *pages; |
856 | |
34d8cc1c |
857 | if (!pi->words) |
858 | continue; |
859 | |
c6536773 |
860 | text = make_para_data(para_Normal, 0, 0, |
861 | conf->base_width - conf->index_colwidth, |
862 | NULL, NULL, entry->text, conf); |
863 | |
864 | pages = make_para_data(para_Normal, 0, 0, |
865 | conf->base_width - conf->index_colwidth, |
866 | NULL, NULL, pi->words, conf); |
867 | |
868 | text->justification = LEFT; |
869 | pages->justification = RIGHT; |
870 | text->last->space_after = pages->first->space_before = |
871 | conf->base_leading / 2; |
872 | |
873 | pages->last->space_after = text->first->space_before = |
874 | conf->base_leading; |
875 | |
876 | assert(text->first); |
877 | assert(pages->first); |
878 | assert(lastidxline); |
879 | assert(lastidx); |
a0768d17 |
880 | |
a0768d17 |
881 | /* |
c6536773 |
882 | * If feasible, fold the two halves of the index entry |
883 | * together. |
a0768d17 |
884 | */ |
c6536773 |
885 | if (text->last->real_shortfall + pages->first->real_shortfall > |
886 | conf->index_colwidth + conf->index_minsep) { |
887 | text->last->space_after = -1; |
888 | pages->first->space_before = -pages->first->line_height+1; |
a0768d17 |
889 | } |
890 | |
c6536773 |
891 | lastidx->next = text; |
892 | text->next = pages; |
893 | pages->next = NULL; |
894 | lastidx = pages; |
895 | |
896 | /* |
897 | * Link all index line structures together into |
898 | * a big list. |
899 | */ |
900 | text->last->next = pages->first; |
901 | pages->first->prev = text->last; |
902 | |
903 | lastidxline->next = text->first; |
904 | text->first->prev = lastidxline; |
905 | |
906 | lastidxline = pages->last; |
907 | |
908 | /* |
909 | * Breaking an index entry anywhere is so bad that I |
910 | * think I'm going to forbid it totally. |
911 | */ |
912 | for (ldata = text->first; ldata && ldata->next; |
913 | ldata = ldata->next) { |
914 | ldata->next->space_before += ldata->space_after + 1; |
915 | ldata->space_after = -1; |
916 | } |
be76d597 |
917 | } |
87bd6353 |
918 | |
be76d597 |
919 | /* |
c6536773 |
920 | * Now break the index into pages. |
2bfd1b76 |
921 | */ |
c6536773 |
922 | ipages = page_breaks(firstidxline, firstidxline, conf->page_height, |
923 | 0, 0); |
924 | ipages2 = page_breaks(firstidxline->next, lastidxline, |
925 | conf->page_height, |
926 | conf->index_cols, |
927 | firstidxline->space_before + |
928 | firstidxline->line_height + |
929 | firstidxline->space_after); |
2bfd1b76 |
930 | |
c6536773 |
931 | /* |
932 | * This will have put each _column_ of the index on a |
933 | * separate page, which isn't what we want. Fold the pages |
934 | * back together. |
935 | */ |
936 | page = ipages2; |
937 | while (page) { |
938 | int i; |
939 | |
940 | for (i = 1; i < conf->index_cols; i++) |
941 | if (page->next) { |
942 | page_data *tpage; |
943 | |
944 | fold_into_page(page, page->next, |
945 | i * (conf->index_colwidth + |
946 | conf->index_gutter)); |
947 | tpage = page->next; |
948 | page->next = page->next->next; |
949 | if (page->next) |
950 | page->next->prev = page; |
951 | sfree(tpage); |
952 | } |
2bfd1b76 |
953 | |
c6536773 |
954 | page = page->next; |
2bfd1b76 |
955 | } |
c6536773 |
956 | /* Also fold the heading on to the same page as the index items. */ |
957 | fold_into_page(ipages, ipages2, 0); |
958 | ipages->next = ipages2->next; |
959 | if (ipages->next) |
960 | ipages->next->prev = ipages; |
961 | sfree(ipages2); |
962 | fold_into_page(first_index_page, ipages, 0); |
963 | first_index_page->next = ipages->next; |
964 | if (first_index_page->next) |
965 | first_index_page->next->prev = first_index_page; |
966 | sfree(ipages); |
967 | ipages = first_index_page; |
2bfd1b76 |
968 | |
969 | /* |
c6536773 |
970 | * Number the index pages, except the already-numbered |
971 | * first one. |
be76d597 |
972 | */ |
c6536773 |
973 | for (page = ipages->next; page; page = page->next) { |
974 | char buf[40]; |
975 | sprintf(buf, "%d", ++pagenum); |
e4ea58f8 |
976 | page->number = ufroma_dup(buf, CS_ASCII); |
43341922 |
977 | } |
c6536773 |
978 | |
979 | /* |
980 | * Render the index pages. |
981 | */ |
982 | for (pdata = firstidx; pdata; pdata = pdata->next) |
983 | render_para(pdata, conf, keywords, idx, |
984 | &index_placeholder_para, first_index_page); |
985 | |
986 | /* |
987 | * Link the index page list on to the end of the main page |
988 | * list. |
989 | */ |
990 | if (!pages) |
991 | pages = ipages; |
992 | else { |
993 | for (page = pages; page->next; page = page->next); |
994 | page->next = ipages; |
995 | } |
996 | |
997 | /* |
998 | * Same with the paragraph list, which will cause the index |
999 | * to be mentioned in the document outline. |
1000 | */ |
1001 | if (!firstpara) |
1002 | firstpara = firstidx; |
1003 | else |
1004 | lastpara->next = firstidx; |
1005 | lastpara = lastidx; |
43341922 |
1006 | } |
1007 | |
f0e51ce1 |
1008 | /* |
9a8dc6b1 |
1009 | * Draw the headers and footers. |
1010 | * |
1011 | * FIXME: this should be fully configurable, but for the moment |
1012 | * I'm just going to put in page numbers in the centre of a |
1013 | * footer and leave it at that. |
1014 | */ |
1015 | { |
1016 | page_data *page; |
1017 | |
1018 | for (page = pages; page; page = page->next) { |
1019 | int width; |
1020 | |
1021 | width = conf->pagenum_fontsize * |
c419cb97 |
1022 | string_width(conf->fbase.fonts[FONT_NORMAL], page->number, |
b5232689 |
1023 | NULL, 0); |
9a8dc6b1 |
1024 | |
c419cb97 |
1025 | render_string(page, conf->fbase.fonts[FONT_NORMAL], |
1026 | conf->pagenum_fontsize, |
9a8dc6b1 |
1027 | conf->left_margin + (conf->base_width - width)/2, |
1028 | conf->bottom_margin - conf->footer_distance, |
b5232689 |
1029 | page->number, 0); |
9a8dc6b1 |
1030 | } |
1031 | } |
1032 | |
1033 | /* |
f0e51ce1 |
1034 | * Start putting together the overall document structure we're |
1035 | * going to return. |
1036 | */ |
f1530049 |
1037 | doc = snew(document); |
43341922 |
1038 | doc->fonts = fontlist; |
1039 | doc->pages = pages; |
be76d597 |
1040 | doc->paper_width = conf->paper_width; |
1041 | doc->paper_height = conf->paper_height; |
f0e51ce1 |
1042 | |
1043 | /* |
1044 | * Collect the section heading paragraphs into a document |
1045 | * outline. This is slightly fiddly because the Title paragraph |
1046 | * isn't required to be at the start, although all the others |
1047 | * must be in order. |
1048 | */ |
1049 | { |
1050 | int osize = 20; |
1051 | |
f1530049 |
1052 | doc->outline_elements = snewn(osize, outline_element); |
f0e51ce1 |
1053 | doc->n_outline_elements = 0; |
1054 | |
1055 | /* First find the title. */ |
be76d597 |
1056 | for (pdata = firstpara; pdata; pdata = pdata->next) { |
1057 | if (pdata->outline_level == 0) { |
f0e51ce1 |
1058 | doc->outline_elements[0].level = 0; |
be76d597 |
1059 | doc->outline_elements[0].pdata = pdata; |
f0e51ce1 |
1060 | doc->n_outline_elements++; |
1061 | break; |
1062 | } |
1063 | } |
1064 | |
1065 | /* Then collect the rest. */ |
be76d597 |
1066 | for (pdata = firstpara; pdata; pdata = pdata->next) { |
1067 | if (pdata->outline_level > 0) { |
f0e51ce1 |
1068 | if (doc->n_outline_elements >= osize) { |
1069 | osize += 20; |
1070 | doc->outline_elements = |
f1530049 |
1071 | sresize(doc->outline_elements, osize, outline_element); |
f0e51ce1 |
1072 | } |
1073 | |
be76d597 |
1074 | doc->outline_elements[doc->n_outline_elements].level = |
1075 | pdata->outline_level; |
1076 | doc->outline_elements[doc->n_outline_elements].pdata = pdata; |
f0e51ce1 |
1077 | doc->n_outline_elements++; |
f0e51ce1 |
1078 | } |
1079 | } |
1080 | } |
1081 | |
43341922 |
1082 | return doc; |
1083 | } |
1084 | |
c419cb97 |
1085 | static void setfont(para_data *p, font_cfg *f) { |
1086 | int i; |
1087 | |
1088 | for (i = 0; i < NFONTS; i++) { |
1089 | p->fonts[i] = f->fonts[i]; |
1090 | p->sizes[i] = f->font_size; |
1091 | } |
1092 | } |
1093 | |
2bfd1b76 |
1094 | static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, |
be76d597 |
1095 | word *pkwtext, word *pkwtext2, word *pwords, |
1096 | paper_conf *conf) |
1097 | { |
1098 | para_data *pdata; |
1099 | line_data *ldata; |
1100 | int extra_indent, firstline_indent, aux_indent; |
1101 | word *aux, *aux2; |
1102 | |
f1530049 |
1103 | pdata = snew(para_data); |
be76d597 |
1104 | pdata->outline_level = -1; |
1105 | pdata->outline_title = NULL; |
1106 | pdata->rect_type = RECT_NONE; |
2bfd1b76 |
1107 | pdata->contents_entry = NULL; |
c6536773 |
1108 | pdata->justification = JUST; |
be76d597 |
1109 | |
1110 | /* |
1111 | * Choose fonts for this paragraph. |
be76d597 |
1112 | */ |
1113 | switch (ptype) { |
1114 | case para_Title: |
c419cb97 |
1115 | setfont(pdata, &conf->ftitle); |
be76d597 |
1116 | pdata->outline_level = 0; |
1117 | break; |
1118 | |
1119 | case para_Chapter: |
1120 | case para_Appendix: |
1121 | case para_UnnumberedChapter: |
c419cb97 |
1122 | setfont(pdata, &conf->fchapter); |
be76d597 |
1123 | pdata->outline_level = 1; |
1124 | break; |
1125 | |
1126 | case para_Heading: |
1127 | case para_Subsect: |
c419cb97 |
1128 | setfont(pdata, |
1129 | &conf->fsect[paux >= conf->nfsect ? conf->nfsect - 1 : paux]); |
be76d597 |
1130 | pdata->outline_level = 2 + paux; |
1131 | break; |
1132 | |
1133 | case para_Normal: |
1134 | case para_BiblioCited: |
1135 | case para_Bullet: |
1136 | case para_NumberedList: |
1137 | case para_DescribedThing: |
1138 | case para_Description: |
1139 | case para_Copyright: |
c419cb97 |
1140 | setfont(pdata, &conf->fbase); |
be76d597 |
1141 | break; |
1142 | } |
1143 | |
1144 | /* |
1145 | * Also select an indentation level depending on the |
1146 | * paragraph type (list paragraphs other than |
1147 | * para_DescribedThing need extra indent). |
1148 | * |
1149 | * (FIXME: Perhaps at some point we might even arrange |
1150 | * for the user to be able to request indented first |
1151 | * lines in paragraphs.) |
1152 | */ |
1153 | if (ptype == para_Bullet || |
1154 | ptype == para_NumberedList || |
1155 | ptype == para_Description) { |
1156 | extra_indent = firstline_indent = conf->indent_list; |
1157 | } else { |
1158 | extra_indent = firstline_indent = 0; |
1159 | } |
1160 | |
1161 | /* |
1162 | * Find the auxiliary text for this paragraph. |
1163 | */ |
1164 | aux = aux2 = NULL; |
1165 | aux_indent = 0; |
1166 | |
1167 | switch (ptype) { |
1168 | case para_Chapter: |
1169 | case para_Appendix: |
1170 | case para_Heading: |
1171 | case para_Subsect: |
1172 | /* |
1173 | * For some heading styles (FIXME: be able to |
1174 | * configure which), the auxiliary text contains |
1175 | * the chapter number and is arranged to be |
1176 | * right-aligned a few points left of the primary |
1177 | * margin. For other styles, the auxiliary text is |
1178 | * the full chapter _name_ and takes up space |
1179 | * within the (wrapped) chapter title, meaning that |
1180 | * we must move the first line indent over to make |
1181 | * space for it. |
1182 | */ |
1183 | if (ptype == para_Heading || ptype == para_Subsect) { |
1184 | int len; |
1185 | |
1186 | aux = pkwtext2; |
dd567011 |
1187 | len = paper_width_simple(pdata, pkwtext2, conf); |
be76d597 |
1188 | aux_indent = -len - conf->sect_num_left_space; |
1189 | |
1190 | pdata->outline_title = |
1191 | prepare_outline_title(pkwtext2, L" ", pwords); |
1192 | } else { |
1193 | aux = pkwtext; |
2bfd1b76 |
1194 | aux2 = fake_word(L": "); |
be76d597 |
1195 | aux_indent = 0; |
1196 | |
dd567011 |
1197 | firstline_indent += paper_width_simple(pdata, aux, conf); |
1198 | firstline_indent += paper_width_simple(pdata, aux2, conf); |
be76d597 |
1199 | |
1200 | pdata->outline_title = |
1201 | prepare_outline_title(pkwtext, L": ", pwords); |
1202 | } |
1203 | break; |
1204 | |
1205 | case para_Bullet: |
1206 | /* |
dd567011 |
1207 | * Auxiliary text consisting of a bullet. |
be76d597 |
1208 | */ |
dd567011 |
1209 | aux = fake_word(conf->bullet); |
be76d597 |
1210 | aux_indent = indent + conf->indent_list_bullet; |
1211 | break; |
1212 | |
1213 | case para_NumberedList: |
1214 | /* |
1215 | * Auxiliary text consisting of the number followed |
1216 | * by a (FIXME: configurable) full stop. |
1217 | */ |
1218 | aux = pkwtext; |
2bfd1b76 |
1219 | aux2 = fake_word(L"."); |
be76d597 |
1220 | aux_indent = indent + conf->indent_list_bullet; |
1221 | break; |
1222 | |
1223 | case para_BiblioCited: |
1224 | /* |
1225 | * Auxiliary text consisting of the bibliography |
1226 | * reference text, and a trailing space. |
1227 | */ |
1228 | aux = pkwtext; |
2bfd1b76 |
1229 | aux2 = fake_word(L" "); |
be76d597 |
1230 | aux_indent = indent; |
dd567011 |
1231 | firstline_indent += paper_width_simple(pdata, aux, conf); |
1232 | firstline_indent += paper_width_simple(pdata, aux2, conf); |
be76d597 |
1233 | break; |
1234 | } |
1235 | |
1236 | if (pdata->outline_level >= 0 && !pdata->outline_title) { |
1237 | pdata->outline_title = |
1238 | prepare_outline_title(NULL, NULL, pwords); |
1239 | } |
1240 | |
2bfd1b76 |
1241 | wrap_paragraph(pdata, pwords, conf->base_width - rmargin, |
be76d597 |
1242 | indent + firstline_indent, |
dd567011 |
1243 | indent + extra_indent, conf); |
be76d597 |
1244 | |
1245 | pdata->first->aux_text = aux; |
1246 | pdata->first->aux_text_2 = aux2; |
1247 | pdata->first->aux_left_indent = aux_indent; |
1248 | |
1249 | /* |
1250 | * Line breaking penalties. |
1251 | */ |
1252 | switch (ptype) { |
1253 | case para_Chapter: |
1254 | case para_Appendix: |
1255 | case para_Heading: |
1256 | case para_Subsect: |
1257 | case para_UnnumberedChapter: |
1258 | /* |
1259 | * Fixed and large penalty for breaking straight |
1260 | * after a heading; corresponding bonus for |
1261 | * breaking straight before. |
1262 | */ |
1263 | pdata->first->penalty_before = -500000; |
1264 | pdata->last->penalty_after = 500000; |
1265 | for (ldata = pdata->first; ldata; ldata = ldata->next) |
1266 | ldata->penalty_after = 500000; |
1267 | break; |
1268 | |
1269 | case para_DescribedThing: |
1270 | /* |
1271 | * This is treated a bit like a small heading: |
1272 | * there's a penalty for breaking after it (i.e. |
1273 | * between it and its description), and a bonus for |
1274 | * breaking before it (actually _between_ list |
1275 | * items). |
1276 | */ |
1277 | pdata->first->penalty_before = -200000; |
1278 | pdata->last->penalty_after = 200000; |
1279 | break; |
1280 | |
1281 | default: |
1282 | /* |
1283 | * Most paragraph types: widow/orphan control by |
1284 | * discouraging breaking one line from the end of |
1285 | * any paragraph. |
1286 | */ |
1287 | if (pdata->first != pdata->last) { |
1288 | pdata->first->penalty_after = 100000; |
1289 | pdata->last->penalty_before = 100000; |
1290 | } |
1291 | break; |
1292 | } |
1293 | |
1294 | standard_line_spacing(pdata, conf); |
1295 | |
1296 | /* |
1297 | * Some kinds of section heading require a page break before |
1298 | * them and an underline after. |
1299 | */ |
1300 | if (ptype == para_Title || |
1301 | ptype == para_Chapter || |
1302 | ptype == para_Appendix || |
1303 | ptype == para_UnnumberedChapter) { |
1304 | pdata->first->page_break = TRUE; |
1305 | pdata->first->space_before = conf->chapter_top_space; |
1306 | pdata->last->space_after += |
1307 | (conf->chapter_underline_depth + |
1308 | conf->chapter_underline_thickness); |
1309 | pdata->rect_type = RECT_CHAPTER_UNDERLINE; |
1310 | } |
1311 | |
1312 | return pdata; |
1313 | } |
1314 | |
1315 | static void standard_line_spacing(para_data *pdata, paper_conf *conf) |
1316 | { |
1317 | line_data *ldata; |
1318 | |
1319 | /* |
1320 | * Set the line spacing for each line in this paragraph. |
1321 | */ |
1322 | for (ldata = pdata->first; ldata; ldata = ldata->next) { |
1323 | if (ldata == pdata->first) |
1324 | ldata->space_before = conf->base_para_spacing / 2; |
1325 | else |
1326 | ldata->space_before = conf->base_leading / 2; |
1327 | if (ldata == pdata->last) |
1328 | ldata->space_after = conf->base_para_spacing / 2; |
1329 | else |
1330 | ldata->space_after = conf->base_leading / 2; |
1331 | ldata->page_break = FALSE; |
1332 | } |
1333 | } |
1334 | |
43341922 |
1335 | static font_encoding *new_font_encoding(font_data *font) |
1336 | { |
1337 | font_encoding *fe; |
1338 | int i; |
1339 | |
f1530049 |
1340 | fe = snew(font_encoding); |
43341922 |
1341 | fe->next = NULL; |
1342 | |
1343 | if (font->list->tail) |
1344 | font->list->tail->next = fe; |
1345 | else |
1346 | font->list->head = fe; |
1347 | font->list->tail = fe; |
1348 | |
1349 | fe->font = font; |
1350 | fe->free_pos = 0x21; |
1351 | |
1352 | for (i = 0; i < 256; i++) { |
d5bc1c48 |
1353 | fe->vector[i] = NOGLYPH; |
43341922 |
1354 | fe->to_unicode[i] = 0xFFFF; |
1355 | } |
1356 | |
1357 | return fe; |
1358 | } |
1359 | |
d5bc1c48 |
1360 | static subfont_map_entry *encode_glyph_at(glyph g, wchar_t u, |
1361 | font_encoding *fe, int pos) |
1362 | { |
1363 | subfont_map_entry *sme = snew(subfont_map_entry); |
1364 | |
1365 | sme->subfont = fe; |
1366 | sme->position = pos; |
1367 | fe->vector[pos] = g; |
1368 | fe->to_unicode[pos] = u; |
1369 | add234(fe->font->subfont_map, sme); |
1370 | return sme; |
1371 | } |
1372 | |
1373 | static int new_sfmap_cmp(void *a, void *b) |
1374 | { |
1375 | glyph ga = *(glyph *)a; |
1376 | subfont_map_entry *sb = b; |
1377 | glyph gb = sb->subfont->vector[sb->position]; |
1378 | |
1379 | if (ga < gb) return -1; |
1380 | if (ga > gb) return 1; |
1381 | return 0; |
1382 | } |
1383 | |
1384 | static subfont_map_entry *encode_glyph(glyph g, wchar_t u, font_data *font) |
1385 | { |
1386 | subfont_map_entry *sme; |
1387 | int c; |
1388 | |
1389 | sme = find234(font->subfont_map, &g, new_sfmap_cmp); |
1390 | if (sme) return sme; |
1391 | |
1392 | /* |
1393 | * This character is not yet in a subfont. Assign one. |
1394 | */ |
1395 | if (font->latest_subfont->free_pos >= 0x100) |
1396 | font->latest_subfont = new_font_encoding(font); |
1397 | |
1398 | c = font->latest_subfont->free_pos++; |
1399 | if (font->latest_subfont->free_pos == 0x7F) |
1400 | font->latest_subfont->free_pos = 0xA1; |
1401 | |
1402 | return encode_glyph_at(g, u, font->latest_subfont, c); |
1403 | } |
1404 | |
1405 | static int sfmap_cmp(void *a, void *b) |
1406 | { |
1407 | subfont_map_entry *sa = a, *sb = b; |
1408 | glyph ga = sa->subfont->vector[sa->position]; |
1409 | glyph gb = sb->subfont->vector[sb->position]; |
1410 | |
1411 | if (ga < gb) return -1; |
1412 | if (ga > gb) return 1; |
1413 | return 0; |
1414 | } |
1415 | |
1416 | int width_cmp(void *a, void *b) |
1417 | { |
1418 | glyph_width const *wa = a, *wb = b; |
1419 | |
1420 | if (wa->glyph < wb->glyph) |
1421 | return -1; |
1422 | if (wa->glyph > wb->glyph) |
1423 | return 1; |
1424 | return 0; |
1425 | } |
1426 | |
ba0fe3ec |
1427 | int kern_cmp(void *a, void *b) |
9db47bc3 |
1428 | { |
1429 | kern_pair const *ka = a, *kb = b; |
1430 | |
1431 | if (ka->left < kb->left) |
1432 | return -1; |
1433 | if (ka->left > kb->left) |
1434 | return 1; |
1435 | if (ka->right < kb->right) |
1436 | return -1; |
1437 | if (ka->right > kb->right) |
1438 | return 1; |
1439 | return 0; |
1440 | } |
1441 | |
b5232689 |
1442 | int lig_cmp(void *a, void *b) |
1443 | { |
1444 | ligature const *la = a, *lb = b; |
1445 | |
1446 | if (la->left < lb->left) |
1447 | return -1; |
1448 | if (la->left > lb->left) |
1449 | return 1; |
1450 | if (la->right < lb->right) |
1451 | return -1; |
1452 | if (la->right > lb->right) |
1453 | return 1; |
1454 | return 0; |
1455 | } |
1456 | |
d5bc1c48 |
1457 | static int utoglyph(font_info const *fi, wchar_t u) { |
1458 | return (u < 0 || u > 0xFFFF ? NOGLYPH : fi->bmp[u]); |
ba0fe3ec |
1459 | } |
1460 | |
43341922 |
1461 | static font_data *make_std_font(font_list *fontlist, char const *name) |
1462 | { |
ba0fe3ec |
1463 | font_info const *fi; |
43341922 |
1464 | font_data *f; |
1465 | font_encoding *fe; |
1466 | int i; |
1467 | |
ae613369 |
1468 | for (fe = fontlist->head; fe; fe = fe->next) |
ba0fe3ec |
1469 | if (strcmp(fe->font->info->name, name) == 0) |
ae613369 |
1470 | return fe->font; |
1471 | |
ba0fe3ec |
1472 | for (fi = all_fonts; fi; fi = fi->next) |
1473 | if (strcmp(fi->name, name) == 0) break; |
1474 | if (!fi) return NULL; |
43341922 |
1475 | |
f1530049 |
1476 | f = snew(font_data); |
43341922 |
1477 | |
1478 | f->list = fontlist; |
ba0fe3ec |
1479 | f->info = fi; |
d5bc1c48 |
1480 | f->subfont_map = newtree234(sfmap_cmp); |
43341922 |
1481 | |
1482 | /* |
1483 | * Our first subfont will contain all of US-ASCII. This isn't |
1484 | * really necessary - we could just create custom subfonts |
1485 | * precisely as the whim of render_string dictated - but |
1486 | * instinct suggests that it might be nice to have the text in |
1487 | * the output files look _marginally_ recognisable. |
1488 | */ |
1489 | fe = new_font_encoding(f); |
1490 | fe->free_pos = 0xA1; /* only the top half is free */ |
1491 | f->latest_subfont = fe; |
1492 | |
d5bc1c48 |
1493 | for (i = 0x21; i <= 0x7E; i++) { |
1494 | glyph g = utoglyph(fi, i); |
1495 | if (g != NOGLYPH) |
1496 | encode_glyph_at(g, i, fe, i); |
43341922 |
1497 | } |
1498 | |
1499 | return f; |
1500 | } |
1501 | |
9db47bc3 |
1502 | /* NB: arguments are glyph numbers from font->bmp. */ |
d5bc1c48 |
1503 | int find_width(font_data *font, glyph index) |
1504 | { |
1505 | glyph_width wantw; |
1506 | glyph_width const *w; |
1507 | |
1508 | wantw.glyph = index; |
1509 | w = find234(font->info->widths, &wantw, NULL); |
1510 | if (!w) return 0; |
1511 | return w->width; |
1512 | } |
1513 | |
9db47bc3 |
1514 | static int find_kern(font_data *font, int lindex, int rindex) |
1515 | { |
1516 | kern_pair wantkp; |
1517 | kern_pair const *kp; |
1518 | |
d5bc1c48 |
1519 | if (lindex == NOGLYPH || rindex == NOGLYPH) |
9db47bc3 |
1520 | return 0; |
1521 | wantkp.left = lindex; |
1522 | wantkp.right = rindex; |
ba0fe3ec |
1523 | kp = find234(font->info->kerns, &wantkp, NULL); |
9db47bc3 |
1524 | if (kp == NULL) |
1525 | return 0; |
1526 | return kp->kern; |
1527 | } |
1528 | |
b5232689 |
1529 | static int find_lig(font_data *font, int lindex, int rindex) |
1530 | { |
1531 | ligature wantlig; |
1532 | ligature const *lig; |
1533 | |
d5bc1c48 |
1534 | if (lindex == NOGLYPH || rindex == NOGLYPH) |
1535 | return NOGLYPH; |
b5232689 |
1536 | wantlig.left = lindex; |
1537 | wantlig.right = rindex; |
1538 | lig = find234(font->info->ligs, &wantlig, NULL); |
1539 | if (lig == NULL) |
d5bc1c48 |
1540 | return NOGLYPH; |
b5232689 |
1541 | return lig->lig; |
1542 | } |
1543 | |
b5232689 |
1544 | static int string_width(font_data *font, wchar_t const *string, int *errs, |
1545 | unsigned flags) |
43341922 |
1546 | { |
1547 | int width = 0; |
b5232689 |
1548 | int nindex, index, oindex, lindex; |
43341922 |
1549 | |
1550 | if (errs) |
1551 | *errs = 0; |
1552 | |
d5bc1c48 |
1553 | oindex = NOGLYPH; |
b5232689 |
1554 | index = utoglyph(font->info, *string); |
43341922 |
1555 | for (; *string; string++) { |
b5232689 |
1556 | nindex = utoglyph(font->info, string[1]); |
5333269a |
1557 | |
d5bc1c48 |
1558 | if (index == NOGLYPH) { |
43341922 |
1559 | if (errs) |
1560 | *errs = 1; |
1561 | } else { |
b5232689 |
1562 | if (!(flags & RS_NOLIG) && |
d5bc1c48 |
1563 | (lindex = find_lig(font, index, nindex)) != NOGLYPH) { |
b5232689 |
1564 | index = lindex; |
1565 | continue; |
1566 | } |
d5bc1c48 |
1567 | width += find_kern(font, oindex, index) + find_width(font, index); |
43341922 |
1568 | } |
9db47bc3 |
1569 | oindex = index; |
b5232689 |
1570 | index = nindex; |
43341922 |
1571 | } |
1572 | |
1573 | return width; |
1574 | } |
1575 | |
faad4952 |
1576 | static int paper_width_internal(void *vctx, word *word, int *nspaces); |
43341922 |
1577 | |
1578 | struct paper_width_ctx { |
1579 | int minspacewidth; |
1580 | para_data *pdata; |
dd567011 |
1581 | paper_conf *conf; |
43341922 |
1582 | }; |
1583 | |
faad4952 |
1584 | static int paper_width_list(void *vctx, word *text, word *end, int *nspaces) { |
43341922 |
1585 | int w = 0; |
faad4952 |
1586 | while (text && text != end) { |
1587 | w += paper_width_internal(vctx, text, nspaces); |
43341922 |
1588 | text = text->next; |
1589 | } |
1590 | return w; |
1591 | } |
1592 | |
faad4952 |
1593 | static int paper_width_internal(void *vctx, word *word, int *nspaces) |
43341922 |
1594 | { |
1595 | struct paper_width_ctx *ctx = (struct paper_width_ctx *)vctx; |
1596 | int style, type, findex, width, errs; |
1597 | wchar_t *str; |
b5232689 |
1598 | unsigned flags = 0; |
43341922 |
1599 | |
1600 | switch (word->type) { |
1601 | case word_HyperLink: |
1602 | case word_HyperEnd: |
1603 | case word_UpperXref: |
1604 | case word_LowerXref: |
3f3d1acc |
1605 | case word_PageXref: |
43341922 |
1606 | case word_XrefEnd: |
1607 | case word_IndexRef: |
1608 | return 0; |
1609 | } |
1610 | |
1611 | style = towordstyle(word->type); |
1612 | type = removeattr(word->type); |
1613 | |
1614 | findex = (style == word_Normal ? FONT_NORMAL : |
1615 | style == word_Emph ? FONT_EMPH : |
1616 | FONT_CODE); |
1617 | |
b5232689 |
1618 | if (style == word_Code || style == word_WeakCode) flags |= RS_NOLIG; |
1619 | |
43341922 |
1620 | if (type == word_Normal) { |
1621 | str = word->text; |
1622 | } else if (type == word_WhiteSpace) { |
faad4952 |
1623 | if (findex != FONT_CODE) { |
1624 | if (nspaces) |
1625 | (*nspaces)++; |
43341922 |
1626 | return ctx->minspacewidth; |
faad4952 |
1627 | } else |
43341922 |
1628 | str = L" "; |
1629 | } else /* if (type == word_Quote) */ { |
1630 | if (word->aux == quote_Open) |
dd567011 |
1631 | str = ctx->conf->lquote; |
43341922 |
1632 | else |
dd567011 |
1633 | str = ctx->conf->rquote; |
43341922 |
1634 | } |
1635 | |
b5232689 |
1636 | width = string_width(ctx->pdata->fonts[findex], str, &errs, flags); |
43341922 |
1637 | |
1638 | if (errs && word->alt) |
faad4952 |
1639 | return paper_width_list(vctx, word->alt, NULL, nspaces); |
43341922 |
1640 | else |
1641 | return ctx->pdata->sizes[findex] * width; |
1642 | } |
1643 | |
faad4952 |
1644 | static int paper_width(void *vctx, word *word) |
1645 | { |
1646 | return paper_width_internal(vctx, word, NULL); |
1647 | } |
1648 | |
dd567011 |
1649 | static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf) |
515d216b |
1650 | { |
1651 | struct paper_width_ctx ctx; |
1652 | |
1653 | ctx.pdata = pdata; |
1654 | ctx.minspacewidth = |
1655 | (pdata->sizes[FONT_NORMAL] * |
b5232689 |
1656 | string_width(pdata->fonts[FONT_NORMAL], L" ", NULL, 0)); |
dd567011 |
1657 | ctx.conf = conf; |
515d216b |
1658 | |
1659 | return paper_width_list(&ctx, text, NULL, NULL); |
1660 | } |
1661 | |
43341922 |
1662 | static void wrap_paragraph(para_data *pdata, word *words, |
dd567011 |
1663 | int w, int i1, int i2, paper_conf *conf) |
43341922 |
1664 | { |
1665 | wrappedline *wrapping, *p; |
1666 | int spacewidth; |
1667 | struct paper_width_ctx ctx; |
1668 | int line_height; |
1669 | |
1670 | /* |
1671 | * We're going to need to store the line height in every line |
1672 | * structure we generate. |
1673 | */ |
1674 | { |
1675 | int i; |
1676 | line_height = 0; |
1677 | for (i = 0; i < NFONTS; i++) |
1678 | if (line_height < pdata->sizes[i]) |
1679 | line_height = pdata->sizes[i]; |
17c71b41 |
1680 | line_height *= UNITS_PER_PT; |
43341922 |
1681 | } |
1682 | |
1683 | spacewidth = (pdata->sizes[FONT_NORMAL] * |
b5232689 |
1684 | string_width(pdata->fonts[FONT_NORMAL], L" ", NULL, 0)); |
43341922 |
1685 | if (spacewidth == 0) { |
1686 | /* |
1687 | * A font without a space?! Disturbing. I hope this never |
1688 | * comes up, but I'll make a random guess anyway and set my |
1689 | * space width to half the point size. |
1690 | */ |
17c71b41 |
1691 | spacewidth = pdata->sizes[FONT_NORMAL] * UNITS_PER_PT / 2; |
43341922 |
1692 | } |
1693 | |
1694 | /* |
1695 | * I'm going to set the _minimum_ space width to 3/5 of the |
1696 | * standard one, and use the standard one as the optimum. |
1697 | */ |
1698 | ctx.minspacewidth = spacewidth * 3 / 5; |
1699 | ctx.pdata = pdata; |
dd567011 |
1700 | ctx.conf = conf; |
43341922 |
1701 | |
1702 | wrapping = wrap_para(words, w - i1, w - i2, paper_width, &ctx, spacewidth); |
1703 | |
1704 | /* |
1705 | * Having done the wrapping, we now concoct a set of line_data |
1706 | * structures. |
1707 | */ |
1708 | pdata->first = pdata->last = NULL; |
1709 | |
1710 | for (p = wrapping; p; p = p->next) { |
1711 | line_data *ldata; |
1712 | word *wd; |
1713 | int len, wid, spaces; |
1714 | |
f1530049 |
1715 | ldata = snew(line_data); |
43341922 |
1716 | |
1717 | ldata->pdata = pdata; |
1718 | ldata->first = p->begin; |
faad4952 |
1719 | ldata->end = p->end; |
43341922 |
1720 | ldata->line_height = line_height; |
1721 | |
1722 | ldata->xpos = (p == wrapping ? i1 : i2); |
1723 | |
1724 | if (pdata->last) { |
1725 | pdata->last->next = ldata; |
1726 | ldata->prev = pdata->last; |
1727 | } else { |
1728 | pdata->first = ldata; |
1729 | ldata->prev = NULL; |
1730 | } |
1731 | ldata->next = NULL; |
1732 | pdata->last = ldata; |
1733 | |
43341922 |
1734 | spaces = 0; |
faad4952 |
1735 | len = paper_width_list(&ctx, ldata->first, ldata->end, &spaces); |
1736 | wid = (p == wrapping ? w - i1 : w - i2); |
43341922 |
1737 | wd = ldata->first; |
43341922 |
1738 | |
faad4952 |
1739 | ldata->hshortfall = wid - len; |
1740 | ldata->nspaces = spaces; |
1741 | /* |
1742 | * This tells us how much the space width needs to |
1743 | * change from _min_spacewidth. But we want to store |
1744 | * its difference from the _natural_ space width, to |
1745 | * make the text rendering easier. |
1746 | */ |
1747 | ldata->hshortfall += ctx.minspacewidth * spaces; |
1748 | ldata->hshortfall -= spacewidth * spaces; |
c6536773 |
1749 | ldata->real_shortfall = ldata->hshortfall; |
faad4952 |
1750 | /* |
1751 | * Special case: on the last line of a paragraph, we |
1752 | * never stretch spaces. |
1753 | */ |
1754 | if (ldata->hshortfall > 0 && !p->next) |
1755 | ldata->hshortfall = 0; |
43341922 |
1756 | |
1757 | ldata->aux_text = NULL; |
515d216b |
1758 | ldata->aux_text_2 = NULL; |
43341922 |
1759 | ldata->aux_left_indent = 0; |
39a0cfb9 |
1760 | ldata->penalty_before = ldata->penalty_after = 0; |
43341922 |
1761 | } |
1762 | |
1763 | } |
1764 | |
1765 | static page_data *page_breaks(line_data *first, line_data *last, |
c6536773 |
1766 | int page_height, int ncols, int headspace) |
43341922 |
1767 | { |
1768 | line_data *l, *m; |
1769 | page_data *ph, *pt; |
c6536773 |
1770 | int n, n1, this_height; |
43341922 |
1771 | |
1772 | /* |
1773 | * Page breaking is done by a close analogue of the optimal |
1774 | * paragraph wrapping algorithm used by wrap_para(). We work |
1775 | * backwards from the end of the document line by line; for |
1776 | * each line, we contemplate every possible number of lines we |
1777 | * could put on a page starting with that line, determine a |
1778 | * cost function for each one, add it to the pre-computed cost |
1779 | * function for optimally page-breaking everything after that |
1780 | * page, and pick the best option. |
1781 | * |
c6536773 |
1782 | * This is made slightly more complex by the fact that we have |
1783 | * a multi-column index with a heading at the top of the |
1784 | * _first_ page, meaning that the first _ncols_ pages must have |
1785 | * a different length. Hence, we must do the wrapping ncols+1 |
1786 | * times over, hypothetically trying to put every subsequence |
1787 | * on every possible page. |
1788 | * |
43341922 |
1789 | * Since my line_data structures are only used for this |
1790 | * purpose, I might as well just store the algorithm data |
1791 | * directly in them. |
1792 | */ |
1793 | |
1794 | for (l = last; l; l = l->prev) { |
f1530049 |
1795 | l->bestcost = snewn(ncols+1, int); |
1796 | l->vshortfall = snewn(ncols+1, int); |
1797 | l->text = snewn(ncols+1, int); |
1798 | l->space = snewn(ncols+1, int); |
1799 | l->page_last = snewn(ncols+1, line_data *); |
c6536773 |
1800 | |
1801 | for (n = 0; n <= ncols; n++) { |
1802 | int minheight, text = 0, space = 0; |
1803 | int cost; |
1804 | |
1805 | n1 = (n < ncols ? n+1 : ncols); |
1806 | if (n < ncols) |
1807 | this_height = page_height - headspace; |
1808 | else |
1809 | this_height = page_height; |
1810 | |
1811 | l->bestcost[n] = -1; |
1812 | for (m = l; m; m = m->next) { |
1813 | if (m != l && m->page_break) |
1814 | break; /* we've gone as far as we can */ |
1815 | |
1816 | if (m != l) { |
1817 | if (m->prev->space_after > 0) |
1818 | space += m->prev->space_after; |
1819 | else |
1820 | text += m->prev->space_after; |
1821 | } |
1822 | if (m != l || m->page_break) { |
1823 | if (m->space_before > 0) |
1824 | space += m->space_before; |
1825 | else |
1826 | text += m->space_before; |
1827 | } |
1828 | text += m->line_height; |
1829 | minheight = text + space; |
43341922 |
1830 | |
c6536773 |
1831 | if (m != l && minheight > this_height) |
1832 | break; |
43341922 |
1833 | |
c6536773 |
1834 | /* |
1835 | * If the space after this paragraph is _negative_ |
1836 | * (which means the next line is folded on to this |
1837 | * one, which happens in the index), we absolutely |
1838 | * cannot break here. |
1839 | */ |
1840 | if (m->space_after >= 0) { |
43341922 |
1841 | |
c6536773 |
1842 | /* |
1843 | * Compute the cost of this arrangement, as the |
1844 | * square of the amount of wasted space on the |
1845 | * page. Exception: if this is the last page |
1846 | * before a mandatory break or the document |
1847 | * end, we don't penalise a large blank area. |
1848 | */ |
1849 | if (m != last && m->next && !m->next->page_break) |
1850 | { |
1ff614b1 |
1851 | int x = (this_height - minheight) / FUNITS_PER_PT * |
1852 | 4096.0; |
c6536773 |
1853 | int xf; |
1854 | |
1855 | xf = x & 0xFF; |
1856 | x >>= 8; |
1857 | |
1858 | cost = x*x; |
1859 | cost += (x * xf) >> 8; |
1860 | } else |
1861 | cost = 0; |
1862 | |
1863 | if (m != last && m->next && !m->next->page_break) { |
1864 | cost += m->penalty_after; |
1865 | cost += m->next->penalty_before; |
1866 | } |
43341922 |
1867 | |
c6536773 |
1868 | if (m != last && m->next && !m->next->page_break) |
1869 | cost += m->next->bestcost[n1]; |
1870 | if (l->bestcost[n] == -1 || l->bestcost[n] > cost) { |
1871 | /* |
1872 | * This is the best option yet for this |
1873 | * starting point. |
1874 | */ |
1875 | l->bestcost[n] = cost; |
1876 | if (m != last && m->next && !m->next->page_break) |
1877 | l->vshortfall[n] = this_height - minheight; |
1878 | else |
1879 | l->vshortfall[n] = 0; |
1880 | l->text[n] = text; |
1881 | l->space[n] = space; |
1882 | l->page_last[n] = m; |
1883 | } |
1884 | } |
43341922 |
1885 | |
c6536773 |
1886 | if (m == last) |
1887 | break; |
43341922 |
1888 | } |
1889 | } |
1890 | } |
1891 | |
1892 | /* |
1893 | * Now go through the line list forwards and assemble the |
1894 | * actual pages. |
1895 | */ |
1896 | ph = pt = NULL; |
1897 | |
1898 | l = first; |
c6536773 |
1899 | n = 0; |
43341922 |
1900 | while (l) { |
1901 | page_data *page; |
c6536773 |
1902 | int text, space, head; |
43341922 |
1903 | |
f1530049 |
1904 | page = snew(page_data); |
43341922 |
1905 | page->next = NULL; |
1906 | page->prev = pt; |
1907 | if (pt) |
1908 | pt->next = page; |
1909 | else |
1910 | ph = page; |
1911 | pt = page; |
1912 | |
1913 | page->first_line = l; |
c6536773 |
1914 | page->last_line = l->page_last[n]; |
43341922 |
1915 | |
1916 | page->first_text = page->last_text = NULL; |
138d7ffb |
1917 | page->first_xref = page->last_xref = NULL; |
23765aeb |
1918 | page->first_rect = page->last_rect = NULL; |
138d7ffb |
1919 | |
43341922 |
1920 | /* |
1921 | * Now assign a y-coordinate to each line on the page. |
1922 | */ |
1923 | text = space = 0; |
c6536773 |
1924 | head = (n < ncols ? headspace : 0); |
43341922 |
1925 | for (l = page->first_line; l; l = l->next) { |
c6536773 |
1926 | if (l != page->first_line) { |
1927 | if (l->prev->space_after > 0) |
1928 | space += l->prev->space_after; |
1929 | else |
1930 | text += l->prev->space_after; |
1931 | } |
1932 | if (l != page->first_line || l->page_break) { |
1933 | if (l->space_before > 0) |
1934 | space += l->space_before; |
1935 | else |
1936 | text += l->space_before; |
1937 | } |
43341922 |
1938 | text += l->line_height; |
1939 | |
1940 | l->page = page; |
416dfe17 |
1941 | l->ypos = text + space + head; |
1942 | if (page->first_line->space[n]) { |
1943 | l->ypos += space * (float)page->first_line->vshortfall[n] / |
1944 | page->first_line->space[n]; |
1945 | } |
43341922 |
1946 | |
1947 | if (l == page->last_line) |
1948 | break; |
1949 | } |
1950 | |
c6536773 |
1951 | l = page->last_line; |
1952 | if (l == last) |
1953 | break; |
1954 | l = l->next; |
1955 | |
1956 | n = (n < ncols ? n+1 : ncols); |
43341922 |
1957 | } |
1958 | |
1959 | return ph; |
1960 | } |
1961 | |
23765aeb |
1962 | static void add_rect_to_page(page_data *page, int x, int y, int w, int h) |
1963 | { |
f1530049 |
1964 | rect *r = snew(rect); |
23765aeb |
1965 | |
1966 | r->next = NULL; |
1967 | if (page->last_rect) |
1968 | page->last_rect->next = r; |
1969 | else |
1970 | page->first_rect = r; |
1971 | page->last_rect = r; |
1972 | |
1973 | r->x = x; |
1974 | r->y = y; |
1975 | r->w = w; |
1976 | r->h = h; |
1977 | } |
1978 | |
43341922 |
1979 | static void add_string_to_page(page_data *page, int x, int y, |
7c8c4239 |
1980 | font_encoding *fe, int size, char *text, |
1981 | int width) |
43341922 |
1982 | { |
1983 | text_fragment *frag; |
1984 | |
f1530049 |
1985 | frag = snew(text_fragment); |
43341922 |
1986 | frag->next = NULL; |
1987 | |
1988 | if (page->last_text) |
1989 | page->last_text->next = frag; |
1990 | else |
1991 | page->first_text = frag; |
1992 | page->last_text = frag; |
1993 | |
1994 | frag->x = x; |
1995 | frag->y = y; |
1996 | frag->fe = fe; |
1997 | frag->fontsize = size; |
1998 | frag->text = dupstr(text); |
7c8c4239 |
1999 | frag->width = width; |
43341922 |
2000 | } |
2001 | |
2002 | /* |
2003 | * Returns the updated x coordinate. |
2004 | */ |
2005 | static int render_string(page_data *page, font_data *font, int fontsize, |
b5232689 |
2006 | int x, int y, wchar_t *str, unsigned flags) |
43341922 |
2007 | { |
2008 | char *text; |
b5232689 |
2009 | int textpos, textwid, kern, nglyph, glyph, oglyph, lig; |
43341922 |
2010 | font_encoding *subfont = NULL, *sf; |
d5bc1c48 |
2011 | subfont_map_entry *sme; |
43341922 |
2012 | |
f1530049 |
2013 | text = snewn(1 + ustrlen(str), char); |
43341922 |
2014 | textpos = textwid = 0; |
2015 | |
d5bc1c48 |
2016 | glyph = NOGLYPH; |
b5232689 |
2017 | nglyph = utoglyph(font->info, *str); |
43341922 |
2018 | while (*str) { |
9db47bc3 |
2019 | oglyph = glyph; |
b5232689 |
2020 | glyph = nglyph; |
2021 | nglyph = utoglyph(font->info, str[1]); |
43341922 |
2022 | |
d5bc1c48 |
2023 | if (glyph == NOGLYPH) { |
4cc00cdd |
2024 | str++; |
43341922 |
2025 | continue; /* nothing more we can do here */ |
4cc00cdd |
2026 | } |
43341922 |
2027 | |
b5232689 |
2028 | if (!(flags & RS_NOLIG) && |
d5bc1c48 |
2029 | (lig = find_lig(font, glyph, nglyph)) != NOGLYPH) { |
b5232689 |
2030 | nglyph = lig; |
2031 | str++; |
2032 | continue; |
2033 | } |
2034 | |
43341922 |
2035 | /* |
2036 | * Find which subfont this character is going in. |
2037 | */ |
d5bc1c48 |
2038 | sme = encode_glyph(glyph, *str, font); |
2039 | sf = sme->subfont; |
43341922 |
2040 | |
9db47bc3 |
2041 | kern = find_kern(font, oglyph, glyph) * fontsize; |
2042 | |
2043 | if (!subfont || sf != subfont || kern) { |
43341922 |
2044 | if (subfont) { |
2045 | text[textpos] = '\0'; |
7c8c4239 |
2046 | add_string_to_page(page, x, y, subfont, fontsize, text, |
2047 | textwid); |
9db47bc3 |
2048 | x += textwid + kern; |
43341922 |
2049 | } else { |
2050 | assert(textpos == 0); |
2051 | } |
2052 | textpos = 0; |
9db47bc3 |
2053 | textwid = 0; |
43341922 |
2054 | subfont = sf; |
2055 | } |
2056 | |
d5bc1c48 |
2057 | text[textpos++] = sme->position; |
2058 | textwid += find_width(font, glyph) * fontsize; |
43341922 |
2059 | |
2060 | str++; |
2061 | } |
2062 | |
2063 | if (textpos > 0) { |
2064 | text[textpos] = '\0'; |
7c8c4239 |
2065 | add_string_to_page(page, x, y, subfont, fontsize, text, textwid); |
43341922 |
2066 | x += textwid; |
2067 | } |
2068 | |
2069 | return x; |
2070 | } |
2071 | |
2072 | /* |
2073 | * Returns the updated x coordinate. |
2074 | */ |
138d7ffb |
2075 | static int render_text(page_data *page, para_data *pdata, line_data *ldata, |
2076 | int x, int y, word *text, word *text_end, xref **xr, |
2077 | int shortfall, int nspaces, int *nspace, |
dd567011 |
2078 | keywordlist *keywords, indexdata *idx, paper_conf *conf) |
43341922 |
2079 | { |
faad4952 |
2080 | while (text && text != text_end) { |
43341922 |
2081 | int style, type, findex, errs; |
2082 | wchar_t *str; |
138d7ffb |
2083 | xref_dest dest; |
b5232689 |
2084 | unsigned flags = 0; |
43341922 |
2085 | |
2086 | switch (text->type) { |
138d7ffb |
2087 | /* |
2088 | * Start a cross-reference. |
2089 | */ |
43341922 |
2090 | case word_HyperLink: |
43341922 |
2091 | case word_UpperXref: |
2092 | case word_LowerXref: |
3f3d1acc |
2093 | case word_PageXref: |
138d7ffb |
2094 | |
2095 | if (text->type == word_HyperLink) { |
2096 | dest.type = URL; |
e4ea58f8 |
2097 | dest.url = utoa_dup(text->text, CS_ASCII); |
138d7ffb |
2098 | dest.page = NULL; |
3f3d1acc |
2099 | } else if (text->type == word_PageXref) { |
2100 | dest.type = PAGE; |
2101 | dest.url = NULL; |
2102 | dest.page = (page_data *)text->private_data; |
138d7ffb |
2103 | } else { |
2104 | keyword *kwl = kw_lookup(keywords, text->text); |
2105 | para_data *pdata; |
2106 | |
2107 | if (kwl) { |
2108 | assert(kwl->para->private_data); |
2109 | pdata = (para_data *) kwl->para->private_data; |
2110 | dest.type = PAGE; |
2111 | dest.page = pdata->first->page; |
2112 | dest.url = NULL; |
2113 | } else { |
2114 | /* |
2115 | * Shouldn't happen, but *shrug* |
2116 | */ |
2117 | dest.type = NONE; |
2118 | dest.page = NULL; |
2119 | dest.url = NULL; |
2120 | } |
2121 | } |
2122 | if (dest.type != NONE) { |
f1530049 |
2123 | *xr = snew(xref); |
138d7ffb |
2124 | (*xr)->dest = dest; /* structure copy */ |
2125 | if (page->last_xref) |
2126 | page->last_xref->next = *xr; |
2127 | else |
2128 | page->first_xref = *xr; |
2129 | page->last_xref = *xr; |
23765aeb |
2130 | (*xr)->next = NULL; |
138d7ffb |
2131 | |
2132 | /* |
2133 | * FIXME: Ideally we should have, and use, some |
2134 | * vertical font metric information here so that |
2135 | * our cross-ref rectangle can take account of |
2136 | * descenders and the font's cap height. This will |
2137 | * do for the moment, but it isn't ideal. |
2138 | */ |
2139 | (*xr)->lx = (*xr)->rx = x; |
2140 | (*xr)->by = y; |
2141 | (*xr)->ty = y + ldata->line_height; |
2142 | } |
2143 | goto nextword; |
2144 | |
2145 | /* |
2146 | * Finish extending a cross-reference box. |
2147 | */ |
2148 | case word_HyperEnd: |
43341922 |
2149 | case word_XrefEnd: |
138d7ffb |
2150 | *xr = NULL; |
2151 | goto nextword; |
2152 | |
43341922 |
2153 | /* |
c6536773 |
2154 | * Add the current page number to the list of pages |
2155 | * referenced by an index entry. |
43341922 |
2156 | */ |
c6536773 |
2157 | case word_IndexRef: |
34d8cc1c |
2158 | /* |
2159 | * We don't create index references in contents entries. |
2160 | */ |
2161 | if (!pdata->contents_entry) { |
c6536773 |
2162 | indextag *tag; |
2163 | int i; |
2164 | |
2165 | tag = index_findtag(idx, text->text); |
2166 | if (!tag) |
2167 | goto nextword; |
2168 | |
2169 | for (i = 0; i < tag->nrefs; i++) { |
2170 | indexentry *entry = tag->refs[i]; |
2171 | paper_idx *pi = (paper_idx *)entry->backend_data; |
2172 | |
2173 | /* |
2174 | * If the same index term is indexed twice |
2175 | * within the same section, we only want to |
2176 | * mention it once in the index. |
2177 | */ |
2178 | if (pi->lastpage != page) { |
3f3d1acc |
2179 | word **wp; |
2180 | |
c6536773 |
2181 | if (pi->lastword) { |
2182 | pi->lastword = pi->lastword->next = |
2183 | fake_word(L","); |
2184 | pi->lastword = pi->lastword->next = |
2185 | fake_space_word(); |
3f3d1acc |
2186 | wp = &pi->lastword->next; |
2187 | } else |
2188 | wp = &pi->words; |
2189 | |
2190 | pi->lastword = *wp = |
2191 | fake_page_ref(page); |
2192 | pi->lastword = pi->lastword->next = |
2193 | fake_word(page->number); |
2194 | pi->lastword = pi->lastword->next = |
2195 | fake_end_ref(); |
c6536773 |
2196 | } |
2197 | |
2198 | pi->lastpage = page; |
2199 | } |
2200 | } |
2201 | goto nextword; |
43341922 |
2202 | } |
2203 | |
2204 | style = towordstyle(text->type); |
2205 | type = removeattr(text->type); |
2206 | |
2207 | findex = (style == word_Normal ? FONT_NORMAL : |
2208 | style == word_Emph ? FONT_EMPH : |
2209 | FONT_CODE); |
2210 | |
b5232689 |
2211 | if (style == word_Code || style == word_WeakCode) flags |= RS_NOLIG; |
2212 | |
43341922 |
2213 | if (type == word_Normal) { |
2214 | str = text->text; |
2215 | } else if (type == word_WhiteSpace) { |
2216 | x += pdata->sizes[findex] * |
b5232689 |
2217 | string_width(pdata->fonts[findex], L" ", NULL, 0); |
faad4952 |
2218 | if (nspaces && findex != FONT_CODE) { |
2219 | x += (*nspace+1) * shortfall / nspaces; |
2220 | x -= *nspace * shortfall / nspaces; |
2221 | (*nspace)++; |
2222 | } |
43341922 |
2223 | goto nextword; |
2224 | } else /* if (type == word_Quote) */ { |
2225 | if (text->aux == quote_Open) |
dd567011 |
2226 | str = conf->lquote; |
43341922 |
2227 | else |
dd567011 |
2228 | str = conf->rquote; |
43341922 |
2229 | } |
2230 | |
b5232689 |
2231 | (void) string_width(pdata->fonts[findex], str, &errs, flags); |
43341922 |
2232 | |
2233 | if (errs && text->alt) |
138d7ffb |
2234 | x = render_text(page, pdata, ldata, x, y, text->alt, NULL, |
dd567011 |
2235 | xr, shortfall, nspaces, nspace, keywords, idx, |
2236 | conf); |
43341922 |
2237 | else |
2238 | x = render_string(page, pdata->fonts[findex], |
b5232689 |
2239 | pdata->sizes[findex], x, y, str, flags); |
43341922 |
2240 | |
138d7ffb |
2241 | if (*xr) |
2242 | (*xr)->rx = x; |
2243 | |
43341922 |
2244 | nextword: |
43341922 |
2245 | text = text->next; |
2246 | } |
2247 | |
2248 | return x; |
2249 | } |
2250 | |
2bfd1b76 |
2251 | /* |
2252 | * Returns the last x position used on the line. |
2253 | */ |
2254 | static int render_line(line_data *ldata, int left_x, int top_y, |
dd567011 |
2255 | xref_dest *dest, keywordlist *keywords, indexdata *idx, |
2256 | paper_conf *conf) |
43341922 |
2257 | { |
faad4952 |
2258 | int nspace; |
138d7ffb |
2259 | xref *xr; |
2bfd1b76 |
2260 | int ret = 0; |
138d7ffb |
2261 | |
faad4952 |
2262 | if (ldata->aux_text) { |
515d216b |
2263 | int x; |
138d7ffb |
2264 | xr = NULL; |
faad4952 |
2265 | nspace = 0; |
515d216b |
2266 | x = render_text(ldata->page, ldata->pdata, ldata, |
2267 | left_x + ldata->aux_left_indent, |
2268 | top_y - ldata->ypos, |
c6536773 |
2269 | ldata->aux_text, NULL, &xr, 0, 0, &nspace, |
dd567011 |
2270 | keywords, idx, conf); |
515d216b |
2271 | if (ldata->aux_text_2) |
2272 | render_text(ldata->page, ldata->pdata, ldata, |
2273 | x, top_y - ldata->ypos, |
c6536773 |
2274 | ldata->aux_text_2, NULL, &xr, 0, 0, &nspace, |
dd567011 |
2275 | keywords, idx, conf); |
faad4952 |
2276 | } |
2277 | nspace = 0; |
138d7ffb |
2278 | |
87bd6353 |
2279 | if (ldata->first) { |
138d7ffb |
2280 | /* |
87bd6353 |
2281 | * There might be a cross-reference carried over from a |
2282 | * previous line. |
138d7ffb |
2283 | */ |
87bd6353 |
2284 | if (dest->type != NONE) { |
f1530049 |
2285 | xr = snew(xref); |
87bd6353 |
2286 | xr->next = NULL; |
2287 | xr->dest = *dest; /* structure copy */ |
2288 | if (ldata->page->last_xref) |
2289 | ldata->page->last_xref->next = xr; |
2290 | else |
2291 | ldata->page->first_xref = xr; |
2292 | ldata->page->last_xref = xr; |
2293 | xr->lx = xr->rx = left_x + ldata->xpos; |
2294 | xr->by = top_y - ldata->ypos; |
2295 | xr->ty = top_y - ldata->ypos + ldata->line_height; |
2296 | } else |
2297 | xr = NULL; |
2298 | |
c6536773 |
2299 | { |
2300 | int extra_indent, shortfall, spaces; |
2301 | int just = ldata->pdata->justification; |
2302 | |
2303 | /* |
2304 | * All forms of justification become JUST when we have |
2305 | * to squeeze the paragraph. |
2306 | */ |
2307 | if (ldata->hshortfall < 0) |
2308 | just = JUST; |
2309 | |
2310 | switch (just) { |
2311 | case JUST: |
2312 | shortfall = ldata->hshortfall; |
2313 | spaces = ldata->nspaces; |
2314 | extra_indent = 0; |
2315 | break; |
2316 | case LEFT: |
2317 | shortfall = spaces = extra_indent = 0; |
2318 | break; |
2319 | case RIGHT: |
2320 | shortfall = spaces = 0; |
2321 | extra_indent = ldata->real_shortfall; |
2322 | break; |
2323 | } |
2324 | |
2325 | ret = render_text(ldata->page, ldata->pdata, ldata, |
2326 | left_x + ldata->xpos + extra_indent, |
2327 | top_y - ldata->ypos, ldata->first, ldata->end, |
2328 | &xr, shortfall, spaces, &nspace, |
dd567011 |
2329 | keywords, idx, conf); |
c6536773 |
2330 | } |
87bd6353 |
2331 | |
2332 | if (xr) { |
2333 | /* |
2334 | * There's a cross-reference continued on to the next line. |
2335 | */ |
2336 | *dest = xr->dest; |
2337 | } else |
2338 | dest->type = NONE; |
2339 | } |
2bfd1b76 |
2340 | |
2341 | return ret; |
43341922 |
2342 | } |
515d216b |
2343 | |
c6536773 |
2344 | static void render_para(para_data *pdata, paper_conf *conf, |
2345 | keywordlist *keywords, indexdata *idx, |
2346 | paragraph *index_placeholder, page_data *index_page) |
2347 | { |
2348 | int last_x; |
2349 | xref *cxref; |
2350 | page_data *cxref_page; |
2351 | xref_dest dest; |
2352 | para_data *target; |
2353 | line_data *ldata; |
2354 | |
2355 | dest.type = NONE; |
2356 | cxref = NULL; |
2357 | cxref_page = NULL; |
2358 | |
2359 | for (ldata = pdata->first; ldata; ldata = ldata->next) { |
2360 | /* |
2361 | * If this is a contents entry, we expect to have a single |
2362 | * enormous cross-reference rectangle covering the whole |
2363 | * thing. (Unless, of course, it spans multiple pages.) |
2364 | */ |
2365 | if (pdata->contents_entry && ldata->page != cxref_page) { |
2366 | cxref_page = ldata->page; |
f1530049 |
2367 | cxref = snew(xref); |
c6536773 |
2368 | cxref->next = NULL; |
2369 | cxref->dest.type = PAGE; |
2370 | if (pdata->contents_entry == index_placeholder) { |
2371 | cxref->dest.page = index_page; |
2372 | } else { |
2373 | assert(pdata->contents_entry->private_data); |
2374 | target = (para_data *)pdata->contents_entry->private_data; |
2375 | cxref->dest.page = target->first->page; |
2376 | } |
2377 | cxref->dest.url = NULL; |
2378 | if (ldata->page->last_xref) |
2379 | ldata->page->last_xref->next = cxref; |
2380 | else |
2381 | ldata->page->first_xref = cxref; |
2382 | ldata->page->last_xref = cxref; |
2383 | cxref->lx = conf->left_margin; |
2384 | cxref->rx = conf->paper_width - conf->right_margin; |
2385 | cxref->ty = conf->paper_height - conf->top_margin |
2386 | - ldata->ypos + ldata->line_height; |
2387 | } |
2388 | if (pdata->contents_entry) { |
2389 | assert(cxref != NULL); |
2390 | cxref->by = conf->paper_height - conf->top_margin |
2391 | - ldata->ypos; |
2392 | } |
2393 | |
2394 | last_x = render_line(ldata, conf->left_margin, |
2395 | conf->paper_height - conf->top_margin, |
dd567011 |
2396 | &dest, keywords, idx, conf); |
c6536773 |
2397 | if (ldata == pdata->last) |
2398 | break; |
2399 | } |
2400 | |
2401 | /* |
2402 | * If this is a contents entry, add leaders and a page |
2403 | * number. |
2404 | */ |
2405 | if (pdata->contents_entry) { |
2406 | word *w; |
2407 | wchar_t *num; |
2408 | int wid; |
2409 | int x; |
2410 | |
2411 | if (pdata->contents_entry == index_placeholder) { |
2412 | num = index_page->number; |
2413 | } else { |
2414 | assert(pdata->contents_entry->private_data); |
2415 | target = (para_data *)pdata->contents_entry->private_data; |
2416 | num = target->first->page->number; |
2417 | } |
2418 | |
2419 | w = fake_word(num); |
dd567011 |
2420 | wid = paper_width_simple(pdata, w, conf); |
c6536773 |
2421 | sfree(w); |
2422 | |
c6536773 |
2423 | for (x = 0; x < conf->base_width; x += conf->leader_separation) |
2424 | if (x - conf->leader_separation > last_x - conf->left_margin && |
2425 | x + conf->leader_separation < conf->base_width - wid) |
2426 | render_string(pdata->last->page, |
2427 | pdata->fonts[FONT_NORMAL], |
2428 | pdata->sizes[FONT_NORMAL], |
2429 | conf->left_margin + x, |
2430 | (conf->paper_height - conf->top_margin - |
b5232689 |
2431 | pdata->last->ypos), L".", 0); |
adbcaa16 |
2432 | |
2433 | render_string(pdata->last->page, |
2434 | pdata->fonts[FONT_NORMAL], |
2435 | pdata->sizes[FONT_NORMAL], |
2436 | conf->paper_width - conf->right_margin - wid, |
2437 | (conf->paper_height - conf->top_margin - |
b5232689 |
2438 | pdata->last->ypos), num, 0); |
c6536773 |
2439 | } |
2440 | |
2441 | /* |
2442 | * Render any rectangle (chapter title underline or rule) |
2443 | * that goes with this paragraph. |
2444 | */ |
2445 | switch (pdata->rect_type) { |
2446 | case RECT_CHAPTER_UNDERLINE: |
2447 | add_rect_to_page(pdata->last->page, |
2448 | conf->left_margin, |
2449 | (conf->paper_height - conf->top_margin - |
2450 | pdata->last->ypos - |
2451 | conf->chapter_underline_depth), |
2452 | conf->base_width, |
2453 | conf->chapter_underline_thickness); |
2454 | break; |
2455 | case RECT_RULE: |
2456 | add_rect_to_page(pdata->first->page, |
2457 | conf->left_margin + pdata->first->xpos, |
2458 | (conf->paper_height - conf->top_margin - |
2459 | pdata->last->ypos - |
2460 | pdata->last->line_height), |
2461 | conf->base_width - pdata->first->xpos, |
2462 | pdata->last->line_height); |
2463 | break; |
2464 | default: /* placate gcc */ |
2465 | break; |
2466 | } |
2467 | } |
2468 | |
be76d597 |
2469 | static para_data *code_paragraph(int indent, word *words, paper_conf *conf) |
515d216b |
2470 | { |
f1530049 |
2471 | para_data *pdata = snew(para_data); |
be76d597 |
2472 | |
515d216b |
2473 | /* |
2474 | * For code paragraphs, I'm going to hack grievously and |
2475 | * pretend the three normal fonts are the three code paragraph |
2476 | * fonts. |
2477 | */ |
c419cb97 |
2478 | setfont(pdata, &conf->fcode); |
515d216b |
2479 | |
2480 | pdata->first = pdata->last = NULL; |
be76d597 |
2481 | pdata->outline_level = -1; |
2482 | pdata->rect_type = RECT_NONE; |
2bfd1b76 |
2483 | pdata->contents_entry = NULL; |
c6536773 |
2484 | pdata->justification = LEFT; |
515d216b |
2485 | |
2486 | for (; words; words = words->next) { |
2487 | wchar_t *t, *e, *start; |
2488 | word *lhead = NULL, *ltail = NULL, *w; |
2489 | line_data *ldata; |
2490 | int prev = -1, curr; |
2491 | |
2492 | t = words->text; |
2493 | if (words->next && words->next->type == word_Emph) { |
2494 | e = words->next->text; |
2495 | words = words->next; |
2496 | } else |
2497 | e = NULL; |
2498 | |
2499 | start = t; |
2500 | |
2501 | while (*start) { |
2502 | while (*t) { |
2503 | if (!e || !*e) |
2504 | curr = 0; |
2505 | else if (*e == L'i') |
2506 | curr = 1; |
2507 | else if (*e == L'b') |
2508 | curr = 2; |
2509 | else |
2510 | curr = 0; |
2511 | |
2512 | if (prev < 0) |
2513 | prev = curr; |
2514 | |
2515 | if (curr != prev) |
2516 | break; |
2517 | |
2518 | t++; |
2519 | if (e && *e) |
2520 | e++; |
2521 | } |
2522 | |
2523 | /* |
2524 | * We've isolated a maximal subsequence of the line |
2525 | * which has the same emphasis. Form it into a word |
2526 | * structure. |
2527 | */ |
f1530049 |
2528 | w = snew(word); |
515d216b |
2529 | w->next = NULL; |
2530 | w->alt = NULL; |
2531 | w->type = (prev == 0 ? word_WeakCode : |
2532 | prev == 1 ? word_Emph : word_Normal); |
f1530049 |
2533 | w->text = snewn(t-start+1, wchar_t); |
515d216b |
2534 | memcpy(w->text, start, (t-start) * sizeof(wchar_t)); |
2535 | w->text[t-start] = '\0'; |
2536 | w->breaks = FALSE; |
2537 | |
2538 | if (ltail) |
2539 | ltail->next = w; |
2540 | else |
2541 | lhead = w; |
2542 | ltail = w; |
2543 | |
2544 | start = t; |
2545 | prev = -1; |
2546 | } |
2547 | |
f1530049 |
2548 | ldata = snew(line_data); |
515d216b |
2549 | |
2550 | ldata->pdata = pdata; |
2551 | ldata->first = lhead; |
2552 | ldata->end = NULL; |
c419cb97 |
2553 | ldata->line_height = conf->fcode.font_size * UNITS_PER_PT; |
515d216b |
2554 | |
2555 | ldata->xpos = indent; |
2556 | |
2557 | if (pdata->last) { |
2558 | pdata->last->next = ldata; |
2559 | ldata->prev = pdata->last; |
2560 | } else { |
2561 | pdata->first = ldata; |
2562 | ldata->prev = NULL; |
2563 | } |
2564 | ldata->next = NULL; |
2565 | pdata->last = ldata; |
2566 | |
2567 | ldata->hshortfall = 0; |
2568 | ldata->nspaces = 0; |
2569 | ldata->aux_text = NULL; |
2570 | ldata->aux_text_2 = NULL; |
2571 | ldata->aux_left_indent = 0; |
39a0cfb9 |
2572 | /* General opprobrium for breaking in a code paragraph. */ |
2573 | ldata->penalty_before = ldata->penalty_after = 50000; |
515d216b |
2574 | } |
be76d597 |
2575 | |
2576 | standard_line_spacing(pdata, conf); |
2577 | |
2578 | return pdata; |
515d216b |
2579 | } |
87bd6353 |
2580 | |
be76d597 |
2581 | static para_data *rule_paragraph(int indent, paper_conf *conf) |
87bd6353 |
2582 | { |
f1530049 |
2583 | para_data *pdata = snew(para_data); |
87bd6353 |
2584 | line_data *ldata; |
2585 | |
f1530049 |
2586 | ldata = snew(line_data); |
87bd6353 |
2587 | |
2588 | ldata->pdata = pdata; |
2589 | ldata->first = NULL; |
2590 | ldata->end = NULL; |
be76d597 |
2591 | ldata->line_height = conf->rule_thickness; |
87bd6353 |
2592 | |
2593 | ldata->xpos = indent; |
2594 | |
2595 | ldata->prev = NULL; |
2596 | ldata->next = NULL; |
2597 | |
2598 | ldata->hshortfall = 0; |
2599 | ldata->nspaces = 0; |
2600 | ldata->aux_text = NULL; |
2601 | ldata->aux_text_2 = NULL; |
2602 | ldata->aux_left_indent = 0; |
2603 | |
2604 | /* |
2605 | * Better to break after a rule than before it |
2606 | */ |
2607 | ldata->penalty_after += 100000; |
2608 | ldata->penalty_before += -100000; |
2609 | |
2610 | pdata->first = pdata->last = ldata; |
be76d597 |
2611 | pdata->outline_level = -1; |
2612 | pdata->rect_type = RECT_RULE; |
2bfd1b76 |
2613 | pdata->contents_entry = NULL; |
c6536773 |
2614 | pdata->justification = LEFT; |
be76d597 |
2615 | |
2616 | standard_line_spacing(pdata, conf); |
2617 | |
2618 | return pdata; |
2619 | } |
2620 | |
2621 | /* |
2622 | * Plain-text-like formatting for outline titles. |
2623 | */ |
2624 | static void paper_rdaddw(rdstring *rs, word *text) { |
2625 | for (; text; text = text->next) switch (text->type) { |
2626 | case word_HyperLink: |
2627 | case word_HyperEnd: |
2628 | case word_UpperXref: |
2629 | case word_LowerXref: |
2630 | case word_XrefEnd: |
2631 | case word_IndexRef: |
2632 | break; |
2633 | |
2634 | case word_Normal: |
2635 | case word_Emph: |
2636 | case word_Code: |
2637 | case word_WeakCode: |
2638 | case word_WhiteSpace: |
2639 | case word_EmphSpace: |
2640 | case word_CodeSpace: |
2641 | case word_WkCodeSpace: |
2642 | case word_Quote: |
2643 | case word_EmphQuote: |
2644 | case word_CodeQuote: |
2645 | case word_WkCodeQuote: |
2646 | assert(text->type != word_CodeQuote && |
2647 | text->type != word_WkCodeQuote); |
2648 | if (towordstyle(text->type) == word_Emph && |
2649 | (attraux(text->aux) == attr_First || |
2650 | attraux(text->aux) == attr_Only)) |
2651 | rdadd(rs, L'_'); /* FIXME: configurability */ |
2652 | else if (towordstyle(text->type) == word_Code && |
2653 | (attraux(text->aux) == attr_First || |
2654 | attraux(text->aux) == attr_Only)) |
2655 | rdadd(rs, L'\''); /* FIXME: configurability */ |
2656 | if (removeattr(text->type) == word_Normal) { |
2657 | rdadds(rs, text->text); |
2658 | } else if (removeattr(text->type) == word_WhiteSpace) { |
2659 | rdadd(rs, L' '); |
2660 | } else if (removeattr(text->type) == word_Quote) { |
2661 | rdadd(rs, L'\''); /* fixme: configurability */ |
2662 | } |
2663 | if (towordstyle(text->type) == word_Emph && |
2664 | (attraux(text->aux) == attr_Last || |
2665 | attraux(text->aux) == attr_Only)) |
2666 | rdadd(rs, L'_'); /* FIXME: configurability */ |
2667 | else if (towordstyle(text->type) == word_Code && |
2668 | (attraux(text->aux) == attr_Last || |
2669 | attraux(text->aux) == attr_Only)) |
2670 | rdadd(rs, L'\''); /* FIXME: configurability */ |
2671 | break; |
2672 | } |
2673 | } |
2674 | |
2675 | static wchar_t *prepare_outline_title(word *first, wchar_t *separator, |
2676 | word *second) |
2677 | { |
2678 | rdstring rs = {0, 0, NULL}; |
2679 | |
2680 | if (first) |
2681 | paper_rdaddw(&rs, first); |
2682 | if (separator) |
2683 | rdadds(&rs, separator); |
2684 | if (second) |
2685 | paper_rdaddw(&rs, second); |
2686 | |
2687 | return rs.text; |
87bd6353 |
2688 | } |
2bfd1b76 |
2689 | |
2690 | static word *fake_word(wchar_t *text) |
2691 | { |
f1530049 |
2692 | word *ret = snew(word); |
2bfd1b76 |
2693 | ret->next = NULL; |
2694 | ret->alt = NULL; |
2695 | ret->type = word_Normal; |
2696 | ret->text = ustrdup(text); |
2697 | ret->breaks = FALSE; |
2698 | ret->aux = 0; |
2699 | return ret; |
2700 | } |
2701 | |
c6536773 |
2702 | static word *fake_space_word(void) |
2703 | { |
f1530049 |
2704 | word *ret = snew(word); |
c6536773 |
2705 | ret->next = NULL; |
2706 | ret->alt = NULL; |
2707 | ret->type = word_WhiteSpace; |
2708 | ret->text = NULL; |
2709 | ret->breaks = TRUE; |
2710 | ret->aux = 0; |
2711 | return ret; |
2712 | } |
2713 | |
3f3d1acc |
2714 | static word *fake_page_ref(page_data *page) |
2715 | { |
f1530049 |
2716 | word *ret = snew(word); |
3f3d1acc |
2717 | ret->next = NULL; |
2718 | ret->alt = NULL; |
2719 | ret->type = word_PageXref; |
2720 | ret->text = NULL; |
2721 | ret->breaks = FALSE; |
2722 | ret->aux = 0; |
2723 | ret->private_data = page; |
2724 | return ret; |
2725 | } |
2726 | |
2727 | static word *fake_end_ref(void) |
2728 | { |
f1530049 |
2729 | word *ret = snew(word); |
3f3d1acc |
2730 | ret->next = NULL; |
2731 | ret->alt = NULL; |
2732 | ret->type = word_XrefEnd; |
2733 | ret->text = NULL; |
2734 | ret->breaks = FALSE; |
2735 | ret->aux = 0; |
2736 | return ret; |
2737 | } |
2738 | |
2bfd1b76 |
2739 | static word *prepare_contents_title(word *first, wchar_t *separator, |
2740 | word *second) |
2741 | { |
2742 | word *ret; |
2743 | word **wptr, *w; |
2744 | |
2745 | wptr = &ret; |
2746 | |
2747 | if (first) { |
2748 | w = dup_word_list(first); |
2749 | *wptr = w; |
2750 | while (w->next) |
2751 | w = w->next; |
2752 | wptr = &w->next; |
2753 | } |
2754 | |
2755 | if (separator) { |
2756 | w = fake_word(separator); |
2757 | *wptr = w; |
2758 | wptr = &w->next; |
2759 | } |
2760 | |
2761 | if (second) { |
2762 | *wptr = dup_word_list(second); |
2763 | } |
2764 | |
2765 | return ret; |
2766 | } |
c6536773 |
2767 | |
2768 | static void fold_into_page(page_data *dest, page_data *src, int right_shift) |
2769 | { |
2770 | line_data *ldata; |
2771 | |
2772 | if (!src->first_line) |
2773 | return; |
2774 | |
2775 | if (dest->last_line) { |
2776 | dest->last_line->next = src->first_line; |
2777 | src->first_line->prev = dest->last_line; |
2778 | } |
2779 | dest->last_line = src->last_line; |
2780 | |
2781 | for (ldata = src->first_line; ldata; ldata = ldata->next) { |
2782 | ldata->page = dest; |
2783 | ldata->xpos += right_shift; |
2784 | |
2785 | if (ldata == src->last_line) |
2786 | break; |
2787 | } |
2788 | } |