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 * - the index should not refer to the contents, if an indexable
16 * term crops up in a section title!
18 * - include the version IDs.
20 * - linearised PDF, perhaps?
22 * - compression of output files. For the actual text display,
23 * both output formats currently average about 50-60 characters
24 * per 5-6 character word of text, and almost all of it's the
26 * * In PS, we can define custom text operators to make things
28 * * In PDF, there already are!
30 * - I'm uncertain of whether I need to include a ToUnicode CMap
31 * in each of my font definitions in PDF. Currently things (by
32 * which I mean cut and paste out of acroread) seem to be
33 * working fairly happily without it, but I don't know.
35 * - rather than the ugly aux_text mechanism for rendering chapter
36 * titles, we could actually build the correct word list and
39 * - get vertical font metrics and use them to position the PDF
40 * xref boxes more pleasantly
43 * * all the measurements in `conf' should be configurable
44 * + notably paper size/shape
45 * * page header and footer should be configurable; we should
46 * be able to shift the page number elsewhere, and add other
47 * things such as the current chapter/section title and fixed
49 * * remove the fixed mapping from heading levels to heading
50 * styles; offer a menu of styles from which the user can
51 * choose at every heading level
52 * * first-line indent in paragraphs
53 * * fixed text: `Contents', `Index', bullet, quotes, the
54 * colon-space and full stop in chapter title constructions
55 * * configurable location of contents?
56 * * certainly configurably _remove_ the contents, and possibly
58 * * double-sided document switch?
59 * + means you have two header/footer formats which
61 * + and means that mandatory page breaks before chapter
62 * titles should include a blank page if necessary to
63 * start the next section to a right-hand page
67 * - ability to import other Type 1 fonts
68 * * we need to parse the font to extract its metrics
69 * * then we pass the font bodily to both PS and PDF so it can
70 * be included in the output file
72 * - character substitution for better typography?
73 * * fi, fl, ffi, ffl ligatures
74 * * use real ellipsis rather than ...
75 * * a hyphen in a word by itself might prefer to be an en-dash
76 * * (Americans might even want a convenient way to use an
78 * * substituting `minus' for `hyphen' in the standard encoding
79 * is probably preferable in Courier, though certainly not in
81 * * if I do do this lot, I'm rather inclined to at least try
82 * to think up a configurable way to do it so that Americans
83 * can do em-dash tricks without my intervention and other
84 * people can do other odd things too.
93 typedef struct paper_conf_Tag paper_conf
;
94 typedef struct paper_idx_Tag paper_idx
;
96 struct paper_conf_Tag
{
103 int indent_list_bullet
;
107 int base_para_spacing
;
108 int chapter_top_space
;
109 int sect_num_left_space
;
110 int chapter_underline_depth
;
111 int chapter_underline_thickness
;
114 int contents_indent_step
;
116 int leader_separation
;
120 int pagenum_fontsize
;
122 /* These are derived from the above */
126 /* Fonts used in the configuration */
127 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
130 struct paper_idx_Tag
{
132 * Word list giving the page numbers on which this index entry
133 * appears. Also the last word in the list, for ease of
139 * The last page added to the list (so we can ensure we don't
146 word_PageXref
= word_NotWordType
+ 1
149 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
150 static void wrap_paragraph(para_data
*pdata
, word
*words
,
151 int w
, int i1
, int i2
);
152 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
153 int page_height
, int ncols
, int headspace
);
154 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
155 int x
, int y
, wchar_t *str
);
156 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
157 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
);
158 static void render_para(para_data
*pdata
, paper_conf
*conf
,
159 keywordlist
*keywords
, indexdata
*idx
,
160 paragraph
*index_placeholder
, page_data
*index_page
);
161 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
);
162 static int paper_width_simple(para_data
*pdata
, word
*text
);
163 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
);
164 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
);
165 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
166 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
167 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
169 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
);
170 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
172 static word
*fake_word(wchar_t *text
);
173 static word
*fake_space_word(void);
174 static word
*fake_page_ref(page_data
*page
);
175 static word
*fake_end_ref(void);
176 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
178 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
);
180 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
184 int indent
, used_contents
;
185 para_data
*pdata
, *firstpara
= NULL
, *lastpara
= NULL
;
186 para_data
*firstcont
, *lastcont
;
187 line_data
*firstline
, *lastline
, *firstcontline
, *lastcontline
;
193 paragraph index_placeholder_para
;
194 page_data
*first_index_page
;
197 * FIXME: All these things ought to become configurable.
199 conf
= mknew(paper_conf
);
200 conf
->paper_width
= 595 * 4096;
201 conf
->paper_height
= 841 * 4096;
202 conf
->left_margin
= 72 * 4096;
203 conf
->top_margin
= 72 * 4096;
204 conf
->right_margin
= 72 * 4096;
205 conf
->bottom_margin
= 108 * 4096;
206 conf
->indent_list_bullet
= 6 * 4096;
207 conf
->indent_list
= 24 * 4096;
208 conf
->indent_quote
= 18 * 4096;
209 conf
->base_leading
= 4096;
210 conf
->base_para_spacing
= 10 * 4096;
211 conf
->chapter_top_space
= 72 * 4096;
212 conf
->sect_num_left_space
= 12 * 4096;
213 conf
->chapter_underline_depth
= 14 * 4096;
214 conf
->chapter_underline_thickness
= 3 * 4096;
215 conf
->rule_thickness
= 1 * 4096;
216 conf
->base_font_size
= 12;
217 conf
->contents_indent_step
= 24 * 4096;
218 conf
->contents_margin
= 84 * 4096;
219 conf
->leader_separation
= 12 * 4096;
220 conf
->index_gutter
= 36 * 4096;
221 conf
->index_cols
= 2;
222 conf
->index_minsep
= 18 * 4096;
223 conf
->pagenum_fontsize
= 12;
224 conf
->footer_distance
= 32 * 4096;
227 conf
->paper_width
- conf
->left_margin
- conf
->right_margin
;
229 conf
->paper_height
- conf
->top_margin
- conf
->bottom_margin
;
230 conf
->index_colwidth
=
231 (conf
->base_width
- (conf
->index_cols
-1) * conf
->index_gutter
)
235 * First, set up some font structures.
237 fontlist
= mknew(font_list
);
238 fontlist
->head
= fontlist
->tail
= NULL
;
239 conf
->tr
= make_std_font(fontlist
, "Times-Roman");
240 conf
->ti
= make_std_font(fontlist
, "Times-Italic");
241 conf
->hr
= make_std_font(fontlist
, "Helvetica-Bold");
242 conf
->hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
243 conf
->cr
= make_std_font(fontlist
, "Courier");
244 conf
->co
= make_std_font(fontlist
, "Courier-Oblique");
245 conf
->cb
= make_std_font(fontlist
, "Courier-Bold");
248 * Set up a data structure to collect page numbers for each
257 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
258 paper_idx
*pi
= mknew(paper_idx
);
262 pi
->words
= pi
->lastword
= NULL
;
265 entry
->backend_data
= pi
;
270 * Format the contents entry for each heading.
273 word
*contents_title
;
274 contents_title
= fake_word(L
"Contents");
276 firstcont
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
277 NULL
, NULL
, contents_title
, conf
);
278 lastcont
= firstcont
;
279 lastcont
->next
= NULL
;
280 firstcontline
= firstcont
->first
;
281 lastcontline
= lastcont
->last
;
282 for (p
= sourceform
; p
; p
= p
->next
) {
289 case para_UnnumberedChapter
:
295 words
= prepare_contents_title(p
->kwtext
, L
": ", p
->words
);
298 case para_UnnumberedChapter
:
299 words
= prepare_contents_title(NULL
, NULL
, p
->words
);
304 words
= prepare_contents_title(p
->kwtext2
, L
" ", p
->words
);
305 indent
= (p
->aux
+ 1) * conf
->contents_indent_step
;
308 pdata
= make_para_data(para_Normal
, p
->aux
, indent
,
309 conf
->contents_margin
,
310 NULL
, NULL
, words
, conf
);
312 pdata
->contents_entry
= p
;
313 lastcont
->next
= pdata
;
317 * Link all contents line structures together into
322 lastcontline
->next
= pdata
->first
;
323 pdata
->first
->prev
= lastcontline
;
325 firstcontline
= pdata
->first
;
326 pdata
->first
->prev
= NULL
;
328 lastcontline
= pdata
->last
;
329 lastcontline
->next
= NULL
;
337 * And one extra one, for the index.
340 pdata
= make_para_data(para_Normal
, 0, 0,
341 conf
->contents_margin
,
342 NULL
, NULL
, fake_word(L
"Index"), conf
);
344 pdata
->contents_entry
= &index_placeholder_para
;
345 lastcont
->next
= pdata
;
350 lastcontline
->next
= pdata
->first
;
351 pdata
->first
->prev
= lastcontline
;
353 firstcontline
= pdata
->first
;
354 pdata
->first
->prev
= NULL
;
356 lastcontline
= pdata
->last
;
357 lastcontline
->next
= NULL
;
363 * Do the main paragraph formatting.
366 used_contents
= FALSE
;
367 firstline
= lastline
= NULL
;
368 for (p
= sourceform
; p
; p
= p
->next
) {
369 p
->private_data
= NULL
;
373 * These paragraph types are either invisible or don't
374 * define text in the normal sense. Either way, they
375 * don't require wrapping.
380 case para_NotParaType
:
387 * These paragraph types don't require wrapping, but
388 * they do affect the line width to which we wrap the
389 * rest of the paragraphs, so we need to pay attention.
392 indent
+= conf
->indent_list
; break;
394 indent
-= conf
->indent_list
; assert(indent
>= 0); break;
396 indent
+= conf
->indent_quote
; break;
398 indent
-= conf
->indent_quote
; assert(indent
>= 0); break;
401 * This paragraph type is special. Process it
405 pdata
= code_paragraph(indent
, p
->words
, conf
);
406 p
->private_data
= pdata
;
407 if (pdata
->first
!= pdata
->last
) {
408 pdata
->first
->penalty_after
+= 100000;
409 pdata
->last
->penalty_before
+= 100000;
414 * This paragraph is also special.
417 pdata
= rule_paragraph(indent
, conf
);
418 p
->private_data
= pdata
;
422 * All of these paragraph types require wrapping in the
423 * ordinary way. So we must supply a set of fonts, a
424 * line width and auxiliary information (e.g. bullet
425 * text) for each one.
429 case para_UnnumberedChapter
:
433 case para_BiblioCited
:
435 case para_NumberedList
:
436 case para_DescribedThing
:
437 case para_Description
:
440 pdata
= make_para_data(p
->type
, p
->aux
, indent
, 0,
441 p
->kwtext
, p
->kwtext2
, p
->words
, conf
);
443 p
->private_data
= pdata
;
448 if (p
->private_data
) {
449 pdata
= (para_data
*)p
->private_data
;
452 * If this is the first non-title heading, we link the
453 * contents section in before it.
455 if (!used_contents
&& pdata
->outline_level
> 0) {
456 used_contents
= TRUE
;
458 lastpara
->next
= firstcont
;
460 firstpara
= firstcont
;
462 assert(lastpara
->next
== NULL
);
465 lastline
->next
= firstcontline
;
466 firstcontline
->prev
= lastline
;
468 firstline
= firstcontline
;
469 firstcontline
->prev
= NULL
;
471 assert(lastcontline
!= NULL
);
472 lastline
= lastcontline
;
473 lastline
->next
= NULL
;
477 * Link all line structures together into a big list.
481 lastline
->next
= pdata
->first
;
482 pdata
->first
->prev
= lastline
;
484 firstline
= pdata
->first
;
485 pdata
->first
->prev
= NULL
;
487 lastline
= pdata
->last
;
488 lastline
->next
= NULL
;
492 * Link all paragraph structures together similarly.
496 lastpara
->next
= pdata
;
504 * Now we have an enormous linked list of every line of text in
505 * the document. Break it up into pages.
507 pages
= page_breaks(firstline
, lastline
, conf
->page_height
, 0, 0);
518 for (page
= pages
; page
; page
= page
->next
) {
519 sprintf(buf
, "%d", ++pagenum
);
520 page
->number
= ufroma_dup(buf
);
524 first_index_page
= mknew(page_data
);
525 first_index_page
->next
= first_index_page
->prev
= NULL
;
526 first_index_page
->first_line
= NULL
;
527 first_index_page
->last_line
= NULL
;
528 first_index_page
->first_text
= first_index_page
->last_text
= NULL
;
529 first_index_page
->first_xref
= first_index_page
->last_xref
= NULL
;
530 first_index_page
->first_rect
= first_index_page
->last_rect
= NULL
;
532 /* And don't forget the as-yet-uncreated index. */
533 sprintf(buf
, "%d", ++pagenum
);
534 first_index_page
->number
= ufroma_dup(buf
);
539 * Now we're ready to actually lay out the pages. We do this by
540 * looping over _paragraphs_, since we may need to track cross-
541 * references between lines and even across pages.
543 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
)
544 render_para(pdata
, conf
, keywords
, idx
,
545 &index_placeholder_para
, first_index_page
);
548 * Now we've laid out the main body pages, we should have
549 * acquired a full set of page numbers for the index.
555 para_data
*firstidx
, *lastidx
;
556 line_data
*firstidxline
, *lastidxline
, *ldata
;
557 page_data
*ipages
, *ipages2
, *page
;
560 * Create a set of paragraphs for the index.
562 index_title
= fake_word(L
"Index");
564 firstidx
= make_para_data(para_UnnumberedChapter
, 0, 0, 0,
565 NULL
, NULL
, index_title
, conf
);
567 lastidx
->next
= NULL
;
568 firstidxline
= firstidx
->first
;
569 lastidxline
= lastidx
->last
;
570 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
571 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
572 para_data
*text
, *pages
;
574 text
= make_para_data(para_Normal
, 0, 0,
575 conf
->base_width
- conf
->index_colwidth
,
576 NULL
, NULL
, entry
->text
, conf
);
578 pages
= make_para_data(para_Normal
, 0, 0,
579 conf
->base_width
- conf
->index_colwidth
,
580 NULL
, NULL
, pi
->words
, conf
);
582 text
->justification
= LEFT
;
583 pages
->justification
= RIGHT
;
584 text
->last
->space_after
= pages
->first
->space_before
=
585 conf
->base_leading
/ 2;
587 pages
->last
->space_after
= text
->first
->space_before
=
591 assert(pages
->first
);
596 * If feasible, fold the two halves of the index entry
599 if (text
->last
->real_shortfall
+ pages
->first
->real_shortfall
>
600 conf
->index_colwidth
+ conf
->index_minsep
) {
601 text
->last
->space_after
= -1;
602 pages
->first
->space_before
= -pages
->first
->line_height
+1;
605 lastidx
->next
= text
;
611 * Link all index line structures together into
614 text
->last
->next
= pages
->first
;
615 pages
->first
->prev
= text
->last
;
617 lastidxline
->next
= text
->first
;
618 text
->first
->prev
= lastidxline
;
620 lastidxline
= pages
->last
;
623 * Breaking an index entry anywhere is so bad that I
624 * think I'm going to forbid it totally.
626 for (ldata
= text
->first
; ldata
&& ldata
->next
;
627 ldata
= ldata
->next
) {
628 ldata
->next
->space_before
+= ldata
->space_after
+ 1;
629 ldata
->space_after
= -1;
634 * Now break the index into pages.
636 ipages
= page_breaks(firstidxline
, firstidxline
, conf
->page_height
,
638 ipages2
= page_breaks(firstidxline
->next
, lastidxline
,
641 firstidxline
->space_before
+
642 firstidxline
->line_height
+
643 firstidxline
->space_after
);
646 * This will have put each _column_ of the index on a
647 * separate page, which isn't what we want. Fold the pages
654 for (i
= 1; i
< conf
->index_cols
; i
++)
658 fold_into_page(page
, page
->next
,
659 i
* (conf
->index_colwidth
+
660 conf
->index_gutter
));
662 page
->next
= page
->next
->next
;
664 page
->next
->prev
= page
;
670 /* Also fold the heading on to the same page as the index items. */
671 fold_into_page(ipages
, ipages2
, 0);
672 ipages
->next
= ipages2
->next
;
674 ipages
->next
->prev
= ipages
;
676 fold_into_page(first_index_page
, ipages
, 0);
677 first_index_page
->next
= ipages
->next
;
678 if (first_index_page
->next
)
679 first_index_page
->next
->prev
= first_index_page
;
681 ipages
= first_index_page
;
684 * Number the index pages, except the already-numbered
687 for (page
= ipages
->next
; page
; page
= page
->next
) {
689 sprintf(buf
, "%d", ++pagenum
);
690 page
->number
= ufroma_dup(buf
);
694 * Render the index pages.
696 for (pdata
= firstidx
; pdata
; pdata
= pdata
->next
)
697 render_para(pdata
, conf
, keywords
, idx
,
698 &index_placeholder_para
, first_index_page
);
701 * Link the index page list on to the end of the main page
707 for (page
= pages
; page
->next
; page
= page
->next
);
712 * Same with the paragraph list, which will cause the index
713 * to be mentioned in the document outline.
716 firstpara
= firstidx
;
718 lastpara
->next
= firstidx
;
723 * Draw the headers and footers.
725 * FIXME: this should be fully configurable, but for the moment
726 * I'm just going to put in page numbers in the centre of a
727 * footer and leave it at that.
732 for (page
= pages
; page
; page
= page
->next
) {
735 width
= conf
->pagenum_fontsize
*
736 string_width(conf
->tr
, page
->number
, NULL
);
738 render_string(page
, conf
->tr
, conf
->pagenum_fontsize
,
739 conf
->left_margin
+ (conf
->base_width
- width
)/2,
740 conf
->bottom_margin
- conf
->footer_distance
,
746 * Start putting together the overall document structure we're
749 doc
= mknew(document
);
750 doc
->fonts
= fontlist
;
752 doc
->paper_width
= conf
->paper_width
;
753 doc
->paper_height
= conf
->paper_height
;
756 * Collect the section heading paragraphs into a document
757 * outline. This is slightly fiddly because the Title paragraph
758 * isn't required to be at the start, although all the others
764 doc
->outline_elements
= mknewa(outline_element
, osize
);
765 doc
->n_outline_elements
= 0;
767 /* First find the title. */
768 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
769 if (pdata
->outline_level
== 0) {
770 doc
->outline_elements
[0].level
= 0;
771 doc
->outline_elements
[0].pdata
= pdata
;
772 doc
->n_outline_elements
++;
777 /* Then collect the rest. */
778 for (pdata
= firstpara
; pdata
; pdata
= pdata
->next
) {
779 if (pdata
->outline_level
> 0) {
780 if (doc
->n_outline_elements
>= osize
) {
782 doc
->outline_elements
=
783 resize(doc
->outline_elements
, osize
);
786 doc
->outline_elements
[doc
->n_outline_elements
].level
=
787 pdata
->outline_level
;
788 doc
->outline_elements
[doc
->n_outline_elements
].pdata
= pdata
;
789 doc
->n_outline_elements
++;
799 static para_data
*make_para_data(int ptype
, int paux
, int indent
, int rmargin
,
800 word
*pkwtext
, word
*pkwtext2
, word
*pwords
,
805 int extra_indent
, firstline_indent
, aux_indent
;
808 pdata
= mknew(para_data
);
809 pdata
->outline_level
= -1;
810 pdata
->outline_title
= NULL
;
811 pdata
->rect_type
= RECT_NONE
;
812 pdata
->contents_entry
= NULL
;
813 pdata
->justification
= JUST
;
816 * Choose fonts for this paragraph.
818 * FIXME: All of this ought to be completely
823 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
824 pdata
->sizes
[FONT_NORMAL
] = 24;
825 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
826 pdata
->sizes
[FONT_EMPH
] = 24;
827 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
828 pdata
->sizes
[FONT_CODE
] = 24;
829 pdata
->outline_level
= 0;
834 case para_UnnumberedChapter
:
835 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
836 pdata
->sizes
[FONT_NORMAL
] = 20;
837 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
838 pdata
->sizes
[FONT_EMPH
] = 20;
839 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
840 pdata
->sizes
[FONT_CODE
] = 20;
841 pdata
->outline_level
= 1;
846 pdata
->fonts
[FONT_NORMAL
] = conf
->hr
;
847 pdata
->fonts
[FONT_EMPH
] = conf
->hi
;
848 pdata
->fonts
[FONT_CODE
] = conf
->cb
;
849 pdata
->sizes
[FONT_NORMAL
] =
850 pdata
->sizes
[FONT_EMPH
] =
851 pdata
->sizes
[FONT_CODE
] =
852 (paux
== 0 ?
16 : paux
== 1 ?
14 : 13);
853 pdata
->outline_level
= 2 + paux
;
857 case para_BiblioCited
:
859 case para_NumberedList
:
860 case para_DescribedThing
:
861 case para_Description
:
863 pdata
->fonts
[FONT_NORMAL
] = conf
->tr
;
864 pdata
->sizes
[FONT_NORMAL
] = 12;
865 pdata
->fonts
[FONT_EMPH
] = conf
->ti
;
866 pdata
->sizes
[FONT_EMPH
] = 12;
867 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
868 pdata
->sizes
[FONT_CODE
] = 12;
873 * Also select an indentation level depending on the
874 * paragraph type (list paragraphs other than
875 * para_DescribedThing need extra indent).
877 * (FIXME: Perhaps at some point we might even arrange
878 * for the user to be able to request indented first
879 * lines in paragraphs.)
881 if (ptype
== para_Bullet
||
882 ptype
== para_NumberedList
||
883 ptype
== para_Description
) {
884 extra_indent
= firstline_indent
= conf
->indent_list
;
886 extra_indent
= firstline_indent
= 0;
890 * Find the auxiliary text for this paragraph.
901 * For some heading styles (FIXME: be able to
902 * configure which), the auxiliary text contains
903 * the chapter number and is arranged to be
904 * right-aligned a few points left of the primary
905 * margin. For other styles, the auxiliary text is
906 * the full chapter _name_ and takes up space
907 * within the (wrapped) chapter title, meaning that
908 * we must move the first line indent over to make
911 if (ptype
== para_Heading
|| ptype
== para_Subsect
) {
915 len
= paper_width_simple(pdata
, pkwtext2
);
916 aux_indent
= -len
- conf
->sect_num_left_space
;
918 pdata
->outline_title
=
919 prepare_outline_title(pkwtext2
, L
" ", pwords
);
922 aux2
= fake_word(L
": ");
925 firstline_indent
+= paper_width_simple(pdata
, aux
);
926 firstline_indent
+= paper_width_simple(pdata
, aux2
);
928 pdata
->outline_title
=
929 prepare_outline_title(pkwtext
, L
": ", pwords
);
935 * Auxiliary text consisting of a bullet. (FIXME:
936 * configurable bullet.)
938 aux
= fake_word(L
"\x2022");
939 aux_indent
= indent
+ conf
->indent_list_bullet
;
942 case para_NumberedList
:
944 * Auxiliary text consisting of the number followed
945 * by a (FIXME: configurable) full stop.
948 aux2
= fake_word(L
".");
949 aux_indent
= indent
+ conf
->indent_list_bullet
;
952 case para_BiblioCited
:
954 * Auxiliary text consisting of the bibliography
955 * reference text, and a trailing space.
958 aux2
= fake_word(L
" ");
960 firstline_indent
+= paper_width_simple(pdata
, aux
);
961 firstline_indent
+= paper_width_simple(pdata
, aux2
);
965 if (pdata
->outline_level
>= 0 && !pdata
->outline_title
) {
966 pdata
->outline_title
=
967 prepare_outline_title(NULL
, NULL
, pwords
);
970 wrap_paragraph(pdata
, pwords
, conf
->base_width
- rmargin
,
971 indent
+ firstline_indent
,
972 indent
+ extra_indent
);
974 pdata
->first
->aux_text
= aux
;
975 pdata
->first
->aux_text_2
= aux2
;
976 pdata
->first
->aux_left_indent
= aux_indent
;
979 * Line breaking penalties.
986 case para_UnnumberedChapter
:
988 * Fixed and large penalty for breaking straight
989 * after a heading; corresponding bonus for
990 * breaking straight before.
992 pdata
->first
->penalty_before
= -500000;
993 pdata
->last
->penalty_after
= 500000;
994 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
995 ldata
->penalty_after
= 500000;
998 case para_DescribedThing
:
1000 * This is treated a bit like a small heading:
1001 * there's a penalty for breaking after it (i.e.
1002 * between it and its description), and a bonus for
1003 * breaking before it (actually _between_ list
1006 pdata
->first
->penalty_before
= -200000;
1007 pdata
->last
->penalty_after
= 200000;
1012 * Most paragraph types: widow/orphan control by
1013 * discouraging breaking one line from the end of
1016 if (pdata
->first
!= pdata
->last
) {
1017 pdata
->first
->penalty_after
= 100000;
1018 pdata
->last
->penalty_before
= 100000;
1023 standard_line_spacing(pdata
, conf
);
1026 * Some kinds of section heading require a page break before
1027 * them and an underline after.
1029 if (ptype
== para_Title
||
1030 ptype
== para_Chapter
||
1031 ptype
== para_Appendix
||
1032 ptype
== para_UnnumberedChapter
) {
1033 pdata
->first
->page_break
= TRUE
;
1034 pdata
->first
->space_before
= conf
->chapter_top_space
;
1035 pdata
->last
->space_after
+=
1036 (conf
->chapter_underline_depth
+
1037 conf
->chapter_underline_thickness
);
1038 pdata
->rect_type
= RECT_CHAPTER_UNDERLINE
;
1044 static void standard_line_spacing(para_data
*pdata
, paper_conf
*conf
)
1049 * Set the line spacing for each line in this paragraph.
1051 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1052 if (ldata
== pdata
->first
)
1053 ldata
->space_before
= conf
->base_para_spacing
/ 2;
1055 ldata
->space_before
= conf
->base_leading
/ 2;
1056 if (ldata
== pdata
->last
)
1057 ldata
->space_after
= conf
->base_para_spacing
/ 2;
1059 ldata
->space_after
= conf
->base_leading
/ 2;
1060 ldata
->page_break
= FALSE
;
1064 static font_encoding
*new_font_encoding(font_data
*font
)
1069 fe
= mknew(font_encoding
);
1072 if (font
->list
->tail
)
1073 font
->list
->tail
->next
= fe
;
1075 font
->list
->head
= fe
;
1076 font
->list
->tail
= fe
;
1079 fe
->free_pos
= 0x21;
1081 for (i
= 0; i
< 256; i
++) {
1082 fe
->vector
[i
] = NULL
;
1083 fe
->indices
[i
] = -1;
1084 fe
->to_unicode
[i
] = 0xFFFF;
1090 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
1098 widths
= ps_std_font_widths(name
);
1102 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
1104 f
= mknew(font_data
);
1108 f
->nglyphs
= nglyphs
;
1109 f
->glyphs
= ps_std_glyphs
;
1111 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
1114 * Our first subfont will contain all of US-ASCII. This isn't
1115 * really necessary - we could just create custom subfonts
1116 * precisely as the whim of render_string dictated - but
1117 * instinct suggests that it might be nice to have the text in
1118 * the output files look _marginally_ recognisable.
1120 fe
= new_font_encoding(f
);
1121 fe
->free_pos
= 0xA1; /* only the top half is free */
1122 f
->latest_subfont
= fe
;
1124 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
1127 for (i
= 0; i
< nglyphs
; i
++) {
1129 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
1130 assert(ucs
!= 0xFFFF);
1132 if (ucs
>= 0x20 && ucs
<= 0x7E) {
1133 fe
->vector
[ucs
] = f
->glyphs
[i
];
1134 fe
->indices
[ucs
] = i
;
1135 fe
->to_unicode
[ucs
] = ucs
;
1136 f
->subfont_map
[i
].subfont
= fe
;
1137 f
->subfont_map
[i
].position
= ucs
;
1140 * This character is not yet assigned to a subfont.
1142 f
->subfont_map
[i
].subfont
= NULL
;
1143 f
->subfont_map
[i
].position
= 0;
1150 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
1157 for (; *string
; string
++) {
1160 index
= font
->bmp
[(unsigned short)*string
];
1161 if (index
== 0xFFFF) {
1165 width
+= font
->widths
[index
];
1172 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
1174 struct paper_width_ctx
{
1179 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
1181 while (text
&& text
!= end
) {
1182 w
+= paper_width_internal(vctx
, text
, nspaces
);
1188 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
1190 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
1191 int style
, type
, findex
, width
, errs
;
1194 switch (word
->type
) {
1195 case word_HyperLink
:
1197 case word_UpperXref
:
1198 case word_LowerXref
:
1205 style
= towordstyle(word
->type
);
1206 type
= removeattr(word
->type
);
1208 findex
= (style
== word_Normal ? FONT_NORMAL
:
1209 style
== word_Emph ? FONT_EMPH
:
1212 if (type
== word_Normal
) {
1214 } else if (type
== word_WhiteSpace
) {
1215 if (findex
!= FONT_CODE
) {
1218 return ctx
->minspacewidth
;
1221 } else /* if (type == word_Quote) */ {
1222 if (word
->aux
== quote_Open
)
1223 str
= L
"\x2018"; /* FIXME: configurability! */
1225 str
= L
"\x2019"; /* FIXME: configurability! */
1228 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
1230 if (errs
&& word
->alt
)
1231 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
1233 return ctx
->pdata
->sizes
[findex
] * width
;
1236 static int paper_width(void *vctx
, word
*word
)
1238 return paper_width_internal(vctx
, word
, NULL
);
1241 static int paper_width_simple(para_data
*pdata
, word
*text
)
1243 struct paper_width_ctx ctx
;
1247 (pdata
->sizes
[FONT_NORMAL
] *
1248 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1250 return paper_width_list(&ctx
, text
, NULL
, NULL
);
1253 static void wrap_paragraph(para_data
*pdata
, word
*words
,
1254 int w
, int i1
, int i2
)
1256 wrappedline
*wrapping
, *p
;
1258 struct paper_width_ctx ctx
;
1262 * We're going to need to store the line height in every line
1263 * structure we generate.
1268 for (i
= 0; i
< NFONTS
; i
++)
1269 if (line_height
< pdata
->sizes
[i
])
1270 line_height
= pdata
->sizes
[i
];
1271 line_height
*= 4096;
1274 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
1275 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
1276 if (spacewidth
== 0) {
1278 * A font without a space?! Disturbing. I hope this never
1279 * comes up, but I'll make a random guess anyway and set my
1280 * space width to half the point size.
1282 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
1286 * I'm going to set the _minimum_ space width to 3/5 of the
1287 * standard one, and use the standard one as the optimum.
1289 ctx
.minspacewidth
= spacewidth
* 3 / 5;
1292 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
1295 * Having done the wrapping, we now concoct a set of line_data
1298 pdata
->first
= pdata
->last
= NULL
;
1300 for (p
= wrapping
; p
; p
= p
->next
) {
1303 int len
, wid
, spaces
;
1305 ldata
= mknew(line_data
);
1307 ldata
->pdata
= pdata
;
1308 ldata
->first
= p
->begin
;
1309 ldata
->end
= p
->end
;
1310 ldata
->line_height
= line_height
;
1312 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
1315 pdata
->last
->next
= ldata
;
1316 ldata
->prev
= pdata
->last
;
1318 pdata
->first
= ldata
;
1322 pdata
->last
= ldata
;
1325 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
1326 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
1329 ldata
->hshortfall
= wid
- len
;
1330 ldata
->nspaces
= spaces
;
1332 * This tells us how much the space width needs to
1333 * change from _min_spacewidth. But we want to store
1334 * its difference from the _natural_ space width, to
1335 * make the text rendering easier.
1337 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
1338 ldata
->hshortfall
-= spacewidth
* spaces
;
1339 ldata
->real_shortfall
= ldata
->hshortfall
;
1341 * Special case: on the last line of a paragraph, we
1342 * never stretch spaces.
1344 if (ldata
->hshortfall
> 0 && !p
->next
)
1345 ldata
->hshortfall
= 0;
1347 ldata
->aux_text
= NULL
;
1348 ldata
->aux_text_2
= NULL
;
1349 ldata
->aux_left_indent
= 0;
1350 ldata
->penalty_before
= ldata
->penalty_after
= 0;
1355 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
1356 int page_height
, int ncols
, int headspace
)
1360 int n
, n1
, this_height
;
1363 * Page breaking is done by a close analogue of the optimal
1364 * paragraph wrapping algorithm used by wrap_para(). We work
1365 * backwards from the end of the document line by line; for
1366 * each line, we contemplate every possible number of lines we
1367 * could put on a page starting with that line, determine a
1368 * cost function for each one, add it to the pre-computed cost
1369 * function for optimally page-breaking everything after that
1370 * page, and pick the best option.
1372 * This is made slightly more complex by the fact that we have
1373 * a multi-column index with a heading at the top of the
1374 * _first_ page, meaning that the first _ncols_ pages must have
1375 * a different length. Hence, we must do the wrapping ncols+1
1376 * times over, hypothetically trying to put every subsequence
1377 * on every possible page.
1379 * Since my line_data structures are only used for this
1380 * purpose, I might as well just store the algorithm data
1384 for (l
= last
; l
; l
= l
->prev
) {
1385 l
->bestcost
= mknewa(int, ncols
+1);
1386 l
->vshortfall
= mknewa(int, ncols
+1);
1387 l
->text
= mknewa(int, ncols
+1);
1388 l
->space
= mknewa(int, ncols
+1);
1389 l
->page_last
= mknewa(line_data
*, ncols
+1);
1391 for (n
= 0; n
<= ncols
; n
++) {
1392 int minheight
, text
= 0, space
= 0;
1395 n1
= (n
< ncols ? n
+1 : ncols
);
1397 this_height
= page_height
- headspace
;
1399 this_height
= page_height
;
1401 l
->bestcost
[n
] = -1;
1402 for (m
= l
; m
; m
= m
->next
) {
1403 if (m
!= l
&& m
->page_break
)
1404 break; /* we've gone as far as we can */
1407 if (m
->prev
->space_after
> 0)
1408 space
+= m
->prev
->space_after
;
1410 text
+= m
->prev
->space_after
;
1412 if (m
!= l
|| m
->page_break
) {
1413 if (m
->space_before
> 0)
1414 space
+= m
->space_before
;
1416 text
+= m
->space_before
;
1418 text
+= m
->line_height
;
1419 minheight
= text
+ space
;
1421 if (m
!= l
&& minheight
> this_height
)
1425 * If the space after this paragraph is _negative_
1426 * (which means the next line is folded on to this
1427 * one, which happens in the index), we absolutely
1428 * cannot break here.
1430 if (m
->space_after
>= 0) {
1433 * Compute the cost of this arrangement, as the
1434 * square of the amount of wasted space on the
1435 * page. Exception: if this is the last page
1436 * before a mandatory break or the document
1437 * end, we don't penalise a large blank area.
1439 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1441 int x
= this_height
- minheight
;
1448 cost
+= (x
* xf
) >> 8;
1452 if (m
!= last
&& m
->next
&& !m
->next
->page_break
) {
1453 cost
+= m
->penalty_after
;
1454 cost
+= m
->next
->penalty_before
;
1457 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1458 cost
+= m
->next
->bestcost
[n1
];
1459 if (l
->bestcost
[n
] == -1 || l
->bestcost
[n
] > cost
) {
1461 * This is the best option yet for this
1464 l
->bestcost
[n
] = cost
;
1465 if (m
!= last
&& m
->next
&& !m
->next
->page_break
)
1466 l
->vshortfall
[n
] = this_height
- minheight
;
1468 l
->vshortfall
[n
] = 0;
1470 l
->space
[n
] = space
;
1471 l
->page_last
[n
] = m
;
1482 * Now go through the line list forwards and assemble the
1491 int text
, space
, head
;
1493 page
= mknew(page_data
);
1502 page
->first_line
= l
;
1503 page
->last_line
= l
->page_last
[n
];
1505 page
->first_text
= page
->last_text
= NULL
;
1506 page
->first_xref
= page
->last_xref
= NULL
;
1507 page
->first_rect
= page
->last_rect
= NULL
;
1510 * Now assign a y-coordinate to each line on the page.
1513 head
= (n
< ncols ? headspace
: 0);
1514 for (l
= page
->first_line
; l
; l
= l
->next
) {
1515 if (l
!= page
->first_line
) {
1516 if (l
->prev
->space_after
> 0)
1517 space
+= l
->prev
->space_after
;
1519 text
+= l
->prev
->space_after
;
1521 if (l
!= page
->first_line
|| l
->page_break
) {
1522 if (l
->space_before
> 0)
1523 space
+= l
->space_before
;
1525 text
+= l
->space_before
;
1527 text
+= l
->line_height
;
1530 l
->ypos
= text
+ space
+ head
+
1531 space
* (float)page
->first_line
->vshortfall
[n
] /
1532 page
->first_line
->space
[n
];
1534 if (l
== page
->last_line
)
1538 l
= page
->last_line
;
1543 n
= (n
< ncols ? n
+1 : ncols
);
1549 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
1551 rect
*r
= mknew(rect
);
1554 if (page
->last_rect
)
1555 page
->last_rect
->next
= r
;
1557 page
->first_rect
= r
;
1558 page
->last_rect
= r
;
1566 static void add_string_to_page(page_data
*page
, int x
, int y
,
1567 font_encoding
*fe
, int size
, char *text
)
1569 text_fragment
*frag
;
1571 frag
= mknew(text_fragment
);
1574 if (page
->last_text
)
1575 page
->last_text
->next
= frag
;
1577 page
->first_text
= frag
;
1578 page
->last_text
= frag
;
1583 frag
->fontsize
= size
;
1584 frag
->text
= dupstr(text
);
1588 * Returns the updated x coordinate.
1590 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
1591 int x
, int y
, wchar_t *str
)
1594 int textpos
, textwid
, glyph
;
1595 font_encoding
*subfont
= NULL
, *sf
;
1597 text
= mknewa(char, 1 + ustrlen(str
));
1598 textpos
= textwid
= 0;
1601 glyph
= font
->bmp
[*str
];
1603 if (glyph
== 0xFFFF)
1604 continue; /* nothing more we can do here */
1607 * Find which subfont this character is going in.
1609 sf
= font
->subfont_map
[glyph
].subfont
;
1615 * This character is not yet in a subfont. Assign one.
1617 if (font
->latest_subfont
->free_pos
>= 0x100)
1618 font
->latest_subfont
= new_font_encoding(font
);
1620 c
= font
->latest_subfont
->free_pos
++;
1621 if (font
->latest_subfont
->free_pos
== 0x7F)
1622 font
->latest_subfont
->free_pos
= 0xA1;
1624 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1625 font
->subfont_map
[glyph
].position
= c
;
1626 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1627 font
->latest_subfont
->indices
[c
] = glyph
;
1628 font
->latest_subfont
->to_unicode
[c
] = *str
;
1630 sf
= font
->latest_subfont
;
1633 if (!subfont
|| sf
!= subfont
) {
1635 text
[textpos
] = '\0';
1636 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1639 assert(textpos
== 0);
1645 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1646 textwid
+= font
->widths
[glyph
] * fontsize
;
1652 text
[textpos
] = '\0';
1653 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1661 * Returns the updated x coordinate.
1663 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1664 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1665 int shortfall
, int nspaces
, int *nspace
,
1666 keywordlist
*keywords
, indexdata
*idx
)
1668 while (text
&& text
!= text_end
) {
1669 int style
, type
, findex
, errs
;
1673 switch (text
->type
) {
1675 * Start a cross-reference.
1677 case word_HyperLink
:
1678 case word_UpperXref
:
1679 case word_LowerXref
:
1682 if (text
->type
== word_HyperLink
) {
1684 dest
.url
= utoa_dup(text
->text
);
1686 } else if (text
->type
== word_PageXref
) {
1689 dest
.page
= (page_data
*)text
->private_data
;
1691 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1695 assert(kwl
->para
->private_data
);
1696 pdata
= (para_data
*) kwl
->para
->private_data
;
1698 dest
.page
= pdata
->first
->page
;
1702 * Shouldn't happen, but *shrug*
1709 if (dest
.type
!= NONE
) {
1711 (*xr
)->dest
= dest
; /* structure copy */
1712 if (page
->last_xref
)
1713 page
->last_xref
->next
= *xr
;
1715 page
->first_xref
= *xr
;
1716 page
->last_xref
= *xr
;
1720 * FIXME: Ideally we should have, and use, some
1721 * vertical font metric information here so that
1722 * our cross-ref rectangle can take account of
1723 * descenders and the font's cap height. This will
1724 * do for the moment, but it isn't ideal.
1726 (*xr
)->lx
= (*xr
)->rx
= x
;
1728 (*xr
)->ty
= y
+ ldata
->line_height
;
1733 * Finish extending a cross-reference box.
1741 * Add the current page number to the list of pages
1742 * referenced by an index entry.
1749 tag
= index_findtag(idx
, text
->text
);
1753 for (i
= 0; i
< tag
->nrefs
; i
++) {
1754 indexentry
*entry
= tag
->refs
[i
];
1755 paper_idx
*pi
= (paper_idx
*)entry
->backend_data
;
1758 * If the same index term is indexed twice
1759 * within the same section, we only want to
1760 * mention it once in the index.
1762 if (pi
->lastpage
!= page
) {
1766 pi
->lastword
= pi
->lastword
->next
=
1768 pi
->lastword
= pi
->lastword
->next
=
1770 wp
= &pi
->lastword
->next
;
1774 pi
->lastword
= *wp
=
1775 fake_page_ref(page
);
1776 pi
->lastword
= pi
->lastword
->next
=
1777 fake_word(page
->number
);
1778 pi
->lastword
= pi
->lastword
->next
=
1782 pi
->lastpage
= page
;
1788 style
= towordstyle(text
->type
);
1789 type
= removeattr(text
->type
);
1791 findex
= (style
== word_Normal ? FONT_NORMAL
:
1792 style
== word_Emph ? FONT_EMPH
:
1795 if (type
== word_Normal
) {
1797 } else if (type
== word_WhiteSpace
) {
1798 x
+= pdata
->sizes
[findex
] *
1799 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1800 if (nspaces
&& findex
!= FONT_CODE
) {
1801 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1802 x
-= *nspace
* shortfall
/ nspaces
;
1806 } else /* if (type == word_Quote) */ {
1807 if (text
->aux
== quote_Open
)
1808 str
= L
"\x2018"; /* FIXME: configurability! */
1810 str
= L
"\x2019"; /* FIXME: configurability! */
1813 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1815 if (errs
&& text
->alt
)
1816 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1817 xr
, shortfall
, nspaces
, nspace
, keywords
, idx
);
1819 x
= render_string(page
, pdata
->fonts
[findex
],
1820 pdata
->sizes
[findex
], x
, y
, str
);
1833 * Returns the last x position used on the line.
1835 static int render_line(line_data
*ldata
, int left_x
, int top_y
,
1836 xref_dest
*dest
, keywordlist
*keywords
, indexdata
*idx
)
1842 if (ldata
->aux_text
) {
1846 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1847 left_x
+ ldata
->aux_left_indent
,
1848 top_y
- ldata
->ypos
,
1849 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
,
1851 if (ldata
->aux_text_2
)
1852 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1853 x
, top_y
- ldata
->ypos
,
1854 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
,
1861 * There might be a cross-reference carried over from a
1864 if (dest
->type
!= NONE
) {
1867 xr
->dest
= *dest
; /* structure copy */
1868 if (ldata
->page
->last_xref
)
1869 ldata
->page
->last_xref
->next
= xr
;
1871 ldata
->page
->first_xref
= xr
;
1872 ldata
->page
->last_xref
= xr
;
1873 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1874 xr
->by
= top_y
- ldata
->ypos
;
1875 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1880 int extra_indent
, shortfall
, spaces
;
1881 int just
= ldata
->pdata
->justification
;
1884 * All forms of justification become JUST when we have
1885 * to squeeze the paragraph.
1887 if (ldata
->hshortfall
< 0)
1892 shortfall
= ldata
->hshortfall
;
1893 spaces
= ldata
->nspaces
;
1897 shortfall
= spaces
= extra_indent
= 0;
1900 shortfall
= spaces
= 0;
1901 extra_indent
= ldata
->real_shortfall
;
1905 ret
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1906 left_x
+ ldata
->xpos
+ extra_indent
,
1907 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
,
1908 &xr
, shortfall
, spaces
, &nspace
,
1914 * There's a cross-reference continued on to the next line.
1924 static void render_para(para_data
*pdata
, paper_conf
*conf
,
1925 keywordlist
*keywords
, indexdata
*idx
,
1926 paragraph
*index_placeholder
, page_data
*index_page
)
1930 page_data
*cxref_page
;
1939 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
1941 * If this is a contents entry, we expect to have a single
1942 * enormous cross-reference rectangle covering the whole
1943 * thing. (Unless, of course, it spans multiple pages.)
1945 if (pdata
->contents_entry
&& ldata
->page
!= cxref_page
) {
1946 cxref_page
= ldata
->page
;
1947 cxref
= mknew(xref
);
1949 cxref
->dest
.type
= PAGE
;
1950 if (pdata
->contents_entry
== index_placeholder
) {
1951 cxref
->dest
.page
= index_page
;
1953 assert(pdata
->contents_entry
->private_data
);
1954 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1955 cxref
->dest
.page
= target
->first
->page
;
1957 cxref
->dest
.url
= NULL
;
1958 if (ldata
->page
->last_xref
)
1959 ldata
->page
->last_xref
->next
= cxref
;
1961 ldata
->page
->first_xref
= cxref
;
1962 ldata
->page
->last_xref
= cxref
;
1963 cxref
->lx
= conf
->left_margin
;
1964 cxref
->rx
= conf
->paper_width
- conf
->right_margin
;
1965 cxref
->ty
= conf
->paper_height
- conf
->top_margin
1966 - ldata
->ypos
+ ldata
->line_height
;
1968 if (pdata
->contents_entry
) {
1969 assert(cxref
!= NULL
);
1970 cxref
->by
= conf
->paper_height
- conf
->top_margin
1974 last_x
= render_line(ldata
, conf
->left_margin
,
1975 conf
->paper_height
- conf
->top_margin
,
1976 &dest
, keywords
, idx
);
1977 if (ldata
== pdata
->last
)
1982 * If this is a contents entry, add leaders and a page
1985 if (pdata
->contents_entry
) {
1991 if (pdata
->contents_entry
== index_placeholder
) {
1992 num
= index_page
->number
;
1994 assert(pdata
->contents_entry
->private_data
);
1995 target
= (para_data
*)pdata
->contents_entry
->private_data
;
1996 num
= target
->first
->page
->number
;
2000 wid
= paper_width_simple(pdata
, w
);
2003 render_string(pdata
->last
->page
,
2004 pdata
->fonts
[FONT_NORMAL
],
2005 pdata
->sizes
[FONT_NORMAL
],
2006 conf
->paper_width
- conf
->right_margin
- wid
,
2007 (conf
->paper_height
- conf
->top_margin
-
2008 pdata
->last
->ypos
), num
);
2010 for (x
= 0; x
< conf
->base_width
; x
+= conf
->leader_separation
)
2011 if (x
- conf
->leader_separation
> last_x
- conf
->left_margin
&&
2012 x
+ conf
->leader_separation
< conf
->base_width
- wid
)
2013 render_string(pdata
->last
->page
,
2014 pdata
->fonts
[FONT_NORMAL
],
2015 pdata
->sizes
[FONT_NORMAL
],
2016 conf
->left_margin
+ x
,
2017 (conf
->paper_height
- conf
->top_margin
-
2018 pdata
->last
->ypos
), L
".");
2022 * Render any rectangle (chapter title underline or rule)
2023 * that goes with this paragraph.
2025 switch (pdata
->rect_type
) {
2026 case RECT_CHAPTER_UNDERLINE
:
2027 add_rect_to_page(pdata
->last
->page
,
2029 (conf
->paper_height
- conf
->top_margin
-
2031 conf
->chapter_underline_depth
),
2033 conf
->chapter_underline_thickness
);
2036 add_rect_to_page(pdata
->first
->page
,
2037 conf
->left_margin
+ pdata
->first
->xpos
,
2038 (conf
->paper_height
- conf
->top_margin
-
2040 pdata
->last
->line_height
),
2041 conf
->base_width
- pdata
->first
->xpos
,
2042 pdata
->last
->line_height
);
2044 default: /* placate gcc */
2049 static para_data
*code_paragraph(int indent
, word
*words
, paper_conf
*conf
)
2051 para_data
*pdata
= mknew(para_data
);
2054 * For code paragraphs, I'm going to hack grievously and
2055 * pretend the three normal fonts are the three code paragraph
2058 pdata
->fonts
[FONT_NORMAL
] = conf
->cb
;
2059 pdata
->fonts
[FONT_EMPH
] = conf
->co
;
2060 pdata
->fonts
[FONT_CODE
] = conf
->cr
;
2061 pdata
->sizes
[FONT_NORMAL
] =
2062 pdata
->sizes
[FONT_EMPH
] =
2063 pdata
->sizes
[FONT_CODE
] = 12;
2065 pdata
->first
= pdata
->last
= NULL
;
2066 pdata
->outline_level
= -1;
2067 pdata
->rect_type
= RECT_NONE
;
2068 pdata
->contents_entry
= NULL
;
2069 pdata
->justification
= LEFT
;
2071 for (; words
; words
= words
->next
) {
2072 wchar_t *t
, *e
, *start
;
2073 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
2075 int prev
= -1, curr
;
2078 if (words
->next
&& words
->next
->type
== word_Emph
) {
2079 e
= words
->next
->text
;
2080 words
= words
->next
;
2090 else if (*e
== L
'i')
2092 else if (*e
== L
'b')
2109 * We've isolated a maximal subsequence of the line
2110 * which has the same emphasis. Form it into a word
2116 w
->type
= (prev
== 0 ? word_WeakCode
:
2117 prev
== 1 ? word_Emph
: word_Normal
);
2118 w
->text
= mknewa(wchar_t, t
-start
+1);
2119 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
2120 w
->text
[t
-start
] = '\0';
2133 ldata
= mknew(line_data
);
2135 ldata
->pdata
= pdata
;
2136 ldata
->first
= lhead
;
2138 ldata
->line_height
= conf
->base_font_size
* 4096;
2140 ldata
->xpos
= indent
;
2143 pdata
->last
->next
= ldata
;
2144 ldata
->prev
= pdata
->last
;
2146 pdata
->first
= ldata
;
2150 pdata
->last
= ldata
;
2152 ldata
->hshortfall
= 0;
2154 ldata
->aux_text
= NULL
;
2155 ldata
->aux_text_2
= NULL
;
2156 ldata
->aux_left_indent
= 0;
2157 /* General opprobrium for breaking in a code paragraph. */
2158 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
2161 standard_line_spacing(pdata
, conf
);
2166 static para_data
*rule_paragraph(int indent
, paper_conf
*conf
)
2168 para_data
*pdata
= mknew(para_data
);
2171 ldata
= mknew(line_data
);
2173 ldata
->pdata
= pdata
;
2174 ldata
->first
= NULL
;
2176 ldata
->line_height
= conf
->rule_thickness
;
2178 ldata
->xpos
= indent
;
2183 ldata
->hshortfall
= 0;
2185 ldata
->aux_text
= NULL
;
2186 ldata
->aux_text_2
= NULL
;
2187 ldata
->aux_left_indent
= 0;
2190 * Better to break after a rule than before it
2192 ldata
->penalty_after
+= 100000;
2193 ldata
->penalty_before
+= -100000;
2195 pdata
->first
= pdata
->last
= ldata
;
2196 pdata
->outline_level
= -1;
2197 pdata
->rect_type
= RECT_RULE
;
2198 pdata
->contents_entry
= NULL
;
2199 pdata
->justification
= LEFT
;
2201 standard_line_spacing(pdata
, conf
);
2207 * Plain-text-like formatting for outline titles.
2209 static void paper_rdaddw(rdstring
*rs
, word
*text
) {
2210 for (; text
; text
= text
->next
) switch (text
->type
) {
2211 case word_HyperLink
:
2213 case word_UpperXref
:
2214 case word_LowerXref
:
2223 case word_WhiteSpace
:
2224 case word_EmphSpace
:
2225 case word_CodeSpace
:
2226 case word_WkCodeSpace
:
2228 case word_EmphQuote
:
2229 case word_CodeQuote
:
2230 case word_WkCodeQuote
:
2231 assert(text
->type
!= word_CodeQuote
&&
2232 text
->type
!= word_WkCodeQuote
);
2233 if (towordstyle(text
->type
) == word_Emph
&&
2234 (attraux(text
->aux
) == attr_First
||
2235 attraux(text
->aux
) == attr_Only
))
2236 rdadd(rs
, L
'_'); /* FIXME: configurability */
2237 else if (towordstyle(text
->type
) == word_Code
&&
2238 (attraux(text
->aux
) == attr_First
||
2239 attraux(text
->aux
) == attr_Only
))
2240 rdadd(rs
, L
'\''); /* FIXME: configurability */
2241 if (removeattr(text
->type
) == word_Normal
) {
2242 rdadds(rs
, text
->text
);
2243 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
2245 } else if (removeattr(text
->type
) == word_Quote
) {
2246 rdadd(rs
, L
'\''); /* fixme: configurability */
2248 if (towordstyle(text
->type
) == word_Emph
&&
2249 (attraux(text
->aux
) == attr_Last
||
2250 attraux(text
->aux
) == attr_Only
))
2251 rdadd(rs
, L
'_'); /* FIXME: configurability */
2252 else if (towordstyle(text
->type
) == word_Code
&&
2253 (attraux(text
->aux
) == attr_Last
||
2254 attraux(text
->aux
) == attr_Only
))
2255 rdadd(rs
, L
'\''); /* FIXME: configurability */
2260 static wchar_t *prepare_outline_title(word
*first
, wchar_t *separator
,
2263 rdstring rs
= {0, 0, NULL
};
2266 paper_rdaddw(&rs
, first
);
2268 rdadds(&rs
, separator
);
2270 paper_rdaddw(&rs
, second
);
2275 static word
*fake_word(wchar_t *text
)
2277 word
*ret
= mknew(word
);
2280 ret
->type
= word_Normal
;
2281 ret
->text
= ustrdup(text
);
2282 ret
->breaks
= FALSE
;
2287 static word
*fake_space_word(void)
2289 word
*ret
= mknew(word
);
2292 ret
->type
= word_WhiteSpace
;
2299 static word
*fake_page_ref(page_data
*page
)
2301 word
*ret
= mknew(word
);
2304 ret
->type
= word_PageXref
;
2306 ret
->breaks
= FALSE
;
2308 ret
->private_data
= page
;
2312 static word
*fake_end_ref(void)
2314 word
*ret
= mknew(word
);
2317 ret
->type
= word_XrefEnd
;
2319 ret
->breaks
= FALSE
;
2324 static word
*prepare_contents_title(word
*first
, wchar_t *separator
,
2333 w
= dup_word_list(first
);
2341 w
= fake_word(separator
);
2347 *wptr
= dup_word_list(second
);
2353 static void fold_into_page(page_data
*dest
, page_data
*src
, int right_shift
)
2357 if (!src
->first_line
)
2360 if (dest
->last_line
) {
2361 dest
->last_line
->next
= src
->first_line
;
2362 src
->first_line
->prev
= dest
->last_line
;
2364 dest
->last_line
= src
->last_line
;
2366 for (ldata
= src
->first_line
; ldata
; ldata
= ldata
->next
) {
2368 ldata
->xpos
+= right_shift
;
2370 if (ldata
== src
->last_line
)