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 * That should bring us to the same level of functionality that
23 * original-Halibut had, and the same in PDF plus the obvious
24 * interactive navigation features. After that, in future work:
26 * - linearised PDF, perhaps?
28 * - I'm uncertain of whether I need to include a ToUnicode CMap
29 * in each of my font definitions in PDF. Currently things (by
30 * which I mean cut and paste out of acroread) seem to be
31 * working fairly happily without it, but I don't know.
44 static font_data
*make_std_font(font_list
*fontlist
, char const *name
);
45 static void wrap_paragraph(para_data
*pdata
, word
*words
,
46 int w
, int i1
, int i2
);
47 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
49 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
50 xref_dest
*dest
, keywordlist
*keywords
);
51 static int paper_width_simple(para_data
*pdata
, word
*text
);
52 static void code_paragraph(para_data
*pdata
,
53 font_data
*fn
, font_data
*fi
, font_data
*fb
,
54 int font_size
, int indent
, word
*words
);
56 void *paper_pre_backend(paragraph
*sourceform
, keywordlist
*keywords
,
60 int indent
, extra_indent
, firstline_indent
, aux_indent
;
62 line_data
*ldata
, *firstline
, *lastline
;
63 font_data
*tr
, *ti
, *hr
, *hi
, *cr
, *co
, *cb
;
69 * FIXME: All these things ought to become configurable.
71 int paper_width
= 595 * 4096;
72 int paper_height
= 841 * 4096;
73 int left_margin
= 72 * 4096;
74 int top_margin
= 72 * 4096;
75 int right_margin
= 72 * 4096;
76 int bottom_margin
= 108 * 4096;
77 int indent_list_bullet
= 6 * 4096;
78 int indent_list
= 24 * 4096;
79 int indent_quote
= 18 * 4096;
80 int base_leading
= 4096;
81 int base_para_spacing
= 10 * 4096;
82 int chapter_top_space
= 72 * 4096;
83 int sect_num_left_space
= 12 * 4096;
85 int base_width
= paper_width
- left_margin
- right_margin
;
86 int page_height
= paper_height
- top_margin
- bottom_margin
;
88 IGNORE(keywords
); /* FIXME */
89 IGNORE(idx
); /* FIXME */
92 * First, set up some font structures.
94 fontlist
= mknew(font_list
);
95 fontlist
->head
= fontlist
->tail
= NULL
;
96 tr
= make_std_font(fontlist
, "Times-Roman");
97 ti
= make_std_font(fontlist
, "Times-Italic");
98 hr
= make_std_font(fontlist
, "Helvetica-Bold");
99 hi
= make_std_font(fontlist
, "Helvetica-BoldOblique");
100 cr
= make_std_font(fontlist
, "Courier");
101 co
= make_std_font(fontlist
, "Courier-Oblique");
102 cb
= make_std_font(fontlist
, "Courier-Bold");
105 * Go through and break up each paragraph into lines.
108 firstline
= lastline
= NULL
;
109 for (p
= sourceform
; p
; p
= p
->next
) {
110 p
->private_data
= NULL
;
114 * These paragraph types are either invisible or don't
115 * define text in the normal sense. Either way, they
116 * don't require wrapping.
122 case para_NotParaType
:
129 * These paragraph types don't require wrapping, but
130 * they do affect the line width to which we wrap the
131 * rest of the paragraphs, so we need to pay attention.
134 indent
+= indent_list
; break;
136 indent
-= indent_list
; assert(indent
>= 0); break;
138 indent
+= indent_quote
; break;
140 indent
-= indent_quote
; assert(indent
>= 0); break;
143 * This paragraph type is special. Process it
147 pdata
= mknew(para_data
);
148 code_paragraph(pdata
, cr
, co
, cb
, 12, indent
, p
->words
);
149 p
->private_data
= pdata
;
153 * All of these paragraph types require wrapping in the
154 * ordinary way. So we must supply a set of fonts, a
155 * line width and auxiliary information (e.g. bullet
156 * text) for each one.
160 case para_UnnumberedChapter
:
164 case para_BiblioCited
:
166 case para_NumberedList
:
167 case para_DescribedThing
:
168 case para_Description
:
171 pdata
= mknew(para_data
);
174 * Choose fonts for this paragraph.
176 * FIXME: All of this ought to be completely
181 pdata
->fonts
[FONT_NORMAL
] = hr
;
182 pdata
->sizes
[FONT_NORMAL
] = 24;
183 pdata
->fonts
[FONT_EMPH
] = hi
;
184 pdata
->sizes
[FONT_EMPH
] = 24;
185 pdata
->fonts
[FONT_CODE
] = cb
;
186 pdata
->sizes
[FONT_CODE
] = 24;
191 case para_UnnumberedChapter
:
192 pdata
->fonts
[FONT_NORMAL
] = hr
;
193 pdata
->sizes
[FONT_NORMAL
] = 20;
194 pdata
->fonts
[FONT_EMPH
] = hi
;
195 pdata
->sizes
[FONT_EMPH
] = 20;
196 pdata
->fonts
[FONT_CODE
] = cb
;
197 pdata
->sizes
[FONT_CODE
] = 20;
202 pdata
->fonts
[FONT_NORMAL
] = hr
;
203 pdata
->fonts
[FONT_EMPH
] = hi
;
204 pdata
->fonts
[FONT_CODE
] = cb
;
205 pdata
->sizes
[FONT_NORMAL
] =
206 pdata
->sizes
[FONT_EMPH
] =
207 pdata
->sizes
[FONT_CODE
] =
208 (p
->aux
== 0 ?
16 : p
->aux
== 1 ?
14 : 13);
212 case para_BiblioCited
:
214 case para_NumberedList
:
215 case para_DescribedThing
:
216 case para_Description
:
218 pdata
->fonts
[FONT_NORMAL
] = tr
;
219 pdata
->sizes
[FONT_NORMAL
] = 12;
220 pdata
->fonts
[FONT_EMPH
] = ti
;
221 pdata
->sizes
[FONT_EMPH
] = 12;
222 pdata
->fonts
[FONT_CODE
] = cr
;
223 pdata
->sizes
[FONT_CODE
] = 12;
228 * Also select an indentation level depending on the
229 * paragraph type (list paragraphs other than
230 * para_DescribedThing need extra indent).
232 * (FIXME: Perhaps at some point we might even arrange
233 * for the user to be able to request indented first
234 * lines in paragraphs.)
236 if (p
->type
== para_Bullet
||
237 p
->type
== para_NumberedList
||
238 p
->type
== para_Description
) {
239 extra_indent
= firstline_indent
= indent_list
;
241 extra_indent
= firstline_indent
= 0;
245 * Find the auxiliary text for this paragraph.
256 * For some heading styles (FIXME: be able to
257 * configure which), the auxiliary text contains
258 * the chapter number and is arranged to be
259 * right-aligned a few points left of the primary
260 * margin. For other styles, the auxiliary text is
261 * the full chapter _name_ and takes up space
262 * within the (wrapped) chapter title, meaning that
263 * we must move the first line indent over to make
266 if (p
->type
== para_Heading
|| p
->type
== para_Subsect
) {
270 len
= paper_width_simple(pdata
, p
->kwtext2
);
271 aux_indent
= -len
- sect_num_left_space
;
277 aux2
->type
= word_Normal
;
278 aux2
->text
= ustrdup(L
": ");
279 aux2
->breaks
= FALSE
;
283 firstline_indent
+= paper_width_simple(pdata
, aux
);
284 firstline_indent
+= paper_width_simple(pdata
, aux2
);
290 * Auxiliary text consisting of a bullet. (FIXME:
291 * configurable bullet.)
296 aux
->type
= word_Normal
;
297 aux
->text
= ustrdup(L
"\x2022");
300 aux_indent
= indent
+ indent_list_bullet
;
303 case para_NumberedList
:
305 * Auxiliary text consisting of the number followed
306 * by a (FIXME: configurable) full stop.
312 aux2
->type
= word_Normal
;
313 aux2
->text
= ustrdup(L
".");
314 aux2
->breaks
= FALSE
;
316 aux_indent
= indent
+ indent_list_bullet
;
319 case para_BiblioCited
:
321 * Auxiliary text consisting of the bibliography
322 * reference text, and a trailing space.
328 aux2
->type
= word_Normal
;
329 aux2
->text
= ustrdup(L
" ");
330 aux2
->breaks
= FALSE
;
333 firstline_indent
+= paper_width_simple(pdata
, aux
);
334 firstline_indent
+= paper_width_simple(pdata
, aux2
);
338 wrap_paragraph(pdata
, p
->words
, base_width
,
339 indent
+ firstline_indent
,
340 indent
+ extra_indent
);
342 p
->private_data
= pdata
;
344 pdata
->first
->aux_text
= aux
;
345 pdata
->first
->aux_text_2
= aux2
;
346 pdata
->first
->aux_left_indent
= aux_indent
;
351 if (p
->private_data
) {
352 pdata
= (para_data
*)p
->private_data
;
355 * Set the line spacing for each line in this paragraph.
357 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
358 if (ldata
== pdata
->first
)
359 ldata
->space_before
= base_para_spacing
/ 2;
361 ldata
->space_before
= base_leading
/ 2;
362 if (ldata
== pdata
->last
)
363 ldata
->space_after
= base_para_spacing
/ 2;
365 ldata
->space_after
= base_leading
/ 2;
366 ldata
->page_break
= FALSE
;
370 * Some kinds of section heading do require a page
373 if (p
->type
== para_Title
||
374 p
->type
== para_Chapter
||
375 p
->type
== para_Appendix
||
376 p
->type
== para_UnnumberedChapter
) {
377 pdata
->first
->page_break
= TRUE
;
378 pdata
->first
->space_before
= chapter_top_space
;
382 * Link all line structures together into a big list.
386 lastline
->next
= pdata
->first
;
387 pdata
->first
->prev
= lastline
;
389 firstline
= pdata
->first
;
390 pdata
->first
->prev
= NULL
;
392 lastline
= pdata
->last
;
398 * Now we have an enormous linked list of every line of text in
399 * the document. Break it up into pages.
401 pages
= page_breaks(firstline
, lastline
, page_height
);
404 * Now we're ready to actually lay out the pages. We do this by
405 * looping over _paragraphs_, since we may need to track cross-
406 * references between lines and even across pages.
408 for (p
= sourceform
; p
; p
= p
->next
) {
409 pdata
= (para_data
*)p
->private_data
;
414 for (ldata
= pdata
->first
; ldata
; ldata
= ldata
->next
) {
415 render_line(ldata
, left_margin
, paper_height
- top_margin
,
417 if (ldata
== pdata
->last
)
423 doc
= mknew(document
);
424 doc
->fonts
= fontlist
;
426 doc
->paper_width
= paper_width
;
427 doc
->paper_height
= paper_height
;
431 static font_encoding
*new_font_encoding(font_data
*font
)
436 fe
= mknew(font_encoding
);
439 if (font
->list
->tail
)
440 font
->list
->tail
->next
= fe
;
442 font
->list
->head
= fe
;
443 font
->list
->tail
= fe
;
448 for (i
= 0; i
< 256; i
++) {
449 fe
->vector
[i
] = NULL
;
451 fe
->to_unicode
[i
] = 0xFFFF;
457 static font_data
*make_std_font(font_list
*fontlist
, char const *name
)
465 widths
= ps_std_font_widths(name
);
469 for (nglyphs
= 0; ps_std_glyphs
[nglyphs
] != NULL
; nglyphs
++);
471 f
= mknew(font_data
);
475 f
->nglyphs
= nglyphs
;
476 f
->glyphs
= ps_std_glyphs
;
478 f
->subfont_map
= mknewa(subfont_map_entry
, nglyphs
);
481 * Our first subfont will contain all of US-ASCII. This isn't
482 * really necessary - we could just create custom subfonts
483 * precisely as the whim of render_string dictated - but
484 * instinct suggests that it might be nice to have the text in
485 * the output files look _marginally_ recognisable.
487 fe
= new_font_encoding(f
);
488 fe
->free_pos
= 0xA1; /* only the top half is free */
489 f
->latest_subfont
= fe
;
491 for (i
= 0; i
< (int)lenof(f
->bmp
); i
++)
494 for (i
= 0; i
< nglyphs
; i
++) {
496 ucs
= ps_glyph_to_unicode(f
->glyphs
[i
]);
497 assert(ucs
!= 0xFFFF);
499 if (ucs
>= 0x20 && ucs
<= 0x7E) {
500 fe
->vector
[ucs
] = f
->glyphs
[i
];
501 fe
->indices
[ucs
] = i
;
502 fe
->to_unicode
[ucs
] = ucs
;
503 f
->subfont_map
[i
].subfont
= fe
;
504 f
->subfont_map
[i
].position
= ucs
;
507 * This character is not yet assigned to a subfont.
509 f
->subfont_map
[i
].subfont
= NULL
;
510 f
->subfont_map
[i
].position
= 0;
517 static int string_width(font_data
*font
, wchar_t const *string
, int *errs
)
524 for (; *string
; string
++) {
527 index
= font
->bmp
[(unsigned short)*string
];
528 if (index
== 0xFFFF) {
532 width
+= font
->widths
[index
];
539 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
);
541 struct paper_width_ctx
{
546 static int paper_width_list(void *vctx
, word
*text
, word
*end
, int *nspaces
) {
548 while (text
&& text
!= end
) {
549 w
+= paper_width_internal(vctx
, text
, nspaces
);
555 static int paper_width_internal(void *vctx
, word
*word
, int *nspaces
)
557 struct paper_width_ctx
*ctx
= (struct paper_width_ctx
*)vctx
;
558 int style
, type
, findex
, width
, errs
;
561 switch (word
->type
) {
571 style
= towordstyle(word
->type
);
572 type
= removeattr(word
->type
);
574 findex
= (style
== word_Normal ? FONT_NORMAL
:
575 style
== word_Emph ? FONT_EMPH
:
578 if (type
== word_Normal
) {
580 } else if (type
== word_WhiteSpace
) {
581 if (findex
!= FONT_CODE
) {
584 return ctx
->minspacewidth
;
587 } else /* if (type == word_Quote) */ {
588 if (word
->aux
== quote_Open
)
589 str
= L
"\x2018"; /* FIXME: configurability! */
591 str
= L
"\x2019"; /* FIXME: configurability! */
594 width
= string_width(ctx
->pdata
->fonts
[findex
], str
, &errs
);
596 if (errs
&& word
->alt
)
597 return paper_width_list(vctx
, word
->alt
, NULL
, nspaces
);
599 return ctx
->pdata
->sizes
[findex
] * width
;
602 static int paper_width(void *vctx
, word
*word
)
604 return paper_width_internal(vctx
, word
, NULL
);
607 static int paper_width_simple(para_data
*pdata
, word
*text
)
609 struct paper_width_ctx ctx
;
613 (pdata
->sizes
[FONT_NORMAL
] *
614 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
616 return paper_width_list(&ctx
, text
, NULL
, NULL
);
619 static void wrap_paragraph(para_data
*pdata
, word
*words
,
620 int w
, int i1
, int i2
)
622 wrappedline
*wrapping
, *p
;
624 struct paper_width_ctx ctx
;
628 * We're going to need to store the line height in every line
629 * structure we generate.
634 for (i
= 0; i
< NFONTS
; i
++)
635 if (line_height
< pdata
->sizes
[i
])
636 line_height
= pdata
->sizes
[i
];
640 spacewidth
= (pdata
->sizes
[FONT_NORMAL
] *
641 string_width(pdata
->fonts
[FONT_NORMAL
], L
" ", NULL
));
642 if (spacewidth
== 0) {
644 * A font without a space?! Disturbing. I hope this never
645 * comes up, but I'll make a random guess anyway and set my
646 * space width to half the point size.
648 spacewidth
= pdata
->sizes
[FONT_NORMAL
] * 4096 / 2;
652 * I'm going to set the _minimum_ space width to 3/5 of the
653 * standard one, and use the standard one as the optimum.
655 ctx
.minspacewidth
= spacewidth
* 3 / 5;
658 wrapping
= wrap_para(words
, w
- i1
, w
- i2
, paper_width
, &ctx
, spacewidth
);
661 * Having done the wrapping, we now concoct a set of line_data
664 pdata
->first
= pdata
->last
= NULL
;
666 for (p
= wrapping
; p
; p
= p
->next
) {
669 int len
, wid
, spaces
;
671 ldata
= mknew(line_data
);
673 ldata
->pdata
= pdata
;
674 ldata
->first
= p
->begin
;
676 ldata
->line_height
= line_height
;
678 ldata
->xpos
= (p
== wrapping ? i1
: i2
);
681 pdata
->last
->next
= ldata
;
682 ldata
->prev
= pdata
->last
;
684 pdata
->first
= ldata
;
691 len
= paper_width_list(&ctx
, ldata
->first
, ldata
->end
, &spaces
);
692 wid
= (p
== wrapping ? w
- i1
: w
- i2
);
695 ldata
->hshortfall
= wid
- len
;
696 ldata
->nspaces
= spaces
;
698 * This tells us how much the space width needs to
699 * change from _min_spacewidth. But we want to store
700 * its difference from the _natural_ space width, to
701 * make the text rendering easier.
703 ldata
->hshortfall
+= ctx
.minspacewidth
* spaces
;
704 ldata
->hshortfall
-= spacewidth
* spaces
;
706 * Special case: on the last line of a paragraph, we
707 * never stretch spaces.
709 if (ldata
->hshortfall
> 0 && !p
->next
)
710 ldata
->hshortfall
= 0;
712 ldata
->aux_text
= NULL
;
713 ldata
->aux_text_2
= NULL
;
714 ldata
->aux_left_indent
= 0;
719 static page_data
*page_breaks(line_data
*first
, line_data
*last
,
726 * Page breaking is done by a close analogue of the optimal
727 * paragraph wrapping algorithm used by wrap_para(). We work
728 * backwards from the end of the document line by line; for
729 * each line, we contemplate every possible number of lines we
730 * could put on a page starting with that line, determine a
731 * cost function for each one, add it to the pre-computed cost
732 * function for optimally page-breaking everything after that
733 * page, and pick the best option.
735 * Since my line_data structures are only used for this
736 * purpose, I might as well just store the algorithm data
740 for (l
= last
; l
; l
= l
->prev
) {
741 int minheight
, text
= 0, space
= 0;
745 for (m
= l
; m
; m
= m
->next
) {
746 if (m
!= l
&& m
->page_break
)
747 break; /* we've gone as far as we can */
750 space
+= m
->prev
->space_after
;
751 if (m
!= l
|| m
->page_break
)
752 space
+= m
->space_before
;
753 text
+= m
->line_height
;
754 minheight
= text
+ space
;
756 if (m
!= l
&& minheight
> page_height
)
760 * Compute the cost of this arrangement, as the square
761 * of the amount of wasted space on the page.
762 * Exception: if this is the last page before a
763 * mandatory break or the document end, we don't
764 * penalise a large blank area.
766 if (m
->next
&& !m
->next
->page_break
)
768 int x
= page_height
- minheight
;
775 cost
+= (x
* xf
) >> 8;
780 * FIXME: here I should introduce penalties for
781 * breaking in mid-paragraph, particularly very close
782 * to one end of a paragraph and particularly in code
786 if (m
->next
&& !m
->next
->page_break
)
787 cost
+= m
->next
->bestcost
;
789 if (l
->bestcost
== -1 || l
->bestcost
> cost
) {
791 * This is the best option yet for this starting
795 if (m
->next
&& !m
->next
->page_break
)
796 l
->vshortfall
= page_height
- minheight
;
807 * Now go through the line list forwards and assemble the
817 page
= mknew(page_data
);
826 page
->first_line
= l
;
827 page
->last_line
= l
->page_last
;
829 page
->first_text
= page
->last_text
= NULL
;
831 page
->first_xref
= page
->last_xref
= NULL
;
834 * Now assign a y-coordinate to each line on the page.
837 for (l
= page
->first_line
; l
; l
= l
->next
) {
838 if (l
!= page
->first_line
)
839 space
+= l
->prev
->space_after
;
840 if (l
!= page
->first_line
|| l
->page_break
)
841 space
+= l
->space_before
;
842 text
+= l
->line_height
;
845 l
->ypos
= text
+ space
+
846 space
* (float)page
->first_line
->vshortfall
/
847 page
->first_line
->space
;
849 if (l
== page
->last_line
)
853 l
= page
->last_line
->next
;
859 static void add_string_to_page(page_data
*page
, int x
, int y
,
860 font_encoding
*fe
, int size
, char *text
)
864 frag
= mknew(text_fragment
);
868 page
->last_text
->next
= frag
;
870 page
->first_text
= frag
;
871 page
->last_text
= frag
;
876 frag
->fontsize
= size
;
877 frag
->text
= dupstr(text
);
881 * Returns the updated x coordinate.
883 static int render_string(page_data
*page
, font_data
*font
, int fontsize
,
884 int x
, int y
, wchar_t *str
)
887 int textpos
, textwid
, glyph
;
888 font_encoding
*subfont
= NULL
, *sf
;
890 text
= mknewa(char, 1 + ustrlen(str
));
891 textpos
= textwid
= 0;
894 glyph
= font
->bmp
[*str
];
897 continue; /* nothing more we can do here */
900 * Find which subfont this character is going in.
902 sf
= font
->subfont_map
[glyph
].subfont
;
908 * This character is not yet in a subfont. Assign one.
910 if (font
->latest_subfont
->free_pos
>= 0x100)
911 font
->latest_subfont
= new_font_encoding(font
);
913 c
= font
->latest_subfont
->free_pos
++;
914 if (font
->latest_subfont
->free_pos
== 0x7F)
915 font
->latest_subfont
->free_pos
= 0xA1;
917 font
->subfont_map
[glyph
].subfont
= font
->latest_subfont
;
918 font
->subfont_map
[glyph
].position
= c
;
919 font
->latest_subfont
->vector
[c
] = font
->glyphs
[glyph
];
920 font
->latest_subfont
->indices
[c
] = glyph
;
921 font
->latest_subfont
->to_unicode
[c
] = *str
;
923 sf
= font
->latest_subfont
;
926 if (!subfont
|| sf
!= subfont
) {
928 text
[textpos
] = '\0';
929 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
932 assert(textpos
== 0);
938 text
[textpos
++] = font
->subfont_map
[glyph
].position
;
939 textwid
+= font
->widths
[glyph
] * fontsize
;
945 text
[textpos
] = '\0';
946 add_string_to_page(page
, x
, y
, subfont
, fontsize
, text
);
954 * Returns the updated x coordinate.
956 static int render_text(page_data
*page
, para_data
*pdata
, line_data
*ldata
,
957 int x
, int y
, word
*text
, word
*text_end
, xref
**xr
,
958 int shortfall
, int nspaces
, int *nspace
,
959 keywordlist
*keywords
)
961 while (text
&& text
!= text_end
) {
962 int style
, type
, findex
, errs
;
966 switch (text
->type
) {
968 * Start a cross-reference.
974 if (text
->type
== word_HyperLink
) {
976 dest
.url
= utoa_dup(text
->text
);
979 keyword
*kwl
= kw_lookup(keywords
, text
->text
);
983 assert(kwl
->para
->private_data
);
984 pdata
= (para_data
*) kwl
->para
->private_data
;
986 dest
.page
= pdata
->first
->page
;
990 * Shouldn't happen, but *shrug*
997 if (dest
.type
!= NONE
) {
999 (*xr
)->dest
= dest
; /* structure copy */
1000 if (page
->last_xref
)
1001 page
->last_xref
->next
= *xr
;
1003 page
->first_xref
= *xr
;
1004 page
->last_xref
= *xr
;
1007 * FIXME: Ideally we should have, and use, some
1008 * vertical font metric information here so that
1009 * our cross-ref rectangle can take account of
1010 * descenders and the font's cap height. This will
1011 * do for the moment, but it isn't ideal.
1013 (*xr
)->lx
= (*xr
)->rx
= x
;
1015 (*xr
)->ty
= y
+ ldata
->line_height
;
1020 * Finish extending a cross-reference box.
1030 * FIXME: we should do something with all of these!
1031 * Hyperlinks and xrefs have meaning in PDF, and this
1032 * is probably the right place to nail down the index
1037 style
= towordstyle(text
->type
);
1038 type
= removeattr(text
->type
);
1040 findex
= (style
== word_Normal ? FONT_NORMAL
:
1041 style
== word_Emph ? FONT_EMPH
:
1044 if (type
== word_Normal
) {
1046 } else if (type
== word_WhiteSpace
) {
1047 x
+= pdata
->sizes
[findex
] *
1048 string_width(pdata
->fonts
[findex
], L
" ", NULL
);
1049 if (nspaces
&& findex
!= FONT_CODE
) {
1050 x
+= (*nspace
+1) * shortfall
/ nspaces
;
1051 x
-= *nspace
* shortfall
/ nspaces
;
1055 } else /* if (type == word_Quote) */ {
1056 if (text
->aux
== quote_Open
)
1057 str
= L
"\x2018"; /* FIXME: configurability! */
1059 str
= L
"\x2019"; /* FIXME: configurability! */
1062 (void) string_width(pdata
->fonts
[findex
], str
, &errs
);
1064 if (errs
&& text
->alt
)
1065 x
= render_text(page
, pdata
, ldata
, x
, y
, text
->alt
, NULL
,
1066 xr
, shortfall
, nspaces
, nspace
, keywords
);
1068 x
= render_string(page
, pdata
->fonts
[findex
],
1069 pdata
->sizes
[findex
], x
, y
, str
);
1081 static void render_line(line_data
*ldata
, int left_x
, int top_y
,
1082 xref_dest
*dest
, keywordlist
*keywords
)
1087 if (ldata
->aux_text
) {
1091 x
= render_text(ldata
->page
, ldata
->pdata
, ldata
,
1092 left_x
+ ldata
->aux_left_indent
,
1093 top_y
- ldata
->ypos
,
1094 ldata
->aux_text
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1095 if (ldata
->aux_text_2
)
1096 render_text(ldata
->page
, ldata
->pdata
, ldata
,
1097 x
, top_y
- ldata
->ypos
,
1098 ldata
->aux_text_2
, NULL
, &xr
, 0, 0, &nspace
, keywords
);
1103 * There might be a cross-reference carried over from a
1106 if (dest
->type
!= NONE
) {
1108 xr
->dest
= *dest
; /* structure copy */
1109 if (ldata
->page
->last_xref
)
1110 ldata
->page
->last_xref
->next
= xr
;
1112 ldata
->page
->first_xref
= xr
;
1113 ldata
->page
->last_xref
= xr
;
1114 xr
->lx
= xr
->rx
= left_x
+ ldata
->xpos
;
1115 xr
->by
= top_y
- ldata
->ypos
;
1116 xr
->ty
= top_y
- ldata
->ypos
+ ldata
->line_height
;
1120 render_text(ldata
->page
, ldata
->pdata
, ldata
, left_x
+ ldata
->xpos
,
1121 top_y
- ldata
->ypos
, ldata
->first
, ldata
->end
, &xr
,
1122 ldata
->hshortfall
, ldata
->nspaces
, &nspace
, keywords
);
1126 * There's a cross-reference continued on to the next line.
1133 static void code_paragraph(para_data
*pdata
,
1134 font_data
*fn
, font_data
*fi
, font_data
*fb
,
1135 int font_size
, int indent
, word
*words
)
1138 * For code paragraphs, I'm going to hack grievously and
1139 * pretend the three normal fonts are the three code paragraph
1142 pdata
->fonts
[FONT_NORMAL
] = fb
;
1143 pdata
->fonts
[FONT_EMPH
] = fi
;
1144 pdata
->fonts
[FONT_CODE
] = fn
;
1145 pdata
->sizes
[FONT_NORMAL
] =
1146 pdata
->sizes
[FONT_EMPH
] =
1147 pdata
->sizes
[FONT_CODE
] = font_size
;
1149 pdata
->first
= pdata
->last
= NULL
;
1151 for (; words
; words
= words
->next
) {
1152 wchar_t *t
, *e
, *start
;
1153 word
*lhead
= NULL
, *ltail
= NULL
, *w
;
1155 int prev
= -1, curr
;
1158 if (words
->next
&& words
->next
->type
== word_Emph
) {
1159 e
= words
->next
->text
;
1160 words
= words
->next
;
1170 else if (*e
== L
'i')
1172 else if (*e
== L
'b')
1189 * We've isolated a maximal subsequence of the line
1190 * which has the same emphasis. Form it into a word
1196 w
->type
= (prev
== 0 ? word_WeakCode
:
1197 prev
== 1 ? word_Emph
: word_Normal
);
1198 w
->text
= mknewa(wchar_t, t
-start
+1);
1199 memcpy(w
->text
, start
, (t
-start
) * sizeof(wchar_t));
1200 w
->text
[t
-start
] = '\0';
1213 ldata
= mknew(line_data
);
1215 ldata
->pdata
= pdata
;
1216 ldata
->first
= lhead
;
1218 ldata
->line_height
= font_size
* 4096;
1220 ldata
->xpos
= indent
;
1223 pdata
->last
->next
= ldata
;
1224 ldata
->prev
= pdata
->last
;
1226 pdata
->first
= ldata
;
1230 pdata
->last
= ldata
;
1232 ldata
->hshortfall
= 0;
1234 ldata
->aux_text
= NULL
;
1235 ldata
->aux_text_2
= NULL
;
1236 ldata
->aux_left_indent
= 0;