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 * * all the measurements in `conf' should be configurable
31 * + notably paper size/shape
32 * * page header and footer should be configurable; we should
33 * be able to shift the page number elsewhere, and add other
34 * things such as the current chapter/section title and fixed
36 * * remove the fixed mapping from heading levels to heading
37 * styles; offer a menu of styles from which the user can
38 * choose at every heading level
39 * * first-line indent in paragraphs
40 * * fixed text: `Contents', `Index', bullet, quotes, the
41 * colon-space and full stop in chapter title constructions
42 * * configurable location of contents?
43 * * certainly configurably _remove_ the contents, and possibly
45 * * double-sided document switch?
46 * + means you have two header/footer formats which
48 * + and means that mandatory page breaks before chapter
49 * titles should include a blank page if necessary to
50 * start the next section to a right-hand page
54 * - ability to import other Type 1 fonts
55 * * we need to parse the font to extract its metrics
56 * * then we pass the font bodily to both PS and PDF so it can
57 * be included in the output file
59 * - character substitution for better typography?
60 * * fi, fl, ffi, ffl ligatures
61 * * use real ellipsis rather than ...
62 * * a hyphen in a word by itself might prefer to be an en-dash
63 * * (Americans might even want a convenient way to use an
65 * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw!
66 * * substituting `minus' for `hyphen' in the standard encoding
67 * is probably preferable in Courier, though certainly not in
69 * * if I do do this lot, I'm rather inclined to at least try
70 * to think up a configurable way to do it so that Americans
71 * can do em-dash tricks without my intervention and other
72 * people can do other odd things too.
81 typedef struct paper_conf_Tag paper_conf
;
82 typedef struct paper_idx_Tag paper_idx
;
84 struct paper_conf_Tag
{
91 int indent_list_bullet
;
95 int base_para_spacing
;
96 int chapter_top_space
;
97 int sect_num_left_space
;
98 int chapter_underline_depth
;
99 int chapter_underline_thickness
;
102 int contents_indent_step
;
104 int leader_separation
;
108 int pagenum_fontsize
;
110 /* These are derived from the above */
114 /* Fonts used in the configuration */
115 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
118 struct paper_idx_Tag
{
120 * Word list giving the page numbers on which this index entry
121 * appears. Also the last word in the list, for ease of
127 * The last page added to the list (so we can ensure we don't
134 word_PageXref
= word_NotWordType
+ 1
137 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
138 static void wrap_paragraph(para_data
*pdata
, word
*words
,
139 int w
, int i1
, int i2
);
140 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
141 int page_height
, int ncols
, int headspace
);
142 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
143 int x
, int y
, wchar_t *str
);
144 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
145 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
);
146 static void render_para(para_data
*pdata
, paper_conf
*conf
,
147 keywordlist
*keywords
, indexdata
*idx
,
148 paragraph
*index_placeholder
, page_data
*index_page
);
149 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
150 static int paper_width_simple(para_data
*pdata
, word
*text
);
151 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
152 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
153 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
154 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
155 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
157 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
158 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
160 static word
*fake_word(wchar_t *text
);
161 static word
*fake_space_word(void);
162 static word
*fake_page_ref(page_data
*page
);
163 static word
*fake_end_ref(void);
164 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
166 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
168 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
172 int indent
, used_contents
;
173 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
174 para_data
*firstcont
, *lastcont
;
175 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
181 paragraph index_placeholder_para
;
182 page_data
*first_index_page
;
185 * FIXME: All these things ought to become configurable.
187 conf
= mknew(paper_conf
);
188 conf
->paper_width
= 595 * 4096;
189 conf
->paper_height
= 841 * 4096;
190 conf
->left_margin
= 72 * 4096;
191 conf
->top_margin
= 72 * 4096;
192 conf
->right_margin
= 72 * 4096;
193 conf
->bottom_margin
= 108 * 4096;
194 conf
->indent_list_bullet
= 6 * 4096;
195 conf
->indent_list
= 24 * 4096;
196 conf
->indent_quote
= 18 * 4096;
197 conf
->base_leading
= 4096;
198 conf
->base_para_spacing
= 10 * 4096;
199 conf
->chapter_top_space
= 72 * 4096;
200 conf
->sect_num_left_space
= 12 * 4096;
201 conf
->chapter_underline_depth
= 14 * 4096;
202 conf
->chapter_underline_thickness
= 3 * 4096;
203 conf
->rule_thickness
= 1 * 4096;
204 conf
->base_font_size
= 12;
205 conf
->contents_indent_step
= 24 * 4096;
206 conf
->contents_margin
= 84 * 4096;
207 conf
->leader_separation
= 12 * 4096;
208 conf
->index_gutter
= 36 * 4096;
209 conf
->index_cols
= 2;
210 conf
->index_minsep
= 18 * 4096;
211 conf
->pagenum_fontsize
= 12;
212 conf
->footer_distance
= 32 * 4096;
215 conf
->paper_width
- conf
->left_margin
- conf
->right_margin
;
217 conf
->paper_height
- conf
->top_margin
- conf
->bottom_margin
;
218 conf
->index_colwidth
=
219 (conf
->base_width
- (conf
->index_cols
-1) * conf
->index_gutter
)
223 * First, set up some font structures.
225 fontlist
= mknew(font_list
);
226 fontlist
->head
= fontlist
->tail
= NULL
;
227 conf
->tr
= make_std_font(fontlist
, "Times-Roman");
228 conf
->ti
= make_std_font(fontlist
, "Times-Italic");
229 conf
->hr
= make_std_font(fontlist
, "Helvetica-Bold");
230 conf
->hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
231 conf
->cr
= make_std_font(fontlist
, "Courier");
232 conf
->co
= make_std_font(fontlist
, "Courier-Oblique");
233 conf
->cb
= make_std_font(fontlist
, "Courier-Bold");
236 * Set up a data structure to collect page numbers for each
245 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
246 paper_idx
*pi
= mknew(paper_idx
);
250 pi
->words
= pi
->lastword
= NULL
;
253 entry
->backend_data
= pi
;
258 * Format the contents entry for each heading.
261 word
*contents_title
;
262 contents_title
= fake_word(L
"Contents");
264 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
265 NULL
, NULL
, contents_title
, conf
);
266 lastcont
= firstcont
;
267 lastcont
->next
= NULL
;
268 firstcontline
= firstcont
->first
;
269 lastcontline
= lastcont
->last
;
270 for (p
= sourceform
; p
; p
= p
->next
) {
277 case para_UnnumberedChapter
:
283 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
286 case para_UnnumberedChapter
:
287 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
292 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
293 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
296 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
297 conf
->contents_margin
,
298 NULL
, NULL
, words
, conf
);
300 pdata
->contents_entry
= p
;
301 lastcont
->next
= pdata
;
305 * Link all contents line structures together into
310 lastcontline
->next
= pdata
->first
;
311 pdata
->first
->prev
= lastcontline
;
313 firstcontline
= pdata
->first
;
314 pdata
->first
->prev
= NULL
;
316 lastcontline
= pdata
->last
;
317 lastcontline
->next
= NULL
;
325 * And one extra one, for the index.
328 pdata
= make_para_data(para_Normal
, 0, 0,
329 conf
->contents_margin
,
330 NULL
, NULL
, fake_word(L
"Index"), conf
);
332 pdata
->contents_entry
= &index_placeholder_para
;
333 lastcont
->next
= pdata
;
338 lastcontline
->next
= pdata
->first
;
339 pdata
->first
->prev
= lastcontline
;
341 firstcontline
= pdata
->first
;
342 pdata
->first
->prev
= NULL
;
344 lastcontline
= pdata
->last
;
345 lastcontline
->next
= NULL
;
351 * Do the main paragraph formatting.
354 used_contents
= FALSE
;
355 firstline
= lastline
= NULL
;
356 for (p
= sourceform
; p
; p
= p
->next
) {
357 p
->private_data
= NULL
;
361 * These paragraph types are either invisible or don't
362 * define text in the normal sense. Either way, they
363 * don't require wrapping.
368 case para_NotParaType
:
375 * These paragraph types don't require wrapping, but
376 * they do affect the line width to which we wrap the
377 * rest of the paragraphs, so we need to pay attention.
380 indent
+= conf
->indent_list
; break;
382 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
384 indent
+= conf
->indent_quote
; break;
386 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
389 * This paragraph type is special. Process it
393 pdata
= code_paragraph(indent
, p
->words
, conf
);
394 p
->private_data
= pdata
;
395 if (pdata
->first
!= pdata
->last
) {
396 pdata
->first
->penalty_after
+= 100000;
397 pdata
->last
->penalty_before
+= 100000;
402 * This paragraph is also special.
405 pdata
= rule_paragraph(indent
, conf
);
406 p
->private_data
= pdata
;
410 * All of these paragraph types require wrapping in the
411 * ordinary way. So we must supply a set of fonts, a
412 * line width and auxiliary information (e.g. bullet
413 * text) for each one.
417 case para_UnnumberedChapter
:
421 case para_BiblioCited
:
423 case para_NumberedList
:
424 case para_DescribedThing
:
425 case para_Description
:
428 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
429 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
431 p
->private_data
= pdata
;
436 if (p
->private_data
) {
437 pdata
= (para_data
*)p
->private_data
;
440 * If this is the first non-title heading, we link the
441 * contents section in before it.
443 if (!used_contents
&& pdata
->outline_level
> 0) {
444 used_contents
= TRUE
;
446 lastpara
->next
= firstcont
;
448 firstpara
= firstcont
;
450 assert(lastpara
->next
== NULL
);
453 lastline
->next
= firstcontline
;
454 firstcontline
->prev
= lastline
;
456 firstline
= firstcontline
;
457 firstcontline
->prev
= NULL
;
459 assert(lastcontline
!= NULL
);
460 lastline
= lastcontline
;
461 lastline
->next
= NULL
;
465 * Link all line structures together into a big list.
469 lastline
->next
= pdata
->first
;
470 pdata
->first
->prev
= lastline
;
472 firstline
= pdata
->first
;
473 pdata
->first
->prev
= NULL
;
475 lastline
= pdata
->last
;
476 lastline
->next
= NULL
;
480 * Link all paragraph structures together similarly.
484 lastpara
->next
= pdata
;
492 * Now we have an enormous linked list of every line of text in
493 * the document. Break it up into pages.
495 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
506 for (page
= pages
; page
; page
= page
->next
) {
507 sprintf(buf
, "%d", ++pagenum
);
508 page
->number
= ufroma_dup(buf
);
512 first_index_page
= mknew(page_data
);
513 first_index_page
->next
= first_index_page
->prev
= NULL
;
514 first_index_page
->first_line
= NULL
;
515 first_index_page
->last_line
= NULL
;
516 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
517 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
518 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
520 /* And don't forget the as-yet-uncreated index. */
521 sprintf(buf
, "%d", ++pagenum
);
522 first_index_page
->number
= ufroma_dup(buf
);
527 * Now we're ready to actually lay out the pages. We do this by
528 * looping over _paragraphs_, since we may need to track cross-
529 * references between lines and even across pages.
531 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
532 render_para(pdata
, conf
, keywords
, idx
,
533 &index_placeholder_para
, first_index_page
);
536 * Now we've laid out the main body pages, we should have
537 * acquired a full set of page numbers for the index.
543 para_data
*firstidx
, *lastidx
;
544 line_data
*firstidxline
, *lastidxline
, *ldata
;
545 page_data
*ipages
, *ipages2
, *page
;
548 * Create a set of paragraphs for the index.
550 index_title
= fake_word(L
"Index");
552 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
553 NULL
, NULL
, index_title
, conf
);
555 lastidx
->next
= NULL
;
556 firstidxline
= firstidx
->first
;
557 lastidxline
= lastidx
->last
;
558 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
559 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
560 para_data
*text
, *pages
;
565 text
= make_para_data(para_Normal
, 0, 0,
566 conf
->base_width
- conf
->index_colwidth
,
567 NULL
, NULL
, entry
->text
, conf
);
569 pages
= make_para_data(para_Normal
, 0, 0,
570 conf
->base_width
- conf
->index_colwidth
,
571 NULL
, NULL
, pi
->words
, conf
);
573 text
->justification
= LEFT
;
574 pages
->justification
= RIGHT
;
575 text
->last
->space_after
= pages
->first
->space_before
=
576 conf
->base_leading
/ 2;
578 pages
->last
->space_after
= text
->first
->space_before
=
582 assert(pages
->first
);
587 * If feasible, fold the two halves of the index entry
590 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
591 conf
->index_colwidth
+ conf
->index_minsep
) {
592 text
->last
->space_after
= -1;
593 pages
->first
->space_before
= -pages
->first
->line_height
+1;
596 lastidx
->next
= text
;
602 * Link all index line structures together into
605 text
->last
->next
= pages
->first
;
606 pages
->first
->prev
= text
->last
;
608 lastidxline
->next
= text
->first
;
609 text
->first
->prev
= lastidxline
;
611 lastidxline
= pages
->last
;
614 * Breaking an index entry anywhere is so bad that I
615 * think I'm going to forbid it totally.
617 for (ldata
= text
->first
; ldata
&& ldata
->next
;
618 ldata
= ldata
->next
) {
619 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
620 ldata
->space_after
= -1;
625 * Now break the index into pages.
627 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
629 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
632 firstidxline
->space_before
+
633 firstidxline
->line_height
+
634 firstidxline
->space_after
);
637 * This will have put each _column_ of the index on a
638 * separate page, which isn't what we want. Fold the pages
645 for (i
= 1; i
< conf
->index_cols
; i
++)
649 fold_into_page(page
, page
->next
,
650 i
* (conf
->index_colwidth
+
651 conf
->index_gutter
));
653 page
->next
= page
->next
->next
;
655 page
->next
->prev
= page
;
661 /* Also fold the heading on to the same page as the index items. */
662 fold_into_page(ipages
, ipages2
, 0);
663 ipages
->next
= ipages2
->next
;
665 ipages
->next
->prev
= ipages
;
667 fold_into_page(first_index_page
, ipages
, 0);
668 first_index_page
->next
= ipages
->next
;
669 if (first_index_page
->next
)
670 first_index_page
->next
->prev
= first_index_page
;
672 ipages
= first_index_page
;
675 * Number the index pages, except the already-numbered
678 for (page
= ipages
->next
; page
; page
= page
->next
) {
680 sprintf(buf
, "%d", ++pagenum
);
681 page
->number
= ufroma_dup(buf
);
685 * Render the index pages.
687 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
688 render_para(pdata
, conf
, keywords
, idx
,
689 &index_placeholder_para
, first_index_page
);
692 * Link the index page list on to the end of the main page
698 for (page
= pages
; page
->next
; page
= page
->next
);
703 * Same with the paragraph list, which will cause the index
704 * to be mentioned in the document outline.
707 firstpara
= firstidx
;
709 lastpara
->next
= firstidx
;
714 * Draw the headers and footers.
716 * FIXME: this should be fully configurable, but for the moment
717 * I'm just going to put in page numbers in the centre of a
718 * footer and leave it at that.
723 for (page
= pages
; page
; page
= page
->next
) {
726 width
= conf
->pagenum_fontsize
*
727 string_width(conf
->tr
, page
->number
, NULL
);
729 render_string(page
, conf
->tr
, conf
->pagenum_fontsize
,
730 conf
->left_margin
+ (conf
->base_width
- width
)/2,
731 conf
->bottom_margin
- conf
->footer_distance
,
737 * Start putting together the overall document structure we're
740 doc
= mknew(document
);
741 doc
->fonts
= fontlist
;
743 doc
->paper_width
= conf
->paper_width
;
744 doc
->paper_height
= conf
->paper_height
;
747 * Collect the section heading paragraphs into a document
748 * outline. This is slightly fiddly because the Title paragraph
749 * isn't required to be at the start, although all the others
755 doc
->outline_elements
= mknewa(outline_element
, osize
);
756 doc
->n_outline_elements
= 0;
758 /* First find the title. */
759 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
760 if (pdata
->outline_level
== 0) {
761 doc
->outline_elements
[0].level
= 0;
762 doc
->outline_elements
[0].pdata
= pdata
;
763 doc
->n_outline_elements
++;
768 /* Then collect the rest. */
769 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
770 if (pdata
->outline_level
> 0) {
771 if (doc
->n_outline_elements
>= osize
) {
773 doc
->outline_elements
=
774 resize(doc
->outline_elements
, osize
);
777 doc
->outline_elements
[doc
->n_outline_elements
].level
=
778 pdata
->outline_level
;
779 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
780 doc
->n_outline_elements
++;
790 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
791 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
796 int extra_indent
, firstline_indent
, aux_indent
;
799 pdata
= mknew(para_data
);
800 pdata
->outline_level
= -1;
801 pdata
->outline_title
= NULL
;
802 pdata
->rect_type
= RECT_NONE
;
803 pdata
->contents_entry
= NULL
;
804 pdata
->justification
= JUST
;
807 * Choose fonts for this paragraph.
809 * FIXME: All of this ought to be completely
814 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
815 pdata
->sizes
[FONT_NORMAL
] = 24;
816 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
817 pdata
->sizes
[FONT_EMPH
] = 24;
818 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
819 pdata
->sizes
[FONT_CODE
] = 24;
820 pdata
->outline_level
= 0;
825 case para_UnnumberedChapter
:
826 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
827 pdata
->sizes
[FONT_NORMAL
] = 20;
828 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
829 pdata
->sizes
[FONT_EMPH
] = 20;
830 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
831 pdata
->sizes
[FONT_CODE
] = 20;
832 pdata
->outline_level
= 1;
837 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
838 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
839 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
840 pdata
->sizes
[FONT_NORMAL
] =
841 pdata
->sizes
[FONT_EMPH
] =
842 pdata
->sizes
[FONT_CODE
] =
843 (paux
== 0 ?
16 : paux
== 1 ?
14 : 13);
844 pdata
->outline_level
= 2 + paux
;
848 case para_BiblioCited
:
850 case para_NumberedList
:
851 case para_DescribedThing
:
852 case para_Description
:
854 pdata
->fonts
[FONT_NORMAL
] = conf
->tr
;
855 pdata
->sizes
[FONT_NORMAL
] = 12;
856 pdata
->fonts
[FONT_EMPH
] = conf
->ti
;
857 pdata
->sizes
[FONT_EMPH
] = 12;
858 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
859 pdata
->sizes
[FONT_CODE
] = 12;
864 * Also select an indentation level depending on the
865 * paragraph type (list paragraphs other than
866 * para_DescribedThing need extra indent).
868 * (FIXME: Perhaps at some point we might even arrange
869 * for the user to be able to request indented first
870 * lines in paragraphs.)
872 if (ptype
== para_Bullet
||
873 ptype
== para_NumberedList
||
874 ptype
== para_Description
) {
875 extra_indent
= firstline_indent
= conf
->indent_list
;
877 extra_indent
= firstline_indent
= 0;
881 * Find the auxiliary text for this paragraph.
892 * For some heading styles (FIXME: be able to
893 * configure which), the auxiliary text contains
894 * the chapter number and is arranged to be
895 * right-aligned a few points left of the primary
896 * margin. For other styles, the auxiliary text is
897 * the full chapter _name_ and takes up space
898 * within the (wrapped) chapter title, meaning that
899 * we must move the first line indent over to make
902 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
906 len
= paper_width_simple(pdata
, pkwtext2
);
907 aux_indent
= -len
- conf
->sect_num_left_space
;
909 pdata
->outline_title
=
910 prepare_outline_title(pkwtext2
, L
" ", pwords
);
913 aux2
= fake_word(L
": ");
916 firstline_indent
+= paper_width_simple(pdata
, aux
);
917 firstline_indent
+= paper_width_simple(pdata
, aux2
);
919 pdata
->outline_title
=
920 prepare_outline_title(pkwtext
, L
": ", pwords
);
926 * Auxiliary text consisting of a bullet. (FIXME:
927 * configurable bullet.)
929 aux
= fake_word(L
"\x2022");
930 aux_indent
= indent
+ conf
->indent_list_bullet
;
933 case para_NumberedList
:
935 * Auxiliary text consisting of the number followed
936 * by a (FIXME: configurable) full stop.
939 aux2
= fake_word(L
".");
940 aux_indent
= indent
+ conf
->indent_list_bullet
;
943 case para_BiblioCited
:
945 * Auxiliary text consisting of the bibliography
946 * reference text, and a trailing space.
949 aux2
= fake_word(L
" ");
951 firstline_indent
+= paper_width_simple(pdata
, aux
);
952 firstline_indent
+= paper_width_simple(pdata
, aux2
);
956 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
957 pdata
->outline_title
=
958 prepare_outline_title(NULL
, NULL
, pwords
);
961 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
962 indent
+ firstline_indent
,
963 indent
+ extra_indent
);
965 pdata
->first
->aux_text
= aux
;
966 pdata
->first
->aux_text_2
= aux2
;
967 pdata
->first
->aux_left_indent
= aux_indent
;
970 * Line breaking penalties.
977 case para_UnnumberedChapter
:
979 * Fixed and large penalty for breaking straight
980 * after a heading; corresponding bonus for
981 * breaking straight before.
983 pdata
->first
->penalty_before
= -500000;
984 pdata
->last
->penalty_after
= 500000;
985 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
986 ldata
->penalty_after
= 500000;
989 case para_DescribedThing
:
991 * This is treated a bit like a small heading:
992 * there's a penalty for breaking after it (i.e.
993 * between it and its description), and a bonus for
994 * breaking before it (actually _between_ list
997 pdata
->first
->penalty_before
= -200000;
998 pdata
->last
->penalty_after
= 200000;
1003 * Most paragraph types: widow/orphan control by
1004 * discouraging breaking one line from the end of
1007 if (pdata
->first
!= pdata
->last
) {
1008 pdata
->first
->penalty_after
= 100000;
1009 pdata
->last
->penalty_before
= 100000;
1014 standard_line_spacing(pdata
, conf
);
1017 * Some kinds of section heading require a page break before
1018 * them and an underline after.
1020 if (ptype
== para_Title
||
1021 ptype
== para_Chapter
||
1022 ptype
== para_Appendix
||
1023 ptype
== para_UnnumberedChapter
) {
1024 pdata
->first
->page_break
= TRUE
;
1025 pdata
->first
->space_before
= conf
->chapter_top_space
;
1026 pdata
->last
->space_after
+=
1027 (conf
->chapter_underline_depth
+
1028 conf
->chapter_underline_thickness
);
1029 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1035 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1040 * Set the line spacing for each line in this paragraph.
1042 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1043 if (ldata
== pdata
->first
)
1044 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1046 ldata
->space_before
= conf
->base_leading
/ 2;
1047 if (ldata
== pdata
->last
)
1048 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1050 ldata
->space_after
= conf
->base_leading
/ 2;
1051 ldata
->page_break
= FALSE
;
1055 static font_encoding
*new_font_encoding(font_data
*font
)
1060 fe
= mknew(font_encoding
);
1063 if (font
->list
->tail
)
1064 font
->list
->tail
->next
= fe
;
1066 font
->list
->head
= fe
;
1067 font
->list
->tail
= fe
;
1070 fe
->free_pos
= 0x21;
1072 for (i
= 0; i
< 256; i
++) {
1073 fe
->vector
[i
] = NULL
;
1074 fe
->indices
[i
] = -1;
1075 fe
->to_unicode
[i
] = 0xFFFF;
1081 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1089 widths
= ps_std_font_widths(name
);
1093 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
1095 f
= mknew(font_data
);
1099 f
->nglyphs
= nglyphs
;
1100 f
->glyphs
= ps_std_glyphs
;
1102 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
1105 * Our first subfont will contain all of US-ASCII. This isn't
1106 * really necessary - we could just create custom subfonts
1107 * precisely as the whim of render_string dictated - but
1108 * instinct suggests that it might be nice to have the text in
1109 * the output files look _marginally_ recognisable.
1111 fe
= new_font_encoding(f
);
1112 fe
->free_pos
= 0xA1; /* only the top half is free */
1113 f
->latest_subfont
= fe
;
1115 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
1118 for (i
= 0; i
< nglyphs
; i
++) {
1120 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
1121 assert(ucs
!= 0xFFFF);
1123 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1124 fe
->vector
[ucs
] = f
->glyphs
[i
];
1125 fe
->indices
[ucs
] = i
;
1126 fe
->to_unicode
[ucs
] = ucs
;
1127 f
->subfont_map
[i
].subfont
= fe
;
1128 f
->subfont_map
[i
].position
= ucs
;
1131 * This character is not yet assigned to a subfont.
1133 f
->subfont_map
[i
].subfont
= NULL
;
1134 f
->subfont_map
[i
].position
= 0;
1141 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1148 for (; *string
; string
++) {
1151 index
= font
->bmp
[(unsigned short)*string
];
1152 if (index
== 0xFFFF) {
1156 width
+= font
->widths
[index
];
1163 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1165 struct paper_width_ctx
{
1170 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1172 while (text
&& text
!= end
) {
1173 w
+= paper_width_internal(vctx
, text
, nspaces
);
1179 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1181 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1182 int style
, type
, findex
, width
, errs
;
1185 switch (word
->type
) {
1186 case word_HyperLink
:
1188 case word_UpperXref
:
1189 case word_LowerXref
:
1196 style
= towordstyle(word
->type
);
1197 type
= removeattr(word
->type
);
1199 findex
= (style
== word_Normal ? FONT_NORMAL
:
1200 style
== word_Emph ? FONT_EMPH
:
1203 if (type
== word_Normal
) {
1205 } else if (type
== word_WhiteSpace
) {
1206 if (findex
!= FONT_CODE
) {
1209 return ctx
->minspacewidth
;
1212 } else /* if (type == word_Quote) */ {
1213 if (word
->aux
== quote_Open
)
1214 str
= L
"\x2018"; /* FIXME: configurability! */
1216 str
= L
"\x2019"; /* FIXME: configurability! */
1219 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1221 if (errs
&& word
->alt
)
1222 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1224 return ctx
->pdata
->sizes
[findex
] * width
;
1227 static int paper_width(void *vctx
, word
*word
)
1229 return paper_width_internal(vctx
, word
, NULL
);
1232 static int paper_width_simple(para_data
*pdata
, word
*text
)
1234 struct paper_width_ctx ctx
;
1238 (pdata
->sizes
[FONT_NORMAL
] *
1239 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1241 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1244 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1245 int w
, int i1
, int i2
)
1247 wrappedline
*wrapping
, *p
;
1249 struct paper_width_ctx ctx
;
1253 * We're going to need to store the line height in every line
1254 * structure we generate.
1259 for (i
= 0; i
< NFONTS
; i
++)
1260 if (line_height
< pdata
->sizes
[i
])
1261 line_height
= pdata
->sizes
[i
];
1262 line_height
*= 4096;
1265 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1266 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1267 if (spacewidth
== 0) {
1269 * A font without a space?! Disturbing. I hope this never
1270 * comes up, but I'll make a random guess anyway and set my
1271 * space width to half the point size.
1273 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
1277 * I'm going to set the _minimum_ space width to 3/5 of the
1278 * standard one, and use the standard one as the optimum.
1280 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1283 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1286 * Having done the wrapping, we now concoct a set of line_data
1289 pdata
->first
= pdata
->last
= NULL
;
1291 for (p
= wrapping
; p
; p
= p
->next
) {
1294 int len
, wid
, spaces
;
1296 ldata
= mknew(line_data
);
1298 ldata
->pdata
= pdata
;
1299 ldata
->first
= p
->begin
;
1300 ldata
->end
= p
->end
;
1301 ldata
->line_height
= line_height
;
1303 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1306 pdata
->last
->next
= ldata
;
1307 ldata
->prev
= pdata
->last
;
1309 pdata
->first
= ldata
;
1313 pdata
->last
= ldata
;
1316 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1317 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1320 ldata
->hshortfall
= wid
- len
;
1321 ldata
->nspaces
= spaces
;
1323 * This tells us how much the space width needs to
1324 * change from _min_spacewidth. But we want to store
1325 * its difference from the _natural_ space width, to
1326 * make the text rendering easier.
1328 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1329 ldata
->hshortfall
-= spacewidth
* spaces
;
1330 ldata
->real_shortfall
= ldata
->hshortfall
;
1332 * Special case: on the last line of a paragraph, we
1333 * never stretch spaces.
1335 if (ldata
->hshortfall
> 0 && !p
->next
)
1336 ldata
->hshortfall
= 0;
1338 ldata
->aux_text
= NULL
;
1339 ldata
->aux_text_2
= NULL
;
1340 ldata
->aux_left_indent
= 0;
1341 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1346 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1347 int page_height
, int ncols
, int headspace
)
1351 int n
, n1
, this_height
;
1354 * Page breaking is done by a close analogue of the optimal
1355 * paragraph wrapping algorithm used by wrap_para(). We work
1356 * backwards from the end of the document line by line; for
1357 * each line, we contemplate every possible number of lines we
1358 * could put on a page starting with that line, determine a
1359 * cost function for each one, add it to the pre-computed cost
1360 * function for optimally page-breaking everything after that
1361 * page, and pick the best option.
1363 * This is made slightly more complex by the fact that we have
1364 * a multi-column index with a heading at the top of the
1365 * _first_ page, meaning that the first _ncols_ pages must have
1366 * a different length. Hence, we must do the wrapping ncols+1
1367 * times over, hypothetically trying to put every subsequence
1368 * on every possible page.
1370 * Since my line_data structures are only used for this
1371 * purpose, I might as well just store the algorithm data
1375 for (l
= last
; l
; l
= l
->prev
) {
1376 l
->bestcost
= mknewa(int, ncols
+1);
1377 l
->vshortfall
= mknewa(int, ncols
+1);
1378 l
->text
= mknewa(int, ncols
+1);
1379 l
->space
= mknewa(int, ncols
+1);
1380 l
->page_last
= mknewa(line_data
*, ncols
+1);
1382 for (n
= 0; n
<= ncols
; n
++) {
1383 int minheight
, text
= 0, space
= 0;
1386 n1
= (n
< ncols ? n
+1 : ncols
);
1388 this_height
= page_height
- headspace
;
1390 this_height
= page_height
;
1392 l
->bestcost
[n
] = -1;
1393 for (m
= l
; m
; m
= m
->next
) {
1394 if (m
!= l
&& m
->page_break
)
1395 break; /* we've gone as far as we can */
1398 if (m
->prev
->space_after
> 0)
1399 space
+= m
->prev
->space_after
;
1401 text
+= m
->prev
->space_after
;
1403 if (m
!= l
|| m
->page_break
) {
1404 if (m
->space_before
> 0)
1405 space
+= m
->space_before
;
1407 text
+= m
->space_before
;
1409 text
+= m
->line_height
;
1410 minheight
= text
+ space
;
1412 if (m
!= l
&& minheight
> this_height
)
1416 * If the space after this paragraph is _negative_
1417 * (which means the next line is folded on to this
1418 * one, which happens in the index), we absolutely
1419 * cannot break here.
1421 if (m
->space_after
>= 0) {
1424 * Compute the cost of this arrangement, as the
1425 * square of the amount of wasted space on the
1426 * page. Exception: if this is the last page
1427 * before a mandatory break or the document
1428 * end, we don't penalise a large blank area.
1430 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1432 int x
= this_height
- minheight
;
1439 cost
+= (x
* xf
) >> 8;
1443 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1444 cost
+= m
->penalty_after
;
1445 cost
+= m
->next
->penalty_before
;
1448 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1449 cost
+= m
->next
->bestcost
[n1
];
1450 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1452 * This is the best option yet for this
1455 l
->bestcost
[n
] = cost
;
1456 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1457 l
->vshortfall
[n
] = this_height
- minheight
;
1459 l
->vshortfall
[n
] = 0;
1461 l
->space
[n
] = space
;
1462 l
->page_last
[n
] = m
;
1473 * Now go through the line list forwards and assemble the
1482 int text
, space
, head
;
1484 page
= mknew(page_data
);
1493 page
->first_line
= l
;
1494 page
->last_line
= l
->page_last
[n
];
1496 page
->first_text
= page
->last_text
= NULL
;
1497 page
->first_xref
= page
->last_xref
= NULL
;
1498 page
->first_rect
= page
->last_rect
= NULL
;
1501 * Now assign a y-coordinate to each line on the page.
1504 head
= (n
< ncols ? headspace
: 0);
1505 for (l
= page
->first_line
; l
; l
= l
->next
) {
1506 if (l
!= page
->first_line
) {
1507 if (l
->prev
->space_after
> 0)
1508 space
+= l
->prev
->space_after
;
1510 text
+= l
->prev
->space_after
;
1512 if (l
!= page
->first_line
|| l
->page_break
) {
1513 if (l
->space_before
> 0)
1514 space
+= l
->space_before
;
1516 text
+= l
->space_before
;
1518 text
+= l
->line_height
;
1521 l
->ypos
= text
+ space
+ head
+
1522 space
* (float)page
->first_line
->vshortfall
[n
] /
1523 page
->first_line
->space
[n
];
1525 if (l
== page
->last_line
)
1529 l
= page
->last_line
;
1534 n
= (n
< ncols ? n
+1 : ncols
);
1540 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1542 rect
*r
= mknew(rect
);
1545 if (page
->last_rect
)
1546 page
->last_rect
->next
= r
;
1548 page
->first_rect
= r
;
1549 page
->last_rect
= r
;
1557 static void add_string_to_page(page_data
*page
, int x
, int y
,
1558 font_encoding
*fe
, int size
, char *text
,
1561 text_fragment
*frag
;
1563 frag
= mknew(text_fragment
);
1566 if (page
->last_text
)
1567 page
->last_text
->next
= frag
;
1569 page
->first_text
= frag
;
1570 page
->last_text
= frag
;
1575 frag
->fontsize
= size
;
1576 frag
->text
= dupstr(text
);
1577 frag
->width
= width
;
1581 * Returns the updated x coordinate.
1583 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1584 int x
, int y
, wchar_t *str
)
1587 int textpos
, textwid
, glyph
;
1588 font_encoding
*subfont
= NULL
, *sf
;
1590 text
= mknewa(char, 1 + ustrlen(str
));
1591 textpos
= textwid
= 0;
1594 glyph
= font
->bmp
[*str
];
1596 if (glyph
== 0xFFFF) {
1598 continue; /* nothing more we can do here */
1602 * Find which subfont this character is going in.
1604 sf
= font
->subfont_map
[glyph
].subfont
;
1610 * This character is not yet in a subfont. Assign one.
1612 if (font
->latest_subfont
->free_pos
>= 0x100)
1613 font
->latest_subfont
= new_font_encoding(font
);
1615 c
= font
->latest_subfont
->free_pos
++;
1616 if (font
->latest_subfont
->free_pos
== 0x7F)
1617 font
->latest_subfont
->free_pos
= 0xA1;
1619 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1620 font
->subfont_map
[glyph
].position
= c
;
1621 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1622 font
->latest_subfont
->indices
[c
] = glyph
;
1623 font
->latest_subfont
->to_unicode
[c
] = *str
;
1625 sf
= font
->latest_subfont
;
1628 if (!subfont
|| sf
!= subfont
) {
1630 text
[textpos
] = '\0';
1631 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
,
1635 assert(textpos
== 0);
1641 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1642 textwid
+= font
->widths
[glyph
] * fontsize
;
1648 text
[textpos
] = '\0';
1649 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
, textwid
);
1657 * Returns the updated x coordinate.
1659 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1660 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1661 int shortfall
, int nspaces
, int *nspace
,
1662 keywordlist
*keywords
, indexdata
*idx
)
1664 while (text
&& text
!= text_end
) {
1665 int style
, type
, findex
, errs
;
1669 switch (text
->type
) {
1671 * Start a cross-reference.
1673 case word_HyperLink
:
1674 case word_UpperXref
:
1675 case word_LowerXref
:
1678 if (text
->type
== word_HyperLink
) {
1680 dest
.url
= utoa_dup(text
->text
);
1682 } else if (text
->type
== word_PageXref
) {
1685 dest
.page
= (page_data
*)text
->private_data
;
1687 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1691 assert(kwl
->para
->private_data
);
1692 pdata
= (para_data
*) kwl
->para
->private_data
;
1694 dest
.page
= pdata
->first
->page
;
1698 * Shouldn't happen, but *shrug*
1705 if (dest
.type
!= NONE
) {
1707 (*xr
)->dest
= dest
; /* structure copy */
1708 if (page
->last_xref
)
1709 page
->last_xref
->next
= *xr
;
1711 page
->first_xref
= *xr
;
1712 page
->last_xref
= *xr
;
1716 * FIXME: Ideally we should have, and use, some
1717 * vertical font metric information here so that
1718 * our cross-ref rectangle can take account of
1719 * descenders and the font's cap height. This will
1720 * do for the moment, but it isn't ideal.
1722 (*xr
)->lx
= (*xr
)->rx
= x
;
1724 (*xr
)->ty
= y
+ ldata
->line_height
;
1729 * Finish extending a cross-reference box.
1737 * Add the current page number to the list of pages
1738 * referenced by an index entry.
1742 * We don't create index references in contents entries.
1744 if (!pdata
->contents_entry
) {
1748 tag
= index_findtag(idx
, text
->text
);
1752 for (i
= 0; i
< tag
->nrefs
; i
++) {
1753 indexentry
*entry
= tag
->refs
[i
];
1754 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
1757 * If the same index term is indexed twice
1758 * within the same section, we only want to
1759 * mention it once in the index.
1761 if (pi
->lastpage
!= page
) {
1765 pi
->lastword
= pi
->lastword
->next
=
1767 pi
->lastword
= pi
->lastword
->next
=
1769 wp
= &pi
->lastword
->next
;
1773 pi
->lastword
= *wp
=
1774 fake_page_ref(page
);
1775 pi
->lastword
= pi
->lastword
->next
=
1776 fake_word(page
->number
);
1777 pi
->lastword
= pi
->lastword
->next
=
1781 pi
->lastpage
= page
;
1787 style
= towordstyle(text
->type
);
1788 type
= removeattr(text
->type
);
1790 findex
= (style
== word_Normal ? FONT_NORMAL
:
1791 style
== word_Emph ? FONT_EMPH
:
1794 if (type
== word_Normal
) {
1796 } else if (type
== word_WhiteSpace
) {
1797 x
+= pdata
->sizes
[findex
] *
1798 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1799 if (nspaces
&& findex
!= FONT_CODE
) {
1800 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1801 x
-= *nspace
* shortfall
/ nspaces
;
1805 } else /* if (type == word_Quote) */ {
1806 if (text
->aux
== quote_Open
)
1807 str
= L
"\x2018"; /* FIXME: configurability! */
1809 str
= L
"\x2019"; /* FIXME: configurability! */
1812 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1814 if (errs
&& text
->alt
)
1815 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1816 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
);
1818 x
= render_string(page
, pdata
->fonts
[findex
],
1819 pdata
->sizes
[findex
], x
, y
, str
);
1832 * Returns the last x position used on the line.
1834 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
1835 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
)
1841 if (ldata
->aux_text
) {
1845 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1846 left_x
+ ldata
->aux_left_indent
,
1847 top_y
- ldata
->ypos
,
1848 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
1850 if (ldata
->aux_text_2
)
1851 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1852 x
, top_y
- ldata
->ypos
,
1853 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
1860 * There might be a cross-reference carried over from a
1863 if (dest
->type
!= NONE
) {
1866 xr
->dest
= *dest
; /* structure copy */
1867 if (ldata
->page
->last_xref
)
1868 ldata
->page
->last_xref
->next
= xr
;
1870 ldata
->page
->first_xref
= xr
;
1871 ldata
->page
->last_xref
= xr
;
1872 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1873 xr
->by
= top_y
- ldata
->ypos
;
1874 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1879 int extra_indent
, shortfall
, spaces
;
1880 int just
= ldata
->pdata
->justification
;
1883 * All forms of justification become JUST when we have
1884 * to squeeze the paragraph.
1886 if (ldata
->hshortfall
< 0)
1891 shortfall
= ldata
->hshortfall
;
1892 spaces
= ldata
->nspaces
;
1896 shortfall
= spaces
= extra_indent
= 0;
1899 shortfall
= spaces
= 0;
1900 extra_indent
= ldata
->real_shortfall
;
1904 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1905 left_x
+ ldata
->xpos
+ extra_indent
,
1906 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
1907 &xr
, shortfall
, spaces
, &nspace
,
1913 * There's a cross-reference continued on to the next line.
1923 static void render_para(para_data
*pdata
, paper_conf
*conf
,
1924 keywordlist
*keywords
, indexdata
*idx
,
1925 paragraph
*index_placeholder
, page_data
*index_page
)
1929 page_data
*cxref_page
;
1938 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1940 * If this is a contents entry, we expect to have a single
1941 * enormous cross-reference rectangle covering the whole
1942 * thing. (Unless, of course, it spans multiple pages.)
1944 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
1945 cxref_page
= ldata
->page
;
1946 cxref
= mknew(xref
);
1948 cxref
->dest
.type
= PAGE
;
1949 if (pdata
->contents_entry
== index_placeholder
) {
1950 cxref
->dest
.page
= index_page
;
1952 assert(pdata
->contents_entry
->private_data
);
1953 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1954 cxref
->dest
.page
= target
->first
->page
;
1956 cxref
->dest
.url
= NULL
;
1957 if (ldata
->page
->last_xref
)
1958 ldata
->page
->last_xref
->next
= cxref
;
1960 ldata
->page
->first_xref
= cxref
;
1961 ldata
->page
->last_xref
= cxref
;
1962 cxref
->lx
= conf
->left_margin
;
1963 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
1964 cxref
->ty
= conf
->paper_height
- conf
->top_margin
1965 - ldata
->ypos
+ ldata
->line_height
;
1967 if (pdata
->contents_entry
) {
1968 assert(cxref
!= NULL
);
1969 cxref
->by
= conf
->paper_height
- conf
->top_margin
1973 last_x
= render_line(ldata
, conf
->left_margin
,
1974 conf
->paper_height
- conf
->top_margin
,
1975 &dest
, keywords
, idx
);
1976 if (ldata
== pdata
->last
)
1981 * If this is a contents entry, add leaders and a page
1984 if (pdata
->contents_entry
) {
1990 if (pdata
->contents_entry
== index_placeholder
) {
1991 num
= index_page
->number
;
1993 assert(pdata
->contents_entry
->private_data
);
1994 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1995 num
= target
->first
->page
->number
;
1999 wid
= paper_width_simple(pdata
, w
);
2002 render_string(pdata
->last
->page
,
2003 pdata
->fonts
[FONT_NORMAL
],
2004 pdata
->sizes
[FONT_NORMAL
],
2005 conf
->paper_width
- conf
->right_margin
- wid
,
2006 (conf
->paper_height
- conf
->top_margin
-
2007 pdata
->last
->ypos
), num
);
2009 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2010 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2011 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2012 render_string(pdata
->last
->page
,
2013 pdata
->fonts
[FONT_NORMAL
],
2014 pdata
->sizes
[FONT_NORMAL
],
2015 conf
->left_margin
+ x
,
2016 (conf
->paper_height
- conf
->top_margin
-
2017 pdata
->last
->ypos
), L
".");
2021 * Render any rectangle (chapter title underline or rule)
2022 * that goes with this paragraph.
2024 switch (pdata
->rect_type
) {
2025 case RECT_CHAPTER_UNDERLINE
:
2026 add_rect_to_page(pdata
->last
->page
,
2028 (conf
->paper_height
- conf
->top_margin
-
2030 conf
->chapter_underline_depth
),
2032 conf
->chapter_underline_thickness
);
2035 add_rect_to_page(pdata
->first
->page
,
2036 conf
->left_margin
+ pdata
->first
->xpos
,
2037 (conf
->paper_height
- conf
->top_margin
-
2039 pdata
->last
->line_height
),
2040 conf
->base_width
- pdata
->first
->xpos
,
2041 pdata
->last
->line_height
);
2043 default: /* placate gcc */
2048 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2050 para_data
*pdata
= mknew(para_data
);
2053 * For code paragraphs, I'm going to hack grievously and
2054 * pretend the three normal fonts are the three code paragraph
2057 pdata
->fonts
[FONT_NORMAL
] = conf
->cb
;
2058 pdata
->fonts
[FONT_EMPH
] = conf
->co
;
2059 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
2060 pdata
->sizes
[FONT_NORMAL
] =
2061 pdata
->sizes
[FONT_EMPH
] =
2062 pdata
->sizes
[FONT_CODE
] = 12;
2064 pdata
->first
= pdata
->last
= NULL
;
2065 pdata
->outline_level
= -1;
2066 pdata
->rect_type
= RECT_NONE
;
2067 pdata
->contents_entry
= NULL
;
2068 pdata
->justification
= LEFT
;
2070 for (; words
; words
= words
->next
) {
2071 wchar_t *t
, *e
, *start
;
2072 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2074 int prev
= -1, curr
;
2077 if (words
->next
&& words
->next
->type
== word_Emph
) {
2078 e
= words
->next
->text
;
2079 words
= words
->next
;
2089 else if (*e
== L
'i')
2091 else if (*e
== L
'b')
2108 * We've isolated a maximal subsequence of the line
2109 * which has the same emphasis. Form it into a word
2115 w
->type
= (prev
== 0 ? word_WeakCode
:
2116 prev
== 1 ? word_Emph
: word_Normal
);
2117 w
->text
= mknewa(wchar_t, t
-start
+1);
2118 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2119 w
->text
[t
-start
] = '\0';
2132 ldata
= mknew(line_data
);
2134 ldata
->pdata
= pdata
;
2135 ldata
->first
= lhead
;
2137 ldata
->line_height
= conf
->base_font_size
* 4096;
2139 ldata
->xpos
= indent
;
2142 pdata
->last
->next
= ldata
;
2143 ldata
->prev
= pdata
->last
;
2145 pdata
->first
= ldata
;
2149 pdata
->last
= ldata
;
2151 ldata
->hshortfall
= 0;
2153 ldata
->aux_text
= NULL
;
2154 ldata
->aux_text_2
= NULL
;
2155 ldata
->aux_left_indent
= 0;
2156 /* General opprobrium for breaking in a code paragraph. */
2157 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2160 standard_line_spacing(pdata
, conf
);
2165 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2167 para_data
*pdata
= mknew(para_data
);
2170 ldata
= mknew(line_data
);
2172 ldata
->pdata
= pdata
;
2173 ldata
->first
= NULL
;
2175 ldata
->line_height
= conf
->rule_thickness
;
2177 ldata
->xpos
= indent
;
2182 ldata
->hshortfall
= 0;
2184 ldata
->aux_text
= NULL
;
2185 ldata
->aux_text_2
= NULL
;
2186 ldata
->aux_left_indent
= 0;
2189 * Better to break after a rule than before it
2191 ldata
->penalty_after
+= 100000;
2192 ldata
->penalty_before
+= -100000;
2194 pdata
->first
= pdata
->last
= ldata
;
2195 pdata
->outline_level
= -1;
2196 pdata
->rect_type
= RECT_RULE
;
2197 pdata
->contents_entry
= NULL
;
2198 pdata
->justification
= LEFT
;
2200 standard_line_spacing(pdata
, conf
);
2206 * Plain-text-like formatting for outline titles.
2208 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2209 for (; text
; text
= text
->next
) switch (text
->type
) {
2210 case word_HyperLink
:
2212 case word_UpperXref
:
2213 case word_LowerXref
:
2222 case word_WhiteSpace
:
2223 case word_EmphSpace
:
2224 case word_CodeSpace
:
2225 case word_WkCodeSpace
:
2227 case word_EmphQuote
:
2228 case word_CodeQuote
:
2229 case word_WkCodeQuote
:
2230 assert(text
->type
!= word_CodeQuote
&&
2231 text
->type
!= word_WkCodeQuote
);
2232 if (towordstyle(text
->type
) == word_Emph
&&
2233 (attraux(text
->aux
) == attr_First
||
2234 attraux(text
->aux
) == attr_Only
))
2235 rdadd(rs
, L
'_'); /* FIXME: configurability */
2236 else if (towordstyle(text
->type
) == word_Code
&&
2237 (attraux(text
->aux
) == attr_First
||
2238 attraux(text
->aux
) == attr_Only
))
2239 rdadd(rs
, L
'\''); /* FIXME: configurability */
2240 if (removeattr(text
->type
) == word_Normal
) {
2241 rdadds(rs
, text
->text
);
2242 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2244 } else if (removeattr(text
->type
) == word_Quote
) {
2245 rdadd(rs
, L
'\''); /* fixme: configurability */
2247 if (towordstyle(text
->type
) == word_Emph
&&
2248 (attraux(text
->aux
) == attr_Last
||
2249 attraux(text
->aux
) == attr_Only
))
2250 rdadd(rs
, L
'_'); /* FIXME: configurability */
2251 else if (towordstyle(text
->type
) == word_Code
&&
2252 (attraux(text
->aux
) == attr_Last
||
2253 attraux(text
->aux
) == attr_Only
))
2254 rdadd(rs
, L
'\''); /* FIXME: configurability */
2259 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2262 rdstring rs
= {0, 0, NULL
};
2265 paper_rdaddw(&rs
, first
);
2267 rdadds(&rs
, separator
);
2269 paper_rdaddw(&rs
, second
);
2274 static word
*fake_word(wchar_t *text
)
2276 word
*ret
= mknew(word
);
2279 ret
->type
= word_Normal
;
2280 ret
->text
= ustrdup(text
);
2281 ret
->breaks
= FALSE
;
2286 static word
*fake_space_word(void)
2288 word
*ret
= mknew(word
);
2291 ret
->type
= word_WhiteSpace
;
2298 static word
*fake_page_ref(page_data
*page
)
2300 word
*ret
= mknew(word
);
2303 ret
->type
= word_PageXref
;
2305 ret
->breaks
= FALSE
;
2307 ret
->private_data
= page
;
2311 static word
*fake_end_ref(void)
2313 word
*ret
= mknew(word
);
2316 ret
->type
= word_XrefEnd
;
2318 ret
->breaks
= FALSE
;
2323 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2332 w
= dup_word_list(first
);
2340 w
= fake_word(separator
);
2346 *wptr
= dup_word_list(second
);
2352 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2356 if (!src
->first_line
)
2359 if (dest
->last_line
) {
2360 dest
->last_line
->next
= src
->first_line
;
2361 src
->first_line
->prev
= dest
->last_line
;
2363 dest
->last_line
= src
->last_line
;
2365 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2367 ldata
->xpos
+= right_shift
;
2369 if (ldata
== src
->last_line
)