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