2 * Paper printing pre-backend for Halibut.
4 * This module does all the processing common to both PostScript
5 * and PDF output: selecting fonts, line wrapping and page breaking
6 * in accordance with font metrics, laying out the contents and
7 * index pages, generally doing all the page layout. After this,
8 * bk_ps.c and bk_pdf.c should only need to do linear translations
9 * into their literal output format.
13 * TODO in future work:
15 * - linearised PDF, perhaps?
17 * - we should use PDFDocEncoding or Unicode for outline strings,
18 * now that I actually know how to do them. Probably easiest if
19 * I do this _after_ bringing in libcharset, since I can simply
20 * supply PDFDocEncoding in there.
22 * - I'm uncertain of whether I need to include a ToUnicode CMap
23 * in each of my font definitions in PDF. Currently things (by
24 * which I mean cut and paste out of acroread) seem to be
25 * working fairly happily without it, but I don't know.
27 * - rather than the ugly aux_text mechanism for rendering chapter
28 * titles, we could actually build the correct word list and
31 * - get vertical font metrics and use them to position the PDF
32 * xref boxes more pleasantly
35 * * page header and footer should be configurable; we should
36 * be able to shift the page number elsewhere, and add other
37 * things such as the current chapter/section title and fixed
39 * * remove the fixed mapping from heading levels to heading
40 * styles; offer a menu of styles from which the user can
41 * choose at every heading level
42 * * first-line indent in paragraphs
43 * * fixed text: `Contents', `Index', the colon-space and full
44 * stop in chapter title constructions
45 * * configurable location of contents?
46 * * certainly configurably _remove_ the contents, and possibly
48 * * double-sided document switch?
49 * + means you have two header/footer formats which
51 * + and means that mandatory page breaks before chapter
52 * titles should include a blank page if necessary to
53 * start the next section to a right-hand page
57 * - ability to import other Type 1 fonts
58 * * we need to parse the font to extract its metrics
59 * * then we pass the font bodily to both PS and PDF so it can
60 * be included in the output file
62 * - character substitution for better typography?
63 * * fi, fl, ffi, ffl ligatures
64 * * use real ellipsis rather than ...
65 * * a hyphen in a word by itself might prefer to be an en-dash
66 * * (Americans might even want a convenient way to use an
68 * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw!
69 * * substituting `minus' for `hyphen' in the standard encoding
70 * is probably preferable in Courier, though certainly not in
72 * * if I do do this lot, I'm rather inclined to at least try
73 * to think up a configurable way to do it so that Americans
74 * can do em-dash tricks without my intervention and other
75 * people can do other odd things too.
86 typedef struct paper_conf_Tag paper_conf
;
87 typedef struct paper_idx_Tag paper_idx
;
90 font_data
*fonts
[NFONTS
];
94 struct paper_conf_Tag
{
101 int indent_list_bullet
;
102 int indent_list_after
;
106 int base_para_spacing
;
107 int chapter_top_space
;
108 int sect_num_left_space
;
109 int chapter_underline_depth
;
110 int chapter_underline_thickness
;
112 font_cfg fbase
, fcode
, ftitle
, fchapter
, *fsect
;
114 int contents_indent_step
;
116 int leader_separation
;
120 int pagenum_fontsize
;
122 wchar_t *lquote
, *rquote
, *bullet
;
123 wchar_t *contents_text
, *index_text
;
124 /* These are derived from the above */
130 struct paper_idx_Tag
{
132 * Word list giving the page numbers on which this index entry
133 * appears. Also the last word in the list, for ease of
139 * The last page added to the list (so we can ensure we don't
146 word_PageXref
= word_NotWordType
+ 1
149 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
150 static void wrap_paragraph(para_data
*pdata
, word
*words
,
151 int w
, int i1
, int i2
, paper_conf
*conf
);
152 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
153 int page_height
, int ncols
, int headspace
);
154 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
155 int x
, int y
, wchar_t *str
);
156 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
157 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
159 static void render_para(para_data
*pdata
, paper_conf
*conf
,
160 keywordlist
*keywords
, indexdata
*idx
,
161 paragraph
*index_placeholder
, page_data
*index_page
);
162 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
163 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
);
164 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
165 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
166 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
167 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
168 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
170 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
171 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
173 static word
*fake_word(wchar_t *text
);
174 static word
*fake_space_word(void);
175 static word
*fake_page_ref(page_data
*page
);
176 static word
*fake_end_ref(void);
177 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
179 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
181 static int fonts_ok(wchar_t *string
, ...)
187 va_start(ap
, string
);
188 while ( (font
= va_arg(ap
, font_data
*)) != NULL
) {
190 (void) string_width(font
, string
, &errs
);
201 static void paper_cfg_fonts(font_data
**fonts
, font_list
*fontlist
,
202 wchar_t *wp
, filepos
*fpos
) {
207 for (i
= 0; i
< NFONTS
&& *wp
; i
++, wp
= uadv(wp
)) {
208 fn
= utoa_dup(wp
, CS_ASCII
);
209 f
= make_std_font(fontlist
, fn
);
213 /* FIXME: proper error */
214 error(err_nofont
, fpos
, wp
);
218 static paper_conf
paper_configure(paragraph
*source
, font_list
*fontlist
) {
225 ret
.paper_width
= 595 * UNITS_PER_PT
;
226 ret
.paper_height
= 842 * UNITS_PER_PT
;
227 ret
.left_margin
= 72 * UNITS_PER_PT
;
228 ret
.top_margin
= 72 * UNITS_PER_PT
;
229 ret
.right_margin
= 72 * UNITS_PER_PT
;
230 ret
.bottom_margin
= 108 * UNITS_PER_PT
;
231 ret
.indent_list_bullet
= 6 * UNITS_PER_PT
;
232 ret
.indent_list_after
= 18 * UNITS_PER_PT
;
233 ret
.indent_quote
= 18 * UNITS_PER_PT
;
234 ret
.base_leading
= UNITS_PER_PT
;
235 ret
.base_para_spacing
= 10 * UNITS_PER_PT
;
236 ret
.chapter_top_space
= 72 * UNITS_PER_PT
;
237 ret
.sect_num_left_space
= 12 * UNITS_PER_PT
;
238 ret
.chapter_underline_depth
= 14 * UNITS_PER_PT
;
239 ret
.chapter_underline_thickness
= 3 * UNITS_PER_PT
;
240 ret
.rule_thickness
= 1 * UNITS_PER_PT
;
241 ret
.fbase
.font_size
= 12;
242 ret
.fbase
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Times-Roman");
243 ret
.fbase
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Times-Italic");
244 ret
.fbase
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
245 ret
.fcode
.font_size
= 12;
246 ret
.fcode
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Courier-Bold");
247 ret
.fcode
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Courier-Oblique");
248 ret
.fcode
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
249 ret
.ftitle
.font_size
= 24;
250 ret
.ftitle
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Helvetica-Bold");
251 ret
.ftitle
.fonts
[FONT_EMPH
] =
252 make_std_font(fontlist
, "Helvetica-BoldOblique");
253 ret
.ftitle
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
254 ret
.fchapter
.font_size
= 20;
255 ret
.fchapter
.fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
256 ret
.fchapter
.fonts
[FONT_EMPH
] =
257 make_std_font(fontlist
, "Helvetica-BoldOblique");
258 ret
.fchapter
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
260 ret
.fsect
= snewn(ret
.nfsect
, font_cfg
);
261 ret
.fsect
[0].font_size
= 16;
262 ret
.fsect
[0].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
263 ret
.fsect
[0].fonts
[FONT_EMPH
] =
264 make_std_font(fontlist
, "Helvetica-BoldOblique");
265 ret
.fsect
[0].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
266 ret
.fsect
[1].font_size
= 14;
267 ret
.fsect
[1].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
268 ret
.fsect
[1].fonts
[FONT_EMPH
] =
269 make_std_font(fontlist
, "Helvetica-BoldOblique");
270 ret
.fsect
[1].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
271 ret
.fsect
[2].font_size
= 13;
272 ret
.fsect
[2].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
273 ret
.fsect
[2].fonts
[FONT_EMPH
] =
274 make_std_font(fontlist
, "Helvetica-BoldOblique");
275 ret
.fsect
[2].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
276 ret
.contents_indent_step
= 24 * UNITS_PER_PT
;
277 ret
.contents_margin
= 84 * UNITS_PER_PT
;
278 ret
.leader_separation
= 12 * UNITS_PER_PT
;
279 ret
.index_gutter
= 36 * UNITS_PER_PT
;
281 ret
.index_minsep
= 18 * UNITS_PER_PT
;
282 ret
.pagenum_fontsize
= 12;
283 ret
.footer_distance
= 32 * UNITS_PER_PT
;
284 ret
.lquote
= L
"\x2018\0\x2019\0'\0'\0\0";
285 ret
.rquote
= uadv(ret
.lquote
);
286 ret
.bullet
= L
"\x2022\0-\0\0";
287 ret
.contents_text
= L
"Contents";
288 ret
.index_text
= L
"Index";
291 * Two-pass configuration so that we can pick up global config
292 * (e.g. `quotes') before having it overridden by specific
293 * config (`paper-quotes'), irrespective of the order in which
296 for (p
= source
; p
; p
= p
->next
) {
297 if (p
->type
== para_Config
) {
298 if (!ustricmp(p
->keyword
, L
"quotes")) {
299 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
300 ret
.lquote
= uadv(p
->keyword
);
301 ret
.rquote
= uadv(ret
.lquote
);
307 for (p
= source
; p
; p
= p
->next
) {
308 p
->private_data
= NULL
;
309 if (p
->type
== para_Config
) {
310 if (!ustricmp(p
->keyword
, L
"paper-quotes")) {
311 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
312 ret
.lquote
= uadv(p
->keyword
);
313 ret
.rquote
= uadv(ret
.lquote
);
315 } else if (!ustricmp(p
->keyword
, L
"contents")) {
316 ret
.contents_text
= uadv(p
->keyword
);
317 } else if (!ustricmp(p
->keyword
, L
"index")) {
318 ret
.index_text
= uadv(p
->keyword
);
319 } else if (!ustricmp(p
->keyword
, L
"paper-bullet")) {
320 ret
.bullet
= uadv(p
->keyword
);
321 } else if (!ustricmp(p
->keyword
, L
"paper-page-width")) {
323 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
324 } else if (!ustricmp(p
->keyword
, L
"paper-page-height")) {
326 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
327 } else if (!ustricmp(p
->keyword
, L
"paper-left-margin")) {
329 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
330 } else if (!ustricmp(p
->keyword
, L
"paper-top-margin")) {
332 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
333 } else if (!ustricmp(p
->keyword
, L
"paper-right-margin")) {
335 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
336 } else if (!ustricmp(p
->keyword
, L
"paper-bottom-margin")) {
338 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
339 } else if (!ustricmp(p
->keyword
, L
"paper-list-indent")) {
340 ret
.indent_list_bullet
=
341 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
342 } else if (!ustricmp(p
->keyword
, L
"paper-listitem-indent")) {
344 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
345 } else if (!ustricmp(p
->keyword
, L
"paper-quote-indent")) {
347 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
348 } else if (!ustricmp(p
->keyword
, L
"paper-base-leading")) {
350 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
351 } else if (!ustricmp(p
->keyword
, L
"paper-base-para-spacing")) {
352 ret
.base_para_spacing
=
353 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
354 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-top-space")) {
355 ret
.chapter_top_space
=
356 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
357 } else if (!ustricmp(p
->keyword
, L
"paper-sect-num-left-space")) {
358 ret
.sect_num_left_space
=
359 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
360 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-depth")) {
361 ret
.chapter_underline_depth
=
362 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
363 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-thickness")) {
364 ret
.chapter_underline_thickness
=
365 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
366 } else if (!ustricmp(p
->keyword
, L
"paper-rule-thickness")) {
368 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
369 } else if (!ustricmp(p
->keyword
, L
"paper-contents-indent-step")) {
370 ret
.contents_indent_step
=
371 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
372 } else if (!ustricmp(p
->keyword
, L
"paper-contents-margin")) {
373 ret
.contents_margin
=
374 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
375 } else if (!ustricmp(p
->keyword
, L
"paper-leader-separation")) {
376 ret
.leader_separation
=
377 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
378 } else if (!ustricmp(p
->keyword
, L
"paper-index-gutter")) {
380 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
381 } else if (!ustricmp(p
->keyword
, L
"paper-index-minsep")) {
383 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
384 } else if (!ustricmp(p
->keyword
, L
"paper-footer-distance")) {
385 ret
.footer_distance
=
386 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
387 } else if (!ustricmp(p
->keyword
, L
"paper-base-font-size")) {
388 ret
.fbase
.font_size
= utoi(uadv(p
->keyword
));
389 } else if (!ustricmp(p
->keyword
, L
"paper-index-columns")) {
390 ret
.index_cols
= utoi(uadv(p
->keyword
));
391 } else if (!ustricmp(p
->keyword
, L
"paper-pagenum-font-size")) {
392 ret
.pagenum_fontsize
= utoi(uadv(p
->keyword
));
393 } else if (!ustricmp(p
->keyword
, L
"paper-base-fonts")) {
394 paper_cfg_fonts(ret
.fbase
.fonts
, fontlist
, uadv(p
->keyword
),
396 } else if (!ustricmp(p
->keyword
, L
"paper-code-font-size")) {
397 ret
.fcode
.font_size
= utoi(uadv(p
->keyword
));
398 } else if (!ustricmp(p
->keyword
, L
"paper-code-fonts")) {
399 paper_cfg_fonts(ret
.fcode
.fonts
, fontlist
, uadv(p
->keyword
),
401 } else if (!ustricmp(p
->keyword
, L
"paper-title-font-size")) {
402 ret
.ftitle
.font_size
= utoi(uadv(p
->keyword
));
403 } else if (!ustricmp(p
->keyword
, L
"paper-title-fonts")) {
404 paper_cfg_fonts(ret
.ftitle
.fonts
, fontlist
, uadv(p
->keyword
),
406 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-font-size")) {
407 ret
.fchapter
.font_size
= utoi(uadv(p
->keyword
));
408 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-fonts")) {
409 paper_cfg_fonts(ret
.fchapter
.fonts
, fontlist
, uadv(p
->keyword
),
411 } else if (!ustricmp(p
->keyword
, L
"paper-section-font-size")) {
412 wchar_t *q
= uadv(p
->keyword
);
418 if (n
>= ret
.nfsect
) {
420 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
421 for (i
= ret
.nfsect
; i
<= n
; i
++)
422 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
425 ret
.fsect
[n
].font_size
= utoi(q
);
426 } else if (!ustricmp(p
->keyword
, L
"paper-section-fonts")) {
427 wchar_t *q
= uadv(p
->keyword
);
433 if (n
>= ret
.nfsect
) {
435 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
436 for (i
= ret
.nfsect
; i
<= n
; i
++)
437 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
440 paper_cfg_fonts(ret
.fsect
[n
].fonts
, fontlist
, q
, &p
->fpos
);
446 * Set up the derived fields in the conf structure.
450 ret
.paper_width
- ret
.left_margin
- ret
.right_margin
;
452 ret
.paper_height
- ret
.top_margin
- ret
.bottom_margin
;
453 ret
.indent_list
= ret
.indent_list_bullet
+ ret
.indent_list_after
;
455 (ret
.base_width
- (ret
.index_cols
-1) * ret
.index_gutter
)
459 * Now process fallbacks on quote characters and bullets. We
460 * use string_width() to determine whether all of the relevant
461 * fonts contain the same character, and fall back whenever we
462 * find a character which not all of them support.
465 /* Quote characters need not be supported in the fixed code fonts,
466 * but must be in the title and body fonts. */
467 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
))) {
469 if (fonts_ok(ret
.lquote
,
470 ret
.fbase
.fonts
[FONT_NORMAL
],
471 ret
.fbase
.fonts
[FONT_EMPH
],
472 ret
.ftitle
.fonts
[FONT_NORMAL
],
473 ret
.ftitle
.fonts
[FONT_EMPH
],
474 ret
.fchapter
.fonts
[FONT_NORMAL
],
475 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
) &&
477 ret
.fbase
.fonts
[FONT_NORMAL
],
478 ret
.fbase
.fonts
[FONT_EMPH
],
479 ret
.ftitle
.fonts
[FONT_NORMAL
],
480 ret
.ftitle
.fonts
[FONT_EMPH
],
481 ret
.fchapter
.fonts
[FONT_NORMAL
],
482 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
)) {
483 for (n
= 0; n
< ret
.nfsect
; n
++)
484 if (!fonts_ok(ret
.lquote
,
485 ret
.fsect
[n
].fonts
[FONT_NORMAL
],
486 ret
.fsect
[n
].fonts
[FONT_EMPH
], NULL
) ||
487 !fonts_ok(ret
.rquote
,
488 ret
.fsect
[n
].fonts
[FONT_NORMAL
],
489 ret
.fsect
[n
].fonts
[FONT_EMPH
], NULL
))
494 ret
.lquote
= uadv(ret
.rquote
);
495 ret
.rquote
= uadv(ret
.lquote
);
498 /* The bullet character only needs to be supported in the normal body
499 * font (not even in italics). */
500 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
501 !fonts_ok(ret
.bullet
, ret
.fbase
.fonts
[FONT_NORMAL
], NULL
))
502 ret
.bullet
= uadv(ret
.bullet
);
507 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
511 int indent
, used_contents
;
512 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
513 para_data
*firstcont
, *lastcont
;
514 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
517 paper_conf
*conf
, ourconf
;
520 paragraph index_placeholder_para
;
521 page_data
*first_index_page
;
524 fontlist
= snew(font_list
);
525 fontlist
->head
= fontlist
->tail
= NULL
;
527 ourconf
= paper_configure(sourceform
, fontlist
);
531 * Set up a data structure to collect page numbers for each
540 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
541 paper_idx
*pi
= snew(paper_idx
);
545 pi
->words
= pi
->lastword
= NULL
;
548 entry
->backend_data
= pi
;
553 * Format the contents entry for each heading.
556 word
*contents_title
;
557 contents_title
= fake_word(conf
->contents_text
);
559 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
560 NULL
, NULL
, contents_title
, conf
);
561 lastcont
= firstcont
;
562 lastcont
->next
= NULL
;
563 firstcontline
= firstcont
->first
;
564 lastcontline
= lastcont
->last
;
565 for (p
= sourceform
; p
; p
= p
->next
) {
572 case para_UnnumberedChapter
:
578 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
581 case para_UnnumberedChapter
:
582 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
587 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
588 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
591 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
592 conf
->contents_margin
,
593 NULL
, NULL
, words
, conf
);
595 pdata
->contents_entry
= p
;
596 lastcont
->next
= pdata
;
600 * Link all contents line structures together into
605 lastcontline
->next
= pdata
->first
;
606 pdata
->first
->prev
= lastcontline
;
608 firstcontline
= pdata
->first
;
609 pdata
->first
->prev
= NULL
;
611 lastcontline
= pdata
->last
;
612 lastcontline
->next
= NULL
;
620 * And one extra one, for the index.
623 pdata
= make_para_data(para_Normal
, 0, 0,
624 conf
->contents_margin
,
626 fake_word(conf
->index_text
), conf
);
628 pdata
->contents_entry
= &index_placeholder_para
;
629 lastcont
->next
= pdata
;
634 lastcontline
->next
= pdata
->first
;
635 pdata
->first
->prev
= lastcontline
;
637 firstcontline
= pdata
->first
;
638 pdata
->first
->prev
= NULL
;
640 lastcontline
= pdata
->last
;
641 lastcontline
->next
= NULL
;
647 * Do the main paragraph formatting.
650 used_contents
= FALSE
;
651 firstline
= lastline
= NULL
;
652 for (p
= sourceform
; p
; p
= p
->next
) {
653 p
->private_data
= NULL
;
657 * These paragraph types are either invisible or don't
658 * define text in the normal sense. Either way, they
659 * don't require wrapping.
664 case para_NotParaType
:
671 * These paragraph types don't require wrapping, but
672 * they do affect the line width to which we wrap the
673 * rest of the paragraphs, so we need to pay attention.
676 indent
+= conf
->indent_list
; break;
678 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
680 indent
+= conf
->indent_quote
; break;
682 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
685 * This paragraph type is special. Process it
689 pdata
= code_paragraph(indent
, p
->words
, conf
);
690 p
->private_data
= pdata
;
691 if (pdata
->first
!= pdata
->last
) {
692 pdata
->first
->penalty_after
+= 100000;
693 pdata
->last
->penalty_before
+= 100000;
698 * This paragraph is also special.
701 pdata
= rule_paragraph(indent
, conf
);
702 p
->private_data
= pdata
;
706 * All of these paragraph types require wrapping in the
707 * ordinary way. So we must supply a set of fonts, a
708 * line width and auxiliary information (e.g. bullet
709 * text) for each one.
713 case para_UnnumberedChapter
:
717 case para_BiblioCited
:
719 case para_NumberedList
:
720 case para_DescribedThing
:
721 case para_Description
:
724 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
725 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
727 p
->private_data
= pdata
;
732 if (p
->private_data
) {
733 pdata
= (para_data
*)p
->private_data
;
736 * If this is the first non-title heading, we link the
737 * contents section in before it.
739 if (!used_contents
&& pdata
->outline_level
> 0) {
740 used_contents
= TRUE
;
742 lastpara
->next
= firstcont
;
744 firstpara
= firstcont
;
746 assert(lastpara
->next
== NULL
);
749 lastline
->next
= firstcontline
;
750 firstcontline
->prev
= lastline
;
752 firstline
= firstcontline
;
753 firstcontline
->prev
= NULL
;
755 assert(lastcontline
!= NULL
);
756 lastline
= lastcontline
;
757 lastline
->next
= NULL
;
761 * Link all line structures together into a big list.
765 lastline
->next
= pdata
->first
;
766 pdata
->first
->prev
= lastline
;
768 firstline
= pdata
->first
;
769 pdata
->first
->prev
= NULL
;
771 lastline
= pdata
->last
;
772 lastline
->next
= NULL
;
776 * Link all paragraph structures together similarly.
780 lastpara
->next
= pdata
;
788 * Now we have an enormous linked list of every line of text in
789 * the document. Break it up into pages.
791 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
802 for (page
= pages
; page
; page
= page
->next
) {
803 sprintf(buf
, "%d", ++pagenum
);
804 page
->number
= ufroma_dup(buf
, CS_ASCII
);
808 first_index_page
= snew(page_data
);
809 first_index_page
->next
= first_index_page
->prev
= NULL
;
810 first_index_page
->first_line
= NULL
;
811 first_index_page
->last_line
= NULL
;
812 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
813 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
814 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
816 /* And don't forget the as-yet-uncreated index. */
817 sprintf(buf
, "%d", ++pagenum
);
818 first_index_page
->number
= ufroma_dup(buf
, CS_ASCII
);
823 * Now we're ready to actually lay out the pages. We do this by
824 * looping over _paragraphs_, since we may need to track cross-
825 * references between lines and even across pages.
827 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
828 render_para(pdata
, conf
, keywords
, idx
,
829 &index_placeholder_para
, first_index_page
);
832 * Now we've laid out the main body pages, we should have
833 * acquired a full set of page numbers for the index.
839 para_data
*firstidx
, *lastidx
;
840 line_data
*firstidxline
, *lastidxline
, *ldata
;
841 page_data
*ipages
, *ipages2
, *page
;
844 * Create a set of paragraphs for the index.
846 index_title
= fake_word(conf
->index_text
);
848 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
849 NULL
, NULL
, index_title
, conf
);
851 lastidx
->next
= NULL
;
852 firstidxline
= firstidx
->first
;
853 lastidxline
= lastidx
->last
;
854 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
855 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
856 para_data
*text
, *pages
;
861 text
= make_para_data(para_Normal
, 0, 0,
862 conf
->base_width
- conf
->index_colwidth
,
863 NULL
, NULL
, entry
->text
, conf
);
865 pages
= make_para_data(para_Normal
, 0, 0,
866 conf
->base_width
- conf
->index_colwidth
,
867 NULL
, NULL
, pi
->words
, conf
);
869 text
->justification
= LEFT
;
870 pages
->justification
= RIGHT
;
871 text
->last
->space_after
= pages
->first
->space_before
=
872 conf
->base_leading
/ 2;
874 pages
->last
->space_after
= text
->first
->space_before
=
878 assert(pages
->first
);
883 * If feasible, fold the two halves of the index entry
886 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
887 conf
->index_colwidth
+ conf
->index_minsep
) {
888 text
->last
->space_after
= -1;
889 pages
->first
->space_before
= -pages
->first
->line_height
+1;
892 lastidx
->next
= text
;
898 * Link all index line structures together into
901 text
->last
->next
= pages
->first
;
902 pages
->first
->prev
= text
->last
;
904 lastidxline
->next
= text
->first
;
905 text
->first
->prev
= lastidxline
;
907 lastidxline
= pages
->last
;
910 * Breaking an index entry anywhere is so bad that I
911 * think I'm going to forbid it totally.
913 for (ldata
= text
->first
; ldata
&& ldata
->next
;
914 ldata
= ldata
->next
) {
915 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
916 ldata
->space_after
= -1;
921 * Now break the index into pages.
923 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
925 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
928 firstidxline
->space_before
+
929 firstidxline
->line_height
+
930 firstidxline
->space_after
);
933 * This will have put each _column_ of the index on a
934 * separate page, which isn't what we want. Fold the pages
941 for (i
= 1; i
< conf
->index_cols
; i
++)
945 fold_into_page(page
, page
->next
,
946 i
* (conf
->index_colwidth
+
947 conf
->index_gutter
));
949 page
->next
= page
->next
->next
;
951 page
->next
->prev
= page
;
957 /* Also fold the heading on to the same page as the index items. */
958 fold_into_page(ipages
, ipages2
, 0);
959 ipages
->next
= ipages2
->next
;
961 ipages
->next
->prev
= ipages
;
963 fold_into_page(first_index_page
, ipages
, 0);
964 first_index_page
->next
= ipages
->next
;
965 if (first_index_page
->next
)
966 first_index_page
->next
->prev
= first_index_page
;
968 ipages
= first_index_page
;
971 * Number the index pages, except the already-numbered
974 for (page
= ipages
->next
; page
; page
= page
->next
) {
976 sprintf(buf
, "%d", ++pagenum
);
977 page
->number
= ufroma_dup(buf
, CS_ASCII
);
981 * Render the index pages.
983 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
984 render_para(pdata
, conf
, keywords
, idx
,
985 &index_placeholder_para
, first_index_page
);
988 * Link the index page list on to the end of the main page
994 for (page
= pages
; page
->next
; page
= page
->next
);
999 * Same with the paragraph list, which will cause the index
1000 * to be mentioned in the document outline.
1003 firstpara
= firstidx
;
1005 lastpara
->next
= firstidx
;
1010 * Draw the headers and footers.
1012 * FIXME: this should be fully configurable, but for the moment
1013 * I'm just going to put in page numbers in the centre of a
1014 * footer and leave it at that.
1019 for (page
= pages
; page
; page
= page
->next
) {
1022 width
= conf
->pagenum_fontsize
*
1023 string_width(conf
->fbase
.fonts
[FONT_NORMAL
], page
->number
,
1026 render_string(page
, conf
->fbase
.fonts
[FONT_NORMAL
],
1027 conf
->pagenum_fontsize
,
1028 conf
->left_margin
+ (conf
->base_width
- width
)/2,
1029 conf
->bottom_margin
- conf
->footer_distance
,
1035 * Start putting together the overall document structure we're
1038 doc
= snew(document
);
1039 doc
->fonts
= fontlist
;
1041 doc
->paper_width
= conf
->paper_width
;
1042 doc
->paper_height
= conf
->paper_height
;
1045 * Collect the section heading paragraphs into a document
1046 * outline. This is slightly fiddly because the Title paragraph
1047 * isn't required to be at the start, although all the others
1053 doc
->outline_elements
= snewn(osize
, outline_element
);
1054 doc
->n_outline_elements
= 0;
1056 /* First find the title. */
1057 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1058 if (pdata
->outline_level
== 0) {
1059 doc
->outline_elements
[0].level
= 0;
1060 doc
->outline_elements
[0].pdata
= pdata
;
1061 doc
->n_outline_elements
++;
1066 /* Then collect the rest. */
1067 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1068 if (pdata
->outline_level
> 0) {
1069 if (doc
->n_outline_elements
>= osize
) {
1071 doc
->outline_elements
=
1072 sresize(doc
->outline_elements
, osize
, outline_element
);
1075 doc
->outline_elements
[doc
->n_outline_elements
].level
=
1076 pdata
->outline_level
;
1077 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
1078 doc
->n_outline_elements
++;
1086 static void setfont(para_data
*p
, font_cfg
*f
) {
1089 for (i
= 0; i
< NFONTS
; i
++) {
1090 p
->fonts
[i
] = f
->fonts
[i
];
1091 p
->sizes
[i
] = f
->font_size
;
1095 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
1096 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
1101 int extra_indent
, firstline_indent
, aux_indent
;
1104 pdata
= snew(para_data
);
1105 pdata
->outline_level
= -1;
1106 pdata
->outline_title
= NULL
;
1107 pdata
->rect_type
= RECT_NONE
;
1108 pdata
->contents_entry
= NULL
;
1109 pdata
->justification
= JUST
;
1112 * Choose fonts for this paragraph.
1116 setfont(pdata
, &conf
->ftitle
);
1117 pdata
->outline_level
= 0;
1122 case para_UnnumberedChapter
:
1123 setfont(pdata
, &conf
->fchapter
);
1124 pdata
->outline_level
= 1;
1130 &conf
->fsect
[paux
>= conf
->nfsect ? conf
->nfsect
- 1 : paux
]);
1131 pdata
->outline_level
= 2 + paux
;
1135 case para_BiblioCited
:
1137 case para_NumberedList
:
1138 case para_DescribedThing
:
1139 case para_Description
:
1140 case para_Copyright
:
1141 setfont(pdata
, &conf
->fbase
);
1146 * Also select an indentation level depending on the
1147 * paragraph type (list paragraphs other than
1148 * para_DescribedThing need extra indent).
1150 * (FIXME: Perhaps at some point we might even arrange
1151 * for the user to be able to request indented first
1152 * lines in paragraphs.)
1154 if (ptype
== para_Bullet
||
1155 ptype
== para_NumberedList
||
1156 ptype
== para_Description
) {
1157 extra_indent
= firstline_indent
= conf
->indent_list
;
1159 extra_indent
= firstline_indent
= 0;
1163 * Find the auxiliary text for this paragraph.
1174 * For some heading styles (FIXME: be able to
1175 * configure which), the auxiliary text contains
1176 * the chapter number and is arranged to be
1177 * right-aligned a few points left of the primary
1178 * margin. For other styles, the auxiliary text is
1179 * the full chapter _name_ and takes up space
1180 * within the (wrapped) chapter title, meaning that
1181 * we must move the first line indent over to make
1184 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
1188 len
= paper_width_simple(pdata
, pkwtext2
, conf
);
1189 aux_indent
= -len
- conf
->sect_num_left_space
;
1191 pdata
->outline_title
=
1192 prepare_outline_title(pkwtext2
, L
" ", pwords
);
1195 aux2
= fake_word(L
": ");
1198 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1199 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1201 pdata
->outline_title
=
1202 prepare_outline_title(pkwtext
, L
": ", pwords
);
1208 * Auxiliary text consisting of a bullet.
1210 aux
= fake_word(conf
->bullet
);
1211 aux_indent
= indent
+ conf
->indent_list_bullet
;
1214 case para_NumberedList
:
1216 * Auxiliary text consisting of the number followed
1217 * by a (FIXME: configurable) full stop.
1220 aux2
= fake_word(L
".");
1221 aux_indent
= indent
+ conf
->indent_list_bullet
;
1224 case para_BiblioCited
:
1226 * Auxiliary text consisting of the bibliography
1227 * reference text, and a trailing space.
1230 aux2
= fake_word(L
" ");
1231 aux_indent
= indent
;
1232 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1233 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1237 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
1238 pdata
->outline_title
=
1239 prepare_outline_title(NULL
, NULL
, pwords
);
1242 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
1243 indent
+ firstline_indent
,
1244 indent
+ extra_indent
, conf
);
1246 pdata
->first
->aux_text
= aux
;
1247 pdata
->first
->aux_text_2
= aux2
;
1248 pdata
->first
->aux_left_indent
= aux_indent
;
1251 * Line breaking penalties.
1258 case para_UnnumberedChapter
:
1260 * Fixed and large penalty for breaking straight
1261 * after a heading; corresponding bonus for
1262 * breaking straight before.
1264 pdata
->first
->penalty_before
= -500000;
1265 pdata
->last
->penalty_after
= 500000;
1266 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
1267 ldata
->penalty_after
= 500000;
1270 case para_DescribedThing
:
1272 * This is treated a bit like a small heading:
1273 * there's a penalty for breaking after it (i.e.
1274 * between it and its description), and a bonus for
1275 * breaking before it (actually _between_ list
1278 pdata
->first
->penalty_before
= -200000;
1279 pdata
->last
->penalty_after
= 200000;
1284 * Most paragraph types: widow/orphan control by
1285 * discouraging breaking one line from the end of
1288 if (pdata
->first
!= pdata
->last
) {
1289 pdata
->first
->penalty_after
= 100000;
1290 pdata
->last
->penalty_before
= 100000;
1295 standard_line_spacing(pdata
, conf
);
1298 * Some kinds of section heading require a page break before
1299 * them and an underline after.
1301 if (ptype
== para_Title
||
1302 ptype
== para_Chapter
||
1303 ptype
== para_Appendix
||
1304 ptype
== para_UnnumberedChapter
) {
1305 pdata
->first
->page_break
= TRUE
;
1306 pdata
->first
->space_before
= conf
->chapter_top_space
;
1307 pdata
->last
->space_after
+=
1308 (conf
->chapter_underline_depth
+
1309 conf
->chapter_underline_thickness
);
1310 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1316 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1321 * Set the line spacing for each line in this paragraph.
1323 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1324 if (ldata
== pdata
->first
)
1325 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1327 ldata
->space_before
= conf
->base_leading
/ 2;
1328 if (ldata
== pdata
->last
)
1329 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1331 ldata
->space_after
= conf
->base_leading
/ 2;
1332 ldata
->page_break
= FALSE
;
1336 static font_encoding
*new_font_encoding(font_data
*font
)
1341 fe
= snew(font_encoding
);
1344 if (font
->list
->tail
)
1345 font
->list
->tail
->next
= fe
;
1347 font
->list
->head
= fe
;
1348 font
->list
->tail
= fe
;
1351 fe
->free_pos
= 0x21;
1353 for (i
= 0; i
< 256; i
++) {
1354 fe
->vector
[i
] = NULL
;
1355 fe
->indices
[i
] = -1;
1356 fe
->to_unicode
[i
] = 0xFFFF;
1362 int kern_cmp(void *a
, void *b
)
1364 kern_pair
const *ka
= a
, *kb
= b
;
1366 if (ka
->left
< kb
->left
)
1368 if (ka
->left
> kb
->left
)
1370 if (ka
->right
< kb
->right
)
1372 if (ka
->right
> kb
->right
)
1377 /* This wouldn't be necessary if C had closures. */
1378 static font_info
*glyph_cmp_fi
;
1380 static int glyph_cmp(void const *a
, void const *b
)
1382 return strcmp(glyph_cmp_fi
->glyphs
[*(unsigned short *)a
],
1383 glyph_cmp_fi
->glyphs
[*(unsigned short *)b
]);
1387 * Set up the glyphsbyname index for a font.
1389 void font_index_glyphs(font_info
*fi
) {
1392 fi
->glyphsbyname
= snewn(fi
->nglyphs
, unsigned short);
1393 for (i
= 0; i
< fi
->nglyphs
; i
++)
1394 fi
->glyphsbyname
[i
] = i
;
1396 qsort(fi
->glyphsbyname
, fi
->nglyphs
, sizeof(fi
->glyphsbyname
[0]),
1400 int find_glyph(font_info
*fi
, char const *name
) {
1407 r
= strcmp(fi
->glyphs
[fi
->glyphsbyname
[k
]], name
);
1409 return fi
->glyphsbyname
[k
];
1418 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1421 font_info
const *fi
;
1426 for (fe
= fontlist
->head
; fe
; fe
= fe
->next
)
1427 if (strcmp(fe
->font
->info
->name
, name
) == 0)
1430 for (fi
= all_fonts
; fi
; fi
= fi
->next
)
1431 if (strcmp(fi
->name
, name
) == 0) break;
1432 if (!fi
) return NULL
;
1434 f
= snew(font_data
);
1438 nglyphs
= f
->info
->nglyphs
;
1439 f
->subfont_map
= snewn(nglyphs
, subfont_map_entry
);
1442 * Our first subfont will contain all of US-ASCII. This isn't
1443 * really necessary - we could just create custom subfonts
1444 * precisely as the whim of render_string dictated - but
1445 * instinct suggests that it might be nice to have the text in
1446 * the output files look _marginally_ recognisable.
1448 fe
= new_font_encoding(f
);
1449 fe
->free_pos
= 0xA1; /* only the top half is free */
1450 f
->latest_subfont
= fe
;
1452 for (i
= 0; i
< nglyphs
; i
++) {
1454 ucs
= ps_glyph_to_unicode(f
->info
->glyphs
[i
]);
1455 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1456 fe
->vector
[ucs
] = f
->info
->glyphs
[i
];
1457 fe
->indices
[ucs
] = i
;
1458 fe
->to_unicode
[ucs
] = ucs
;
1459 f
->subfont_map
[i
].subfont
= fe
;
1460 f
->subfont_map
[i
].position
= ucs
;
1463 * This character is not yet assigned to a subfont.
1465 f
->subfont_map
[i
].subfont
= NULL
;
1466 f
->subfont_map
[i
].position
= 0;
1473 /* NB: arguments are glyph numbers from font->bmp. */
1474 static int find_kern(font_data
*font
, int lindex
, int rindex
)
1477 kern_pair
const *kp
;
1479 if (lindex
== 0xFFFF || rindex
== 0xFFFF)
1481 wantkp
.left
= lindex
;
1482 wantkp
.right
= rindex
;
1483 kp
= find234(font
->info
->kerns
, &wantkp
, NULL
);
1489 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1498 for (; *string
; string
++) {
1499 index
= (*string
< 0 || *string
> 0xFFFF ?
0xFFFF :
1500 font
->info
->bmp
[*string
]);
1502 if (index
== 0xFFFF) {
1506 width
+= find_kern(font
, oindex
, index
) +
1507 font
->info
->widths
[index
];
1515 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1517 struct paper_width_ctx
{
1523 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1525 while (text
&& text
!= end
) {
1526 w
+= paper_width_internal(vctx
, text
, nspaces
);
1532 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1534 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1535 int style
, type
, findex
, width
, errs
;
1538 switch (word
->type
) {
1539 case word_HyperLink
:
1541 case word_UpperXref
:
1542 case word_LowerXref
:
1549 style
= towordstyle(word
->type
);
1550 type
= removeattr(word
->type
);
1552 findex
= (style
== word_Normal ? FONT_NORMAL
:
1553 style
== word_Emph ? FONT_EMPH
:
1556 if (type
== word_Normal
) {
1558 } else if (type
== word_WhiteSpace
) {
1559 if (findex
!= FONT_CODE
) {
1562 return ctx
->minspacewidth
;
1565 } else /* if (type == word_Quote) */ {
1566 if (word
->aux
== quote_Open
)
1567 str
= ctx
->conf
->lquote
;
1569 str
= ctx
->conf
->rquote
;
1572 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1574 if (errs
&& word
->alt
)
1575 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1577 return ctx
->pdata
->sizes
[findex
] * width
;
1580 static int paper_width(void *vctx
, word
*word
)
1582 return paper_width_internal(vctx
, word
, NULL
);
1585 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
)
1587 struct paper_width_ctx ctx
;
1591 (pdata
->sizes
[FONT_NORMAL
] *
1592 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1595 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1598 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1599 int w
, int i1
, int i2
, paper_conf
*conf
)
1601 wrappedline
*wrapping
, *p
;
1603 struct paper_width_ctx ctx
;
1607 * We're going to need to store the line height in every line
1608 * structure we generate.
1613 for (i
= 0; i
< NFONTS
; i
++)
1614 if (line_height
< pdata
->sizes
[i
])
1615 line_height
= pdata
->sizes
[i
];
1616 line_height
*= UNITS_PER_PT
;
1619 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1620 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1621 if (spacewidth
== 0) {
1623 * A font without a space?! Disturbing. I hope this never
1624 * comes up, but I'll make a random guess anyway and set my
1625 * space width to half the point size.
1627 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * UNITS_PER_PT
/ 2;
1631 * I'm going to set the _minimum_ space width to 3/5 of the
1632 * standard one, and use the standard one as the optimum.
1634 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1638 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1641 * Having done the wrapping, we now concoct a set of line_data
1644 pdata
->first
= pdata
->last
= NULL
;
1646 for (p
= wrapping
; p
; p
= p
->next
) {
1649 int len
, wid
, spaces
;
1651 ldata
= snew(line_data
);
1653 ldata
->pdata
= pdata
;
1654 ldata
->first
= p
->begin
;
1655 ldata
->end
= p
->end
;
1656 ldata
->line_height
= line_height
;
1658 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1661 pdata
->last
->next
= ldata
;
1662 ldata
->prev
= pdata
->last
;
1664 pdata
->first
= ldata
;
1668 pdata
->last
= ldata
;
1671 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1672 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1675 ldata
->hshortfall
= wid
- len
;
1676 ldata
->nspaces
= spaces
;
1678 * This tells us how much the space width needs to
1679 * change from _min_spacewidth. But we want to store
1680 * its difference from the _natural_ space width, to
1681 * make the text rendering easier.
1683 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1684 ldata
->hshortfall
-= spacewidth
* spaces
;
1685 ldata
->real_shortfall
= ldata
->hshortfall
;
1687 * Special case: on the last line of a paragraph, we
1688 * never stretch spaces.
1690 if (ldata
->hshortfall
> 0 && !p
->next
)
1691 ldata
->hshortfall
= 0;
1693 ldata
->aux_text
= NULL
;
1694 ldata
->aux_text_2
= NULL
;
1695 ldata
->aux_left_indent
= 0;
1696 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1701 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1702 int page_height
, int ncols
, int headspace
)
1706 int n
, n1
, this_height
;
1709 * Page breaking is done by a close analogue of the optimal
1710 * paragraph wrapping algorithm used by wrap_para(). We work
1711 * backwards from the end of the document line by line; for
1712 * each line, we contemplate every possible number of lines we
1713 * could put on a page starting with that line, determine a
1714 * cost function for each one, add it to the pre-computed cost
1715 * function for optimally page-breaking everything after that
1716 * page, and pick the best option.
1718 * This is made slightly more complex by the fact that we have
1719 * a multi-column index with a heading at the top of the
1720 * _first_ page, meaning that the first _ncols_ pages must have
1721 * a different length. Hence, we must do the wrapping ncols+1
1722 * times over, hypothetically trying to put every subsequence
1723 * on every possible page.
1725 * Since my line_data structures are only used for this
1726 * purpose, I might as well just store the algorithm data
1730 for (l
= last
; l
; l
= l
->prev
) {
1731 l
->bestcost
= snewn(ncols
+1, int);
1732 l
->vshortfall
= snewn(ncols
+1, int);
1733 l
->text
= snewn(ncols
+1, int);
1734 l
->space
= snewn(ncols
+1, int);
1735 l
->page_last
= snewn(ncols
+1, line_data
*);
1737 for (n
= 0; n
<= ncols
; n
++) {
1738 int minheight
, text
= 0, space
= 0;
1741 n1
= (n
< ncols ? n
+1 : ncols
);
1743 this_height
= page_height
- headspace
;
1745 this_height
= page_height
;
1747 l
->bestcost
[n
] = -1;
1748 for (m
= l
; m
; m
= m
->next
) {
1749 if (m
!= l
&& m
->page_break
)
1750 break; /* we've gone as far as we can */
1753 if (m
->prev
->space_after
> 0)
1754 space
+= m
->prev
->space_after
;
1756 text
+= m
->prev
->space_after
;
1758 if (m
!= l
|| m
->page_break
) {
1759 if (m
->space_before
> 0)
1760 space
+= m
->space_before
;
1762 text
+= m
->space_before
;
1764 text
+= m
->line_height
;
1765 minheight
= text
+ space
;
1767 if (m
!= l
&& minheight
> this_height
)
1771 * If the space after this paragraph is _negative_
1772 * (which means the next line is folded on to this
1773 * one, which happens in the index), we absolutely
1774 * cannot break here.
1776 if (m
->space_after
>= 0) {
1779 * Compute the cost of this arrangement, as the
1780 * square of the amount of wasted space on the
1781 * page. Exception: if this is the last page
1782 * before a mandatory break or the document
1783 * end, we don't penalise a large blank area.
1785 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1787 int x
= (this_height
- minheight
) / FUNITS_PER_PT
*
1795 cost
+= (x
* xf
) >> 8;
1799 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1800 cost
+= m
->penalty_after
;
1801 cost
+= m
->next
->penalty_before
;
1804 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1805 cost
+= m
->next
->bestcost
[n1
];
1806 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1808 * This is the best option yet for this
1811 l
->bestcost
[n
] = cost
;
1812 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1813 l
->vshortfall
[n
] = this_height
- minheight
;
1815 l
->vshortfall
[n
] = 0;
1817 l
->space
[n
] = space
;
1818 l
->page_last
[n
] = m
;
1829 * Now go through the line list forwards and assemble the
1838 int text
, space
, head
;
1840 page
= snew(page_data
);
1849 page
->first_line
= l
;
1850 page
->last_line
= l
->page_last
[n
];
1852 page
->first_text
= page
->last_text
= NULL
;
1853 page
->first_xref
= page
->last_xref
= NULL
;
1854 page
->first_rect
= page
->last_rect
= NULL
;
1857 * Now assign a y-coordinate to each line on the page.
1860 head
= (n
< ncols ? headspace
: 0);
1861 for (l
= page
->first_line
; l
; l
= l
->next
) {
1862 if (l
!= page
->first_line
) {
1863 if (l
->prev
->space_after
> 0)
1864 space
+= l
->prev
->space_after
;
1866 text
+= l
->prev
->space_after
;
1868 if (l
!= page
->first_line
|| l
->page_break
) {
1869 if (l
->space_before
> 0)
1870 space
+= l
->space_before
;
1872 text
+= l
->space_before
;
1874 text
+= l
->line_height
;
1877 l
->ypos
= text
+ space
+ head
;
1878 if (page
->first_line
->space
[n
]) {
1879 l
->ypos
+= space
* (float)page
->first_line
->vshortfall
[n
] /
1880 page
->first_line
->space
[n
];
1883 if (l
== page
->last_line
)
1887 l
= page
->last_line
;
1892 n
= (n
< ncols ? n
+1 : ncols
);
1898 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1900 rect
*r
= snew(rect
);
1903 if (page
->last_rect
)
1904 page
->last_rect
->next
= r
;
1906 page
->first_rect
= r
;
1907 page
->last_rect
= r
;
1915 static void add_string_to_page(page_data
*page
, int x
, int y
,
1916 font_encoding
*fe
, int size
, char *text
,
1919 text_fragment
*frag
;
1921 frag
= snew(text_fragment
);
1924 if (page
->last_text
)
1925 page
->last_text
->next
= frag
;
1927 page
->first_text
= frag
;
1928 page
->last_text
= frag
;
1933 frag
->fontsize
= size
;
1934 frag
->text
= dupstr(text
);
1935 frag
->width
= width
;
1939 * Returns the updated x coordinate.
1941 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1942 int x
, int y
, wchar_t *str
)
1945 int textpos
, textwid
, kern
, glyph
, oglyph
;
1946 font_encoding
*subfont
= NULL
, *sf
;
1948 text
= snewn(1 + ustrlen(str
), char);
1949 textpos
= textwid
= 0;
1954 glyph
= (*str
< 0 || *str
> 0xFFFF ?
0xFFFF :
1955 font
->info
->bmp
[*str
]);
1957 if (glyph
== 0xFFFF) {
1959 continue; /* nothing more we can do here */
1963 * Find which subfont this character is going in.
1965 sf
= font
->subfont_map
[glyph
].subfont
;
1971 * This character is not yet in a subfont. Assign one.
1973 if (font
->latest_subfont
->free_pos
>= 0x100)
1974 font
->latest_subfont
= new_font_encoding(font
);
1976 c
= font
->latest_subfont
->free_pos
++;
1977 if (font
->latest_subfont
->free_pos
== 0x7F)
1978 font
->latest_subfont
->free_pos
= 0xA1;
1980 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1981 font
->subfont_map
[glyph
].position
= c
;
1982 font
->latest_subfont
->vector
[c
] = font
->info
->glyphs
[glyph
];
1983 font
->latest_subfont
->indices
[c
] = glyph
;
1984 font
->latest_subfont
->to_unicode
[c
] = *str
;
1986 sf
= font
->latest_subfont
;
1989 kern
= find_kern(font
, oglyph
, glyph
) * fontsize
;
1991 if (!subfont
|| sf
!= subfont
|| kern
) {
1993 text
[textpos
] = '\0';
1994 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
,
1996 x
+= textwid
+ kern
;
1998 assert(textpos
== 0);
2005 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
2006 textwid
+= font
->info
->widths
[glyph
] * fontsize
;
2012 text
[textpos
] = '\0';
2013 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
, textwid
);
2021 * Returns the updated x coordinate.
2023 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
2024 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
2025 int shortfall
, int nspaces
, int *nspace
,
2026 keywordlist
*keywords
, indexdata
*idx
, paper_conf
*conf
)
2028 while (text
&& text
!= text_end
) {
2029 int style
, type
, findex
, errs
;
2033 switch (text
->type
) {
2035 * Start a cross-reference.
2037 case word_HyperLink
:
2038 case word_UpperXref
:
2039 case word_LowerXref
:
2042 if (text
->type
== word_HyperLink
) {
2044 dest
.url
= utoa_dup(text
->text
, CS_ASCII
);
2046 } else if (text
->type
== word_PageXref
) {
2049 dest
.page
= (page_data
*)text
->private_data
;
2051 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
2055 assert(kwl
->para
->private_data
);
2056 pdata
= (para_data
*) kwl
->para
->private_data
;
2058 dest
.page
= pdata
->first
->page
;
2062 * Shouldn't happen, but *shrug*
2069 if (dest
.type
!= NONE
) {
2071 (*xr
)->dest
= dest
; /* structure copy */
2072 if (page
->last_xref
)
2073 page
->last_xref
->next
= *xr
;
2075 page
->first_xref
= *xr
;
2076 page
->last_xref
= *xr
;
2080 * FIXME: Ideally we should have, and use, some
2081 * vertical font metric information here so that
2082 * our cross-ref rectangle can take account of
2083 * descenders and the font's cap height. This will
2084 * do for the moment, but it isn't ideal.
2086 (*xr
)->lx
= (*xr
)->rx
= x
;
2088 (*xr
)->ty
= y
+ ldata
->line_height
;
2093 * Finish extending a cross-reference box.
2101 * Add the current page number to the list of pages
2102 * referenced by an index entry.
2106 * We don't create index references in contents entries.
2108 if (!pdata
->contents_entry
) {
2112 tag
= index_findtag(idx
, text
->text
);
2116 for (i
= 0; i
< tag
->nrefs
; i
++) {
2117 indexentry
*entry
= tag
->refs
[i
];
2118 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
2121 * If the same index term is indexed twice
2122 * within the same section, we only want to
2123 * mention it once in the index.
2125 if (pi
->lastpage
!= page
) {
2129 pi
->lastword
= pi
->lastword
->next
=
2131 pi
->lastword
= pi
->lastword
->next
=
2133 wp
= &pi
->lastword
->next
;
2137 pi
->lastword
= *wp
=
2138 fake_page_ref(page
);
2139 pi
->lastword
= pi
->lastword
->next
=
2140 fake_word(page
->number
);
2141 pi
->lastword
= pi
->lastword
->next
=
2145 pi
->lastpage
= page
;
2151 style
= towordstyle(text
->type
);
2152 type
= removeattr(text
->type
);
2154 findex
= (style
== word_Normal ? FONT_NORMAL
:
2155 style
== word_Emph ? FONT_EMPH
:
2158 if (type
== word_Normal
) {
2160 } else if (type
== word_WhiteSpace
) {
2161 x
+= pdata
->sizes
[findex
] *
2162 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
2163 if (nspaces
&& findex
!= FONT_CODE
) {
2164 x
+= (*nspace
+1) * shortfall
/ nspaces
;
2165 x
-= *nspace
* shortfall
/ nspaces
;
2169 } else /* if (type == word_Quote) */ {
2170 if (text
->aux
== quote_Open
)
2176 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
2178 if (errs
&& text
->alt
)
2179 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
2180 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
,
2183 x
= render_string(page
, pdata
->fonts
[findex
],
2184 pdata
->sizes
[findex
], x
, y
, str
);
2197 * Returns the last x position used on the line.
2199 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
2200 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
2207 if (ldata
->aux_text
) {
2211 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2212 left_x
+ ldata
->aux_left_indent
,
2213 top_y
- ldata
->ypos
,
2214 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
2215 keywords
, idx
, conf
);
2216 if (ldata
->aux_text_2
)
2217 render_text(ldata
->page
, ldata
->pdata
, ldata
,
2218 x
, top_y
- ldata
->ypos
,
2219 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
2220 keywords
, idx
, conf
);
2226 * There might be a cross-reference carried over from a
2229 if (dest
->type
!= NONE
) {
2232 xr
->dest
= *dest
; /* structure copy */
2233 if (ldata
->page
->last_xref
)
2234 ldata
->page
->last_xref
->next
= xr
;
2236 ldata
->page
->first_xref
= xr
;
2237 ldata
->page
->last_xref
= xr
;
2238 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
2239 xr
->by
= top_y
- ldata
->ypos
;
2240 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
2245 int extra_indent
, shortfall
, spaces
;
2246 int just
= ldata
->pdata
->justification
;
2249 * All forms of justification become JUST when we have
2250 * to squeeze the paragraph.
2252 if (ldata
->hshortfall
< 0)
2257 shortfall
= ldata
->hshortfall
;
2258 spaces
= ldata
->nspaces
;
2262 shortfall
= spaces
= extra_indent
= 0;
2265 shortfall
= spaces
= 0;
2266 extra_indent
= ldata
->real_shortfall
;
2270 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2271 left_x
+ ldata
->xpos
+ extra_indent
,
2272 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
2273 &xr
, shortfall
, spaces
, &nspace
,
2274 keywords
, idx
, conf
);
2279 * There's a cross-reference continued on to the next line.
2289 static void render_para(para_data
*pdata
, paper_conf
*conf
,
2290 keywordlist
*keywords
, indexdata
*idx
,
2291 paragraph
*index_placeholder
, page_data
*index_page
)
2295 page_data
*cxref_page
;
2304 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
2306 * If this is a contents entry, we expect to have a single
2307 * enormous cross-reference rectangle covering the whole
2308 * thing. (Unless, of course, it spans multiple pages.)
2310 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
2311 cxref_page
= ldata
->page
;
2314 cxref
->dest
.type
= PAGE
;
2315 if (pdata
->contents_entry
== index_placeholder
) {
2316 cxref
->dest
.page
= index_page
;
2318 assert(pdata
->contents_entry
->private_data
);
2319 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2320 cxref
->dest
.page
= target
->first
->page
;
2322 cxref
->dest
.url
= NULL
;
2323 if (ldata
->page
->last_xref
)
2324 ldata
->page
->last_xref
->next
= cxref
;
2326 ldata
->page
->first_xref
= cxref
;
2327 ldata
->page
->last_xref
= cxref
;
2328 cxref
->lx
= conf
->left_margin
;
2329 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
2330 cxref
->ty
= conf
->paper_height
- conf
->top_margin
2331 - ldata
->ypos
+ ldata
->line_height
;
2333 if (pdata
->contents_entry
) {
2334 assert(cxref
!= NULL
);
2335 cxref
->by
= conf
->paper_height
- conf
->top_margin
2339 last_x
= render_line(ldata
, conf
->left_margin
,
2340 conf
->paper_height
- conf
->top_margin
,
2341 &dest
, keywords
, idx
, conf
);
2342 if (ldata
== pdata
->last
)
2347 * If this is a contents entry, add leaders and a page
2350 if (pdata
->contents_entry
) {
2356 if (pdata
->contents_entry
== index_placeholder
) {
2357 num
= index_page
->number
;
2359 assert(pdata
->contents_entry
->private_data
);
2360 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2361 num
= target
->first
->page
->number
;
2365 wid
= paper_width_simple(pdata
, w
, conf
);
2368 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2369 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2370 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2371 render_string(pdata
->last
->page
,
2372 pdata
->fonts
[FONT_NORMAL
],
2373 pdata
->sizes
[FONT_NORMAL
],
2374 conf
->left_margin
+ x
,
2375 (conf
->paper_height
- conf
->top_margin
-
2376 pdata
->last
->ypos
), L
".");
2378 render_string(pdata
->last
->page
,
2379 pdata
->fonts
[FONT_NORMAL
],
2380 pdata
->sizes
[FONT_NORMAL
],
2381 conf
->paper_width
- conf
->right_margin
- wid
,
2382 (conf
->paper_height
- conf
->top_margin
-
2383 pdata
->last
->ypos
), num
);
2387 * Render any rectangle (chapter title underline or rule)
2388 * that goes with this paragraph.
2390 switch (pdata
->rect_type
) {
2391 case RECT_CHAPTER_UNDERLINE
:
2392 add_rect_to_page(pdata
->last
->page
,
2394 (conf
->paper_height
- conf
->top_margin
-
2396 conf
->chapter_underline_depth
),
2398 conf
->chapter_underline_thickness
);
2401 add_rect_to_page(pdata
->first
->page
,
2402 conf
->left_margin
+ pdata
->first
->xpos
,
2403 (conf
->paper_height
- conf
->top_margin
-
2405 pdata
->last
->line_height
),
2406 conf
->base_width
- pdata
->first
->xpos
,
2407 pdata
->last
->line_height
);
2409 default: /* placate gcc */
2414 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2416 para_data
*pdata
= snew(para_data
);
2419 * For code paragraphs, I'm going to hack grievously and
2420 * pretend the three normal fonts are the three code paragraph
2423 setfont(pdata
, &conf
->fcode
);
2425 pdata
->first
= pdata
->last
= NULL
;
2426 pdata
->outline_level
= -1;
2427 pdata
->rect_type
= RECT_NONE
;
2428 pdata
->contents_entry
= NULL
;
2429 pdata
->justification
= LEFT
;
2431 for (; words
; words
= words
->next
) {
2432 wchar_t *t
, *e
, *start
;
2433 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2435 int prev
= -1, curr
;
2438 if (words
->next
&& words
->next
->type
== word_Emph
) {
2439 e
= words
->next
->text
;
2440 words
= words
->next
;
2450 else if (*e
== L
'i')
2452 else if (*e
== L
'b')
2469 * We've isolated a maximal subsequence of the line
2470 * which has the same emphasis. Form it into a word
2476 w
->type
= (prev
== 0 ? word_WeakCode
:
2477 prev
== 1 ? word_Emph
: word_Normal
);
2478 w
->text
= snewn(t
-start
+1, wchar_t);
2479 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2480 w
->text
[t
-start
] = '\0';
2493 ldata
= snew(line_data
);
2495 ldata
->pdata
= pdata
;
2496 ldata
->first
= lhead
;
2498 ldata
->line_height
= conf
->fcode
.font_size
* UNITS_PER_PT
;
2500 ldata
->xpos
= indent
;
2503 pdata
->last
->next
= ldata
;
2504 ldata
->prev
= pdata
->last
;
2506 pdata
->first
= ldata
;
2510 pdata
->last
= ldata
;
2512 ldata
->hshortfall
= 0;
2514 ldata
->aux_text
= NULL
;
2515 ldata
->aux_text_2
= NULL
;
2516 ldata
->aux_left_indent
= 0;
2517 /* General opprobrium for breaking in a code paragraph. */
2518 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2521 standard_line_spacing(pdata
, conf
);
2526 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2528 para_data
*pdata
= snew(para_data
);
2531 ldata
= snew(line_data
);
2533 ldata
->pdata
= pdata
;
2534 ldata
->first
= NULL
;
2536 ldata
->line_height
= conf
->rule_thickness
;
2538 ldata
->xpos
= indent
;
2543 ldata
->hshortfall
= 0;
2545 ldata
->aux_text
= NULL
;
2546 ldata
->aux_text_2
= NULL
;
2547 ldata
->aux_left_indent
= 0;
2550 * Better to break after a rule than before it
2552 ldata
->penalty_after
+= 100000;
2553 ldata
->penalty_before
+= -100000;
2555 pdata
->first
= pdata
->last
= ldata
;
2556 pdata
->outline_level
= -1;
2557 pdata
->rect_type
= RECT_RULE
;
2558 pdata
->contents_entry
= NULL
;
2559 pdata
->justification
= LEFT
;
2561 standard_line_spacing(pdata
, conf
);
2567 * Plain-text-like formatting for outline titles.
2569 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2570 for (; text
; text
= text
->next
) switch (text
->type
) {
2571 case word_HyperLink
:
2573 case word_UpperXref
:
2574 case word_LowerXref
:
2583 case word_WhiteSpace
:
2584 case word_EmphSpace
:
2585 case word_CodeSpace
:
2586 case word_WkCodeSpace
:
2588 case word_EmphQuote
:
2589 case word_CodeQuote
:
2590 case word_WkCodeQuote
:
2591 assert(text
->type
!= word_CodeQuote
&&
2592 text
->type
!= word_WkCodeQuote
);
2593 if (towordstyle(text
->type
) == word_Emph
&&
2594 (attraux(text
->aux
) == attr_First
||
2595 attraux(text
->aux
) == attr_Only
))
2596 rdadd(rs
, L
'_'); /* FIXME: configurability */
2597 else if (towordstyle(text
->type
) == word_Code
&&
2598 (attraux(text
->aux
) == attr_First
||
2599 attraux(text
->aux
) == attr_Only
))
2600 rdadd(rs
, L
'\''); /* FIXME: configurability */
2601 if (removeattr(text
->type
) == word_Normal
) {
2602 rdadds(rs
, text
->text
);
2603 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2605 } else if (removeattr(text
->type
) == word_Quote
) {
2606 rdadd(rs
, L
'\''); /* fixme: configurability */
2608 if (towordstyle(text
->type
) == word_Emph
&&
2609 (attraux(text
->aux
) == attr_Last
||
2610 attraux(text
->aux
) == attr_Only
))
2611 rdadd(rs
, L
'_'); /* FIXME: configurability */
2612 else if (towordstyle(text
->type
) == word_Code
&&
2613 (attraux(text
->aux
) == attr_Last
||
2614 attraux(text
->aux
) == attr_Only
))
2615 rdadd(rs
, L
'\''); /* FIXME: configurability */
2620 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2623 rdstring rs
= {0, 0, NULL
};
2626 paper_rdaddw(&rs
, first
);
2628 rdadds(&rs
, separator
);
2630 paper_rdaddw(&rs
, second
);
2635 static word
*fake_word(wchar_t *text
)
2637 word
*ret
= snew(word
);
2640 ret
->type
= word_Normal
;
2641 ret
->text
= ustrdup(text
);
2642 ret
->breaks
= FALSE
;
2647 static word
*fake_space_word(void)
2649 word
*ret
= snew(word
);
2652 ret
->type
= word_WhiteSpace
;
2659 static word
*fake_page_ref(page_data
*page
)
2661 word
*ret
= snew(word
);
2664 ret
->type
= word_PageXref
;
2666 ret
->breaks
= FALSE
;
2668 ret
->private_data
= page
;
2672 static word
*fake_end_ref(void)
2674 word
*ret
= snew(word
);
2677 ret
->type
= word_XrefEnd
;
2679 ret
->breaks
= FALSE
;
2684 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2693 w
= dup_word_list(first
);
2701 w
= fake_word(separator
);
2707 *wptr
= dup_word_list(second
);
2713 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2717 if (!src
->first_line
)
2720 if (dest
->last_line
) {
2721 dest
->last_line
->next
= src
->first_line
;
2722 src
->first_line
->prev
= dest
->last_line
;
2724 dest
->last_line
= src
->last_line
;
2726 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2728 ldata
->xpos
+= right_shift
;
2730 if (ldata
== src
->last_line
)