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 para_Rule
17 * - set up contents section now we know what sections begin on
24 * - header/footer? Page numbers at least would be handy. Fully
25 * configurable footer can wait, though.
27 * That should bring us to the same level of functionality that
28 * original-Halibut had, and the same in PDF plus the obvious
29 * interactive navigation features. After that, in future work:
31 * - linearised PDF, perhaps?
33 * - I'm uncertain of whether I need to include a ToUnicode CMap
34 * in each of my font definitions in PDF. Currently things (by
35 * which I mean cut and paste out of acroread) seem to be
36 * working fairly happily without it, but I don't know.
49 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
50 static void wrap_paragraph(para_data
*pdata
, word
*words
,
51 int w
, int i1
, int i2
);
52 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
54 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
55 xref_dest
*dest
, keywordlist
*keywords
);
56 static int paper_width_simple(para_data
*pdata
, word
*text
);
57 static void code_paragraph(para_data
*pdata
,
58 font_data
*fn
, font_data
*fi
, font_data
*fb
,
59 int font_size
, int indent
, word
*words
);
60 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
);
62 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
66 int indent
, extra_indent
, firstline_indent
, aux_indent
;
68 line_data
*ldata
, *firstline
, *lastline
;
69 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
75 * FIXME: All these things ought to become configurable.
77 int paper_width
= 595 * 4096;
78 int paper_height
= 841 * 4096;
79 int left_margin
= 72 * 4096;
80 int top_margin
= 72 * 4096;
81 int right_margin
= 72 * 4096;
82 int bottom_margin
= 108 * 4096;
83 int indent_list_bullet
= 6 * 4096;
84 int indent_list
= 24 * 4096;
85 int indent_quote
= 18 * 4096;
86 int base_leading
= 4096;
87 int base_para_spacing
= 10 * 4096;
88 int chapter_top_space
= 72 * 4096;
89 int sect_num_left_space
= 12 * 4096;
90 int chapter_underline_depth
= 14 * 4096;
91 int chapter_underline_thickness
= 3 * 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 * All of these paragraph types require wrapping in the
164 * ordinary way. So we must supply a set of fonts, a
165 * line width and auxiliary information (e.g. bullet
166 * text) for each one.
170 case para_UnnumberedChapter
:
174 case para_BiblioCited
:
176 case para_NumberedList
:
177 case para_DescribedThing
:
178 case para_Description
:
181 pdata
= mknew(para_data
);
184 * Choose fonts for this paragraph.
186 * FIXME: All of this ought to be completely
191 pdata
->fonts
[FONT_NORMAL
] = hr
;
192 pdata
->sizes
[FONT_NORMAL
] = 24;
193 pdata
->fonts
[FONT_EMPH
] = hi
;
194 pdata
->sizes
[FONT_EMPH
] = 24;
195 pdata
->fonts
[FONT_CODE
] = cb
;
196 pdata
->sizes
[FONT_CODE
] = 24;
201 case para_UnnumberedChapter
:
202 pdata
->fonts
[FONT_NORMAL
] = hr
;
203 pdata
->sizes
[FONT_NORMAL
] = 20;
204 pdata
->fonts
[FONT_EMPH
] = hi
;
205 pdata
->sizes
[FONT_EMPH
] = 20;
206 pdata
->fonts
[FONT_CODE
] = cb
;
207 pdata
->sizes
[FONT_CODE
] = 20;
212 pdata
->fonts
[FONT_NORMAL
] = hr
;
213 pdata
->fonts
[FONT_EMPH
] = hi
;
214 pdata
->fonts
[FONT_CODE
] = cb
;
215 pdata
->sizes
[FONT_NORMAL
] =
216 pdata
->sizes
[FONT_EMPH
] =
217 pdata
->sizes
[FONT_CODE
] =
218 (p
->aux
== 0 ?
16 : p
->aux
== 1 ?
14 : 13);
222 case para_BiblioCited
:
224 case para_NumberedList
:
225 case para_DescribedThing
:
226 case para_Description
:
228 pdata
->fonts
[FONT_NORMAL
] = tr
;
229 pdata
->sizes
[FONT_NORMAL
] = 12;
230 pdata
->fonts
[FONT_EMPH
] = ti
;
231 pdata
->sizes
[FONT_EMPH
] = 12;
232 pdata
->fonts
[FONT_CODE
] = cr
;
233 pdata
->sizes
[FONT_CODE
] = 12;
238 * Also select an indentation level depending on the
239 * paragraph type (list paragraphs other than
240 * para_DescribedThing need extra indent).
242 * (FIXME: Perhaps at some point we might even arrange
243 * for the user to be able to request indented first
244 * lines in paragraphs.)
246 if (p
->type
== para_Bullet
||
247 p
->type
== para_NumberedList
||
248 p
->type
== para_Description
) {
249 extra_indent
= firstline_indent
= indent_list
;
251 extra_indent
= firstline_indent
= 0;
255 * Find the auxiliary text for this paragraph.
266 * For some heading styles (FIXME: be able to
267 * configure which), the auxiliary text contains
268 * the chapter number and is arranged to be
269 * right-aligned a few points left of the primary
270 * margin. For other styles, the auxiliary text is
271 * the full chapter _name_ and takes up space
272 * within the (wrapped) chapter title, meaning that
273 * we must move the first line indent over to make
276 if (p
->type
== para_Heading
|| p
->type
== para_Subsect
) {
280 len
= paper_width_simple(pdata
, p
->kwtext2
);
281 aux_indent
= -len
- sect_num_left_space
;
287 aux2
->type
= word_Normal
;
288 aux2
->text
= ustrdup(L
": ");
289 aux2
->breaks
= FALSE
;
293 firstline_indent
+= paper_width_simple(pdata
, aux
);
294 firstline_indent
+= paper_width_simple(pdata
, aux2
);
300 * Auxiliary text consisting of a bullet. (FIXME:
301 * configurable bullet.)
306 aux
->type
= word_Normal
;
307 aux
->text
= ustrdup(L
"\x2022");
310 aux_indent
= indent
+ indent_list_bullet
;
313 case para_NumberedList
:
315 * Auxiliary text consisting of the number followed
316 * by a (FIXME: configurable) full stop.
322 aux2
->type
= word_Normal
;
323 aux2
->text
= ustrdup(L
".");
324 aux2
->breaks
= FALSE
;
326 aux_indent
= indent
+ indent_list_bullet
;
329 case para_BiblioCited
:
331 * Auxiliary text consisting of the bibliography
332 * reference text, and a trailing space.
338 aux2
->type
= word_Normal
;
339 aux2
->text
= ustrdup(L
" ");
340 aux2
->breaks
= FALSE
;
343 firstline_indent
+= paper_width_simple(pdata
, aux
);
344 firstline_indent
+= paper_width_simple(pdata
, aux2
);
348 wrap_paragraph(pdata
, p
->words
, base_width
,
349 indent
+ firstline_indent
,
350 indent
+ extra_indent
);
352 p
->private_data
= pdata
;
354 pdata
->first
->aux_text
= aux
;
355 pdata
->first
->aux_text_2
= aux2
;
356 pdata
->first
->aux_left_indent
= aux_indent
;
359 * Line breaking penalties.
366 case para_UnnumberedChapter
:
368 * Fixed and large penalty for breaking straight
369 * after a heading; corresponding bonus for
370 * breaking straight before.
372 pdata
->first
->penalty_before
= -500000;
373 pdata
->last
->penalty_after
= 500000;
374 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
)
375 ldata
->penalty_after
= 500000;
378 case para_DescribedThing
:
380 * This is treated a bit like a small heading:
381 * there's a penalty for breaking after it (i.e.
382 * between it and its description), and a bonus for
383 * breaking before it (actually _between_ list
386 pdata
->first
->penalty_before
= -200000;
387 pdata
->last
->penalty_after
= 200000;
392 * Most paragraph types: widow/orphan control by
393 * discouraging breaking one line from the end of
396 if (pdata
->first
!= pdata
->last
) {
397 pdata
->first
->penalty_after
= 100000;
398 pdata
->last
->penalty_before
= 100000;
406 if (p
->private_data
) {
407 pdata
= (para_data
*)p
->private_data
;
410 * Set the line spacing for each line in this paragraph.
412 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
413 if (ldata
== pdata
->first
)
414 ldata
->space_before
= base_para_spacing
/ 2;
416 ldata
->space_before
= base_leading
/ 2;
417 if (ldata
== pdata
->last
)
418 ldata
->space_after
= base_para_spacing
/ 2;
420 ldata
->space_after
= base_leading
/ 2;
421 ldata
->page_break
= FALSE
;
425 * Some kinds of section heading do require a page
428 if (p
->type
== para_Title
||
429 p
->type
== para_Chapter
||
430 p
->type
== para_Appendix
||
431 p
->type
== para_UnnumberedChapter
) {
432 pdata
->first
->page_break
= TRUE
;
433 pdata
->first
->space_before
= chapter_top_space
;
434 pdata
->last
->space_after
+=
435 chapter_underline_depth
+ chapter_underline_thickness
;
439 * Link all line structures together into a big list.
443 lastline
->next
= pdata
->first
;
444 pdata
->first
->prev
= lastline
;
446 firstline
= pdata
->first
;
447 pdata
->first
->prev
= NULL
;
449 lastline
= pdata
->last
;
455 * Now we have an enormous linked list of every line of text in
456 * the document. Break it up into pages.
458 pages
= page_breaks(firstline
, lastline
, page_height
);
461 * Now we're ready to actually lay out the pages. We do this by
462 * looping over _paragraphs_, since we may need to track cross-
463 * references between lines and even across pages.
465 for (p
= sourceform
; p
; p
= p
->next
) {
466 pdata
= (para_data
*)p
->private_data
;
471 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
472 render_line(ldata
, left_margin
, paper_height
- top_margin
,
474 if (ldata
== pdata
->last
)
479 * Some section headings (FIXME: should be configurable
480 * which) want to be underlined.
482 if (p
->type
== para_Chapter
|| p
->type
== para_Appendix
||
483 p
->type
== para_UnnumberedChapter
|| p
->type
== para_Title
) {
484 add_rect_to_page(pdata
->last
->page
,
486 (paper_height
- top_margin
-
487 pdata
->last
->ypos
- chapter_underline_depth
),
489 chapter_underline_thickness
);
494 doc
= mknew(document
);
495 doc
->fonts
= fontlist
;
497 doc
->paper_width
= paper_width
;
498 doc
->paper_height
= paper_height
;
502 static font_encoding
*new_font_encoding(font_data
*font
)
507 fe
= mknew(font_encoding
);
510 if (font
->list
->tail
)
511 font
->list
->tail
->next
= fe
;
513 font
->list
->head
= fe
;
514 font
->list
->tail
= fe
;
519 for (i
= 0; i
< 256; i
++) {
520 fe
->vector
[i
] = NULL
;
522 fe
->to_unicode
[i
] = 0xFFFF;
528 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
536 widths
= ps_std_font_widths(name
);
540 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
542 f
= mknew(font_data
);
546 f
->nglyphs
= nglyphs
;
547 f
->glyphs
= ps_std_glyphs
;
549 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
552 * Our first subfont will contain all of US-ASCII. This isn't
553 * really necessary - we could just create custom subfonts
554 * precisely as the whim of render_string dictated - but
555 * instinct suggests that it might be nice to have the text in
556 * the output files look _marginally_ recognisable.
558 fe
= new_font_encoding(f
);
559 fe
->free_pos
= 0xA1; /* only the top half is free */
560 f
->latest_subfont
= fe
;
562 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
565 for (i
= 0; i
< nglyphs
; i
++) {
567 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
568 assert(ucs
!= 0xFFFF);
570 if (ucs
>= 0x20 && ucs
<= 0x7E) {
571 fe
->vector
[ucs
] = f
->glyphs
[i
];
572 fe
->indices
[ucs
] = i
;
573 fe
->to_unicode
[ucs
] = ucs
;
574 f
->subfont_map
[i
].subfont
= fe
;
575 f
->subfont_map
[i
].position
= ucs
;
578 * This character is not yet assigned to a subfont.
580 f
->subfont_map
[i
].subfont
= NULL
;
581 f
->subfont_map
[i
].position
= 0;
588 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
595 for (; *string
; string
++) {
598 index
= font
->bmp
[(unsigned short)*string
];
599 if (index
== 0xFFFF) {
603 width
+= font
->widths
[index
];
610 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
612 struct paper_width_ctx
{
617 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
619 while (text
&& text
!= end
) {
620 w
+= paper_width_internal(vctx
, text
, nspaces
);
626 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
628 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
629 int style
, type
, findex
, width
, errs
;
632 switch (word
->type
) {
642 style
= towordstyle(word
->type
);
643 type
= removeattr(word
->type
);
645 findex
= (style
== word_Normal ? FONT_NORMAL
:
646 style
== word_Emph ? FONT_EMPH
:
649 if (type
== word_Normal
) {
651 } else if (type
== word_WhiteSpace
) {
652 if (findex
!= FONT_CODE
) {
655 return ctx
->minspacewidth
;
658 } else /* if (type == word_Quote) */ {
659 if (word
->aux
== quote_Open
)
660 str
= L
"\x2018"; /* FIXME: configurability! */
662 str
= L
"\x2019"; /* FIXME: configurability! */
665 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
667 if (errs
&& word
->alt
)
668 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
670 return ctx
->pdata
->sizes
[findex
] * width
;
673 static int paper_width(void *vctx
, word
*word
)
675 return paper_width_internal(vctx
, word
, NULL
);
678 static int paper_width_simple(para_data
*pdata
, word
*text
)
680 struct paper_width_ctx ctx
;
684 (pdata
->sizes
[FONT_NORMAL
] *
685 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
687 return paper_width_list(&ctx
, text
, NULL
, NULL
);
690 static void wrap_paragraph(para_data
*pdata
, word
*words
,
691 int w
, int i1
, int i2
)
693 wrappedline
*wrapping
, *p
;
695 struct paper_width_ctx ctx
;
699 * We're going to need to store the line height in every line
700 * structure we generate.
705 for (i
= 0; i
< NFONTS
; i
++)
706 if (line_height
< pdata
->sizes
[i
])
707 line_height
= pdata
->sizes
[i
];
711 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
712 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
713 if (spacewidth
== 0) {
715 * A font without a space?! Disturbing. I hope this never
716 * comes up, but I'll make a random guess anyway and set my
717 * space width to half the point size.
719 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
723 * I'm going to set the _minimum_ space width to 3/5 of the
724 * standard one, and use the standard one as the optimum.
726 ctx
.minspacewidth
= spacewidth
* 3 / 5;
729 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
732 * Having done the wrapping, we now concoct a set of line_data
735 pdata
->first
= pdata
->last
= NULL
;
737 for (p
= wrapping
; p
; p
= p
->next
) {
740 int len
, wid
, spaces
;
742 ldata
= mknew(line_data
);
744 ldata
->pdata
= pdata
;
745 ldata
->first
= p
->begin
;
747 ldata
->line_height
= line_height
;
749 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
752 pdata
->last
->next
= ldata
;
753 ldata
->prev
= pdata
->last
;
755 pdata
->first
= ldata
;
762 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
763 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
766 ldata
->hshortfall
= wid
- len
;
767 ldata
->nspaces
= spaces
;
769 * This tells us how much the space width needs to
770 * change from _min_spacewidth. But we want to store
771 * its difference from the _natural_ space width, to
772 * make the text rendering easier.
774 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
775 ldata
->hshortfall
-= spacewidth
* spaces
;
777 * Special case: on the last line of a paragraph, we
778 * never stretch spaces.
780 if (ldata
->hshortfall
> 0 && !p
->next
)
781 ldata
->hshortfall
= 0;
783 ldata
->aux_text
= NULL
;
784 ldata
->aux_text_2
= NULL
;
785 ldata
->aux_left_indent
= 0;
786 ldata
->penalty_before
= ldata
->penalty_after
= 0;
791 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
798 * Page breaking is done by a close analogue of the optimal
799 * paragraph wrapping algorithm used by wrap_para(). We work
800 * backwards from the end of the document line by line; for
801 * each line, we contemplate every possible number of lines we
802 * could put on a page starting with that line, determine a
803 * cost function for each one, add it to the pre-computed cost
804 * function for optimally page-breaking everything after that
805 * page, and pick the best option.
807 * Since my line_data structures are only used for this
808 * purpose, I might as well just store the algorithm data
812 for (l
= last
; l
; l
= l
->prev
) {
813 int minheight
, text
= 0, space
= 0;
817 for (m
= l
; m
; m
= m
->next
) {
818 if (m
!= l
&& m
->page_break
)
819 break; /* we've gone as far as we can */
822 space
+= m
->prev
->space_after
;
823 if (m
!= l
|| m
->page_break
)
824 space
+= m
->space_before
;
825 text
+= m
->line_height
;
826 minheight
= text
+ space
;
828 if (m
!= l
&& minheight
> page_height
)
832 * Compute the cost of this arrangement, as the square
833 * of the amount of wasted space on the page.
834 * Exception: if this is the last page before a
835 * mandatory break or the document end, we don't
836 * penalise a large blank area.
838 if (m
->next
&& !m
->next
->page_break
)
840 int x
= page_height
- minheight
;
847 cost
+= (x
* xf
) >> 8;
851 if (m
->next
&& !m
->next
->page_break
) {
852 cost
+= m
->penalty_after
;
853 cost
+= m
->next
->penalty_before
;
856 if (m
->next
&& !m
->next
->page_break
)
857 cost
+= m
->next
->bestcost
;
858 if (l
->bestcost
== -1 || l
->bestcost
> cost
) {
860 * This is the best option yet for this starting
864 if (m
->next
&& !m
->next
->page_break
)
865 l
->vshortfall
= page_height
- minheight
;
876 * Now go through the line list forwards and assemble the
886 page
= mknew(page_data
);
895 page
->first_line
= l
;
896 page
->last_line
= l
->page_last
;
898 page
->first_text
= page
->last_text
= NULL
;
899 page
->first_xref
= page
->last_xref
= NULL
;
900 page
->first_rect
= page
->last_rect
= NULL
;
903 * Now assign a y-coordinate to each line on the page.
906 for (l
= page
->first_line
; l
; l
= l
->next
) {
907 if (l
!= page
->first_line
)
908 space
+= l
->prev
->space_after
;
909 if (l
!= page
->first_line
|| l
->page_break
)
910 space
+= l
->space_before
;
911 text
+= l
->line_height
;
914 l
->ypos
= text
+ space
+
915 space
* (float)page
->first_line
->vshortfall
/
916 page
->first_line
->space
;
918 if (l
== page
->last_line
)
922 l
= page
->last_line
->next
;
928 static void add_rect_to_page(page_data
*page
, int x
, int y
, int w
, int h
)
930 rect
*r
= mknew(rect
);
934 page
->last_rect
->next
= r
;
936 page
->first_rect
= r
;
945 static void add_string_to_page(page_data
*page
, int x
, int y
,
946 font_encoding
*fe
, int size
, char *text
)
950 frag
= mknew(text_fragment
);
954 page
->last_text
->next
= frag
;
956 page
->first_text
= frag
;
957 page
->last_text
= frag
;
962 frag
->fontsize
= size
;
963 frag
->text
= dupstr(text
);
967 * Returns the updated x coordinate.
969 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
970 int x
, int y
, wchar_t *str
)
973 int textpos
, textwid
, glyph
;
974 font_encoding
*subfont
= NULL
, *sf
;
976 text
= mknewa(char, 1 + ustrlen(str
));
977 textpos
= textwid
= 0;
980 glyph
= font
->bmp
[*str
];
983 continue; /* nothing more we can do here */
986 * Find which subfont this character is going in.
988 sf
= font
->subfont_map
[glyph
].subfont
;
994 * This character is not yet in a subfont. Assign one.
996 if (font
->latest_subfont
->free_pos
>= 0x100)
997 font
->latest_subfont
= new_font_encoding(font
);
999 c
= font
->latest_subfont
->free_pos
++;
1000 if (font
->latest_subfont
->free_pos
== 0x7F)
1001 font
->latest_subfont
->free_pos
= 0xA1;
1003 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
1004 font
->subfont_map
[glyph
].position
= c
;
1005 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
1006 font
->latest_subfont
->indices
[c
] = glyph
;
1007 font
->latest_subfont
->to_unicode
[c
] = *str
;
1009 sf
= font
->latest_subfont
;
1012 if (!subfont
|| sf
!= subfont
) {
1014 text
[textpos
] = '\0';
1015 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1018 assert(textpos
== 0);
1024 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
1025 textwid
+= font
->widths
[glyph
] * fontsize
;
1031 text
[textpos
] = '\0';
1032 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
1040 * Returns the updated x coordinate.
1042 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
1043 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
1044 int shortfall
, int nspaces
, int *nspace
,
1045 keywordlist
*keywords
)
1047 while (text
&& text
!= text_end
) {
1048 int style
, type
, findex
, errs
;
1052 switch (text
->type
) {
1054 * Start a cross-reference.
1056 case word_HyperLink
:
1057 case word_UpperXref
:
1058 case word_LowerXref
:
1060 if (text
->type
== word_HyperLink
) {
1062 dest
.url
= utoa_dup(text
->text
);
1065 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1069 assert(kwl
->para
->private_data
);
1070 pdata
= (para_data
*) kwl
->para
->private_data
;
1072 dest
.page
= pdata
->first
->page
;
1076 * Shouldn't happen, but *shrug*
1083 if (dest
.type
!= NONE
) {
1085 (*xr
)->dest
= dest
; /* structure copy */
1086 if (page
->last_xref
)
1087 page
->last_xref
->next
= *xr
;
1089 page
->first_xref
= *xr
;
1090 page
->last_xref
= *xr
;
1094 * FIXME: Ideally we should have, and use, some
1095 * vertical font metric information here so that
1096 * our cross-ref rectangle can take account of
1097 * descenders and the font's cap height. This will
1098 * do for the moment, but it isn't ideal.
1100 (*xr
)->lx
= (*xr
)->rx
= x
;
1102 (*xr
)->ty
= y
+ ldata
->line_height
;
1107 * Finish extending a cross-reference box.
1117 * FIXME: we should do something with this.
1121 style
= towordstyle(text
->type
);
1122 type
= removeattr(text
->type
);
1124 findex
= (style
== word_Normal ? FONT_NORMAL
:
1125 style
== word_Emph ? FONT_EMPH
:
1128 if (type
== word_Normal
) {
1130 } else if (type
== word_WhiteSpace
) {
1131 x
+= pdata
->sizes
[findex
] *
1132 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1133 if (nspaces
&& findex
!= FONT_CODE
) {
1134 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1135 x
-= *nspace
* shortfall
/ nspaces
;
1139 } else /* if (type == word_Quote) */ {
1140 if (text
->aux
== quote_Open
)
1141 str
= L
"\x2018"; /* FIXME: configurability! */
1143 str
= L
"\x2019"; /* FIXME: configurability! */
1146 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1148 if (errs
&& text
->alt
)
1149 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1150 xr
, shortfall
, nspaces
, nspace
, keywords
);
1152 x
= render_string(page
, pdata
->fonts
[findex
],
1153 pdata
->sizes
[findex
], x
, y
, str
);
1165 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
1166 xref_dest
*dest
, keywordlist
*keywords
)
1171 if (ldata
->aux_text
) {
1175 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1176 left_x
+ ldata
->aux_left_indent
,
1177 top_y
- ldata
->ypos
,
1178 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1179 if (ldata
->aux_text_2
)
1180 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1181 x
, top_y
- ldata
->ypos
,
1182 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1187 * There might be a cross-reference carried over from a
1190 if (dest
->type
!= NONE
) {
1193 xr
->dest
= *dest
; /* structure copy */
1194 if (ldata
->page
->last_xref
)
1195 ldata
->page
->last_xref
->next
= xr
;
1197 ldata
->page
->first_xref
= xr
;
1198 ldata
->page
->last_xref
= xr
;
1199 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1200 xr
->by
= top_y
- ldata
->ypos
;
1201 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1205 render_text(ldata
->page
, ldata
->pdata
, ldata
, left_x
+ ldata
->xpos
,
1206 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
, &xr
,
1207 ldata
->hshortfall
, ldata
->nspaces
, &nspace
, keywords
);
1211 * There's a cross-reference continued on to the next line.
1218 static void code_paragraph(para_data
*pdata
,
1219 font_data
*fn
, font_data
*fi
, font_data
*fb
,
1220 int font_size
, int indent
, word
*words
)
1223 * For code paragraphs, I'm going to hack grievously and
1224 * pretend the three normal fonts are the three code paragraph
1227 pdata
->fonts
[FONT_NORMAL
] = fb
;
1228 pdata
->fonts
[FONT_EMPH
] = fi
;
1229 pdata
->fonts
[FONT_CODE
] = fn
;
1230 pdata
->sizes
[FONT_NORMAL
] =
1231 pdata
->sizes
[FONT_EMPH
] =
1232 pdata
->sizes
[FONT_CODE
] = font_size
;
1234 pdata
->first
= pdata
->last
= NULL
;
1236 for (; words
; words
= words
->next
) {
1237 wchar_t *t
, *e
, *start
;
1238 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1240 int prev
= -1, curr
;
1243 if (words
->next
&& words
->next
->type
== word_Emph
) {
1244 e
= words
->next
->text
;
1245 words
= words
->next
;
1255 else if (*e
== L
'i')
1257 else if (*e
== L
'b')
1274 * We've isolated a maximal subsequence of the line
1275 * which has the same emphasis. Form it into a word
1281 w
->type
= (prev
== 0 ? word_WeakCode
:
1282 prev
== 1 ? word_Emph
: word_Normal
);
1283 w
->text
= mknewa(wchar_t, t
-start
+1);
1284 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
1285 w
->text
[t
-start
] = '\0';
1298 ldata
= mknew(line_data
);
1300 ldata
->pdata
= pdata
;
1301 ldata
->first
= lhead
;
1303 ldata
->line_height
= font_size
* 4096;
1305 ldata
->xpos
= indent
;
1308 pdata
->last
->next
= ldata
;
1309 ldata
->prev
= pdata
->last
;
1311 pdata
->first
= ldata
;
1315 pdata
->last
= ldata
;
1317 ldata
->hshortfall
= 0;
1319 ldata
->aux_text
= NULL
;
1320 ldata
->aux_text_2
= NULL
;
1321 ldata
->aux_left_indent
= 0;
1322 /* General opprobrium for breaking in a code paragraph. */
1323 ldata
->penalty_before
= ldata
->penalty_after
= 50000;