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 * - set up contents section now we know what sections begin on
20 * - header/footer? Page numbers at least would be handy. Fully
21 * configurable footer can wait, though.
23 * That should bring us to the same level of functionality that
24 * original-Halibut had, and the same in PDF plus the obvious
25 * interactive navigation features. After that, in future work:
27 * - linearised PDF, perhaps?
29 * - I'm uncertain of whether I need to include a ToUnicode CMap
30 * in each of my font definitions in PDF. Currently things (by
31 * which I mean cut and paste out of acroread) seem to be
32 * working fairly happily without it, but I don't know.
45 typedef struct paper_conf_Tag paper_conf
;
47 struct paper_conf_Tag
{
54 int indent_list_bullet
;
58 int base_para_spacing
;
59 int chapter_top_space
;
60 int sect_num_left_space
;
61 int chapter_underline_depth
;
62 int chapter_underline_thickness
;
65 int contents_indent_step
;
67 int leader_separation
;
68 /* These are derived from the above */
71 /* Fonts used in the configuration */
72 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
75 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
76 static void wrap_paragraph(para_data
*pdata
, word
*words
,
77 int w
, int i1
, int i2
);
78 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
80 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
81 int x
, int y
, wchar_t *str
);
82 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
83 xref_dest
*dest
, keywordlist
*keywords
);
84 static int paper_width_simple(para_data
*pdata
, word
*text
);
85 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
86 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
87 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
88 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
89 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
91 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
92 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
94 static word
*fake_word(wchar_t *text
);
95 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
98 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
102 int indent
, used_contents
;
103 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
104 para_data
*firstcont
, *lastcont
;
105 line_data
*ldata
, *firstline
, *lastline
, *firstcontline
, *lastcontline
;
111 * FIXME: All these things ought to become configurable.
113 conf
= mknew(paper_conf
);
114 conf
->paper_width
= 595 * 4096;
115 conf
->paper_height
= 841 * 4096;
116 conf
->left_margin
= 72 * 4096;
117 conf
->top_margin
= 72 * 4096;
118 conf
->right_margin
= 72 * 4096;
119 conf
->bottom_margin
= 108 * 4096;
120 conf
->indent_list_bullet
= 6 * 4096;
121 conf
->indent_list
= 24 * 4096;
122 conf
->indent_quote
= 18 * 4096;
123 conf
->base_leading
= 4096;
124 conf
->base_para_spacing
= 10 * 4096;
125 conf
->chapter_top_space
= 72 * 4096;
126 conf
->sect_num_left_space
= 12 * 4096;
127 conf
->chapter_underline_depth
= 14 * 4096;
128 conf
->chapter_underline_thickness
= 3 * 4096;
129 conf
->rule_thickness
= 1 * 4096;
130 conf
->base_font_size
= 12;
131 conf
->contents_indent_step
= 24 * 4096;
132 conf
->contents_margin
= 84 * 4096;
133 conf
->leader_separation
= 12 * 4096;
136 conf
->paper_width
- conf
->left_margin
- conf
->right_margin
;
138 conf
->paper_height
- conf
->top_margin
- conf
->bottom_margin
;
140 IGNORE(idx
); /* FIXME */
143 * First, set up some font structures.
145 fontlist
= mknew(font_list
);
146 fontlist
->head
= fontlist
->tail
= NULL
;
147 conf
->tr
= make_std_font(fontlist
, "Times-Roman");
148 conf
->ti
= make_std_font(fontlist
, "Times-Italic");
149 conf
->hr
= make_std_font(fontlist
, "Helvetica-Bold");
150 conf
->hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
151 conf
->cr
= make_std_font(fontlist
, "Courier");
152 conf
->co
= make_std_font(fontlist
, "Courier-Oblique");
153 conf
->cb
= make_std_font(fontlist
, "Courier-Bold");
156 * Format the contents entry for each heading.
159 word
*contents_title
;
160 contents_title
= fake_word(L
"Contents");
162 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
163 NULL
, NULL
, contents_title
, conf
);
164 lastcont
= firstcont
;
165 lastcont
->next
= NULL
;
166 firstcontline
= firstcont
->first
;
167 lastcontline
= lastcont
->last
;
168 for (p
= sourceform
; p
; p
= p
->next
) {
175 case para_UnnumberedChapter
:
181 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
184 case para_UnnumberedChapter
:
185 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
190 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
191 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
194 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
195 conf
->contents_margin
,
196 NULL
, NULL
, words
, conf
);
198 pdata
->contents_entry
= p
;
199 lastcont
->next
= pdata
;
203 * Link all contents line structures together into
208 lastcontline
->next
= pdata
->first
;
209 pdata
->first
->prev
= lastcontline
;
211 firstcontline
= pdata
->first
;
212 pdata
->first
->prev
= NULL
;
214 lastcontline
= pdata
->last
;
215 lastcontline
->next
= NULL
;
224 * Do the main paragraph formatting.
227 used_contents
= FALSE
;
228 firstline
= lastline
= NULL
;
229 for (p
= sourceform
; p
; p
= p
->next
) {
230 p
->private_data
= NULL
;
234 * These paragraph types are either invisible or don't
235 * define text in the normal sense. Either way, they
236 * don't require wrapping.
241 case para_NotParaType
:
248 * These paragraph types don't require wrapping, but
249 * they do affect the line width to which we wrap the
250 * rest of the paragraphs, so we need to pay attention.
253 indent
+= conf
->indent_list
; break;
255 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
257 indent
+= conf
->indent_quote
; break;
259 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
262 * This paragraph type is special. Process it
266 pdata
= code_paragraph(indent
, p
->words
, conf
);
267 p
->private_data
= pdata
;
268 if (pdata
->first
!= pdata
->last
) {
269 pdata
->first
->penalty_after
+= 100000;
270 pdata
->last
->penalty_before
+= 100000;
275 * This paragraph is also special.
278 pdata
= rule_paragraph(indent
, conf
);
279 p
->private_data
= pdata
;
283 * All of these paragraph types require wrapping in the
284 * ordinary way. So we must supply a set of fonts, a
285 * line width and auxiliary information (e.g. bullet
286 * text) for each one.
290 case para_UnnumberedChapter
:
294 case para_BiblioCited
:
296 case para_NumberedList
:
297 case para_DescribedThing
:
298 case para_Description
:
301 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
302 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
304 p
->private_data
= pdata
;
309 if (p
->private_data
) {
310 pdata
= (para_data
*)p
->private_data
;
313 * If this is the first non-title heading, we link the
314 * contents section in before it.
316 if (!used_contents
&& pdata
->outline_level
> 0) {
317 used_contents
= TRUE
;
319 lastpara
->next
= firstcont
;
321 firstpara
= firstcont
;
323 assert(lastpara
->next
== NULL
);
326 lastline
->next
= firstcontline
;
327 firstcontline
->prev
= lastline
;
329 firstline
= firstcontline
;
330 firstcontline
->prev
= NULL
;
332 assert(lastcontline
!= NULL
);
333 lastline
= lastcontline
;
334 lastline
->next
= NULL
;
338 * Link all line structures together into a big list.
342 lastline
->next
= pdata
->first
;
343 pdata
->first
->prev
= lastline
;
345 firstline
= pdata
->first
;
346 pdata
->first
->prev
= NULL
;
348 lastline
= pdata
->last
;
349 lastline
->next
= NULL
;
353 * Link all paragraph structures together similarly.
357 lastpara
->next
= pdata
;
365 * Now we have an enormous linked list of every line of text in
366 * the document. Break it up into pages.
368 pages
= page_breaks(firstline
, lastline
, conf
->page_height
);
376 for (page
= pages
; page
; page
= page
->next
) {
378 sprintf(buf
, "%d", ++num
);
379 page
->number
= ufroma_dup(buf
);
384 * Now we're ready to actually lay out the pages. We do this by
385 * looping over _paragraphs_, since we may need to track cross-
386 * references between lines and even across pages.
388 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
391 page_data
*cxref_page
;
399 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
401 * If this is a contents entry, we expect to have a single
402 * enormous cross-reference rectangle covering the whole
403 * thing. (Unless, of course, it spans multiple pages.)
405 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
406 cxref_page
= ldata
->page
;
409 cxref
->dest
.type
= PAGE
;
410 assert(pdata
->contents_entry
->private_data
);
411 target
= (para_data
*)pdata
->contents_entry
->private_data
;
412 cxref
->dest
.page
= target
->first
->page
;
413 cxref
->dest
.url
= NULL
;
414 if (ldata
->page
->last_xref
)
415 ldata
->page
->last_xref
->next
= cxref
;
417 ldata
->page
->first_xref
= cxref
;
418 ldata
->page
->last_xref
= cxref
;
419 cxref
->lx
= conf
->left_margin
;
420 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
421 cxref
->ty
= conf
->paper_height
- conf
->top_margin
422 - ldata
->ypos
+ ldata
->line_height
;
424 if (pdata
->contents_entry
) {
425 assert(cxref
!= NULL
);
426 cxref
->by
= conf
->paper_height
- conf
->top_margin
430 last_x
= render_line(ldata
, conf
->left_margin
,
431 conf
->paper_height
- conf
->top_margin
,
433 if (ldata
== pdata
->last
)
438 * If this is a contents entry, add leaders and a page
441 if (pdata
->contents_entry
) {
447 assert(pdata
->contents_entry
->private_data
);
448 target
= (para_data
*)pdata
->contents_entry
->private_data
;
449 num
= target
->first
->page
->number
;
452 wid
= paper_width_simple(pdata
, w
);
455 render_string(pdata
->last
->page
,
456 pdata
->fonts
[FONT_NORMAL
],
457 pdata
->sizes
[FONT_NORMAL
],
458 conf
->paper_width
- conf
->right_margin
- wid
,
459 (conf
->paper_height
- conf
->top_margin
-
460 pdata
->last
->ypos
), num
);
462 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
463 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
464 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
465 render_string(pdata
->last
->page
,
466 pdata
->fonts
[FONT_NORMAL
],
467 pdata
->sizes
[FONT_NORMAL
],
468 conf
->left_margin
+ x
,
469 (conf
->paper_height
- conf
->top_margin
-
470 pdata
->last
->ypos
), L
".");
474 * Render any rectangle (chapter title underline or rule)
475 * that goes with this paragraph.
477 switch (pdata
->rect_type
) {
478 case RECT_CHAPTER_UNDERLINE
:
479 add_rect_to_page(pdata
->last
->page
,
481 (conf
->paper_height
- conf
->top_margin
-
483 conf
->chapter_underline_depth
),
485 conf
->chapter_underline_thickness
);
488 add_rect_to_page(pdata
->first
->page
,
489 conf
->left_margin
+ pdata
->first
->xpos
,
490 (conf
->paper_height
- conf
->top_margin
-
492 pdata
->last
->line_height
),
493 conf
->base_width
- pdata
->first
->xpos
,
494 pdata
->last
->line_height
);
496 default: /* placate gcc */
502 * Start putting together the overall document structure we're
505 doc
= mknew(document
);
506 doc
->fonts
= fontlist
;
508 doc
->paper_width
= conf
->paper_width
;
509 doc
->paper_height
= conf
->paper_height
;
512 * Collect the section heading paragraphs into a document
513 * outline. This is slightly fiddly because the Title paragraph
514 * isn't required to be at the start, although all the others
520 doc
->outline_elements
= mknewa(outline_element
, osize
);
521 doc
->n_outline_elements
= 0;
523 /* First find the title. */
524 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
525 if (pdata
->outline_level
== 0) {
526 doc
->outline_elements
[0].level
= 0;
527 doc
->outline_elements
[0].pdata
= pdata
;
528 doc
->n_outline_elements
++;
533 /* Then collect the rest. */
534 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
535 if (pdata
->outline_level
> 0) {
536 if (doc
->n_outline_elements
>= osize
) {
538 doc
->outline_elements
=
539 resize(doc
->outline_elements
, osize
);
542 doc
->outline_elements
[doc
->n_outline_elements
].level
=
543 pdata
->outline_level
;
544 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
545 doc
->n_outline_elements
++;
555 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
556 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
561 int extra_indent
, firstline_indent
, aux_indent
;
564 pdata
= mknew(para_data
);
565 pdata
->outline_level
= -1;
566 pdata
->outline_title
= NULL
;
567 pdata
->rect_type
= RECT_NONE
;
568 pdata
->contents_entry
= NULL
;
571 * Choose fonts for this paragraph.
573 * FIXME: All of this ought to be completely
578 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
579 pdata
->sizes
[FONT_NORMAL
] = 24;
580 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
581 pdata
->sizes
[FONT_EMPH
] = 24;
582 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
583 pdata
->sizes
[FONT_CODE
] = 24;
584 pdata
->outline_level
= 0;
589 case para_UnnumberedChapter
:
590 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
591 pdata
->sizes
[FONT_NORMAL
] = 20;
592 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
593 pdata
->sizes
[FONT_EMPH
] = 20;
594 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
595 pdata
->sizes
[FONT_CODE
] = 20;
596 pdata
->outline_level
= 1;
601 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
602 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
603 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
604 pdata
->sizes
[FONT_NORMAL
] =
605 pdata
->sizes
[FONT_EMPH
] =
606 pdata
->sizes
[FONT_CODE
] =
607 (paux
== 0 ?
16 : paux
== 1 ?
14 : 13);
608 pdata
->outline_level
= 2 + paux
;
612 case para_BiblioCited
:
614 case para_NumberedList
:
615 case para_DescribedThing
:
616 case para_Description
:
618 pdata
->fonts
[FONT_NORMAL
] = conf
->tr
;
619 pdata
->sizes
[FONT_NORMAL
] = 12;
620 pdata
->fonts
[FONT_EMPH
] = conf
->ti
;
621 pdata
->sizes
[FONT_EMPH
] = 12;
622 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
623 pdata
->sizes
[FONT_CODE
] = 12;
628 * Also select an indentation level depending on the
629 * paragraph type (list paragraphs other than
630 * para_DescribedThing need extra indent).
632 * (FIXME: Perhaps at some point we might even arrange
633 * for the user to be able to request indented first
634 * lines in paragraphs.)
636 if (ptype
== para_Bullet
||
637 ptype
== para_NumberedList
||
638 ptype
== para_Description
) {
639 extra_indent
= firstline_indent
= conf
->indent_list
;
641 extra_indent
= firstline_indent
= 0;
645 * Find the auxiliary text for this paragraph.
656 * For some heading styles (FIXME: be able to
657 * configure which), the auxiliary text contains
658 * the chapter number and is arranged to be
659 * right-aligned a few points left of the primary
660 * margin. For other styles, the auxiliary text is
661 * the full chapter _name_ and takes up space
662 * within the (wrapped) chapter title, meaning that
663 * we must move the first line indent over to make
666 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
670 len
= paper_width_simple(pdata
, pkwtext2
);
671 aux_indent
= -len
- conf
->sect_num_left_space
;
673 pdata
->outline_title
=
674 prepare_outline_title(pkwtext2
, L
" ", pwords
);
677 aux2
= fake_word(L
": ");
680 firstline_indent
+= paper_width_simple(pdata
, aux
);
681 firstline_indent
+= paper_width_simple(pdata
, aux2
);
683 pdata
->outline_title
=
684 prepare_outline_title(pkwtext
, L
": ", pwords
);
690 * Auxiliary text consisting of a bullet. (FIXME:
691 * configurable bullet.)
693 aux
= fake_word(L
"\x2022");
694 aux_indent
= indent
+ conf
->indent_list_bullet
;
697 case para_NumberedList
:
699 * Auxiliary text consisting of the number followed
700 * by a (FIXME: configurable) full stop.
703 aux2
= fake_word(L
".");
704 aux_indent
= indent
+ conf
->indent_list_bullet
;
707 case para_BiblioCited
:
709 * Auxiliary text consisting of the bibliography
710 * reference text, and a trailing space.
713 aux2
= fake_word(L
" ");
715 firstline_indent
+= paper_width_simple(pdata
, aux
);
716 firstline_indent
+= paper_width_simple(pdata
, aux2
);
720 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
721 pdata
->outline_title
=
722 prepare_outline_title(NULL
, NULL
, pwords
);
725 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
726 indent
+ firstline_indent
,
727 indent
+ extra_indent
);
729 pdata
->first
->aux_text
= aux
;
730 pdata
->first
->aux_text_2
= aux2
;
731 pdata
->first
->aux_left_indent
= aux_indent
;
734 * Line breaking penalties.
741 case para_UnnumberedChapter
:
743 * Fixed and large penalty for breaking straight
744 * after a heading; corresponding bonus for
745 * breaking straight before.
747 pdata
->first
->penalty_before
= -500000;
748 pdata
->last
->penalty_after
= 500000;
749 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
750 ldata
->penalty_after
= 500000;
753 case para_DescribedThing
:
755 * This is treated a bit like a small heading:
756 * there's a penalty for breaking after it (i.e.
757 * between it and its description), and a bonus for
758 * breaking before it (actually _between_ list
761 pdata
->first
->penalty_before
= -200000;
762 pdata
->last
->penalty_after
= 200000;
767 * Most paragraph types: widow/orphan control by
768 * discouraging breaking one line from the end of
771 if (pdata
->first
!= pdata
->last
) {
772 pdata
->first
->penalty_after
= 100000;
773 pdata
->last
->penalty_before
= 100000;
778 standard_line_spacing(pdata
, conf
);
781 * Some kinds of section heading require a page break before
782 * them and an underline after.
784 if (ptype
== para_Title
||
785 ptype
== para_Chapter
||
786 ptype
== para_Appendix
||
787 ptype
== para_UnnumberedChapter
) {
788 pdata
->first
->page_break
= TRUE
;
789 pdata
->first
->space_before
= conf
->chapter_top_space
;
790 pdata
->last
->space_after
+=
791 (conf
->chapter_underline_depth
+
792 conf
->chapter_underline_thickness
);
793 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
799 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
804 * Set the line spacing for each line in this paragraph.
806 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
807 if (ldata
== pdata
->first
)
808 ldata
->space_before
= conf
->base_para_spacing
/ 2;
810 ldata
->space_before
= conf
->base_leading
/ 2;
811 if (ldata
== pdata
->last
)
812 ldata
->space_after
= conf
->base_para_spacing
/ 2;
814 ldata
->space_after
= conf
->base_leading
/ 2;
815 ldata
->page_break
= FALSE
;
819 static font_encoding
*new_font_encoding(font_data
*font
)
824 fe
= mknew(font_encoding
);
827 if (font
->list
->tail
)
828 font
->list
->tail
->next
= fe
;
830 font
->list
->head
= fe
;
831 font
->list
->tail
= fe
;
836 for (i
= 0; i
< 256; i
++) {
837 fe
->vector
[i
] = NULL
;
839 fe
->to_unicode
[i
] = 0xFFFF;
845 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
853 widths
= ps_std_font_widths(name
);
857 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
859 f
= mknew(font_data
);
863 f
->nglyphs
= nglyphs
;
864 f
->glyphs
= ps_std_glyphs
;
866 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
869 * Our first subfont will contain all of US-ASCII. This isn't
870 * really necessary - we could just create custom subfonts
871 * precisely as the whim of render_string dictated - but
872 * instinct suggests that it might be nice to have the text in
873 * the output files look _marginally_ recognisable.
875 fe
= new_font_encoding(f
);
876 fe
->free_pos
= 0xA1; /* only the top half is free */
877 f
->latest_subfont
= fe
;
879 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
882 for (i
= 0; i
< nglyphs
; i
++) {
884 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
885 assert(ucs
!= 0xFFFF);
887 if (ucs
>= 0x20 && ucs
<= 0x7E) {
888 fe
->vector
[ucs
] = f
->glyphs
[i
];
889 fe
->indices
[ucs
] = i
;
890 fe
->to_unicode
[ucs
] = ucs
;
891 f
->subfont_map
[i
].subfont
= fe
;
892 f
->subfont_map
[i
].position
= ucs
;
895 * This character is not yet assigned to a subfont.
897 f
->subfont_map
[i
].subfont
= NULL
;
898 f
->subfont_map
[i
].position
= 0;
905 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
912 for (; *string
; string
++) {
915 index
= font
->bmp
[(unsigned short)*string
];
916 if (index
== 0xFFFF) {
920 width
+= font
->widths
[index
];
927 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
929 struct paper_width_ctx
{
934 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
936 while (text
&& text
!= end
) {
937 w
+= paper_width_internal(vctx
, text
, nspaces
);
943 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
945 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
946 int style
, type
, findex
, width
, errs
;
949 switch (word
->type
) {
959 style
= towordstyle(word
->type
);
960 type
= removeattr(word
->type
);
962 findex
= (style
== word_Normal ? FONT_NORMAL
:
963 style
== word_Emph ? FONT_EMPH
:
966 if (type
== word_Normal
) {
968 } else if (type
== word_WhiteSpace
) {
969 if (findex
!= FONT_CODE
) {
972 return ctx
->minspacewidth
;
975 } else /* if (type == word_Quote) */ {
976 if (word
->aux
== quote_Open
)
977 str
= L
"\x2018"; /* FIXME: configurability! */
979 str
= L
"\x2019"; /* FIXME: configurability! */
982 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
984 if (errs
&& word
->alt
)
985 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
987 return ctx
->pdata
->sizes
[findex
] * width
;
990 static int paper_width(void *vctx
, word
*word
)
992 return paper_width_internal(vctx
, word
, NULL
);
995 static int paper_width_simple(para_data
*pdata
, word
*text
)
997 struct paper_width_ctx ctx
;
1001 (pdata
->sizes
[FONT_NORMAL
] *
1002 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1004 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1007 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1008 int w
, int i1
, int i2
)
1010 wrappedline
*wrapping
, *p
;
1012 struct paper_width_ctx ctx
;
1016 * We're going to need to store the line height in every line
1017 * structure we generate.
1022 for (i
= 0; i
< NFONTS
; i
++)
1023 if (line_height
< pdata
->sizes
[i
])
1024 line_height
= pdata
->sizes
[i
];
1025 line_height
*= 4096;
1028 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1029 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1030 if (spacewidth
== 0) {
1032 * A font without a space?! Disturbing. I hope this never
1033 * comes up, but I'll make a random guess anyway and set my
1034 * space width to half the point size.
1036 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
1040 * I'm going to set the _minimum_ space width to 3/5 of the
1041 * standard one, and use the standard one as the optimum.
1043 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1046 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1049 * Having done the wrapping, we now concoct a set of line_data
1052 pdata
->first
= pdata
->last
= NULL
;
1054 for (p
= wrapping
; p
; p
= p
->next
) {
1057 int len
, wid
, spaces
;
1059 ldata
= mknew(line_data
);
1061 ldata
->pdata
= pdata
;
1062 ldata
->first
= p
->begin
;
1063 ldata
->end
= p
->end
;
1064 ldata
->line_height
= line_height
;
1066 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1069 pdata
->last
->next
= ldata
;
1070 ldata
->prev
= pdata
->last
;
1072 pdata
->first
= ldata
;
1076 pdata
->last
= ldata
;
1079 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1080 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1083 ldata
->hshortfall
= wid
- len
;
1084 ldata
->nspaces
= spaces
;
1086 * This tells us how much the space width needs to
1087 * change from _min_spacewidth. But we want to store
1088 * its difference from the _natural_ space width, to
1089 * make the text rendering easier.
1091 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1092 ldata
->hshortfall
-= spacewidth
* spaces
;
1094 * Special case: on the last line of a paragraph, we
1095 * never stretch spaces.
1097 if (ldata
->hshortfall
> 0 && !p
->next
)
1098 ldata
->hshortfall
= 0;
1100 ldata
->aux_text
= NULL
;
1101 ldata
->aux_text_2
= NULL
;
1102 ldata
->aux_left_indent
= 0;
1103 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1108 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1115 * Page breaking is done by a close analogue of the optimal
1116 * paragraph wrapping algorithm used by wrap_para(). We work
1117 * backwards from the end of the document line by line; for
1118 * each line, we contemplate every possible number of lines we
1119 * could put on a page starting with that line, determine a
1120 * cost function for each one, add it to the pre-computed cost
1121 * function for optimally page-breaking everything after that
1122 * page, and pick the best option.
1124 * Since my line_data structures are only used for this
1125 * purpose, I might as well just store the algorithm data
1129 for (l
= last
; l
; l
= l
->prev
) {
1130 int minheight
, text
= 0, space
= 0;
1134 for (m
= l
; m
; m
= m
->next
) {
1135 if (m
!= l
&& m
->page_break
)
1136 break; /* we've gone as far as we can */
1139 space
+= m
->prev
->space_after
;
1140 if (m
!= l
|| m
->page_break
)
1141 space
+= m
->space_before
;
1142 text
+= m
->line_height
;
1143 minheight
= text
+ space
;
1145 if (m
!= l
&& minheight
> page_height
)
1149 * Compute the cost of this arrangement, as the square
1150 * of the amount of wasted space on the page.
1151 * Exception: if this is the last page before a
1152 * mandatory break or the document end, we don't
1153 * penalise a large blank area.
1155 if (m
->next
&& !m
->next
->page_break
)
1157 int x
= page_height
- minheight
;
1164 cost
+= (x
* xf
) >> 8;
1168 if (m
->next
&& !m
->next
->page_break
) {
1169 cost
+= m
->penalty_after
;
1170 cost
+= m
->next
->penalty_before
;
1173 if (m
->next
&& !m
->next
->page_break
)
1174 cost
+= m
->next
->bestcost
;
1175 if (l
->bestcost
== -1 || l
->bestcost
> cost
) {
1177 * This is the best option yet for this starting
1181 if (m
->next
&& !m
->next
->page_break
)
1182 l
->vshortfall
= page_height
- minheight
;
1193 * Now go through the line list forwards and assemble the
1203 page
= mknew(page_data
);
1212 page
->first_line
= l
;
1213 page
->last_line
= l
->page_last
;
1215 page
->first_text
= page
->last_text
= NULL
;
1216 page
->first_xref
= page
->last_xref
= NULL
;
1217 page
->first_rect
= page
->last_rect
= NULL
;
1220 * Now assign a y-coordinate to each line on the page.
1223 for (l
= page
->first_line
; l
; l
= l
->next
) {
1224 if (l
!= page
->first_line
)
1225 space
+= l
->prev
->space_after
;
1226 if (l
!= page
->first_line
|| l
->page_break
)
1227 space
+= l
->space_before
;
1228 text
+= l
->line_height
;
1231 l
->ypos
= text
+ space
+
1232 space
* (float)page
->first_line
->vshortfall
/
1233 page
->first_line
->space
;
1235 if (l
== page
->last_line
)
1239 l
= page
->last_line
->next
;
1245 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1247 rect
*r
= mknew(rect
);
1250 if (page
->last_rect
)
1251 page
->last_rect
->next
= r
;
1253 page
->first_rect
= r
;
1254 page
->last_rect
= r
;
1262 static void add_string_to_page(page_data
*page
, int x
, int y
,
1263 font_encoding
*fe
, int size
, char *text
)
1265 text_fragment
*frag
;
1267 frag
= mknew(text_fragment
);
1270 if (page
->last_text
)
1271 page
->last_text
->next
= frag
;
1273 page
->first_text
= frag
;
1274 page
->last_text
= frag
;
1279 frag
->fontsize
= size
;
1280 frag
->text
= dupstr(text
);
1284 * Returns the updated x coordinate.
1286 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1287 int x
, int y
, wchar_t *str
)
1290 int textpos
, textwid
, glyph
;
1291 font_encoding
*subfont
= NULL
, *sf
;
1293 text
= mknewa(char, 1 + ustrlen(str
));
1294 textpos
= textwid
= 0;
1297 glyph
= font
->bmp
[*str
];
1299 if (glyph
== 0xFFFF)
1300 continue; /* nothing more we can do here */
1303 * Find which subfont this character is going in.
1305 sf
= font
->subfont_map
[glyph
].subfont
;
1311 * This character is not yet in a subfont. Assign one.
1313 if (font
->latest_subfont
->free_pos
>= 0x100)
1314 font
->latest_subfont
= new_font_encoding(font
);
1316 c
= font
->latest_subfont
->free_pos
++;
1317 if (font
->latest_subfont
->free_pos
== 0x7F)
1318 font
->latest_subfont
->free_pos
= 0xA1;
1320 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1321 font
->subfont_map
[glyph
].position
= c
;
1322 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1323 font
->latest_subfont
->indices
[c
] = glyph
;
1324 font
->latest_subfont
->to_unicode
[c
] = *str
;
1326 sf
= font
->latest_subfont
;
1329 if (!subfont
|| sf
!= subfont
) {
1331 text
[textpos
] = '\0';
1332 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1335 assert(textpos
== 0);
1341 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1342 textwid
+= font
->widths
[glyph
] * fontsize
;
1348 text
[textpos
] = '\0';
1349 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1357 * Returns the updated x coordinate.
1359 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1360 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1361 int shortfall
, int nspaces
, int *nspace
,
1362 keywordlist
*keywords
)
1364 while (text
&& text
!= text_end
) {
1365 int style
, type
, findex
, errs
;
1369 switch (text
->type
) {
1371 * Start a cross-reference.
1373 case word_HyperLink
:
1374 case word_UpperXref
:
1375 case word_LowerXref
:
1377 if (text
->type
== word_HyperLink
) {
1379 dest
.url
= utoa_dup(text
->text
);
1382 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1386 assert(kwl
->para
->private_data
);
1387 pdata
= (para_data
*) kwl
->para
->private_data
;
1389 dest
.page
= pdata
->first
->page
;
1393 * Shouldn't happen, but *shrug*
1400 if (dest
.type
!= NONE
) {
1402 (*xr
)->dest
= dest
; /* structure copy */
1403 if (page
->last_xref
)
1404 page
->last_xref
->next
= *xr
;
1406 page
->first_xref
= *xr
;
1407 page
->last_xref
= *xr
;
1411 * FIXME: Ideally we should have, and use, some
1412 * vertical font metric information here so that
1413 * our cross-ref rectangle can take account of
1414 * descenders and the font's cap height. This will
1415 * do for the moment, but it isn't ideal.
1417 (*xr
)->lx
= (*xr
)->rx
= x
;
1419 (*xr
)->ty
= y
+ ldata
->line_height
;
1424 * Finish extending a cross-reference box.
1434 * FIXME: we should do something with this.
1438 style
= towordstyle(text
->type
);
1439 type
= removeattr(text
->type
);
1441 findex
= (style
== word_Normal ? FONT_NORMAL
:
1442 style
== word_Emph ? FONT_EMPH
:
1445 if (type
== word_Normal
) {
1447 } else if (type
== word_WhiteSpace
) {
1448 x
+= pdata
->sizes
[findex
] *
1449 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1450 if (nspaces
&& findex
!= FONT_CODE
) {
1451 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1452 x
-= *nspace
* shortfall
/ nspaces
;
1456 } else /* if (type == word_Quote) */ {
1457 if (text
->aux
== quote_Open
)
1458 str
= L
"\x2018"; /* FIXME: configurability! */
1460 str
= L
"\x2019"; /* FIXME: configurability! */
1463 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1465 if (errs
&& text
->alt
)
1466 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1467 xr
, shortfall
, nspaces
, nspace
, keywords
);
1469 x
= render_string(page
, pdata
->fonts
[findex
],
1470 pdata
->sizes
[findex
], x
, y
, str
);
1483 * Returns the last x position used on the line.
1485 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
1486 xref_dest
*dest
, keywordlist
*keywords
)
1492 if (ldata
->aux_text
) {
1496 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1497 left_x
+ ldata
->aux_left_indent
,
1498 top_y
- ldata
->ypos
,
1499 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1500 if (ldata
->aux_text_2
)
1501 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1502 x
, top_y
- ldata
->ypos
,
1503 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1509 * There might be a cross-reference carried over from a
1512 if (dest
->type
!= NONE
) {
1515 xr
->dest
= *dest
; /* structure copy */
1516 if (ldata
->page
->last_xref
)
1517 ldata
->page
->last_xref
->next
= xr
;
1519 ldata
->page
->first_xref
= xr
;
1520 ldata
->page
->last_xref
= xr
;
1521 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1522 xr
->by
= top_y
- ldata
->ypos
;
1523 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1527 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1528 left_x
+ ldata
->xpos
,
1529 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
, &xr
,
1530 ldata
->hshortfall
, ldata
->nspaces
, &nspace
,
1535 * There's a cross-reference continued on to the next line.
1545 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
1547 para_data
*pdata
= mknew(para_data
);
1550 * For code paragraphs, I'm going to hack grievously and
1551 * pretend the three normal fonts are the three code paragraph
1554 pdata
->fonts
[FONT_NORMAL
] = conf
->cb
;
1555 pdata
->fonts
[FONT_EMPH
] = conf
->co
;
1556 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
1557 pdata
->sizes
[FONT_NORMAL
] =
1558 pdata
->sizes
[FONT_EMPH
] =
1559 pdata
->sizes
[FONT_CODE
] = 12;
1561 pdata
->first
= pdata
->last
= NULL
;
1562 pdata
->outline_level
= -1;
1563 pdata
->rect_type
= RECT_NONE
;
1564 pdata
->contents_entry
= NULL
;
1566 for (; words
; words
= words
->next
) {
1567 wchar_t *t
, *e
, *start
;
1568 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1570 int prev
= -1, curr
;
1573 if (words
->next
&& words
->next
->type
== word_Emph
) {
1574 e
= words
->next
->text
;
1575 words
= words
->next
;
1585 else if (*e
== L
'i')
1587 else if (*e
== L
'b')
1604 * We've isolated a maximal subsequence of the line
1605 * which has the same emphasis. Form it into a word
1611 w
->type
= (prev
== 0 ? word_WeakCode
:
1612 prev
== 1 ? word_Emph
: word_Normal
);
1613 w
->text
= mknewa(wchar_t, t
-start
+1);
1614 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
1615 w
->text
[t
-start
] = '\0';
1628 ldata
= mknew(line_data
);
1630 ldata
->pdata
= pdata
;
1631 ldata
->first
= lhead
;
1633 ldata
->line_height
= conf
->base_font_size
* 4096;
1635 ldata
->xpos
= indent
;
1638 pdata
->last
->next
= ldata
;
1639 ldata
->prev
= pdata
->last
;
1641 pdata
->first
= ldata
;
1645 pdata
->last
= ldata
;
1647 ldata
->hshortfall
= 0;
1649 ldata
->aux_text
= NULL
;
1650 ldata
->aux_text_2
= NULL
;
1651 ldata
->aux_left_indent
= 0;
1652 /* General opprobrium for breaking in a code paragraph. */
1653 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
1656 standard_line_spacing(pdata
, conf
);
1661 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
1663 para_data
*pdata
= mknew(para_data
);
1666 ldata
= mknew(line_data
);
1668 ldata
->pdata
= pdata
;
1669 ldata
->first
= NULL
;
1671 ldata
->line_height
= conf
->rule_thickness
;
1673 ldata
->xpos
= indent
;
1678 ldata
->hshortfall
= 0;
1680 ldata
->aux_text
= NULL
;
1681 ldata
->aux_text_2
= NULL
;
1682 ldata
->aux_left_indent
= 0;
1685 * Better to break after a rule than before it
1687 ldata
->penalty_after
+= 100000;
1688 ldata
->penalty_before
+= -100000;
1690 pdata
->first
= pdata
->last
= ldata
;
1691 pdata
->outline_level
= -1;
1692 pdata
->rect_type
= RECT_RULE
;
1693 pdata
->contents_entry
= NULL
;
1695 standard_line_spacing(pdata
, conf
);
1701 * Plain-text-like formatting for outline titles.
1703 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
1704 for (; text
; text
= text
->next
) switch (text
->type
) {
1705 case word_HyperLink
:
1707 case word_UpperXref
:
1708 case word_LowerXref
:
1717 case word_WhiteSpace
:
1718 case word_EmphSpace
:
1719 case word_CodeSpace
:
1720 case word_WkCodeSpace
:
1722 case word_EmphQuote
:
1723 case word_CodeQuote
:
1724 case word_WkCodeQuote
:
1725 assert(text
->type
!= word_CodeQuote
&&
1726 text
->type
!= word_WkCodeQuote
);
1727 if (towordstyle(text
->type
) == word_Emph
&&
1728 (attraux(text
->aux
) == attr_First
||
1729 attraux(text
->aux
) == attr_Only
))
1730 rdadd(rs
, L
'_'); /* FIXME: configurability */
1731 else if (towordstyle(text
->type
) == word_Code
&&
1732 (attraux(text
->aux
) == attr_First
||
1733 attraux(text
->aux
) == attr_Only
))
1734 rdadd(rs
, L
'\''); /* FIXME: configurability */
1735 if (removeattr(text
->type
) == word_Normal
) {
1736 rdadds(rs
, text
->text
);
1737 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
1739 } else if (removeattr(text
->type
) == word_Quote
) {
1740 rdadd(rs
, L
'\''); /* fixme: configurability */
1742 if (towordstyle(text
->type
) == word_Emph
&&
1743 (attraux(text
->aux
) == attr_Last
||
1744 attraux(text
->aux
) == attr_Only
))
1745 rdadd(rs
, L
'_'); /* FIXME: configurability */
1746 else if (towordstyle(text
->type
) == word_Code
&&
1747 (attraux(text
->aux
) == attr_Last
||
1748 attraux(text
->aux
) == attr_Only
))
1749 rdadd(rs
, L
'\''); /* FIXME: configurability */
1754 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
1757 rdstring rs
= {0, 0, NULL
};
1760 paper_rdaddw(&rs
, first
);
1762 rdadds(&rs
, separator
);
1764 paper_rdaddw(&rs
, second
);
1769 static word
*fake_word(wchar_t *text
)
1771 word
*ret
= mknew(word
);
1774 ret
->type
= word_Normal
;
1775 ret
->text
= ustrdup(text
);
1776 ret
->breaks
= FALSE
;
1781 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
1790 w
= dup_word_list(first
);
1798 w
= fake_word(separator
);
1804 *wptr
= dup_word_list(second
);