2 * Paper printing pre-backend for Halibut.
4 * This module does all the processing common to both PostScript
5 * and PDF output: selecting fonts, line wrapping and page breaking
6 * in accordance with font metrics, laying out the contents and
7 * index pages, generally doing all the page layout. After this,
8 * bk_ps.c and bk_pdf.c should only need to do linear translations
9 * into their literal output format.
13 * TODO in future work:
15 * - linearised PDF, perhaps?
17 * - I'm uncertain of whether I need to include a ToUnicode CMap
18 * in each of my font definitions in PDF. Currently things (by
19 * which I mean cut and paste out of acroread) seem to be
20 * working fairly happily without it, but I don't know.
22 * - rather than the ugly aux_text mechanism for rendering chapter
23 * titles, we could actually build the correct word list and
26 * - get vertical font metrics and use them to position the PDF
27 * xref boxes more pleasantly
30 * * page header and footer should be configurable; we should
31 * be able to shift the page number elsewhere, and add other
32 * things such as the current chapter/section title and fixed
34 * * remove the fixed mapping from heading levels to heading
35 * styles; offer a menu of styles from which the user can
36 * choose at every heading level
37 * * first-line indent in paragraphs
38 * * fixed text: `Contents', `Index', the colon-space and full
39 * stop in chapter title constructions
40 * * configurable location of contents?
41 * * certainly configurably _remove_ the contents, and possibly
43 * * double-sided document switch?
44 * + means you have two header/footer formats which
46 * + and means that mandatory page breaks before chapter
47 * titles should include a blank page if necessary to
48 * start the next section to a right-hand page
52 * - ability to import other Type 1 fonts
53 * * we need to parse the font to extract its metrics
54 * * then we pass the font bodily to both PS and PDF so it can
55 * be included in the output file
57 * - character substitution for better typography?
58 * * fi, fl, ffi, ffl ligatures
59 * * use real ellipsis rather than ...
60 * * a hyphen in a word by itself might prefer to be an en-dash
61 * * (Americans might even want a convenient way to use an
63 * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw!
64 * * substituting `minus' for `hyphen' in the standard encoding
65 * is probably preferable in Courier, though certainly not in
67 * * if I do do this lot, I'm rather inclined to at least try
68 * to think up a configurable way to do it so that Americans
69 * can do em-dash tricks without my intervention and other
70 * people can do other odd things too.
81 typedef struct paper_conf_Tag paper_conf
;
82 typedef struct paper_idx_Tag paper_idx
;
85 font_data
*fonts
[NFONTS
];
89 struct paper_conf_Tag
{
96 int indent_list_bullet
;
97 int indent_list_after
;
101 int base_para_spacing
;
102 int chapter_top_space
;
103 int sect_num_left_space
;
104 int chapter_underline_depth
;
105 int chapter_underline_thickness
;
107 font_cfg fbase
, fcode
, ftitle
, fchapter
, *fsect
;
109 int contents_indent_step
;
111 int leader_separation
;
115 int pagenum_fontsize
;
117 wchar_t *lquote
, *rquote
, *bullet
;
118 wchar_t *contents_text
, *index_text
;
119 /* These are derived from the above */
125 struct paper_idx_Tag
{
127 * Word list giving the page numbers on which this index entry
128 * appears. Also the last word in the list, for ease of
134 * The last page added to the list (so we can ensure we don't
141 word_PageXref
= word_NotWordType
+ 1
144 /* Flags for render_string() */
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
, unsigned flags
);
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
,
162 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
);
163 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
164 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
165 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
166 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
167 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
169 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
170 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
172 static word
*fake_word(wchar_t *text
);
173 static word
*fake_space_word(void);
174 static word
*fake_page_ref(page_data
*page
);
175 static word
*fake_end_ref(void);
176 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
178 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
180 static int fonts_ok(wchar_t *string
, ...)
186 va_start(ap
, string
);
187 while ( (font
= va_arg(ap
, font_data
*)) != NULL
) {
189 (void) string_width(font
, string
, &errs
, 0);
200 static void paper_cfg_fonts(font_data
**fonts
, font_list
*fontlist
,
201 wchar_t *wp
, filepos
*fpos
) {
206 for (i
= 0; i
< NFONTS
&& *wp
; i
++, wp
= uadv(wp
)) {
207 fn
= utoa_dup(wp
, CS_ASCII
);
208 f
= make_std_font(fontlist
, fn
);
212 /* FIXME: proper error */
213 error(err_nofont
, fpos
, wp
);
217 static paper_conf
paper_configure(paragraph
*source
, font_list
*fontlist
) {
224 ret
.paper_width
= 595 * UNITS_PER_PT
;
225 ret
.paper_height
= 842 * UNITS_PER_PT
;
226 ret
.left_margin
= 72 * UNITS_PER_PT
;
227 ret
.top_margin
= 72 * UNITS_PER_PT
;
228 ret
.right_margin
= 72 * UNITS_PER_PT
;
229 ret
.bottom_margin
= 108 * UNITS_PER_PT
;
230 ret
.indent_list_bullet
= 6 * UNITS_PER_PT
;
231 ret
.indent_list_after
= 18 * UNITS_PER_PT
;
232 ret
.indent_quote
= 18 * UNITS_PER_PT
;
233 ret
.base_leading
= UNITS_PER_PT
;
234 ret
.base_para_spacing
= 10 * UNITS_PER_PT
;
235 ret
.chapter_top_space
= 72 * UNITS_PER_PT
;
236 ret
.sect_num_left_space
= 12 * UNITS_PER_PT
;
237 ret
.chapter_underline_depth
= 14 * UNITS_PER_PT
;
238 ret
.chapter_underline_thickness
= 3 * UNITS_PER_PT
;
239 ret
.rule_thickness
= 1 * UNITS_PER_PT
;
240 ret
.fbase
.font_size
= 12;
241 ret
.fbase
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Times-Roman");
242 ret
.fbase
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Times-Italic");
243 ret
.fbase
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
244 ret
.fcode
.font_size
= 12;
245 ret
.fcode
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Courier-Bold");
246 ret
.fcode
.fonts
[FONT_EMPH
] = make_std_font(fontlist
, "Courier-Oblique");
247 ret
.fcode
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier");
248 ret
.ftitle
.font_size
= 24;
249 ret
.ftitle
.fonts
[FONT_NORMAL
] = make_std_font(fontlist
, "Helvetica-Bold");
250 ret
.ftitle
.fonts
[FONT_EMPH
] =
251 make_std_font(fontlist
, "Helvetica-BoldOblique");
252 ret
.ftitle
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
253 ret
.fchapter
.font_size
= 20;
254 ret
.fchapter
.fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
255 ret
.fchapter
.fonts
[FONT_EMPH
] =
256 make_std_font(fontlist
, "Helvetica-BoldOblique");
257 ret
.fchapter
.fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
259 ret
.fsect
= snewn(ret
.nfsect
, font_cfg
);
260 ret
.fsect
[0].font_size
= 16;
261 ret
.fsect
[0].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
262 ret
.fsect
[0].fonts
[FONT_EMPH
] =
263 make_std_font(fontlist
, "Helvetica-BoldOblique");
264 ret
.fsect
[0].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
265 ret
.fsect
[1].font_size
= 14;
266 ret
.fsect
[1].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
267 ret
.fsect
[1].fonts
[FONT_EMPH
] =
268 make_std_font(fontlist
, "Helvetica-BoldOblique");
269 ret
.fsect
[1].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
270 ret
.fsect
[2].font_size
= 13;
271 ret
.fsect
[2].fonts
[FONT_NORMAL
]= make_std_font(fontlist
, "Helvetica-Bold");
272 ret
.fsect
[2].fonts
[FONT_EMPH
] =
273 make_std_font(fontlist
, "Helvetica-BoldOblique");
274 ret
.fsect
[2].fonts
[FONT_CODE
] = make_std_font(fontlist
, "Courier-Bold");
275 ret
.contents_indent_step
= 24 * UNITS_PER_PT
;
276 ret
.contents_margin
= 84 * UNITS_PER_PT
;
277 ret
.leader_separation
= 12 * UNITS_PER_PT
;
278 ret
.index_gutter
= 36 * UNITS_PER_PT
;
280 ret
.index_minsep
= 18 * UNITS_PER_PT
;
281 ret
.pagenum_fontsize
= 12;
282 ret
.footer_distance
= 32 * UNITS_PER_PT
;
283 ret
.lquote
= L
"\x2018\0\x2019\0'\0'\0\0";
284 ret
.rquote
= uadv(ret
.lquote
);
285 ret
.bullet
= L
"\x2022\0-\0\0";
286 ret
.contents_text
= L
"Contents";
287 ret
.index_text
= L
"Index";
290 * Two-pass configuration so that we can pick up global config
291 * (e.g. `quotes') before having it overridden by specific
292 * config (`paper-quotes'), irrespective of the order in which
295 for (p
= source
; p
; p
= p
->next
) {
296 if (p
->type
== para_Config
) {
297 if (!ustricmp(p
->keyword
, L
"quotes")) {
298 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
299 ret
.lquote
= uadv(p
->keyword
);
300 ret
.rquote
= uadv(ret
.lquote
);
306 for (p
= source
; p
; p
= p
->next
) {
307 p
->private_data
= NULL
;
308 if (p
->type
== para_Config
) {
309 if (!ustricmp(p
->keyword
, L
"paper-quotes")) {
310 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
311 ret
.lquote
= uadv(p
->keyword
);
312 ret
.rquote
= uadv(ret
.lquote
);
314 } else if (!ustricmp(p
->keyword
, L
"contents")) {
315 ret
.contents_text
= uadv(p
->keyword
);
316 } else if (!ustricmp(p
->keyword
, L
"index")) {
317 ret
.index_text
= uadv(p
->keyword
);
318 } else if (!ustricmp(p
->keyword
, L
"paper-bullet")) {
319 ret
.bullet
= uadv(p
->keyword
);
320 } else if (!ustricmp(p
->keyword
, L
"paper-page-width")) {
322 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
323 } else if (!ustricmp(p
->keyword
, L
"paper-page-height")) {
325 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
326 } else if (!ustricmp(p
->keyword
, L
"paper-left-margin")) {
328 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
329 } else if (!ustricmp(p
->keyword
, L
"paper-top-margin")) {
331 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
332 } else if (!ustricmp(p
->keyword
, L
"paper-right-margin")) {
334 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
335 } else if (!ustricmp(p
->keyword
, L
"paper-bottom-margin")) {
337 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
338 } else if (!ustricmp(p
->keyword
, L
"paper-list-indent")) {
339 ret
.indent_list_bullet
=
340 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
341 } else if (!ustricmp(p
->keyword
, L
"paper-listitem-indent")) {
343 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
344 } else if (!ustricmp(p
->keyword
, L
"paper-quote-indent")) {
346 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
347 } else if (!ustricmp(p
->keyword
, L
"paper-base-leading")) {
349 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
350 } else if (!ustricmp(p
->keyword
, L
"paper-base-para-spacing")) {
351 ret
.base_para_spacing
=
352 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
353 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-top-space")) {
354 ret
.chapter_top_space
=
355 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
356 } else if (!ustricmp(p
->keyword
, L
"paper-sect-num-left-space")) {
357 ret
.sect_num_left_space
=
358 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
359 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-depth")) {
360 ret
.chapter_underline_depth
=
361 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
362 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-underline-thickness")) {
363 ret
.chapter_underline_thickness
=
364 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
365 } else if (!ustricmp(p
->keyword
, L
"paper-rule-thickness")) {
367 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
368 } else if (!ustricmp(p
->keyword
, L
"paper-contents-indent-step")) {
369 ret
.contents_indent_step
=
370 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
371 } else if (!ustricmp(p
->keyword
, L
"paper-contents-margin")) {
372 ret
.contents_margin
=
373 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
374 } else if (!ustricmp(p
->keyword
, L
"paper-leader-separation")) {
375 ret
.leader_separation
=
376 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
377 } else if (!ustricmp(p
->keyword
, L
"paper-index-gutter")) {
379 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
380 } else if (!ustricmp(p
->keyword
, L
"paper-index-minsep")) {
382 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
383 } else if (!ustricmp(p
->keyword
, L
"paper-footer-distance")) {
384 ret
.footer_distance
=
385 (int) 0.5 + FUNITS_PER_PT
* utof(uadv(p
->keyword
));
386 } else if (!ustricmp(p
->keyword
, L
"paper-base-font-size")) {
387 ret
.fbase
.font_size
= utoi(uadv(p
->keyword
));
388 } else if (!ustricmp(p
->keyword
, L
"paper-index-columns")) {
389 ret
.index_cols
= utoi(uadv(p
->keyword
));
390 } else if (!ustricmp(p
->keyword
, L
"paper-pagenum-font-size")) {
391 ret
.pagenum_fontsize
= utoi(uadv(p
->keyword
));
392 } else if (!ustricmp(p
->keyword
, L
"paper-base-fonts")) {
393 paper_cfg_fonts(ret
.fbase
.fonts
, fontlist
, uadv(p
->keyword
),
395 } else if (!ustricmp(p
->keyword
, L
"paper-code-font-size")) {
396 ret
.fcode
.font_size
= utoi(uadv(p
->keyword
));
397 } else if (!ustricmp(p
->keyword
, L
"paper-code-fonts")) {
398 paper_cfg_fonts(ret
.fcode
.fonts
, fontlist
, uadv(p
->keyword
),
400 } else if (!ustricmp(p
->keyword
, L
"paper-title-font-size")) {
401 ret
.ftitle
.font_size
= utoi(uadv(p
->keyword
));
402 } else if (!ustricmp(p
->keyword
, L
"paper-title-fonts")) {
403 paper_cfg_fonts(ret
.ftitle
.fonts
, fontlist
, uadv(p
->keyword
),
405 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-font-size")) {
406 ret
.fchapter
.font_size
= utoi(uadv(p
->keyword
));
407 } else if (!ustricmp(p
->keyword
, L
"paper-chapter-fonts")) {
408 paper_cfg_fonts(ret
.fchapter
.fonts
, fontlist
, uadv(p
->keyword
),
410 } else if (!ustricmp(p
->keyword
, L
"paper-section-font-size")) {
411 wchar_t *q
= uadv(p
->keyword
);
417 if (n
>= ret
.nfsect
) {
419 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
420 for (i
= ret
.nfsect
; i
<= n
; i
++)
421 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
424 ret
.fsect
[n
].font_size
= utoi(q
);
425 } else if (!ustricmp(p
->keyword
, L
"paper-section-fonts")) {
426 wchar_t *q
= uadv(p
->keyword
);
432 if (n
>= ret
.nfsect
) {
434 ret
.fsect
= sresize(ret
.fsect
, n
+1, font_cfg
);
435 for (i
= ret
.nfsect
; i
<= n
; i
++)
436 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
439 paper_cfg_fonts(ret
.fsect
[n
].fonts
, fontlist
, q
, &p
->fpos
);
445 * Set up the derived fields in the conf structure.
449 ret
.paper_width
- ret
.left_margin
- ret
.right_margin
;
451 ret
.paper_height
- ret
.top_margin
- ret
.bottom_margin
;
452 ret
.indent_list
= ret
.indent_list_bullet
+ ret
.indent_list_after
;
454 (ret
.base_width
- (ret
.index_cols
-1) * ret
.index_gutter
)
458 * Now process fallbacks on quote characters and bullets. We
459 * use string_width() to determine whether all of the relevant
460 * fonts contain the same character, and fall back whenever we
461 * find a character which not all of them support.
464 /* Quote characters need not be supported in the fixed code fonts,
465 * but must be in the title and body fonts. */
466 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
))) {
468 if (fonts_ok(ret
.lquote
,
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 ret
.fbase
.fonts
[FONT_NORMAL
],
477 ret
.fbase
.fonts
[FONT_EMPH
],
478 ret
.ftitle
.fonts
[FONT_NORMAL
],
479 ret
.ftitle
.fonts
[FONT_EMPH
],
480 ret
.fchapter
.fonts
[FONT_NORMAL
],
481 ret
.fchapter
.fonts
[FONT_EMPH
], NULL
)) {
482 for (n
= 0; n
< ret
.nfsect
; n
++)
483 if (!fonts_ok(ret
.lquote
,
484 ret
.fsect
[n
].fonts
[FONT_NORMAL
],
485 ret
.fsect
[n
].fonts
[FONT_EMPH
], NULL
) ||
486 !fonts_ok(ret
.rquote
,
487 ret
.fsect
[n
].fonts
[FONT_NORMAL
],
488 ret
.fsect
[n
].fonts
[FONT_EMPH
], NULL
))
493 ret
.lquote
= uadv(ret
.rquote
);
494 ret
.rquote
= uadv(ret
.lquote
);
497 /* The bullet character only needs to be supported in the normal body
498 * font (not even in italics). */
499 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
500 !fonts_ok(ret
.bullet
, ret
.fbase
.fonts
[FONT_NORMAL
], NULL
))
501 ret
.bullet
= uadv(ret
.bullet
);
506 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
510 int indent
, used_contents
;
511 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
512 para_data
*firstcont
, *lastcont
;
513 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
516 paper_conf
*conf
, ourconf
;
519 paragraph index_placeholder_para
;
520 page_data
*first_index_page
;
523 fontlist
= snew(font_list
);
524 fontlist
->head
= fontlist
->tail
= NULL
;
526 ourconf
= paper_configure(sourceform
, fontlist
);
530 * Set up a data structure to collect page numbers for each
539 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
540 paper_idx
*pi
= snew(paper_idx
);
544 pi
->words
= pi
->lastword
= NULL
;
547 entry
->backend_data
= pi
;
552 * Format the contents entry for each heading.
555 word
*contents_title
;
556 contents_title
= fake_word(conf
->contents_text
);
558 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
559 NULL
, NULL
, contents_title
, conf
);
560 lastcont
= firstcont
;
561 lastcont
->next
= NULL
;
562 firstcontline
= firstcont
->first
;
563 lastcontline
= lastcont
->last
;
564 for (p
= sourceform
; p
; p
= p
->next
) {
571 case para_UnnumberedChapter
:
577 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
580 case para_UnnumberedChapter
:
581 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
586 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
587 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
590 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
591 conf
->contents_margin
,
592 NULL
, NULL
, words
, conf
);
594 pdata
->contents_entry
= p
;
595 lastcont
->next
= pdata
;
599 * Link all contents line structures together into
604 lastcontline
->next
= pdata
->first
;
605 pdata
->first
->prev
= lastcontline
;
607 firstcontline
= pdata
->first
;
608 pdata
->first
->prev
= NULL
;
610 lastcontline
= pdata
->last
;
611 lastcontline
->next
= NULL
;
619 * And one extra one, for the index.
622 pdata
= make_para_data(para_Normal
, 0, 0,
623 conf
->contents_margin
,
625 fake_word(conf
->index_text
), conf
);
627 pdata
->contents_entry
= &index_placeholder_para
;
628 lastcont
->next
= pdata
;
633 lastcontline
->next
= pdata
->first
;
634 pdata
->first
->prev
= lastcontline
;
636 firstcontline
= pdata
->first
;
637 pdata
->first
->prev
= NULL
;
639 lastcontline
= pdata
->last
;
640 lastcontline
->next
= NULL
;
646 * Do the main paragraph formatting.
649 used_contents
= FALSE
;
650 firstline
= lastline
= NULL
;
651 for (p
= sourceform
; p
; p
= p
->next
) {
652 p
->private_data
= NULL
;
656 * These paragraph types are either invisible or don't
657 * define text in the normal sense. Either way, they
658 * don't require wrapping.
663 case para_NotParaType
:
670 * These paragraph types don't require wrapping, but
671 * they do affect the line width to which we wrap the
672 * rest of the paragraphs, so we need to pay attention.
675 indent
+= conf
->indent_list
; break;
677 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
679 indent
+= conf
->indent_quote
; break;
681 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
684 * This paragraph type is special. Process it
688 pdata
= code_paragraph(indent
, p
->words
, conf
);
689 p
->private_data
= pdata
;
690 if (pdata
->first
!= pdata
->last
) {
691 pdata
->first
->penalty_after
+= 100000;
692 pdata
->last
->penalty_before
+= 100000;
697 * This paragraph is also special.
700 pdata
= rule_paragraph(indent
, conf
);
701 p
->private_data
= pdata
;
705 * All of these paragraph types require wrapping in the
706 * ordinary way. So we must supply a set of fonts, a
707 * line width and auxiliary information (e.g. bullet
708 * text) for each one.
712 case para_UnnumberedChapter
:
716 case para_BiblioCited
:
718 case para_NumberedList
:
719 case para_DescribedThing
:
720 case para_Description
:
723 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
724 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
726 p
->private_data
= pdata
;
731 if (p
->private_data
) {
732 pdata
= (para_data
*)p
->private_data
;
735 * If this is the first non-title heading, we link the
736 * contents section in before it.
738 if (!used_contents
&& pdata
->outline_level
> 0) {
739 used_contents
= TRUE
;
741 lastpara
->next
= firstcont
;
743 firstpara
= firstcont
;
745 assert(lastpara
->next
== NULL
);
748 lastline
->next
= firstcontline
;
749 firstcontline
->prev
= lastline
;
751 firstline
= firstcontline
;
752 firstcontline
->prev
= NULL
;
754 assert(lastcontline
!= NULL
);
755 lastline
= lastcontline
;
756 lastline
->next
= NULL
;
760 * Link all line structures together into a big list.
764 lastline
->next
= pdata
->first
;
765 pdata
->first
->prev
= lastline
;
767 firstline
= pdata
->first
;
768 pdata
->first
->prev
= NULL
;
770 lastline
= pdata
->last
;
771 lastline
->next
= NULL
;
775 * Link all paragraph structures together similarly.
779 lastpara
->next
= pdata
;
787 * Now we have an enormous linked list of every line of text in
788 * the document. Break it up into pages.
790 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
801 for (page
= pages
; page
; page
= page
->next
) {
802 sprintf(buf
, "%d", ++pagenum
);
803 page
->number
= ufroma_dup(buf
, CS_ASCII
);
807 first_index_page
= snew(page_data
);
808 first_index_page
->next
= first_index_page
->prev
= NULL
;
809 first_index_page
->first_line
= NULL
;
810 first_index_page
->last_line
= NULL
;
811 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
812 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
813 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
815 /* And don't forget the as-yet-uncreated index. */
816 sprintf(buf
, "%d", ++pagenum
);
817 first_index_page
->number
= ufroma_dup(buf
, CS_ASCII
);
822 * Now we're ready to actually lay out the pages. We do this by
823 * looping over _paragraphs_, since we may need to track cross-
824 * references between lines and even across pages.
826 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
827 render_para(pdata
, conf
, keywords
, idx
,
828 &index_placeholder_para
, first_index_page
);
831 * Now we've laid out the main body pages, we should have
832 * acquired a full set of page numbers for the index.
838 para_data
*firstidx
, *lastidx
;
839 line_data
*firstidxline
, *lastidxline
, *ldata
;
840 page_data
*ipages
, *ipages2
, *page
;
843 * Create a set of paragraphs for the index.
845 index_title
= fake_word(conf
->index_text
);
847 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
848 NULL
, NULL
, index_title
, conf
);
850 lastidx
->next
= NULL
;
851 firstidxline
= firstidx
->first
;
852 lastidxline
= lastidx
->last
;
853 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
854 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
855 para_data
*text
, *pages
;
860 text
= make_para_data(para_Normal
, 0, 0,
861 conf
->base_width
- conf
->index_colwidth
,
862 NULL
, NULL
, entry
->text
, conf
);
864 pages
= make_para_data(para_Normal
, 0, 0,
865 conf
->base_width
- conf
->index_colwidth
,
866 NULL
, NULL
, pi
->words
, conf
);
868 text
->justification
= LEFT
;
869 pages
->justification
= RIGHT
;
870 text
->last
->space_after
= pages
->first
->space_before
=
871 conf
->base_leading
/ 2;
873 pages
->last
->space_after
= text
->first
->space_before
=
877 assert(pages
->first
);
882 * If feasible, fold the two halves of the index entry
885 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
886 conf
->index_colwidth
+ conf
->index_minsep
) {
887 text
->last
->space_after
= -1;
888 pages
->first
->space_before
= -pages
->first
->line_height
+1;
891 lastidx
->next
= text
;
897 * Link all index line structures together into
900 text
->last
->next
= pages
->first
;
901 pages
->first
->prev
= text
->last
;
903 lastidxline
->next
= text
->first
;
904 text
->first
->prev
= lastidxline
;
906 lastidxline
= pages
->last
;
909 * Breaking an index entry anywhere is so bad that I
910 * think I'm going to forbid it totally.
912 for (ldata
= text
->first
; ldata
&& ldata
->next
;
913 ldata
= ldata
->next
) {
914 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
915 ldata
->space_after
= -1;
920 * Now break the index into pages.
922 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
924 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
927 firstidxline
->space_before
+
928 firstidxline
->line_height
+
929 firstidxline
->space_after
);
932 * This will have put each _column_ of the index on a
933 * separate page, which isn't what we want. Fold the pages
940 for (i
= 1; i
< conf
->index_cols
; i
++)
944 fold_into_page(page
, page
->next
,
945 i
* (conf
->index_colwidth
+
946 conf
->index_gutter
));
948 page
->next
= page
->next
->next
;
950 page
->next
->prev
= page
;
956 /* Also fold the heading on to the same page as the index items. */
957 fold_into_page(ipages
, ipages2
, 0);
958 ipages
->next
= ipages2
->next
;
960 ipages
->next
->prev
= ipages
;
962 fold_into_page(first_index_page
, ipages
, 0);
963 first_index_page
->next
= ipages
->next
;
964 if (first_index_page
->next
)
965 first_index_page
->next
->prev
= first_index_page
;
967 ipages
= first_index_page
;
970 * Number the index pages, except the already-numbered
973 for (page
= ipages
->next
; page
; page
= page
->next
) {
975 sprintf(buf
, "%d", ++pagenum
);
976 page
->number
= ufroma_dup(buf
, CS_ASCII
);
980 * Render the index pages.
982 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
983 render_para(pdata
, conf
, keywords
, idx
,
984 &index_placeholder_para
, first_index_page
);
987 * Link the index page list on to the end of the main page
993 for (page
= pages
; page
->next
; page
= page
->next
);
998 * Same with the paragraph list, which will cause the index
999 * to be mentioned in the document outline.
1002 firstpara
= firstidx
;
1004 lastpara
->next
= firstidx
;
1009 * Draw the headers and footers.
1011 * FIXME: this should be fully configurable, but for the moment
1012 * I'm just going to put in page numbers in the centre of a
1013 * footer and leave it at that.
1018 for (page
= pages
; page
; page
= page
->next
) {
1021 width
= conf
->pagenum_fontsize
*
1022 string_width(conf
->fbase
.fonts
[FONT_NORMAL
], page
->number
,
1025 render_string(page
, conf
->fbase
.fonts
[FONT_NORMAL
],
1026 conf
->pagenum_fontsize
,
1027 conf
->left_margin
+ (conf
->base_width
- width
)/2,
1028 conf
->bottom_margin
- conf
->footer_distance
,
1034 * Start putting together the overall document structure we're
1037 doc
= snew(document
);
1038 doc
->fonts
= fontlist
;
1040 doc
->paper_width
= conf
->paper_width
;
1041 doc
->paper_height
= conf
->paper_height
;
1044 * Collect the section heading paragraphs into a document
1045 * outline. This is slightly fiddly because the Title paragraph
1046 * isn't required to be at the start, although all the others
1052 doc
->outline_elements
= snewn(osize
, outline_element
);
1053 doc
->n_outline_elements
= 0;
1055 /* First find the title. */
1056 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1057 if (pdata
->outline_level
== 0) {
1058 doc
->outline_elements
[0].level
= 0;
1059 doc
->outline_elements
[0].pdata
= pdata
;
1060 doc
->n_outline_elements
++;
1065 /* Then collect the rest. */
1066 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
1067 if (pdata
->outline_level
> 0) {
1068 if (doc
->n_outline_elements
>= osize
) {
1070 doc
->outline_elements
=
1071 sresize(doc
->outline_elements
, osize
, outline_element
);
1074 doc
->outline_elements
[doc
->n_outline_elements
].level
=
1075 pdata
->outline_level
;
1076 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
1077 doc
->n_outline_elements
++;
1085 static void setfont(para_data
*p
, font_cfg
*f
) {
1088 for (i
= 0; i
< NFONTS
; i
++) {
1089 p
->fonts
[i
] = f
->fonts
[i
];
1090 p
->sizes
[i
] = f
->font_size
;
1094 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
1095 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
1100 int extra_indent
, firstline_indent
, aux_indent
;
1103 pdata
= snew(para_data
);
1104 pdata
->outline_level
= -1;
1105 pdata
->outline_title
= NULL
;
1106 pdata
->rect_type
= RECT_NONE
;
1107 pdata
->contents_entry
= NULL
;
1108 pdata
->justification
= JUST
;
1111 * Choose fonts for this paragraph.
1115 setfont(pdata
, &conf
->ftitle
);
1116 pdata
->outline_level
= 0;
1121 case para_UnnumberedChapter
:
1122 setfont(pdata
, &conf
->fchapter
);
1123 pdata
->outline_level
= 1;
1129 &conf
->fsect
[paux
>= conf
->nfsect ? conf
->nfsect
- 1 : paux
]);
1130 pdata
->outline_level
= 2 + paux
;
1134 case para_BiblioCited
:
1136 case para_NumberedList
:
1137 case para_DescribedThing
:
1138 case para_Description
:
1139 case para_Copyright
:
1140 setfont(pdata
, &conf
->fbase
);
1145 * Also select an indentation level depending on the
1146 * paragraph type (list paragraphs other than
1147 * para_DescribedThing need extra indent).
1149 * (FIXME: Perhaps at some point we might even arrange
1150 * for the user to be able to request indented first
1151 * lines in paragraphs.)
1153 if (ptype
== para_Bullet
||
1154 ptype
== para_NumberedList
||
1155 ptype
== para_Description
) {
1156 extra_indent
= firstline_indent
= conf
->indent_list
;
1158 extra_indent
= firstline_indent
= 0;
1162 * Find the auxiliary text for this paragraph.
1173 * For some heading styles (FIXME: be able to
1174 * configure which), the auxiliary text contains
1175 * the chapter number and is arranged to be
1176 * right-aligned a few points left of the primary
1177 * margin. For other styles, the auxiliary text is
1178 * the full chapter _name_ and takes up space
1179 * within the (wrapped) chapter title, meaning that
1180 * we must move the first line indent over to make
1183 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
1187 len
= paper_width_simple(pdata
, pkwtext2
, conf
);
1188 aux_indent
= -len
- conf
->sect_num_left_space
;
1190 pdata
->outline_title
=
1191 prepare_outline_title(pkwtext2
, L
" ", pwords
);
1194 aux2
= fake_word(L
": ");
1197 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1198 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1200 pdata
->outline_title
=
1201 prepare_outline_title(pkwtext
, L
": ", pwords
);
1207 * Auxiliary text consisting of a bullet.
1209 aux
= fake_word(conf
->bullet
);
1210 aux_indent
= indent
+ conf
->indent_list_bullet
;
1213 case para_NumberedList
:
1215 * Auxiliary text consisting of the number followed
1216 * by a (FIXME: configurable) full stop.
1219 aux2
= fake_word(L
".");
1220 aux_indent
= indent
+ conf
->indent_list_bullet
;
1223 case para_BiblioCited
:
1225 * Auxiliary text consisting of the bibliography
1226 * reference text, and a trailing space.
1229 aux2
= fake_word(L
" ");
1230 aux_indent
= indent
;
1231 firstline_indent
+= paper_width_simple(pdata
, aux
, conf
);
1232 firstline_indent
+= paper_width_simple(pdata
, aux2
, conf
);
1236 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
1237 pdata
->outline_title
=
1238 prepare_outline_title(NULL
, NULL
, pwords
);
1241 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
1242 indent
+ firstline_indent
,
1243 indent
+ extra_indent
, conf
);
1245 pdata
->first
->aux_text
= aux
;
1246 pdata
->first
->aux_text_2
= aux2
;
1247 pdata
->first
->aux_left_indent
= aux_indent
;
1250 * Line breaking penalties.
1257 case para_UnnumberedChapter
:
1259 * Fixed and large penalty for breaking straight
1260 * after a heading; corresponding bonus for
1261 * breaking straight before.
1263 pdata
->first
->penalty_before
= -500000;
1264 pdata
->last
->penalty_after
= 500000;
1265 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
1266 ldata
->penalty_after
= 500000;
1269 case para_DescribedThing
:
1271 * This is treated a bit like a small heading:
1272 * there's a penalty for breaking after it (i.e.
1273 * between it and its description), and a bonus for
1274 * breaking before it (actually _between_ list
1277 pdata
->first
->penalty_before
= -200000;
1278 pdata
->last
->penalty_after
= 200000;
1283 * Most paragraph types: widow/orphan control by
1284 * discouraging breaking one line from the end of
1287 if (pdata
->first
!= pdata
->last
) {
1288 pdata
->first
->penalty_after
= 100000;
1289 pdata
->last
->penalty_before
= 100000;
1294 standard_line_spacing(pdata
, conf
);
1297 * Some kinds of section heading require a page break before
1298 * them and an underline after.
1300 if (ptype
== para_Title
||
1301 ptype
== para_Chapter
||
1302 ptype
== para_Appendix
||
1303 ptype
== para_UnnumberedChapter
) {
1304 pdata
->first
->page_break
= TRUE
;
1305 pdata
->first
->space_before
= conf
->chapter_top_space
;
1306 pdata
->last
->space_after
+=
1307 (conf
->chapter_underline_depth
+
1308 conf
->chapter_underline_thickness
);
1309 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1315 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1320 * Set the line spacing for each line in this paragraph.
1322 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1323 if (ldata
== pdata
->first
)
1324 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1326 ldata
->space_before
= conf
->base_leading
/ 2;
1327 if (ldata
== pdata
->last
)
1328 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1330 ldata
->space_after
= conf
->base_leading
/ 2;
1331 ldata
->page_break
= FALSE
;
1335 static font_encoding
*new_font_encoding(font_data
*font
)
1340 fe
= snew(font_encoding
);
1343 if (font
->list
->tail
)
1344 font
->list
->tail
->next
= fe
;
1346 font
->list
->head
= fe
;
1347 font
->list
->tail
= fe
;
1350 fe
->free_pos
= 0x21;
1352 for (i
= 0; i
< 256; i
++) {
1353 fe
->vector
[i
] = NOGLYPH
;
1354 fe
->to_unicode
[i
] = 0xFFFF;
1360 static subfont_map_entry
*encode_glyph_at(glyph g
, wchar_t u
,
1361 font_encoding
*fe
, int pos
)
1363 subfont_map_entry
*sme
= snew(subfont_map_entry
);
1366 sme
->position
= pos
;
1367 fe
->vector
[pos
] = g
;
1368 fe
->to_unicode
[pos
] = u
;
1369 add234(fe
->font
->subfont_map
, sme
);
1373 static int new_sfmap_cmp(void *a
, void *b
)
1375 glyph ga
= *(glyph
*)a
;
1376 subfont_map_entry
*sb
= b
;
1377 glyph gb
= sb
->subfont
->vector
[sb
->position
];
1379 if (ga
< gb
) return -1;
1380 if (ga
> gb
) return 1;
1384 static subfont_map_entry
*encode_glyph(glyph g
, wchar_t u
, font_data
*font
)
1386 subfont_map_entry
*sme
;
1389 sme
= find234(font
->subfont_map
, &g
, new_sfmap_cmp
);
1390 if (sme
) return sme
;
1393 * This character is not yet in a subfont. Assign one.
1395 if (font
->latest_subfont
->free_pos
>= 0x100)
1396 font
->latest_subfont
= new_font_encoding(font
);
1398 c
= font
->latest_subfont
->free_pos
++;
1399 if (font
->latest_subfont
->free_pos
== 0x7F)
1400 font
->latest_subfont
->free_pos
= 0xA1;
1402 return encode_glyph_at(g
, u
, font
->latest_subfont
, c
);
1405 static int sfmap_cmp(void *a
, void *b
)
1407 subfont_map_entry
*sa
= a
, *sb
= b
;
1408 glyph ga
= sa
->subfont
->vector
[sa
->position
];
1409 glyph gb
= sb
->subfont
->vector
[sb
->position
];
1411 if (ga
< gb
) return -1;
1412 if (ga
> gb
) return 1;
1416 int width_cmp(void *a
, void *b
)
1418 glyph_width
const *wa
= a
, *wb
= b
;
1420 if (wa
->glyph
< wb
->glyph
)
1422 if (wa
->glyph
> wb
->glyph
)
1427 int kern_cmp(void *a
, void *b
)
1429 kern_pair
const *ka
= a
, *kb
= b
;
1431 if (ka
->left
< kb
->left
)
1433 if (ka
->left
> kb
->left
)
1435 if (ka
->right
< kb
->right
)
1437 if (ka
->right
> kb
->right
)
1442 int lig_cmp(void *a
, void *b
)
1444 ligature
const *la
= a
, *lb
= b
;
1446 if (la
->left
< lb
->left
)
1448 if (la
->left
> lb
->left
)
1450 if (la
->right
< lb
->right
)
1452 if (la
->right
> lb
->right
)
1457 static int utoglyph(font_info
const *fi
, wchar_t u
) {
1458 return (u
< 0 || u
> 0xFFFF ? NOGLYPH
: fi
->bmp
[u
]);
1461 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1463 font_info
const *fi
;
1468 for (fe
= fontlist
->head
; fe
; fe
= fe
->next
)
1469 if (strcmp(fe
->font
->info
->name
, name
) == 0)
1472 for (fi
= all_fonts
; fi
; fi
= fi
->next
)
1473 if (strcmp(fi
->name
, name
) == 0) break;
1474 if (!fi
) return NULL
;
1476 f
= snew(font_data
);
1480 f
->subfont_map
= newtree234(sfmap_cmp
);
1483 * Our first subfont will contain all of US-ASCII. This isn't
1484 * really necessary - we could just create custom subfonts
1485 * precisely as the whim of render_string dictated - but
1486 * instinct suggests that it might be nice to have the text in
1487 * the output files look _marginally_ recognisable.
1489 fe
= new_font_encoding(f
);
1490 fe
->free_pos
= 0xA1; /* only the top half is free */
1491 f
->latest_subfont
= fe
;
1493 for (i
= 0x21; i
<= 0x7E; i
++) {
1494 glyph g
= utoglyph(fi
, i
);
1496 encode_glyph_at(g
, i
, fe
, i
);
1502 /* NB: arguments are glyph numbers from font->bmp. */
1503 int find_width(font_data
*font
, glyph index
)
1506 glyph_width
const *w
;
1508 wantw
.glyph
= index
;
1509 w
= find234(font
->info
->widths
, &wantw
, NULL
);
1514 static int find_kern(font_data
*font
, int lindex
, int rindex
)
1517 kern_pair
const *kp
;
1519 if (lindex
== NOGLYPH
|| rindex
== NOGLYPH
)
1521 wantkp
.left
= lindex
;
1522 wantkp
.right
= rindex
;
1523 kp
= find234(font
->info
->kerns
, &wantkp
, NULL
);
1529 static int find_lig(font_data
*font
, int lindex
, int rindex
)
1532 ligature
const *lig
;
1534 if (lindex
== NOGLYPH
|| rindex
== NOGLYPH
)
1536 wantlig
.left
= lindex
;
1537 wantlig
.right
= rindex
;
1538 lig
= find234(font
->info
->ligs
, &wantlig
, NULL
);
1544 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
,
1548 int nindex
, index
, oindex
, lindex
;
1554 index
= utoglyph(font
->info
, *string
);
1555 for (; *string
; string
++) {
1556 nindex
= utoglyph(font
->info
, string
[1]);
1558 if (index
== NOGLYPH
) {
1562 if (!(flags
& RS_NOLIG
) &&
1563 (lindex
= find_lig(font
, index
, nindex
)) != NOGLYPH
) {
1567 width
+= find_kern(font
, oindex
, index
) + find_width(font
, index
);
1576 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1578 struct paper_width_ctx
{
1584 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1586 while (text
&& text
!= end
) {
1587 w
+= paper_width_internal(vctx
, text
, nspaces
);
1593 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1595 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1596 int style
, type
, findex
, width
, errs
;
1600 switch (word
->type
) {
1601 case word_HyperLink
:
1603 case word_UpperXref
:
1604 case word_LowerXref
:
1611 style
= towordstyle(word
->type
);
1612 type
= removeattr(word
->type
);
1614 findex
= (style
== word_Normal ? FONT_NORMAL
:
1615 style
== word_Emph ? FONT_EMPH
:
1618 if (style
== word_Code
|| style
== word_WeakCode
) flags
|= RS_NOLIG
;
1620 if (type
== word_Normal
) {
1622 } else if (type
== word_WhiteSpace
) {
1623 if (findex
!= FONT_CODE
) {
1626 return ctx
->minspacewidth
;
1629 } else /* if (type == word_Quote) */ {
1630 if (word
->aux
== quote_Open
)
1631 str
= ctx
->conf
->lquote
;
1633 str
= ctx
->conf
->rquote
;
1636 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
, flags
);
1638 if (errs
&& word
->alt
)
1639 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1641 return ctx
->pdata
->sizes
[findex
] * width
;
1644 static int paper_width(void *vctx
, word
*word
)
1646 return paper_width_internal(vctx
, word
, NULL
);
1649 static int paper_width_simple(para_data
*pdata
, word
*text
, paper_conf
*conf
)
1651 struct paper_width_ctx ctx
;
1655 (pdata
->sizes
[FONT_NORMAL
] *
1656 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
, 0));
1659 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1662 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1663 int w
, int i1
, int i2
, paper_conf
*conf
)
1665 wrappedline
*wrapping
, *p
;
1667 struct paper_width_ctx ctx
;
1671 * We're going to need to store the line height in every line
1672 * structure we generate.
1677 for (i
= 0; i
< NFONTS
; i
++)
1678 if (line_height
< pdata
->sizes
[i
])
1679 line_height
= pdata
->sizes
[i
];
1680 line_height
*= UNITS_PER_PT
;
1683 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1684 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
, 0));
1685 if (spacewidth
== 0) {
1687 * A font without a space?! Disturbing. I hope this never
1688 * comes up, but I'll make a random guess anyway and set my
1689 * space width to half the point size.
1691 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * UNITS_PER_PT
/ 2;
1695 * I'm going to set the _minimum_ space width to 3/5 of the
1696 * standard one, and use the standard one as the optimum.
1698 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1702 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1705 * Having done the wrapping, we now concoct a set of line_data
1708 pdata
->first
= pdata
->last
= NULL
;
1710 for (p
= wrapping
; p
; p
= p
->next
) {
1713 int len
, wid
, spaces
;
1715 ldata
= snew(line_data
);
1717 ldata
->pdata
= pdata
;
1718 ldata
->first
= p
->begin
;
1719 ldata
->end
= p
->end
;
1720 ldata
->line_height
= line_height
;
1722 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1725 pdata
->last
->next
= ldata
;
1726 ldata
->prev
= pdata
->last
;
1728 pdata
->first
= ldata
;
1732 pdata
->last
= ldata
;
1735 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1736 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1739 ldata
->hshortfall
= wid
- len
;
1740 ldata
->nspaces
= spaces
;
1742 * This tells us how much the space width needs to
1743 * change from _min_spacewidth. But we want to store
1744 * its difference from the _natural_ space width, to
1745 * make the text rendering easier.
1747 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1748 ldata
->hshortfall
-= spacewidth
* spaces
;
1749 ldata
->real_shortfall
= ldata
->hshortfall
;
1751 * Special case: on the last line of a paragraph, we
1752 * never stretch spaces.
1754 if (ldata
->hshortfall
> 0 && !p
->next
)
1755 ldata
->hshortfall
= 0;
1757 ldata
->aux_text
= NULL
;
1758 ldata
->aux_text_2
= NULL
;
1759 ldata
->aux_left_indent
= 0;
1760 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1765 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1766 int page_height
, int ncols
, int headspace
)
1770 int n
, n1
, this_height
;
1773 * Page breaking is done by a close analogue of the optimal
1774 * paragraph wrapping algorithm used by wrap_para(). We work
1775 * backwards from the end of the document line by line; for
1776 * each line, we contemplate every possible number of lines we
1777 * could put on a page starting with that line, determine a
1778 * cost function for each one, add it to the pre-computed cost
1779 * function for optimally page-breaking everything after that
1780 * page, and pick the best option.
1782 * This is made slightly more complex by the fact that we have
1783 * a multi-column index with a heading at the top of the
1784 * _first_ page, meaning that the first _ncols_ pages must have
1785 * a different length. Hence, we must do the wrapping ncols+1
1786 * times over, hypothetically trying to put every subsequence
1787 * on every possible page.
1789 * Since my line_data structures are only used for this
1790 * purpose, I might as well just store the algorithm data
1794 for (l
= last
; l
; l
= l
->prev
) {
1795 l
->bestcost
= snewn(ncols
+1, int);
1796 l
->vshortfall
= snewn(ncols
+1, int);
1797 l
->text
= snewn(ncols
+1, int);
1798 l
->space
= snewn(ncols
+1, int);
1799 l
->page_last
= snewn(ncols
+1, line_data
*);
1801 for (n
= 0; n
<= ncols
; n
++) {
1802 int minheight
, text
= 0, space
= 0;
1805 n1
= (n
< ncols ? n
+1 : ncols
);
1807 this_height
= page_height
- headspace
;
1809 this_height
= page_height
;
1811 l
->bestcost
[n
] = -1;
1812 for (m
= l
; m
; m
= m
->next
) {
1813 if (m
!= l
&& m
->page_break
)
1814 break; /* we've gone as far as we can */
1817 if (m
->prev
->space_after
> 0)
1818 space
+= m
->prev
->space_after
;
1820 text
+= m
->prev
->space_after
;
1822 if (m
!= l
|| m
->page_break
) {
1823 if (m
->space_before
> 0)
1824 space
+= m
->space_before
;
1826 text
+= m
->space_before
;
1828 text
+= m
->line_height
;
1829 minheight
= text
+ space
;
1831 if (m
!= l
&& minheight
> this_height
)
1835 * If the space after this paragraph is _negative_
1836 * (which means the next line is folded on to this
1837 * one, which happens in the index), we absolutely
1838 * cannot break here.
1840 if (m
->space_after
>= 0) {
1843 * Compute the cost of this arrangement, as the
1844 * square of the amount of wasted space on the
1845 * page. Exception: if this is the last page
1846 * before a mandatory break or the document
1847 * end, we don't penalise a large blank area.
1849 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1851 int x
= (this_height
- minheight
) / FUNITS_PER_PT
*
1859 cost
+= (x
* xf
) >> 8;
1863 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1864 cost
+= m
->penalty_after
;
1865 cost
+= m
->next
->penalty_before
;
1868 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1869 cost
+= m
->next
->bestcost
[n1
];
1870 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1872 * This is the best option yet for this
1875 l
->bestcost
[n
] = cost
;
1876 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1877 l
->vshortfall
[n
] = this_height
- minheight
;
1879 l
->vshortfall
[n
] = 0;
1881 l
->space
[n
] = space
;
1882 l
->page_last
[n
] = m
;
1893 * Now go through the line list forwards and assemble the
1902 int text
, space
, head
;
1904 page
= snew(page_data
);
1913 page
->first_line
= l
;
1914 page
->last_line
= l
->page_last
[n
];
1916 page
->first_text
= page
->last_text
= NULL
;
1917 page
->first_xref
= page
->last_xref
= NULL
;
1918 page
->first_rect
= page
->last_rect
= NULL
;
1921 * Now assign a y-coordinate to each line on the page.
1924 head
= (n
< ncols ? headspace
: 0);
1925 for (l
= page
->first_line
; l
; l
= l
->next
) {
1926 if (l
!= page
->first_line
) {
1927 if (l
->prev
->space_after
> 0)
1928 space
+= l
->prev
->space_after
;
1930 text
+= l
->prev
->space_after
;
1932 if (l
!= page
->first_line
|| l
->page_break
) {
1933 if (l
->space_before
> 0)
1934 space
+= l
->space_before
;
1936 text
+= l
->space_before
;
1938 text
+= l
->line_height
;
1941 l
->ypos
= text
+ space
+ head
;
1942 if (page
->first_line
->space
[n
]) {
1943 l
->ypos
+= space
* (float)page
->first_line
->vshortfall
[n
] /
1944 page
->first_line
->space
[n
];
1947 if (l
== page
->last_line
)
1951 l
= page
->last_line
;
1956 n
= (n
< ncols ? n
+1 : ncols
);
1962 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1964 rect
*r
= snew(rect
);
1967 if (page
->last_rect
)
1968 page
->last_rect
->next
= r
;
1970 page
->first_rect
= r
;
1971 page
->last_rect
= r
;
1979 static void add_string_to_page(page_data
*page
, int x
, int y
,
1980 font_encoding
*fe
, int size
, char *text
,
1983 text_fragment
*frag
;
1985 frag
= snew(text_fragment
);
1988 if (page
->last_text
)
1989 page
->last_text
->next
= frag
;
1991 page
->first_text
= frag
;
1992 page
->last_text
= frag
;
1997 frag
->fontsize
= size
;
1998 frag
->text
= dupstr(text
);
1999 frag
->width
= width
;
2003 * Returns the updated x coordinate.
2005 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
2006 int x
, int y
, wchar_t *str
, unsigned flags
)
2009 int textpos
, textwid
, kern
, nglyph
, glyph
, oglyph
, lig
;
2010 font_encoding
*subfont
= NULL
, *sf
;
2011 subfont_map_entry
*sme
;
2013 text
= snewn(1 + ustrlen(str
), char);
2014 textpos
= textwid
= 0;
2017 nglyph
= utoglyph(font
->info
, *str
);
2021 nglyph
= utoglyph(font
->info
, str
[1]);
2023 if (glyph
== NOGLYPH
) {
2025 continue; /* nothing more we can do here */
2028 if (!(flags
& RS_NOLIG
) &&
2029 (lig
= find_lig(font
, glyph
, nglyph
)) != NOGLYPH
) {
2036 * Find which subfont this character is going in.
2038 sme
= encode_glyph(glyph
, *str
, font
);
2041 kern
= find_kern(font
, oglyph
, glyph
) * fontsize
;
2043 if (!subfont
|| sf
!= subfont
|| kern
) {
2045 text
[textpos
] = '\0';
2046 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
,
2048 x
+= textwid
+ kern
;
2050 assert(textpos
== 0);
2057 text
[textpos
++] = sme
->position
;
2058 textwid
+= find_width(font
, glyph
) * fontsize
;
2064 text
[textpos
] = '\0';
2065 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
, textwid
);
2073 * Returns the updated x coordinate.
2075 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
2076 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
2077 int shortfall
, int nspaces
, int *nspace
,
2078 keywordlist
*keywords
, indexdata
*idx
, paper_conf
*conf
)
2080 while (text
&& text
!= text_end
) {
2081 int style
, type
, findex
, errs
;
2086 switch (text
->type
) {
2088 * Start a cross-reference.
2090 case word_HyperLink
:
2091 case word_UpperXref
:
2092 case word_LowerXref
:
2095 if (text
->type
== word_HyperLink
) {
2097 dest
.url
= utoa_dup(text
->text
, CS_ASCII
);
2099 } else if (text
->type
== word_PageXref
) {
2102 dest
.page
= (page_data
*)text
->private_data
;
2104 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
2108 assert(kwl
->para
->private_data
);
2109 pdata
= (para_data
*) kwl
->para
->private_data
;
2111 dest
.page
= pdata
->first
->page
;
2115 * Shouldn't happen, but *shrug*
2122 if (dest
.type
!= NONE
) {
2124 (*xr
)->dest
= dest
; /* structure copy */
2125 if (page
->last_xref
)
2126 page
->last_xref
->next
= *xr
;
2128 page
->first_xref
= *xr
;
2129 page
->last_xref
= *xr
;
2133 * FIXME: Ideally we should have, and use, some
2134 * vertical font metric information here so that
2135 * our cross-ref rectangle can take account of
2136 * descenders and the font's cap height. This will
2137 * do for the moment, but it isn't ideal.
2139 (*xr
)->lx
= (*xr
)->rx
= x
;
2141 (*xr
)->ty
= y
+ ldata
->line_height
;
2146 * Finish extending a cross-reference box.
2154 * Add the current page number to the list of pages
2155 * referenced by an index entry.
2159 * We don't create index references in contents entries.
2161 if (!pdata
->contents_entry
) {
2165 tag
= index_findtag(idx
, text
->text
);
2169 for (i
= 0; i
< tag
->nrefs
; i
++) {
2170 indexentry
*entry
= tag
->refs
[i
];
2171 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
2174 * If the same index term is indexed twice
2175 * within the same section, we only want to
2176 * mention it once in the index.
2178 if (pi
->lastpage
!= page
) {
2182 pi
->lastword
= pi
->lastword
->next
=
2184 pi
->lastword
= pi
->lastword
->next
=
2186 wp
= &pi
->lastword
->next
;
2190 pi
->lastword
= *wp
=
2191 fake_page_ref(page
);
2192 pi
->lastword
= pi
->lastword
->next
=
2193 fake_word(page
->number
);
2194 pi
->lastword
= pi
->lastword
->next
=
2198 pi
->lastpage
= page
;
2204 style
= towordstyle(text
->type
);
2205 type
= removeattr(text
->type
);
2207 findex
= (style
== word_Normal ? FONT_NORMAL
:
2208 style
== word_Emph ? FONT_EMPH
:
2211 if (style
== word_Code
|| style
== word_WeakCode
) flags
|= RS_NOLIG
;
2213 if (type
== word_Normal
) {
2215 } else if (type
== word_WhiteSpace
) {
2216 x
+= pdata
->sizes
[findex
] *
2217 string_width(pdata
->fonts
[findex
], L
" ", NULL
, 0);
2218 if (nspaces
&& findex
!= FONT_CODE
) {
2219 x
+= (*nspace
+1) * shortfall
/ nspaces
;
2220 x
-= *nspace
* shortfall
/ nspaces
;
2224 } else /* if (type == word_Quote) */ {
2225 if (text
->aux
== quote_Open
)
2231 (void) string_width(pdata
->fonts
[findex
], str
, &errs
, flags
);
2233 if (errs
&& text
->alt
)
2234 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
2235 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
,
2238 x
= render_string(page
, pdata
->fonts
[findex
],
2239 pdata
->sizes
[findex
], x
, y
, str
, flags
);
2252 * Returns the last x position used on the line.
2254 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
2255 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
,
2262 if (ldata
->aux_text
) {
2266 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2267 left_x
+ ldata
->aux_left_indent
,
2268 top_y
- ldata
->ypos
,
2269 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
2270 keywords
, idx
, conf
);
2271 if (ldata
->aux_text_2
)
2272 render_text(ldata
->page
, ldata
->pdata
, ldata
,
2273 x
, top_y
- ldata
->ypos
,
2274 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
2275 keywords
, idx
, conf
);
2281 * There might be a cross-reference carried over from a
2284 if (dest
->type
!= NONE
) {
2287 xr
->dest
= *dest
; /* structure copy */
2288 if (ldata
->page
->last_xref
)
2289 ldata
->page
->last_xref
->next
= xr
;
2291 ldata
->page
->first_xref
= xr
;
2292 ldata
->page
->last_xref
= xr
;
2293 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
2294 xr
->by
= top_y
- ldata
->ypos
;
2295 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
2300 int extra_indent
, shortfall
, spaces
;
2301 int just
= ldata
->pdata
->justification
;
2304 * All forms of justification become JUST when we have
2305 * to squeeze the paragraph.
2307 if (ldata
->hshortfall
< 0)
2312 shortfall
= ldata
->hshortfall
;
2313 spaces
= ldata
->nspaces
;
2317 shortfall
= spaces
= extra_indent
= 0;
2320 shortfall
= spaces
= 0;
2321 extra_indent
= ldata
->real_shortfall
;
2325 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
2326 left_x
+ ldata
->xpos
+ extra_indent
,
2327 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
2328 &xr
, shortfall
, spaces
, &nspace
,
2329 keywords
, idx
, conf
);
2334 * There's a cross-reference continued on to the next line.
2344 static void render_para(para_data
*pdata
, paper_conf
*conf
,
2345 keywordlist
*keywords
, indexdata
*idx
,
2346 paragraph
*index_placeholder
, page_data
*index_page
)
2350 page_data
*cxref_page
;
2359 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
2361 * If this is a contents entry, we expect to have a single
2362 * enormous cross-reference rectangle covering the whole
2363 * thing. (Unless, of course, it spans multiple pages.)
2365 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
2366 cxref_page
= ldata
->page
;
2369 cxref
->dest
.type
= PAGE
;
2370 if (pdata
->contents_entry
== index_placeholder
) {
2371 cxref
->dest
.page
= index_page
;
2373 assert(pdata
->contents_entry
->private_data
);
2374 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2375 cxref
->dest
.page
= target
->first
->page
;
2377 cxref
->dest
.url
= NULL
;
2378 if (ldata
->page
->last_xref
)
2379 ldata
->page
->last_xref
->next
= cxref
;
2381 ldata
->page
->first_xref
= cxref
;
2382 ldata
->page
->last_xref
= cxref
;
2383 cxref
->lx
= conf
->left_margin
;
2384 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
2385 cxref
->ty
= conf
->paper_height
- conf
->top_margin
2386 - ldata
->ypos
+ ldata
->line_height
;
2388 if (pdata
->contents_entry
) {
2389 assert(cxref
!= NULL
);
2390 cxref
->by
= conf
->paper_height
- conf
->top_margin
2394 last_x
= render_line(ldata
, conf
->left_margin
,
2395 conf
->paper_height
- conf
->top_margin
,
2396 &dest
, keywords
, idx
, conf
);
2397 if (ldata
== pdata
->last
)
2402 * If this is a contents entry, add leaders and a page
2405 if (pdata
->contents_entry
) {
2411 if (pdata
->contents_entry
== index_placeholder
) {
2412 num
= index_page
->number
;
2414 assert(pdata
->contents_entry
->private_data
);
2415 target
= (para_data
*)pdata
->contents_entry
->private_data
;
2416 num
= target
->first
->page
->number
;
2420 wid
= paper_width_simple(pdata
, w
, conf
);
2423 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2424 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2425 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2426 render_string(pdata
->last
->page
,
2427 pdata
->fonts
[FONT_NORMAL
],
2428 pdata
->sizes
[FONT_NORMAL
],
2429 conf
->left_margin
+ x
,
2430 (conf
->paper_height
- conf
->top_margin
-
2431 pdata
->last
->ypos
), L
".", 0);
2433 render_string(pdata
->last
->page
,
2434 pdata
->fonts
[FONT_NORMAL
],
2435 pdata
->sizes
[FONT_NORMAL
],
2436 conf
->paper_width
- conf
->right_margin
- wid
,
2437 (conf
->paper_height
- conf
->top_margin
-
2438 pdata
->last
->ypos
), num
, 0);
2442 * Render any rectangle (chapter title underline or rule)
2443 * that goes with this paragraph.
2445 switch (pdata
->rect_type
) {
2446 case RECT_CHAPTER_UNDERLINE
:
2447 add_rect_to_page(pdata
->last
->page
,
2449 (conf
->paper_height
- conf
->top_margin
-
2451 conf
->chapter_underline_depth
),
2453 conf
->chapter_underline_thickness
);
2456 add_rect_to_page(pdata
->first
->page
,
2457 conf
->left_margin
+ pdata
->first
->xpos
,
2458 (conf
->paper_height
- conf
->top_margin
-
2460 pdata
->last
->line_height
),
2461 conf
->base_width
- pdata
->first
->xpos
,
2462 pdata
->last
->line_height
);
2464 default: /* placate gcc */
2469 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2471 para_data
*pdata
= snew(para_data
);
2474 * For code paragraphs, I'm going to hack grievously and
2475 * pretend the three normal fonts are the three code paragraph
2478 setfont(pdata
, &conf
->fcode
);
2480 pdata
->first
= pdata
->last
= NULL
;
2481 pdata
->outline_level
= -1;
2482 pdata
->rect_type
= RECT_NONE
;
2483 pdata
->contents_entry
= NULL
;
2484 pdata
->justification
= LEFT
;
2486 for (; words
; words
= words
->next
) {
2487 wchar_t *t
, *e
, *start
;
2488 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2490 int prev
= -1, curr
;
2493 if (words
->next
&& words
->next
->type
== word_Emph
) {
2494 e
= words
->next
->text
;
2495 words
= words
->next
;
2505 else if (*e
== L
'i')
2507 else if (*e
== L
'b')
2524 * We've isolated a maximal subsequence of the line
2525 * which has the same emphasis. Form it into a word
2531 w
->type
= (prev
== 0 ? word_WeakCode
:
2532 prev
== 1 ? word_Emph
: word_Normal
);
2533 w
->text
= snewn(t
-start
+1, wchar_t);
2534 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2535 w
->text
[t
-start
] = '\0';
2548 ldata
= snew(line_data
);
2550 ldata
->pdata
= pdata
;
2551 ldata
->first
= lhead
;
2553 ldata
->line_height
= conf
->fcode
.font_size
* UNITS_PER_PT
;
2555 ldata
->xpos
= indent
;
2558 pdata
->last
->next
= ldata
;
2559 ldata
->prev
= pdata
->last
;
2561 pdata
->first
= ldata
;
2565 pdata
->last
= ldata
;
2567 ldata
->hshortfall
= 0;
2569 ldata
->aux_text
= NULL
;
2570 ldata
->aux_text_2
= NULL
;
2571 ldata
->aux_left_indent
= 0;
2572 /* General opprobrium for breaking in a code paragraph. */
2573 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2576 standard_line_spacing(pdata
, conf
);
2581 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2583 para_data
*pdata
= snew(para_data
);
2586 ldata
= snew(line_data
);
2588 ldata
->pdata
= pdata
;
2589 ldata
->first
= NULL
;
2591 ldata
->line_height
= conf
->rule_thickness
;
2593 ldata
->xpos
= indent
;
2598 ldata
->hshortfall
= 0;
2600 ldata
->aux_text
= NULL
;
2601 ldata
->aux_text_2
= NULL
;
2602 ldata
->aux_left_indent
= 0;
2605 * Better to break after a rule than before it
2607 ldata
->penalty_after
+= 100000;
2608 ldata
->penalty_before
+= -100000;
2610 pdata
->first
= pdata
->last
= ldata
;
2611 pdata
->outline_level
= -1;
2612 pdata
->rect_type
= RECT_RULE
;
2613 pdata
->contents_entry
= NULL
;
2614 pdata
->justification
= LEFT
;
2616 standard_line_spacing(pdata
, conf
);
2622 * Plain-text-like formatting for outline titles.
2624 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2625 for (; text
; text
= text
->next
) switch (text
->type
) {
2626 case word_HyperLink
:
2628 case word_UpperXref
:
2629 case word_LowerXref
:
2638 case word_WhiteSpace
:
2639 case word_EmphSpace
:
2640 case word_CodeSpace
:
2641 case word_WkCodeSpace
:
2643 case word_EmphQuote
:
2644 case word_CodeQuote
:
2645 case word_WkCodeQuote
:
2646 assert(text
->type
!= word_CodeQuote
&&
2647 text
->type
!= word_WkCodeQuote
);
2648 if (towordstyle(text
->type
) == word_Emph
&&
2649 (attraux(text
->aux
) == attr_First
||
2650 attraux(text
->aux
) == attr_Only
))
2651 rdadd(rs
, L
'_'); /* FIXME: configurability */
2652 else if (towordstyle(text
->type
) == word_Code
&&
2653 (attraux(text
->aux
) == attr_First
||
2654 attraux(text
->aux
) == attr_Only
))
2655 rdadd(rs
, L
'\''); /* FIXME: configurability */
2656 if (removeattr(text
->type
) == word_Normal
) {
2657 rdadds(rs
, text
->text
);
2658 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2660 } else if (removeattr(text
->type
) == word_Quote
) {
2661 rdadd(rs
, L
'\''); /* fixme: configurability */
2663 if (towordstyle(text
->type
) == word_Emph
&&
2664 (attraux(text
->aux
) == attr_Last
||
2665 attraux(text
->aux
) == attr_Only
))
2666 rdadd(rs
, L
'_'); /* FIXME: configurability */
2667 else if (towordstyle(text
->type
) == word_Code
&&
2668 (attraux(text
->aux
) == attr_Last
||
2669 attraux(text
->aux
) == attr_Only
))
2670 rdadd(rs
, L
'\''); /* FIXME: configurability */
2675 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2678 rdstring rs
= {0, 0, NULL
};
2681 paper_rdaddw(&rs
, first
);
2683 rdadds(&rs
, separator
);
2685 paper_rdaddw(&rs
, second
);
2690 static word
*fake_word(wchar_t *text
)
2692 word
*ret
= snew(word
);
2695 ret
->type
= word_Normal
;
2696 ret
->text
= ustrdup(text
);
2697 ret
->breaks
= FALSE
;
2702 static word
*fake_space_word(void)
2704 word
*ret
= snew(word
);
2707 ret
->type
= word_WhiteSpace
;
2714 static word
*fake_page_ref(page_data
*page
)
2716 word
*ret
= snew(word
);
2719 ret
->type
= word_PageXref
;
2721 ret
->breaks
= FALSE
;
2723 ret
->private_data
= page
;
2727 static word
*fake_end_ref(void)
2729 word
*ret
= snew(word
);
2732 ret
->type
= word_XrefEnd
;
2734 ret
->breaks
= FALSE
;
2739 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2748 w
= dup_word_list(first
);
2756 w
= fake_word(separator
);
2762 *wptr
= dup_word_list(second
);
2768 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2772 if (!src
->first_line
)
2775 if (dest
->last_line
) {
2776 dest
->last_line
->next
= src
->first_line
;
2777 src
->first_line
->prev
= dest
->last_line
;
2779 dest
->last_line
= src
->last_line
;
2781 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2783 ldata
->xpos
+= right_shift
;
2785 if (ldata
== src
->last_line
)