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 * - I'm uncertain of whether I need to include a ToUnicode CMap
18 * in each of my font definitions in PDF. Currently things (by
19 * which I mean cut and paste out of acroread) seem to be
20 * working fairly happily without it, but I don't know.
22 * - rather than the ugly aux_text mechanism for rendering chapter
23 * titles, we could actually build the correct word list and
26 * - get vertical font metrics and use them to position the PDF
27 * xref boxes more pleasantly
30 * * page header and footer should be configurable; we should
31 * be able to shift the page number elsewhere, and add other
32 * things such as the current chapter/section title and fixed
34 * * remove the fixed mapping from heading levels to heading
35 * styles; offer a menu of styles from which the user can
36 * choose at every heading level
37 * * first-line indent in paragraphs
38 * * fixed text: `Contents', `Index', the colon-space and full
39 * stop in chapter title constructions
40 * * configurable location of contents?
41 * * certainly configurably _remove_ the contents, and possibly
43 * * double-sided document switch?
44 * + means you have two header/footer formats which
46 * + and means that mandatory page breaks before chapter
47 * titles should include a blank page if necessary to
48 * start the next section to a right-hand page
52 * - ability to import other Type 1 fonts
53 * * we need to parse the font to extract its metrics
54 * * then we pass the font bodily to both PS and PDF so it can
55 * be included in the output file
57 * - character substitution for better typography?
58 * * fi, fl, ffi, ffl ligatures
59 * * use real ellipsis rather than ...
60 * * a hyphen in a word by itself might prefer to be an en-dash
61 * * (Americans might even want a convenient way to use an
63 * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw!
64 * * substituting `minus' for `hyphen' in the standard encoding
65 * is probably preferable in Courier, though certainly not in
67 * * if I do do this lot, I'm rather inclined to at least try
68 * to think up a configurable way to do it so that Americans
69 * can do em-dash tricks without my intervention and other
70 * people can do other odd things too.
81 typedef struct paper_conf_Tag paper_conf
;
82 typedef struct paper_idx_Tag paper_idx
;
85 font_data
*fonts
[NFONTS
];
89 struct paper_conf_Tag
{
96 int indent_list_bullet
;
97 int indent_list_after
;
101 int base_para_spacing
;
102 int chapter_top_space
;
103 int sect_num_left_space
;
104 int chapter_underline_depth
;
105 int chapter_underline_thickness
;
107 font_cfg fbase
, fcode
, ftitle
, fchapter
, *fsect
;
109 int contents_indent_step
;
111 int leader_separation
;
115 int pagenum_fontsize
;
117 wchar_t *lquote
, *rquote
, *bullet
;
118 wchar_t *contents_text
, *index_text
;
119 /* These are derived from the above */
125 struct paper_idx_Tag
{
127 * Word list giving the page numbers on which this index entry
128 * appears. Also the last word in the list, for ease of
134 * The last page added to the list (so we can ensure we don't
141 word_PageXref
= word_NotWordType
+ 1
144 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
145 static void wrap_paragraph(para_data
*pdata
, word
*words
,
146 int w
, int i1
, int i2
, paper_conf
*conf
);
147 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
148 int page_height
, int ncols
, int headspace
);
149 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
150 int x
, int y
, wchar_t *str
);
151 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
152 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
);
157 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
158 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
);
159 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
160 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
161 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
162 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
163 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
165 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
166 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
168 static word
*fake_word(wchar_t *text
);
169 static word
*fake_space_word(void);
170 static word
*fake_page_ref(page_data
*page
);
171 static word
*fake_end_ref(void);
172 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
174 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
176 static int fonts_ok(wchar_t *string
, ...)
182 va_start(ap
, string
);
183 while ( (font
= va_arg(ap
, font_data
*)) != NULL
) {
185 (void) string_width(font
, string
, &errs
);
196 static void paper_cfg_fonts(font_data
**fonts
, font_list
*fontlist
,
197 wchar_t *wp
, filepos
*fpos
) {
202 for (i
= 0; i
< NFONTS
&& *wp
; i
++, wp
= uadv(wp
)) {
203 fn
= utoa_dup(wp
, CS_ASCII
);
204 f
= make_std_font(fontlist
, fn
);
208 /* FIXME: proper error */
209 error(err_nofont
, fpos
, wp
);
213 static paper_conf
paper_configure(paragraph
*source
, font_list
*fontlist
) {
220 ret
.paper_width
= 595 * UNITS_PER_PT
;
221 ret
.paper_height
= 842 * UNITS_PER_PT
;
222 ret
.left_margin
= 72 * UNITS_PER_PT
;
223 ret
.top_margin
= 72 * UNITS_PER_PT
;
224 ret
.right_margin
= 72 * UNITS_PER_PT
;
225 ret
.bottom_margin
= 108 * UNITS_PER_PT
;
226 ret
.indent_list_bullet
= 6 * UNITS_PER_PT
;
227 ret
.indent_list_after
= 18 * UNITS_PER_PT
;
228 ret
.indent_quote
= 18 * UNITS_PER_PT
;
229 ret
.base_leading
= UNITS_PER_PT
;
230 ret
.base_para_spacing
= 10 * UNITS_PER_PT
;
231 ret
.chapter_top_space
= 72 * UNITS_PER_PT
;
232 ret
.sect_num_left_space
= 12 * UNITS_PER_PT
;
233 ret
.chapter_underline_depth
= 14 * UNITS_PER_PT
;
234 ret
.chapter_underline_thickness
= 3 * UNITS_PER_PT
;
235 ret
.rule_thickness
= 1 * UNITS_PER_PT
;
236 ret
.fbase
.font_size
= 12;
237 ret
.fbase
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Times-Roman");
238 ret
.fbase
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Times-Italic");
239 ret
.fbase
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
240 ret
.fcode
.font_size
= 12;
241 ret
.fcode
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Courier-Bold");
242 ret
.fcode
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Courier-Oblique");
243 ret
.fcode
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
244 ret
.ftitle
.font_size
= 24;
245 ret
.ftitle
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Helvetica-Bold");
246 ret
.ftitle
.fonts
[FONT_EMPH
] =
247 make_std_font(fontlist
, "Helvetica-BoldOblique");
248 ret
.ftitle
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
249 ret
.fchapter
.font_size
= 20;
250 ret
.fchapter
.fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
251 ret
.fchapter
.fonts
[FONT_EMPH
] =
252 make_std_font(fontlist
, "Helvetica-BoldOblique");
253 ret
.fchapter
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
255 ret
.fsect
= snewn(ret
.nfsect
, font_cfg
);
256 ret
.fsect
[0].font_size
= 16;
257 ret
.fsect
[0].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
258 ret
.fsect
[0].fonts
[FONT_EMPH
] =
259 make_std_font(fontlist
, "Helvetica-BoldOblique");
260 ret
.fsect
[0].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
261 ret
.fsect
[1].font_size
= 14;
262 ret
.fsect
[1].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
263 ret
.fsect
[1].fonts
[FONT_EMPH
] =
264 make_std_font(fontlist
, "Helvetica-BoldOblique");
265 ret
.fsect
[1].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
266 ret
.fsect
[2].font_size
= 13;
267 ret
.fsect
[2].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
268 ret
.fsect
[2].fonts
[FONT_EMPH
] =
269 make_std_font(fontlist
, "Helvetica-BoldOblique");
270 ret
.fsect
[2].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
271 ret
.contents_indent_step
= 24 * UNITS_PER_PT
;
272 ret
.contents_margin
= 84 * UNITS_PER_PT
;
273 ret
.leader_separation
= 12 * UNITS_PER_PT
;
274 ret
.index_gutter
= 36 * UNITS_PER_PT
;
276 ret
.index_minsep
= 18 * UNITS_PER_PT
;
277 ret
.pagenum_fontsize
= 12;
278 ret
.footer_distance
= 32 * UNITS_PER_PT
;
279 ret
.lquote
= L
"\x2018\0\x2019\0'\0'\0\0";
280 ret
.rquote
= uadv(ret
.lquote
);
281 ret
.bullet
= L
"\x2022\0-\0\0";
282 ret
.contents_text
= L
"Contents";
283 ret
.index_text
= L
"Index";
286 * Two-pass configuration so that we can pick up global config
287 * (e.g. `quotes') before having it overridden by specific
288 * config (`paper-quotes'), irrespective of the order in which
291 for (p
= source
; p
; p
= p
->next
) {
292 if (p
->type
== para_Config
) {
293 if (!ustricmp(p
->keyword
, L
"quotes")) {
294 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
295 ret
.lquote
= uadv(p
->keyword
);
296 ret
.rquote
= uadv(ret
.lquote
);
302 for (p
= source
; p
; p
= p
->next
) {
303 p
->private_data
= NULL
;
304 if (p
->type
== para_Config
) {
305 if (!ustricmp(p
->keyword
, L
"paper-quotes")) {
306 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
307 ret
.lquote
= uadv(p
->keyword
);
308 ret
.rquote
= uadv(ret
.lquote
);
310 } else if (!ustricmp(p
->keyword
, L
"contents")) {
311 ret
.contents_text
= uadv(p
->keyword
);
312 } else if (!ustricmp(p
->keyword
, L
"index")) {
313 ret
.index_text
= uadv(p
->keyword
);
314 } else if (!ustricmp(p
->keyword
, L
"paper-bullet")) {
315 ret
.bullet
= uadv(p
->keyword
);
316 } else if (!ustricmp(p
->keyword
, L
"paper-page-width")) {
318 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
319 } else if (!ustricmp(p
->keyword
, L
"paper-page-height")) {
321 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
322 } else if (!ustricmp(p
->keyword
, L
"paper-left-margin")) {
324 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
325 } else if (!ustricmp(p
->keyword
, L
"paper-top-margin")) {
327 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
328 } else if (!ustricmp(p
->keyword
, L
"paper-right-margin")) {
330 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
331 } else if (!ustricmp(p
->keyword
, L
"paper-bottom-margin")) {
333 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
334 } else if (!ustricmp(p
->keyword
, L
"paper-list-indent")) {
335 ret
.indent_list_bullet
=
336 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
337 } else if (!ustricmp(p
->keyword
, L
"paper-listitem-indent")) {
339 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
340 } else if (!ustricmp(p
->keyword
, L
"paper-quote-indent")) {
342 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
343 } else if (!ustricmp(p
->keyword
, L
"paper-base-leading")) {
345 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
346 } else if (!ustricmp(p
->keyword
, L
"paper-base-para-spacing")) {
347 ret
.base_para_spacing
=
348 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
349 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-top-space")) {
350 ret
.chapter_top_space
=
351 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
352 } else if (!ustricmp(p
->keyword
, L
"paper-sect-num-left-space")) {
353 ret
.sect_num_left_space
=
354 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
355 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-depth")) {
356 ret
.chapter_underline_depth
=
357 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
358 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-thickness")) {
359 ret
.chapter_underline_thickness
=
360 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
361 } else if (!ustricmp(p
->keyword
, L
"paper-rule-thickness")) {
363 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
364 } else if (!ustricmp(p
->keyword
, L
"paper-contents-indent-step")) {
365 ret
.contents_indent_step
=
366 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
367 } else if (!ustricmp(p
->keyword
, L
"paper-contents-margin")) {
368 ret
.contents_margin
=
369 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
370 } else if (!ustricmp(p
->keyword
, L
"paper-leader-separation")) {
371 ret
.leader_separation
=
372 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
373 } else if (!ustricmp(p
->keyword
, L
"paper-index-gutter")) {
375 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
376 } else if (!ustricmp(p
->keyword
, L
"paper-index-minsep")) {
378 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
379 } else if (!ustricmp(p
->keyword
, L
"paper-footer-distance")) {
380 ret
.footer_distance
=
381 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
382 } else if (!ustricmp(p
->keyword
, L
"paper-base-font-size")) {
383 ret
.fbase
.font_size
= utoi(uadv(p
->keyword
));
384 } else if (!ustricmp(p
->keyword
, L
"paper-index-columns")) {
385 ret
.index_cols
= utoi(uadv(p
->keyword
));
386 } else if (!ustricmp(p
->keyword
, L
"paper-pagenum-font-size")) {
387 ret
.pagenum_fontsize
= utoi(uadv(p
->keyword
));
388 } else if (!ustricmp(p
->keyword
, L
"paper-base-fonts")) {
389 paper_cfg_fonts(ret
.fbase
.fonts
, fontlist
, uadv(p
->keyword
),
391 } else if (!ustricmp(p
->keyword
, L
"paper-code-font-size")) {
392 ret
.fcode
.font_size
= utoi(uadv(p
->keyword
));
393 } else if (!ustricmp(p
->keyword
, L
"paper-code-fonts")) {
394 paper_cfg_fonts(ret
.fcode
.fonts
, fontlist
, uadv(p
->keyword
),
396 } else if (!ustricmp(p
->keyword
, L
"paper-title-font-size")) {
397 ret
.ftitle
.font_size
= utoi(uadv(p
->keyword
));
398 } else if (!ustricmp(p
->keyword
, L
"paper-title-fonts")) {
399 paper_cfg_fonts(ret
.ftitle
.fonts
, fontlist
, uadv(p
->keyword
),
401 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-font-size")) {
402 ret
.fchapter
.font_size
= utoi(uadv(p
->keyword
));
403 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-fonts")) {
404 paper_cfg_fonts(ret
.fchapter
.fonts
, fontlist
, uadv(p
->keyword
),
406 } else if (!ustricmp(p
->keyword
, L
"paper-section-font-size")) {
407 wchar_t *q
= uadv(p
->keyword
);
413 if (n
>= ret
.nfsect
) {
415 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
416 for (i
= ret
.nfsect
; i
<= n
; i
++)
417 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
420 ret
.fsect
[n
].font_size
= utoi(q
);
421 } else if (!ustricmp(p
->keyword
, L
"paper-section-fonts")) {
422 wchar_t *q
= uadv(p
->keyword
);
428 if (n
>= ret
.nfsect
) {
430 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
431 for (i
= ret
.nfsect
; i
<= n
; i
++)
432 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
435 paper_cfg_fonts(ret
.fsect
[n
].fonts
, fontlist
, q
, &p
->fpos
);
441 * Set up the derived fields in the conf structure.
445 ret
.paper_width
- ret
.left_margin
- ret
.right_margin
;
447 ret
.paper_height
- ret
.top_margin
- ret
.bottom_margin
;
448 ret
.indent_list
= ret
.indent_list_bullet
+ ret
.indent_list_after
;
450 (ret
.base_width
- (ret
.index_cols
-1) * ret
.index_gutter
)
454 * Now process fallbacks on quote characters and bullets. We
455 * use string_width() to determine whether all of the relevant
456 * fonts contain the same character, and fall back whenever we
457 * find a character which not all of them support.
460 /* Quote characters need not be supported in the fixed code fonts,
461 * but must be in the title and body fonts. */
462 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
))) {
464 if (fonts_ok(ret
.lquote
,
465 ret
.fbase
.fonts
[FONT_NORMAL
],
466 ret
.fbase
.fonts
[FONT_EMPH
],
467 ret
.ftitle
.fonts
[FONT_NORMAL
],
468 ret
.ftitle
.fonts
[FONT_EMPH
],
469 ret
.fchapter
.fonts
[FONT_NORMAL
],
470 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
) &&
472 ret
.fbase
.fonts
[FONT_NORMAL
],
473 ret
.fbase
.fonts
[FONT_EMPH
],
474 ret
.ftitle
.fonts
[FONT_NORMAL
],
475 ret
.ftitle
.fonts
[FONT_EMPH
],
476 ret
.fchapter
.fonts
[FONT_NORMAL
],
477 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
)) {
478 for (n
= 0; n
< ret
.nfsect
; n
++)
479 if (!fonts_ok(ret
.lquote
,
480 ret
.fsect
[n
].fonts
[FONT_NORMAL
],
481 ret
.fsect
[n
].fonts
[FONT_EMPH
], NULL
) ||
482 !fonts_ok(ret
.rquote
,
483 ret
.fsect
[n
].fonts
[FONT_NORMAL
],
484 ret
.fsect
[n
].fonts
[FONT_EMPH
], NULL
))
489 ret
.lquote
= uadv(ret
.rquote
);
490 ret
.rquote
= uadv(ret
.lquote
);
493 /* The bullet character only needs to be supported in the normal body
494 * font (not even in italics). */
495 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
496 !fonts_ok(ret
.bullet
, ret
.fbase
.fonts
[FONT_NORMAL
], NULL
))
497 ret
.bullet
= uadv(ret
.bullet
);
502 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
506 int indent
, used_contents
;
507 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
508 para_data
*firstcont
, *lastcont
;
509 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
512 paper_conf
*conf
, ourconf
;
515 paragraph index_placeholder_para
;
516 page_data
*first_index_page
;
519 fontlist
= snew(font_list
);
520 fontlist
->head
= fontlist
->tail
= NULL
;
522 ourconf
= paper_configure(sourceform
, fontlist
);
526 * Set up a data structure to collect page numbers for each
535 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
536 paper_idx
*pi
= snew(paper_idx
);
540 pi
->words
= pi
->lastword
= NULL
;
543 entry
->backend_data
= pi
;
548 * Format the contents entry for each heading.
551 word
*contents_title
;
552 contents_title
= fake_word(conf
->contents_text
);
554 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
555 NULL
, NULL
, contents_title
, conf
);
556 lastcont
= firstcont
;
557 lastcont
->next
= NULL
;
558 firstcontline
= firstcont
->first
;
559 lastcontline
= lastcont
->last
;
560 for (p
= sourceform
; p
; p
= p
->next
) {
567 case para_UnnumberedChapter
:
573 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
576 case para_UnnumberedChapter
:
577 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
582 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
583 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
586 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
587 conf
->contents_margin
,
588 NULL
, NULL
, words
, conf
);
590 pdata
->contents_entry
= p
;
591 lastcont
->next
= pdata
;
595 * Link all contents line structures together into
600 lastcontline
->next
= pdata
->first
;
601 pdata
->first
->prev
= lastcontline
;
603 firstcontline
= pdata
->first
;
604 pdata
->first
->prev
= NULL
;
606 lastcontline
= pdata
->last
;
607 lastcontline
->next
= NULL
;
615 * And one extra one, for the index.
618 pdata
= make_para_data(para_Normal
, 0, 0,
619 conf
->contents_margin
,
621 fake_word(conf
->index_text
), conf
);
623 pdata
->contents_entry
= &index_placeholder_para
;
624 lastcont
->next
= pdata
;
629 lastcontline
->next
= pdata
->first
;
630 pdata
->first
->prev
= lastcontline
;
632 firstcontline
= pdata
->first
;
633 pdata
->first
->prev
= NULL
;
635 lastcontline
= pdata
->last
;
636 lastcontline
->next
= NULL
;
642 * Do the main paragraph formatting.
645 used_contents
= FALSE
;
646 firstline
= lastline
= NULL
;
647 for (p
= sourceform
; p
; p
= p
->next
) {
648 p
->private_data
= NULL
;
652 * These paragraph types are either invisible or don't
653 * define text in the normal sense. Either way, they
654 * don't require wrapping.
659 case para_NotParaType
:
666 * These paragraph types don't require wrapping, but
667 * they do affect the line width to which we wrap the
668 * rest of the paragraphs, so we need to pay attention.
671 indent
+= conf
->indent_list
; break;
673 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
675 indent
+= conf
->indent_quote
; break;
677 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
680 * This paragraph type is special. Process it
684 pdata
= code_paragraph(indent
, p
->words
, conf
);
685 p
->private_data
= pdata
;
686 if (pdata
->first
!= pdata
->last
) {
687 pdata
->first
->penalty_after
+= 100000;
688 pdata
->last
->penalty_before
+= 100000;
693 * This paragraph is also special.
696 pdata
= rule_paragraph(indent
, conf
);
697 p
->private_data
= pdata
;
701 * All of these paragraph types require wrapping in the
702 * ordinary way. So we must supply a set of fonts, a
703 * line width and auxiliary information (e.g. bullet
704 * text) for each one.
708 case para_UnnumberedChapter
:
712 case para_BiblioCited
:
714 case para_NumberedList
:
715 case para_DescribedThing
:
716 case para_Description
:
719 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
720 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
722 p
->private_data
= pdata
;
727 if (p
->private_data
) {
728 pdata
= (para_data
*)p
->private_data
;
731 * If this is the first non-title heading, we link the
732 * contents section in before it.
734 if (!used_contents
&& pdata
->outline_level
> 0) {
735 used_contents
= TRUE
;
737 lastpara
->next
= firstcont
;
739 firstpara
= firstcont
;
741 assert(lastpara
->next
== NULL
);
744 lastline
->next
= firstcontline
;
745 firstcontline
->prev
= lastline
;
747 firstline
= firstcontline
;
748 firstcontline
->prev
= NULL
;
750 assert(lastcontline
!= NULL
);
751 lastline
= lastcontline
;
752 lastline
->next
= NULL
;
756 * Link all line structures together into a big list.
760 lastline
->next
= pdata
->first
;
761 pdata
->first
->prev
= lastline
;
763 firstline
= pdata
->first
;
764 pdata
->first
->prev
= NULL
;
766 lastline
= pdata
->last
;
767 lastline
->next
= NULL
;
771 * Link all paragraph structures together similarly.
775 lastpara
->next
= pdata
;
783 * Now we have an enormous linked list of every line of text in
784 * the document. Break it up into pages.
786 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
797 for (page
= pages
; page
; page
= page
->next
) {
798 sprintf(buf
, "%d", ++pagenum
);
799 page
->number
= ufroma_dup(buf
, CS_ASCII
);
803 first_index_page
= snew(page_data
);
804 first_index_page
->next
= first_index_page
->prev
= NULL
;
805 first_index_page
->first_line
= NULL
;
806 first_index_page
->last_line
= NULL
;
807 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
808 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
809 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
811 /* And don't forget the as-yet-uncreated index. */
812 sprintf(buf
, "%d", ++pagenum
);
813 first_index_page
->number
= ufroma_dup(buf
, CS_ASCII
);
818 * Now we're ready to actually lay out the pages. We do this by
819 * looping over _paragraphs_, since we may need to track cross-
820 * references between lines and even across pages.
822 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
823 render_para(pdata
, conf
, keywords
, idx
,
824 &index_placeholder_para
, first_index_page
);
827 * Now we've laid out the main body pages, we should have
828 * acquired a full set of page numbers for the index.
834 para_data
*firstidx
, *lastidx
;
835 line_data
*firstidxline
, *lastidxline
, *ldata
;
836 page_data
*ipages
, *ipages2
, *page
;
839 * Create a set of paragraphs for the index.
841 index_title
= fake_word(conf
->index_text
);
843 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
844 NULL
, NULL
, index_title
, conf
);
846 lastidx
->next
= NULL
;
847 firstidxline
= firstidx
->first
;
848 lastidxline
= lastidx
->last
;
849 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
850 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
851 para_data
*text
, *pages
;
856 text
= make_para_data(para_Normal
, 0, 0,
857 conf
->base_width
- conf
->index_colwidth
,
858 NULL
, NULL
, entry
->text
, conf
);
860 pages
= make_para_data(para_Normal
, 0, 0,
861 conf
->base_width
- conf
->index_colwidth
,
862 NULL
, NULL
, pi
->words
, conf
);
864 text
->justification
= LEFT
;
865 pages
->justification
= RIGHT
;
866 text
->last
->space_after
= pages
->first
->space_before
=
867 conf
->base_leading
/ 2;
869 pages
->last
->space_after
= text
->first
->space_before
=
873 assert(pages
->first
);
878 * If feasible, fold the two halves of the index entry
881 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
882 conf
->index_colwidth
+ conf
->index_minsep
) {
883 text
->last
->space_after
= -1;
884 pages
->first
->space_before
= -pages
->first
->line_height
+1;
887 lastidx
->next
= text
;
893 * Link all index line structures together into
896 text
->last
->next
= pages
->first
;
897 pages
->first
->prev
= text
->last
;
899 lastidxline
->next
= text
->first
;
900 text
->first
->prev
= lastidxline
;
902 lastidxline
= pages
->last
;
905 * Breaking an index entry anywhere is so bad that I
906 * think I'm going to forbid it totally.
908 for (ldata
= text
->first
; ldata
&& ldata
->next
;
909 ldata
= ldata
->next
) {
910 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
911 ldata
->space_after
= -1;
916 * Now break the index into pages.
918 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
920 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
923 firstidxline
->space_before
+
924 firstidxline
->line_height
+
925 firstidxline
->space_after
);
928 * This will have put each _column_ of the index on a
929 * separate page, which isn't what we want. Fold the pages
936 for (i
= 1; i
< conf
->index_cols
; i
++)
940 fold_into_page(page
, page
->next
,
941 i
* (conf
->index_colwidth
+
942 conf
->index_gutter
));
944 page
->next
= page
->next
->next
;
946 page
->next
->prev
= page
;
952 /* Also fold the heading on to the same page as the index items. */
953 fold_into_page(ipages
, ipages2
, 0);
954 ipages
->next
= ipages2
->next
;
956 ipages
->next
->prev
= ipages
;
958 fold_into_page(first_index_page
, ipages
, 0);
959 first_index_page
->next
= ipages
->next
;
960 if (first_index_page
->next
)
961 first_index_page
->next
->prev
= first_index_page
;
963 ipages
= first_index_page
;
966 * Number the index pages, except the already-numbered
969 for (page
= ipages
->next
; page
; page
= page
->next
) {
971 sprintf(buf
, "%d", ++pagenum
);
972 page
->number
= ufroma_dup(buf
, CS_ASCII
);
976 * Render the index pages.
978 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
979 render_para(pdata
, conf
, keywords
, idx
,
980 &index_placeholder_para
, first_index_page
);
983 * Link the index page list on to the end of the main page
989 for (page
= pages
; page
->next
; page
= page
->next
);
994 * Same with the paragraph list, which will cause the index
995 * to be mentioned in the document outline.
998 firstpara
= firstidx
;
1000 lastpara
->next
= firstidx
;
1005 * Draw the headers and footers.
1007 * FIXME: this should be fully configurable, but for the moment
1008 * I'm just going to put in page numbers in the centre of a
1009 * footer and leave it at that.
1014 for (page
= pages
; page
; page
= page
->next
) {
1017 width
= conf
->pagenum_fontsize
*
1018 string_width(conf
->fbase
.fonts
[FONT_NORMAL
], page
->number
,
1021 render_string(page
, conf
->fbase
.fonts
[FONT_NORMAL
],
1022 conf
->pagenum_fontsize
,
1023 conf
->left_margin
+ (conf
->base_width
- width
)/2,
1024 conf
->bottom_margin
- conf
->footer_distance
,
1030 * Start putting together the overall document structure we're
1033 doc
= snew(document
);
1034 doc
->fonts
= fontlist
;
1036 doc
->paper_width
= conf
->paper_width
;
1037 doc
->paper_height
= conf
->paper_height
;
1040 * Collect the section heading paragraphs into a document
1041 * outline. This is slightly fiddly because the Title paragraph
1042 * isn't required to be at the start, although all the others
1048 doc
->outline_elements
= snewn(osize
, outline_element
);
1049 doc
->n_outline_elements
= 0;
1051 /* First find the title. */
1052 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1053 if (pdata
->outline_level
== 0) {
1054 doc
->outline_elements
[0].level
= 0;
1055 doc
->outline_elements
[0].pdata
= pdata
;
1056 doc
->n_outline_elements
++;
1061 /* Then collect the rest. */
1062 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1063 if (pdata
->outline_level
> 0) {
1064 if (doc
->n_outline_elements
>= osize
) {
1066 doc
->outline_elements
=
1067 sresize(doc
->outline_elements
, osize
, outline_element
);
1070 doc
->outline_elements
[doc
->n_outline_elements
].level
=
1071 pdata
->outline_level
;
1072 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
1073 doc
->n_outline_elements
++;
1081 static void setfont(para_data
*p
, font_cfg
*f
) {
1084 for (i
= 0; i
< NFONTS
; i
++) {
1085 p
->fonts
[i
] = f
->fonts
[i
];
1086 p
->sizes
[i
] = f
->font_size
;
1090 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
1091 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
1096 int extra_indent
, firstline_indent
, aux_indent
;
1099 pdata
= snew(para_data
);
1100 pdata
->outline_level
= -1;
1101 pdata
->outline_title
= NULL
;
1102 pdata
->rect_type
= RECT_NONE
;
1103 pdata
->contents_entry
= NULL
;
1104 pdata
->justification
= JUST
;
1107 * Choose fonts for this paragraph.
1111 setfont(pdata
, &conf
->ftitle
);
1112 pdata
->outline_level
= 0;
1117 case para_UnnumberedChapter
:
1118 setfont(pdata
, &conf
->fchapter
);
1119 pdata
->outline_level
= 1;
1125 &conf
->fsect
[paux
>= conf
->nfsect ? conf
->nfsect
- 1 : paux
]);
1126 pdata
->outline_level
= 2 + paux
;
1130 case para_BiblioCited
:
1132 case para_NumberedList
:
1133 case para_DescribedThing
:
1134 case para_Description
:
1135 case para_Copyright
:
1136 setfont(pdata
, &conf
->fbase
);
1141 * Also select an indentation level depending on the
1142 * paragraph type (list paragraphs other than
1143 * para_DescribedThing need extra indent).
1145 * (FIXME: Perhaps at some point we might even arrange
1146 * for the user to be able to request indented first
1147 * lines in paragraphs.)
1149 if (ptype
== para_Bullet
||
1150 ptype
== para_NumberedList
||
1151 ptype
== para_Description
) {
1152 extra_indent
= firstline_indent
= conf
->indent_list
;
1154 extra_indent
= firstline_indent
= 0;
1158 * Find the auxiliary text for this paragraph.
1169 * For some heading styles (FIXME: be able to
1170 * configure which), the auxiliary text contains
1171 * the chapter number and is arranged to be
1172 * right-aligned a few points left of the primary
1173 * margin. For other styles, the auxiliary text is
1174 * the full chapter _name_ and takes up space
1175 * within the (wrapped) chapter title, meaning that
1176 * we must move the first line indent over to make
1179 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
1183 len
= paper_width_simple(pdata
, pkwtext2
, conf
);
1184 aux_indent
= -len
- conf
->sect_num_left_space
;
1186 pdata
->outline_title
=
1187 prepare_outline_title(pkwtext2
, L
" ", pwords
);
1190 aux2
= fake_word(L
": ");
1193 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1194 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1196 pdata
->outline_title
=
1197 prepare_outline_title(pkwtext
, L
": ", pwords
);
1203 * Auxiliary text consisting of a bullet.
1205 aux
= fake_word(conf
->bullet
);
1206 aux_indent
= indent
+ conf
->indent_list_bullet
;
1209 case para_NumberedList
:
1211 * Auxiliary text consisting of the number followed
1212 * by a (FIXME: configurable) full stop.
1215 aux2
= fake_word(L
".");
1216 aux_indent
= indent
+ conf
->indent_list_bullet
;
1219 case para_BiblioCited
:
1221 * Auxiliary text consisting of the bibliography
1222 * reference text, and a trailing space.
1225 aux2
= fake_word(L
" ");
1226 aux_indent
= indent
;
1227 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1228 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1232 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
1233 pdata
->outline_title
=
1234 prepare_outline_title(NULL
, NULL
, pwords
);
1237 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
1238 indent
+ firstline_indent
,
1239 indent
+ extra_indent
, conf
);
1241 pdata
->first
->aux_text
= aux
;
1242 pdata
->first
->aux_text_2
= aux2
;
1243 pdata
->first
->aux_left_indent
= aux_indent
;
1246 * Line breaking penalties.
1253 case para_UnnumberedChapter
:
1255 * Fixed and large penalty for breaking straight
1256 * after a heading; corresponding bonus for
1257 * breaking straight before.
1259 pdata
->first
->penalty_before
= -500000;
1260 pdata
->last
->penalty_after
= 500000;
1261 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
1262 ldata
->penalty_after
= 500000;
1265 case para_DescribedThing
:
1267 * This is treated a bit like a small heading:
1268 * there's a penalty for breaking after it (i.e.
1269 * between it and its description), and a bonus for
1270 * breaking before it (actually _between_ list
1273 pdata
->first
->penalty_before
= -200000;
1274 pdata
->last
->penalty_after
= 200000;
1279 * Most paragraph types: widow/orphan control by
1280 * discouraging breaking one line from the end of
1283 if (pdata
->first
!= pdata
->last
) {
1284 pdata
->first
->penalty_after
= 100000;
1285 pdata
->last
->penalty_before
= 100000;
1290 standard_line_spacing(pdata
, conf
);
1293 * Some kinds of section heading require a page break before
1294 * them and an underline after.
1296 if (ptype
== para_Title
||
1297 ptype
== para_Chapter
||
1298 ptype
== para_Appendix
||
1299 ptype
== para_UnnumberedChapter
) {
1300 pdata
->first
->page_break
= TRUE
;
1301 pdata
->first
->space_before
= conf
->chapter_top_space
;
1302 pdata
->last
->space_after
+=
1303 (conf
->chapter_underline_depth
+
1304 conf
->chapter_underline_thickness
);
1305 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1311 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1316 * Set the line spacing for each line in this paragraph.
1318 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1319 if (ldata
== pdata
->first
)
1320 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1322 ldata
->space_before
= conf
->base_leading
/ 2;
1323 if (ldata
== pdata
->last
)
1324 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1326 ldata
->space_after
= conf
->base_leading
/ 2;
1327 ldata
->page_break
= FALSE
;
1331 static font_encoding
*new_font_encoding(font_data
*font
)
1336 fe
= snew(font_encoding
);
1339 if (font
->list
->tail
)
1340 font
->list
->tail
->next
= fe
;
1342 font
->list
->head
= fe
;
1343 font
->list
->tail
= fe
;
1346 fe
->free_pos
= 0x21;
1348 for (i
= 0; i
< 256; i
++) {
1349 fe
->vector
[i
] = NULL
;
1350 fe
->indices
[i
] = -1;
1351 fe
->to_unicode
[i
] = 0xFFFF;
1357 int kern_cmp(void *a
, void *b
)
1359 kern_pair
const *ka
= a
, *kb
= b
;
1361 if (ka
->left
< kb
->left
)
1363 if (ka
->left
> kb
->left
)
1365 if (ka
->right
< kb
->right
)
1367 if (ka
->right
> kb
->right
)
1372 /* This wouldn't be necessary if C had closures. */
1373 static font_info
*glyph_cmp_fi
;
1375 static int glyph_cmp(void const *a
, void const *b
)
1377 return strcmp(glyph_cmp_fi
->glyphs
[*(unsigned short *)a
],
1378 glyph_cmp_fi
->glyphs
[*(unsigned short *)b
]);
1382 * Set up the glyphsbyname index for a font.
1384 void font_index_glyphs(font_info
*fi
) {
1387 fi
->glyphsbyname
= snewn(fi
->nglyphs
, unsigned short);
1388 for (i
= 0; i
< fi
->nglyphs
; i
++)
1389 fi
->glyphsbyname
[i
] = i
;
1391 qsort(fi
->glyphsbyname
, fi
->nglyphs
, sizeof(fi
->glyphsbyname
[0]),
1395 int find_glyph(font_info
*fi
, char const *name
) {
1402 r
= strcmp(fi
->glyphs
[fi
->glyphsbyname
[k
]], name
);
1404 return fi
->glyphsbyname
[k
];
1413 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1416 font_info
const *fi
;
1421 for (fe
= fontlist
->head
; fe
; fe
= fe
->next
)
1422 if (strcmp(fe
->font
->info
->name
, name
) == 0)
1425 for (fi
= all_fonts
; fi
; fi
= fi
->next
)
1426 if (strcmp(fi
->name
, name
) == 0) break;
1427 if (!fi
) return NULL
;
1429 f
= snew(font_data
);
1433 nglyphs
= f
->info
->nglyphs
;
1434 f
->subfont_map
= snewn(nglyphs
, subfont_map_entry
);
1437 * Our first subfont will contain all of US-ASCII. This isn't
1438 * really necessary - we could just create custom subfonts
1439 * precisely as the whim of render_string dictated - but
1440 * instinct suggests that it might be nice to have the text in
1441 * the output files look _marginally_ recognisable.
1443 fe
= new_font_encoding(f
);
1444 fe
->free_pos
= 0xA1; /* only the top half is free */
1445 f
->latest_subfont
= fe
;
1447 for (i
= 0; i
< nglyphs
; i
++) {
1449 ucs
= ps_glyph_to_unicode(f
->info
->glyphs
[i
]);
1450 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1451 fe
->vector
[ucs
] = f
->info
->glyphs
[i
];
1452 fe
->indices
[ucs
] = i
;
1453 fe
->to_unicode
[ucs
] = ucs
;
1454 f
->subfont_map
[i
].subfont
= fe
;
1455 f
->subfont_map
[i
].position
= ucs
;
1458 * This character is not yet assigned to a subfont.
1460 f
->subfont_map
[i
].subfont
= NULL
;
1461 f
->subfont_map
[i
].position
= 0;
1468 /* NB: arguments are glyph numbers from font->bmp. */
1469 static int find_kern(font_data
*font
, int lindex
, int rindex
)
1472 kern_pair
const *kp
;
1474 if (lindex
== 0xFFFF || rindex
== 0xFFFF)
1476 wantkp
.left
= lindex
;
1477 wantkp
.right
= rindex
;
1478 kp
= find234(font
->info
->kerns
, &wantkp
, NULL
);
1484 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1493 for (; *string
; string
++) {
1494 index
= (*string
< 0 || *string
> 0xFFFF ?
0xFFFF :
1495 font
->info
->bmp
[*string
]);
1497 if (index
== 0xFFFF) {
1501 width
+= find_kern(font
, oindex
, index
) +
1502 font
->info
->widths
[index
];
1510 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1512 struct paper_width_ctx
{
1518 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1520 while (text
&& text
!= end
) {
1521 w
+= paper_width_internal(vctx
, text
, nspaces
);
1527 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1529 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1530 int style
, type
, findex
, width
, errs
;
1533 switch (word
->type
) {
1534 case word_HyperLink
:
1536 case word_UpperXref
:
1537 case word_LowerXref
:
1544 style
= towordstyle(word
->type
);
1545 type
= removeattr(word
->type
);
1547 findex
= (style
== word_Normal ? FONT_NORMAL
:
1548 style
== word_Emph ? FONT_EMPH
:
1551 if (type
== word_Normal
) {
1553 } else if (type
== word_WhiteSpace
) {
1554 if (findex
!= FONT_CODE
) {
1557 return ctx
->minspacewidth
;
1560 } else /* if (type == word_Quote) */ {
1561 if (word
->aux
== quote_Open
)
1562 str
= ctx
->conf
->lquote
;
1564 str
= ctx
->conf
->rquote
;
1567 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1569 if (errs
&& word
->alt
)
1570 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1572 return ctx
->pdata
->sizes
[findex
] * width
;
1575 static int paper_width(void *vctx
, word
*word
)
1577 return paper_width_internal(vctx
, word
, NULL
);
1580 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
)
1582 struct paper_width_ctx ctx
;
1586 (pdata
->sizes
[FONT_NORMAL
] *
1587 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1590 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1593 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1594 int w
, int i1
, int i2
, paper_conf
*conf
)
1596 wrappedline
*wrapping
, *p
;
1598 struct paper_width_ctx ctx
;
1602 * We're going to need to store the line height in every line
1603 * structure we generate.
1608 for (i
= 0; i
< NFONTS
; i
++)
1609 if (line_height
< pdata
->sizes
[i
])
1610 line_height
= pdata
->sizes
[i
];
1611 line_height
*= UNITS_PER_PT
;
1614 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1615 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1616 if (spacewidth
== 0) {
1618 * A font without a space?! Disturbing. I hope this never
1619 * comes up, but I'll make a random guess anyway and set my
1620 * space width to half the point size.
1622 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * UNITS_PER_PT
/ 2;
1626 * I'm going to set the _minimum_ space width to 3/5 of the
1627 * standard one, and use the standard one as the optimum.
1629 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1633 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1636 * Having done the wrapping, we now concoct a set of line_data
1639 pdata
->first
= pdata
->last
= NULL
;
1641 for (p
= wrapping
; p
; p
= p
->next
) {
1644 int len
, wid
, spaces
;
1646 ldata
= snew(line_data
);
1648 ldata
->pdata
= pdata
;
1649 ldata
->first
= p
->begin
;
1650 ldata
->end
= p
->end
;
1651 ldata
->line_height
= line_height
;
1653 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1656 pdata
->last
->next
= ldata
;
1657 ldata
->prev
= pdata
->last
;
1659 pdata
->first
= ldata
;
1663 pdata
->last
= ldata
;
1666 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1667 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1670 ldata
->hshortfall
= wid
- len
;
1671 ldata
->nspaces
= spaces
;
1673 * This tells us how much the space width needs to
1674 * change from _min_spacewidth. But we want to store
1675 * its difference from the _natural_ space width, to
1676 * make the text rendering easier.
1678 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1679 ldata
->hshortfall
-= spacewidth
* spaces
;
1680 ldata
->real_shortfall
= ldata
->hshortfall
;
1682 * Special case: on the last line of a paragraph, we
1683 * never stretch spaces.
1685 if (ldata
->hshortfall
> 0 && !p
->next
)
1686 ldata
->hshortfall
= 0;
1688 ldata
->aux_text
= NULL
;
1689 ldata
->aux_text_2
= NULL
;
1690 ldata
->aux_left_indent
= 0;
1691 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1696 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1697 int page_height
, int ncols
, int headspace
)
1701 int n
, n1
, this_height
;
1704 * Page breaking is done by a close analogue of the optimal
1705 * paragraph wrapping algorithm used by wrap_para(). We work
1706 * backwards from the end of the document line by line; for
1707 * each line, we contemplate every possible number of lines we
1708 * could put on a page starting with that line, determine a
1709 * cost function for each one, add it to the pre-computed cost
1710 * function for optimally page-breaking everything after that
1711 * page, and pick the best option.
1713 * This is made slightly more complex by the fact that we have
1714 * a multi-column index with a heading at the top of the
1715 * _first_ page, meaning that the first _ncols_ pages must have
1716 * a different length. Hence, we must do the wrapping ncols+1
1717 * times over, hypothetically trying to put every subsequence
1718 * on every possible page.
1720 * Since my line_data structures are only used for this
1721 * purpose, I might as well just store the algorithm data
1725 for (l
= last
; l
; l
= l
->prev
) {
1726 l
->bestcost
= snewn(ncols
+1, int);
1727 l
->vshortfall
= snewn(ncols
+1, int);
1728 l
->text
= snewn(ncols
+1, int);
1729 l
->space
= snewn(ncols
+1, int);
1730 l
->page_last
= snewn(ncols
+1, line_data
*);
1732 for (n
= 0; n
<= ncols
; n
++) {
1733 int minheight
, text
= 0, space
= 0;
1736 n1
= (n
< ncols ? n
+1 : ncols
);
1738 this_height
= page_height
- headspace
;
1740 this_height
= page_height
;
1742 l
->bestcost
[n
] = -1;
1743 for (m
= l
; m
; m
= m
->next
) {
1744 if (m
!= l
&& m
->page_break
)
1745 break; /* we've gone as far as we can */
1748 if (m
->prev
->space_after
> 0)
1749 space
+= m
->prev
->space_after
;
1751 text
+= m
->prev
->space_after
;
1753 if (m
!= l
|| m
->page_break
) {
1754 if (m
->space_before
> 0)
1755 space
+= m
->space_before
;
1757 text
+= m
->space_before
;
1759 text
+= m
->line_height
;
1760 minheight
= text
+ space
;
1762 if (m
!= l
&& minheight
> this_height
)
1766 * If the space after this paragraph is _negative_
1767 * (which means the next line is folded on to this
1768 * one, which happens in the index), we absolutely
1769 * cannot break here.
1771 if (m
->space_after
>= 0) {
1774 * Compute the cost of this arrangement, as the
1775 * square of the amount of wasted space on the
1776 * page. Exception: if this is the last page
1777 * before a mandatory break or the document
1778 * end, we don't penalise a large blank area.
1780 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1782 int x
= (this_height
- minheight
) / FUNITS_PER_PT
*
1790 cost
+= (x
* xf
) >> 8;
1794 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1795 cost
+= m
->penalty_after
;
1796 cost
+= m
->next
->penalty_before
;
1799 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1800 cost
+= m
->next
->bestcost
[n1
];
1801 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1803 * This is the best option yet for this
1806 l
->bestcost
[n
] = cost
;
1807 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1808 l
->vshortfall
[n
] = this_height
- minheight
;
1810 l
->vshortfall
[n
] = 0;
1812 l
->space
[n
] = space
;
1813 l
->page_last
[n
] = m
;
1824 * Now go through the line list forwards and assemble the
1833 int text
, space
, head
;
1835 page
= snew(page_data
);
1844 page
->first_line
= l
;
1845 page
->last_line
= l
->page_last
[n
];
1847 page
->first_text
= page
->last_text
= NULL
;
1848 page
->first_xref
= page
->last_xref
= NULL
;
1849 page
->first_rect
= page
->last_rect
= NULL
;
1852 * Now assign a y-coordinate to each line on the page.
1855 head
= (n
< ncols ? headspace
: 0);
1856 for (l
= page
->first_line
; l
; l
= l
->next
) {
1857 if (l
!= page
->first_line
) {
1858 if (l
->prev
->space_after
> 0)
1859 space
+= l
->prev
->space_after
;
1861 text
+= l
->prev
->space_after
;
1863 if (l
!= page
->first_line
|| l
->page_break
) {
1864 if (l
->space_before
> 0)
1865 space
+= l
->space_before
;
1867 text
+= l
->space_before
;
1869 text
+= l
->line_height
;
1872 l
->ypos
= text
+ space
+ head
;
1873 if (page
->first_line
->space
[n
]) {
1874 l
->ypos
+= space
* (float)page
->first_line
->vshortfall
[n
] /
1875 page
->first_line
->space
[n
];
1878 if (l
== page
->last_line
)
1882 l
= page
->last_line
;
1887 n
= (n
< ncols ? n
+1 : ncols
);
1893 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1895 rect
*r
= snew(rect
);
1898 if (page
->last_rect
)
1899 page
->last_rect
->next
= r
;
1901 page
->first_rect
= r
;
1902 page
->last_rect
= r
;
1910 static void add_string_to_page(page_data
*page
, int x
, int y
,
1911 font_encoding
*fe
, int size
, char *text
,
1914 text_fragment
*frag
;
1916 frag
= snew(text_fragment
);
1919 if (page
->last_text
)
1920 page
->last_text
->next
= frag
;
1922 page
->first_text
= frag
;
1923 page
->last_text
= frag
;
1928 frag
->fontsize
= size
;
1929 frag
->text
= dupstr(text
);
1930 frag
->width
= width
;
1934 * Returns the updated x coordinate.
1936 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1937 int x
, int y
, wchar_t *str
)
1940 int textpos
, textwid
, kern
, glyph
, oglyph
;
1941 font_encoding
*subfont
= NULL
, *sf
;
1943 text
= snewn(1 + ustrlen(str
), char);
1944 textpos
= textwid
= 0;
1949 glyph
= (*str
< 0 || *str
> 0xFFFF ?
0xFFFF :
1950 font
->info
->bmp
[*str
]);
1952 if (glyph
== 0xFFFF) {
1954 continue; /* nothing more we can do here */
1958 * Find which subfont this character is going in.
1960 sf
= font
->subfont_map
[glyph
].subfont
;
1966 * This character is not yet in a subfont. Assign one.
1968 if (font
->latest_subfont
->free_pos
>= 0x100)
1969 font
->latest_subfont
= new_font_encoding(font
);
1971 c
= font
->latest_subfont
->free_pos
++;
1972 if (font
->latest_subfont
->free_pos
== 0x7F)
1973 font
->latest_subfont
->free_pos
= 0xA1;
1975 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1976 font
->subfont_map
[glyph
].position
= c
;
1977 font
->latest_subfont
->vector
[c
] = font
->info
->glyphs
[glyph
];
1978 font
->latest_subfont
->indices
[c
] = glyph
;
1979 font
->latest_subfont
->to_unicode
[c
] = *str
;
1981 sf
= font
->latest_subfont
;
1984 kern
= find_kern(font
, oglyph
, glyph
) * fontsize
;
1986 if (!subfont
|| sf
!= subfont
|| kern
) {
1988 text
[textpos
] = '\0';
1989 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
,
1991 x
+= textwid
+ kern
;
1993 assert(textpos
== 0);
2000 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
2001 textwid
+= font
->info
->widths
[glyph
] * fontsize
;
2007 text
[textpos
] = '\0';
2008 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
, textwid
);
2016 * Returns the updated x coordinate.
2018 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
2019 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
2020 int shortfall
, int nspaces
, int *nspace
,
2021 keywordlist
*keywords
, indexdata
*idx
, paper_conf
*conf
)
2023 while (text
&& text
!= text_end
) {
2024 int style
, type
, findex
, errs
;
2028 switch (text
->type
) {
2030 * Start a cross-reference.
2032 case word_HyperLink
:
2033 case word_UpperXref
:
2034 case word_LowerXref
:
2037 if (text
->type
== word_HyperLink
) {
2039 dest
.url
= utoa_dup(text
->text
, CS_ASCII
);
2041 } else if (text
->type
== word_PageXref
) {
2044 dest
.page
= (page_data
*)text
->private_data
;
2046 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
2050 assert(kwl
->para
->private_data
);
2051 pdata
= (para_data
*) kwl
->para
->private_data
;
2053 dest
.page
= pdata
->first
->page
;
2057 * Shouldn't happen, but *shrug*
2064 if (dest
.type
!= NONE
) {
2066 (*xr
)->dest
= dest
; /* structure copy */
2067 if (page
->last_xref
)
2068 page
->last_xref
->next
= *xr
;
2070 page
->first_xref
= *xr
;
2071 page
->last_xref
= *xr
;
2075 * FIXME: Ideally we should have, and use, some
2076 * vertical font metric information here so that
2077 * our cross-ref rectangle can take account of
2078 * descenders and the font's cap height. This will
2079 * do for the moment, but it isn't ideal.
2081 (*xr
)->lx
= (*xr
)->rx
= x
;
2083 (*xr
)->ty
= y
+ ldata
->line_height
;
2088 * Finish extending a cross-reference box.
2096 * Add the current page number to the list of pages
2097 * referenced by an index entry.
2101 * We don't create index references in contents entries.
2103 if (!pdata
->contents_entry
) {
2107 tag
= index_findtag(idx
, text
->text
);
2111 for (i
= 0; i
< tag
->nrefs
; i
++) {
2112 indexentry
*entry
= tag
->refs
[i
];
2113 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
2116 * If the same index term is indexed twice
2117 * within the same section, we only want to
2118 * mention it once in the index.
2120 if (pi
->lastpage
!= page
) {
2124 pi
->lastword
= pi
->lastword
->next
=
2126 pi
->lastword
= pi
->lastword
->next
=
2128 wp
= &pi
->lastword
->next
;
2132 pi
->lastword
= *wp
=
2133 fake_page_ref(page
);
2134 pi
->lastword
= pi
->lastword
->next
=
2135 fake_word(page
->number
);
2136 pi
->lastword
= pi
->lastword
->next
=
2140 pi
->lastpage
= page
;
2146 style
= towordstyle(text
->type
);
2147 type
= removeattr(text
->type
);
2149 findex
= (style
== word_Normal ? FONT_NORMAL
:
2150 style
== word_Emph ? FONT_EMPH
:
2153 if (type
== word_Normal
) {
2155 } else if (type
== word_WhiteSpace
) {
2156 x
+= pdata
->sizes
[findex
] *
2157 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
2158 if (nspaces
&& findex
!= FONT_CODE
) {
2159 x
+= (*nspace
+1) * shortfall
/ nspaces
;
2160 x
-= *nspace
* shortfall
/ nspaces
;
2164 } else /* if (type == word_Quote) */ {
2165 if (text
->aux
== quote_Open
)
2171 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
2173 if (errs
&& text
->alt
)
2174 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
2175 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
,
2178 x
= render_string(page
, pdata
->fonts
[findex
],
2179 pdata
->sizes
[findex
], x
, y
, str
);
2192 * Returns the last x position used on the line.
2194 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
2195 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
2202 if (ldata
->aux_text
) {
2206 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2207 left_x
+ ldata
->aux_left_indent
,
2208 top_y
- ldata
->ypos
,
2209 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
2210 keywords
, idx
, conf
);
2211 if (ldata
->aux_text_2
)
2212 render_text(ldata
->page
, ldata
->pdata
, ldata
,
2213 x
, top_y
- ldata
->ypos
,
2214 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
2215 keywords
, idx
, conf
);
2221 * There might be a cross-reference carried over from a
2224 if (dest
->type
!= NONE
) {
2227 xr
->dest
= *dest
; /* structure copy */
2228 if (ldata
->page
->last_xref
)
2229 ldata
->page
->last_xref
->next
= xr
;
2231 ldata
->page
->first_xref
= xr
;
2232 ldata
->page
->last_xref
= xr
;
2233 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
2234 xr
->by
= top_y
- ldata
->ypos
;
2235 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
2240 int extra_indent
, shortfall
, spaces
;
2241 int just
= ldata
->pdata
->justification
;
2244 * All forms of justification become JUST when we have
2245 * to squeeze the paragraph.
2247 if (ldata
->hshortfall
< 0)
2252 shortfall
= ldata
->hshortfall
;
2253 spaces
= ldata
->nspaces
;
2257 shortfall
= spaces
= extra_indent
= 0;
2260 shortfall
= spaces
= 0;
2261 extra_indent
= ldata
->real_shortfall
;
2265 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2266 left_x
+ ldata
->xpos
+ extra_indent
,
2267 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
2268 &xr
, shortfall
, spaces
, &nspace
,
2269 keywords
, idx
, conf
);
2274 * There's a cross-reference continued on to the next line.
2284 static void render_para(para_data
*pdata
, paper_conf
*conf
,
2285 keywordlist
*keywords
, indexdata
*idx
,
2286 paragraph
*index_placeholder
, page_data
*index_page
)
2290 page_data
*cxref_page
;
2299 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
2301 * If this is a contents entry, we expect to have a single
2302 * enormous cross-reference rectangle covering the whole
2303 * thing. (Unless, of course, it spans multiple pages.)
2305 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
2306 cxref_page
= ldata
->page
;
2309 cxref
->dest
.type
= PAGE
;
2310 if (pdata
->contents_entry
== index_placeholder
) {
2311 cxref
->dest
.page
= index_page
;
2313 assert(pdata
->contents_entry
->private_data
);
2314 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2315 cxref
->dest
.page
= target
->first
->page
;
2317 cxref
->dest
.url
= NULL
;
2318 if (ldata
->page
->last_xref
)
2319 ldata
->page
->last_xref
->next
= cxref
;
2321 ldata
->page
->first_xref
= cxref
;
2322 ldata
->page
->last_xref
= cxref
;
2323 cxref
->lx
= conf
->left_margin
;
2324 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
2325 cxref
->ty
= conf
->paper_height
- conf
->top_margin
2326 - ldata
->ypos
+ ldata
->line_height
;
2328 if (pdata
->contents_entry
) {
2329 assert(cxref
!= NULL
);
2330 cxref
->by
= conf
->paper_height
- conf
->top_margin
2334 last_x
= render_line(ldata
, conf
->left_margin
,
2335 conf
->paper_height
- conf
->top_margin
,
2336 &dest
, keywords
, idx
, conf
);
2337 if (ldata
== pdata
->last
)
2342 * If this is a contents entry, add leaders and a page
2345 if (pdata
->contents_entry
) {
2351 if (pdata
->contents_entry
== index_placeholder
) {
2352 num
= index_page
->number
;
2354 assert(pdata
->contents_entry
->private_data
);
2355 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2356 num
= target
->first
->page
->number
;
2360 wid
= paper_width_simple(pdata
, w
, conf
);
2363 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2364 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2365 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2366 render_string(pdata
->last
->page
,
2367 pdata
->fonts
[FONT_NORMAL
],
2368 pdata
->sizes
[FONT_NORMAL
],
2369 conf
->left_margin
+ x
,
2370 (conf
->paper_height
- conf
->top_margin
-
2371 pdata
->last
->ypos
), L
".");
2373 render_string(pdata
->last
->page
,
2374 pdata
->fonts
[FONT_NORMAL
],
2375 pdata
->sizes
[FONT_NORMAL
],
2376 conf
->paper_width
- conf
->right_margin
- wid
,
2377 (conf
->paper_height
- conf
->top_margin
-
2378 pdata
->last
->ypos
), num
);
2382 * Render any rectangle (chapter title underline or rule)
2383 * that goes with this paragraph.
2385 switch (pdata
->rect_type
) {
2386 case RECT_CHAPTER_UNDERLINE
:
2387 add_rect_to_page(pdata
->last
->page
,
2389 (conf
->paper_height
- conf
->top_margin
-
2391 conf
->chapter_underline_depth
),
2393 conf
->chapter_underline_thickness
);
2396 add_rect_to_page(pdata
->first
->page
,
2397 conf
->left_margin
+ pdata
->first
->xpos
,
2398 (conf
->paper_height
- conf
->top_margin
-
2400 pdata
->last
->line_height
),
2401 conf
->base_width
- pdata
->first
->xpos
,
2402 pdata
->last
->line_height
);
2404 default: /* placate gcc */
2409 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2411 para_data
*pdata
= snew(para_data
);
2414 * For code paragraphs, I'm going to hack grievously and
2415 * pretend the three normal fonts are the three code paragraph
2418 setfont(pdata
, &conf
->fcode
);
2420 pdata
->first
= pdata
->last
= NULL
;
2421 pdata
->outline_level
= -1;
2422 pdata
->rect_type
= RECT_NONE
;
2423 pdata
->contents_entry
= NULL
;
2424 pdata
->justification
= LEFT
;
2426 for (; words
; words
= words
->next
) {
2427 wchar_t *t
, *e
, *start
;
2428 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2430 int prev
= -1, curr
;
2433 if (words
->next
&& words
->next
->type
== word_Emph
) {
2434 e
= words
->next
->text
;
2435 words
= words
->next
;
2445 else if (*e
== L
'i')
2447 else if (*e
== L
'b')
2464 * We've isolated a maximal subsequence of the line
2465 * which has the same emphasis. Form it into a word
2471 w
->type
= (prev
== 0 ? word_WeakCode
:
2472 prev
== 1 ? word_Emph
: word_Normal
);
2473 w
->text
= snewn(t
-start
+1, wchar_t);
2474 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2475 w
->text
[t
-start
] = '\0';
2488 ldata
= snew(line_data
);
2490 ldata
->pdata
= pdata
;
2491 ldata
->first
= lhead
;
2493 ldata
->line_height
= conf
->fcode
.font_size
* UNITS_PER_PT
;
2495 ldata
->xpos
= indent
;
2498 pdata
->last
->next
= ldata
;
2499 ldata
->prev
= pdata
->last
;
2501 pdata
->first
= ldata
;
2505 pdata
->last
= ldata
;
2507 ldata
->hshortfall
= 0;
2509 ldata
->aux_text
= NULL
;
2510 ldata
->aux_text_2
= NULL
;
2511 ldata
->aux_left_indent
= 0;
2512 /* General opprobrium for breaking in a code paragraph. */
2513 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2516 standard_line_spacing(pdata
, conf
);
2521 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2523 para_data
*pdata
= snew(para_data
);
2526 ldata
= snew(line_data
);
2528 ldata
->pdata
= pdata
;
2529 ldata
->first
= NULL
;
2531 ldata
->line_height
= conf
->rule_thickness
;
2533 ldata
->xpos
= indent
;
2538 ldata
->hshortfall
= 0;
2540 ldata
->aux_text
= NULL
;
2541 ldata
->aux_text_2
= NULL
;
2542 ldata
->aux_left_indent
= 0;
2545 * Better to break after a rule than before it
2547 ldata
->penalty_after
+= 100000;
2548 ldata
->penalty_before
+= -100000;
2550 pdata
->first
= pdata
->last
= ldata
;
2551 pdata
->outline_level
= -1;
2552 pdata
->rect_type
= RECT_RULE
;
2553 pdata
->contents_entry
= NULL
;
2554 pdata
->justification
= LEFT
;
2556 standard_line_spacing(pdata
, conf
);
2562 * Plain-text-like formatting for outline titles.
2564 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2565 for (; text
; text
= text
->next
) switch (text
->type
) {
2566 case word_HyperLink
:
2568 case word_UpperXref
:
2569 case word_LowerXref
:
2578 case word_WhiteSpace
:
2579 case word_EmphSpace
:
2580 case word_CodeSpace
:
2581 case word_WkCodeSpace
:
2583 case word_EmphQuote
:
2584 case word_CodeQuote
:
2585 case word_WkCodeQuote
:
2586 assert(text
->type
!= word_CodeQuote
&&
2587 text
->type
!= word_WkCodeQuote
);
2588 if (towordstyle(text
->type
) == word_Emph
&&
2589 (attraux(text
->aux
) == attr_First
||
2590 attraux(text
->aux
) == attr_Only
))
2591 rdadd(rs
, L
'_'); /* FIXME: configurability */
2592 else if (towordstyle(text
->type
) == word_Code
&&
2593 (attraux(text
->aux
) == attr_First
||
2594 attraux(text
->aux
) == attr_Only
))
2595 rdadd(rs
, L
'\''); /* FIXME: configurability */
2596 if (removeattr(text
->type
) == word_Normal
) {
2597 rdadds(rs
, text
->text
);
2598 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2600 } else if (removeattr(text
->type
) == word_Quote
) {
2601 rdadd(rs
, L
'\''); /* fixme: configurability */
2603 if (towordstyle(text
->type
) == word_Emph
&&
2604 (attraux(text
->aux
) == attr_Last
||
2605 attraux(text
->aux
) == attr_Only
))
2606 rdadd(rs
, L
'_'); /* FIXME: configurability */
2607 else if (towordstyle(text
->type
) == word_Code
&&
2608 (attraux(text
->aux
) == attr_Last
||
2609 attraux(text
->aux
) == attr_Only
))
2610 rdadd(rs
, L
'\''); /* FIXME: configurability */
2615 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2618 rdstring rs
= {0, 0, NULL
};
2621 paper_rdaddw(&rs
, first
);
2623 rdadds(&rs
, separator
);
2625 paper_rdaddw(&rs
, second
);
2630 static word
*fake_word(wchar_t *text
)
2632 word
*ret
= snew(word
);
2635 ret
->type
= word_Normal
;
2636 ret
->text
= ustrdup(text
);
2637 ret
->breaks
= FALSE
;
2642 static word
*fake_space_word(void)
2644 word
*ret
= snew(word
);
2647 ret
->type
= word_WhiteSpace
;
2654 static word
*fake_page_ref(page_data
*page
)
2656 word
*ret
= snew(word
);
2659 ret
->type
= word_PageXref
;
2661 ret
->breaks
= FALSE
;
2663 ret
->private_data
= page
;
2667 static word
*fake_end_ref(void)
2669 word
*ret
= snew(word
);
2672 ret
->type
= word_XrefEnd
;
2674 ret
->breaks
= FALSE
;
2679 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2688 w
= dup_word_list(first
);
2696 w
= fake_word(separator
);
2702 *wptr
= dup_word_list(second
);
2708 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2712 if (!src
->first_line
)
2715 if (dest
->last_line
) {
2716 dest
->last_line
->next
= src
->first_line
;
2717 src
->first_line
->prev
= dest
->last_line
;
2719 dest
->last_line
= src
->last_line
;
2721 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2723 ldata
->xpos
+= right_shift
;
2725 if (ldata
== src
->last_line
)