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