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.
15 * - header/footer? Page numbers at least would be handy. Fully
16 * configurable footer can wait, though.
18 * That should bring us to the same level of functionality that
19 * original-Halibut had, and the same in PDF plus the obvious
20 * interactive navigation features. After that, in future work:
22 * - linearised PDF, perhaps?
24 * - I'm uncertain of whether I need to include a ToUnicode CMap
25 * in each of my font definitions in PDF. Currently things (by
26 * which I mean cut and paste out of acroread) seem to be
27 * working fairly happily without it, but I don't know.
40 typedef struct paper_conf_Tag paper_conf
;
41 typedef struct paper_idx_Tag paper_idx
;
43 struct paper_conf_Tag
{
50 int indent_list_bullet
;
54 int base_para_spacing
;
55 int chapter_top_space
;
56 int sect_num_left_space
;
57 int chapter_underline_depth
;
58 int chapter_underline_thickness
;
61 int contents_indent_step
;
63 int leader_separation
;
67 /* These are derived from the above */
71 /* Fonts used in the configuration */
72 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
75 struct paper_idx_Tag
{
77 * Word list giving the page numbers on which this index entry
78 * appears. Also the last word in the list, for ease of
84 * The last page added to the list (so we can ensure we don't
91 word_PageXref
= word_NotWordType
+ 1
94 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
95 static void wrap_paragraph(para_data
*pdata
, word
*words
,
96 int w
, int i1
, int i2
);
97 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
98 int page_height
, int ncols
, int headspace
);
99 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
100 int x
, int y
, wchar_t *str
);
101 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
102 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
);
103 static void render_para(para_data
*pdata
, paper_conf
*conf
,
104 keywordlist
*keywords
, indexdata
*idx
,
105 paragraph
*index_placeholder
, page_data
*index_page
);
106 static int paper_width_simple(para_data
*pdata
, word
*text
);
107 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
108 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
109 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
110 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
111 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
113 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
114 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
116 static word
*fake_word(wchar_t *text
);
117 static word
*fake_space_word(void);
118 static word
*fake_page_ref(page_data
*page
);
119 static word
*fake_end_ref(void);
120 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
122 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
124 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
128 int indent
, used_contents
;
129 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
130 para_data
*firstcont
, *lastcont
;
131 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
137 paragraph index_placeholder_para
;
138 page_data
*first_index_page
;
141 * FIXME: All these things ought to become configurable.
143 conf
= mknew(paper_conf
);
144 conf
->paper_width
= 595 * 4096;
145 conf
->paper_height
= 841 * 4096;
146 conf
->left_margin
= 72 * 4096;
147 conf
->top_margin
= 72 * 4096;
148 conf
->right_margin
= 72 * 4096;
149 conf
->bottom_margin
= 108 * 4096;
150 conf
->indent_list_bullet
= 6 * 4096;
151 conf
->indent_list
= 24 * 4096;
152 conf
->indent_quote
= 18 * 4096;
153 conf
->base_leading
= 4096;
154 conf
->base_para_spacing
= 10 * 4096;
155 conf
->chapter_top_space
= 72 * 4096;
156 conf
->sect_num_left_space
= 12 * 4096;
157 conf
->chapter_underline_depth
= 14 * 4096;
158 conf
->chapter_underline_thickness
= 3 * 4096;
159 conf
->rule_thickness
= 1 * 4096;
160 conf
->base_font_size
= 12;
161 conf
->contents_indent_step
= 24 * 4096;
162 conf
->contents_margin
= 84 * 4096;
163 conf
->leader_separation
= 12 * 4096;
164 conf
->index_gutter
= 36 * 4096;
165 conf
->index_cols
= 2;
166 conf
->index_minsep
= 18 * 4096;
169 conf
->paper_width
- conf
->left_margin
- conf
->right_margin
;
171 conf
->paper_height
- conf
->top_margin
- conf
->bottom_margin
;
172 conf
->index_colwidth
=
173 (conf
->base_width
- (conf
->index_cols
-1) * conf
->index_gutter
)
176 IGNORE(idx
); /* FIXME */
179 * First, set up some font structures.
181 fontlist
= mknew(font_list
);
182 fontlist
->head
= fontlist
->tail
= NULL
;
183 conf
->tr
= make_std_font(fontlist
, "Times-Roman");
184 conf
->ti
= make_std_font(fontlist
, "Times-Italic");
185 conf
->hr
= make_std_font(fontlist
, "Helvetica-Bold");
186 conf
->hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
187 conf
->cr
= make_std_font(fontlist
, "Courier");
188 conf
->co
= make_std_font(fontlist
, "Courier-Oblique");
189 conf
->cb
= make_std_font(fontlist
, "Courier-Bold");
192 * Set up a data structure to collect page numbers for each
201 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
202 paper_idx
*pi
= mknew(paper_idx
);
206 pi
->words
= pi
->lastword
= NULL
;
209 entry
->backend_data
= pi
;
214 * Format the contents entry for each heading.
217 word
*contents_title
;
218 contents_title
= fake_word(L
"Contents");
220 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
221 NULL
, NULL
, contents_title
, conf
);
222 lastcont
= firstcont
;
223 lastcont
->next
= NULL
;
224 firstcontline
= firstcont
->first
;
225 lastcontline
= lastcont
->last
;
226 for (p
= sourceform
; p
; p
= p
->next
) {
233 case para_UnnumberedChapter
:
239 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
242 case para_UnnumberedChapter
:
243 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
248 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
249 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
252 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
253 conf
->contents_margin
,
254 NULL
, NULL
, words
, conf
);
256 pdata
->contents_entry
= p
;
257 lastcont
->next
= pdata
;
261 * Link all contents line structures together into
266 lastcontline
->next
= pdata
->first
;
267 pdata
->first
->prev
= lastcontline
;
269 firstcontline
= pdata
->first
;
270 pdata
->first
->prev
= NULL
;
272 lastcontline
= pdata
->last
;
273 lastcontline
->next
= NULL
;
281 * And one extra one, for the index.
284 pdata
= make_para_data(para_Normal
, 0, 0,
285 conf
->contents_margin
,
286 NULL
, NULL
, fake_word(L
"Index"), conf
);
288 pdata
->contents_entry
= &index_placeholder_para
;
289 lastcont
->next
= pdata
;
294 lastcontline
->next
= pdata
->first
;
295 pdata
->first
->prev
= lastcontline
;
297 firstcontline
= pdata
->first
;
298 pdata
->first
->prev
= NULL
;
300 lastcontline
= pdata
->last
;
301 lastcontline
->next
= NULL
;
307 * Do the main paragraph formatting.
310 used_contents
= FALSE
;
311 firstline
= lastline
= NULL
;
312 for (p
= sourceform
; p
; p
= p
->next
) {
313 p
->private_data
= NULL
;
317 * These paragraph types are either invisible or don't
318 * define text in the normal sense. Either way, they
319 * don't require wrapping.
324 case para_NotParaType
:
331 * These paragraph types don't require wrapping, but
332 * they do affect the line width to which we wrap the
333 * rest of the paragraphs, so we need to pay attention.
336 indent
+= conf
->indent_list
; break;
338 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
340 indent
+= conf
->indent_quote
; break;
342 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
345 * This paragraph type is special. Process it
349 pdata
= code_paragraph(indent
, p
->words
, conf
);
350 p
->private_data
= pdata
;
351 if (pdata
->first
!= pdata
->last
) {
352 pdata
->first
->penalty_after
+= 100000;
353 pdata
->last
->penalty_before
+= 100000;
358 * This paragraph is also special.
361 pdata
= rule_paragraph(indent
, conf
);
362 p
->private_data
= pdata
;
366 * All of these paragraph types require wrapping in the
367 * ordinary way. So we must supply a set of fonts, a
368 * line width and auxiliary information (e.g. bullet
369 * text) for each one.
373 case para_UnnumberedChapter
:
377 case para_BiblioCited
:
379 case para_NumberedList
:
380 case para_DescribedThing
:
381 case para_Description
:
384 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
385 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
387 p
->private_data
= pdata
;
392 if (p
->private_data
) {
393 pdata
= (para_data
*)p
->private_data
;
396 * If this is the first non-title heading, we link the
397 * contents section in before it.
399 if (!used_contents
&& pdata
->outline_level
> 0) {
400 used_contents
= TRUE
;
402 lastpara
->next
= firstcont
;
404 firstpara
= firstcont
;
406 assert(lastpara
->next
== NULL
);
409 lastline
->next
= firstcontline
;
410 firstcontline
->prev
= lastline
;
412 firstline
= firstcontline
;
413 firstcontline
->prev
= NULL
;
415 assert(lastcontline
!= NULL
);
416 lastline
= lastcontline
;
417 lastline
->next
= NULL
;
421 * Link all line structures together into a big list.
425 lastline
->next
= pdata
->first
;
426 pdata
->first
->prev
= lastline
;
428 firstline
= pdata
->first
;
429 pdata
->first
->prev
= NULL
;
431 lastline
= pdata
->last
;
432 lastline
->next
= NULL
;
436 * Link all paragraph structures together similarly.
440 lastpara
->next
= pdata
;
448 * Now we have an enormous linked list of every line of text in
449 * the document. Break it up into pages.
451 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
462 for (page
= pages
; page
; page
= page
->next
) {
463 sprintf(buf
, "%d", ++pagenum
);
464 page
->number
= ufroma_dup(buf
);
468 first_index_page
= mknew(page_data
);
469 first_index_page
->next
= first_index_page
->prev
= NULL
;
470 first_index_page
->first_line
= NULL
;
471 first_index_page
->last_line
= NULL
;
472 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
473 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
474 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
476 /* And don't forget the as-yet-uncreated index. */
477 sprintf(buf
, "%d", ++pagenum
);
478 first_index_page
->number
= ufroma_dup(buf
);
483 * Now we're ready to actually lay out the pages. We do this by
484 * looping over _paragraphs_, since we may need to track cross-
485 * references between lines and even across pages.
487 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
488 render_para(pdata
, conf
, keywords
, idx
,
489 &index_placeholder_para
, first_index_page
);
492 * Now we've laid out the main body pages, we should have
493 * acquired a full set of page numbers for the index.
499 para_data
*firstidx
, *lastidx
;
500 line_data
*firstidxline
, *lastidxline
, *ldata
;
501 page_data
*ipages
, *ipages2
, *page
;
504 * Create a set of paragraphs for the index.
506 index_title
= fake_word(L
"Index");
508 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
509 NULL
, NULL
, index_title
, conf
);
511 lastidx
->next
= NULL
;
512 firstidxline
= firstidx
->first
;
513 lastidxline
= lastidx
->last
;
514 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
515 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
516 para_data
*text
, *pages
;
518 text
= make_para_data(para_Normal
, 0, 0,
519 conf
->base_width
- conf
->index_colwidth
,
520 NULL
, NULL
, entry
->text
, conf
);
522 pages
= make_para_data(para_Normal
, 0, 0,
523 conf
->base_width
- conf
->index_colwidth
,
524 NULL
, NULL
, pi
->words
, conf
);
526 text
->justification
= LEFT
;
527 pages
->justification
= RIGHT
;
528 text
->last
->space_after
= pages
->first
->space_before
=
529 conf
->base_leading
/ 2;
531 pages
->last
->space_after
= text
->first
->space_before
=
535 assert(pages
->first
);
540 * If feasible, fold the two halves of the index entry
543 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
544 conf
->index_colwidth
+ conf
->index_minsep
) {
545 text
->last
->space_after
= -1;
546 pages
->first
->space_before
= -pages
->first
->line_height
+1;
549 lastidx
->next
= text
;
555 * Link all index line structures together into
558 text
->last
->next
= pages
->first
;
559 pages
->first
->prev
= text
->last
;
561 lastidxline
->next
= text
->first
;
562 text
->first
->prev
= lastidxline
;
564 lastidxline
= pages
->last
;
567 * Breaking an index entry anywhere is so bad that I
568 * think I'm going to forbid it totally.
570 for (ldata
= text
->first
; ldata
&& ldata
->next
;
571 ldata
= ldata
->next
) {
572 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
573 ldata
->space_after
= -1;
578 * Now break the index into pages.
580 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
582 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
585 firstidxline
->space_before
+
586 firstidxline
->line_height
+
587 firstidxline
->space_after
);
590 * This will have put each _column_ of the index on a
591 * separate page, which isn't what we want. Fold the pages
598 for (i
= 1; i
< conf
->index_cols
; i
++)
602 fold_into_page(page
, page
->next
,
603 i
* (conf
->index_colwidth
+
604 conf
->index_gutter
));
606 page
->next
= page
->next
->next
;
608 page
->next
->prev
= page
;
614 /* Also fold the heading on to the same page as the index items. */
615 fold_into_page(ipages
, ipages2
, 0);
616 ipages
->next
= ipages2
->next
;
618 ipages
->next
->prev
= ipages
;
620 fold_into_page(first_index_page
, ipages
, 0);
621 first_index_page
->next
= ipages
->next
;
622 if (first_index_page
->next
)
623 first_index_page
->next
->prev
= first_index_page
;
625 ipages
= first_index_page
;
628 * Number the index pages, except the already-numbered
631 for (page
= ipages
->next
; page
; page
= page
->next
) {
633 sprintf(buf
, "%d", ++pagenum
);
634 page
->number
= ufroma_dup(buf
);
638 * Render the index pages.
640 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
641 render_para(pdata
, conf
, keywords
, idx
,
642 &index_placeholder_para
, first_index_page
);
645 * Link the index page list on to the end of the main page
651 for (page
= pages
; page
->next
; page
= page
->next
);
656 * Same with the paragraph list, which will cause the index
657 * to be mentioned in the document outline.
660 firstpara
= firstidx
;
662 lastpara
->next
= firstidx
;
667 * Start putting together the overall document structure we're
670 doc
= mknew(document
);
671 doc
->fonts
= fontlist
;
673 doc
->paper_width
= conf
->paper_width
;
674 doc
->paper_height
= conf
->paper_height
;
677 * Collect the section heading paragraphs into a document
678 * outline. This is slightly fiddly because the Title paragraph
679 * isn't required to be at the start, although all the others
685 doc
->outline_elements
= mknewa(outline_element
, osize
);
686 doc
->n_outline_elements
= 0;
688 /* First find the title. */
689 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
690 if (pdata
->outline_level
== 0) {
691 doc
->outline_elements
[0].level
= 0;
692 doc
->outline_elements
[0].pdata
= pdata
;
693 doc
->n_outline_elements
++;
698 /* Then collect the rest. */
699 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
700 if (pdata
->outline_level
> 0) {
701 if (doc
->n_outline_elements
>= osize
) {
703 doc
->outline_elements
=
704 resize(doc
->outline_elements
, osize
);
707 doc
->outline_elements
[doc
->n_outline_elements
].level
=
708 pdata
->outline_level
;
709 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
710 doc
->n_outline_elements
++;
720 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
721 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
726 int extra_indent
, firstline_indent
, aux_indent
;
729 pdata
= mknew(para_data
);
730 pdata
->outline_level
= -1;
731 pdata
->outline_title
= NULL
;
732 pdata
->rect_type
= RECT_NONE
;
733 pdata
->contents_entry
= NULL
;
734 pdata
->justification
= JUST
;
737 * Choose fonts for this paragraph.
739 * FIXME: All of this ought to be completely
744 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
745 pdata
->sizes
[FONT_NORMAL
] = 24;
746 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
747 pdata
->sizes
[FONT_EMPH
] = 24;
748 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
749 pdata
->sizes
[FONT_CODE
] = 24;
750 pdata
->outline_level
= 0;
755 case para_UnnumberedChapter
:
756 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
757 pdata
->sizes
[FONT_NORMAL
] = 20;
758 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
759 pdata
->sizes
[FONT_EMPH
] = 20;
760 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
761 pdata
->sizes
[FONT_CODE
] = 20;
762 pdata
->outline_level
= 1;
767 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
768 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
769 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
770 pdata
->sizes
[FONT_NORMAL
] =
771 pdata
->sizes
[FONT_EMPH
] =
772 pdata
->sizes
[FONT_CODE
] =
773 (paux
== 0 ?
16 : paux
== 1 ?
14 : 13);
774 pdata
->outline_level
= 2 + paux
;
778 case para_BiblioCited
:
780 case para_NumberedList
:
781 case para_DescribedThing
:
782 case para_Description
:
784 pdata
->fonts
[FONT_NORMAL
] = conf
->tr
;
785 pdata
->sizes
[FONT_NORMAL
] = 12;
786 pdata
->fonts
[FONT_EMPH
] = conf
->ti
;
787 pdata
->sizes
[FONT_EMPH
] = 12;
788 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
789 pdata
->sizes
[FONT_CODE
] = 12;
794 * Also select an indentation level depending on the
795 * paragraph type (list paragraphs other than
796 * para_DescribedThing need extra indent).
798 * (FIXME: Perhaps at some point we might even arrange
799 * for the user to be able to request indented first
800 * lines in paragraphs.)
802 if (ptype
== para_Bullet
||
803 ptype
== para_NumberedList
||
804 ptype
== para_Description
) {
805 extra_indent
= firstline_indent
= conf
->indent_list
;
807 extra_indent
= firstline_indent
= 0;
811 * Find the auxiliary text for this paragraph.
822 * For some heading styles (FIXME: be able to
823 * configure which), the auxiliary text contains
824 * the chapter number and is arranged to be
825 * right-aligned a few points left of the primary
826 * margin. For other styles, the auxiliary text is
827 * the full chapter _name_ and takes up space
828 * within the (wrapped) chapter title, meaning that
829 * we must move the first line indent over to make
832 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
836 len
= paper_width_simple(pdata
, pkwtext2
);
837 aux_indent
= -len
- conf
->sect_num_left_space
;
839 pdata
->outline_title
=
840 prepare_outline_title(pkwtext2
, L
" ", pwords
);
843 aux2
= fake_word(L
": ");
846 firstline_indent
+= paper_width_simple(pdata
, aux
);
847 firstline_indent
+= paper_width_simple(pdata
, aux2
);
849 pdata
->outline_title
=
850 prepare_outline_title(pkwtext
, L
": ", pwords
);
856 * Auxiliary text consisting of a bullet. (FIXME:
857 * configurable bullet.)
859 aux
= fake_word(L
"\x2022");
860 aux_indent
= indent
+ conf
->indent_list_bullet
;
863 case para_NumberedList
:
865 * Auxiliary text consisting of the number followed
866 * by a (FIXME: configurable) full stop.
869 aux2
= fake_word(L
".");
870 aux_indent
= indent
+ conf
->indent_list_bullet
;
873 case para_BiblioCited
:
875 * Auxiliary text consisting of the bibliography
876 * reference text, and a trailing space.
879 aux2
= fake_word(L
" ");
881 firstline_indent
+= paper_width_simple(pdata
, aux
);
882 firstline_indent
+= paper_width_simple(pdata
, aux2
);
886 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
887 pdata
->outline_title
=
888 prepare_outline_title(NULL
, NULL
, pwords
);
891 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
892 indent
+ firstline_indent
,
893 indent
+ extra_indent
);
895 pdata
->first
->aux_text
= aux
;
896 pdata
->first
->aux_text_2
= aux2
;
897 pdata
->first
->aux_left_indent
= aux_indent
;
900 * Line breaking penalties.
907 case para_UnnumberedChapter
:
909 * Fixed and large penalty for breaking straight
910 * after a heading; corresponding bonus for
911 * breaking straight before.
913 pdata
->first
->penalty_before
= -500000;
914 pdata
->last
->penalty_after
= 500000;
915 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
916 ldata
->penalty_after
= 500000;
919 case para_DescribedThing
:
921 * This is treated a bit like a small heading:
922 * there's a penalty for breaking after it (i.e.
923 * between it and its description), and a bonus for
924 * breaking before it (actually _between_ list
927 pdata
->first
->penalty_before
= -200000;
928 pdata
->last
->penalty_after
= 200000;
933 * Most paragraph types: widow/orphan control by
934 * discouraging breaking one line from the end of
937 if (pdata
->first
!= pdata
->last
) {
938 pdata
->first
->penalty_after
= 100000;
939 pdata
->last
->penalty_before
= 100000;
944 standard_line_spacing(pdata
, conf
);
947 * Some kinds of section heading require a page break before
948 * them and an underline after.
950 if (ptype
== para_Title
||
951 ptype
== para_Chapter
||
952 ptype
== para_Appendix
||
953 ptype
== para_UnnumberedChapter
) {
954 pdata
->first
->page_break
= TRUE
;
955 pdata
->first
->space_before
= conf
->chapter_top_space
;
956 pdata
->last
->space_after
+=
957 (conf
->chapter_underline_depth
+
958 conf
->chapter_underline_thickness
);
959 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
965 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
970 * Set the line spacing for each line in this paragraph.
972 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
973 if (ldata
== pdata
->first
)
974 ldata
->space_before
= conf
->base_para_spacing
/ 2;
976 ldata
->space_before
= conf
->base_leading
/ 2;
977 if (ldata
== pdata
->last
)
978 ldata
->space_after
= conf
->base_para_spacing
/ 2;
980 ldata
->space_after
= conf
->base_leading
/ 2;
981 ldata
->page_break
= FALSE
;
985 static font_encoding
*new_font_encoding(font_data
*font
)
990 fe
= mknew(font_encoding
);
993 if (font
->list
->tail
)
994 font
->list
->tail
->next
= fe
;
996 font
->list
->head
= fe
;
997 font
->list
->tail
= fe
;
1000 fe
->free_pos
= 0x21;
1002 for (i
= 0; i
< 256; i
++) {
1003 fe
->vector
[i
] = NULL
;
1004 fe
->indices
[i
] = -1;
1005 fe
->to_unicode
[i
] = 0xFFFF;
1011 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1019 widths
= ps_std_font_widths(name
);
1023 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
1025 f
= mknew(font_data
);
1029 f
->nglyphs
= nglyphs
;
1030 f
->glyphs
= ps_std_glyphs
;
1032 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
1035 * Our first subfont will contain all of US-ASCII. This isn't
1036 * really necessary - we could just create custom subfonts
1037 * precisely as the whim of render_string dictated - but
1038 * instinct suggests that it might be nice to have the text in
1039 * the output files look _marginally_ recognisable.
1041 fe
= new_font_encoding(f
);
1042 fe
->free_pos
= 0xA1; /* only the top half is free */
1043 f
->latest_subfont
= fe
;
1045 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
1048 for (i
= 0; i
< nglyphs
; i
++) {
1050 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
1051 assert(ucs
!= 0xFFFF);
1053 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1054 fe
->vector
[ucs
] = f
->glyphs
[i
];
1055 fe
->indices
[ucs
] = i
;
1056 fe
->to_unicode
[ucs
] = ucs
;
1057 f
->subfont_map
[i
].subfont
= fe
;
1058 f
->subfont_map
[i
].position
= ucs
;
1061 * This character is not yet assigned to a subfont.
1063 f
->subfont_map
[i
].subfont
= NULL
;
1064 f
->subfont_map
[i
].position
= 0;
1071 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1078 for (; *string
; string
++) {
1081 index
= font
->bmp
[(unsigned short)*string
];
1082 if (index
== 0xFFFF) {
1086 width
+= font
->widths
[index
];
1093 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1095 struct paper_width_ctx
{
1100 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1102 while (text
&& text
!= end
) {
1103 w
+= paper_width_internal(vctx
, text
, nspaces
);
1109 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1111 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1112 int style
, type
, findex
, width
, errs
;
1115 switch (word
->type
) {
1116 case word_HyperLink
:
1118 case word_UpperXref
:
1119 case word_LowerXref
:
1126 style
= towordstyle(word
->type
);
1127 type
= removeattr(word
->type
);
1129 findex
= (style
== word_Normal ? FONT_NORMAL
:
1130 style
== word_Emph ? FONT_EMPH
:
1133 if (type
== word_Normal
) {
1135 } else if (type
== word_WhiteSpace
) {
1136 if (findex
!= FONT_CODE
) {
1139 return ctx
->minspacewidth
;
1142 } else /* if (type == word_Quote) */ {
1143 if (word
->aux
== quote_Open
)
1144 str
= L
"\x2018"; /* FIXME: configurability! */
1146 str
= L
"\x2019"; /* FIXME: configurability! */
1149 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1151 if (errs
&& word
->alt
)
1152 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1154 return ctx
->pdata
->sizes
[findex
] * width
;
1157 static int paper_width(void *vctx
, word
*word
)
1159 return paper_width_internal(vctx
, word
, NULL
);
1162 static int paper_width_simple(para_data
*pdata
, word
*text
)
1164 struct paper_width_ctx ctx
;
1168 (pdata
->sizes
[FONT_NORMAL
] *
1169 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1171 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1174 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1175 int w
, int i1
, int i2
)
1177 wrappedline
*wrapping
, *p
;
1179 struct paper_width_ctx ctx
;
1183 * We're going to need to store the line height in every line
1184 * structure we generate.
1189 for (i
= 0; i
< NFONTS
; i
++)
1190 if (line_height
< pdata
->sizes
[i
])
1191 line_height
= pdata
->sizes
[i
];
1192 line_height
*= 4096;
1195 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1196 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1197 if (spacewidth
== 0) {
1199 * A font without a space?! Disturbing. I hope this never
1200 * comes up, but I'll make a random guess anyway and set my
1201 * space width to half the point size.
1203 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
1207 * I'm going to set the _minimum_ space width to 3/5 of the
1208 * standard one, and use the standard one as the optimum.
1210 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1213 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1216 * Having done the wrapping, we now concoct a set of line_data
1219 pdata
->first
= pdata
->last
= NULL
;
1221 for (p
= wrapping
; p
; p
= p
->next
) {
1224 int len
, wid
, spaces
;
1226 ldata
= mknew(line_data
);
1228 ldata
->pdata
= pdata
;
1229 ldata
->first
= p
->begin
;
1230 ldata
->end
= p
->end
;
1231 ldata
->line_height
= line_height
;
1233 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1236 pdata
->last
->next
= ldata
;
1237 ldata
->prev
= pdata
->last
;
1239 pdata
->first
= ldata
;
1243 pdata
->last
= ldata
;
1246 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1247 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1250 ldata
->hshortfall
= wid
- len
;
1251 ldata
->nspaces
= spaces
;
1253 * This tells us how much the space width needs to
1254 * change from _min_spacewidth. But we want to store
1255 * its difference from the _natural_ space width, to
1256 * make the text rendering easier.
1258 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1259 ldata
->hshortfall
-= spacewidth
* spaces
;
1260 ldata
->real_shortfall
= ldata
->hshortfall
;
1262 * Special case: on the last line of a paragraph, we
1263 * never stretch spaces.
1265 if (ldata
->hshortfall
> 0 && !p
->next
)
1266 ldata
->hshortfall
= 0;
1268 ldata
->aux_text
= NULL
;
1269 ldata
->aux_text_2
= NULL
;
1270 ldata
->aux_left_indent
= 0;
1271 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1276 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1277 int page_height
, int ncols
, int headspace
)
1281 int n
, n1
, this_height
;
1284 * Page breaking is done by a close analogue of the optimal
1285 * paragraph wrapping algorithm used by wrap_para(). We work
1286 * backwards from the end of the document line by line; for
1287 * each line, we contemplate every possible number of lines we
1288 * could put on a page starting with that line, determine a
1289 * cost function for each one, add it to the pre-computed cost
1290 * function for optimally page-breaking everything after that
1291 * page, and pick the best option.
1293 * This is made slightly more complex by the fact that we have
1294 * a multi-column index with a heading at the top of the
1295 * _first_ page, meaning that the first _ncols_ pages must have
1296 * a different length. Hence, we must do the wrapping ncols+1
1297 * times over, hypothetically trying to put every subsequence
1298 * on every possible page.
1300 * Since my line_data structures are only used for this
1301 * purpose, I might as well just store the algorithm data
1305 for (l
= last
; l
; l
= l
->prev
) {
1306 l
->bestcost
= mknewa(int, ncols
+1);
1307 l
->vshortfall
= mknewa(int, ncols
+1);
1308 l
->text
= mknewa(int, ncols
+1);
1309 l
->space
= mknewa(int, ncols
+1);
1310 l
->page_last
= mknewa(line_data
*, ncols
+1);
1312 for (n
= 0; n
<= ncols
; n
++) {
1313 int minheight
, text
= 0, space
= 0;
1316 n1
= (n
< ncols ? n
+1 : ncols
);
1318 this_height
= page_height
- headspace
;
1320 this_height
= page_height
;
1322 l
->bestcost
[n
] = -1;
1323 for (m
= l
; m
; m
= m
->next
) {
1324 if (m
!= l
&& m
->page_break
)
1325 break; /* we've gone as far as we can */
1328 if (m
->prev
->space_after
> 0)
1329 space
+= m
->prev
->space_after
;
1331 text
+= m
->prev
->space_after
;
1333 if (m
!= l
|| m
->page_break
) {
1334 if (m
->space_before
> 0)
1335 space
+= m
->space_before
;
1337 text
+= m
->space_before
;
1339 text
+= m
->line_height
;
1340 minheight
= text
+ space
;
1342 if (m
!= l
&& minheight
> this_height
)
1346 * If the space after this paragraph is _negative_
1347 * (which means the next line is folded on to this
1348 * one, which happens in the index), we absolutely
1349 * cannot break here.
1351 if (m
->space_after
>= 0) {
1354 * Compute the cost of this arrangement, as the
1355 * square of the amount of wasted space on the
1356 * page. Exception: if this is the last page
1357 * before a mandatory break or the document
1358 * end, we don't penalise a large blank area.
1360 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1362 int x
= this_height
- minheight
;
1369 cost
+= (x
* xf
) >> 8;
1373 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1374 cost
+= m
->penalty_after
;
1375 cost
+= m
->next
->penalty_before
;
1378 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1379 cost
+= m
->next
->bestcost
[n1
];
1380 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1382 * This is the best option yet for this
1385 l
->bestcost
[n
] = cost
;
1386 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1387 l
->vshortfall
[n
] = this_height
- minheight
;
1389 l
->vshortfall
[n
] = 0;
1391 l
->space
[n
] = space
;
1392 l
->page_last
[n
] = m
;
1403 * Now go through the line list forwards and assemble the
1412 int text
, space
, head
;
1414 page
= mknew(page_data
);
1423 page
->first_line
= l
;
1424 page
->last_line
= l
->page_last
[n
];
1426 page
->first_text
= page
->last_text
= NULL
;
1427 page
->first_xref
= page
->last_xref
= NULL
;
1428 page
->first_rect
= page
->last_rect
= NULL
;
1431 * Now assign a y-coordinate to each line on the page.
1434 head
= (n
< ncols ? headspace
: 0);
1435 for (l
= page
->first_line
; l
; l
= l
->next
) {
1436 if (l
!= page
->first_line
) {
1437 if (l
->prev
->space_after
> 0)
1438 space
+= l
->prev
->space_after
;
1440 text
+= l
->prev
->space_after
;
1442 if (l
!= page
->first_line
|| l
->page_break
) {
1443 if (l
->space_before
> 0)
1444 space
+= l
->space_before
;
1446 text
+= l
->space_before
;
1448 text
+= l
->line_height
;
1451 l
->ypos
= text
+ space
+ head
+
1452 space
* (float)page
->first_line
->vshortfall
[n
] /
1453 page
->first_line
->space
[n
];
1455 if (l
== page
->last_line
)
1459 l
= page
->last_line
;
1464 n
= (n
< ncols ? n
+1 : ncols
);
1470 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1472 rect
*r
= mknew(rect
);
1475 if (page
->last_rect
)
1476 page
->last_rect
->next
= r
;
1478 page
->first_rect
= r
;
1479 page
->last_rect
= r
;
1487 static void add_string_to_page(page_data
*page
, int x
, int y
,
1488 font_encoding
*fe
, int size
, char *text
)
1490 text_fragment
*frag
;
1492 frag
= mknew(text_fragment
);
1495 if (page
->last_text
)
1496 page
->last_text
->next
= frag
;
1498 page
->first_text
= frag
;
1499 page
->last_text
= frag
;
1504 frag
->fontsize
= size
;
1505 frag
->text
= dupstr(text
);
1509 * Returns the updated x coordinate.
1511 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1512 int x
, int y
, wchar_t *str
)
1515 int textpos
, textwid
, glyph
;
1516 font_encoding
*subfont
= NULL
, *sf
;
1518 text
= mknewa(char, 1 + ustrlen(str
));
1519 textpos
= textwid
= 0;
1522 glyph
= font
->bmp
[*str
];
1524 if (glyph
== 0xFFFF)
1525 continue; /* nothing more we can do here */
1528 * Find which subfont this character is going in.
1530 sf
= font
->subfont_map
[glyph
].subfont
;
1536 * This character is not yet in a subfont. Assign one.
1538 if (font
->latest_subfont
->free_pos
>= 0x100)
1539 font
->latest_subfont
= new_font_encoding(font
);
1541 c
= font
->latest_subfont
->free_pos
++;
1542 if (font
->latest_subfont
->free_pos
== 0x7F)
1543 font
->latest_subfont
->free_pos
= 0xA1;
1545 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1546 font
->subfont_map
[glyph
].position
= c
;
1547 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1548 font
->latest_subfont
->indices
[c
] = glyph
;
1549 font
->latest_subfont
->to_unicode
[c
] = *str
;
1551 sf
= font
->latest_subfont
;
1554 if (!subfont
|| sf
!= subfont
) {
1556 text
[textpos
] = '\0';
1557 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1560 assert(textpos
== 0);
1566 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1567 textwid
+= font
->widths
[glyph
] * fontsize
;
1573 text
[textpos
] = '\0';
1574 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1582 * Returns the updated x coordinate.
1584 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1585 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1586 int shortfall
, int nspaces
, int *nspace
,
1587 keywordlist
*keywords
, indexdata
*idx
)
1589 while (text
&& text
!= text_end
) {
1590 int style
, type
, findex
, errs
;
1594 switch (text
->type
) {
1596 * Start a cross-reference.
1598 case word_HyperLink
:
1599 case word_UpperXref
:
1600 case word_LowerXref
:
1603 if (text
->type
== word_HyperLink
) {
1605 dest
.url
= utoa_dup(text
->text
);
1607 } else if (text
->type
== word_PageXref
) {
1610 dest
.page
= (page_data
*)text
->private_data
;
1612 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1616 assert(kwl
->para
->private_data
);
1617 pdata
= (para_data
*) kwl
->para
->private_data
;
1619 dest
.page
= pdata
->first
->page
;
1623 * Shouldn't happen, but *shrug*
1630 if (dest
.type
!= NONE
) {
1632 (*xr
)->dest
= dest
; /* structure copy */
1633 if (page
->last_xref
)
1634 page
->last_xref
->next
= *xr
;
1636 page
->first_xref
= *xr
;
1637 page
->last_xref
= *xr
;
1641 * FIXME: Ideally we should have, and use, some
1642 * vertical font metric information here so that
1643 * our cross-ref rectangle can take account of
1644 * descenders and the font's cap height. This will
1645 * do for the moment, but it isn't ideal.
1647 (*xr
)->lx
= (*xr
)->rx
= x
;
1649 (*xr
)->ty
= y
+ ldata
->line_height
;
1654 * Finish extending a cross-reference box.
1662 * Add the current page number to the list of pages
1663 * referenced by an index entry.
1670 tag
= index_findtag(idx
, text
->text
);
1674 for (i
= 0; i
< tag
->nrefs
; i
++) {
1675 indexentry
*entry
= tag
->refs
[i
];
1676 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
1679 * If the same index term is indexed twice
1680 * within the same section, we only want to
1681 * mention it once in the index.
1683 if (pi
->lastpage
!= page
) {
1687 pi
->lastword
= pi
->lastword
->next
=
1689 pi
->lastword
= pi
->lastword
->next
=
1691 wp
= &pi
->lastword
->next
;
1695 pi
->lastword
= *wp
=
1696 fake_page_ref(page
);
1697 pi
->lastword
= pi
->lastword
->next
=
1698 fake_word(page
->number
);
1699 pi
->lastword
= pi
->lastword
->next
=
1703 pi
->lastpage
= page
;
1709 style
= towordstyle(text
->type
);
1710 type
= removeattr(text
->type
);
1712 findex
= (style
== word_Normal ? FONT_NORMAL
:
1713 style
== word_Emph ? FONT_EMPH
:
1716 if (type
== word_Normal
) {
1718 } else if (type
== word_WhiteSpace
) {
1719 x
+= pdata
->sizes
[findex
] *
1720 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1721 if (nspaces
&& findex
!= FONT_CODE
) {
1722 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1723 x
-= *nspace
* shortfall
/ nspaces
;
1727 } else /* if (type == word_Quote) */ {
1728 if (text
->aux
== quote_Open
)
1729 str
= L
"\x2018"; /* FIXME: configurability! */
1731 str
= L
"\x2019"; /* FIXME: configurability! */
1734 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1736 if (errs
&& text
->alt
)
1737 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1738 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
);
1740 x
= render_string(page
, pdata
->fonts
[findex
],
1741 pdata
->sizes
[findex
], x
, y
, str
);
1754 * Returns the last x position used on the line.
1756 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
1757 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
)
1763 if (ldata
->aux_text
) {
1767 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1768 left_x
+ ldata
->aux_left_indent
,
1769 top_y
- ldata
->ypos
,
1770 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
1772 if (ldata
->aux_text_2
)
1773 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1774 x
, top_y
- ldata
->ypos
,
1775 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
1782 * There might be a cross-reference carried over from a
1785 if (dest
->type
!= NONE
) {
1788 xr
->dest
= *dest
; /* structure copy */
1789 if (ldata
->page
->last_xref
)
1790 ldata
->page
->last_xref
->next
= xr
;
1792 ldata
->page
->first_xref
= xr
;
1793 ldata
->page
->last_xref
= xr
;
1794 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1795 xr
->by
= top_y
- ldata
->ypos
;
1796 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1801 int extra_indent
, shortfall
, spaces
;
1802 int just
= ldata
->pdata
->justification
;
1805 * All forms of justification become JUST when we have
1806 * to squeeze the paragraph.
1808 if (ldata
->hshortfall
< 0)
1813 shortfall
= ldata
->hshortfall
;
1814 spaces
= ldata
->nspaces
;
1818 shortfall
= spaces
= extra_indent
= 0;
1821 shortfall
= spaces
= 0;
1822 extra_indent
= ldata
->real_shortfall
;
1826 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1827 left_x
+ ldata
->xpos
+ extra_indent
,
1828 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
1829 &xr
, shortfall
, spaces
, &nspace
,
1835 * There's a cross-reference continued on to the next line.
1845 static void render_para(para_data
*pdata
, paper_conf
*conf
,
1846 keywordlist
*keywords
, indexdata
*idx
,
1847 paragraph
*index_placeholder
, page_data
*index_page
)
1851 page_data
*cxref_page
;
1860 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1862 * If this is a contents entry, we expect to have a single
1863 * enormous cross-reference rectangle covering the whole
1864 * thing. (Unless, of course, it spans multiple pages.)
1866 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
1867 cxref_page
= ldata
->page
;
1868 cxref
= mknew(xref
);
1870 cxref
->dest
.type
= PAGE
;
1871 if (pdata
->contents_entry
== index_placeholder
) {
1872 cxref
->dest
.page
= index_page
;
1874 assert(pdata
->contents_entry
->private_data
);
1875 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1876 cxref
->dest
.page
= target
->first
->page
;
1878 cxref
->dest
.url
= NULL
;
1879 if (ldata
->page
->last_xref
)
1880 ldata
->page
->last_xref
->next
= cxref
;
1882 ldata
->page
->first_xref
= cxref
;
1883 ldata
->page
->last_xref
= cxref
;
1884 cxref
->lx
= conf
->left_margin
;
1885 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
1886 cxref
->ty
= conf
->paper_height
- conf
->top_margin
1887 - ldata
->ypos
+ ldata
->line_height
;
1889 if (pdata
->contents_entry
) {
1890 assert(cxref
!= NULL
);
1891 cxref
->by
= conf
->paper_height
- conf
->top_margin
1895 last_x
= render_line(ldata
, conf
->left_margin
,
1896 conf
->paper_height
- conf
->top_margin
,
1897 &dest
, keywords
, idx
);
1898 if (ldata
== pdata
->last
)
1903 * If this is a contents entry, add leaders and a page
1906 if (pdata
->contents_entry
) {
1912 if (pdata
->contents_entry
== index_placeholder
) {
1913 num
= index_page
->number
;
1915 assert(pdata
->contents_entry
->private_data
);
1916 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1917 num
= target
->first
->page
->number
;
1921 wid
= paper_width_simple(pdata
, w
);
1924 render_string(pdata
->last
->page
,
1925 pdata
->fonts
[FONT_NORMAL
],
1926 pdata
->sizes
[FONT_NORMAL
],
1927 conf
->paper_width
- conf
->right_margin
- wid
,
1928 (conf
->paper_height
- conf
->top_margin
-
1929 pdata
->last
->ypos
), num
);
1931 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
1932 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
1933 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
1934 render_string(pdata
->last
->page
,
1935 pdata
->fonts
[FONT_NORMAL
],
1936 pdata
->sizes
[FONT_NORMAL
],
1937 conf
->left_margin
+ x
,
1938 (conf
->paper_height
- conf
->top_margin
-
1939 pdata
->last
->ypos
), L
".");
1943 * Render any rectangle (chapter title underline or rule)
1944 * that goes with this paragraph.
1946 switch (pdata
->rect_type
) {
1947 case RECT_CHAPTER_UNDERLINE
:
1948 add_rect_to_page(pdata
->last
->page
,
1950 (conf
->paper_height
- conf
->top_margin
-
1952 conf
->chapter_underline_depth
),
1954 conf
->chapter_underline_thickness
);
1957 add_rect_to_page(pdata
->first
->page
,
1958 conf
->left_margin
+ pdata
->first
->xpos
,
1959 (conf
->paper_height
- conf
->top_margin
-
1961 pdata
->last
->line_height
),
1962 conf
->base_width
- pdata
->first
->xpos
,
1963 pdata
->last
->line_height
);
1965 default: /* placate gcc */
1970 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
1972 para_data
*pdata
= mknew(para_data
);
1975 * For code paragraphs, I'm going to hack grievously and
1976 * pretend the three normal fonts are the three code paragraph
1979 pdata
->fonts
[FONT_NORMAL
] = conf
->cb
;
1980 pdata
->fonts
[FONT_EMPH
] = conf
->co
;
1981 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
1982 pdata
->sizes
[FONT_NORMAL
] =
1983 pdata
->sizes
[FONT_EMPH
] =
1984 pdata
->sizes
[FONT_CODE
] = 12;
1986 pdata
->first
= pdata
->last
= NULL
;
1987 pdata
->outline_level
= -1;
1988 pdata
->rect_type
= RECT_NONE
;
1989 pdata
->contents_entry
= NULL
;
1990 pdata
->justification
= LEFT
;
1992 for (; words
; words
= words
->next
) {
1993 wchar_t *t
, *e
, *start
;
1994 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1996 int prev
= -1, curr
;
1999 if (words
->next
&& words
->next
->type
== word_Emph
) {
2000 e
= words
->next
->text
;
2001 words
= words
->next
;
2011 else if (*e
== L
'i')
2013 else if (*e
== L
'b')
2030 * We've isolated a maximal subsequence of the line
2031 * which has the same emphasis. Form it into a word
2037 w
->type
= (prev
== 0 ? word_WeakCode
:
2038 prev
== 1 ? word_Emph
: word_Normal
);
2039 w
->text
= mknewa(wchar_t, t
-start
+1);
2040 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2041 w
->text
[t
-start
] = '\0';
2054 ldata
= mknew(line_data
);
2056 ldata
->pdata
= pdata
;
2057 ldata
->first
= lhead
;
2059 ldata
->line_height
= conf
->base_font_size
* 4096;
2061 ldata
->xpos
= indent
;
2064 pdata
->last
->next
= ldata
;
2065 ldata
->prev
= pdata
->last
;
2067 pdata
->first
= ldata
;
2071 pdata
->last
= ldata
;
2073 ldata
->hshortfall
= 0;
2075 ldata
->aux_text
= NULL
;
2076 ldata
->aux_text_2
= NULL
;
2077 ldata
->aux_left_indent
= 0;
2078 /* General opprobrium for breaking in a code paragraph. */
2079 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2082 standard_line_spacing(pdata
, conf
);
2087 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2089 para_data
*pdata
= mknew(para_data
);
2092 ldata
= mknew(line_data
);
2094 ldata
->pdata
= pdata
;
2095 ldata
->first
= NULL
;
2097 ldata
->line_height
= conf
->rule_thickness
;
2099 ldata
->xpos
= indent
;
2104 ldata
->hshortfall
= 0;
2106 ldata
->aux_text
= NULL
;
2107 ldata
->aux_text_2
= NULL
;
2108 ldata
->aux_left_indent
= 0;
2111 * Better to break after a rule than before it
2113 ldata
->penalty_after
+= 100000;
2114 ldata
->penalty_before
+= -100000;
2116 pdata
->first
= pdata
->last
= ldata
;
2117 pdata
->outline_level
= -1;
2118 pdata
->rect_type
= RECT_RULE
;
2119 pdata
->contents_entry
= NULL
;
2120 pdata
->justification
= LEFT
;
2122 standard_line_spacing(pdata
, conf
);
2128 * Plain-text-like formatting for outline titles.
2130 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2131 for (; text
; text
= text
->next
) switch (text
->type
) {
2132 case word_HyperLink
:
2134 case word_UpperXref
:
2135 case word_LowerXref
:
2144 case word_WhiteSpace
:
2145 case word_EmphSpace
:
2146 case word_CodeSpace
:
2147 case word_WkCodeSpace
:
2149 case word_EmphQuote
:
2150 case word_CodeQuote
:
2151 case word_WkCodeQuote
:
2152 assert(text
->type
!= word_CodeQuote
&&
2153 text
->type
!= word_WkCodeQuote
);
2154 if (towordstyle(text
->type
) == word_Emph
&&
2155 (attraux(text
->aux
) == attr_First
||
2156 attraux(text
->aux
) == attr_Only
))
2157 rdadd(rs
, L
'_'); /* FIXME: configurability */
2158 else if (towordstyle(text
->type
) == word_Code
&&
2159 (attraux(text
->aux
) == attr_First
||
2160 attraux(text
->aux
) == attr_Only
))
2161 rdadd(rs
, L
'\''); /* FIXME: configurability */
2162 if (removeattr(text
->type
) == word_Normal
) {
2163 rdadds(rs
, text
->text
);
2164 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2166 } else if (removeattr(text
->type
) == word_Quote
) {
2167 rdadd(rs
, L
'\''); /* fixme: configurability */
2169 if (towordstyle(text
->type
) == word_Emph
&&
2170 (attraux(text
->aux
) == attr_Last
||
2171 attraux(text
->aux
) == attr_Only
))
2172 rdadd(rs
, L
'_'); /* FIXME: configurability */
2173 else if (towordstyle(text
->type
) == word_Code
&&
2174 (attraux(text
->aux
) == attr_Last
||
2175 attraux(text
->aux
) == attr_Only
))
2176 rdadd(rs
, L
'\''); /* FIXME: configurability */
2181 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2184 rdstring rs
= {0, 0, NULL
};
2187 paper_rdaddw(&rs
, first
);
2189 rdadds(&rs
, separator
);
2191 paper_rdaddw(&rs
, second
);
2196 static word
*fake_word(wchar_t *text
)
2198 word
*ret
= mknew(word
);
2201 ret
->type
= word_Normal
;
2202 ret
->text
= ustrdup(text
);
2203 ret
->breaks
= FALSE
;
2208 static word
*fake_space_word(void)
2210 word
*ret
= mknew(word
);
2213 ret
->type
= word_WhiteSpace
;
2220 static word
*fake_page_ref(page_data
*page
)
2222 word
*ret
= mknew(word
);
2225 ret
->type
= word_PageXref
;
2227 ret
->breaks
= FALSE
;
2229 ret
->private_data
= page
;
2233 static word
*fake_end_ref(void)
2235 word
*ret
= mknew(word
);
2238 ret
->type
= word_XrefEnd
;
2240 ret
->breaks
= FALSE
;
2245 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2254 w
= dup_word_list(first
);
2262 w
= fake_word(separator
);
2268 *wptr
= dup_word_list(second
);
2274 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2278 if (!src
->first_line
)
2281 if (dest
->last_line
) {
2282 dest
->last_line
->next
= src
->first_line
;
2283 src
->first_line
->prev
= dest
->last_line
;
2285 dest
->last_line
= src
->last_line
;
2287 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2289 ldata
->xpos
+= right_shift
;
2291 if (ldata
== src
->last_line
)