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 * - compression of output files. For the actual text display,
18 * both output formats currently average about 50-60 characters
19 * per 5-6 character word of text, and almost all of it's the
21 * * In PS, we can define custom text operators to make things
23 * * In PDF, there already are!
25 * - I'm uncertain of whether I need to include a ToUnicode CMap
26 * in each of my font definitions in PDF. Currently things (by
27 * which I mean cut and paste out of acroread) seem to be
28 * working fairly happily without it, but I don't know.
30 * - rather than the ugly aux_text mechanism for rendering chapter
31 * titles, we could actually build the correct word list and
34 * - get vertical font metrics and use them to position the PDF
35 * xref boxes more pleasantly
38 * * all the measurements in `conf' should be configurable
39 * + notably paper size/shape
40 * * page header and footer should be configurable; we should
41 * be able to shift the page number elsewhere, and add other
42 * things such as the current chapter/section title and fixed
44 * * remove the fixed mapping from heading levels to heading
45 * styles; offer a menu of styles from which the user can
46 * choose at every heading level
47 * * first-line indent in paragraphs
48 * * fixed text: `Contents', `Index', bullet, quotes, the
49 * colon-space and full stop in chapter title constructions
50 * * configurable location of contents?
51 * * certainly configurably _remove_ the contents, and possibly
53 * * double-sided document switch?
54 * + means you have two header/footer formats which
56 * + and means that mandatory page breaks before chapter
57 * titles should include a blank page if necessary to
58 * start the next section to a right-hand page
62 * - ability to import other Type 1 fonts
63 * * we need to parse the font to extract its metrics
64 * * then we pass the font bodily to both PS and PDF so it can
65 * be included in the output file
74 typedef struct paper_conf_Tag paper_conf
;
75 typedef struct paper_idx_Tag paper_idx
;
77 struct paper_conf_Tag
{
84 int indent_list_bullet
;
88 int base_para_spacing
;
89 int chapter_top_space
;
90 int sect_num_left_space
;
91 int chapter_underline_depth
;
92 int chapter_underline_thickness
;
95 int contents_indent_step
;
97 int leader_separation
;
101 int pagenum_fontsize
;
103 /* These are derived from the above */
107 /* Fonts used in the configuration */
108 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
111 struct paper_idx_Tag
{
113 * Word list giving the page numbers on which this index entry
114 * appears. Also the last word in the list, for ease of
120 * The last page added to the list (so we can ensure we don't
127 word_PageXref
= word_NotWordType
+ 1
130 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
131 static void wrap_paragraph(para_data
*pdata
, word
*words
,
132 int w
, int i1
, int i2
);
133 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
134 int page_height
, int ncols
, int headspace
);
135 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
136 int x
, int y
, wchar_t *str
);
137 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
138 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
);
139 static void render_para(para_data
*pdata
, paper_conf
*conf
,
140 keywordlist
*keywords
, indexdata
*idx
,
141 paragraph
*index_placeholder
, page_data
*index_page
);
142 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
143 static int paper_width_simple(para_data
*pdata
, word
*text
);
144 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
145 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
146 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
147 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
148 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
150 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
151 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
153 static word
*fake_word(wchar_t *text
);
154 static word
*fake_space_word(void);
155 static word
*fake_page_ref(page_data
*page
);
156 static word
*fake_end_ref(void);
157 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
159 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
161 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
165 int indent
, used_contents
;
166 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
167 para_data
*firstcont
, *lastcont
;
168 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
174 paragraph index_placeholder_para
;
175 page_data
*first_index_page
;
178 * FIXME: All these things ought to become configurable.
180 conf
= mknew(paper_conf
);
181 conf
->paper_width
= 595 * 4096;
182 conf
->paper_height
= 841 * 4096;
183 conf
->left_margin
= 72 * 4096;
184 conf
->top_margin
= 72 * 4096;
185 conf
->right_margin
= 72 * 4096;
186 conf
->bottom_margin
= 108 * 4096;
187 conf
->indent_list_bullet
= 6 * 4096;
188 conf
->indent_list
= 24 * 4096;
189 conf
->indent_quote
= 18 * 4096;
190 conf
->base_leading
= 4096;
191 conf
->base_para_spacing
= 10 * 4096;
192 conf
->chapter_top_space
= 72 * 4096;
193 conf
->sect_num_left_space
= 12 * 4096;
194 conf
->chapter_underline_depth
= 14 * 4096;
195 conf
->chapter_underline_thickness
= 3 * 4096;
196 conf
->rule_thickness
= 1 * 4096;
197 conf
->base_font_size
= 12;
198 conf
->contents_indent_step
= 24 * 4096;
199 conf
->contents_margin
= 84 * 4096;
200 conf
->leader_separation
= 12 * 4096;
201 conf
->index_gutter
= 36 * 4096;
202 conf
->index_cols
= 2;
203 conf
->index_minsep
= 18 * 4096;
204 conf
->pagenum_fontsize
= 12;
205 conf
->footer_distance
= 32 * 4096;
208 conf
->paper_width
- conf
->left_margin
- conf
->right_margin
;
210 conf
->paper_height
- conf
->top_margin
- conf
->bottom_margin
;
211 conf
->index_colwidth
=
212 (conf
->base_width
- (conf
->index_cols
-1) * conf
->index_gutter
)
216 * First, set up some font structures.
218 fontlist
= mknew(font_list
);
219 fontlist
->head
= fontlist
->tail
= NULL
;
220 conf
->tr
= make_std_font(fontlist
, "Times-Roman");
221 conf
->ti
= make_std_font(fontlist
, "Times-Italic");
222 conf
->hr
= make_std_font(fontlist
, "Helvetica-Bold");
223 conf
->hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
224 conf
->cr
= make_std_font(fontlist
, "Courier");
225 conf
->co
= make_std_font(fontlist
, "Courier-Oblique");
226 conf
->cb
= make_std_font(fontlist
, "Courier-Bold");
229 * Set up a data structure to collect page numbers for each
238 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
239 paper_idx
*pi
= mknew(paper_idx
);
243 pi
->words
= pi
->lastword
= NULL
;
246 entry
->backend_data
= pi
;
251 * Format the contents entry for each heading.
254 word
*contents_title
;
255 contents_title
= fake_word(L
"Contents");
257 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
258 NULL
, NULL
, contents_title
, conf
);
259 lastcont
= firstcont
;
260 lastcont
->next
= NULL
;
261 firstcontline
= firstcont
->first
;
262 lastcontline
= lastcont
->last
;
263 for (p
= sourceform
; p
; p
= p
->next
) {
270 case para_UnnumberedChapter
:
276 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
279 case para_UnnumberedChapter
:
280 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
285 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
286 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
289 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
290 conf
->contents_margin
,
291 NULL
, NULL
, words
, conf
);
293 pdata
->contents_entry
= p
;
294 lastcont
->next
= pdata
;
298 * Link all contents line structures together into
303 lastcontline
->next
= pdata
->first
;
304 pdata
->first
->prev
= lastcontline
;
306 firstcontline
= pdata
->first
;
307 pdata
->first
->prev
= NULL
;
309 lastcontline
= pdata
->last
;
310 lastcontline
->next
= NULL
;
318 * And one extra one, for the index.
321 pdata
= make_para_data(para_Normal
, 0, 0,
322 conf
->contents_margin
,
323 NULL
, NULL
, fake_word(L
"Index"), conf
);
325 pdata
->contents_entry
= &index_placeholder_para
;
326 lastcont
->next
= pdata
;
331 lastcontline
->next
= pdata
->first
;
332 pdata
->first
->prev
= lastcontline
;
334 firstcontline
= pdata
->first
;
335 pdata
->first
->prev
= NULL
;
337 lastcontline
= pdata
->last
;
338 lastcontline
->next
= NULL
;
344 * Do the main paragraph formatting.
347 used_contents
= FALSE
;
348 firstline
= lastline
= NULL
;
349 for (p
= sourceform
; p
; p
= p
->next
) {
350 p
->private_data
= NULL
;
354 * These paragraph types are either invisible or don't
355 * define text in the normal sense. Either way, they
356 * don't require wrapping.
361 case para_NotParaType
:
368 * These paragraph types don't require wrapping, but
369 * they do affect the line width to which we wrap the
370 * rest of the paragraphs, so we need to pay attention.
373 indent
+= conf
->indent_list
; break;
375 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
377 indent
+= conf
->indent_quote
; break;
379 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
382 * This paragraph type is special. Process it
386 pdata
= code_paragraph(indent
, p
->words
, conf
);
387 p
->private_data
= pdata
;
388 if (pdata
->first
!= pdata
->last
) {
389 pdata
->first
->penalty_after
+= 100000;
390 pdata
->last
->penalty_before
+= 100000;
395 * This paragraph is also special.
398 pdata
= rule_paragraph(indent
, conf
);
399 p
->private_data
= pdata
;
403 * All of these paragraph types require wrapping in the
404 * ordinary way. So we must supply a set of fonts, a
405 * line width and auxiliary information (e.g. bullet
406 * text) for each one.
410 case para_UnnumberedChapter
:
414 case para_BiblioCited
:
416 case para_NumberedList
:
417 case para_DescribedThing
:
418 case para_Description
:
421 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
422 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
424 p
->private_data
= pdata
;
429 if (p
->private_data
) {
430 pdata
= (para_data
*)p
->private_data
;
433 * If this is the first non-title heading, we link the
434 * contents section in before it.
436 if (!used_contents
&& pdata
->outline_level
> 0) {
437 used_contents
= TRUE
;
439 lastpara
->next
= firstcont
;
441 firstpara
= firstcont
;
443 assert(lastpara
->next
== NULL
);
446 lastline
->next
= firstcontline
;
447 firstcontline
->prev
= lastline
;
449 firstline
= firstcontline
;
450 firstcontline
->prev
= NULL
;
452 assert(lastcontline
!= NULL
);
453 lastline
= lastcontline
;
454 lastline
->next
= NULL
;
458 * Link all line structures together into a big list.
462 lastline
->next
= pdata
->first
;
463 pdata
->first
->prev
= lastline
;
465 firstline
= pdata
->first
;
466 pdata
->first
->prev
= NULL
;
468 lastline
= pdata
->last
;
469 lastline
->next
= NULL
;
473 * Link all paragraph structures together similarly.
477 lastpara
->next
= pdata
;
485 * Now we have an enormous linked list of every line of text in
486 * the document. Break it up into pages.
488 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
499 for (page
= pages
; page
; page
= page
->next
) {
500 sprintf(buf
, "%d", ++pagenum
);
501 page
->number
= ufroma_dup(buf
);
505 first_index_page
= mknew(page_data
);
506 first_index_page
->next
= first_index_page
->prev
= NULL
;
507 first_index_page
->first_line
= NULL
;
508 first_index_page
->last_line
= NULL
;
509 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
510 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
511 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
513 /* And don't forget the as-yet-uncreated index. */
514 sprintf(buf
, "%d", ++pagenum
);
515 first_index_page
->number
= ufroma_dup(buf
);
520 * Now we're ready to actually lay out the pages. We do this by
521 * looping over _paragraphs_, since we may need to track cross-
522 * references between lines and even across pages.
524 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
525 render_para(pdata
, conf
, keywords
, idx
,
526 &index_placeholder_para
, first_index_page
);
529 * Now we've laid out the main body pages, we should have
530 * acquired a full set of page numbers for the index.
536 para_data
*firstidx
, *lastidx
;
537 line_data
*firstidxline
, *lastidxline
, *ldata
;
538 page_data
*ipages
, *ipages2
, *page
;
541 * Create a set of paragraphs for the index.
543 index_title
= fake_word(L
"Index");
545 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
546 NULL
, NULL
, index_title
, conf
);
548 lastidx
->next
= NULL
;
549 firstidxline
= firstidx
->first
;
550 lastidxline
= lastidx
->last
;
551 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
552 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
553 para_data
*text
, *pages
;
555 text
= make_para_data(para_Normal
, 0, 0,
556 conf
->base_width
- conf
->index_colwidth
,
557 NULL
, NULL
, entry
->text
, conf
);
559 pages
= make_para_data(para_Normal
, 0, 0,
560 conf
->base_width
- conf
->index_colwidth
,
561 NULL
, NULL
, pi
->words
, conf
);
563 text
->justification
= LEFT
;
564 pages
->justification
= RIGHT
;
565 text
->last
->space_after
= pages
->first
->space_before
=
566 conf
->base_leading
/ 2;
568 pages
->last
->space_after
= text
->first
->space_before
=
572 assert(pages
->first
);
577 * If feasible, fold the two halves of the index entry
580 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
581 conf
->index_colwidth
+ conf
->index_minsep
) {
582 text
->last
->space_after
= -1;
583 pages
->first
->space_before
= -pages
->first
->line_height
+1;
586 lastidx
->next
= text
;
592 * Link all index line structures together into
595 text
->last
->next
= pages
->first
;
596 pages
->first
->prev
= text
->last
;
598 lastidxline
->next
= text
->first
;
599 text
->first
->prev
= lastidxline
;
601 lastidxline
= pages
->last
;
604 * Breaking an index entry anywhere is so bad that I
605 * think I'm going to forbid it totally.
607 for (ldata
= text
->first
; ldata
&& ldata
->next
;
608 ldata
= ldata
->next
) {
609 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
610 ldata
->space_after
= -1;
615 * Now break the index into pages.
617 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
619 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
622 firstidxline
->space_before
+
623 firstidxline
->line_height
+
624 firstidxline
->space_after
);
627 * This will have put each _column_ of the index on a
628 * separate page, which isn't what we want. Fold the pages
635 for (i
= 1; i
< conf
->index_cols
; i
++)
639 fold_into_page(page
, page
->next
,
640 i
* (conf
->index_colwidth
+
641 conf
->index_gutter
));
643 page
->next
= page
->next
->next
;
645 page
->next
->prev
= page
;
651 /* Also fold the heading on to the same page as the index items. */
652 fold_into_page(ipages
, ipages2
, 0);
653 ipages
->next
= ipages2
->next
;
655 ipages
->next
->prev
= ipages
;
657 fold_into_page(first_index_page
, ipages
, 0);
658 first_index_page
->next
= ipages
->next
;
659 if (first_index_page
->next
)
660 first_index_page
->next
->prev
= first_index_page
;
662 ipages
= first_index_page
;
665 * Number the index pages, except the already-numbered
668 for (page
= ipages
->next
; page
; page
= page
->next
) {
670 sprintf(buf
, "%d", ++pagenum
);
671 page
->number
= ufroma_dup(buf
);
675 * Render the index pages.
677 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
678 render_para(pdata
, conf
, keywords
, idx
,
679 &index_placeholder_para
, first_index_page
);
682 * Link the index page list on to the end of the main page
688 for (page
= pages
; page
->next
; page
= page
->next
);
693 * Same with the paragraph list, which will cause the index
694 * to be mentioned in the document outline.
697 firstpara
= firstidx
;
699 lastpara
->next
= firstidx
;
704 * Draw the headers and footers.
706 * FIXME: this should be fully configurable, but for the moment
707 * I'm just going to put in page numbers in the centre of a
708 * footer and leave it at that.
713 for (page
= pages
; page
; page
= page
->next
) {
716 width
= conf
->pagenum_fontsize
*
717 string_width(conf
->tr
, page
->number
, NULL
);
719 render_string(page
, conf
->tr
, conf
->pagenum_fontsize
,
720 conf
->left_margin
+ (conf
->base_width
- width
)/2,
721 conf
->bottom_margin
- conf
->footer_distance
,
727 * Start putting together the overall document structure we're
730 doc
= mknew(document
);
731 doc
->fonts
= fontlist
;
733 doc
->paper_width
= conf
->paper_width
;
734 doc
->paper_height
= conf
->paper_height
;
737 * Collect the section heading paragraphs into a document
738 * outline. This is slightly fiddly because the Title paragraph
739 * isn't required to be at the start, although all the others
745 doc
->outline_elements
= mknewa(outline_element
, osize
);
746 doc
->n_outline_elements
= 0;
748 /* First find the title. */
749 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
750 if (pdata
->outline_level
== 0) {
751 doc
->outline_elements
[0].level
= 0;
752 doc
->outline_elements
[0].pdata
= pdata
;
753 doc
->n_outline_elements
++;
758 /* Then collect the rest. */
759 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
760 if (pdata
->outline_level
> 0) {
761 if (doc
->n_outline_elements
>= osize
) {
763 doc
->outline_elements
=
764 resize(doc
->outline_elements
, osize
);
767 doc
->outline_elements
[doc
->n_outline_elements
].level
=
768 pdata
->outline_level
;
769 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
770 doc
->n_outline_elements
++;
780 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
781 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
786 int extra_indent
, firstline_indent
, aux_indent
;
789 pdata
= mknew(para_data
);
790 pdata
->outline_level
= -1;
791 pdata
->outline_title
= NULL
;
792 pdata
->rect_type
= RECT_NONE
;
793 pdata
->contents_entry
= NULL
;
794 pdata
->justification
= JUST
;
797 * Choose fonts for this paragraph.
799 * FIXME: All of this ought to be completely
804 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
805 pdata
->sizes
[FONT_NORMAL
] = 24;
806 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
807 pdata
->sizes
[FONT_EMPH
] = 24;
808 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
809 pdata
->sizes
[FONT_CODE
] = 24;
810 pdata
->outline_level
= 0;
815 case para_UnnumberedChapter
:
816 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
817 pdata
->sizes
[FONT_NORMAL
] = 20;
818 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
819 pdata
->sizes
[FONT_EMPH
] = 20;
820 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
821 pdata
->sizes
[FONT_CODE
] = 20;
822 pdata
->outline_level
= 1;
827 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
828 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
829 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
830 pdata
->sizes
[FONT_NORMAL
] =
831 pdata
->sizes
[FONT_EMPH
] =
832 pdata
->sizes
[FONT_CODE
] =
833 (paux
== 0 ?
16 : paux
== 1 ?
14 : 13);
834 pdata
->outline_level
= 2 + paux
;
838 case para_BiblioCited
:
840 case para_NumberedList
:
841 case para_DescribedThing
:
842 case para_Description
:
844 pdata
->fonts
[FONT_NORMAL
] = conf
->tr
;
845 pdata
->sizes
[FONT_NORMAL
] = 12;
846 pdata
->fonts
[FONT_EMPH
] = conf
->ti
;
847 pdata
->sizes
[FONT_EMPH
] = 12;
848 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
849 pdata
->sizes
[FONT_CODE
] = 12;
854 * Also select an indentation level depending on the
855 * paragraph type (list paragraphs other than
856 * para_DescribedThing need extra indent).
858 * (FIXME: Perhaps at some point we might even arrange
859 * for the user to be able to request indented first
860 * lines in paragraphs.)
862 if (ptype
== para_Bullet
||
863 ptype
== para_NumberedList
||
864 ptype
== para_Description
) {
865 extra_indent
= firstline_indent
= conf
->indent_list
;
867 extra_indent
= firstline_indent
= 0;
871 * Find the auxiliary text for this paragraph.
882 * For some heading styles (FIXME: be able to
883 * configure which), the auxiliary text contains
884 * the chapter number and is arranged to be
885 * right-aligned a few points left of the primary
886 * margin. For other styles, the auxiliary text is
887 * the full chapter _name_ and takes up space
888 * within the (wrapped) chapter title, meaning that
889 * we must move the first line indent over to make
892 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
896 len
= paper_width_simple(pdata
, pkwtext2
);
897 aux_indent
= -len
- conf
->sect_num_left_space
;
899 pdata
->outline_title
=
900 prepare_outline_title(pkwtext2
, L
" ", pwords
);
903 aux2
= fake_word(L
": ");
906 firstline_indent
+= paper_width_simple(pdata
, aux
);
907 firstline_indent
+= paper_width_simple(pdata
, aux2
);
909 pdata
->outline_title
=
910 prepare_outline_title(pkwtext
, L
": ", pwords
);
916 * Auxiliary text consisting of a bullet. (FIXME:
917 * configurable bullet.)
919 aux
= fake_word(L
"\x2022");
920 aux_indent
= indent
+ conf
->indent_list_bullet
;
923 case para_NumberedList
:
925 * Auxiliary text consisting of the number followed
926 * by a (FIXME: configurable) full stop.
929 aux2
= fake_word(L
".");
930 aux_indent
= indent
+ conf
->indent_list_bullet
;
933 case para_BiblioCited
:
935 * Auxiliary text consisting of the bibliography
936 * reference text, and a trailing space.
939 aux2
= fake_word(L
" ");
941 firstline_indent
+= paper_width_simple(pdata
, aux
);
942 firstline_indent
+= paper_width_simple(pdata
, aux2
);
946 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
947 pdata
->outline_title
=
948 prepare_outline_title(NULL
, NULL
, pwords
);
951 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
952 indent
+ firstline_indent
,
953 indent
+ extra_indent
);
955 pdata
->first
->aux_text
= aux
;
956 pdata
->first
->aux_text_2
= aux2
;
957 pdata
->first
->aux_left_indent
= aux_indent
;
960 * Line breaking penalties.
967 case para_UnnumberedChapter
:
969 * Fixed and large penalty for breaking straight
970 * after a heading; corresponding bonus for
971 * breaking straight before.
973 pdata
->first
->penalty_before
= -500000;
974 pdata
->last
->penalty_after
= 500000;
975 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
976 ldata
->penalty_after
= 500000;
979 case para_DescribedThing
:
981 * This is treated a bit like a small heading:
982 * there's a penalty for breaking after it (i.e.
983 * between it and its description), and a bonus for
984 * breaking before it (actually _between_ list
987 pdata
->first
->penalty_before
= -200000;
988 pdata
->last
->penalty_after
= 200000;
993 * Most paragraph types: widow/orphan control by
994 * discouraging breaking one line from the end of
997 if (pdata
->first
!= pdata
->last
) {
998 pdata
->first
->penalty_after
= 100000;
999 pdata
->last
->penalty_before
= 100000;
1004 standard_line_spacing(pdata
, conf
);
1007 * Some kinds of section heading require a page break before
1008 * them and an underline after.
1010 if (ptype
== para_Title
||
1011 ptype
== para_Chapter
||
1012 ptype
== para_Appendix
||
1013 ptype
== para_UnnumberedChapter
) {
1014 pdata
->first
->page_break
= TRUE
;
1015 pdata
->first
->space_before
= conf
->chapter_top_space
;
1016 pdata
->last
->space_after
+=
1017 (conf
->chapter_underline_depth
+
1018 conf
->chapter_underline_thickness
);
1019 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1025 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1030 * Set the line spacing for each line in this paragraph.
1032 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1033 if (ldata
== pdata
->first
)
1034 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1036 ldata
->space_before
= conf
->base_leading
/ 2;
1037 if (ldata
== pdata
->last
)
1038 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1040 ldata
->space_after
= conf
->base_leading
/ 2;
1041 ldata
->page_break
= FALSE
;
1045 static font_encoding
*new_font_encoding(font_data
*font
)
1050 fe
= mknew(font_encoding
);
1053 if (font
->list
->tail
)
1054 font
->list
->tail
->next
= fe
;
1056 font
->list
->head
= fe
;
1057 font
->list
->tail
= fe
;
1060 fe
->free_pos
= 0x21;
1062 for (i
= 0; i
< 256; i
++) {
1063 fe
->vector
[i
] = NULL
;
1064 fe
->indices
[i
] = -1;
1065 fe
->to_unicode
[i
] = 0xFFFF;
1071 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1079 widths
= ps_std_font_widths(name
);
1083 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
1085 f
= mknew(font_data
);
1089 f
->nglyphs
= nglyphs
;
1090 f
->glyphs
= ps_std_glyphs
;
1092 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
1095 * Our first subfont will contain all of US-ASCII. This isn't
1096 * really necessary - we could just create custom subfonts
1097 * precisely as the whim of render_string dictated - but
1098 * instinct suggests that it might be nice to have the text in
1099 * the output files look _marginally_ recognisable.
1101 fe
= new_font_encoding(f
);
1102 fe
->free_pos
= 0xA1; /* only the top half is free */
1103 f
->latest_subfont
= fe
;
1105 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
1108 for (i
= 0; i
< nglyphs
; i
++) {
1110 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
1111 assert(ucs
!= 0xFFFF);
1113 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1114 fe
->vector
[ucs
] = f
->glyphs
[i
];
1115 fe
->indices
[ucs
] = i
;
1116 fe
->to_unicode
[ucs
] = ucs
;
1117 f
->subfont_map
[i
].subfont
= fe
;
1118 f
->subfont_map
[i
].position
= ucs
;
1121 * This character is not yet assigned to a subfont.
1123 f
->subfont_map
[i
].subfont
= NULL
;
1124 f
->subfont_map
[i
].position
= 0;
1131 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1138 for (; *string
; string
++) {
1141 index
= font
->bmp
[(unsigned short)*string
];
1142 if (index
== 0xFFFF) {
1146 width
+= font
->widths
[index
];
1153 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1155 struct paper_width_ctx
{
1160 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1162 while (text
&& text
!= end
) {
1163 w
+= paper_width_internal(vctx
, text
, nspaces
);
1169 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1171 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1172 int style
, type
, findex
, width
, errs
;
1175 switch (word
->type
) {
1176 case word_HyperLink
:
1178 case word_UpperXref
:
1179 case word_LowerXref
:
1186 style
= towordstyle(word
->type
);
1187 type
= removeattr(word
->type
);
1189 findex
= (style
== word_Normal ? FONT_NORMAL
:
1190 style
== word_Emph ? FONT_EMPH
:
1193 if (type
== word_Normal
) {
1195 } else if (type
== word_WhiteSpace
) {
1196 if (findex
!= FONT_CODE
) {
1199 return ctx
->minspacewidth
;
1202 } else /* if (type == word_Quote) */ {
1203 if (word
->aux
== quote_Open
)
1204 str
= L
"\x2018"; /* FIXME: configurability! */
1206 str
= L
"\x2019"; /* FIXME: configurability! */
1209 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1211 if (errs
&& word
->alt
)
1212 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1214 return ctx
->pdata
->sizes
[findex
] * width
;
1217 static int paper_width(void *vctx
, word
*word
)
1219 return paper_width_internal(vctx
, word
, NULL
);
1222 static int paper_width_simple(para_data
*pdata
, word
*text
)
1224 struct paper_width_ctx ctx
;
1228 (pdata
->sizes
[FONT_NORMAL
] *
1229 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1231 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1234 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1235 int w
, int i1
, int i2
)
1237 wrappedline
*wrapping
, *p
;
1239 struct paper_width_ctx ctx
;
1243 * We're going to need to store the line height in every line
1244 * structure we generate.
1249 for (i
= 0; i
< NFONTS
; i
++)
1250 if (line_height
< pdata
->sizes
[i
])
1251 line_height
= pdata
->sizes
[i
];
1252 line_height
*= 4096;
1255 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1256 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1257 if (spacewidth
== 0) {
1259 * A font without a space?! Disturbing. I hope this never
1260 * comes up, but I'll make a random guess anyway and set my
1261 * space width to half the point size.
1263 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
1267 * I'm going to set the _minimum_ space width to 3/5 of the
1268 * standard one, and use the standard one as the optimum.
1270 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1273 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1276 * Having done the wrapping, we now concoct a set of line_data
1279 pdata
->first
= pdata
->last
= NULL
;
1281 for (p
= wrapping
; p
; p
= p
->next
) {
1284 int len
, wid
, spaces
;
1286 ldata
= mknew(line_data
);
1288 ldata
->pdata
= pdata
;
1289 ldata
->first
= p
->begin
;
1290 ldata
->end
= p
->end
;
1291 ldata
->line_height
= line_height
;
1293 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1296 pdata
->last
->next
= ldata
;
1297 ldata
->prev
= pdata
->last
;
1299 pdata
->first
= ldata
;
1303 pdata
->last
= ldata
;
1306 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1307 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1310 ldata
->hshortfall
= wid
- len
;
1311 ldata
->nspaces
= spaces
;
1313 * This tells us how much the space width needs to
1314 * change from _min_spacewidth. But we want to store
1315 * its difference from the _natural_ space width, to
1316 * make the text rendering easier.
1318 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1319 ldata
->hshortfall
-= spacewidth
* spaces
;
1320 ldata
->real_shortfall
= ldata
->hshortfall
;
1322 * Special case: on the last line of a paragraph, we
1323 * never stretch spaces.
1325 if (ldata
->hshortfall
> 0 && !p
->next
)
1326 ldata
->hshortfall
= 0;
1328 ldata
->aux_text
= NULL
;
1329 ldata
->aux_text_2
= NULL
;
1330 ldata
->aux_left_indent
= 0;
1331 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1336 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1337 int page_height
, int ncols
, int headspace
)
1341 int n
, n1
, this_height
;
1344 * Page breaking is done by a close analogue of the optimal
1345 * paragraph wrapping algorithm used by wrap_para(). We work
1346 * backwards from the end of the document line by line; for
1347 * each line, we contemplate every possible number of lines we
1348 * could put on a page starting with that line, determine a
1349 * cost function for each one, add it to the pre-computed cost
1350 * function for optimally page-breaking everything after that
1351 * page, and pick the best option.
1353 * This is made slightly more complex by the fact that we have
1354 * a multi-column index with a heading at the top of the
1355 * _first_ page, meaning that the first _ncols_ pages must have
1356 * a different length. Hence, we must do the wrapping ncols+1
1357 * times over, hypothetically trying to put every subsequence
1358 * on every possible page.
1360 * Since my line_data structures are only used for this
1361 * purpose, I might as well just store the algorithm data
1365 for (l
= last
; l
; l
= l
->prev
) {
1366 l
->bestcost
= mknewa(int, ncols
+1);
1367 l
->vshortfall
= mknewa(int, ncols
+1);
1368 l
->text
= mknewa(int, ncols
+1);
1369 l
->space
= mknewa(int, ncols
+1);
1370 l
->page_last
= mknewa(line_data
*, ncols
+1);
1372 for (n
= 0; n
<= ncols
; n
++) {
1373 int minheight
, text
= 0, space
= 0;
1376 n1
= (n
< ncols ? n
+1 : ncols
);
1378 this_height
= page_height
- headspace
;
1380 this_height
= page_height
;
1382 l
->bestcost
[n
] = -1;
1383 for (m
= l
; m
; m
= m
->next
) {
1384 if (m
!= l
&& m
->page_break
)
1385 break; /* we've gone as far as we can */
1388 if (m
->prev
->space_after
> 0)
1389 space
+= m
->prev
->space_after
;
1391 text
+= m
->prev
->space_after
;
1393 if (m
!= l
|| m
->page_break
) {
1394 if (m
->space_before
> 0)
1395 space
+= m
->space_before
;
1397 text
+= m
->space_before
;
1399 text
+= m
->line_height
;
1400 minheight
= text
+ space
;
1402 if (m
!= l
&& minheight
> this_height
)
1406 * If the space after this paragraph is _negative_
1407 * (which means the next line is folded on to this
1408 * one, which happens in the index), we absolutely
1409 * cannot break here.
1411 if (m
->space_after
>= 0) {
1414 * Compute the cost of this arrangement, as the
1415 * square of the amount of wasted space on the
1416 * page. Exception: if this is the last page
1417 * before a mandatory break or the document
1418 * end, we don't penalise a large blank area.
1420 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1422 int x
= this_height
- minheight
;
1429 cost
+= (x
* xf
) >> 8;
1433 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1434 cost
+= m
->penalty_after
;
1435 cost
+= m
->next
->penalty_before
;
1438 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1439 cost
+= m
->next
->bestcost
[n1
];
1440 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1442 * This is the best option yet for this
1445 l
->bestcost
[n
] = cost
;
1446 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1447 l
->vshortfall
[n
] = this_height
- minheight
;
1449 l
->vshortfall
[n
] = 0;
1451 l
->space
[n
] = space
;
1452 l
->page_last
[n
] = m
;
1463 * Now go through the line list forwards and assemble the
1472 int text
, space
, head
;
1474 page
= mknew(page_data
);
1483 page
->first_line
= l
;
1484 page
->last_line
= l
->page_last
[n
];
1486 page
->first_text
= page
->last_text
= NULL
;
1487 page
->first_xref
= page
->last_xref
= NULL
;
1488 page
->first_rect
= page
->last_rect
= NULL
;
1491 * Now assign a y-coordinate to each line on the page.
1494 head
= (n
< ncols ? headspace
: 0);
1495 for (l
= page
->first_line
; l
; l
= l
->next
) {
1496 if (l
!= page
->first_line
) {
1497 if (l
->prev
->space_after
> 0)
1498 space
+= l
->prev
->space_after
;
1500 text
+= l
->prev
->space_after
;
1502 if (l
!= page
->first_line
|| l
->page_break
) {
1503 if (l
->space_before
> 0)
1504 space
+= l
->space_before
;
1506 text
+= l
->space_before
;
1508 text
+= l
->line_height
;
1511 l
->ypos
= text
+ space
+ head
+
1512 space
* (float)page
->first_line
->vshortfall
[n
] /
1513 page
->first_line
->space
[n
];
1515 if (l
== page
->last_line
)
1519 l
= page
->last_line
;
1524 n
= (n
< ncols ? n
+1 : ncols
);
1530 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1532 rect
*r
= mknew(rect
);
1535 if (page
->last_rect
)
1536 page
->last_rect
->next
= r
;
1538 page
->first_rect
= r
;
1539 page
->last_rect
= r
;
1547 static void add_string_to_page(page_data
*page
, int x
, int y
,
1548 font_encoding
*fe
, int size
, char *text
)
1550 text_fragment
*frag
;
1552 frag
= mknew(text_fragment
);
1555 if (page
->last_text
)
1556 page
->last_text
->next
= frag
;
1558 page
->first_text
= frag
;
1559 page
->last_text
= frag
;
1564 frag
->fontsize
= size
;
1565 frag
->text
= dupstr(text
);
1569 * Returns the updated x coordinate.
1571 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1572 int x
, int y
, wchar_t *str
)
1575 int textpos
, textwid
, glyph
;
1576 font_encoding
*subfont
= NULL
, *sf
;
1578 text
= mknewa(char, 1 + ustrlen(str
));
1579 textpos
= textwid
= 0;
1582 glyph
= font
->bmp
[*str
];
1584 if (glyph
== 0xFFFF)
1585 continue; /* nothing more we can do here */
1588 * Find which subfont this character is going in.
1590 sf
= font
->subfont_map
[glyph
].subfont
;
1596 * This character is not yet in a subfont. Assign one.
1598 if (font
->latest_subfont
->free_pos
>= 0x100)
1599 font
->latest_subfont
= new_font_encoding(font
);
1601 c
= font
->latest_subfont
->free_pos
++;
1602 if (font
->latest_subfont
->free_pos
== 0x7F)
1603 font
->latest_subfont
->free_pos
= 0xA1;
1605 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1606 font
->subfont_map
[glyph
].position
= c
;
1607 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1608 font
->latest_subfont
->indices
[c
] = glyph
;
1609 font
->latest_subfont
->to_unicode
[c
] = *str
;
1611 sf
= font
->latest_subfont
;
1614 if (!subfont
|| sf
!= subfont
) {
1616 text
[textpos
] = '\0';
1617 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1620 assert(textpos
== 0);
1626 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1627 textwid
+= font
->widths
[glyph
] * fontsize
;
1633 text
[textpos
] = '\0';
1634 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1642 * Returns the updated x coordinate.
1644 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1645 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1646 int shortfall
, int nspaces
, int *nspace
,
1647 keywordlist
*keywords
, indexdata
*idx
)
1649 while (text
&& text
!= text_end
) {
1650 int style
, type
, findex
, errs
;
1654 switch (text
->type
) {
1656 * Start a cross-reference.
1658 case word_HyperLink
:
1659 case word_UpperXref
:
1660 case word_LowerXref
:
1663 if (text
->type
== word_HyperLink
) {
1665 dest
.url
= utoa_dup(text
->text
);
1667 } else if (text
->type
== word_PageXref
) {
1670 dest
.page
= (page_data
*)text
->private_data
;
1672 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1676 assert(kwl
->para
->private_data
);
1677 pdata
= (para_data
*) kwl
->para
->private_data
;
1679 dest
.page
= pdata
->first
->page
;
1683 * Shouldn't happen, but *shrug*
1690 if (dest
.type
!= NONE
) {
1692 (*xr
)->dest
= dest
; /* structure copy */
1693 if (page
->last_xref
)
1694 page
->last_xref
->next
= *xr
;
1696 page
->first_xref
= *xr
;
1697 page
->last_xref
= *xr
;
1701 * FIXME: Ideally we should have, and use, some
1702 * vertical font metric information here so that
1703 * our cross-ref rectangle can take account of
1704 * descenders and the font's cap height. This will
1705 * do for the moment, but it isn't ideal.
1707 (*xr
)->lx
= (*xr
)->rx
= x
;
1709 (*xr
)->ty
= y
+ ldata
->line_height
;
1714 * Finish extending a cross-reference box.
1722 * Add the current page number to the list of pages
1723 * referenced by an index entry.
1730 tag
= index_findtag(idx
, text
->text
);
1734 for (i
= 0; i
< tag
->nrefs
; i
++) {
1735 indexentry
*entry
= tag
->refs
[i
];
1736 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
1739 * If the same index term is indexed twice
1740 * within the same section, we only want to
1741 * mention it once in the index.
1743 if (pi
->lastpage
!= page
) {
1747 pi
->lastword
= pi
->lastword
->next
=
1749 pi
->lastword
= pi
->lastword
->next
=
1751 wp
= &pi
->lastword
->next
;
1755 pi
->lastword
= *wp
=
1756 fake_page_ref(page
);
1757 pi
->lastword
= pi
->lastword
->next
=
1758 fake_word(page
->number
);
1759 pi
->lastword
= pi
->lastword
->next
=
1763 pi
->lastpage
= page
;
1769 style
= towordstyle(text
->type
);
1770 type
= removeattr(text
->type
);
1772 findex
= (style
== word_Normal ? FONT_NORMAL
:
1773 style
== word_Emph ? FONT_EMPH
:
1776 if (type
== word_Normal
) {
1778 } else if (type
== word_WhiteSpace
) {
1779 x
+= pdata
->sizes
[findex
] *
1780 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1781 if (nspaces
&& findex
!= FONT_CODE
) {
1782 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1783 x
-= *nspace
* shortfall
/ nspaces
;
1787 } else /* if (type == word_Quote) */ {
1788 if (text
->aux
== quote_Open
)
1789 str
= L
"\x2018"; /* FIXME: configurability! */
1791 str
= L
"\x2019"; /* FIXME: configurability! */
1794 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1796 if (errs
&& text
->alt
)
1797 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1798 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
);
1800 x
= render_string(page
, pdata
->fonts
[findex
],
1801 pdata
->sizes
[findex
], x
, y
, str
);
1814 * Returns the last x position used on the line.
1816 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
1817 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
)
1823 if (ldata
->aux_text
) {
1827 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1828 left_x
+ ldata
->aux_left_indent
,
1829 top_y
- ldata
->ypos
,
1830 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
1832 if (ldata
->aux_text_2
)
1833 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1834 x
, top_y
- ldata
->ypos
,
1835 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
1842 * There might be a cross-reference carried over from a
1845 if (dest
->type
!= NONE
) {
1848 xr
->dest
= *dest
; /* structure copy */
1849 if (ldata
->page
->last_xref
)
1850 ldata
->page
->last_xref
->next
= xr
;
1852 ldata
->page
->first_xref
= xr
;
1853 ldata
->page
->last_xref
= xr
;
1854 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1855 xr
->by
= top_y
- ldata
->ypos
;
1856 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1861 int extra_indent
, shortfall
, spaces
;
1862 int just
= ldata
->pdata
->justification
;
1865 * All forms of justification become JUST when we have
1866 * to squeeze the paragraph.
1868 if (ldata
->hshortfall
< 0)
1873 shortfall
= ldata
->hshortfall
;
1874 spaces
= ldata
->nspaces
;
1878 shortfall
= spaces
= extra_indent
= 0;
1881 shortfall
= spaces
= 0;
1882 extra_indent
= ldata
->real_shortfall
;
1886 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1887 left_x
+ ldata
->xpos
+ extra_indent
,
1888 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
1889 &xr
, shortfall
, spaces
, &nspace
,
1895 * There's a cross-reference continued on to the next line.
1905 static void render_para(para_data
*pdata
, paper_conf
*conf
,
1906 keywordlist
*keywords
, indexdata
*idx
,
1907 paragraph
*index_placeholder
, page_data
*index_page
)
1911 page_data
*cxref_page
;
1920 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1922 * If this is a contents entry, we expect to have a single
1923 * enormous cross-reference rectangle covering the whole
1924 * thing. (Unless, of course, it spans multiple pages.)
1926 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
1927 cxref_page
= ldata
->page
;
1928 cxref
= mknew(xref
);
1930 cxref
->dest
.type
= PAGE
;
1931 if (pdata
->contents_entry
== index_placeholder
) {
1932 cxref
->dest
.page
= index_page
;
1934 assert(pdata
->contents_entry
->private_data
);
1935 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1936 cxref
->dest
.page
= target
->first
->page
;
1938 cxref
->dest
.url
= NULL
;
1939 if (ldata
->page
->last_xref
)
1940 ldata
->page
->last_xref
->next
= cxref
;
1942 ldata
->page
->first_xref
= cxref
;
1943 ldata
->page
->last_xref
= cxref
;
1944 cxref
->lx
= conf
->left_margin
;
1945 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
1946 cxref
->ty
= conf
->paper_height
- conf
->top_margin
1947 - ldata
->ypos
+ ldata
->line_height
;
1949 if (pdata
->contents_entry
) {
1950 assert(cxref
!= NULL
);
1951 cxref
->by
= conf
->paper_height
- conf
->top_margin
1955 last_x
= render_line(ldata
, conf
->left_margin
,
1956 conf
->paper_height
- conf
->top_margin
,
1957 &dest
, keywords
, idx
);
1958 if (ldata
== pdata
->last
)
1963 * If this is a contents entry, add leaders and a page
1966 if (pdata
->contents_entry
) {
1972 if (pdata
->contents_entry
== index_placeholder
) {
1973 num
= index_page
->number
;
1975 assert(pdata
->contents_entry
->private_data
);
1976 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1977 num
= target
->first
->page
->number
;
1981 wid
= paper_width_simple(pdata
, w
);
1984 render_string(pdata
->last
->page
,
1985 pdata
->fonts
[FONT_NORMAL
],
1986 pdata
->sizes
[FONT_NORMAL
],
1987 conf
->paper_width
- conf
->right_margin
- wid
,
1988 (conf
->paper_height
- conf
->top_margin
-
1989 pdata
->last
->ypos
), num
);
1991 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
1992 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
1993 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
1994 render_string(pdata
->last
->page
,
1995 pdata
->fonts
[FONT_NORMAL
],
1996 pdata
->sizes
[FONT_NORMAL
],
1997 conf
->left_margin
+ x
,
1998 (conf
->paper_height
- conf
->top_margin
-
1999 pdata
->last
->ypos
), L
".");
2003 * Render any rectangle (chapter title underline or rule)
2004 * that goes with this paragraph.
2006 switch (pdata
->rect_type
) {
2007 case RECT_CHAPTER_UNDERLINE
:
2008 add_rect_to_page(pdata
->last
->page
,
2010 (conf
->paper_height
- conf
->top_margin
-
2012 conf
->chapter_underline_depth
),
2014 conf
->chapter_underline_thickness
);
2017 add_rect_to_page(pdata
->first
->page
,
2018 conf
->left_margin
+ pdata
->first
->xpos
,
2019 (conf
->paper_height
- conf
->top_margin
-
2021 pdata
->last
->line_height
),
2022 conf
->base_width
- pdata
->first
->xpos
,
2023 pdata
->last
->line_height
);
2025 default: /* placate gcc */
2030 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2032 para_data
*pdata
= mknew(para_data
);
2035 * For code paragraphs, I'm going to hack grievously and
2036 * pretend the three normal fonts are the three code paragraph
2039 pdata
->fonts
[FONT_NORMAL
] = conf
->cb
;
2040 pdata
->fonts
[FONT_EMPH
] = conf
->co
;
2041 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
2042 pdata
->sizes
[FONT_NORMAL
] =
2043 pdata
->sizes
[FONT_EMPH
] =
2044 pdata
->sizes
[FONT_CODE
] = 12;
2046 pdata
->first
= pdata
->last
= NULL
;
2047 pdata
->outline_level
= -1;
2048 pdata
->rect_type
= RECT_NONE
;
2049 pdata
->contents_entry
= NULL
;
2050 pdata
->justification
= LEFT
;
2052 for (; words
; words
= words
->next
) {
2053 wchar_t *t
, *e
, *start
;
2054 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2056 int prev
= -1, curr
;
2059 if (words
->next
&& words
->next
->type
== word_Emph
) {
2060 e
= words
->next
->text
;
2061 words
= words
->next
;
2071 else if (*e
== L
'i')
2073 else if (*e
== L
'b')
2090 * We've isolated a maximal subsequence of the line
2091 * which has the same emphasis. Form it into a word
2097 w
->type
= (prev
== 0 ? word_WeakCode
:
2098 prev
== 1 ? word_Emph
: word_Normal
);
2099 w
->text
= mknewa(wchar_t, t
-start
+1);
2100 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2101 w
->text
[t
-start
] = '\0';
2114 ldata
= mknew(line_data
);
2116 ldata
->pdata
= pdata
;
2117 ldata
->first
= lhead
;
2119 ldata
->line_height
= conf
->base_font_size
* 4096;
2121 ldata
->xpos
= indent
;
2124 pdata
->last
->next
= ldata
;
2125 ldata
->prev
= pdata
->last
;
2127 pdata
->first
= ldata
;
2131 pdata
->last
= ldata
;
2133 ldata
->hshortfall
= 0;
2135 ldata
->aux_text
= NULL
;
2136 ldata
->aux_text_2
= NULL
;
2137 ldata
->aux_left_indent
= 0;
2138 /* General opprobrium for breaking in a code paragraph. */
2139 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2142 standard_line_spacing(pdata
, conf
);
2147 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2149 para_data
*pdata
= mknew(para_data
);
2152 ldata
= mknew(line_data
);
2154 ldata
->pdata
= pdata
;
2155 ldata
->first
= NULL
;
2157 ldata
->line_height
= conf
->rule_thickness
;
2159 ldata
->xpos
= indent
;
2164 ldata
->hshortfall
= 0;
2166 ldata
->aux_text
= NULL
;
2167 ldata
->aux_text_2
= NULL
;
2168 ldata
->aux_left_indent
= 0;
2171 * Better to break after a rule than before it
2173 ldata
->penalty_after
+= 100000;
2174 ldata
->penalty_before
+= -100000;
2176 pdata
->first
= pdata
->last
= ldata
;
2177 pdata
->outline_level
= -1;
2178 pdata
->rect_type
= RECT_RULE
;
2179 pdata
->contents_entry
= NULL
;
2180 pdata
->justification
= LEFT
;
2182 standard_line_spacing(pdata
, conf
);
2188 * Plain-text-like formatting for outline titles.
2190 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2191 for (; text
; text
= text
->next
) switch (text
->type
) {
2192 case word_HyperLink
:
2194 case word_UpperXref
:
2195 case word_LowerXref
:
2204 case word_WhiteSpace
:
2205 case word_EmphSpace
:
2206 case word_CodeSpace
:
2207 case word_WkCodeSpace
:
2209 case word_EmphQuote
:
2210 case word_CodeQuote
:
2211 case word_WkCodeQuote
:
2212 assert(text
->type
!= word_CodeQuote
&&
2213 text
->type
!= word_WkCodeQuote
);
2214 if (towordstyle(text
->type
) == word_Emph
&&
2215 (attraux(text
->aux
) == attr_First
||
2216 attraux(text
->aux
) == attr_Only
))
2217 rdadd(rs
, L
'_'); /* FIXME: configurability */
2218 else if (towordstyle(text
->type
) == word_Code
&&
2219 (attraux(text
->aux
) == attr_First
||
2220 attraux(text
->aux
) == attr_Only
))
2221 rdadd(rs
, L
'\''); /* FIXME: configurability */
2222 if (removeattr(text
->type
) == word_Normal
) {
2223 rdadds(rs
, text
->text
);
2224 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2226 } else if (removeattr(text
->type
) == word_Quote
) {
2227 rdadd(rs
, L
'\''); /* fixme: configurability */
2229 if (towordstyle(text
->type
) == word_Emph
&&
2230 (attraux(text
->aux
) == attr_Last
||
2231 attraux(text
->aux
) == attr_Only
))
2232 rdadd(rs
, L
'_'); /* FIXME: configurability */
2233 else if (towordstyle(text
->type
) == word_Code
&&
2234 (attraux(text
->aux
) == attr_Last
||
2235 attraux(text
->aux
) == attr_Only
))
2236 rdadd(rs
, L
'\''); /* FIXME: configurability */
2241 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2244 rdstring rs
= {0, 0, NULL
};
2247 paper_rdaddw(&rs
, first
);
2249 rdadds(&rs
, separator
);
2251 paper_rdaddw(&rs
, second
);
2256 static word
*fake_word(wchar_t *text
)
2258 word
*ret
= mknew(word
);
2261 ret
->type
= word_Normal
;
2262 ret
->text
= ustrdup(text
);
2263 ret
->breaks
= FALSE
;
2268 static word
*fake_space_word(void)
2270 word
*ret
= mknew(word
);
2273 ret
->type
= word_WhiteSpace
;
2280 static word
*fake_page_ref(page_data
*page
)
2282 word
*ret
= mknew(word
);
2285 ret
->type
= word_PageXref
;
2287 ret
->breaks
= FALSE
;
2289 ret
->private_data
= page
;
2293 static word
*fake_end_ref(void)
2295 word
*ret
= mknew(word
);
2298 ret
->type
= word_XrefEnd
;
2300 ret
->breaks
= FALSE
;
2305 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2314 w
= dup_word_list(first
);
2322 w
= fake_word(separator
);
2328 *wptr
= dup_word_list(second
);
2334 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2338 if (!src
->first_line
)
2341 if (dest
->last_line
) {
2342 dest
->last_line
->next
= src
->first_line
;
2343 src
->first_line
->prev
= dest
->last_line
;
2345 dest
->last_line
= src
->last_line
;
2347 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2349 ldata
->xpos
+= right_shift
;
2351 if (ldata
== src
->last_line
)