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