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 * - implement some simple graphics
16 * * I had an underline below chapter headings in the original
17 * Perl version, and I thought it looked rather nice
18 * * also we need para_Rule.
20 * - set up contents section now we know what sections begin on
27 * - header/footer? Page numbers at least would be handy. Fully
28 * configurable footer can wait, though.
30 * That should bring us to the same level of functionality that
31 * original-Halibut had, and the same in PDF plus the obvious
32 * interactive navigation features. After that, in future work:
34 * - linearised PDF, perhaps?
36 * - I'm uncertain of whether I need to include a ToUnicode CMap
37 * in each of my font definitions in PDF. Currently things (by
38 * which I mean cut and paste out of acroread) seem to be
39 * working fairly happily without it, but I don't know.
52 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
53 static void wrap_paragraph(para_data
*pdata
, word
*words
,
54 int w
, int i1
, int i2
);
55 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
57 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
58 xref_dest
*dest
, keywordlist
*keywords
);
59 static int paper_width_simple(para_data
*pdata
, word
*text
);
60 static void code_paragraph(para_data
*pdata
,
61 font_data
*fn
, font_data
*fi
, font_data
*fb
,
62 int font_size
, int indent
, word
*words
);
64 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
68 int indent
, extra_indent
, firstline_indent
, aux_indent
;
70 line_data
*ldata
, *firstline
, *lastline
;
71 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
77 * FIXME: All these things ought to become configurable.
79 int paper_width
= 595 * 4096;
80 int paper_height
= 841 * 4096;
81 int left_margin
= 72 * 4096;
82 int top_margin
= 72 * 4096;
83 int right_margin
= 72 * 4096;
84 int bottom_margin
= 108 * 4096;
85 int indent_list_bullet
= 6 * 4096;
86 int indent_list
= 24 * 4096;
87 int indent_quote
= 18 * 4096;
88 int base_leading
= 4096;
89 int base_para_spacing
= 10 * 4096;
90 int chapter_top_space
= 72 * 4096;
91 int sect_num_left_space
= 12 * 4096;
93 int base_width
= paper_width
- left_margin
- right_margin
;
94 int page_height
= paper_height
- top_margin
- bottom_margin
;
96 IGNORE(idx
); /* FIXME */
99 * First, set up some font structures.
101 fontlist
= mknew(font_list
);
102 fontlist
->head
= fontlist
->tail
= NULL
;
103 tr
= make_std_font(fontlist
, "Times-Roman");
104 ti
= make_std_font(fontlist
, "Times-Italic");
105 hr
= make_std_font(fontlist
, "Helvetica-Bold");
106 hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
107 cr
= make_std_font(fontlist
, "Courier");
108 co
= make_std_font(fontlist
, "Courier-Oblique");
109 cb
= make_std_font(fontlist
, "Courier-Bold");
112 * Go through and break up each paragraph into lines.
115 firstline
= lastline
= NULL
;
116 for (p
= sourceform
; p
; p
= p
->next
) {
117 p
->private_data
= NULL
;
121 * These paragraph types are either invisible or don't
122 * define text in the normal sense. Either way, they
123 * don't require wrapping.
129 case para_NotParaType
:
136 * These paragraph types don't require wrapping, but
137 * they do affect the line width to which we wrap the
138 * rest of the paragraphs, so we need to pay attention.
141 indent
+= indent_list
; break;
143 indent
-= indent_list
; assert(indent
>= 0); break;
145 indent
+= indent_quote
; break;
147 indent
-= indent_quote
; assert(indent
>= 0); break;
150 * This paragraph type is special. Process it
154 pdata
= mknew(para_data
);
155 code_paragraph(pdata
, cr
, co
, cb
, 12, indent
, p
->words
);
156 p
->private_data
= pdata
;
157 if (pdata
->first
!= pdata
->last
) {
158 pdata
->first
->penalty_after
+= 100000;
159 pdata
->last
->penalty_before
+= 100000;
164 * All of these paragraph types require wrapping in the
165 * ordinary way. So we must supply a set of fonts, a
166 * line width and auxiliary information (e.g. bullet
167 * text) for each one.
171 case para_UnnumberedChapter
:
175 case para_BiblioCited
:
177 case para_NumberedList
:
178 case para_DescribedThing
:
179 case para_Description
:
182 pdata
= mknew(para_data
);
185 * Choose fonts for this paragraph.
187 * FIXME: All of this ought to be completely
192 pdata
->fonts
[FONT_NORMAL
] = hr
;
193 pdata
->sizes
[FONT_NORMAL
] = 24;
194 pdata
->fonts
[FONT_EMPH
] = hi
;
195 pdata
->sizes
[FONT_EMPH
] = 24;
196 pdata
->fonts
[FONT_CODE
] = cb
;
197 pdata
->sizes
[FONT_CODE
] = 24;
202 case para_UnnumberedChapter
:
203 pdata
->fonts
[FONT_NORMAL
] = hr
;
204 pdata
->sizes
[FONT_NORMAL
] = 20;
205 pdata
->fonts
[FONT_EMPH
] = hi
;
206 pdata
->sizes
[FONT_EMPH
] = 20;
207 pdata
->fonts
[FONT_CODE
] = cb
;
208 pdata
->sizes
[FONT_CODE
] = 20;
213 pdata
->fonts
[FONT_NORMAL
] = hr
;
214 pdata
->fonts
[FONT_EMPH
] = hi
;
215 pdata
->fonts
[FONT_CODE
] = cb
;
216 pdata
->sizes
[FONT_NORMAL
] =
217 pdata
->sizes
[FONT_EMPH
] =
218 pdata
->sizes
[FONT_CODE
] =
219 (p
->aux
== 0 ?
16 : p
->aux
== 1 ?
14 : 13);
223 case para_BiblioCited
:
225 case para_NumberedList
:
226 case para_DescribedThing
:
227 case para_Description
:
229 pdata
->fonts
[FONT_NORMAL
] = tr
;
230 pdata
->sizes
[FONT_NORMAL
] = 12;
231 pdata
->fonts
[FONT_EMPH
] = ti
;
232 pdata
->sizes
[FONT_EMPH
] = 12;
233 pdata
->fonts
[FONT_CODE
] = cr
;
234 pdata
->sizes
[FONT_CODE
] = 12;
239 * Also select an indentation level depending on the
240 * paragraph type (list paragraphs other than
241 * para_DescribedThing need extra indent).
243 * (FIXME: Perhaps at some point we might even arrange
244 * for the user to be able to request indented first
245 * lines in paragraphs.)
247 if (p
->type
== para_Bullet
||
248 p
->type
== para_NumberedList
||
249 p
->type
== para_Description
) {
250 extra_indent
= firstline_indent
= indent_list
;
252 extra_indent
= firstline_indent
= 0;
256 * Find the auxiliary text for this paragraph.
267 * For some heading styles (FIXME: be able to
268 * configure which), the auxiliary text contains
269 * the chapter number and is arranged to be
270 * right-aligned a few points left of the primary
271 * margin. For other styles, the auxiliary text is
272 * the full chapter _name_ and takes up space
273 * within the (wrapped) chapter title, meaning that
274 * we must move the first line indent over to make
277 if (p
->type
== para_Heading
|| p
->type
== para_Subsect
) {
281 len
= paper_width_simple(pdata
, p
->kwtext2
);
282 aux_indent
= -len
- sect_num_left_space
;
288 aux2
->type
= word_Normal
;
289 aux2
->text
= ustrdup(L
": ");
290 aux2
->breaks
= FALSE
;
294 firstline_indent
+= paper_width_simple(pdata
, aux
);
295 firstline_indent
+= paper_width_simple(pdata
, aux2
);
301 * Auxiliary text consisting of a bullet. (FIXME:
302 * configurable bullet.)
307 aux
->type
= word_Normal
;
308 aux
->text
= ustrdup(L
"\x2022");
311 aux_indent
= indent
+ indent_list_bullet
;
314 case para_NumberedList
:
316 * Auxiliary text consisting of the number followed
317 * by a (FIXME: configurable) full stop.
323 aux2
->type
= word_Normal
;
324 aux2
->text
= ustrdup(L
".");
325 aux2
->breaks
= FALSE
;
327 aux_indent
= indent
+ indent_list_bullet
;
330 case para_BiblioCited
:
332 * Auxiliary text consisting of the bibliography
333 * reference text, and a trailing space.
339 aux2
->type
= word_Normal
;
340 aux2
->text
= ustrdup(L
" ");
341 aux2
->breaks
= FALSE
;
344 firstline_indent
+= paper_width_simple(pdata
, aux
);
345 firstline_indent
+= paper_width_simple(pdata
, aux2
);
349 wrap_paragraph(pdata
, p
->words
, base_width
,
350 indent
+ firstline_indent
,
351 indent
+ extra_indent
);
353 p
->private_data
= pdata
;
355 pdata
->first
->aux_text
= aux
;
356 pdata
->first
->aux_text_2
= aux2
;
357 pdata
->first
->aux_left_indent
= aux_indent
;
360 * Line breaking penalties.
367 case para_UnnumberedChapter
:
369 * Fixed and large penalty for breaking straight
370 * after a heading; corresponding bonus for
371 * breaking straight before.
373 pdata
->first
->penalty_before
= -500000;
374 pdata
->last
->penalty_after
= 500000;
375 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
376 ldata
->penalty_after
= 500000;
379 case para_DescribedThing
:
381 * This is treated a bit like a small heading:
382 * there's a penalty for breaking after it (i.e.
383 * between it and its description), and a bonus for
384 * breaking before it (actually _between_ list
387 pdata
->first
->penalty_before
= -200000;
388 pdata
->last
->penalty_after
= 200000;
393 * Most paragraph types: widow/orphan control by
394 * discouraging breaking one line from the end of
397 if (pdata
->first
!= pdata
->last
) {
398 pdata
->first
->penalty_after
= 100000;
399 pdata
->last
->penalty_before
= 100000;
407 if (p
->private_data
) {
408 pdata
= (para_data
*)p
->private_data
;
411 * Set the line spacing for each line in this paragraph.
413 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
414 if (ldata
== pdata
->first
)
415 ldata
->space_before
= base_para_spacing
/ 2;
417 ldata
->space_before
= base_leading
/ 2;
418 if (ldata
== pdata
->last
)
419 ldata
->space_after
= base_para_spacing
/ 2;
421 ldata
->space_after
= base_leading
/ 2;
422 ldata
->page_break
= FALSE
;
426 * Some kinds of section heading do require a page
429 if (p
->type
== para_Title
||
430 p
->type
== para_Chapter
||
431 p
->type
== para_Appendix
||
432 p
->type
== para_UnnumberedChapter
) {
433 pdata
->first
->page_break
= TRUE
;
434 pdata
->first
->space_before
= chapter_top_space
;
438 * Link all line structures together into a big list.
442 lastline
->next
= pdata
->first
;
443 pdata
->first
->prev
= lastline
;
445 firstline
= pdata
->first
;
446 pdata
->first
->prev
= NULL
;
448 lastline
= pdata
->last
;
454 * Now we have an enormous linked list of every line of text in
455 * the document. Break it up into pages.
457 pages
= page_breaks(firstline
, lastline
, page_height
);
460 * Now we're ready to actually lay out the pages. We do this by
461 * looping over _paragraphs_, since we may need to track cross-
462 * references between lines and even across pages.
464 for (p
= sourceform
; p
; p
= p
->next
) {
465 pdata
= (para_data
*)p
->private_data
;
470 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
471 render_line(ldata
, left_margin
, paper_height
- top_margin
,
473 if (ldata
== pdata
->last
)
479 doc
= mknew(document
);
480 doc
->fonts
= fontlist
;
482 doc
->paper_width
= paper_width
;
483 doc
->paper_height
= paper_height
;
487 static font_encoding
*new_font_encoding(font_data
*font
)
492 fe
= mknew(font_encoding
);
495 if (font
->list
->tail
)
496 font
->list
->tail
->next
= fe
;
498 font
->list
->head
= fe
;
499 font
->list
->tail
= fe
;
504 for (i
= 0; i
< 256; i
++) {
505 fe
->vector
[i
] = NULL
;
507 fe
->to_unicode
[i
] = 0xFFFF;
513 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
521 widths
= ps_std_font_widths(name
);
525 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
527 f
= mknew(font_data
);
531 f
->nglyphs
= nglyphs
;
532 f
->glyphs
= ps_std_glyphs
;
534 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
537 * Our first subfont will contain all of US-ASCII. This isn't
538 * really necessary - we could just create custom subfonts
539 * precisely as the whim of render_string dictated - but
540 * instinct suggests that it might be nice to have the text in
541 * the output files look _marginally_ recognisable.
543 fe
= new_font_encoding(f
);
544 fe
->free_pos
= 0xA1; /* only the top half is free */
545 f
->latest_subfont
= fe
;
547 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
550 for (i
= 0; i
< nglyphs
; i
++) {
552 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
553 assert(ucs
!= 0xFFFF);
555 if (ucs
>= 0x20 && ucs
<= 0x7E) {
556 fe
->vector
[ucs
] = f
->glyphs
[i
];
557 fe
->indices
[ucs
] = i
;
558 fe
->to_unicode
[ucs
] = ucs
;
559 f
->subfont_map
[i
].subfont
= fe
;
560 f
->subfont_map
[i
].position
= ucs
;
563 * This character is not yet assigned to a subfont.
565 f
->subfont_map
[i
].subfont
= NULL
;
566 f
->subfont_map
[i
].position
= 0;
573 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
580 for (; *string
; string
++) {
583 index
= font
->bmp
[(unsigned short)*string
];
584 if (index
== 0xFFFF) {
588 width
+= font
->widths
[index
];
595 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
597 struct paper_width_ctx
{
602 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
604 while (text
&& text
!= end
) {
605 w
+= paper_width_internal(vctx
, text
, nspaces
);
611 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
613 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
614 int style
, type
, findex
, width
, errs
;
617 switch (word
->type
) {
627 style
= towordstyle(word
->type
);
628 type
= removeattr(word
->type
);
630 findex
= (style
== word_Normal ? FONT_NORMAL
:
631 style
== word_Emph ? FONT_EMPH
:
634 if (type
== word_Normal
) {
636 } else if (type
== word_WhiteSpace
) {
637 if (findex
!= FONT_CODE
) {
640 return ctx
->minspacewidth
;
643 } else /* if (type == word_Quote) */ {
644 if (word
->aux
== quote_Open
)
645 str
= L
"\x2018"; /* FIXME: configurability! */
647 str
= L
"\x2019"; /* FIXME: configurability! */
650 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
652 if (errs
&& word
->alt
)
653 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
655 return ctx
->pdata
->sizes
[findex
] * width
;
658 static int paper_width(void *vctx
, word
*word
)
660 return paper_width_internal(vctx
, word
, NULL
);
663 static int paper_width_simple(para_data
*pdata
, word
*text
)
665 struct paper_width_ctx ctx
;
669 (pdata
->sizes
[FONT_NORMAL
] *
670 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
672 return paper_width_list(&ctx
, text
, NULL
, NULL
);
675 static void wrap_paragraph(para_data
*pdata
, word
*words
,
676 int w
, int i1
, int i2
)
678 wrappedline
*wrapping
, *p
;
680 struct paper_width_ctx ctx
;
684 * We're going to need to store the line height in every line
685 * structure we generate.
690 for (i
= 0; i
< NFONTS
; i
++)
691 if (line_height
< pdata
->sizes
[i
])
692 line_height
= pdata
->sizes
[i
];
696 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
697 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
698 if (spacewidth
== 0) {
700 * A font without a space?! Disturbing. I hope this never
701 * comes up, but I'll make a random guess anyway and set my
702 * space width to half the point size.
704 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
708 * I'm going to set the _minimum_ space width to 3/5 of the
709 * standard one, and use the standard one as the optimum.
711 ctx
.minspacewidth
= spacewidth
* 3 / 5;
714 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
717 * Having done the wrapping, we now concoct a set of line_data
720 pdata
->first
= pdata
->last
= NULL
;
722 for (p
= wrapping
; p
; p
= p
->next
) {
725 int len
, wid
, spaces
;
727 ldata
= mknew(line_data
);
729 ldata
->pdata
= pdata
;
730 ldata
->first
= p
->begin
;
732 ldata
->line_height
= line_height
;
734 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
737 pdata
->last
->next
= ldata
;
738 ldata
->prev
= pdata
->last
;
740 pdata
->first
= ldata
;
747 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
748 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
751 ldata
->hshortfall
= wid
- len
;
752 ldata
->nspaces
= spaces
;
754 * This tells us how much the space width needs to
755 * change from _min_spacewidth. But we want to store
756 * its difference from the _natural_ space width, to
757 * make the text rendering easier.
759 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
760 ldata
->hshortfall
-= spacewidth
* spaces
;
762 * Special case: on the last line of a paragraph, we
763 * never stretch spaces.
765 if (ldata
->hshortfall
> 0 && !p
->next
)
766 ldata
->hshortfall
= 0;
768 ldata
->aux_text
= NULL
;
769 ldata
->aux_text_2
= NULL
;
770 ldata
->aux_left_indent
= 0;
771 ldata
->penalty_before
= ldata
->penalty_after
= 0;
776 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
783 * Page breaking is done by a close analogue of the optimal
784 * paragraph wrapping algorithm used by wrap_para(). We work
785 * backwards from the end of the document line by line; for
786 * each line, we contemplate every possible number of lines we
787 * could put on a page starting with that line, determine a
788 * cost function for each one, add it to the pre-computed cost
789 * function for optimally page-breaking everything after that
790 * page, and pick the best option.
792 * Since my line_data structures are only used for this
793 * purpose, I might as well just store the algorithm data
797 for (l
= last
; l
; l
= l
->prev
) {
798 int minheight
, text
= 0, space
= 0;
802 for (m
= l
; m
; m
= m
->next
) {
803 if (m
!= l
&& m
->page_break
)
804 break; /* we've gone as far as we can */
807 space
+= m
->prev
->space_after
;
808 if (m
!= l
|| m
->page_break
)
809 space
+= m
->space_before
;
810 text
+= m
->line_height
;
811 minheight
= text
+ space
;
813 if (m
!= l
&& minheight
> page_height
)
817 * Compute the cost of this arrangement, as the square
818 * of the amount of wasted space on the page.
819 * Exception: if this is the last page before a
820 * mandatory break or the document end, we don't
821 * penalise a large blank area.
823 if (m
->next
&& !m
->next
->page_break
)
825 int x
= page_height
- minheight
;
832 cost
+= (x
* xf
) >> 8;
836 if (m
->next
&& !m
->next
->page_break
) {
837 cost
+= m
->penalty_after
;
838 cost
+= m
->next
->penalty_before
;
841 if (m
->next
&& !m
->next
->page_break
)
842 cost
+= m
->next
->bestcost
;
843 if (l
->bestcost
== -1 || l
->bestcost
> cost
) {
845 * This is the best option yet for this starting
849 if (m
->next
&& !m
->next
->page_break
)
850 l
->vshortfall
= page_height
- minheight
;
861 * Now go through the line list forwards and assemble the
871 page
= mknew(page_data
);
880 page
->first_line
= l
;
881 page
->last_line
= l
->page_last
;
883 page
->first_text
= page
->last_text
= NULL
;
885 page
->first_xref
= page
->last_xref
= NULL
;
888 * Now assign a y-coordinate to each line on the page.
891 for (l
= page
->first_line
; l
; l
= l
->next
) {
892 if (l
!= page
->first_line
)
893 space
+= l
->prev
->space_after
;
894 if (l
!= page
->first_line
|| l
->page_break
)
895 space
+= l
->space_before
;
896 text
+= l
->line_height
;
899 l
->ypos
= text
+ space
+
900 space
* (float)page
->first_line
->vshortfall
/
901 page
->first_line
->space
;
903 if (l
== page
->last_line
)
907 l
= page
->last_line
->next
;
913 static void add_string_to_page(page_data
*page
, int x
, int y
,
914 font_encoding
*fe
, int size
, char *text
)
918 frag
= mknew(text_fragment
);
922 page
->last_text
->next
= frag
;
924 page
->first_text
= frag
;
925 page
->last_text
= frag
;
930 frag
->fontsize
= size
;
931 frag
->text
= dupstr(text
);
935 * Returns the updated x coordinate.
937 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
938 int x
, int y
, wchar_t *str
)
941 int textpos
, textwid
, glyph
;
942 font_encoding
*subfont
= NULL
, *sf
;
944 text
= mknewa(char, 1 + ustrlen(str
));
945 textpos
= textwid
= 0;
948 glyph
= font
->bmp
[*str
];
951 continue; /* nothing more we can do here */
954 * Find which subfont this character is going in.
956 sf
= font
->subfont_map
[glyph
].subfont
;
962 * This character is not yet in a subfont. Assign one.
964 if (font
->latest_subfont
->free_pos
>= 0x100)
965 font
->latest_subfont
= new_font_encoding(font
);
967 c
= font
->latest_subfont
->free_pos
++;
968 if (font
->latest_subfont
->free_pos
== 0x7F)
969 font
->latest_subfont
->free_pos
= 0xA1;
971 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
972 font
->subfont_map
[glyph
].position
= c
;
973 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
974 font
->latest_subfont
->indices
[c
] = glyph
;
975 font
->latest_subfont
->to_unicode
[c
] = *str
;
977 sf
= font
->latest_subfont
;
980 if (!subfont
|| sf
!= subfont
) {
982 text
[textpos
] = '\0';
983 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
986 assert(textpos
== 0);
992 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
993 textwid
+= font
->widths
[glyph
] * fontsize
;
999 text
[textpos
] = '\0';
1000 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1008 * Returns the updated x coordinate.
1010 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1011 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1012 int shortfall
, int nspaces
, int *nspace
,
1013 keywordlist
*keywords
)
1015 while (text
&& text
!= text_end
) {
1016 int style
, type
, findex
, errs
;
1020 switch (text
->type
) {
1022 * Start a cross-reference.
1024 case word_HyperLink
:
1025 case word_UpperXref
:
1026 case word_LowerXref
:
1028 if (text
->type
== word_HyperLink
) {
1030 dest
.url
= utoa_dup(text
->text
);
1033 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1037 assert(kwl
->para
->private_data
);
1038 pdata
= (para_data
*) kwl
->para
->private_data
;
1040 dest
.page
= pdata
->first
->page
;
1044 * Shouldn't happen, but *shrug*
1051 if (dest
.type
!= NONE
) {
1053 (*xr
)->dest
= dest
; /* structure copy */
1054 if (page
->last_xref
)
1055 page
->last_xref
->next
= *xr
;
1057 page
->first_xref
= *xr
;
1058 page
->last_xref
= *xr
;
1061 * FIXME: Ideally we should have, and use, some
1062 * vertical font metric information here so that
1063 * our cross-ref rectangle can take account of
1064 * descenders and the font's cap height. This will
1065 * do for the moment, but it isn't ideal.
1067 (*xr
)->lx
= (*xr
)->rx
= x
;
1069 (*xr
)->ty
= y
+ ldata
->line_height
;
1074 * Finish extending a cross-reference box.
1084 * FIXME: we should do something with this.
1088 style
= towordstyle(text
->type
);
1089 type
= removeattr(text
->type
);
1091 findex
= (style
== word_Normal ? FONT_NORMAL
:
1092 style
== word_Emph ? FONT_EMPH
:
1095 if (type
== word_Normal
) {
1097 } else if (type
== word_WhiteSpace
) {
1098 x
+= pdata
->sizes
[findex
] *
1099 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1100 if (nspaces
&& findex
!= FONT_CODE
) {
1101 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1102 x
-= *nspace
* shortfall
/ nspaces
;
1106 } else /* if (type == word_Quote) */ {
1107 if (text
->aux
== quote_Open
)
1108 str
= L
"\x2018"; /* FIXME: configurability! */
1110 str
= L
"\x2019"; /* FIXME: configurability! */
1113 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1115 if (errs
&& text
->alt
)
1116 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1117 xr
, shortfall
, nspaces
, nspace
, keywords
);
1119 x
= render_string(page
, pdata
->fonts
[findex
],
1120 pdata
->sizes
[findex
], x
, y
, str
);
1132 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
1133 xref_dest
*dest
, keywordlist
*keywords
)
1138 if (ldata
->aux_text
) {
1142 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1143 left_x
+ ldata
->aux_left_indent
,
1144 top_y
- ldata
->ypos
,
1145 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1146 if (ldata
->aux_text_2
)
1147 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1148 x
, top_y
- ldata
->ypos
,
1149 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1154 * There might be a cross-reference carried over from a
1157 if (dest
->type
!= NONE
) {
1159 xr
->dest
= *dest
; /* structure copy */
1160 if (ldata
->page
->last_xref
)
1161 ldata
->page
->last_xref
->next
= xr
;
1163 ldata
->page
->first_xref
= xr
;
1164 ldata
->page
->last_xref
= xr
;
1165 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1166 xr
->by
= top_y
- ldata
->ypos
;
1167 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1171 render_text(ldata
->page
, ldata
->pdata
, ldata
, left_x
+ ldata
->xpos
,
1172 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
, &xr
,
1173 ldata
->hshortfall
, ldata
->nspaces
, &nspace
, keywords
);
1177 * There's a cross-reference continued on to the next line.
1184 static void code_paragraph(para_data
*pdata
,
1185 font_data
*fn
, font_data
*fi
, font_data
*fb
,
1186 int font_size
, int indent
, word
*words
)
1189 * For code paragraphs, I'm going to hack grievously and
1190 * pretend the three normal fonts are the three code paragraph
1193 pdata
->fonts
[FONT_NORMAL
] = fb
;
1194 pdata
->fonts
[FONT_EMPH
] = fi
;
1195 pdata
->fonts
[FONT_CODE
] = fn
;
1196 pdata
->sizes
[FONT_NORMAL
] =
1197 pdata
->sizes
[FONT_EMPH
] =
1198 pdata
->sizes
[FONT_CODE
] = font_size
;
1200 pdata
->first
= pdata
->last
= NULL
;
1202 for (; words
; words
= words
->next
) {
1203 wchar_t *t
, *e
, *start
;
1204 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1206 int prev
= -1, curr
;
1209 if (words
->next
&& words
->next
->type
== word_Emph
) {
1210 e
= words
->next
->text
;
1211 words
= words
->next
;
1221 else if (*e
== L
'i')
1223 else if (*e
== L
'b')
1240 * We've isolated a maximal subsequence of the line
1241 * which has the same emphasis. Form it into a word
1247 w
->type
= (prev
== 0 ? word_WeakCode
:
1248 prev
== 1 ? word_Emph
: word_Normal
);
1249 w
->text
= mknewa(wchar_t, t
-start
+1);
1250 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
1251 w
->text
[t
-start
] = '\0';
1264 ldata
= mknew(line_data
);
1266 ldata
->pdata
= pdata
;
1267 ldata
->first
= lhead
;
1269 ldata
->line_height
= font_size
* 4096;
1271 ldata
->xpos
= indent
;
1274 pdata
->last
->next
= ldata
;
1275 ldata
->prev
= pdata
->last
;
1277 pdata
->first
= ldata
;
1281 pdata
->last
= ldata
;
1283 ldata
->hshortfall
= 0;
1285 ldata
->aux_text
= NULL
;
1286 ldata
->aux_text_2
= NULL
;
1287 ldata
->aux_left_indent
= 0;
1288 /* General opprobrium for breaking in a code paragraph. */
1289 ldata
->penalty_before
= ldata
->penalty_after
= 50000;