Duplicate fix of Kevin Lyles's bug from r7092.
[sgt/halibut] / paper.h
CommitLineData
43341922 1/*
2 * Paper printing definitions.
3 *
4 * This header file defines data structures and constants which are
5 * shared between bk_paper.c and its clients bk_ps.c and bk_pdf.c.
6 */
7
8#ifndef HALIBUT_PAPER_H
9#define HALIBUT_PAPER_H
10
17c71b41 11/* Number of internal units per PostScript point. */
0f6715c9 12#define UNITS_PER_PT 1000
13#define FUNITS_PER_PT 1000.0
17c71b41 14
d5bc1c48 15/* Glyphs are represented by integer indicies into a table of names. */
16typedef unsigned short glyph;
17#define NOGLYPH 0xFFFF
18
43341922 19typedef struct document_Tag document;
d5bc1c48 20typedef struct glyph_width_Tag glyph_width;
9db47bc3 21typedef struct kern_pair_Tag kern_pair;
b5232689 22typedef struct ligature_Tag ligature;
ba0fe3ec 23typedef struct font_info_Tag font_info;
43341922 24typedef struct font_data_Tag font_data;
25typedef struct font_encoding_Tag font_encoding;
26typedef struct font_list_Tag font_list;
27typedef struct para_data_Tag para_data;
28typedef struct line_data_Tag line_data;
29typedef struct page_data_Tag page_data;
30typedef struct subfont_map_entry_Tag subfont_map_entry;
31typedef struct text_fragment_Tag text_fragment;
138d7ffb 32typedef struct xref_Tag xref;
33typedef struct xref_dest_Tag xref_dest;
23765aeb 34typedef struct rect_Tag rect;
f0e51ce1 35typedef struct outline_element_Tag outline_element;
43341922 36
37/*
38 * This data structure represents the overall document, in the form
39 * it will be given to the client backends.
40 */
41struct document_Tag {
42 int paper_width, paper_height;
43 font_list *fonts;
44 page_data *pages;
f0e51ce1 45 outline_element *outline_elements;
46 int n_outline_elements;
43341922 47};
48
49/*
d5bc1c48 50 * This data structure represents the normal width of a single glyph
51 * in a font.
52 */
53struct glyph_width_Tag {
54 glyph glyph;
55 int width;
56};
57
58/*
9db47bc3 59 * This data structure represents a kerning pair within a font.
60 */
61struct kern_pair_Tag {
d5bc1c48 62 /* Glyph indices. */
63 glyph left, right;
9db47bc3 64 /* Kern amount, in internal units. */
65 int kern;
66};
67
68/*
b5232689 69 * ... and this one represents a ligature.
70 */
71struct ligature_Tag {
d5bc1c48 72 glyph left, right, lig;
b5232689 73};
74
75/*
ba0fe3ec 76 * This data structure holds static information about a font that doesn't
77 * depend on the particular document. It gets generated when the font's
78 * metrics are read in.
43341922 79 */
ba0fe3ec 80
81font_info *all_fonts;
82
83struct font_info_Tag {
84 font_info *next;
43341922 85 /*
86 * Specify the PostScript name of the font and its point size.
87 */
88 const char *name;
89 /*
44407fea 90 * The file containing this font, if any.
91 */
92 FILE *fp;
c885c2ff 93 filepos pos;
94 /*
95 * Lengths of the unencrypted and encrypted portions of the font.
96 */
97 long length1, length2;
d5bc1c48 98 /* A tree of glyph_widths */
99 tree234 *widths;
ba0fe3ec 100 /* A tree of kern_pairs */
9db47bc3 101 tree234 *kerns;
b5232689 102 /* ... and one of ligatures */
103 tree234 *ligs;
43341922 104 /*
105 * For reasonably speedy lookup, we set up a 65536-element
106 * table representing the Unicode BMP (I can conveniently
107 * restrict myself to the BMP for the moment since I happen to
108 * know that no glyph in the Adobe Glyph List falls outside
109 * it), whose elements are indices into the above two arrays.
110 */
d5bc1c48 111 glyph bmp[65536];
255b7ff3 112 /*
113 * Various bits of metadata needed for the /FontDescriptor dictionary
114 * in PDF.
115 */
116 float fontbbox[4];
117 float capheight;
118 float xheight;
119 float ascent;
120 float descent;
121 float stemv;
122 float stemh;
123 float italicangle;
ba0fe3ec 124};
125
126/*
127 * This structure holds the information about how a font is used
128 * in a document.
129 */
130struct font_data_Tag {
131 font_info const *info;
43341922 132 /*
133 * At some point I'm going to divide the font into sub-fonts
d5bc1c48 134 * with largely non-overlapping encoding vectors. This tree
43341922 135 * will track which glyphs go into which subfonts. Also here I
136 * keep track of the latest subfont of any given font, so I can
137 * go back and extend its encoding.
138 */
d5bc1c48 139 tree234 *subfont_map;
43341922 140 font_encoding *latest_subfont;
141 /*
142 * The font list to which this font belongs.
143 */
144 font_list *list;
145};
146
147struct subfont_map_entry_Tag {
148 font_encoding *subfont;
149 unsigned char position;
150};
151
152/*
153 * This data structure represents a sub-font: a font with an
154 * encoding vector.
155 */
156struct font_encoding_Tag {
157 font_encoding *next;
158
159 char *name; /* used by client backends */
160
161 font_data *font; /* the parent font structure */
d5bc1c48 162 glyph vector[256]; /* the actual encoding vector */
43341922 163 wchar_t to_unicode[256]; /* PDF will want to know this */
164 int free_pos; /* space left to extend encoding */
165};
166
167/*
168 * This data structure represents the overall list of sub-fonts in
169 * the whole document.
170 */
171struct font_list_Tag {
172 font_encoding *head;
173 font_encoding *tail;
174};
175
176/*
177 * Constants defining array indices for the various fonts used in a
178 * paragraph.
179 */
180enum {
181 FONT_NORMAL,
182 FONT_EMPH,
183 FONT_CODE,
184 NFONTS
185};
186
187/*
188 * This is the data structure which is stored in the private_data
189 * field of each paragraph. It divides the paragraph up into a
190 * linked list of lines, while at the same time providing for those
191 * lines to be linked together into a much longer list spanning the
192 * whole document for page-breaking purposes.
193 */
194
195struct para_data_Tag {
be76d597 196 para_data *next;
43341922 197 /*
198 * Data about the fonts used in this paragraph. Indices are the
199 * FONT_* constants defined above.
200 */
201 font_data *fonts[NFONTS];
202 int sizes[NFONTS];
203 /*
204 * Pointers to the first and last line of the paragraph. The
205 * line structures are linked into a list, which runs from
206 * `first' to `last' as might be expected. However, the list
207 * does not terminate there: first->prev will end up pointing
208 * to the last line of the previous paragraph in most cases,
209 * and likewise last->next will point to the first line of the
210 * next paragraph.
211 */
212 line_data *first; /* first line in paragraph */
213 line_data *last; /* last line in paragraph */
be76d597 214 /*
215 * Some paragraphs have associated graphics; currently this is
216 * nothing more complex than a single black rectangle.
217 */
218 enum {
219 RECT_NONE, RECT_CHAPTER_UNDERLINE, RECT_RULE
220 } rect_type;
221 /*
c6536773 222 * We left- and right-justify in special circumstances.
223 */
224 enum {
225 JUST, LEFT, RIGHT
226 } justification;
227 /*
5aa98a4b 228 * Sometimes (in code paragraphs) we want to override the flags
229 * passed to render_string().
230 */
231 unsigned extraflags;
232 /*
be76d597 233 * For constructing the page outline.
234 */
235 int outline_level; /* 0=title 1=C 2=H 3=S 4=S2... */
236 wchar_t *outline_title;
2bfd1b76 237 /*
238 * For adding the page number of a contents entry afterwards.
239 */
240 paragraph *contents_entry;
43341922 241};
242
243struct line_data_Tag {
244 /*
245 * The parent paragraph.
246 */
247 para_data *pdata;
248 /*
249 * Pointers to join lines into a linked list.
250 */
251 line_data *prev;
252 line_data *next;
253 /*
254 * The extent of the text displayed on this line. Also mention
255 * its starting x position, and by how much the width of spaces
256 * needs to be adjusted for paragraph justification.
257 *
faad4952 258 * (Unlike most of the `last' pointers defined in this file,
259 * this `end' pointer points to the word _after_ the last one
260 * that should be displayed on the line. This is how it's
261 * returned from wrap_para().)
43341922 262 */
263 word *first;
faad4952 264 word *end;
43341922 265 int xpos;
faad4952 266 int hshortfall, nspaces; /* for justifying paragraphs */
c6536773 267 int real_shortfall;
43341922 268 /*
269 * Auxiliary text: a section number in a margin, or a list item
270 * bullet or number. Also mention where to display this text
271 * relative to the left margin.
272 */
273 word *aux_text;
515d216b 274 word *aux_text_2;
43341922 275 int aux_left_indent;
276 /*
277 * This line might have a non-negotiable page break before it.
278 * Also there will be space required above and below it; also I
279 * store the physical line height (defined as the maximum of
280 * the heights of the three fonts in the pdata) because it's
281 * easier than looking it up repeatedly during page breaking.
282 */
283 int page_break;
284 int space_before;
285 int space_after;
286 int line_height;
287 /*
39a0cfb9 288 * Penalties for page breaking before or after this line.
289 */
290 int penalty_before, penalty_after;
291 /*
43341922 292 * These fields are used in the page breaking algorithm.
293 */
c6536773 294 int *bestcost;
295 int *vshortfall, *text, *space;
296 line_data **page_last; /* last line on a page starting here */
43341922 297 /*
298 * After page breaking, we can assign an actual y-coordinate on
299 * the page to each line. Also we store a pointer back to the
300 * page structure itself.
301 */
302 int ypos;
303 page_data *page;
304};
305
306/*
307 * This data structure is constructed to describe each page of the
308 * printed output.
309 */
310struct page_data_Tag {
311 /*
312 * Pointers to join pages into a linked list.
313 */
314 page_data *prev;
315 page_data *next;
316 /*
317 * The set of lines displayed on this page.
318 */
319 line_data *first_line;
320 line_data *last_line;
321 /*
322 * After text rendering: the set of actual pieces of text
323 * needing to be displayed on this page.
324 */
325 text_fragment *first_text;
326 text_fragment *last_text;
327 /*
138d7ffb 328 * Cross-references.
329 */
330 xref *first_xref;
331 xref *last_xref;
332 /*
23765aeb 333 * Rectangles to be drawn. (These are currently only used for
334 * underlining chapter titles and drawing horizontal rules.)
335 */
336 rect *first_rect;
337 rect *last_rect;
338 /*
2bfd1b76 339 * The page number, as a string.
340 */
341 wchar_t *number;
342 /*
43341922 343 * This spare pointer field is for use by the client backends.
344 */
345 void *spare;
346};
347
348struct text_fragment_Tag {
349 text_fragment *next;
350 int x, y;
351 font_encoding *fe;
352 int fontsize;
353 char *text;
7c8c4239 354 int width;
43341922 355};
356
138d7ffb 357struct xref_dest_Tag {
358 enum { NONE, PAGE, URL } type;
359 page_data *page;
360 char *url;
361};
362
363struct xref_Tag {
364 xref *next;
365 int lx, rx, ty, by;
366 xref_dest dest;
367};
368
23765aeb 369struct rect_Tag {
370 rect *next;
371 int x, y, w, h;
372};
373
f0e51ce1 374struct outline_element_Tag {
375 int level; /* 0=title 1=C 2=H 3=S 4=S2... */
be76d597 376 para_data *pdata;
f0e51ce1 377};
378
43341922 379/*
ba0fe3ec 380 * Functions exported from bk_paper.c
381 */
d5bc1c48 382int width_cmp(void *, void *); /* use when setting up widths */
ba0fe3ec 383int kern_cmp(void *, void *); /* use when setting up kern_pairs */
b5232689 384int lig_cmp(void *, void *); /* use when setting up ligatures */
d5bc1c48 385int find_width(font_data *, glyph);
ba0fe3ec 386
387/*
43341922 388 * Functions and data exported from psdata.c.
389 */
d5bc1c48 390glyph glyph_intern(char const *);
391char const *glyph_extern(glyph);
36e8f0f5 392wchar_t ps_glyph_to_unicode(glyph);
43341922 393extern const char *const ps_std_glyphs[];
ba0fe3ec 394void init_std_fonts(void);
43341922 395const int *ps_std_font_widths(char const *fontname);
9db47bc3 396const kern_pair *ps_std_font_kerns(char const *fontname);
43341922 397
f8194b21 398/*
399 * Function from bk_pdf.c borrowed by bk_ps.c
400 */
401char *pdf_outline_convert(wchar_t *s, int *len);
402
c885c2ff 403/*
404 * Backend functions exported by in_pf.c
405 */
406void pf_part1(font_info *fi, char **bufp, size_t *lenp);
407void pf_part2(font_info *fi, char **bufp, size_t *lenp);
408
43341922 409#endif