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 /* Glyphs are represented by integer indicies into a table of names. */
16 typedef unsigned short glyph;
17 #define NOGLYPH 0xFFFF
18 
19 typedef struct document_Tag document;
20 typedef struct glyph_width_Tag glyph_width;
21 typedef struct kern_pair_Tag kern_pair;
22 typedef struct ligature_Tag ligature;
23 typedef struct font_info_Tag font_info;
24 typedef struct font_data_Tag font_data;
25 typedef struct font_encoding_Tag font_encoding;
26 typedef struct font_list_Tag font_list;
27 typedef struct para_data_Tag para_data;
28 typedef struct line_data_Tag line_data;
29 typedef struct page_data_Tag page_data;
30 typedef struct subfont_map_entry_Tag subfont_map_entry;
31 typedef struct text_fragment_Tag text_fragment;
32 typedef struct xref_Tag xref;
33 typedef struct xref_dest_Tag xref_dest;
34 typedef struct rect_Tag rect;
35 typedef struct outline_element_Tag outline_element;
36 
37 /*
38  * This data structure represents the overall document, in the form
39  * it will be given to the client backends.
40  */
41 struct document_Tag {
42     int paper_width, paper_height;
43     font_list *fonts;
44     page_data *pages;
45     outline_element *outline_elements;
46     int n_outline_elements;
47 };
48 
49 /*
50  * This data structure represents the normal width of a single glyph
51  * in a font.
52  */
53 struct glyph_width_Tag {
54     glyph glyph;
55     int width;
56 };
57 
58 /*
59  * This data structure represents a kerning pair within a font.
60  */
61 struct kern_pair_Tag {
62     /* Glyph indices. */
63     glyph left, right;
64     /* Kern amount, in internal units. */
65     int kern;
66 };
67 
68 /*
69  * ... and this one represents a ligature.
70  */
71 struct ligature_Tag {
72     glyph left, right, lig;
73 };
74 
75 /*
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.
79  */
80 
81 font_info *all_fonts;
82 
83 struct font_info_Tag {
84     font_info *next;
85     /*
86      * Specify the PostScript name of the font and its point size.
87      */
88     const char *name;
89     /*
90      * Pointer to data about the file containing the font, if any.
91      */
92     void *fontfile;
93     enum { TYPE1, TRUETYPE } filetype;
94     /* A tree of glyph_widths */
95     tree234 *widths;
96     /* A tree of kern_pairs */
97     tree234 *kerns;
98     /* ... and one of ligatures */
99     tree234 *ligs;
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      */
107     glyph bmp[65536];
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;
120 };
121 
122 /*
123  * This structure holds the information about how a font is used
124  * in a document.
125  */
126 struct font_data_Tag {
127     font_info const *info;
128     /*
129      * At some point I'm going to divide the font into sub-fonts
130      * with largely non-overlapping encoding vectors. This tree
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      */
135     tree234 *subfont_map;
136     font_encoding *latest_subfont;
137     /*
138      * The font list to which this font belongs.
139      */
140     font_list *list;
141 };
142 
143 struct 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  */
152 struct font_encoding_Tag {
153     font_encoding *next;
154 
155     char *name;			       /* used by client backends */
156 
157     font_data *font;		       /* the parent font structure */
158     glyph vector[256];		       /* the actual encoding vector */
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  */
167 struct 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  */
176 enum {
177     FONT_NORMAL,
178     FONT_EMPH,
179     FONT_STRONG,
180     FONT_CODE,
181     NFONTS
182 };
183 
184 /*
185  * This is the data structure which is stored in the private_data
186  * field of each paragraph. It divides the paragraph up into a
187  * linked list of lines, while at the same time providing for those
188  * lines to be linked together into a much longer list spanning the
189  * whole document for page-breaking purposes.
190  */
191 
192 struct para_data_Tag {
193     para_data *next;
194     /*
195      * Data about the fonts used in this paragraph. Indices are the
196      * FONT_* constants defined above.
197      */
198     font_data *fonts[NFONTS];
199     int sizes[NFONTS];
200     /*
201      * Pointers to the first and last line of the paragraph. The
202      * line structures are linked into a list, which runs from
203      * `first' to `last' as might be expected. However, the list
204      * does not terminate there: first->prev will end up pointing
205      * to the last line of the previous paragraph in most cases,
206      * and likewise last->next will point to the first line of the
207      * next paragraph.
208      */
209     line_data *first;		       /* first line in paragraph */
210     line_data *last;		       /* last line in paragraph */
211     /*
212      * Some paragraphs have associated graphics; currently this is
213      * nothing more complex than a single black rectangle.
214      */
215     enum {
216 	RECT_NONE, RECT_CHAPTER_UNDERLINE, RECT_RULE
217     } rect_type;
218     /*
219      * We left- and right-justify in special circumstances.
220      */
221     enum {
222 	JUST, LEFT, RIGHT
223     } justification;
224     /*
225      * Sometimes (in code paragraphs) we want to override the flags
226      * passed to render_string().
227      */
228     unsigned extraflags;
229     /*
230      * For constructing the page outline.
231      */
232     int outline_level;		       /* 0=title 1=C 2=H 3=S 4=S2... */
233     wchar_t *outline_title;
234     /*
235      * For adding the page number of a contents entry afterwards.
236      */
237     paragraph *contents_entry;
238 };
239 
240 struct line_data_Tag {
241     /*
242      * The parent paragraph.
243      */
244     para_data *pdata;
245     /*
246      * Pointers to join lines into a linked list.
247      */
248     line_data *prev;
249     line_data *next;
250     /*
251      * The extent of the text displayed on this line. Also mention
252      * its starting x position, and by how much the width of spaces
253      * needs to be adjusted for paragraph justification.
254      *
255      * (Unlike most of the `last' pointers defined in this file,
256      * this `end' pointer points to the word _after_ the last one
257      * that should be displayed on the line. This is how it's
258      * returned from wrap_para().)
259      */
260     word *first;
261     word *end;
262     int xpos;
263     int hshortfall, nspaces;	       /* for justifying paragraphs */
264     int real_shortfall;
265     /*
266      * Auxiliary text: a section number in a margin, or a list item
267      * bullet or number. Also mention where to display this text
268      * relative to the left margin.
269      */
270     word *aux_text;
271     word *aux_text_2;
272     int aux_left_indent;
273     /*
274      * This line might have a non-negotiable page break before it.
275      * Also there will be space required above and below it; also I
276      * store the physical line height (defined as the maximum of
277      * the heights of the three fonts in the pdata) because it's
278      * easier than looking it up repeatedly during page breaking.
279      */
280     int page_break;
281     int space_before;
282     int space_after;
283     int line_height;
284     /*
285      * Penalties for page breaking before or after this line.
286      */
287     int penalty_before, penalty_after;
288     /*
289      * These fields are used in the page breaking algorithm.
290      */
291     int *bestcost;
292     int *vshortfall, *text, *space;
293     line_data **page_last;	       /* last line on a page starting here */
294     /*
295      * After page breaking, we can assign an actual y-coordinate on
296      * the page to each line. Also we store a pointer back to the
297      * page structure itself.
298      */
299     int ypos;
300     page_data *page;
301 };
302 
303 /*
304  * This data structure is constructed to describe each page of the
305  * printed output.
306  */
307 struct page_data_Tag {
308     /*
309      * Pointers to join pages into a linked list.
310      */
311     page_data *prev;
312     page_data *next;
313     /*
314      * The set of lines displayed on this page.
315      */
316     line_data *first_line;
317     line_data *last_line;
318     /*
319      * After text rendering: the set of actual pieces of text
320      * needing to be displayed on this page.
321      */
322     text_fragment *first_text;
323     text_fragment *last_text;
324     /*
325      * Cross-references.
326      */
327     xref *first_xref;
328     xref *last_xref;
329     /*
330      * Rectangles to be drawn. (These are currently only used for
331      * underlining chapter titles and drawing horizontal rules.)
332      */
333     rect *first_rect;
334     rect *last_rect;
335     /*
336      * The page number, as a string.
337      */
338     wchar_t *number;
339     /*
340      * This spare pointer field is for use by the client backends.
341      */
342     void *spare;
343 };
344 
345 struct text_fragment_Tag {
346     text_fragment *next;
347     int x, y;
348     font_encoding *fe;
349     int fontsize;
350     char *text;
351     int width;
352 };
353 
354 struct xref_dest_Tag {
355     enum { NONE, PAGE, URL } type;
356     page_data *page;
357     char *url;
358 };
359 
360 struct xref_Tag {
361     xref *next;
362     int lx, rx, ty, by;
363     xref_dest dest;
364 };
365 
366 struct rect_Tag {
367     rect *next;
368     int x, y, w, h;
369 };
370 
371 struct outline_element_Tag {
372     int level;			       /* 0=title 1=C 2=H 3=S 4=S2... */
373     para_data *pdata;
374 };
375 
376 /*
377  * Functions exported from bk_paper.c
378  */
379 int width_cmp(void *, void *); /* use when setting up widths */
380 int kern_cmp(void *, void *); /* use when setting up kern_pairs */
381 int lig_cmp(void *, void *); /* use when setting up ligatures */
382 int find_width(font_data *, glyph);
383 
384 /*
385  * Functions and data exported from psdata.c.
386  */
387 glyph glyph_intern(char const *);
388 char const *glyph_extern(glyph);
389 wchar_t ps_glyph_to_unicode(glyph);
390 extern const char *const ps_std_glyphs[];
391 extern glyph const tt_std_glyphs[];
392 void init_std_fonts(void);
393 const int *ps_std_font_widths(char const *fontname);
394 const kern_pair *ps_std_font_kerns(char const *fontname);
395 
396 /*
397  * Functions exported from bk_pdf.c
398  */
399 typedef struct object_Tag object;
400 typedef struct objlist_Tag objlist;
401 object *new_object(objlist *list);
402 void objtext(object *o, char const *text);
403 void objstream(object *o, char const *text);
404 void objstream_len(object *o, char const *text, size_t len);
405 char *pdf_outline_convert(wchar_t *s, int *len);
406 
407 /*
408  * Function exported from bk_ps.c
409  */
410 void ps_token(FILE *fp, int *cc, char const *fmt, ...);
411 
412 /*
413  * Backend functions exported by in_pf.c
414  */
415 void pf_part1(font_info *fi, char **bufp, size_t *lenp);
416 void pf_part2(font_info *fi, char **bufp, size_t *lenp);
417 void pf_writeps(font_info const *fi, FILE *ofp);
418 
419 /*
420  * Backend functions exported by in_sfnt.c
421  */
422 typedef struct sfnt_Tag sfnt;
423 glyph sfnt_indextoglyph(sfnt *sf, unsigned idx);
424 unsigned sfnt_glyphtoindex(sfnt *sf, glyph g);
425 unsigned sfnt_nglyphs(sfnt *sf);
426 void sfnt_writeps(font_info const *fi, FILE *ofp);
427 void sfnt_data(font_info *fi, char **bufp, size_t *lenp);
428 
429 #endif
430