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