2 * Paper printing pre-backend for Halibut.
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.
13 * TODO in future work:
15 * - linearised PDF, perhaps?
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.
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.
27 * - rather than the ugly aux_text mechanism for rendering chapter
28 * titles, we could actually build the correct word list and
31 * - get vertical font metrics and use them to position the PDF
32 * xref boxes more pleasantly
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
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
43 * * fixed text: `Contents', `Index', the colon-space and full
44 * stop in chapter title constructions
45 * * configurable location of contents?
46 * * certainly configurably _remove_ the contents, and possibly
48 * * double-sided document switch?
49 * + means you have two header/footer formats which
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
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
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
68 * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw!
69 * * substituting `minus' for `hyphen' in the standard encoding
70 * is probably preferable in Courier, though certainly not in
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.
86 typedef struct paper_conf_Tag paper_conf
;
87 typedef struct paper_idx_Tag paper_idx
;
90 font_data
*fonts
[NFONTS
];
94 struct paper_conf_Tag
{
101 int indent_list_bullet
;
102 int indent_list_after
;
106 int base_para_spacing
;
107 int chapter_top_space
;
108 int sect_num_left_space
;
109 int chapter_underline_depth
;
110 int chapter_underline_thickness
;
112 font_cfg fbase
, fcode
, ftitle
, fchapter
, *fsect
;
114 int contents_indent_step
;
116 int leader_separation
;
120 int pagenum_fontsize
;
122 wchar_t *lquote
, *rquote
, *bullet
;
123 /* These are derived from the above */
129 struct paper_idx_Tag
{
131 * Word list giving the page numbers on which this index entry
132 * appears. Also the last word in the list, for ease of
138 * The last page added to the list (so we can ensure we don't
145 word_PageXref
= word_NotWordType
+ 1
148 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
149 static void wrap_paragraph(para_data
*pdata
, word
*words
,
150 int w
, int i1
, int i2
, paper_conf
*conf
);
151 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
152 int page_height
, int ncols
, int headspace
);
153 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
154 int x
, int y
, wchar_t *str
);
155 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
156 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
158 static void render_para(para_data
*pdata
, paper_conf
*conf
,
159 keywordlist
*keywords
, indexdata
*idx
,
160 paragraph
*index_placeholder
, page_data
*index_page
);
161 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
162 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
);
163 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
164 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
165 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
166 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
167 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
169 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
170 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
172 static word
*fake_word(wchar_t *text
);
173 static word
*fake_space_word(void);
174 static word
*fake_page_ref(page_data
*page
);
175 static word
*fake_end_ref(void);
176 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
178 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
180 static int fonts_ok(wchar_t *string
, ...)
186 va_start(ap
, string
);
187 while ( (font
= va_arg(ap
, font_data
*)) != NULL
) {
189 (void) string_width(font
, string
, &errs
);
200 static void paper_cfg_fonts(font_data
**fonts
, font_list
*fontlist
,
201 wchar_t *wp
, filepos
*fpos
) {
206 for (i
= 0; i
< NFONTS
&& *wp
; i
++, wp
= uadv(wp
)) {
207 fn
= utoa_dup(wp
, CS_ASCII
);
208 f
= make_std_font(fontlist
, fn
);
212 /* FIXME: proper error */
213 error(err_nofont
, fpos
, wp
);
217 static paper_conf
paper_configure(paragraph
*source
, font_list
*fontlist
) {
224 ret
.paper_width
= 595 * UNITS_PER_PT
;
225 ret
.paper_height
= 842 * UNITS_PER_PT
;
226 ret
.left_margin
= 72 * UNITS_PER_PT
;
227 ret
.top_margin
= 72 * UNITS_PER_PT
;
228 ret
.right_margin
= 72 * UNITS_PER_PT
;
229 ret
.bottom_margin
= 108 * UNITS_PER_PT
;
230 ret
.indent_list_bullet
= 6 * UNITS_PER_PT
;
231 ret
.indent_list_after
= 18 * UNITS_PER_PT
;
232 ret
.indent_quote
= 18 * UNITS_PER_PT
;
233 ret
.base_leading
= UNITS_PER_PT
;
234 ret
.base_para_spacing
= 10 * UNITS_PER_PT
;
235 ret
.chapter_top_space
= 72 * UNITS_PER_PT
;
236 ret
.sect_num_left_space
= 12 * UNITS_PER_PT
;
237 ret
.chapter_underline_depth
= 14 * UNITS_PER_PT
;
238 ret
.chapter_underline_thickness
= 3 * UNITS_PER_PT
;
239 ret
.rule_thickness
= 1 * UNITS_PER_PT
;
240 ret
.fbase
.font_size
= 12;
241 ret
.fbase
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Times-Roman");
242 ret
.fbase
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Times-Italic");
243 ret
.fbase
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
244 ret
.fcode
.font_size
= 12;
245 ret
.fcode
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Courier-Bold");
246 ret
.fcode
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Courier-Oblique");
247 ret
.fcode
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
248 ret
.ftitle
.font_size
= 24;
249 ret
.ftitle
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Helvetica-Bold");
250 ret
.ftitle
.fonts
[FONT_EMPH
] =
251 make_std_font(fontlist
, "Helvetica-BoldOblique");
252 ret
.ftitle
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
253 ret
.fchapter
.font_size
= 20;
254 ret
.fchapter
.fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
255 ret
.fchapter
.fonts
[FONT_EMPH
] =
256 make_std_font(fontlist
, "Helvetica-BoldOblique");
257 ret
.fchapter
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
259 ret
.fsect
= snewn(ret
.nfsect
, font_cfg
);
260 ret
.fsect
[0].font_size
= 16;
261 ret
.fsect
[0].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
262 ret
.fsect
[0].fonts
[FONT_EMPH
] =
263 make_std_font(fontlist
, "Helvetica-BoldOblique");
264 ret
.fsect
[0].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
265 ret
.fsect
[1].font_size
= 14;
266 ret
.fsect
[1].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
267 ret
.fsect
[1].fonts
[FONT_EMPH
] =
268 make_std_font(fontlist
, "Helvetica-BoldOblique");
269 ret
.fsect
[1].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
270 ret
.fsect
[2].font_size
= 13;
271 ret
.fsect
[2].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
272 ret
.fsect
[2].fonts
[FONT_EMPH
] =
273 make_std_font(fontlist
, "Helvetica-BoldOblique");
274 ret
.fsect
[2].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
275 ret
.contents_indent_step
= 24 * UNITS_PER_PT
;
276 ret
.contents_margin
= 84 * UNITS_PER_PT
;
277 ret
.leader_separation
= 12 * UNITS_PER_PT
;
278 ret
.index_gutter
= 36 * UNITS_PER_PT
;
280 ret
.index_minsep
= 18 * UNITS_PER_PT
;
281 ret
.pagenum_fontsize
= 12;
282 ret
.footer_distance
= 32 * UNITS_PER_PT
;
283 ret
.lquote
= L
"\x2018\0\x2019\0'\0'\0\0";
284 ret
.rquote
= uadv(ret
.lquote
);
285 ret
.bullet
= L
"\x2022\0-\0\0";
288 * Two-pass configuration so that we can pick up global config
289 * (e.g. `quotes') before having it overridden by specific
290 * config (`paper-quotes'), irrespective of the order in which
293 for (p
= source
; p
; p
= p
->next
) {
294 if (p
->type
== para_Config
) {
295 if (!ustricmp(p
->keyword
, L
"quotes")) {
296 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
297 ret
.lquote
= uadv(p
->keyword
);
298 ret
.rquote
= uadv(ret
.lquote
);
304 for (p
= source
; p
; p
= p
->next
) {
305 p
->private_data
= NULL
;
306 if (p
->type
== para_Config
) {
307 if (!ustricmp(p
->keyword
, L
"paper-quotes")) {
308 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
309 ret
.lquote
= uadv(p
->keyword
);
310 ret
.rquote
= uadv(ret
.lquote
);
312 } else if (!ustricmp(p
->keyword
, L
"paper-bullet")) {
313 ret
.bullet
= uadv(p
->keyword
);
314 } else if (!ustricmp(p
->keyword
, L
"paper-page-width")) {
316 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
317 } else if (!ustricmp(p
->keyword
, L
"paper-page-height")) {
319 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
320 } else if (!ustricmp(p
->keyword
, L
"paper-left-margin")) {
322 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
323 } else if (!ustricmp(p
->keyword
, L
"paper-top-margin")) {
325 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
326 } else if (!ustricmp(p
->keyword
, L
"paper-right-margin")) {
328 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
329 } else if (!ustricmp(p
->keyword
, L
"paper-bottom-margin")) {
331 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
332 } else if (!ustricmp(p
->keyword
, L
"paper-list-indent")) {
333 ret
.indent_list_bullet
=
334 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
335 } else if (!ustricmp(p
->keyword
, L
"paper-listitem-indent")) {
337 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
338 } else if (!ustricmp(p
->keyword
, L
"paper-quote-indent")) {
340 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
341 } else if (!ustricmp(p
->keyword
, L
"paper-base-leading")) {
343 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
344 } else if (!ustricmp(p
->keyword
, L
"paper-base-para-spacing")) {
345 ret
.base_para_spacing
=
346 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
347 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-top-space")) {
348 ret
.chapter_top_space
=
349 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
350 } else if (!ustricmp(p
->keyword
, L
"paper-sect-num-left-space")) {
351 ret
.sect_num_left_space
=
352 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
353 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-depth")) {
354 ret
.chapter_underline_depth
=
355 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
356 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-thickness")) {
357 ret
.chapter_underline_thickness
=
358 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
359 } else if (!ustricmp(p
->keyword
, L
"paper-rule-thickness")) {
361 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
362 } else if (!ustricmp(p
->keyword
, L
"paper-contents-indent-step")) {
363 ret
.contents_indent_step
=
364 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
365 } else if (!ustricmp(p
->keyword
, L
"paper-contents-margin")) {
366 ret
.contents_margin
=
367 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
368 } else if (!ustricmp(p
->keyword
, L
"paper-leader-separation")) {
369 ret
.leader_separation
=
370 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
371 } else if (!ustricmp(p
->keyword
, L
"paper-index-gutter")) {
373 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
374 } else if (!ustricmp(p
->keyword
, L
"paper-index-minsep")) {
376 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
377 } else if (!ustricmp(p
->keyword
, L
"paper-footer-distance")) {
378 ret
.footer_distance
=
379 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
380 } else if (!ustricmp(p
->keyword
, L
"paper-base-font-size")) {
381 ret
.fbase
.font_size
= utoi(uadv(p
->keyword
));
382 } else if (!ustricmp(p
->keyword
, L
"paper-index-columns")) {
383 ret
.index_cols
= utoi(uadv(p
->keyword
));
384 } else if (!ustricmp(p
->keyword
, L
"paper-pagenum-font-size")) {
385 ret
.pagenum_fontsize
= utoi(uadv(p
->keyword
));
386 } else if (!ustricmp(p
->keyword
, L
"paper-base-fonts")) {
387 paper_cfg_fonts(ret
.fbase
.fonts
, fontlist
, uadv(p
->keyword
),
389 } else if (!ustricmp(p
->keyword
, L
"paper-code-font-size")) {
390 ret
.fcode
.font_size
= utoi(uadv(p
->keyword
));
391 } else if (!ustricmp(p
->keyword
, L
"paper-code-fonts")) {
392 paper_cfg_fonts(ret
.fcode
.fonts
, fontlist
, uadv(p
->keyword
),
394 } else if (!ustricmp(p
->keyword
, L
"paper-title-font-size")) {
395 ret
.ftitle
.font_size
= utoi(uadv(p
->keyword
));
396 } else if (!ustricmp(p
->keyword
, L
"paper-title-fonts")) {
397 paper_cfg_fonts(ret
.ftitle
.fonts
, fontlist
, uadv(p
->keyword
),
399 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-font-size")) {
400 ret
.fchapter
.font_size
= utoi(uadv(p
->keyword
));
401 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-fonts")) {
402 paper_cfg_fonts(ret
.fchapter
.fonts
, fontlist
, uadv(p
->keyword
),
404 } else if (!ustricmp(p
->keyword
, L
"paper-section-font-size")) {
405 wchar_t *q
= uadv(p
->keyword
);
411 if (n
>= ret
.nfsect
) {
413 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
414 for (i
= ret
.nfsect
; i
<= n
; i
++)
415 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
418 ret
.fsect
[n
].font_size
= utoi(q
);
419 } else if (!ustricmp(p
->keyword
, L
"paper-section-fonts")) {
420 wchar_t *q
= uadv(p
->keyword
);
426 if (n
>= ret
.nfsect
) {
428 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
429 for (i
= ret
.nfsect
; i
<= n
; i
++)
430 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
433 paper_cfg_fonts(ret
.fsect
[n
].fonts
, fontlist
, q
, &p
->fpos
);
439 * Set up the derived fields in the conf structure.
443 ret
.paper_width
- ret
.left_margin
- ret
.right_margin
;
445 ret
.paper_height
- ret
.top_margin
- ret
.bottom_margin
;
446 ret
.indent_list
= ret
.indent_list_bullet
+ ret
.indent_list_after
;
448 (ret
.base_width
- (ret
.index_cols
-1) * ret
.index_gutter
)
452 * Now process fallbacks on quote characters and bullets. We
453 * use string_width() to determine whether all of the relevant
454 * fonts contain the same character, and fall back whenever we
455 * find a character which not all of them support.
458 /* Quote characters need not be supported in the fixed code fonts,
459 * but must be in the title and body fonts. */
460 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
))) {
462 if (fonts_ok(ret
.lquote
,
463 ret
.fbase
.fonts
[FONT_NORMAL
],
464 ret
.fbase
.fonts
[FONT_EMPH
],
465 ret
.ftitle
.fonts
[FONT_NORMAL
],
466 ret
.ftitle
.fonts
[FONT_EMPH
],
467 ret
.fchapter
.fonts
[FONT_NORMAL
],
468 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
) &&
470 ret
.fbase
.fonts
[FONT_NORMAL
],
471 ret
.fbase
.fonts
[FONT_EMPH
],
472 ret
.ftitle
.fonts
[FONT_NORMAL
],
473 ret
.ftitle
.fonts
[FONT_EMPH
],
474 ret
.fchapter
.fonts
[FONT_NORMAL
],
475 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
)) {
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
))
487 ret
.lquote
= uadv(ret
.rquote
);
488 ret
.rquote
= uadv(ret
.lquote
);
491 /* The bullet character only needs to be supported in the normal body
492 * font (not even in italics). */
493 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
494 !fonts_ok(ret
.bullet
, ret
.fbase
.fonts
[FONT_NORMAL
], NULL
))
495 ret
.bullet
= uadv(ret
.bullet
);
500 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
504 int indent
, used_contents
;
505 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
506 para_data
*firstcont
, *lastcont
;
507 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
510 paper_conf
*conf
, ourconf
;
513 paragraph index_placeholder_para
;
514 page_data
*first_index_page
;
517 fontlist
= snew(font_list
);
518 fontlist
->head
= fontlist
->tail
= NULL
;
520 ourconf
= paper_configure(sourceform
, fontlist
);
524 * Set up a data structure to collect page numbers for each
533 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
534 paper_idx
*pi
= snew(paper_idx
);
538 pi
->words
= pi
->lastword
= NULL
;
541 entry
->backend_data
= pi
;
546 * Format the contents entry for each heading.
549 word
*contents_title
;
550 contents_title
= fake_word(L
"Contents");
552 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
553 NULL
, NULL
, contents_title
, conf
);
554 lastcont
= firstcont
;
555 lastcont
->next
= NULL
;
556 firstcontline
= firstcont
->first
;
557 lastcontline
= lastcont
->last
;
558 for (p
= sourceform
; p
; p
= p
->next
) {
565 case para_UnnumberedChapter
:
571 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
574 case para_UnnumberedChapter
:
575 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
580 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
581 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
584 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
585 conf
->contents_margin
,
586 NULL
, NULL
, words
, conf
);
588 pdata
->contents_entry
= p
;
589 lastcont
->next
= pdata
;
593 * Link all contents line structures together into
598 lastcontline
->next
= pdata
->first
;
599 pdata
->first
->prev
= lastcontline
;
601 firstcontline
= pdata
->first
;
602 pdata
->first
->prev
= NULL
;
604 lastcontline
= pdata
->last
;
605 lastcontline
->next
= NULL
;
613 * And one extra one, for the index.
616 pdata
= make_para_data(para_Normal
, 0, 0,
617 conf
->contents_margin
,
618 NULL
, NULL
, fake_word(L
"Index"), conf
);
620 pdata
->contents_entry
= &index_placeholder_para
;
621 lastcont
->next
= pdata
;
626 lastcontline
->next
= pdata
->first
;
627 pdata
->first
->prev
= lastcontline
;
629 firstcontline
= pdata
->first
;
630 pdata
->first
->prev
= NULL
;
632 lastcontline
= pdata
->last
;
633 lastcontline
->next
= NULL
;
639 * Do the main paragraph formatting.
642 used_contents
= FALSE
;
643 firstline
= lastline
= NULL
;
644 for (p
= sourceform
; p
; p
= p
->next
) {
645 p
->private_data
= NULL
;
649 * These paragraph types are either invisible or don't
650 * define text in the normal sense. Either way, they
651 * don't require wrapping.
656 case para_NotParaType
:
663 * These paragraph types don't require wrapping, but
664 * they do affect the line width to which we wrap the
665 * rest of the paragraphs, so we need to pay attention.
668 indent
+= conf
->indent_list
; break;
670 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
672 indent
+= conf
->indent_quote
; break;
674 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
677 * This paragraph type is special. Process it
681 pdata
= code_paragraph(indent
, p
->words
, conf
);
682 p
->private_data
= pdata
;
683 if (pdata
->first
!= pdata
->last
) {
684 pdata
->first
->penalty_after
+= 100000;
685 pdata
->last
->penalty_before
+= 100000;
690 * This paragraph is also special.
693 pdata
= rule_paragraph(indent
, conf
);
694 p
->private_data
= pdata
;
698 * All of these paragraph types require wrapping in the
699 * ordinary way. So we must supply a set of fonts, a
700 * line width and auxiliary information (e.g. bullet
701 * text) for each one.
705 case para_UnnumberedChapter
:
709 case para_BiblioCited
:
711 case para_NumberedList
:
712 case para_DescribedThing
:
713 case para_Description
:
716 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
717 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
719 p
->private_data
= pdata
;
724 if (p
->private_data
) {
725 pdata
= (para_data
*)p
->private_data
;
728 * If this is the first non-title heading, we link the
729 * contents section in before it.
731 if (!used_contents
&& pdata
->outline_level
> 0) {
732 used_contents
= TRUE
;
734 lastpara
->next
= firstcont
;
736 firstpara
= firstcont
;
738 assert(lastpara
->next
== NULL
);
741 lastline
->next
= firstcontline
;
742 firstcontline
->prev
= lastline
;
744 firstline
= firstcontline
;
745 firstcontline
->prev
= NULL
;
747 assert(lastcontline
!= NULL
);
748 lastline
= lastcontline
;
749 lastline
->next
= NULL
;
753 * Link all line structures together into a big list.
757 lastline
->next
= pdata
->first
;
758 pdata
->first
->prev
= lastline
;
760 firstline
= pdata
->first
;
761 pdata
->first
->prev
= NULL
;
763 lastline
= pdata
->last
;
764 lastline
->next
= NULL
;
768 * Link all paragraph structures together similarly.
772 lastpara
->next
= pdata
;
780 * Now we have an enormous linked list of every line of text in
781 * the document. Break it up into pages.
783 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
794 for (page
= pages
; page
; page
= page
->next
) {
795 sprintf(buf
, "%d", ++pagenum
);
796 page
->number
= ufroma_dup(buf
, CS_ASCII
);
800 first_index_page
= snew(page_data
);
801 first_index_page
->next
= first_index_page
->prev
= NULL
;
802 first_index_page
->first_line
= NULL
;
803 first_index_page
->last_line
= NULL
;
804 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
805 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
806 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
808 /* And don't forget the as-yet-uncreated index. */
809 sprintf(buf
, "%d", ++pagenum
);
810 first_index_page
->number
= ufroma_dup(buf
, CS_ASCII
);
815 * Now we're ready to actually lay out the pages. We do this by
816 * looping over _paragraphs_, since we may need to track cross-
817 * references between lines and even across pages.
819 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
820 render_para(pdata
, conf
, keywords
, idx
,
821 &index_placeholder_para
, first_index_page
);
824 * Now we've laid out the main body pages, we should have
825 * acquired a full set of page numbers for the index.
831 para_data
*firstidx
, *lastidx
;
832 line_data
*firstidxline
, *lastidxline
, *ldata
;
833 page_data
*ipages
, *ipages2
, *page
;
836 * Create a set of paragraphs for the index.
838 index_title
= fake_word(L
"Index");
840 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
841 NULL
, NULL
, index_title
, conf
);
843 lastidx
->next
= NULL
;
844 firstidxline
= firstidx
->first
;
845 lastidxline
= lastidx
->last
;
846 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
847 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
848 para_data
*text
, *pages
;
853 text
= make_para_data(para_Normal
, 0, 0,
854 conf
->base_width
- conf
->index_colwidth
,
855 NULL
, NULL
, entry
->text
, conf
);
857 pages
= make_para_data(para_Normal
, 0, 0,
858 conf
->base_width
- conf
->index_colwidth
,
859 NULL
, NULL
, pi
->words
, conf
);
861 text
->justification
= LEFT
;
862 pages
->justification
= RIGHT
;
863 text
->last
->space_after
= pages
->first
->space_before
=
864 conf
->base_leading
/ 2;
866 pages
->last
->space_after
= text
->first
->space_before
=
870 assert(pages
->first
);
875 * If feasible, fold the two halves of the index entry
878 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
879 conf
->index_colwidth
+ conf
->index_minsep
) {
880 text
->last
->space_after
= -1;
881 pages
->first
->space_before
= -pages
->first
->line_height
+1;
884 lastidx
->next
= text
;
890 * Link all index line structures together into
893 text
->last
->next
= pages
->first
;
894 pages
->first
->prev
= text
->last
;
896 lastidxline
->next
= text
->first
;
897 text
->first
->prev
= lastidxline
;
899 lastidxline
= pages
->last
;
902 * Breaking an index entry anywhere is so bad that I
903 * think I'm going to forbid it totally.
905 for (ldata
= text
->first
; ldata
&& ldata
->next
;
906 ldata
= ldata
->next
) {
907 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
908 ldata
->space_after
= -1;
913 * Now break the index into pages.
915 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
917 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
920 firstidxline
->space_before
+
921 firstidxline
->line_height
+
922 firstidxline
->space_after
);
925 * This will have put each _column_ of the index on a
926 * separate page, which isn't what we want. Fold the pages
933 for (i
= 1; i
< conf
->index_cols
; i
++)
937 fold_into_page(page
, page
->next
,
938 i
* (conf
->index_colwidth
+
939 conf
->index_gutter
));
941 page
->next
= page
->next
->next
;
943 page
->next
->prev
= page
;
949 /* Also fold the heading on to the same page as the index items. */
950 fold_into_page(ipages
, ipages2
, 0);
951 ipages
->next
= ipages2
->next
;
953 ipages
->next
->prev
= ipages
;
955 fold_into_page(first_index_page
, ipages
, 0);
956 first_index_page
->next
= ipages
->next
;
957 if (first_index_page
->next
)
958 first_index_page
->next
->prev
= first_index_page
;
960 ipages
= first_index_page
;
963 * Number the index pages, except the already-numbered
966 for (page
= ipages
->next
; page
; page
= page
->next
) {
968 sprintf(buf
, "%d", ++pagenum
);
969 page
->number
= ufroma_dup(buf
, CS_ASCII
);
973 * Render the index pages.
975 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
976 render_para(pdata
, conf
, keywords
, idx
,
977 &index_placeholder_para
, first_index_page
);
980 * Link the index page list on to the end of the main page
986 for (page
= pages
; page
->next
; page
= page
->next
);
991 * Same with the paragraph list, which will cause the index
992 * to be mentioned in the document outline.
995 firstpara
= firstidx
;
997 lastpara
->next
= firstidx
;
1002 * Draw the headers and footers.
1004 * FIXME: this should be fully configurable, but for the moment
1005 * I'm just going to put in page numbers in the centre of a
1006 * footer and leave it at that.
1011 for (page
= pages
; page
; page
= page
->next
) {
1014 width
= conf
->pagenum_fontsize
*
1015 string_width(conf
->fbase
.fonts
[FONT_NORMAL
], page
->number
,
1018 render_string(page
, conf
->fbase
.fonts
[FONT_NORMAL
],
1019 conf
->pagenum_fontsize
,
1020 conf
->left_margin
+ (conf
->base_width
- width
)/2,
1021 conf
->bottom_margin
- conf
->footer_distance
,
1027 * Start putting together the overall document structure we're
1030 doc
= snew(document
);
1031 doc
->fonts
= fontlist
;
1033 doc
->paper_width
= conf
->paper_width
;
1034 doc
->paper_height
= conf
->paper_height
;
1037 * Collect the section heading paragraphs into a document
1038 * outline. This is slightly fiddly because the Title paragraph
1039 * isn't required to be at the start, although all the others
1045 doc
->outline_elements
= snewn(osize
, outline_element
);
1046 doc
->n_outline_elements
= 0;
1048 /* First find the title. */
1049 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1050 if (pdata
->outline_level
== 0) {
1051 doc
->outline_elements
[0].level
= 0;
1052 doc
->outline_elements
[0].pdata
= pdata
;
1053 doc
->n_outline_elements
++;
1058 /* Then collect the rest. */
1059 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1060 if (pdata
->outline_level
> 0) {
1061 if (doc
->n_outline_elements
>= osize
) {
1063 doc
->outline_elements
=
1064 sresize(doc
->outline_elements
, osize
, outline_element
);
1067 doc
->outline_elements
[doc
->n_outline_elements
].level
=
1068 pdata
->outline_level
;
1069 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
1070 doc
->n_outline_elements
++;
1078 static void setfont(para_data
*p
, font_cfg
*f
) {
1081 for (i
= 0; i
< NFONTS
; i
++) {
1082 p
->fonts
[i
] = f
->fonts
[i
];
1083 p
->sizes
[i
] = f
->font_size
;
1087 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
1088 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
1093 int extra_indent
, firstline_indent
, aux_indent
;
1096 pdata
= snew(para_data
);
1097 pdata
->outline_level
= -1;
1098 pdata
->outline_title
= NULL
;
1099 pdata
->rect_type
= RECT_NONE
;
1100 pdata
->contents_entry
= NULL
;
1101 pdata
->justification
= JUST
;
1104 * Choose fonts for this paragraph.
1108 setfont(pdata
, &conf
->ftitle
);
1109 pdata
->outline_level
= 0;
1114 case para_UnnumberedChapter
:
1115 setfont(pdata
, &conf
->fchapter
);
1116 pdata
->outline_level
= 1;
1122 &conf
->fsect
[paux
>= conf
->nfsect ? conf
->nfsect
- 1 : paux
]);
1123 pdata
->outline_level
= 2 + paux
;
1127 case para_BiblioCited
:
1129 case para_NumberedList
:
1130 case para_DescribedThing
:
1131 case para_Description
:
1132 case para_Copyright
:
1133 setfont(pdata
, &conf
->fbase
);
1138 * Also select an indentation level depending on the
1139 * paragraph type (list paragraphs other than
1140 * para_DescribedThing need extra indent).
1142 * (FIXME: Perhaps at some point we might even arrange
1143 * for the user to be able to request indented first
1144 * lines in paragraphs.)
1146 if (ptype
== para_Bullet
||
1147 ptype
== para_NumberedList
||
1148 ptype
== para_Description
) {
1149 extra_indent
= firstline_indent
= conf
->indent_list
;
1151 extra_indent
= firstline_indent
= 0;
1155 * Find the auxiliary text for this paragraph.
1166 * For some heading styles (FIXME: be able to
1167 * configure which), the auxiliary text contains
1168 * the chapter number and is arranged to be
1169 * right-aligned a few points left of the primary
1170 * margin. For other styles, the auxiliary text is
1171 * the full chapter _name_ and takes up space
1172 * within the (wrapped) chapter title, meaning that
1173 * we must move the first line indent over to make
1176 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
1180 len
= paper_width_simple(pdata
, pkwtext2
, conf
);
1181 aux_indent
= -len
- conf
->sect_num_left_space
;
1183 pdata
->outline_title
=
1184 prepare_outline_title(pkwtext2
, L
" ", pwords
);
1187 aux2
= fake_word(L
": ");
1190 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1191 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1193 pdata
->outline_title
=
1194 prepare_outline_title(pkwtext
, L
": ", pwords
);
1200 * Auxiliary text consisting of a bullet.
1202 aux
= fake_word(conf
->bullet
);
1203 aux_indent
= indent
+ conf
->indent_list_bullet
;
1206 case para_NumberedList
:
1208 * Auxiliary text consisting of the number followed
1209 * by a (FIXME: configurable) full stop.
1212 aux2
= fake_word(L
".");
1213 aux_indent
= indent
+ conf
->indent_list_bullet
;
1216 case para_BiblioCited
:
1218 * Auxiliary text consisting of the bibliography
1219 * reference text, and a trailing space.
1222 aux2
= fake_word(L
" ");
1223 aux_indent
= indent
;
1224 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1225 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1229 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
1230 pdata
->outline_title
=
1231 prepare_outline_title(NULL
, NULL
, pwords
);
1234 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
1235 indent
+ firstline_indent
,
1236 indent
+ extra_indent
, conf
);
1238 pdata
->first
->aux_text
= aux
;
1239 pdata
->first
->aux_text_2
= aux2
;
1240 pdata
->first
->aux_left_indent
= aux_indent
;
1243 * Line breaking penalties.
1250 case para_UnnumberedChapter
:
1252 * Fixed and large penalty for breaking straight
1253 * after a heading; corresponding bonus for
1254 * breaking straight before.
1256 pdata
->first
->penalty_before
= -500000;
1257 pdata
->last
->penalty_after
= 500000;
1258 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
1259 ldata
->penalty_after
= 500000;
1262 case para_DescribedThing
:
1264 * This is treated a bit like a small heading:
1265 * there's a penalty for breaking after it (i.e.
1266 * between it and its description), and a bonus for
1267 * breaking before it (actually _between_ list
1270 pdata
->first
->penalty_before
= -200000;
1271 pdata
->last
->penalty_after
= 200000;
1276 * Most paragraph types: widow/orphan control by
1277 * discouraging breaking one line from the end of
1280 if (pdata
->first
!= pdata
->last
) {
1281 pdata
->first
->penalty_after
= 100000;
1282 pdata
->last
->penalty_before
= 100000;
1287 standard_line_spacing(pdata
, conf
);
1290 * Some kinds of section heading require a page break before
1291 * them and an underline after.
1293 if (ptype
== para_Title
||
1294 ptype
== para_Chapter
||
1295 ptype
== para_Appendix
||
1296 ptype
== para_UnnumberedChapter
) {
1297 pdata
->first
->page_break
= TRUE
;
1298 pdata
->first
->space_before
= conf
->chapter_top_space
;
1299 pdata
->last
->space_after
+=
1300 (conf
->chapter_underline_depth
+
1301 conf
->chapter_underline_thickness
);
1302 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1308 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1313 * Set the line spacing for each line in this paragraph.
1315 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1316 if (ldata
== pdata
->first
)
1317 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1319 ldata
->space_before
= conf
->base_leading
/ 2;
1320 if (ldata
== pdata
->last
)
1321 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1323 ldata
->space_after
= conf
->base_leading
/ 2;
1324 ldata
->page_break
= FALSE
;
1328 static font_encoding
*new_font_encoding(font_data
*font
)
1333 fe
= snew(font_encoding
);
1336 if (font
->list
->tail
)
1337 font
->list
->tail
->next
= fe
;
1339 font
->list
->head
= fe
;
1340 font
->list
->tail
= fe
;
1343 fe
->free_pos
= 0x21;
1345 for (i
= 0; i
< 256; i
++) {
1346 fe
->vector
[i
] = NULL
;
1347 fe
->indices
[i
] = -1;
1348 fe
->to_unicode
[i
] = 0xFFFF;
1354 int kern_cmp(void *a
, void *b
)
1356 kern_pair
const *ka
= a
, *kb
= b
;
1358 if (ka
->left
< kb
->left
)
1360 if (ka
->left
> kb
->left
)
1362 if (ka
->right
< kb
->right
)
1364 if (ka
->right
> kb
->right
)
1369 /* This wouldn't be necessary if C had closures. */
1370 static font_info
*glyph_cmp_fi
;
1372 static int glyph_cmp(void const *a
, void const *b
)
1374 return strcmp(glyph_cmp_fi
->glyphs
[*(unsigned short *)a
],
1375 glyph_cmp_fi
->glyphs
[*(unsigned short *)b
]);
1379 * Set up the glyphsbyname index for a font.
1381 void font_index_glyphs(font_info
*fi
) {
1384 fi
->glyphsbyname
= snewn(fi
->nglyphs
, unsigned short);
1385 for (i
= 0; i
< fi
->nglyphs
; i
++)
1386 fi
->glyphsbyname
[i
] = i
;
1388 qsort(fi
->glyphsbyname
, fi
->nglyphs
, sizeof(fi
->glyphsbyname
[0]),
1392 int find_glyph(font_info
*fi
, char const *name
) {
1399 r
= strcmp(fi
->glyphs
[fi
->glyphsbyname
[k
]], name
);
1401 return fi
->glyphsbyname
[k
];
1410 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1413 font_info
const *fi
;
1418 for (fe
= fontlist
->head
; fe
; fe
= fe
->next
)
1419 if (strcmp(fe
->font
->info
->name
, name
) == 0)
1422 for (fi
= all_fonts
; fi
; fi
= fi
->next
)
1423 if (strcmp(fi
->name
, name
) == 0) break;
1424 if (!fi
) return NULL
;
1426 f
= snew(font_data
);
1430 nglyphs
= f
->info
->nglyphs
;
1431 f
->subfont_map
= snewn(nglyphs
, subfont_map_entry
);
1434 * Our first subfont will contain all of US-ASCII. This isn't
1435 * really necessary - we could just create custom subfonts
1436 * precisely as the whim of render_string dictated - but
1437 * instinct suggests that it might be nice to have the text in
1438 * the output files look _marginally_ recognisable.
1440 fe
= new_font_encoding(f
);
1441 fe
->free_pos
= 0xA1; /* only the top half is free */
1442 f
->latest_subfont
= fe
;
1444 for (i
= 0; i
< nglyphs
; i
++) {
1446 ucs
= ps_glyph_to_unicode(f
->info
->glyphs
[i
]);
1447 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1448 fe
->vector
[ucs
] = f
->info
->glyphs
[i
];
1449 fe
->indices
[ucs
] = i
;
1450 fe
->to_unicode
[ucs
] = ucs
;
1451 f
->subfont_map
[i
].subfont
= fe
;
1452 f
->subfont_map
[i
].position
= ucs
;
1455 * This character is not yet assigned to a subfont.
1457 f
->subfont_map
[i
].subfont
= NULL
;
1458 f
->subfont_map
[i
].position
= 0;
1465 /* NB: arguments are glyph numbers from font->bmp. */
1466 static int find_kern(font_data
*font
, int lindex
, int rindex
)
1469 kern_pair
const *kp
;
1471 if (lindex
== 0xFFFF || rindex
== 0xFFFF)
1473 wantkp
.left
= lindex
;
1474 wantkp
.right
= rindex
;
1475 kp
= find234(font
->info
->kerns
, &wantkp
, NULL
);
1481 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1490 for (; *string
; string
++) {
1491 index
= (*string
< 0 || *string
> 0xFFFF ?
0xFFFF :
1492 font
->info
->bmp
[*string
]);
1494 if (index
== 0xFFFF) {
1498 width
+= find_kern(font
, oindex
, index
) +
1499 font
->info
->widths
[index
];
1507 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1509 struct paper_width_ctx
{
1515 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1517 while (text
&& text
!= end
) {
1518 w
+= paper_width_internal(vctx
, text
, nspaces
);
1524 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1526 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1527 int style
, type
, findex
, width
, errs
;
1530 switch (word
->type
) {
1531 case word_HyperLink
:
1533 case word_UpperXref
:
1534 case word_LowerXref
:
1541 style
= towordstyle(word
->type
);
1542 type
= removeattr(word
->type
);
1544 findex
= (style
== word_Normal ? FONT_NORMAL
:
1545 style
== word_Emph ? FONT_EMPH
:
1548 if (type
== word_Normal
) {
1550 } else if (type
== word_WhiteSpace
) {
1551 if (findex
!= FONT_CODE
) {
1554 return ctx
->minspacewidth
;
1557 } else /* if (type == word_Quote) */ {
1558 if (word
->aux
== quote_Open
)
1559 str
= ctx
->conf
->lquote
;
1561 str
= ctx
->conf
->rquote
;
1564 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1566 if (errs
&& word
->alt
)
1567 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1569 return ctx
->pdata
->sizes
[findex
] * width
;
1572 static int paper_width(void *vctx
, word
*word
)
1574 return paper_width_internal(vctx
, word
, NULL
);
1577 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
)
1579 struct paper_width_ctx ctx
;
1583 (pdata
->sizes
[FONT_NORMAL
] *
1584 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1587 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1590 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1591 int w
, int i1
, int i2
, paper_conf
*conf
)
1593 wrappedline
*wrapping
, *p
;
1595 struct paper_width_ctx ctx
;
1599 * We're going to need to store the line height in every line
1600 * structure we generate.
1605 for (i
= 0; i
< NFONTS
; i
++)
1606 if (line_height
< pdata
->sizes
[i
])
1607 line_height
= pdata
->sizes
[i
];
1608 line_height
*= UNITS_PER_PT
;
1611 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1612 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1613 if (spacewidth
== 0) {
1615 * A font without a space?! Disturbing. I hope this never
1616 * comes up, but I'll make a random guess anyway and set my
1617 * space width to half the point size.
1619 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * UNITS_PER_PT
/ 2;
1623 * I'm going to set the _minimum_ space width to 3/5 of the
1624 * standard one, and use the standard one as the optimum.
1626 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1630 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1633 * Having done the wrapping, we now concoct a set of line_data
1636 pdata
->first
= pdata
->last
= NULL
;
1638 for (p
= wrapping
; p
; p
= p
->next
) {
1641 int len
, wid
, spaces
;
1643 ldata
= snew(line_data
);
1645 ldata
->pdata
= pdata
;
1646 ldata
->first
= p
->begin
;
1647 ldata
->end
= p
->end
;
1648 ldata
->line_height
= line_height
;
1650 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1653 pdata
->last
->next
= ldata
;
1654 ldata
->prev
= pdata
->last
;
1656 pdata
->first
= ldata
;
1660 pdata
->last
= ldata
;
1663 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1664 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1667 ldata
->hshortfall
= wid
- len
;
1668 ldata
->nspaces
= spaces
;
1670 * This tells us how much the space width needs to
1671 * change from _min_spacewidth. But we want to store
1672 * its difference from the _natural_ space width, to
1673 * make the text rendering easier.
1675 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1676 ldata
->hshortfall
-= spacewidth
* spaces
;
1677 ldata
->real_shortfall
= ldata
->hshortfall
;
1679 * Special case: on the last line of a paragraph, we
1680 * never stretch spaces.
1682 if (ldata
->hshortfall
> 0 && !p
->next
)
1683 ldata
->hshortfall
= 0;
1685 ldata
->aux_text
= NULL
;
1686 ldata
->aux_text_2
= NULL
;
1687 ldata
->aux_left_indent
= 0;
1688 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1693 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1694 int page_height
, int ncols
, int headspace
)
1698 int n
, n1
, this_height
;
1701 * Page breaking is done by a close analogue of the optimal
1702 * paragraph wrapping algorithm used by wrap_para(). We work
1703 * backwards from the end of the document line by line; for
1704 * each line, we contemplate every possible number of lines we
1705 * could put on a page starting with that line, determine a
1706 * cost function for each one, add it to the pre-computed cost
1707 * function for optimally page-breaking everything after that
1708 * page, and pick the best option.
1710 * This is made slightly more complex by the fact that we have
1711 * a multi-column index with a heading at the top of the
1712 * _first_ page, meaning that the first _ncols_ pages must have
1713 * a different length. Hence, we must do the wrapping ncols+1
1714 * times over, hypothetically trying to put every subsequence
1715 * on every possible page.
1717 * Since my line_data structures are only used for this
1718 * purpose, I might as well just store the algorithm data
1722 for (l
= last
; l
; l
= l
->prev
) {
1723 l
->bestcost
= snewn(ncols
+1, int);
1724 l
->vshortfall
= snewn(ncols
+1, int);
1725 l
->text
= snewn(ncols
+1, int);
1726 l
->space
= snewn(ncols
+1, int);
1727 l
->page_last
= snewn(ncols
+1, line_data
*);
1729 for (n
= 0; n
<= ncols
; n
++) {
1730 int minheight
, text
= 0, space
= 0;
1733 n1
= (n
< ncols ? n
+1 : ncols
);
1735 this_height
= page_height
- headspace
;
1737 this_height
= page_height
;
1739 l
->bestcost
[n
] = -1;
1740 for (m
= l
; m
; m
= m
->next
) {
1741 if (m
!= l
&& m
->page_break
)
1742 break; /* we've gone as far as we can */
1745 if (m
->prev
->space_after
> 0)
1746 space
+= m
->prev
->space_after
;
1748 text
+= m
->prev
->space_after
;
1750 if (m
!= l
|| m
->page_break
) {
1751 if (m
->space_before
> 0)
1752 space
+= m
->space_before
;
1754 text
+= m
->space_before
;
1756 text
+= m
->line_height
;
1757 minheight
= text
+ space
;
1759 if (m
!= l
&& minheight
> this_height
)
1763 * If the space after this paragraph is _negative_
1764 * (which means the next line is folded on to this
1765 * one, which happens in the index), we absolutely
1766 * cannot break here.
1768 if (m
->space_after
>= 0) {
1771 * Compute the cost of this arrangement, as the
1772 * square of the amount of wasted space on the
1773 * page. Exception: if this is the last page
1774 * before a mandatory break or the document
1775 * end, we don't penalise a large blank area.
1777 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1779 int x
= (this_height
- minheight
) / FUNITS_PER_PT
*
1787 cost
+= (x
* xf
) >> 8;
1791 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1792 cost
+= m
->penalty_after
;
1793 cost
+= m
->next
->penalty_before
;
1796 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1797 cost
+= m
->next
->bestcost
[n1
];
1798 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1800 * This is the best option yet for this
1803 l
->bestcost
[n
] = cost
;
1804 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1805 l
->vshortfall
[n
] = this_height
- minheight
;
1807 l
->vshortfall
[n
] = 0;
1809 l
->space
[n
] = space
;
1810 l
->page_last
[n
] = m
;
1821 * Now go through the line list forwards and assemble the
1830 int text
, space
, head
;
1832 page
= snew(page_data
);
1841 page
->first_line
= l
;
1842 page
->last_line
= l
->page_last
[n
];
1844 page
->first_text
= page
->last_text
= NULL
;
1845 page
->first_xref
= page
->last_xref
= NULL
;
1846 page
->first_rect
= page
->last_rect
= NULL
;
1849 * Now assign a y-coordinate to each line on the page.
1852 head
= (n
< ncols ? headspace
: 0);
1853 for (l
= page
->first_line
; l
; l
= l
->next
) {
1854 if (l
!= page
->first_line
) {
1855 if (l
->prev
->space_after
> 0)
1856 space
+= l
->prev
->space_after
;
1858 text
+= l
->prev
->space_after
;
1860 if (l
!= page
->first_line
|| l
->page_break
) {
1861 if (l
->space_before
> 0)
1862 space
+= l
->space_before
;
1864 text
+= l
->space_before
;
1866 text
+= l
->line_height
;
1869 l
->ypos
= text
+ space
+ head
;
1870 if (page
->first_line
->space
[n
]) {
1871 l
->ypos
+= space
* (float)page
->first_line
->vshortfall
[n
] /
1872 page
->first_line
->space
[n
];
1875 if (l
== page
->last_line
)
1879 l
= page
->last_line
;
1884 n
= (n
< ncols ? n
+1 : ncols
);
1890 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1892 rect
*r
= snew(rect
);
1895 if (page
->last_rect
)
1896 page
->last_rect
->next
= r
;
1898 page
->first_rect
= r
;
1899 page
->last_rect
= r
;
1907 static void add_string_to_page(page_data
*page
, int x
, int y
,
1908 font_encoding
*fe
, int size
, char *text
,
1911 text_fragment
*frag
;
1913 frag
= snew(text_fragment
);
1916 if (page
->last_text
)
1917 page
->last_text
->next
= frag
;
1919 page
->first_text
= frag
;
1920 page
->last_text
= frag
;
1925 frag
->fontsize
= size
;
1926 frag
->text
= dupstr(text
);
1927 frag
->width
= width
;
1931 * Returns the updated x coordinate.
1933 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1934 int x
, int y
, wchar_t *str
)
1937 int textpos
, textwid
, kern
, glyph
, oglyph
;
1938 font_encoding
*subfont
= NULL
, *sf
;
1940 text
= snewn(1 + ustrlen(str
), char);
1941 textpos
= textwid
= 0;
1946 glyph
= (*str
< 0 || *str
> 0xFFFF ?
0xFFFF :
1947 font
->info
->bmp
[*str
]);
1949 if (glyph
== 0xFFFF) {
1951 continue; /* nothing more we can do here */
1955 * Find which subfont this character is going in.
1957 sf
= font
->subfont_map
[glyph
].subfont
;
1963 * This character is not yet in a subfont. Assign one.
1965 if (font
->latest_subfont
->free_pos
>= 0x100)
1966 font
->latest_subfont
= new_font_encoding(font
);
1968 c
= font
->latest_subfont
->free_pos
++;
1969 if (font
->latest_subfont
->free_pos
== 0x7F)
1970 font
->latest_subfont
->free_pos
= 0xA1;
1972 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1973 font
->subfont_map
[glyph
].position
= c
;
1974 font
->latest_subfont
->vector
[c
] = font
->info
->glyphs
[glyph
];
1975 font
->latest_subfont
->indices
[c
] = glyph
;
1976 font
->latest_subfont
->to_unicode
[c
] = *str
;
1978 sf
= font
->latest_subfont
;
1981 kern
= find_kern(font
, oglyph
, glyph
) * fontsize
;
1983 if (!subfont
|| sf
!= subfont
|| kern
) {
1985 text
[textpos
] = '\0';
1986 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
,
1988 x
+= textwid
+ kern
;
1990 assert(textpos
== 0);
1997 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1998 textwid
+= font
->info
->widths
[glyph
] * fontsize
;
2004 text
[textpos
] = '\0';
2005 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
, textwid
);
2013 * Returns the updated x coordinate.
2015 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
2016 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
2017 int shortfall
, int nspaces
, int *nspace
,
2018 keywordlist
*keywords
, indexdata
*idx
, paper_conf
*conf
)
2020 while (text
&& text
!= text_end
) {
2021 int style
, type
, findex
, errs
;
2025 switch (text
->type
) {
2027 * Start a cross-reference.
2029 case word_HyperLink
:
2030 case word_UpperXref
:
2031 case word_LowerXref
:
2034 if (text
->type
== word_HyperLink
) {
2036 dest
.url
= utoa_dup(text
->text
, CS_ASCII
);
2038 } else if (text
->type
== word_PageXref
) {
2041 dest
.page
= (page_data
*)text
->private_data
;
2043 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
2047 assert(kwl
->para
->private_data
);
2048 pdata
= (para_data
*) kwl
->para
->private_data
;
2050 dest
.page
= pdata
->first
->page
;
2054 * Shouldn't happen, but *shrug*
2061 if (dest
.type
!= NONE
) {
2063 (*xr
)->dest
= dest
; /* structure copy */
2064 if (page
->last_xref
)
2065 page
->last_xref
->next
= *xr
;
2067 page
->first_xref
= *xr
;
2068 page
->last_xref
= *xr
;
2072 * FIXME: Ideally we should have, and use, some
2073 * vertical font metric information here so that
2074 * our cross-ref rectangle can take account of
2075 * descenders and the font's cap height. This will
2076 * do for the moment, but it isn't ideal.
2078 (*xr
)->lx
= (*xr
)->rx
= x
;
2080 (*xr
)->ty
= y
+ ldata
->line_height
;
2085 * Finish extending a cross-reference box.
2093 * Add the current page number to the list of pages
2094 * referenced by an index entry.
2098 * We don't create index references in contents entries.
2100 if (!pdata
->contents_entry
) {
2104 tag
= index_findtag(idx
, text
->text
);
2108 for (i
= 0; i
< tag
->nrefs
; i
++) {
2109 indexentry
*entry
= tag
->refs
[i
];
2110 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
2113 * If the same index term is indexed twice
2114 * within the same section, we only want to
2115 * mention it once in the index.
2117 if (pi
->lastpage
!= page
) {
2121 pi
->lastword
= pi
->lastword
->next
=
2123 pi
->lastword
= pi
->lastword
->next
=
2125 wp
= &pi
->lastword
->next
;
2129 pi
->lastword
= *wp
=
2130 fake_page_ref(page
);
2131 pi
->lastword
= pi
->lastword
->next
=
2132 fake_word(page
->number
);
2133 pi
->lastword
= pi
->lastword
->next
=
2137 pi
->lastpage
= page
;
2143 style
= towordstyle(text
->type
);
2144 type
= removeattr(text
->type
);
2146 findex
= (style
== word_Normal ? FONT_NORMAL
:
2147 style
== word_Emph ? FONT_EMPH
:
2150 if (type
== word_Normal
) {
2152 } else if (type
== word_WhiteSpace
) {
2153 x
+= pdata
->sizes
[findex
] *
2154 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
2155 if (nspaces
&& findex
!= FONT_CODE
) {
2156 x
+= (*nspace
+1) * shortfall
/ nspaces
;
2157 x
-= *nspace
* shortfall
/ nspaces
;
2161 } else /* if (type == word_Quote) */ {
2162 if (text
->aux
== quote_Open
)
2168 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
2170 if (errs
&& text
->alt
)
2171 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
2172 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
,
2175 x
= render_string(page
, pdata
->fonts
[findex
],
2176 pdata
->sizes
[findex
], x
, y
, str
);
2189 * Returns the last x position used on the line.
2191 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
2192 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
2199 if (ldata
->aux_text
) {
2203 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2204 left_x
+ ldata
->aux_left_indent
,
2205 top_y
- ldata
->ypos
,
2206 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
2207 keywords
, idx
, conf
);
2208 if (ldata
->aux_text_2
)
2209 render_text(ldata
->page
, ldata
->pdata
, ldata
,
2210 x
, top_y
- ldata
->ypos
,
2211 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
2212 keywords
, idx
, conf
);
2218 * There might be a cross-reference carried over from a
2221 if (dest
->type
!= NONE
) {
2224 xr
->dest
= *dest
; /* structure copy */
2225 if (ldata
->page
->last_xref
)
2226 ldata
->page
->last_xref
->next
= xr
;
2228 ldata
->page
->first_xref
= xr
;
2229 ldata
->page
->last_xref
= xr
;
2230 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
2231 xr
->by
= top_y
- ldata
->ypos
;
2232 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
2237 int extra_indent
, shortfall
, spaces
;
2238 int just
= ldata
->pdata
->justification
;
2241 * All forms of justification become JUST when we have
2242 * to squeeze the paragraph.
2244 if (ldata
->hshortfall
< 0)
2249 shortfall
= ldata
->hshortfall
;
2250 spaces
= ldata
->nspaces
;
2254 shortfall
= spaces
= extra_indent
= 0;
2257 shortfall
= spaces
= 0;
2258 extra_indent
= ldata
->real_shortfall
;
2262 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2263 left_x
+ ldata
->xpos
+ extra_indent
,
2264 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
2265 &xr
, shortfall
, spaces
, &nspace
,
2266 keywords
, idx
, conf
);
2271 * There's a cross-reference continued on to the next line.
2281 static void render_para(para_data
*pdata
, paper_conf
*conf
,
2282 keywordlist
*keywords
, indexdata
*idx
,
2283 paragraph
*index_placeholder
, page_data
*index_page
)
2287 page_data
*cxref_page
;
2296 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
2298 * If this is a contents entry, we expect to have a single
2299 * enormous cross-reference rectangle covering the whole
2300 * thing. (Unless, of course, it spans multiple pages.)
2302 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
2303 cxref_page
= ldata
->page
;
2306 cxref
->dest
.type
= PAGE
;
2307 if (pdata
->contents_entry
== index_placeholder
) {
2308 cxref
->dest
.page
= index_page
;
2310 assert(pdata
->contents_entry
->private_data
);
2311 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2312 cxref
->dest
.page
= target
->first
->page
;
2314 cxref
->dest
.url
= NULL
;
2315 if (ldata
->page
->last_xref
)
2316 ldata
->page
->last_xref
->next
= cxref
;
2318 ldata
->page
->first_xref
= cxref
;
2319 ldata
->page
->last_xref
= cxref
;
2320 cxref
->lx
= conf
->left_margin
;
2321 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
2322 cxref
->ty
= conf
->paper_height
- conf
->top_margin
2323 - ldata
->ypos
+ ldata
->line_height
;
2325 if (pdata
->contents_entry
) {
2326 assert(cxref
!= NULL
);
2327 cxref
->by
= conf
->paper_height
- conf
->top_margin
2331 last_x
= render_line(ldata
, conf
->left_margin
,
2332 conf
->paper_height
- conf
->top_margin
,
2333 &dest
, keywords
, idx
, conf
);
2334 if (ldata
== pdata
->last
)
2339 * If this is a contents entry, add leaders and a page
2342 if (pdata
->contents_entry
) {
2348 if (pdata
->contents_entry
== index_placeholder
) {
2349 num
= index_page
->number
;
2351 assert(pdata
->contents_entry
->private_data
);
2352 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2353 num
= target
->first
->page
->number
;
2357 wid
= paper_width_simple(pdata
, w
, conf
);
2360 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2361 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2362 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2363 render_string(pdata
->last
->page
,
2364 pdata
->fonts
[FONT_NORMAL
],
2365 pdata
->sizes
[FONT_NORMAL
],
2366 conf
->left_margin
+ x
,
2367 (conf
->paper_height
- conf
->top_margin
-
2368 pdata
->last
->ypos
), L
".");
2370 render_string(pdata
->last
->page
,
2371 pdata
->fonts
[FONT_NORMAL
],
2372 pdata
->sizes
[FONT_NORMAL
],
2373 conf
->paper_width
- conf
->right_margin
- wid
,
2374 (conf
->paper_height
- conf
->top_margin
-
2375 pdata
->last
->ypos
), num
);
2379 * Render any rectangle (chapter title underline or rule)
2380 * that goes with this paragraph.
2382 switch (pdata
->rect_type
) {
2383 case RECT_CHAPTER_UNDERLINE
:
2384 add_rect_to_page(pdata
->last
->page
,
2386 (conf
->paper_height
- conf
->top_margin
-
2388 conf
->chapter_underline_depth
),
2390 conf
->chapter_underline_thickness
);
2393 add_rect_to_page(pdata
->first
->page
,
2394 conf
->left_margin
+ pdata
->first
->xpos
,
2395 (conf
->paper_height
- conf
->top_margin
-
2397 pdata
->last
->line_height
),
2398 conf
->base_width
- pdata
->first
->xpos
,
2399 pdata
->last
->line_height
);
2401 default: /* placate gcc */
2406 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2408 para_data
*pdata
= snew(para_data
);
2411 * For code paragraphs, I'm going to hack grievously and
2412 * pretend the three normal fonts are the three code paragraph
2415 setfont(pdata
, &conf
->fcode
);
2417 pdata
->first
= pdata
->last
= NULL
;
2418 pdata
->outline_level
= -1;
2419 pdata
->rect_type
= RECT_NONE
;
2420 pdata
->contents_entry
= NULL
;
2421 pdata
->justification
= LEFT
;
2423 for (; words
; words
= words
->next
) {
2424 wchar_t *t
, *e
, *start
;
2425 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2427 int prev
= -1, curr
;
2430 if (words
->next
&& words
->next
->type
== word_Emph
) {
2431 e
= words
->next
->text
;
2432 words
= words
->next
;
2442 else if (*e
== L
'i')
2444 else if (*e
== L
'b')
2461 * We've isolated a maximal subsequence of the line
2462 * which has the same emphasis. Form it into a word
2468 w
->type
= (prev
== 0 ? word_WeakCode
:
2469 prev
== 1 ? word_Emph
: word_Normal
);
2470 w
->text
= snewn(t
-start
+1, wchar_t);
2471 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2472 w
->text
[t
-start
] = '\0';
2485 ldata
= snew(line_data
);
2487 ldata
->pdata
= pdata
;
2488 ldata
->first
= lhead
;
2490 ldata
->line_height
= conf
->fcode
.font_size
* UNITS_PER_PT
;
2492 ldata
->xpos
= indent
;
2495 pdata
->last
->next
= ldata
;
2496 ldata
->prev
= pdata
->last
;
2498 pdata
->first
= ldata
;
2502 pdata
->last
= ldata
;
2504 ldata
->hshortfall
= 0;
2506 ldata
->aux_text
= NULL
;
2507 ldata
->aux_text_2
= NULL
;
2508 ldata
->aux_left_indent
= 0;
2509 /* General opprobrium for breaking in a code paragraph. */
2510 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2513 standard_line_spacing(pdata
, conf
);
2518 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2520 para_data
*pdata
= snew(para_data
);
2523 ldata
= snew(line_data
);
2525 ldata
->pdata
= pdata
;
2526 ldata
->first
= NULL
;
2528 ldata
->line_height
= conf
->rule_thickness
;
2530 ldata
->xpos
= indent
;
2535 ldata
->hshortfall
= 0;
2537 ldata
->aux_text
= NULL
;
2538 ldata
->aux_text_2
= NULL
;
2539 ldata
->aux_left_indent
= 0;
2542 * Better to break after a rule than before it
2544 ldata
->penalty_after
+= 100000;
2545 ldata
->penalty_before
+= -100000;
2547 pdata
->first
= pdata
->last
= ldata
;
2548 pdata
->outline_level
= -1;
2549 pdata
->rect_type
= RECT_RULE
;
2550 pdata
->contents_entry
= NULL
;
2551 pdata
->justification
= LEFT
;
2553 standard_line_spacing(pdata
, conf
);
2559 * Plain-text-like formatting for outline titles.
2561 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2562 for (; text
; text
= text
->next
) switch (text
->type
) {
2563 case word_HyperLink
:
2565 case word_UpperXref
:
2566 case word_LowerXref
:
2575 case word_WhiteSpace
:
2576 case word_EmphSpace
:
2577 case word_CodeSpace
:
2578 case word_WkCodeSpace
:
2580 case word_EmphQuote
:
2581 case word_CodeQuote
:
2582 case word_WkCodeQuote
:
2583 assert(text
->type
!= word_CodeQuote
&&
2584 text
->type
!= word_WkCodeQuote
);
2585 if (towordstyle(text
->type
) == word_Emph
&&
2586 (attraux(text
->aux
) == attr_First
||
2587 attraux(text
->aux
) == attr_Only
))
2588 rdadd(rs
, L
'_'); /* FIXME: configurability */
2589 else if (towordstyle(text
->type
) == word_Code
&&
2590 (attraux(text
->aux
) == attr_First
||
2591 attraux(text
->aux
) == attr_Only
))
2592 rdadd(rs
, L
'\''); /* FIXME: configurability */
2593 if (removeattr(text
->type
) == word_Normal
) {
2594 rdadds(rs
, text
->text
);
2595 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2597 } else if (removeattr(text
->type
) == word_Quote
) {
2598 rdadd(rs
, L
'\''); /* fixme: configurability */
2600 if (towordstyle(text
->type
) == word_Emph
&&
2601 (attraux(text
->aux
) == attr_Last
||
2602 attraux(text
->aux
) == attr_Only
))
2603 rdadd(rs
, L
'_'); /* FIXME: configurability */
2604 else if (towordstyle(text
->type
) == word_Code
&&
2605 (attraux(text
->aux
) == attr_Last
||
2606 attraux(text
->aux
) == attr_Only
))
2607 rdadd(rs
, L
'\''); /* FIXME: configurability */
2612 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2615 rdstring rs
= {0, 0, NULL
};
2618 paper_rdaddw(&rs
, first
);
2620 rdadds(&rs
, separator
);
2622 paper_rdaddw(&rs
, second
);
2627 static word
*fake_word(wchar_t *text
)
2629 word
*ret
= snew(word
);
2632 ret
->type
= word_Normal
;
2633 ret
->text
= ustrdup(text
);
2634 ret
->breaks
= FALSE
;
2639 static word
*fake_space_word(void)
2641 word
*ret
= snew(word
);
2644 ret
->type
= word_WhiteSpace
;
2651 static word
*fake_page_ref(page_data
*page
)
2653 word
*ret
= snew(word
);
2656 ret
->type
= word_PageXref
;
2658 ret
->breaks
= FALSE
;
2660 ret
->private_data
= page
;
2664 static word
*fake_end_ref(void)
2666 word
*ret
= snew(word
);
2669 ret
->type
= word_XrefEnd
;
2671 ret
->breaks
= FALSE
;
2676 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2685 w
= dup_word_list(first
);
2693 w
= fake_word(separator
);
2699 *wptr
= dup_word_list(second
);
2705 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2709 if (!src
->first_line
)
2712 if (dest
->last_line
) {
2713 dest
->last_line
->next
= src
->first_line
;
2714 src
->first_line
->prev
= dest
->last_line
;
2716 dest
->last_line
= src
->last_line
;
2718 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2720 ldata
->xpos
+= right_shift
;
2722 if (ldata
== src
->last_line
)