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