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 * - tune the page breaking algorithm to impose penalties on
17 * * breaking in the middle of a code paragraph
18 * * breaking one line from the start or end of a paragraph
19 * * breaking immediately after a heading of any kind (or
21 * * we may also need to impose a limit on the amount by which
22 * we can _stretch_ a page; after a certain point we may
23 * prefer just to unapologetically leave space at the bottom.
25 * - implement some simple graphics
26 * * I had an underline below chapter headings in the original
27 * Perl version, and I thought it looked rather nice
28 * * also we need para_Rule.
30 * - set up contents section now we know what sections begin on
37 * - header/footer? Page numbers at least would be handy. Fully
38 * configurable footer can wait, though.
40 * That should bring us to the same level of functionality that
41 * original-Halibut had, and the same in PDF plus the obvious
42 * interactive navigation features. After that, in future work:
44 * - linearised PDF, perhaps?
46 * - I'm uncertain of whether I need to include a ToUnicode CMap
47 * in each of my font definitions in PDF. Currently things (by
48 * which I mean cut and paste out of acroread) seem to be
49 * working fairly happily without it, but I don't know.
62 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
63 static void wrap_paragraph(para_data
*pdata
, word
*words
,
64 int w
, int i1
, int i2
);
65 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
67 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
68 xref_dest
*dest
, keywordlist
*keywords
);
69 static int paper_width_simple(para_data
*pdata
, word
*text
);
70 static void code_paragraph(para_data
*pdata
,
71 font_data
*fn
, font_data
*fi
, font_data
*fb
,
72 int font_size
, int indent
, word
*words
);
74 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
78 int indent
, extra_indent
, firstline_indent
, aux_indent
;
80 line_data
*ldata
, *firstline
, *lastline
;
81 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
87 * FIXME: All these things ought to become configurable.
89 int paper_width
= 595 * 4096;
90 int paper_height
= 841 * 4096;
91 int left_margin
= 72 * 4096;
92 int top_margin
= 72 * 4096;
93 int right_margin
= 72 * 4096;
94 int bottom_margin
= 108 * 4096;
95 int indent_list_bullet
= 6 * 4096;
96 int indent_list
= 24 * 4096;
97 int indent_quote
= 18 * 4096;
98 int base_leading
= 4096;
99 int base_para_spacing
= 10 * 4096;
100 int chapter_top_space
= 72 * 4096;
101 int sect_num_left_space
= 12 * 4096;
103 int base_width
= paper_width
- left_margin
- right_margin
;
104 int page_height
= paper_height
- top_margin
- bottom_margin
;
106 IGNORE(idx
); /* FIXME */
109 * First, set up some font structures.
111 fontlist
= mknew(font_list
);
112 fontlist
->head
= fontlist
->tail
= NULL
;
113 tr
= make_std_font(fontlist
, "Times-Roman");
114 ti
= make_std_font(fontlist
, "Times-Italic");
115 hr
= make_std_font(fontlist
, "Helvetica-Bold");
116 hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
117 cr
= make_std_font(fontlist
, "Courier");
118 co
= make_std_font(fontlist
, "Courier-Oblique");
119 cb
= make_std_font(fontlist
, "Courier-Bold");
122 * Go through and break up each paragraph into lines.
125 firstline
= lastline
= NULL
;
126 for (p
= sourceform
; p
; p
= p
->next
) {
127 p
->private_data
= NULL
;
131 * These paragraph types are either invisible or don't
132 * define text in the normal sense. Either way, they
133 * don't require wrapping.
139 case para_NotParaType
:
146 * These paragraph types don't require wrapping, but
147 * they do affect the line width to which we wrap the
148 * rest of the paragraphs, so we need to pay attention.
151 indent
+= indent_list
; break;
153 indent
-= indent_list
; assert(indent
>= 0); break;
155 indent
+= indent_quote
; break;
157 indent
-= indent_quote
; assert(indent
>= 0); break;
160 * This paragraph type is special. Process it
164 pdata
= mknew(para_data
);
165 code_paragraph(pdata
, cr
, co
, cb
, 12, indent
, p
->words
);
166 p
->private_data
= pdata
;
170 * All of these paragraph types require wrapping in the
171 * ordinary way. So we must supply a set of fonts, a
172 * line width and auxiliary information (e.g. bullet
173 * text) for each one.
177 case para_UnnumberedChapter
:
181 case para_BiblioCited
:
183 case para_NumberedList
:
184 case para_DescribedThing
:
185 case para_Description
:
188 pdata
= mknew(para_data
);
191 * Choose fonts for this paragraph.
193 * FIXME: All of this ought to be completely
198 pdata
->fonts
[FONT_NORMAL
] = hr
;
199 pdata
->sizes
[FONT_NORMAL
] = 24;
200 pdata
->fonts
[FONT_EMPH
] = hi
;
201 pdata
->sizes
[FONT_EMPH
] = 24;
202 pdata
->fonts
[FONT_CODE
] = cb
;
203 pdata
->sizes
[FONT_CODE
] = 24;
208 case para_UnnumberedChapter
:
209 pdata
->fonts
[FONT_NORMAL
] = hr
;
210 pdata
->sizes
[FONT_NORMAL
] = 20;
211 pdata
->fonts
[FONT_EMPH
] = hi
;
212 pdata
->sizes
[FONT_EMPH
] = 20;
213 pdata
->fonts
[FONT_CODE
] = cb
;
214 pdata
->sizes
[FONT_CODE
] = 20;
219 pdata
->fonts
[FONT_NORMAL
] = hr
;
220 pdata
->fonts
[FONT_EMPH
] = hi
;
221 pdata
->fonts
[FONT_CODE
] = cb
;
222 pdata
->sizes
[FONT_NORMAL
] =
223 pdata
->sizes
[FONT_EMPH
] =
224 pdata
->sizes
[FONT_CODE
] =
225 (p
->aux
== 0 ?
16 : p
->aux
== 1 ?
14 : 13);
229 case para_BiblioCited
:
231 case para_NumberedList
:
232 case para_DescribedThing
:
233 case para_Description
:
235 pdata
->fonts
[FONT_NORMAL
] = tr
;
236 pdata
->sizes
[FONT_NORMAL
] = 12;
237 pdata
->fonts
[FONT_EMPH
] = ti
;
238 pdata
->sizes
[FONT_EMPH
] = 12;
239 pdata
->fonts
[FONT_CODE
] = cr
;
240 pdata
->sizes
[FONT_CODE
] = 12;
245 * Also select an indentation level depending on the
246 * paragraph type (list paragraphs other than
247 * para_DescribedThing need extra indent).
249 * (FIXME: Perhaps at some point we might even arrange
250 * for the user to be able to request indented first
251 * lines in paragraphs.)
253 if (p
->type
== para_Bullet
||
254 p
->type
== para_NumberedList
||
255 p
->type
== para_Description
) {
256 extra_indent
= firstline_indent
= indent_list
;
258 extra_indent
= firstline_indent
= 0;
262 * Find the auxiliary text for this paragraph.
273 * For some heading styles (FIXME: be able to
274 * configure which), the auxiliary text contains
275 * the chapter number and is arranged to be
276 * right-aligned a few points left of the primary
277 * margin. For other styles, the auxiliary text is
278 * the full chapter _name_ and takes up space
279 * within the (wrapped) chapter title, meaning that
280 * we must move the first line indent over to make
283 if (p
->type
== para_Heading
|| p
->type
== para_Subsect
) {
287 len
= paper_width_simple(pdata
, p
->kwtext2
);
288 aux_indent
= -len
- sect_num_left_space
;
294 aux2
->type
= word_Normal
;
295 aux2
->text
= ustrdup(L
": ");
296 aux2
->breaks
= FALSE
;
300 firstline_indent
+= paper_width_simple(pdata
, aux
);
301 firstline_indent
+= paper_width_simple(pdata
, aux2
);
307 * Auxiliary text consisting of a bullet. (FIXME:
308 * configurable bullet.)
313 aux
->type
= word_Normal
;
314 aux
->text
= ustrdup(L
"\x2022");
317 aux_indent
= indent
+ indent_list_bullet
;
320 case para_NumberedList
:
322 * Auxiliary text consisting of the number followed
323 * by a (FIXME: configurable) full stop.
329 aux2
->type
= word_Normal
;
330 aux2
->text
= ustrdup(L
".");
331 aux2
->breaks
= FALSE
;
333 aux_indent
= indent
+ indent_list_bullet
;
336 case para_BiblioCited
:
338 * Auxiliary text consisting of the bibliography
339 * reference text, and a trailing space.
345 aux2
->type
= word_Normal
;
346 aux2
->text
= ustrdup(L
" ");
347 aux2
->breaks
= FALSE
;
350 firstline_indent
+= paper_width_simple(pdata
, aux
);
351 firstline_indent
+= paper_width_simple(pdata
, aux2
);
355 wrap_paragraph(pdata
, p
->words
, base_width
,
356 indent
+ firstline_indent
,
357 indent
+ extra_indent
);
359 p
->private_data
= pdata
;
361 pdata
->first
->aux_text
= aux
;
362 pdata
->first
->aux_text_2
= aux2
;
363 pdata
->first
->aux_left_indent
= aux_indent
;
368 if (p
->private_data
) {
369 pdata
= (para_data
*)p
->private_data
;
372 * Set the line spacing for each line in this paragraph.
374 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
375 if (ldata
== pdata
->first
)
376 ldata
->space_before
= base_para_spacing
/ 2;
378 ldata
->space_before
= base_leading
/ 2;
379 if (ldata
== pdata
->last
)
380 ldata
->space_after
= base_para_spacing
/ 2;
382 ldata
->space_after
= base_leading
/ 2;
383 ldata
->page_break
= FALSE
;
387 * Some kinds of section heading do require a page
390 if (p
->type
== para_Title
||
391 p
->type
== para_Chapter
||
392 p
->type
== para_Appendix
||
393 p
->type
== para_UnnumberedChapter
) {
394 pdata
->first
->page_break
= TRUE
;
395 pdata
->first
->space_before
= chapter_top_space
;
399 * Link all line structures together into a big list.
403 lastline
->next
= pdata
->first
;
404 pdata
->first
->prev
= lastline
;
406 firstline
= pdata
->first
;
407 pdata
->first
->prev
= NULL
;
409 lastline
= pdata
->last
;
415 * Now we have an enormous linked list of every line of text in
416 * the document. Break it up into pages.
418 pages
= page_breaks(firstline
, lastline
, page_height
);
421 * Now we're ready to actually lay out the pages. We do this by
422 * looping over _paragraphs_, since we may need to track cross-
423 * references between lines and even across pages.
425 for (p
= sourceform
; p
; p
= p
->next
) {
426 pdata
= (para_data
*)p
->private_data
;
431 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
432 render_line(ldata
, left_margin
, paper_height
- top_margin
,
434 if (ldata
== pdata
->last
)
440 doc
= mknew(document
);
441 doc
->fonts
= fontlist
;
443 doc
->paper_width
= paper_width
;
444 doc
->paper_height
= paper_height
;
448 static font_encoding
*new_font_encoding(font_data
*font
)
453 fe
= mknew(font_encoding
);
456 if (font
->list
->tail
)
457 font
->list
->tail
->next
= fe
;
459 font
->list
->head
= fe
;
460 font
->list
->tail
= fe
;
465 for (i
= 0; i
< 256; i
++) {
466 fe
->vector
[i
] = NULL
;
468 fe
->to_unicode
[i
] = 0xFFFF;
474 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
482 widths
= ps_std_font_widths(name
);
486 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
488 f
= mknew(font_data
);
492 f
->nglyphs
= nglyphs
;
493 f
->glyphs
= ps_std_glyphs
;
495 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
498 * Our first subfont will contain all of US-ASCII. This isn't
499 * really necessary - we could just create custom subfonts
500 * precisely as the whim of render_string dictated - but
501 * instinct suggests that it might be nice to have the text in
502 * the output files look _marginally_ recognisable.
504 fe
= new_font_encoding(f
);
505 fe
->free_pos
= 0xA1; /* only the top half is free */
506 f
->latest_subfont
= fe
;
508 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
511 for (i
= 0; i
< nglyphs
; i
++) {
513 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
514 assert(ucs
!= 0xFFFF);
516 if (ucs
>= 0x20 && ucs
<= 0x7E) {
517 fe
->vector
[ucs
] = f
->glyphs
[i
];
518 fe
->indices
[ucs
] = i
;
519 fe
->to_unicode
[ucs
] = ucs
;
520 f
->subfont_map
[i
].subfont
= fe
;
521 f
->subfont_map
[i
].position
= ucs
;
524 * This character is not yet assigned to a subfont.
526 f
->subfont_map
[i
].subfont
= NULL
;
527 f
->subfont_map
[i
].position
= 0;
534 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
541 for (; *string
; string
++) {
544 index
= font
->bmp
[(unsigned short)*string
];
545 if (index
== 0xFFFF) {
549 width
+= font
->widths
[index
];
556 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
558 struct paper_width_ctx
{
563 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
565 while (text
&& text
!= end
) {
566 w
+= paper_width_internal(vctx
, text
, nspaces
);
572 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
574 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
575 int style
, type
, findex
, width
, errs
;
578 switch (word
->type
) {
588 style
= towordstyle(word
->type
);
589 type
= removeattr(word
->type
);
591 findex
= (style
== word_Normal ? FONT_NORMAL
:
592 style
== word_Emph ? FONT_EMPH
:
595 if (type
== word_Normal
) {
597 } else if (type
== word_WhiteSpace
) {
598 if (findex
!= FONT_CODE
) {
601 return ctx
->minspacewidth
;
604 } else /* if (type == word_Quote) */ {
605 if (word
->aux
== quote_Open
)
606 str
= L
"\x2018"; /* FIXME: configurability! */
608 str
= L
"\x2019"; /* FIXME: configurability! */
611 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
613 if (errs
&& word
->alt
)
614 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
616 return ctx
->pdata
->sizes
[findex
] * width
;
619 static int paper_width(void *vctx
, word
*word
)
621 return paper_width_internal(vctx
, word
, NULL
);
624 static int paper_width_simple(para_data
*pdata
, word
*text
)
626 struct paper_width_ctx ctx
;
630 (pdata
->sizes
[FONT_NORMAL
] *
631 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
633 return paper_width_list(&ctx
, text
, NULL
, NULL
);
636 static void wrap_paragraph(para_data
*pdata
, word
*words
,
637 int w
, int i1
, int i2
)
639 wrappedline
*wrapping
, *p
;
641 struct paper_width_ctx ctx
;
645 * We're going to need to store the line height in every line
646 * structure we generate.
651 for (i
= 0; i
< NFONTS
; i
++)
652 if (line_height
< pdata
->sizes
[i
])
653 line_height
= pdata
->sizes
[i
];
657 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
658 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
659 if (spacewidth
== 0) {
661 * A font without a space?! Disturbing. I hope this never
662 * comes up, but I'll make a random guess anyway and set my
663 * space width to half the point size.
665 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
669 * I'm going to set the _minimum_ space width to 3/5 of the
670 * standard one, and use the standard one as the optimum.
672 ctx
.minspacewidth
= spacewidth
* 3 / 5;
675 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
678 * Having done the wrapping, we now concoct a set of line_data
681 pdata
->first
= pdata
->last
= NULL
;
683 for (p
= wrapping
; p
; p
= p
->next
) {
686 int len
, wid
, spaces
;
688 ldata
= mknew(line_data
);
690 ldata
->pdata
= pdata
;
691 ldata
->first
= p
->begin
;
693 ldata
->line_height
= line_height
;
695 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
698 pdata
->last
->next
= ldata
;
699 ldata
->prev
= pdata
->last
;
701 pdata
->first
= ldata
;
708 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
709 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
712 ldata
->hshortfall
= wid
- len
;
713 ldata
->nspaces
= spaces
;
715 * This tells us how much the space width needs to
716 * change from _min_spacewidth. But we want to store
717 * its difference from the _natural_ space width, to
718 * make the text rendering easier.
720 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
721 ldata
->hshortfall
-= spacewidth
* spaces
;
723 * Special case: on the last line of a paragraph, we
724 * never stretch spaces.
726 if (ldata
->hshortfall
> 0 && !p
->next
)
727 ldata
->hshortfall
= 0;
729 ldata
->aux_text
= NULL
;
730 ldata
->aux_text_2
= NULL
;
731 ldata
->aux_left_indent
= 0;
736 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
743 * Page breaking is done by a close analogue of the optimal
744 * paragraph wrapping algorithm used by wrap_para(). We work
745 * backwards from the end of the document line by line; for
746 * each line, we contemplate every possible number of lines we
747 * could put on a page starting with that line, determine a
748 * cost function for each one, add it to the pre-computed cost
749 * function for optimally page-breaking everything after that
750 * page, and pick the best option.
752 * Since my line_data structures are only used for this
753 * purpose, I might as well just store the algorithm data
757 for (l
= last
; l
; l
= l
->prev
) {
758 int minheight
, text
= 0, space
= 0;
762 for (m
= l
; m
; m
= m
->next
) {
763 if (m
!= l
&& m
->page_break
)
764 break; /* we've gone as far as we can */
767 space
+= m
->prev
->space_after
;
768 if (m
!= l
|| m
->page_break
)
769 space
+= m
->space_before
;
770 text
+= m
->line_height
;
771 minheight
= text
+ space
;
773 if (m
!= l
&& minheight
> page_height
)
777 * Compute the cost of this arrangement, as the square
778 * of the amount of wasted space on the page.
779 * Exception: if this is the last page before a
780 * mandatory break or the document end, we don't
781 * penalise a large blank area.
783 if (m
->next
&& !m
->next
->page_break
)
785 int x
= page_height
- minheight
;
792 cost
+= (x
* xf
) >> 8;
797 * FIXME: here I should introduce penalties for
798 * breaking in mid-paragraph, particularly very close
799 * to one end of a paragraph and particularly in code
803 if (m
->next
&& !m
->next
->page_break
)
804 cost
+= m
->next
->bestcost
;
806 if (l
->bestcost
== -1 || l
->bestcost
> cost
) {
808 * This is the best option yet for this starting
812 if (m
->next
&& !m
->next
->page_break
)
813 l
->vshortfall
= page_height
- minheight
;
824 * Now go through the line list forwards and assemble the
834 page
= mknew(page_data
);
843 page
->first_line
= l
;
844 page
->last_line
= l
->page_last
;
846 page
->first_text
= page
->last_text
= NULL
;
848 page
->first_xref
= page
->last_xref
= NULL
;
851 * Now assign a y-coordinate to each line on the page.
854 for (l
= page
->first_line
; l
; l
= l
->next
) {
855 if (l
!= page
->first_line
)
856 space
+= l
->prev
->space_after
;
857 if (l
!= page
->first_line
|| l
->page_break
)
858 space
+= l
->space_before
;
859 text
+= l
->line_height
;
862 l
->ypos
= text
+ space
+
863 space
* (float)page
->first_line
->vshortfall
/
864 page
->first_line
->space
;
866 if (l
== page
->last_line
)
870 l
= page
->last_line
->next
;
876 static void add_string_to_page(page_data
*page
, int x
, int y
,
877 font_encoding
*fe
, int size
, char *text
)
881 frag
= mknew(text_fragment
);
885 page
->last_text
->next
= frag
;
887 page
->first_text
= frag
;
888 page
->last_text
= frag
;
893 frag
->fontsize
= size
;
894 frag
->text
= dupstr(text
);
898 * Returns the updated x coordinate.
900 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
901 int x
, int y
, wchar_t *str
)
904 int textpos
, textwid
, glyph
;
905 font_encoding
*subfont
= NULL
, *sf
;
907 text
= mknewa(char, 1 + ustrlen(str
));
908 textpos
= textwid
= 0;
911 glyph
= font
->bmp
[*str
];
914 continue; /* nothing more we can do here */
917 * Find which subfont this character is going in.
919 sf
= font
->subfont_map
[glyph
].subfont
;
925 * This character is not yet in a subfont. Assign one.
927 if (font
->latest_subfont
->free_pos
>= 0x100)
928 font
->latest_subfont
= new_font_encoding(font
);
930 c
= font
->latest_subfont
->free_pos
++;
931 if (font
->latest_subfont
->free_pos
== 0x7F)
932 font
->latest_subfont
->free_pos
= 0xA1;
934 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
935 font
->subfont_map
[glyph
].position
= c
;
936 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
937 font
->latest_subfont
->indices
[c
] = glyph
;
938 font
->latest_subfont
->to_unicode
[c
] = *str
;
940 sf
= font
->latest_subfont
;
943 if (!subfont
|| sf
!= subfont
) {
945 text
[textpos
] = '\0';
946 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
949 assert(textpos
== 0);
955 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
956 textwid
+= font
->widths
[glyph
] * fontsize
;
962 text
[textpos
] = '\0';
963 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
971 * Returns the updated x coordinate.
973 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
974 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
975 int shortfall
, int nspaces
, int *nspace
,
976 keywordlist
*keywords
)
978 while (text
&& text
!= text_end
) {
979 int style
, type
, findex
, errs
;
983 switch (text
->type
) {
985 * Start a cross-reference.
991 if (text
->type
== word_HyperLink
) {
993 dest
.url
= utoa_dup(text
->text
);
996 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
1000 assert(kwl
->para
->private_data
);
1001 pdata
= (para_data
*) kwl
->para
->private_data
;
1003 dest
.page
= pdata
->first
->page
;
1007 * Shouldn't happen, but *shrug*
1014 if (dest
.type
!= NONE
) {
1016 (*xr
)->dest
= dest
; /* structure copy */
1017 if (page
->last_xref
)
1018 page
->last_xref
->next
= *xr
;
1020 page
->first_xref
= *xr
;
1021 page
->last_xref
= *xr
;
1024 * FIXME: Ideally we should have, and use, some
1025 * vertical font metric information here so that
1026 * our cross-ref rectangle can take account of
1027 * descenders and the font's cap height. This will
1028 * do for the moment, but it isn't ideal.
1030 (*xr
)->lx
= (*xr
)->rx
= x
;
1032 (*xr
)->ty
= y
+ ldata
->line_height
;
1037 * Finish extending a cross-reference box.
1047 * FIXME: we should do something with this.
1051 style
= towordstyle(text
->type
);
1052 type
= removeattr(text
->type
);
1054 findex
= (style
== word_Normal ? FONT_NORMAL
:
1055 style
== word_Emph ? FONT_EMPH
:
1058 if (type
== word_Normal
) {
1060 } else if (type
== word_WhiteSpace
) {
1061 x
+= pdata
->sizes
[findex
] *
1062 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1063 if (nspaces
&& findex
!= FONT_CODE
) {
1064 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1065 x
-= *nspace
* shortfall
/ nspaces
;
1069 } else /* if (type == word_Quote) */ {
1070 if (text
->aux
== quote_Open
)
1071 str
= L
"\x2018"; /* FIXME: configurability! */
1073 str
= L
"\x2019"; /* FIXME: configurability! */
1076 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1078 if (errs
&& text
->alt
)
1079 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1080 xr
, shortfall
, nspaces
, nspace
, keywords
);
1082 x
= render_string(page
, pdata
->fonts
[findex
],
1083 pdata
->sizes
[findex
], x
, y
, str
);
1095 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
1096 xref_dest
*dest
, keywordlist
*keywords
)
1101 if (ldata
->aux_text
) {
1105 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1106 left_x
+ ldata
->aux_left_indent
,
1107 top_y
- ldata
->ypos
,
1108 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1109 if (ldata
->aux_text_2
)
1110 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1111 x
, top_y
- ldata
->ypos
,
1112 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1117 * There might be a cross-reference carried over from a
1120 if (dest
->type
!= NONE
) {
1122 xr
->dest
= *dest
; /* structure copy */
1123 if (ldata
->page
->last_xref
)
1124 ldata
->page
->last_xref
->next
= xr
;
1126 ldata
->page
->first_xref
= xr
;
1127 ldata
->page
->last_xref
= xr
;
1128 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1129 xr
->by
= top_y
- ldata
->ypos
;
1130 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1134 render_text(ldata
->page
, ldata
->pdata
, ldata
, left_x
+ ldata
->xpos
,
1135 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
, &xr
,
1136 ldata
->hshortfall
, ldata
->nspaces
, &nspace
, keywords
);
1140 * There's a cross-reference continued on to the next line.
1147 static void code_paragraph(para_data
*pdata
,
1148 font_data
*fn
, font_data
*fi
, font_data
*fb
,
1149 int font_size
, int indent
, word
*words
)
1152 * For code paragraphs, I'm going to hack grievously and
1153 * pretend the three normal fonts are the three code paragraph
1156 pdata
->fonts
[FONT_NORMAL
] = fb
;
1157 pdata
->fonts
[FONT_EMPH
] = fi
;
1158 pdata
->fonts
[FONT_CODE
] = fn
;
1159 pdata
->sizes
[FONT_NORMAL
] =
1160 pdata
->sizes
[FONT_EMPH
] =
1161 pdata
->sizes
[FONT_CODE
] = font_size
;
1163 pdata
->first
= pdata
->last
= NULL
;
1165 for (; words
; words
= words
->next
) {
1166 wchar_t *t
, *e
, *start
;
1167 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1169 int prev
= -1, curr
;
1172 if (words
->next
&& words
->next
->type
== word_Emph
) {
1173 e
= words
->next
->text
;
1174 words
= words
->next
;
1184 else if (*e
== L
'i')
1186 else if (*e
== L
'b')
1203 * We've isolated a maximal subsequence of the line
1204 * which has the same emphasis. Form it into a word
1210 w
->type
= (prev
== 0 ? word_WeakCode
:
1211 prev
== 1 ? word_Emph
: word_Normal
);
1212 w
->text
= mknewa(wchar_t, t
-start
+1);
1213 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
1214 w
->text
[t
-start
] = '\0';
1227 ldata
= mknew(line_data
);
1229 ldata
->pdata
= pdata
;
1230 ldata
->first
= lhead
;
1232 ldata
->line_height
= font_size
* 4096;
1234 ldata
->xpos
= indent
;
1237 pdata
->last
->next
= ldata
;
1238 ldata
->prev
= pdata
->last
;
1240 pdata
->first
= ldata
;
1244 pdata
->last
= ldata
;
1246 ldata
->hshortfall
= 0;
1248 ldata
->aux_text
= NULL
;
1249 ldata
->aux_text_2
= NULL
;
1250 ldata
->aux_left_indent
= 0;