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