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.
85 typedef struct paper_conf_Tag paper_conf
;
86 typedef struct paper_idx_Tag paper_idx
;
89 font_data
*fonts
[NFONTS
];
93 struct paper_conf_Tag
{
100 int indent_list_bullet
;
101 int indent_list_after
;
105 int base_para_spacing
;
106 int chapter_top_space
;
107 int sect_num_left_space
;
108 int chapter_underline_depth
;
109 int chapter_underline_thickness
;
111 font_cfg fbase
, fcode
, ftitle
, fchapter
, *fsect
;
113 int contents_indent_step
;
115 int leader_separation
;
119 int pagenum_fontsize
;
121 wchar_t *lquote
, *rquote
, *bullet
;
122 /* These are derived from the above */
128 struct paper_idx_Tag
{
130 * Word list giving the page numbers on which this index entry
131 * appears. Also the last word in the list, for ease of
137 * The last page added to the list (so we can ensure we don't
144 word_PageXref
= word_NotWordType
+ 1
147 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
148 static void wrap_paragraph(para_data
*pdata
, word
*words
,
149 int w
, int i1
, int i2
, paper_conf
*conf
);
150 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
151 int page_height
, int ncols
, int headspace
);
152 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
153 int x
, int y
, wchar_t *str
);
154 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
155 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
157 static void render_para(para_data
*pdata
, paper_conf
*conf
,
158 keywordlist
*keywords
, indexdata
*idx
,
159 paragraph
*index_placeholder
, page_data
*index_page
);
160 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
161 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
);
162 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
163 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
164 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
165 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
166 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
168 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
169 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
171 static word
*fake_word(wchar_t *text
);
172 static word
*fake_space_word(void);
173 static word
*fake_page_ref(page_data
*page
);
174 static word
*fake_end_ref(void);
175 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
177 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
179 static int fonts_ok(wchar_t *string
, ...)
185 va_start(ap
, string
);
186 while ( (font
= va_arg(ap
, font_data
*)) != NULL
) {
188 (void) string_width(font
, string
, &errs
);
199 static void paper_cfg_fonts(font_data
**fonts
, font_list
*fontlist
,
200 wchar_t *wp
, filepos
*fpos
) {
205 for (i
= 0; i
< NFONTS
&& *wp
; i
++, wp
= uadv(wp
)) {
206 fn
= utoa_dup(wp
, CS_ASCII
);
207 f
= make_std_font(fontlist
, fn
);
211 /* FIXME: proper error */
212 error(err_nofont
, fpos
, wp
);
216 static paper_conf
paper_configure(paragraph
*source
, font_list
*fontlist
) {
223 ret
.paper_width
= 595 * UNITS_PER_PT
;
224 ret
.paper_height
= 841 * UNITS_PER_PT
;
225 ret
.left_margin
= 72 * UNITS_PER_PT
;
226 ret
.top_margin
= 72 * UNITS_PER_PT
;
227 ret
.right_margin
= 72 * UNITS_PER_PT
;
228 ret
.bottom_margin
= 108 * UNITS_PER_PT
;
229 ret
.indent_list_bullet
= 6 * UNITS_PER_PT
;
230 ret
.indent_list_after
= 18 * UNITS_PER_PT
;
231 ret
.indent_quote
= 18 * UNITS_PER_PT
;
232 ret
.base_leading
= UNITS_PER_PT
;
233 ret
.base_para_spacing
= 10 * UNITS_PER_PT
;
234 ret
.chapter_top_space
= 72 * UNITS_PER_PT
;
235 ret
.sect_num_left_space
= 12 * UNITS_PER_PT
;
236 ret
.chapter_underline_depth
= 14 * UNITS_PER_PT
;
237 ret
.chapter_underline_thickness
= 3 * UNITS_PER_PT
;
238 ret
.rule_thickness
= 1 * UNITS_PER_PT
;
239 ret
.fbase
.font_size
= 12;
240 ret
.fbase
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Times-Roman");
241 ret
.fbase
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Times-Italic");
242 ret
.fbase
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
243 ret
.fcode
.font_size
= 12;
244 ret
.fcode
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Courier-Bold");
245 ret
.fcode
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Courier-Oblique");
246 ret
.fcode
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
247 ret
.ftitle
.font_size
= 24;
248 ret
.ftitle
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Helvetica-Bold");
249 ret
.ftitle
.fonts
[FONT_EMPH
] =
250 make_std_font(fontlist
, "Helvetica-BoldOblique");
251 ret
.ftitle
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
252 ret
.fchapter
.font_size
= 20;
253 ret
.fchapter
.fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
254 ret
.fchapter
.fonts
[FONT_EMPH
] =
255 make_std_font(fontlist
, "Helvetica-BoldOblique");
256 ret
.fchapter
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
258 ret
.fsect
= snewn(ret
.nfsect
, font_cfg
);
259 ret
.fsect
[0].font_size
= 16;
260 ret
.fsect
[0].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
261 ret
.fsect
[0].fonts
[FONT_EMPH
] =
262 make_std_font(fontlist
, "Helvetica-BoldOblique");
263 ret
.fsect
[0].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
264 ret
.fsect
[1].font_size
= 14;
265 ret
.fsect
[1].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
266 ret
.fsect
[1].fonts
[FONT_EMPH
] =
267 make_std_font(fontlist
, "Helvetica-BoldOblique");
268 ret
.fsect
[1].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
269 ret
.fsect
[2].font_size
= 13;
270 ret
.fsect
[2].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
271 ret
.fsect
[2].fonts
[FONT_EMPH
] =
272 make_std_font(fontlist
, "Helvetica-BoldOblique");
273 ret
.fsect
[2].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
274 ret
.contents_indent_step
= 24 * UNITS_PER_PT
;
275 ret
.contents_margin
= 84 * UNITS_PER_PT
;
276 ret
.leader_separation
= 12 * UNITS_PER_PT
;
277 ret
.index_gutter
= 36 * UNITS_PER_PT
;
279 ret
.index_minsep
= 18 * UNITS_PER_PT
;
280 ret
.pagenum_fontsize
= 12;
281 ret
.footer_distance
= 32 * UNITS_PER_PT
;
282 ret
.lquote
= L
"\x2018\0\x2019\0'\0'\0\0";
283 ret
.rquote
= uadv(ret
.lquote
);
284 ret
.bullet
= L
"\x2022\0-\0\0";
287 * Two-pass configuration so that we can pick up global config
288 * (e.g. `quotes') before having it overridden by specific
289 * config (`paper-quotes'), irrespective of the order in which
292 for (p
= source
; p
; p
= p
->next
) {
293 if (p
->type
== para_Config
) {
294 if (!ustricmp(p
->keyword
, L
"quotes")) {
295 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
296 ret
.lquote
= uadv(p
->keyword
);
297 ret
.rquote
= uadv(ret
.lquote
);
303 for (p
= source
; p
; p
= p
->next
) {
304 p
->private_data
= NULL
;
305 if (p
->type
== para_Config
) {
306 if (!ustricmp(p
->keyword
, L
"paper-quotes")) {
307 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
308 ret
.lquote
= uadv(p
->keyword
);
309 ret
.rquote
= uadv(ret
.lquote
);
311 } else if (!ustricmp(p
->keyword
, L
"paper-bullet")) {
312 ret
.bullet
= uadv(p
->keyword
);
313 } else if (!ustricmp(p
->keyword
, L
"paper-page-width")) {
315 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
316 } else if (!ustricmp(p
->keyword
, L
"paper-page-height")) {
318 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
319 } else if (!ustricmp(p
->keyword
, L
"paper-left-margin")) {
321 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
322 } else if (!ustricmp(p
->keyword
, L
"paper-top-margin")) {
324 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
325 } else if (!ustricmp(p
->keyword
, L
"paper-right-margin")) {
327 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
328 } else if (!ustricmp(p
->keyword
, L
"paper-bottom-margin")) {
330 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
331 } else if (!ustricmp(p
->keyword
, L
"paper-list-indent")) {
332 ret
.indent_list_bullet
=
333 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
334 } else if (!ustricmp(p
->keyword
, L
"paper-listitem-indent")) {
336 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
337 } else if (!ustricmp(p
->keyword
, L
"paper-quote-indent")) {
339 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
340 } else if (!ustricmp(p
->keyword
, L
"paper-base-leading")) {
342 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
343 } else if (!ustricmp(p
->keyword
, L
"paper-base-para-spacing")) {
344 ret
.base_para_spacing
=
345 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
346 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-top-space")) {
347 ret
.chapter_top_space
=
348 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
349 } else if (!ustricmp(p
->keyword
, L
"paper-sect-num-left-space")) {
350 ret
.sect_num_left_space
=
351 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
352 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-depth")) {
353 ret
.chapter_underline_depth
=
354 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
355 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-thickness")) {
356 ret
.chapter_underline_thickness
=
357 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
358 } else if (!ustricmp(p
->keyword
, L
"paper-rule-thickness")) {
360 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
361 } else if (!ustricmp(p
->keyword
, L
"paper-contents-indent-step")) {
362 ret
.contents_indent_step
=
363 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
364 } else if (!ustricmp(p
->keyword
, L
"paper-contents-margin")) {
365 ret
.contents_margin
=
366 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
367 } else if (!ustricmp(p
->keyword
, L
"paper-leader-separation")) {
368 ret
.leader_separation
=
369 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
370 } else if (!ustricmp(p
->keyword
, L
"paper-index-gutter")) {
372 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
373 } else if (!ustricmp(p
->keyword
, L
"paper-index-minsep")) {
375 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
376 } else if (!ustricmp(p
->keyword
, L
"paper-footer-distance")) {
377 ret
.footer_distance
=
378 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
379 } else if (!ustricmp(p
->keyword
, L
"paper-base-font-size")) {
380 ret
.fbase
.font_size
= utoi(uadv(p
->keyword
));
381 } else if (!ustricmp(p
->keyword
, L
"paper-index-columns")) {
382 ret
.index_cols
= utoi(uadv(p
->keyword
));
383 } else if (!ustricmp(p
->keyword
, L
"paper-pagenum-font-size")) {
384 ret
.pagenum_fontsize
= utoi(uadv(p
->keyword
));
385 } else if (!ustricmp(p
->keyword
, L
"paper-base-fonts")) {
386 paper_cfg_fonts(ret
.fbase
.fonts
, fontlist
, uadv(p
->keyword
),
388 } else if (!ustricmp(p
->keyword
, L
"paper-code-font-size")) {
389 ret
.fcode
.font_size
= utoi(uadv(p
->keyword
));
390 } else if (!ustricmp(p
->keyword
, L
"paper-code-fonts")) {
391 paper_cfg_fonts(ret
.fcode
.fonts
, fontlist
, uadv(p
->keyword
),
393 } else if (!ustricmp(p
->keyword
, L
"paper-title-font-size")) {
394 ret
.ftitle
.font_size
= utoi(uadv(p
->keyword
));
395 } else if (!ustricmp(p
->keyword
, L
"paper-title-fonts")) {
396 paper_cfg_fonts(ret
.ftitle
.fonts
, fontlist
, uadv(p
->keyword
),
398 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-font-size")) {
399 ret
.ftitle
.font_size
= utoi(uadv(p
->keyword
));
400 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-fonts")) {
401 paper_cfg_fonts(ret
.ftitle
.fonts
, fontlist
, uadv(p
->keyword
),
403 } else if (!ustricmp(p
->keyword
, L
"paper-section-font-size")) {
404 wchar_t *q
= uadv(p
->keyword
);
410 if (n
>= ret
.nfsect
) {
412 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
413 for (i
= ret
.nfsect
; i
<= n
; i
++)
414 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
417 ret
.fsect
[n
].font_size
= utoi(q
);
418 } else if (!ustricmp(p
->keyword
, L
"paper-section-fonts")) {
419 wchar_t *q
= uadv(p
->keyword
);
425 if (n
>= ret
.nfsect
) {
427 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
428 for (i
= ret
.nfsect
; i
<= n
; i
++)
429 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
432 paper_cfg_fonts(ret
.fsect
[n
].fonts
, fontlist
, q
, &p
->fpos
);
438 * Set up the derived fields in the conf structure.
442 ret
.paper_width
- ret
.left_margin
- ret
.right_margin
;
444 ret
.paper_height
- ret
.top_margin
- ret
.bottom_margin
;
445 ret
.indent_list
= ret
.indent_list_bullet
+ ret
.indent_list_after
;
447 (ret
.base_width
- (ret
.index_cols
-1) * ret
.index_gutter
)
451 * Now process fallbacks on quote characters and bullets. We
452 * use string_width() to determine whether all of the relevant
453 * fonts contain the same character, and fall back whenever we
454 * find a character which not all of them support.
457 /* Quote characters need not be supported in the fixed code fonts,
458 * but must be in the title and body fonts. */
459 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
))) {
461 if (!fonts_ok(ret
.lquote
,
462 ret
.fbase
.fonts
[FONT_NORMAL
],
463 ret
.fbase
.fonts
[FONT_EMPH
],
464 ret
.ftitle
.fonts
[FONT_NORMAL
],
465 ret
.ftitle
.fonts
[FONT_EMPH
],
466 ret
.fchapter
.fonts
[FONT_NORMAL
],
467 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
) ||
468 !fonts_ok(ret
.rquote
,
469 ret
.fbase
.fonts
[FONT_NORMAL
],
470 ret
.fbase
.fonts
[FONT_EMPH
],
471 ret
.ftitle
.fonts
[FONT_NORMAL
],
472 ret
.ftitle
.fonts
[FONT_EMPH
],
473 ret
.fchapter
.fonts
[FONT_NORMAL
],
474 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
))
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
))
484 ret
.lquote
= uadv(ret
.rquote
);
485 ret
.rquote
= uadv(ret
.lquote
);
488 /* The bullet character only needs to be supported in the normal body
489 * font (not even in italics). */
490 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
491 !fonts_ok(ret
.bullet
, ret
.fbase
.fonts
[FONT_NORMAL
], NULL
))
492 ret
.bullet
= uadv(ret
.bullet
);
497 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
501 int indent
, used_contents
;
502 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
503 para_data
*firstcont
, *lastcont
;
504 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
507 paper_conf
*conf
, ourconf
;
510 paragraph index_placeholder_para
;
511 page_data
*first_index_page
;
513 fontlist
= snew(font_list
);
514 fontlist
->head
= fontlist
->tail
= NULL
;
516 ourconf
= paper_configure(sourceform
, fontlist
);
520 * Set up a data structure to collect page numbers for each
529 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
530 paper_idx
*pi
= snew(paper_idx
);
534 pi
->words
= pi
->lastword
= NULL
;
537 entry
->backend_data
= pi
;
542 * Format the contents entry for each heading.
545 word
*contents_title
;
546 contents_title
= fake_word(L
"Contents");
548 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
549 NULL
, NULL
, contents_title
, conf
);
550 lastcont
= firstcont
;
551 lastcont
->next
= NULL
;
552 firstcontline
= firstcont
->first
;
553 lastcontline
= lastcont
->last
;
554 for (p
= sourceform
; p
; p
= p
->next
) {
561 case para_UnnumberedChapter
:
567 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
570 case para_UnnumberedChapter
:
571 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
576 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
577 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
580 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
581 conf
->contents_margin
,
582 NULL
, NULL
, words
, conf
);
584 pdata
->contents_entry
= p
;
585 lastcont
->next
= pdata
;
589 * Link all contents line structures together into
594 lastcontline
->next
= pdata
->first
;
595 pdata
->first
->prev
= lastcontline
;
597 firstcontline
= pdata
->first
;
598 pdata
->first
->prev
= NULL
;
600 lastcontline
= pdata
->last
;
601 lastcontline
->next
= NULL
;
609 * And one extra one, for the index.
612 pdata
= make_para_data(para_Normal
, 0, 0,
613 conf
->contents_margin
,
614 NULL
, NULL
, fake_word(L
"Index"), conf
);
616 pdata
->contents_entry
= &index_placeholder_para
;
617 lastcont
->next
= pdata
;
622 lastcontline
->next
= pdata
->first
;
623 pdata
->first
->prev
= lastcontline
;
625 firstcontline
= pdata
->first
;
626 pdata
->first
->prev
= NULL
;
628 lastcontline
= pdata
->last
;
629 lastcontline
->next
= NULL
;
635 * Do the main paragraph formatting.
638 used_contents
= FALSE
;
639 firstline
= lastline
= NULL
;
640 for (p
= sourceform
; p
; p
= p
->next
) {
641 p
->private_data
= NULL
;
645 * These paragraph types are either invisible or don't
646 * define text in the normal sense. Either way, they
647 * don't require wrapping.
652 case para_NotParaType
:
659 * These paragraph types don't require wrapping, but
660 * they do affect the line width to which we wrap the
661 * rest of the paragraphs, so we need to pay attention.
664 indent
+= conf
->indent_list
; break;
666 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
668 indent
+= conf
->indent_quote
; break;
670 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
673 * This paragraph type is special. Process it
677 pdata
= code_paragraph(indent
, p
->words
, conf
);
678 p
->private_data
= pdata
;
679 if (pdata
->first
!= pdata
->last
) {
680 pdata
->first
->penalty_after
+= 100000;
681 pdata
->last
->penalty_before
+= 100000;
686 * This paragraph is also special.
689 pdata
= rule_paragraph(indent
, conf
);
690 p
->private_data
= pdata
;
694 * All of these paragraph types require wrapping in the
695 * ordinary way. So we must supply a set of fonts, a
696 * line width and auxiliary information (e.g. bullet
697 * text) for each one.
701 case para_UnnumberedChapter
:
705 case para_BiblioCited
:
707 case para_NumberedList
:
708 case para_DescribedThing
:
709 case para_Description
:
712 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
713 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
715 p
->private_data
= pdata
;
720 if (p
->private_data
) {
721 pdata
= (para_data
*)p
->private_data
;
724 * If this is the first non-title heading, we link the
725 * contents section in before it.
727 if (!used_contents
&& pdata
->outline_level
> 0) {
728 used_contents
= TRUE
;
730 lastpara
->next
= firstcont
;
732 firstpara
= firstcont
;
734 assert(lastpara
->next
== NULL
);
737 lastline
->next
= firstcontline
;
738 firstcontline
->prev
= lastline
;
740 firstline
= firstcontline
;
741 firstcontline
->prev
= NULL
;
743 assert(lastcontline
!= NULL
);
744 lastline
= lastcontline
;
745 lastline
->next
= NULL
;
749 * Link all line structures together into a big list.
753 lastline
->next
= pdata
->first
;
754 pdata
->first
->prev
= lastline
;
756 firstline
= pdata
->first
;
757 pdata
->first
->prev
= NULL
;
759 lastline
= pdata
->last
;
760 lastline
->next
= NULL
;
764 * Link all paragraph structures together similarly.
768 lastpara
->next
= pdata
;
776 * Now we have an enormous linked list of every line of text in
777 * the document. Break it up into pages.
779 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
790 for (page
= pages
; page
; page
= page
->next
) {
791 sprintf(buf
, "%d", ++pagenum
);
792 page
->number
= ufroma_dup(buf
, CS_ASCII
);
796 first_index_page
= snew(page_data
);
797 first_index_page
->next
= first_index_page
->prev
= NULL
;
798 first_index_page
->first_line
= NULL
;
799 first_index_page
->last_line
= NULL
;
800 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
801 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
802 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
804 /* And don't forget the as-yet-uncreated index. */
805 sprintf(buf
, "%d", ++pagenum
);
806 first_index_page
->number
= ufroma_dup(buf
, CS_ASCII
);
811 * Now we're ready to actually lay out the pages. We do this by
812 * looping over _paragraphs_, since we may need to track cross-
813 * references between lines and even across pages.
815 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
816 render_para(pdata
, conf
, keywords
, idx
,
817 &index_placeholder_para
, first_index_page
);
820 * Now we've laid out the main body pages, we should have
821 * acquired a full set of page numbers for the index.
827 para_data
*firstidx
, *lastidx
;
828 line_data
*firstidxline
, *lastidxline
, *ldata
;
829 page_data
*ipages
, *ipages2
, *page
;
832 * Create a set of paragraphs for the index.
834 index_title
= fake_word(L
"Index");
836 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
837 NULL
, NULL
, index_title
, conf
);
839 lastidx
->next
= NULL
;
840 firstidxline
= firstidx
->first
;
841 lastidxline
= lastidx
->last
;
842 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
843 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
844 para_data
*text
, *pages
;
849 text
= make_para_data(para_Normal
, 0, 0,
850 conf
->base_width
- conf
->index_colwidth
,
851 NULL
, NULL
, entry
->text
, conf
);
853 pages
= make_para_data(para_Normal
, 0, 0,
854 conf
->base_width
- conf
->index_colwidth
,
855 NULL
, NULL
, pi
->words
, conf
);
857 text
->justification
= LEFT
;
858 pages
->justification
= RIGHT
;
859 text
->last
->space_after
= pages
->first
->space_before
=
860 conf
->base_leading
/ 2;
862 pages
->last
->space_after
= text
->first
->space_before
=
866 assert(pages
->first
);
871 * If feasible, fold the two halves of the index entry
874 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
875 conf
->index_colwidth
+ conf
->index_minsep
) {
876 text
->last
->space_after
= -1;
877 pages
->first
->space_before
= -pages
->first
->line_height
+1;
880 lastidx
->next
= text
;
886 * Link all index line structures together into
889 text
->last
->next
= pages
->first
;
890 pages
->first
->prev
= text
->last
;
892 lastidxline
->next
= text
->first
;
893 text
->first
->prev
= lastidxline
;
895 lastidxline
= pages
->last
;
898 * Breaking an index entry anywhere is so bad that I
899 * think I'm going to forbid it totally.
901 for (ldata
= text
->first
; ldata
&& ldata
->next
;
902 ldata
= ldata
->next
) {
903 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
904 ldata
->space_after
= -1;
909 * Now break the index into pages.
911 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
913 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
916 firstidxline
->space_before
+
917 firstidxline
->line_height
+
918 firstidxline
->space_after
);
921 * This will have put each _column_ of the index on a
922 * separate page, which isn't what we want. Fold the pages
929 for (i
= 1; i
< conf
->index_cols
; i
++)
933 fold_into_page(page
, page
->next
,
934 i
* (conf
->index_colwidth
+
935 conf
->index_gutter
));
937 page
->next
= page
->next
->next
;
939 page
->next
->prev
= page
;
945 /* Also fold the heading on to the same page as the index items. */
946 fold_into_page(ipages
, ipages2
, 0);
947 ipages
->next
= ipages2
->next
;
949 ipages
->next
->prev
= ipages
;
951 fold_into_page(first_index_page
, ipages
, 0);
952 first_index_page
->next
= ipages
->next
;
953 if (first_index_page
->next
)
954 first_index_page
->next
->prev
= first_index_page
;
956 ipages
= first_index_page
;
959 * Number the index pages, except the already-numbered
962 for (page
= ipages
->next
; page
; page
= page
->next
) {
964 sprintf(buf
, "%d", ++pagenum
);
965 page
->number
= ufroma_dup(buf
, CS_ASCII
);
969 * Render the index pages.
971 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
972 render_para(pdata
, conf
, keywords
, idx
,
973 &index_placeholder_para
, first_index_page
);
976 * Link the index page list on to the end of the main page
982 for (page
= pages
; page
->next
; page
= page
->next
);
987 * Same with the paragraph list, which will cause the index
988 * to be mentioned in the document outline.
991 firstpara
= firstidx
;
993 lastpara
->next
= firstidx
;
998 * Draw the headers and footers.
1000 * FIXME: this should be fully configurable, but for the moment
1001 * I'm just going to put in page numbers in the centre of a
1002 * footer and leave it at that.
1007 for (page
= pages
; page
; page
= page
->next
) {
1010 width
= conf
->pagenum_fontsize
*
1011 string_width(conf
->fbase
.fonts
[FONT_NORMAL
], page
->number
,
1014 render_string(page
, conf
->fbase
.fonts
[FONT_NORMAL
],
1015 conf
->pagenum_fontsize
,
1016 conf
->left_margin
+ (conf
->base_width
- width
)/2,
1017 conf
->bottom_margin
- conf
->footer_distance
,
1023 * Start putting together the overall document structure we're
1026 doc
= snew(document
);
1027 doc
->fonts
= fontlist
;
1029 doc
->paper_width
= conf
->paper_width
;
1030 doc
->paper_height
= conf
->paper_height
;
1033 * Collect the section heading paragraphs into a document
1034 * outline. This is slightly fiddly because the Title paragraph
1035 * isn't required to be at the start, although all the others
1041 doc
->outline_elements
= snewn(osize
, outline_element
);
1042 doc
->n_outline_elements
= 0;
1044 /* First find the title. */
1045 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1046 if (pdata
->outline_level
== 0) {
1047 doc
->outline_elements
[0].level
= 0;
1048 doc
->outline_elements
[0].pdata
= pdata
;
1049 doc
->n_outline_elements
++;
1054 /* Then collect the rest. */
1055 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1056 if (pdata
->outline_level
> 0) {
1057 if (doc
->n_outline_elements
>= osize
) {
1059 doc
->outline_elements
=
1060 sresize(doc
->outline_elements
, osize
, outline_element
);
1063 doc
->outline_elements
[doc
->n_outline_elements
].level
=
1064 pdata
->outline_level
;
1065 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
1066 doc
->n_outline_elements
++;
1074 static void setfont(para_data
*p
, font_cfg
*f
) {
1077 for (i
= 0; i
< NFONTS
; i
++) {
1078 p
->fonts
[i
] = f
->fonts
[i
];
1079 p
->sizes
[i
] = f
->font_size
;
1083 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
1084 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
1089 int extra_indent
, firstline_indent
, aux_indent
;
1092 pdata
= snew(para_data
);
1093 pdata
->outline_level
= -1;
1094 pdata
->outline_title
= NULL
;
1095 pdata
->rect_type
= RECT_NONE
;
1096 pdata
->contents_entry
= NULL
;
1097 pdata
->justification
= JUST
;
1100 * Choose fonts for this paragraph.
1104 setfont(pdata
, &conf
->ftitle
);
1105 pdata
->outline_level
= 0;
1110 case para_UnnumberedChapter
:
1111 setfont(pdata
, &conf
->fchapter
);
1112 pdata
->outline_level
= 1;
1118 &conf
->fsect
[paux
>= conf
->nfsect ? conf
->nfsect
- 1 : paux
]);
1119 pdata
->outline_level
= 2 + paux
;
1123 case para_BiblioCited
:
1125 case para_NumberedList
:
1126 case para_DescribedThing
:
1127 case para_Description
:
1128 case para_Copyright
:
1129 setfont(pdata
, &conf
->fbase
);
1134 * Also select an indentation level depending on the
1135 * paragraph type (list paragraphs other than
1136 * para_DescribedThing need extra indent).
1138 * (FIXME: Perhaps at some point we might even arrange
1139 * for the user to be able to request indented first
1140 * lines in paragraphs.)
1142 if (ptype
== para_Bullet
||
1143 ptype
== para_NumberedList
||
1144 ptype
== para_Description
) {
1145 extra_indent
= firstline_indent
= conf
->indent_list
;
1147 extra_indent
= firstline_indent
= 0;
1151 * Find the auxiliary text for this paragraph.
1162 * For some heading styles (FIXME: be able to
1163 * configure which), the auxiliary text contains
1164 * the chapter number and is arranged to be
1165 * right-aligned a few points left of the primary
1166 * margin. For other styles, the auxiliary text is
1167 * the full chapter _name_ and takes up space
1168 * within the (wrapped) chapter title, meaning that
1169 * we must move the first line indent over to make
1172 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
1176 len
= paper_width_simple(pdata
, pkwtext2
, conf
);
1177 aux_indent
= -len
- conf
->sect_num_left_space
;
1179 pdata
->outline_title
=
1180 prepare_outline_title(pkwtext2
, L
" ", pwords
);
1183 aux2
= fake_word(L
": ");
1186 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1187 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1189 pdata
->outline_title
=
1190 prepare_outline_title(pkwtext
, L
": ", pwords
);
1196 * Auxiliary text consisting of a bullet.
1198 aux
= fake_word(conf
->bullet
);
1199 aux_indent
= indent
+ conf
->indent_list_bullet
;
1202 case para_NumberedList
:
1204 * Auxiliary text consisting of the number followed
1205 * by a (FIXME: configurable) full stop.
1208 aux2
= fake_word(L
".");
1209 aux_indent
= indent
+ conf
->indent_list_bullet
;
1212 case para_BiblioCited
:
1214 * Auxiliary text consisting of the bibliography
1215 * reference text, and a trailing space.
1218 aux2
= fake_word(L
" ");
1219 aux_indent
= indent
;
1220 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1221 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1225 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
1226 pdata
->outline_title
=
1227 prepare_outline_title(NULL
, NULL
, pwords
);
1230 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
1231 indent
+ firstline_indent
,
1232 indent
+ extra_indent
, conf
);
1234 pdata
->first
->aux_text
= aux
;
1235 pdata
->first
->aux_text_2
= aux2
;
1236 pdata
->first
->aux_left_indent
= aux_indent
;
1239 * Line breaking penalties.
1246 case para_UnnumberedChapter
:
1248 * Fixed and large penalty for breaking straight
1249 * after a heading; corresponding bonus for
1250 * breaking straight before.
1252 pdata
->first
->penalty_before
= -500000;
1253 pdata
->last
->penalty_after
= 500000;
1254 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
1255 ldata
->penalty_after
= 500000;
1258 case para_DescribedThing
:
1260 * This is treated a bit like a small heading:
1261 * there's a penalty for breaking after it (i.e.
1262 * between it and its description), and a bonus for
1263 * breaking before it (actually _between_ list
1266 pdata
->first
->penalty_before
= -200000;
1267 pdata
->last
->penalty_after
= 200000;
1272 * Most paragraph types: widow/orphan control by
1273 * discouraging breaking one line from the end of
1276 if (pdata
->first
!= pdata
->last
) {
1277 pdata
->first
->penalty_after
= 100000;
1278 pdata
->last
->penalty_before
= 100000;
1283 standard_line_spacing(pdata
, conf
);
1286 * Some kinds of section heading require a page break before
1287 * them and an underline after.
1289 if (ptype
== para_Title
||
1290 ptype
== para_Chapter
||
1291 ptype
== para_Appendix
||
1292 ptype
== para_UnnumberedChapter
) {
1293 pdata
->first
->page_break
= TRUE
;
1294 pdata
->first
->space_before
= conf
->chapter_top_space
;
1295 pdata
->last
->space_after
+=
1296 (conf
->chapter_underline_depth
+
1297 conf
->chapter_underline_thickness
);
1298 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1304 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1309 * Set the line spacing for each line in this paragraph.
1311 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1312 if (ldata
== pdata
->first
)
1313 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1315 ldata
->space_before
= conf
->base_leading
/ 2;
1316 if (ldata
== pdata
->last
)
1317 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1319 ldata
->space_after
= conf
->base_leading
/ 2;
1320 ldata
->page_break
= FALSE
;
1324 static font_encoding
*new_font_encoding(font_data
*font
)
1329 fe
= snew(font_encoding
);
1332 if (font
->list
->tail
)
1333 font
->list
->tail
->next
= fe
;
1335 font
->list
->head
= fe
;
1336 font
->list
->tail
= fe
;
1339 fe
->free_pos
= 0x21;
1341 for (i
= 0; i
< 256; i
++) {
1342 fe
->vector
[i
] = NULL
;
1343 fe
->indices
[i
] = -1;
1344 fe
->to_unicode
[i
] = 0xFFFF;
1350 static int kern_cmp(void *a
, void *b
)
1352 kern_pair
const *ka
= a
, *kb
= b
;
1354 if (ka
->left
< kb
->left
)
1356 if (ka
->left
> kb
->left
)
1358 if (ka
->right
< kb
->right
)
1360 if (ka
->right
> kb
->right
)
1365 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1368 const kern_pair
*kerns
;
1375 widths
= ps_std_font_widths(name
);
1376 kerns
= ps_std_font_kerns(name
);
1377 if (!widths
|| !kerns
)
1380 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
1382 f
= snew(font_data
);
1386 f
->nglyphs
= nglyphs
;
1387 f
->glyphs
= ps_std_glyphs
;
1389 f
->kerns
= newtree234(kern_cmp
);
1390 for (;kerns
->left
!= 0xFFFF; kerns
++)
1391 add234(f
->kerns
, (void *)kerns
);
1392 f
->subfont_map
= snewn(nglyphs
, subfont_map_entry
);
1395 * Our first subfont will contain all of US-ASCII. This isn't
1396 * really necessary - we could just create custom subfonts
1397 * precisely as the whim of render_string dictated - but
1398 * instinct suggests that it might be nice to have the text in
1399 * the output files look _marginally_ recognisable.
1401 fe
= new_font_encoding(f
);
1402 fe
->free_pos
= 0xA1; /* only the top half is free */
1403 f
->latest_subfont
= fe
;
1405 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
1408 for (i
= 0; i
< nglyphs
; i
++) {
1410 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
1411 assert(ucs
!= 0xFFFF);
1413 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1414 fe
->vector
[ucs
] = f
->glyphs
[i
];
1415 fe
->indices
[ucs
] = i
;
1416 fe
->to_unicode
[ucs
] = ucs
;
1417 f
->subfont_map
[i
].subfont
= fe
;
1418 f
->subfont_map
[i
].position
= ucs
;
1421 * This character is not yet assigned to a subfont.
1423 f
->subfont_map
[i
].subfont
= NULL
;
1424 f
->subfont_map
[i
].position
= 0;
1431 /* NB: arguments are glyph numbers from font->bmp. */
1432 static int find_kern(font_data
*font
, int lindex
, int rindex
)
1435 kern_pair
const *kp
;
1437 if (lindex
== 0xFFFF || rindex
== 0xFFFF)
1439 wantkp
.left
= lindex
;
1440 wantkp
.right
= rindex
;
1441 kp
= find234(font
->kerns
, &wantkp
, NULL
);
1447 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1456 for (; *string
; string
++) {
1457 index
= (*string
< 0 || *string
> 0xFFFF ?
0xFFFF :
1458 font
->bmp
[*string
]);
1460 if (index
== 0xFFFF) {
1464 width
+= find_kern(font
, oindex
, index
) + font
->widths
[index
];
1472 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1474 struct paper_width_ctx
{
1480 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1482 while (text
&& text
!= end
) {
1483 w
+= paper_width_internal(vctx
, text
, nspaces
);
1489 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1491 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1492 int style
, type
, findex
, width
, errs
;
1495 switch (word
->type
) {
1496 case word_HyperLink
:
1498 case word_UpperXref
:
1499 case word_LowerXref
:
1506 style
= towordstyle(word
->type
);
1507 type
= removeattr(word
->type
);
1509 findex
= (style
== word_Normal ? FONT_NORMAL
:
1510 style
== word_Emph ? FONT_EMPH
:
1513 if (type
== word_Normal
) {
1515 } else if (type
== word_WhiteSpace
) {
1516 if (findex
!= FONT_CODE
) {
1519 return ctx
->minspacewidth
;
1522 } else /* if (type == word_Quote) */ {
1523 if (word
->aux
== quote_Open
)
1524 str
= ctx
->conf
->lquote
;
1526 str
= ctx
->conf
->rquote
;
1529 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1531 if (errs
&& word
->alt
)
1532 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1534 return ctx
->pdata
->sizes
[findex
] * width
;
1537 static int paper_width(void *vctx
, word
*word
)
1539 return paper_width_internal(vctx
, word
, NULL
);
1542 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
)
1544 struct paper_width_ctx ctx
;
1548 (pdata
->sizes
[FONT_NORMAL
] *
1549 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1552 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1555 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1556 int w
, int i1
, int i2
, paper_conf
*conf
)
1558 wrappedline
*wrapping
, *p
;
1560 struct paper_width_ctx ctx
;
1564 * We're going to need to store the line height in every line
1565 * structure we generate.
1570 for (i
= 0; i
< NFONTS
; i
++)
1571 if (line_height
< pdata
->sizes
[i
])
1572 line_height
= pdata
->sizes
[i
];
1573 line_height
*= UNITS_PER_PT
;
1576 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1577 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1578 if (spacewidth
== 0) {
1580 * A font without a space?! Disturbing. I hope this never
1581 * comes up, but I'll make a random guess anyway and set my
1582 * space width to half the point size.
1584 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * UNITS_PER_PT
/ 2;
1588 * I'm going to set the _minimum_ space width to 3/5 of the
1589 * standard one, and use the standard one as the optimum.
1591 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1595 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1598 * Having done the wrapping, we now concoct a set of line_data
1601 pdata
->first
= pdata
->last
= NULL
;
1603 for (p
= wrapping
; p
; p
= p
->next
) {
1606 int len
, wid
, spaces
;
1608 ldata
= snew(line_data
);
1610 ldata
->pdata
= pdata
;
1611 ldata
->first
= p
->begin
;
1612 ldata
->end
= p
->end
;
1613 ldata
->line_height
= line_height
;
1615 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1618 pdata
->last
->next
= ldata
;
1619 ldata
->prev
= pdata
->last
;
1621 pdata
->first
= ldata
;
1625 pdata
->last
= ldata
;
1628 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1629 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1632 ldata
->hshortfall
= wid
- len
;
1633 ldata
->nspaces
= spaces
;
1635 * This tells us how much the space width needs to
1636 * change from _min_spacewidth. But we want to store
1637 * its difference from the _natural_ space width, to
1638 * make the text rendering easier.
1640 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1641 ldata
->hshortfall
-= spacewidth
* spaces
;
1642 ldata
->real_shortfall
= ldata
->hshortfall
;
1644 * Special case: on the last line of a paragraph, we
1645 * never stretch spaces.
1647 if (ldata
->hshortfall
> 0 && !p
->next
)
1648 ldata
->hshortfall
= 0;
1650 ldata
->aux_text
= NULL
;
1651 ldata
->aux_text_2
= NULL
;
1652 ldata
->aux_left_indent
= 0;
1653 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1658 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1659 int page_height
, int ncols
, int headspace
)
1663 int n
, n1
, this_height
;
1666 * Page breaking is done by a close analogue of the optimal
1667 * paragraph wrapping algorithm used by wrap_para(). We work
1668 * backwards from the end of the document line by line; for
1669 * each line, we contemplate every possible number of lines we
1670 * could put on a page starting with that line, determine a
1671 * cost function for each one, add it to the pre-computed cost
1672 * function for optimally page-breaking everything after that
1673 * page, and pick the best option.
1675 * This is made slightly more complex by the fact that we have
1676 * a multi-column index with a heading at the top of the
1677 * _first_ page, meaning that the first _ncols_ pages must have
1678 * a different length. Hence, we must do the wrapping ncols+1
1679 * times over, hypothetically trying to put every subsequence
1680 * on every possible page.
1682 * Since my line_data structures are only used for this
1683 * purpose, I might as well just store the algorithm data
1687 for (l
= last
; l
; l
= l
->prev
) {
1688 l
->bestcost
= snewn(ncols
+1, int);
1689 l
->vshortfall
= snewn(ncols
+1, int);
1690 l
->text
= snewn(ncols
+1, int);
1691 l
->space
= snewn(ncols
+1, int);
1692 l
->page_last
= snewn(ncols
+1, line_data
*);
1694 for (n
= 0; n
<= ncols
; n
++) {
1695 int minheight
, text
= 0, space
= 0;
1698 n1
= (n
< ncols ? n
+1 : ncols
);
1700 this_height
= page_height
- headspace
;
1702 this_height
= page_height
;
1704 l
->bestcost
[n
] = -1;
1705 for (m
= l
; m
; m
= m
->next
) {
1706 if (m
!= l
&& m
->page_break
)
1707 break; /* we've gone as far as we can */
1710 if (m
->prev
->space_after
> 0)
1711 space
+= m
->prev
->space_after
;
1713 text
+= m
->prev
->space_after
;
1715 if (m
!= l
|| m
->page_break
) {
1716 if (m
->space_before
> 0)
1717 space
+= m
->space_before
;
1719 text
+= m
->space_before
;
1721 text
+= m
->line_height
;
1722 minheight
= text
+ space
;
1724 if (m
!= l
&& minheight
> this_height
)
1728 * If the space after this paragraph is _negative_
1729 * (which means the next line is folded on to this
1730 * one, which happens in the index), we absolutely
1731 * cannot break here.
1733 if (m
->space_after
>= 0) {
1736 * Compute the cost of this arrangement, as the
1737 * square of the amount of wasted space on the
1738 * page. Exception: if this is the last page
1739 * before a mandatory break or the document
1740 * end, we don't penalise a large blank area.
1742 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1744 int x
= (this_height
- minheight
) / FUNITS_PER_PT
*
1752 cost
+= (x
* xf
) >> 8;
1756 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1757 cost
+= m
->penalty_after
;
1758 cost
+= m
->next
->penalty_before
;
1761 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1762 cost
+= m
->next
->bestcost
[n1
];
1763 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1765 * This is the best option yet for this
1768 l
->bestcost
[n
] = cost
;
1769 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1770 l
->vshortfall
[n
] = this_height
- minheight
;
1772 l
->vshortfall
[n
] = 0;
1774 l
->space
[n
] = space
;
1775 l
->page_last
[n
] = m
;
1786 * Now go through the line list forwards and assemble the
1795 int text
, space
, head
;
1797 page
= snew(page_data
);
1806 page
->first_line
= l
;
1807 page
->last_line
= l
->page_last
[n
];
1809 page
->first_text
= page
->last_text
= NULL
;
1810 page
->first_xref
= page
->last_xref
= NULL
;
1811 page
->first_rect
= page
->last_rect
= NULL
;
1814 * Now assign a y-coordinate to each line on the page.
1817 head
= (n
< ncols ? headspace
: 0);
1818 for (l
= page
->first_line
; l
; l
= l
->next
) {
1819 if (l
!= page
->first_line
) {
1820 if (l
->prev
->space_after
> 0)
1821 space
+= l
->prev
->space_after
;
1823 text
+= l
->prev
->space_after
;
1825 if (l
!= page
->first_line
|| l
->page_break
) {
1826 if (l
->space_before
> 0)
1827 space
+= l
->space_before
;
1829 text
+= l
->space_before
;
1831 text
+= l
->line_height
;
1834 l
->ypos
= text
+ space
+ head
;
1835 if (page
->first_line
->space
[n
]) {
1836 l
->ypos
+= space
* (float)page
->first_line
->vshortfall
[n
] /
1837 page
->first_line
->space
[n
];
1840 if (l
== page
->last_line
)
1844 l
= page
->last_line
;
1849 n
= (n
< ncols ? n
+1 : ncols
);
1855 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1857 rect
*r
= snew(rect
);
1860 if (page
->last_rect
)
1861 page
->last_rect
->next
= r
;
1863 page
->first_rect
= r
;
1864 page
->last_rect
= r
;
1872 static void add_string_to_page(page_data
*page
, int x
, int y
,
1873 font_encoding
*fe
, int size
, char *text
,
1876 text_fragment
*frag
;
1878 frag
= snew(text_fragment
);
1881 if (page
->last_text
)
1882 page
->last_text
->next
= frag
;
1884 page
->first_text
= frag
;
1885 page
->last_text
= frag
;
1890 frag
->fontsize
= size
;
1891 frag
->text
= dupstr(text
);
1892 frag
->width
= width
;
1896 * Returns the updated x coordinate.
1898 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1899 int x
, int y
, wchar_t *str
)
1902 int textpos
, textwid
, kern
, glyph
, oglyph
;
1903 font_encoding
*subfont
= NULL
, *sf
;
1905 text
= snewn(1 + ustrlen(str
), char);
1906 textpos
= textwid
= 0;
1911 glyph
= (*str
< 0 || *str
> 0xFFFF ?
0xFFFF :
1914 if (glyph
== 0xFFFF) {
1916 continue; /* nothing more we can do here */
1920 * Find which subfont this character is going in.
1922 sf
= font
->subfont_map
[glyph
].subfont
;
1928 * This character is not yet in a subfont. Assign one.
1930 if (font
->latest_subfont
->free_pos
>= 0x100)
1931 font
->latest_subfont
= new_font_encoding(font
);
1933 c
= font
->latest_subfont
->free_pos
++;
1934 if (font
->latest_subfont
->free_pos
== 0x7F)
1935 font
->latest_subfont
->free_pos
= 0xA1;
1937 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1938 font
->subfont_map
[glyph
].position
= c
;
1939 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1940 font
->latest_subfont
->indices
[c
] = glyph
;
1941 font
->latest_subfont
->to_unicode
[c
] = *str
;
1943 sf
= font
->latest_subfont
;
1946 kern
= find_kern(font
, oglyph
, glyph
) * fontsize
;
1948 if (!subfont
|| sf
!= subfont
|| kern
) {
1950 text
[textpos
] = '\0';
1951 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
,
1953 x
+= textwid
+ kern
;
1955 assert(textpos
== 0);
1962 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1963 textwid
+= font
->widths
[glyph
] * fontsize
;
1969 text
[textpos
] = '\0';
1970 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
, textwid
);
1978 * Returns the updated x coordinate.
1980 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1981 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1982 int shortfall
, int nspaces
, int *nspace
,
1983 keywordlist
*keywords
, indexdata
*idx
, paper_conf
*conf
)
1985 while (text
&& text
!= text_end
) {
1986 int style
, type
, findex
, errs
;
1990 switch (text
->type
) {
1992 * Start a cross-reference.
1994 case word_HyperLink
:
1995 case word_UpperXref
:
1996 case word_LowerXref
:
1999 if (text
->type
== word_HyperLink
) {
2001 dest
.url
= utoa_dup(text
->text
, CS_ASCII
);
2003 } else if (text
->type
== word_PageXref
) {
2006 dest
.page
= (page_data
*)text
->private_data
;
2008 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
2012 assert(kwl
->para
->private_data
);
2013 pdata
= (para_data
*) kwl
->para
->private_data
;
2015 dest
.page
= pdata
->first
->page
;
2019 * Shouldn't happen, but *shrug*
2026 if (dest
.type
!= NONE
) {
2028 (*xr
)->dest
= dest
; /* structure copy */
2029 if (page
->last_xref
)
2030 page
->last_xref
->next
= *xr
;
2032 page
->first_xref
= *xr
;
2033 page
->last_xref
= *xr
;
2037 * FIXME: Ideally we should have, and use, some
2038 * vertical font metric information here so that
2039 * our cross-ref rectangle can take account of
2040 * descenders and the font's cap height. This will
2041 * do for the moment, but it isn't ideal.
2043 (*xr
)->lx
= (*xr
)->rx
= x
;
2045 (*xr
)->ty
= y
+ ldata
->line_height
;
2050 * Finish extending a cross-reference box.
2058 * Add the current page number to the list of pages
2059 * referenced by an index entry.
2063 * We don't create index references in contents entries.
2065 if (!pdata
->contents_entry
) {
2069 tag
= index_findtag(idx
, text
->text
);
2073 for (i
= 0; i
< tag
->nrefs
; i
++) {
2074 indexentry
*entry
= tag
->refs
[i
];
2075 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
2078 * If the same index term is indexed twice
2079 * within the same section, we only want to
2080 * mention it once in the index.
2082 if (pi
->lastpage
!= page
) {
2086 pi
->lastword
= pi
->lastword
->next
=
2088 pi
->lastword
= pi
->lastword
->next
=
2090 wp
= &pi
->lastword
->next
;
2094 pi
->lastword
= *wp
=
2095 fake_page_ref(page
);
2096 pi
->lastword
= pi
->lastword
->next
=
2097 fake_word(page
->number
);
2098 pi
->lastword
= pi
->lastword
->next
=
2102 pi
->lastpage
= page
;
2108 style
= towordstyle(text
->type
);
2109 type
= removeattr(text
->type
);
2111 findex
= (style
== word_Normal ? FONT_NORMAL
:
2112 style
== word_Emph ? FONT_EMPH
:
2115 if (type
== word_Normal
) {
2117 } else if (type
== word_WhiteSpace
) {
2118 x
+= pdata
->sizes
[findex
] *
2119 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
2120 if (nspaces
&& findex
!= FONT_CODE
) {
2121 x
+= (*nspace
+1) * shortfall
/ nspaces
;
2122 x
-= *nspace
* shortfall
/ nspaces
;
2126 } else /* if (type == word_Quote) */ {
2127 if (text
->aux
== quote_Open
)
2133 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
2135 if (errs
&& text
->alt
)
2136 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
2137 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
,
2140 x
= render_string(page
, pdata
->fonts
[findex
],
2141 pdata
->sizes
[findex
], x
, y
, str
);
2154 * Returns the last x position used on the line.
2156 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
2157 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
2164 if (ldata
->aux_text
) {
2168 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2169 left_x
+ ldata
->aux_left_indent
,
2170 top_y
- ldata
->ypos
,
2171 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
2172 keywords
, idx
, conf
);
2173 if (ldata
->aux_text_2
)
2174 render_text(ldata
->page
, ldata
->pdata
, ldata
,
2175 x
, top_y
- ldata
->ypos
,
2176 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
2177 keywords
, idx
, conf
);
2183 * There might be a cross-reference carried over from a
2186 if (dest
->type
!= NONE
) {
2189 xr
->dest
= *dest
; /* structure copy */
2190 if (ldata
->page
->last_xref
)
2191 ldata
->page
->last_xref
->next
= xr
;
2193 ldata
->page
->first_xref
= xr
;
2194 ldata
->page
->last_xref
= xr
;
2195 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
2196 xr
->by
= top_y
- ldata
->ypos
;
2197 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
2202 int extra_indent
, shortfall
, spaces
;
2203 int just
= ldata
->pdata
->justification
;
2206 * All forms of justification become JUST when we have
2207 * to squeeze the paragraph.
2209 if (ldata
->hshortfall
< 0)
2214 shortfall
= ldata
->hshortfall
;
2215 spaces
= ldata
->nspaces
;
2219 shortfall
= spaces
= extra_indent
= 0;
2222 shortfall
= spaces
= 0;
2223 extra_indent
= ldata
->real_shortfall
;
2227 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2228 left_x
+ ldata
->xpos
+ extra_indent
,
2229 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
2230 &xr
, shortfall
, spaces
, &nspace
,
2231 keywords
, idx
, conf
);
2236 * There's a cross-reference continued on to the next line.
2246 static void render_para(para_data
*pdata
, paper_conf
*conf
,
2247 keywordlist
*keywords
, indexdata
*idx
,
2248 paragraph
*index_placeholder
, page_data
*index_page
)
2252 page_data
*cxref_page
;
2261 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
2263 * If this is a contents entry, we expect to have a single
2264 * enormous cross-reference rectangle covering the whole
2265 * thing. (Unless, of course, it spans multiple pages.)
2267 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
2268 cxref_page
= ldata
->page
;
2271 cxref
->dest
.type
= PAGE
;
2272 if (pdata
->contents_entry
== index_placeholder
) {
2273 cxref
->dest
.page
= index_page
;
2275 assert(pdata
->contents_entry
->private_data
);
2276 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2277 cxref
->dest
.page
= target
->first
->page
;
2279 cxref
->dest
.url
= NULL
;
2280 if (ldata
->page
->last_xref
)
2281 ldata
->page
->last_xref
->next
= cxref
;
2283 ldata
->page
->first_xref
= cxref
;
2284 ldata
->page
->last_xref
= cxref
;
2285 cxref
->lx
= conf
->left_margin
;
2286 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
2287 cxref
->ty
= conf
->paper_height
- conf
->top_margin
2288 - ldata
->ypos
+ ldata
->line_height
;
2290 if (pdata
->contents_entry
) {
2291 assert(cxref
!= NULL
);
2292 cxref
->by
= conf
->paper_height
- conf
->top_margin
2296 last_x
= render_line(ldata
, conf
->left_margin
,
2297 conf
->paper_height
- conf
->top_margin
,
2298 &dest
, keywords
, idx
, conf
);
2299 if (ldata
== pdata
->last
)
2304 * If this is a contents entry, add leaders and a page
2307 if (pdata
->contents_entry
) {
2313 if (pdata
->contents_entry
== index_placeholder
) {
2314 num
= index_page
->number
;
2316 assert(pdata
->contents_entry
->private_data
);
2317 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2318 num
= target
->first
->page
->number
;
2322 wid
= paper_width_simple(pdata
, w
, conf
);
2325 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2326 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2327 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2328 render_string(pdata
->last
->page
,
2329 pdata
->fonts
[FONT_NORMAL
],
2330 pdata
->sizes
[FONT_NORMAL
],
2331 conf
->left_margin
+ x
,
2332 (conf
->paper_height
- conf
->top_margin
-
2333 pdata
->last
->ypos
), L
".");
2335 render_string(pdata
->last
->page
,
2336 pdata
->fonts
[FONT_NORMAL
],
2337 pdata
->sizes
[FONT_NORMAL
],
2338 conf
->paper_width
- conf
->right_margin
- wid
,
2339 (conf
->paper_height
- conf
->top_margin
-
2340 pdata
->last
->ypos
), num
);
2344 * Render any rectangle (chapter title underline or rule)
2345 * that goes with this paragraph.
2347 switch (pdata
->rect_type
) {
2348 case RECT_CHAPTER_UNDERLINE
:
2349 add_rect_to_page(pdata
->last
->page
,
2351 (conf
->paper_height
- conf
->top_margin
-
2353 conf
->chapter_underline_depth
),
2355 conf
->chapter_underline_thickness
);
2358 add_rect_to_page(pdata
->first
->page
,
2359 conf
->left_margin
+ pdata
->first
->xpos
,
2360 (conf
->paper_height
- conf
->top_margin
-
2362 pdata
->last
->line_height
),
2363 conf
->base_width
- pdata
->first
->xpos
,
2364 pdata
->last
->line_height
);
2366 default: /* placate gcc */
2371 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2373 para_data
*pdata
= snew(para_data
);
2376 * For code paragraphs, I'm going to hack grievously and
2377 * pretend the three normal fonts are the three code paragraph
2380 setfont(pdata
, &conf
->fcode
);
2382 pdata
->first
= pdata
->last
= NULL
;
2383 pdata
->outline_level
= -1;
2384 pdata
->rect_type
= RECT_NONE
;
2385 pdata
->contents_entry
= NULL
;
2386 pdata
->justification
= LEFT
;
2388 for (; words
; words
= words
->next
) {
2389 wchar_t *t
, *e
, *start
;
2390 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2392 int prev
= -1, curr
;
2395 if (words
->next
&& words
->next
->type
== word_Emph
) {
2396 e
= words
->next
->text
;
2397 words
= words
->next
;
2407 else if (*e
== L
'i')
2409 else if (*e
== L
'b')
2426 * We've isolated a maximal subsequence of the line
2427 * which has the same emphasis. Form it into a word
2433 w
->type
= (prev
== 0 ? word_WeakCode
:
2434 prev
== 1 ? word_Emph
: word_Normal
);
2435 w
->text
= snewn(t
-start
+1, wchar_t);
2436 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2437 w
->text
[t
-start
] = '\0';
2450 ldata
= snew(line_data
);
2452 ldata
->pdata
= pdata
;
2453 ldata
->first
= lhead
;
2455 ldata
->line_height
= conf
->fcode
.font_size
* UNITS_PER_PT
;
2457 ldata
->xpos
= indent
;
2460 pdata
->last
->next
= ldata
;
2461 ldata
->prev
= pdata
->last
;
2463 pdata
->first
= ldata
;
2467 pdata
->last
= ldata
;
2469 ldata
->hshortfall
= 0;
2471 ldata
->aux_text
= NULL
;
2472 ldata
->aux_text_2
= NULL
;
2473 ldata
->aux_left_indent
= 0;
2474 /* General opprobrium for breaking in a code paragraph. */
2475 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2478 standard_line_spacing(pdata
, conf
);
2483 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2485 para_data
*pdata
= snew(para_data
);
2488 ldata
= snew(line_data
);
2490 ldata
->pdata
= pdata
;
2491 ldata
->first
= NULL
;
2493 ldata
->line_height
= conf
->rule_thickness
;
2495 ldata
->xpos
= indent
;
2500 ldata
->hshortfall
= 0;
2502 ldata
->aux_text
= NULL
;
2503 ldata
->aux_text_2
= NULL
;
2504 ldata
->aux_left_indent
= 0;
2507 * Better to break after a rule than before it
2509 ldata
->penalty_after
+= 100000;
2510 ldata
->penalty_before
+= -100000;
2512 pdata
->first
= pdata
->last
= ldata
;
2513 pdata
->outline_level
= -1;
2514 pdata
->rect_type
= RECT_RULE
;
2515 pdata
->contents_entry
= NULL
;
2516 pdata
->justification
= LEFT
;
2518 standard_line_spacing(pdata
, conf
);
2524 * Plain-text-like formatting for outline titles.
2526 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2527 for (; text
; text
= text
->next
) switch (text
->type
) {
2528 case word_HyperLink
:
2530 case word_UpperXref
:
2531 case word_LowerXref
:
2540 case word_WhiteSpace
:
2541 case word_EmphSpace
:
2542 case word_CodeSpace
:
2543 case word_WkCodeSpace
:
2545 case word_EmphQuote
:
2546 case word_CodeQuote
:
2547 case word_WkCodeQuote
:
2548 assert(text
->type
!= word_CodeQuote
&&
2549 text
->type
!= word_WkCodeQuote
);
2550 if (towordstyle(text
->type
) == word_Emph
&&
2551 (attraux(text
->aux
) == attr_First
||
2552 attraux(text
->aux
) == attr_Only
))
2553 rdadd(rs
, L
'_'); /* FIXME: configurability */
2554 else if (towordstyle(text
->type
) == word_Code
&&
2555 (attraux(text
->aux
) == attr_First
||
2556 attraux(text
->aux
) == attr_Only
))
2557 rdadd(rs
, L
'\''); /* FIXME: configurability */
2558 if (removeattr(text
->type
) == word_Normal
) {
2559 rdadds(rs
, text
->text
);
2560 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2562 } else if (removeattr(text
->type
) == word_Quote
) {
2563 rdadd(rs
, L
'\''); /* fixme: configurability */
2565 if (towordstyle(text
->type
) == word_Emph
&&
2566 (attraux(text
->aux
) == attr_Last
||
2567 attraux(text
->aux
) == attr_Only
))
2568 rdadd(rs
, L
'_'); /* FIXME: configurability */
2569 else if (towordstyle(text
->type
) == word_Code
&&
2570 (attraux(text
->aux
) == attr_Last
||
2571 attraux(text
->aux
) == attr_Only
))
2572 rdadd(rs
, L
'\''); /* FIXME: configurability */
2577 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2580 rdstring rs
= {0, 0, NULL
};
2583 paper_rdaddw(&rs
, first
);
2585 rdadds(&rs
, separator
);
2587 paper_rdaddw(&rs
, second
);
2592 static word
*fake_word(wchar_t *text
)
2594 word
*ret
= snew(word
);
2597 ret
->type
= word_Normal
;
2598 ret
->text
= ustrdup(text
);
2599 ret
->breaks
= FALSE
;
2604 static word
*fake_space_word(void)
2606 word
*ret
= snew(word
);
2609 ret
->type
= word_WhiteSpace
;
2616 static word
*fake_page_ref(page_data
*page
)
2618 word
*ret
= snew(word
);
2621 ret
->type
= word_PageXref
;
2623 ret
->breaks
= FALSE
;
2625 ret
->private_data
= page
;
2629 static word
*fake_end_ref(void)
2631 word
*ret
= snew(word
);
2634 ret
->type
= word_XrefEnd
;
2636 ret
->breaks
= FALSE
;
2641 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2650 w
= dup_word_list(first
);
2658 w
= fake_word(separator
);
2664 *wptr
= dup_word_list(second
);
2670 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2674 if (!src
->first_line
)
2677 if (dest
->last_line
) {
2678 dest
->last_line
->next
= src
->first_line
;
2679 src
->first_line
->prev
= dest
->last_line
;
2681 dest
->last_line
= src
->last_line
;
2683 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2685 ldata
->xpos
+= right_shift
;
2687 if (ldata
== src
->last_line
)