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