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
22 * - header/footer? Page numbers at least would be handy. Fully
23 * configurable footer can wait, though.
25 * That should bring us to the same level of functionality that
26 * original-Halibut had, and the same in PDF plus the obvious
27 * interactive navigation features. After that, in future work:
29 * - linearised PDF, perhaps?
31 * - I'm uncertain of whether I need to include a ToUnicode CMap
32 * in each of my font definitions in PDF. Currently things (by
33 * which I mean cut and paste out of acroread) seem to be
34 * working fairly happily without it, but I don't know.
47 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
48 static void wrap_paragraph(para_data
*pdata
, word
*words
,
49 int w
, int i1
, int i2
);
50 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
52 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
53 xref_dest
*dest
, keywordlist
*keywords
);
54 static int paper_width_simple(para_data
*pdata
, word
*text
);
55 static void code_paragraph(para_data
*pdata
,
56 font_data
*fn
, font_data
*fi
, font_data
*fb
,
57 int font_size
, int indent
, word
*words
);
58 static void rule_paragraph(para_data
*pdata
, int indent
, int height
);
59 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
61 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
65 int indent
, extra_indent
, firstline_indent
, aux_indent
;
67 line_data
*ldata
, *firstline
, *lastline
;
68 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
74 * FIXME: All these things ought to become configurable.
76 int paper_width
= 595 * 4096;
77 int paper_height
= 841 * 4096;
78 int left_margin
= 72 * 4096;
79 int top_margin
= 72 * 4096;
80 int right_margin
= 72 * 4096;
81 int bottom_margin
= 108 * 4096;
82 int indent_list_bullet
= 6 * 4096;
83 int indent_list
= 24 * 4096;
84 int indent_quote
= 18 * 4096;
85 int base_leading
= 4096;
86 int base_para_spacing
= 10 * 4096;
87 int chapter_top_space
= 72 * 4096;
88 int sect_num_left_space
= 12 * 4096;
89 int chapter_underline_depth
= 14 * 4096;
90 int chapter_underline_thickness
= 3 * 4096;
91 int rule_thickness
= 1 * 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.
128 case para_NotParaType
:
135 * These paragraph types don't require wrapping, but
136 * they do affect the line width to which we wrap the
137 * rest of the paragraphs, so we need to pay attention.
140 indent
+= indent_list
; break;
142 indent
-= indent_list
; assert(indent
>= 0); break;
144 indent
+= indent_quote
; break;
146 indent
-= indent_quote
; assert(indent
>= 0); break;
149 * This paragraph type is special. Process it
153 pdata
= mknew(para_data
);
154 code_paragraph(pdata
, cr
, co
, cb
, 12, indent
, p
->words
);
155 p
->private_data
= pdata
;
156 if (pdata
->first
!= pdata
->last
) {
157 pdata
->first
->penalty_after
+= 100000;
158 pdata
->last
->penalty_before
+= 100000;
163 * This paragraph is also special.
166 pdata
= mknew(para_data
);
167 rule_paragraph(pdata
, indent
, rule_thickness
);
168 p
->private_data
= pdata
;
172 * All of these paragraph types require wrapping in the
173 * ordinary way. So we must supply a set of fonts, a
174 * line width and auxiliary information (e.g. bullet
175 * text) for each one.
179 case para_UnnumberedChapter
:
183 case para_BiblioCited
:
185 case para_NumberedList
:
186 case para_DescribedThing
:
187 case para_Description
:
190 pdata
= mknew(para_data
);
193 * Choose fonts for this paragraph.
195 * FIXME: All of this ought to be completely
200 pdata
->fonts
[FONT_NORMAL
] = hr
;
201 pdata
->sizes
[FONT_NORMAL
] = 24;
202 pdata
->fonts
[FONT_EMPH
] = hi
;
203 pdata
->sizes
[FONT_EMPH
] = 24;
204 pdata
->fonts
[FONT_CODE
] = cb
;
205 pdata
->sizes
[FONT_CODE
] = 24;
210 case para_UnnumberedChapter
:
211 pdata
->fonts
[FONT_NORMAL
] = hr
;
212 pdata
->sizes
[FONT_NORMAL
] = 20;
213 pdata
->fonts
[FONT_EMPH
] = hi
;
214 pdata
->sizes
[FONT_EMPH
] = 20;
215 pdata
->fonts
[FONT_CODE
] = cb
;
216 pdata
->sizes
[FONT_CODE
] = 20;
221 pdata
->fonts
[FONT_NORMAL
] = hr
;
222 pdata
->fonts
[FONT_EMPH
] = hi
;
223 pdata
->fonts
[FONT_CODE
] = cb
;
224 pdata
->sizes
[FONT_NORMAL
] =
225 pdata
->sizes
[FONT_EMPH
] =
226 pdata
->sizes
[FONT_CODE
] =
227 (p
->aux
== 0 ?
16 : p
->aux
== 1 ?
14 : 13);
231 case para_BiblioCited
:
233 case para_NumberedList
:
234 case para_DescribedThing
:
235 case para_Description
:
237 pdata
->fonts
[FONT_NORMAL
] = tr
;
238 pdata
->sizes
[FONT_NORMAL
] = 12;
239 pdata
->fonts
[FONT_EMPH
] = ti
;
240 pdata
->sizes
[FONT_EMPH
] = 12;
241 pdata
->fonts
[FONT_CODE
] = cr
;
242 pdata
->sizes
[FONT_CODE
] = 12;
247 * Also select an indentation level depending on the
248 * paragraph type (list paragraphs other than
249 * para_DescribedThing need extra indent).
251 * (FIXME: Perhaps at some point we might even arrange
252 * for the user to be able to request indented first
253 * lines in paragraphs.)
255 if (p
->type
== para_Bullet
||
256 p
->type
== para_NumberedList
||
257 p
->type
== para_Description
) {
258 extra_indent
= firstline_indent
= indent_list
;
260 extra_indent
= firstline_indent
= 0;
264 * Find the auxiliary text for this paragraph.
275 * For some heading styles (FIXME: be able to
276 * configure which), the auxiliary text contains
277 * the chapter number and is arranged to be
278 * right-aligned a few points left of the primary
279 * margin. For other styles, the auxiliary text is
280 * the full chapter _name_ and takes up space
281 * within the (wrapped) chapter title, meaning that
282 * we must move the first line indent over to make
285 if (p
->type
== para_Heading
|| p
->type
== para_Subsect
) {
289 len
= paper_width_simple(pdata
, p
->kwtext2
);
290 aux_indent
= -len
- sect_num_left_space
;
296 aux2
->type
= word_Normal
;
297 aux2
->text
= ustrdup(L
": ");
298 aux2
->breaks
= FALSE
;
302 firstline_indent
+= paper_width_simple(pdata
, aux
);
303 firstline_indent
+= paper_width_simple(pdata
, aux2
);
309 * Auxiliary text consisting of a bullet. (FIXME:
310 * configurable bullet.)
315 aux
->type
= word_Normal
;
316 aux
->text
= ustrdup(L
"\x2022");
319 aux_indent
= indent
+ indent_list_bullet
;
322 case para_NumberedList
:
324 * Auxiliary text consisting of the number followed
325 * by a (FIXME: configurable) full stop.
331 aux2
->type
= word_Normal
;
332 aux2
->text
= ustrdup(L
".");
333 aux2
->breaks
= FALSE
;
335 aux_indent
= indent
+ indent_list_bullet
;
338 case para_BiblioCited
:
340 * Auxiliary text consisting of the bibliography
341 * reference text, and a trailing space.
347 aux2
->type
= word_Normal
;
348 aux2
->text
= ustrdup(L
" ");
349 aux2
->breaks
= FALSE
;
352 firstline_indent
+= paper_width_simple(pdata
, aux
);
353 firstline_indent
+= paper_width_simple(pdata
, aux2
);
357 wrap_paragraph(pdata
, p
->words
, base_width
,
358 indent
+ firstline_indent
,
359 indent
+ extra_indent
);
361 p
->private_data
= pdata
;
363 pdata
->first
->aux_text
= aux
;
364 pdata
->first
->aux_text_2
= aux2
;
365 pdata
->first
->aux_left_indent
= aux_indent
;
368 * Line breaking penalties.
375 case para_UnnumberedChapter
:
377 * Fixed and large penalty for breaking straight
378 * after a heading; corresponding bonus for
379 * breaking straight before.
381 pdata
->first
->penalty_before
= -500000;
382 pdata
->last
->penalty_after
= 500000;
383 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
384 ldata
->penalty_after
= 500000;
387 case para_DescribedThing
:
389 * This is treated a bit like a small heading:
390 * there's a penalty for breaking after it (i.e.
391 * between it and its description), and a bonus for
392 * breaking before it (actually _between_ list
395 pdata
->first
->penalty_before
= -200000;
396 pdata
->last
->penalty_after
= 200000;
401 * Most paragraph types: widow/orphan control by
402 * discouraging breaking one line from the end of
405 if (pdata
->first
!= pdata
->last
) {
406 pdata
->first
->penalty_after
= 100000;
407 pdata
->last
->penalty_before
= 100000;
415 if (p
->private_data
) {
416 pdata
= (para_data
*)p
->private_data
;
419 * Set the line spacing for each line in this paragraph.
421 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
422 if (ldata
== pdata
->first
)
423 ldata
->space_before
= base_para_spacing
/ 2;
425 ldata
->space_before
= base_leading
/ 2;
426 if (ldata
== pdata
->last
)
427 ldata
->space_after
= base_para_spacing
/ 2;
429 ldata
->space_after
= base_leading
/ 2;
430 ldata
->page_break
= FALSE
;
434 * Some kinds of section heading do require a page
437 if (p
->type
== para_Title
||
438 p
->type
== para_Chapter
||
439 p
->type
== para_Appendix
||
440 p
->type
== para_UnnumberedChapter
) {
441 pdata
->first
->page_break
= TRUE
;
442 pdata
->first
->space_before
= chapter_top_space
;
443 pdata
->last
->space_after
+=
444 chapter_underline_depth
+ chapter_underline_thickness
;
448 * Link all line structures together into a big list.
452 lastline
->next
= pdata
->first
;
453 pdata
->first
->prev
= lastline
;
455 firstline
= pdata
->first
;
456 pdata
->first
->prev
= NULL
;
458 lastline
= pdata
->last
;
464 * Now we have an enormous linked list of every line of text in
465 * the document. Break it up into pages.
467 pages
= page_breaks(firstline
, lastline
, page_height
);
470 * Now we're ready to actually lay out the pages. We do this by
471 * looping over _paragraphs_, since we may need to track cross-
472 * references between lines and even across pages.
474 for (p
= sourceform
; p
; p
= p
->next
) {
475 pdata
= (para_data
*)p
->private_data
;
480 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
481 render_line(ldata
, left_margin
, paper_height
- top_margin
,
483 if (ldata
== pdata
->last
)
488 * Some section headings (FIXME: should be configurable
489 * which) want to be underlined.
491 if (p
->type
== para_Chapter
|| p
->type
== para_Appendix
||
492 p
->type
== para_UnnumberedChapter
|| p
->type
== para_Title
) {
493 add_rect_to_page(pdata
->last
->page
,
495 (paper_height
- top_margin
-
496 pdata
->last
->ypos
- chapter_underline_depth
),
498 chapter_underline_thickness
);
502 * Rule paragraphs need to contain an actual rule!
504 if (p
->type
== para_Rule
) {
505 add_rect_to_page(pdata
->first
->page
,
506 left_margin
+ pdata
->first
->xpos
,
507 (paper_height
- top_margin
-
509 pdata
->last
->line_height
),
510 base_width
- pdata
->first
->xpos
,
511 pdata
->last
->line_height
);
516 doc
= mknew(document
);
517 doc
->fonts
= fontlist
;
519 doc
->paper_width
= paper_width
;
520 doc
->paper_height
= paper_height
;
524 static font_encoding
*new_font_encoding(font_data
*font
)
529 fe
= mknew(font_encoding
);
532 if (font
->list
->tail
)
533 font
->list
->tail
->next
= fe
;
535 font
->list
->head
= fe
;
536 font
->list
->tail
= fe
;
541 for (i
= 0; i
< 256; i
++) {
542 fe
->vector
[i
] = NULL
;
544 fe
->to_unicode
[i
] = 0xFFFF;
550 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
558 widths
= ps_std_font_widths(name
);
562 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
564 f
= mknew(font_data
);
568 f
->nglyphs
= nglyphs
;
569 f
->glyphs
= ps_std_glyphs
;
571 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
574 * Our first subfont will contain all of US-ASCII. This isn't
575 * really necessary - we could just create custom subfonts
576 * precisely as the whim of render_string dictated - but
577 * instinct suggests that it might be nice to have the text in
578 * the output files look _marginally_ recognisable.
580 fe
= new_font_encoding(f
);
581 fe
->free_pos
= 0xA1; /* only the top half is free */
582 f
->latest_subfont
= fe
;
584 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
587 for (i
= 0; i
< nglyphs
; i
++) {
589 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
590 assert(ucs
!= 0xFFFF);
592 if (ucs
>= 0x20 && ucs
<= 0x7E) {
593 fe
->vector
[ucs
] = f
->glyphs
[i
];
594 fe
->indices
[ucs
] = i
;
595 fe
->to_unicode
[ucs
] = ucs
;
596 f
->subfont_map
[i
].subfont
= fe
;
597 f
->subfont_map
[i
].position
= ucs
;
600 * This character is not yet assigned to a subfont.
602 f
->subfont_map
[i
].subfont
= NULL
;
603 f
->subfont_map
[i
].position
= 0;
610 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
617 for (; *string
; string
++) {
620 index
= font
->bmp
[(unsigned short)*string
];
621 if (index
== 0xFFFF) {
625 width
+= font
->widths
[index
];
632 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
634 struct paper_width_ctx
{
639 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
641 while (text
&& text
!= end
) {
642 w
+= paper_width_internal(vctx
, text
, nspaces
);
648 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
650 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
651 int style
, type
, findex
, width
, errs
;
654 switch (word
->type
) {
664 style
= towordstyle(word
->type
);
665 type
= removeattr(word
->type
);
667 findex
= (style
== word_Normal ? FONT_NORMAL
:
668 style
== word_Emph ? FONT_EMPH
:
671 if (type
== word_Normal
) {
673 } else if (type
== word_WhiteSpace
) {
674 if (findex
!= FONT_CODE
) {
677 return ctx
->minspacewidth
;
680 } else /* if (type == word_Quote) */ {
681 if (word
->aux
== quote_Open
)
682 str
= L
"\x2018"; /* FIXME: configurability! */
684 str
= L
"\x2019"; /* FIXME: configurability! */
687 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
689 if (errs
&& word
->alt
)
690 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
692 return ctx
->pdata
->sizes
[findex
] * width
;
695 static int paper_width(void *vctx
, word
*word
)
697 return paper_width_internal(vctx
, word
, NULL
);
700 static int paper_width_simple(para_data
*pdata
, word
*text
)
702 struct paper_width_ctx ctx
;
706 (pdata
->sizes
[FONT_NORMAL
] *
707 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
709 return paper_width_list(&ctx
, text
, NULL
, NULL
);
712 static void wrap_paragraph(para_data
*pdata
, word
*words
,
713 int w
, int i1
, int i2
)
715 wrappedline
*wrapping
, *p
;
717 struct paper_width_ctx ctx
;
721 * We're going to need to store the line height in every line
722 * structure we generate.
727 for (i
= 0; i
< NFONTS
; i
++)
728 if (line_height
< pdata
->sizes
[i
])
729 line_height
= pdata
->sizes
[i
];
733 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
734 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
735 if (spacewidth
== 0) {
737 * A font without a space?! Disturbing. I hope this never
738 * comes up, but I'll make a random guess anyway and set my
739 * space width to half the point size.
741 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
745 * I'm going to set the _minimum_ space width to 3/5 of the
746 * standard one, and use the standard one as the optimum.
748 ctx
.minspacewidth
= spacewidth
* 3 / 5;
751 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
754 * Having done the wrapping, we now concoct a set of line_data
757 pdata
->first
= pdata
->last
= NULL
;
759 for (p
= wrapping
; p
; p
= p
->next
) {
762 int len
, wid
, spaces
;
764 ldata
= mknew(line_data
);
766 ldata
->pdata
= pdata
;
767 ldata
->first
= p
->begin
;
769 ldata
->line_height
= line_height
;
771 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
774 pdata
->last
->next
= ldata
;
775 ldata
->prev
= pdata
->last
;
777 pdata
->first
= ldata
;
784 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
785 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
788 ldata
->hshortfall
= wid
- len
;
789 ldata
->nspaces
= spaces
;
791 * This tells us how much the space width needs to
792 * change from _min_spacewidth. But we want to store
793 * its difference from the _natural_ space width, to
794 * make the text rendering easier.
796 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
797 ldata
->hshortfall
-= spacewidth
* spaces
;
799 * Special case: on the last line of a paragraph, we
800 * never stretch spaces.
802 if (ldata
->hshortfall
> 0 && !p
->next
)
803 ldata
->hshortfall
= 0;
805 ldata
->aux_text
= NULL
;
806 ldata
->aux_text_2
= NULL
;
807 ldata
->aux_left_indent
= 0;
808 ldata
->penalty_before
= ldata
->penalty_after
= 0;
813 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
820 * Page breaking is done by a close analogue of the optimal
821 * paragraph wrapping algorithm used by wrap_para(). We work
822 * backwards from the end of the document line by line; for
823 * each line, we contemplate every possible number of lines we
824 * could put on a page starting with that line, determine a
825 * cost function for each one, add it to the pre-computed cost
826 * function for optimally page-breaking everything after that
827 * page, and pick the best option.
829 * Since my line_data structures are only used for this
830 * purpose, I might as well just store the algorithm data
834 for (l
= last
; l
; l
= l
->prev
) {
835 int minheight
, text
= 0, space
= 0;
839 for (m
= l
; m
; m
= m
->next
) {
840 if (m
!= l
&& m
->page_break
)
841 break; /* we've gone as far as we can */
844 space
+= m
->prev
->space_after
;
845 if (m
!= l
|| m
->page_break
)
846 space
+= m
->space_before
;
847 text
+= m
->line_height
;
848 minheight
= text
+ space
;
850 if (m
!= l
&& minheight
> page_height
)
854 * Compute the cost of this arrangement, as the square
855 * of the amount of wasted space on the page.
856 * Exception: if this is the last page before a
857 * mandatory break or the document end, we don't
858 * penalise a large blank area.
860 if (m
->next
&& !m
->next
->page_break
)
862 int x
= page_height
- minheight
;
869 cost
+= (x
* xf
) >> 8;
873 if (m
->next
&& !m
->next
->page_break
) {
874 cost
+= m
->penalty_after
;
875 cost
+= m
->next
->penalty_before
;
878 if (m
->next
&& !m
->next
->page_break
)
879 cost
+= m
->next
->bestcost
;
880 if (l
->bestcost
== -1 || l
->bestcost
> cost
) {
882 * This is the best option yet for this starting
886 if (m
->next
&& !m
->next
->page_break
)
887 l
->vshortfall
= page_height
- minheight
;
898 * Now go through the line list forwards and assemble the
908 page
= mknew(page_data
);
917 page
->first_line
= l
;
918 page
->last_line
= l
->page_last
;
920 page
->first_text
= page
->last_text
= NULL
;
921 page
->first_xref
= page
->last_xref
= NULL
;
922 page
->first_rect
= page
->last_rect
= NULL
;
925 * Now assign a y-coordinate to each line on the page.
928 for (l
= page
->first_line
; l
; l
= l
->next
) {
929 if (l
!= page
->first_line
)
930 space
+= l
->prev
->space_after
;
931 if (l
!= page
->first_line
|| l
->page_break
)
932 space
+= l
->space_before
;
933 text
+= l
->line_height
;
936 l
->ypos
= text
+ space
+
937 space
* (float)page
->first_line
->vshortfall
/
938 page
->first_line
->space
;
940 if (l
== page
->last_line
)
944 l
= page
->last_line
->next
;
950 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
952 rect
*r
= mknew(rect
);
956 page
->last_rect
->next
= r
;
958 page
->first_rect
= r
;
967 static void add_string_to_page(page_data
*page
, int x
, int y
,
968 font_encoding
*fe
, int size
, char *text
)
972 frag
= mknew(text_fragment
);
976 page
->last_text
->next
= frag
;
978 page
->first_text
= frag
;
979 page
->last_text
= frag
;
984 frag
->fontsize
= size
;
985 frag
->text
= dupstr(text
);
989 * Returns the updated x coordinate.
991 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
992 int x
, int y
, wchar_t *str
)
995 int textpos
, textwid
, glyph
;
996 font_encoding
*subfont
= NULL
, *sf
;
998 text
= mknewa(char, 1 + ustrlen(str
));
999 textpos
= textwid
= 0;
1002 glyph
= font
->bmp
[*str
];
1004 if (glyph
== 0xFFFF)
1005 continue; /* nothing more we can do here */
1008 * Find which subfont this character is going in.
1010 sf
= font
->subfont_map
[glyph
].subfont
;
1016 * This character is not yet in a subfont. Assign one.
1018 if (font
->latest_subfont
->free_pos
>= 0x100)
1019 font
->latest_subfont
= new_font_encoding(font
);
1021 c
= font
->latest_subfont
->free_pos
++;
1022 if (font
->latest_subfont
->free_pos
== 0x7F)
1023 font
->latest_subfont
->free_pos
= 0xA1;
1025 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1026 font
->subfont_map
[glyph
].position
= c
;
1027 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1028 font
->latest_subfont
->indices
[c
] = glyph
;
1029 font
->latest_subfont
->to_unicode
[c
] = *str
;
1031 sf
= font
->latest_subfont
;
1034 if (!subfont
|| sf
!= subfont
) {
1036 text
[textpos
] = '\0';
1037 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1040 assert(textpos
== 0);
1046 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1047 textwid
+= font
->widths
[glyph
] * fontsize
;
1053 text
[textpos
] = '\0';
1054 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1062 * Returns the updated x coordinate.
1064 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1065 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1066 int shortfall
, int nspaces
, int *nspace
,
1067 keywordlist
*keywords
)
1069 while (text
&& text
!= text_end
) {
1070 int style
, type
, findex
, errs
;
1074 switch (text
->type
) {
1076 * Start a cross-reference.
1078 case word_HyperLink
:
1079 case word_UpperXref
:
1080 case word_LowerXref
:
1082 if (text
->type
== word_HyperLink
) {
1084 dest
.url
= utoa_dup(text
->text
);
1087 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1091 assert(kwl
->para
->private_data
);
1092 pdata
= (para_data
*) kwl
->para
->private_data
;
1094 dest
.page
= pdata
->first
->page
;
1098 * Shouldn't happen, but *shrug*
1105 if (dest
.type
!= NONE
) {
1107 (*xr
)->dest
= dest
; /* structure copy */
1108 if (page
->last_xref
)
1109 page
->last_xref
->next
= *xr
;
1111 page
->first_xref
= *xr
;
1112 page
->last_xref
= *xr
;
1116 * FIXME: Ideally we should have, and use, some
1117 * vertical font metric information here so that
1118 * our cross-ref rectangle can take account of
1119 * descenders and the font's cap height. This will
1120 * do for the moment, but it isn't ideal.
1122 (*xr
)->lx
= (*xr
)->rx
= x
;
1124 (*xr
)->ty
= y
+ ldata
->line_height
;
1129 * Finish extending a cross-reference box.
1139 * FIXME: we should do something with this.
1143 style
= towordstyle(text
->type
);
1144 type
= removeattr(text
->type
);
1146 findex
= (style
== word_Normal ? FONT_NORMAL
:
1147 style
== word_Emph ? FONT_EMPH
:
1150 if (type
== word_Normal
) {
1152 } else if (type
== word_WhiteSpace
) {
1153 x
+= pdata
->sizes
[findex
] *
1154 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1155 if (nspaces
&& findex
!= FONT_CODE
) {
1156 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1157 x
-= *nspace
* shortfall
/ nspaces
;
1161 } else /* if (type == word_Quote) */ {
1162 if (text
->aux
== quote_Open
)
1163 str
= L
"\x2018"; /* FIXME: configurability! */
1165 str
= L
"\x2019"; /* FIXME: configurability! */
1168 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1170 if (errs
&& text
->alt
)
1171 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1172 xr
, shortfall
, nspaces
, nspace
, keywords
);
1174 x
= render_string(page
, pdata
->fonts
[findex
],
1175 pdata
->sizes
[findex
], x
, y
, str
);
1187 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
1188 xref_dest
*dest
, keywordlist
*keywords
)
1193 if (ldata
->aux_text
) {
1197 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1198 left_x
+ ldata
->aux_left_indent
,
1199 top_y
- ldata
->ypos
,
1200 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1201 if (ldata
->aux_text_2
)
1202 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1203 x
, top_y
- ldata
->ypos
,
1204 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1210 * There might be a cross-reference carried over from a
1213 if (dest
->type
!= NONE
) {
1216 xr
->dest
= *dest
; /* structure copy */
1217 if (ldata
->page
->last_xref
)
1218 ldata
->page
->last_xref
->next
= xr
;
1220 ldata
->page
->first_xref
= xr
;
1221 ldata
->page
->last_xref
= xr
;
1222 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1223 xr
->by
= top_y
- ldata
->ypos
;
1224 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1228 render_text(ldata
->page
, ldata
->pdata
, ldata
, left_x
+ ldata
->xpos
,
1229 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
, &xr
,
1230 ldata
->hshortfall
, ldata
->nspaces
, &nspace
, keywords
);
1234 * There's a cross-reference continued on to the next line.
1242 static void code_paragraph(para_data
*pdata
,
1243 font_data
*fn
, font_data
*fi
, font_data
*fb
,
1244 int font_size
, int indent
, word
*words
)
1247 * For code paragraphs, I'm going to hack grievously and
1248 * pretend the three normal fonts are the three code paragraph
1251 pdata
->fonts
[FONT_NORMAL
] = fb
;
1252 pdata
->fonts
[FONT_EMPH
] = fi
;
1253 pdata
->fonts
[FONT_CODE
] = fn
;
1254 pdata
->sizes
[FONT_NORMAL
] =
1255 pdata
->sizes
[FONT_EMPH
] =
1256 pdata
->sizes
[FONT_CODE
] = font_size
;
1258 pdata
->first
= pdata
->last
= NULL
;
1260 for (; words
; words
= words
->next
) {
1261 wchar_t *t
, *e
, *start
;
1262 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1264 int prev
= -1, curr
;
1267 if (words
->next
&& words
->next
->type
== word_Emph
) {
1268 e
= words
->next
->text
;
1269 words
= words
->next
;
1279 else if (*e
== L
'i')
1281 else if (*e
== L
'b')
1298 * We've isolated a maximal subsequence of the line
1299 * which has the same emphasis. Form it into a word
1305 w
->type
= (prev
== 0 ? word_WeakCode
:
1306 prev
== 1 ? word_Emph
: word_Normal
);
1307 w
->text
= mknewa(wchar_t, t
-start
+1);
1308 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
1309 w
->text
[t
-start
] = '\0';
1322 ldata
= mknew(line_data
);
1324 ldata
->pdata
= pdata
;
1325 ldata
->first
= lhead
;
1327 ldata
->line_height
= font_size
* 4096;
1329 ldata
->xpos
= indent
;
1332 pdata
->last
->next
= ldata
;
1333 ldata
->prev
= pdata
->last
;
1335 pdata
->first
= ldata
;
1339 pdata
->last
= ldata
;
1341 ldata
->hshortfall
= 0;
1343 ldata
->aux_text
= NULL
;
1344 ldata
->aux_text_2
= NULL
;
1345 ldata
->aux_left_indent
= 0;
1346 /* General opprobrium for breaking in a code paragraph. */
1347 ldata
->penalty_before
= ldata
->penalty_after
= 50000;
1351 static void rule_paragraph(para_data
*pdata
, int indent
, int height
)
1355 ldata
= mknew(line_data
);
1357 ldata
->pdata
= pdata
;
1358 ldata
->first
= NULL
;
1360 ldata
->line_height
= height
;
1362 ldata
->xpos
= indent
;
1367 ldata
->hshortfall
= 0;
1369 ldata
->aux_text
= NULL
;
1370 ldata
->aux_text_2
= NULL
;
1371 ldata
->aux_left_indent
= 0;
1374 * Better to break after a rule than before it
1376 ldata
->penalty_after
+= 100000;
1377 ldata
->penalty_before
+= -100000;
1379 pdata
->first
= pdata
->last
= ldata
;