Initial work on PS and PDF output. Because these two backends share
[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 typedef struct document_Tag document;
12 typedef struct font_data_Tag font_data;
13 typedef struct font_encoding_Tag font_encoding;
14 typedef struct font_list_Tag font_list;
15 typedef struct para_data_Tag para_data;
16 typedef struct line_data_Tag line_data;
17 typedef struct page_data_Tag page_data;
18 typedef struct subfont_map_entry_Tag subfont_map_entry;
19 typedef struct text_fragment_Tag text_fragment;
20
21 /*
22 * This data structure represents the overall document, in the form
23 * it will be given to the client backends.
24 */
25 struct document_Tag {
26 int paper_width, paper_height;
27 font_list *fonts;
28 page_data *pages;
29 };
30
31 /*
32 * This data structure represents a particular font.
33 */
34 struct font_data_Tag {
35 /*
36 * Specify the PostScript name of the font and its point size.
37 */
38 const char *name;
39 /*
40 * An array of pointers to the available glyph names, and their
41 * corresponding character widths. These two arrays have
42 * parallel indices.
43 */
44 int nglyphs;
45 const char *const *glyphs;
46 const int *widths;
47 /*
48 * For reasonably speedy lookup, we set up a 65536-element
49 * table representing the Unicode BMP (I can conveniently
50 * restrict myself to the BMP for the moment since I happen to
51 * know that no glyph in the Adobe Glyph List falls outside
52 * it), whose elements are indices into the above two arrays.
53 */
54 unsigned short bmp[65536];
55 /*
56 * At some point I'm going to divide the font into sub-fonts
57 * with largely non-overlapping encoding vectors. This array
58 * will track which glyphs go into which subfonts. Also here I
59 * keep track of the latest subfont of any given font, so I can
60 * go back and extend its encoding.
61 */
62 subfont_map_entry *subfont_map;
63 font_encoding *latest_subfont;
64 /*
65 * The font list to which this font belongs.
66 */
67 font_list *list;
68 };
69
70 struct subfont_map_entry_Tag {
71 font_encoding *subfont;
72 unsigned char position;
73 };
74
75 /*
76 * This data structure represents a sub-font: a font with an
77 * encoding vector.
78 */
79 struct font_encoding_Tag {
80 font_encoding *next;
81
82 char *name; /* used by client backends */
83
84 font_data *font; /* the parent font structure */
85 const char *vector[256]; /* the actual encoding vector */
86 int indices[256]; /* indices back into main font struct */
87 wchar_t to_unicode[256]; /* PDF will want to know this */
88 int free_pos; /* space left to extend encoding */
89 };
90
91 /*
92 * This data structure represents the overall list of sub-fonts in
93 * the whole document.
94 */
95 struct font_list_Tag {
96 font_encoding *head;
97 font_encoding *tail;
98 };
99
100 /*
101 * Constants defining array indices for the various fonts used in a
102 * paragraph.
103 */
104 enum {
105 FONT_NORMAL,
106 FONT_EMPH,
107 FONT_CODE,
108 NFONTS
109 };
110
111 /*
112 * This is the data structure which is stored in the private_data
113 * field of each paragraph. It divides the paragraph up into a
114 * linked list of lines, while at the same time providing for those
115 * lines to be linked together into a much longer list spanning the
116 * whole document for page-breaking purposes.
117 */
118
119 struct para_data_Tag {
120 /*
121 * Data about the fonts used in this paragraph. Indices are the
122 * FONT_* constants defined above.
123 */
124 font_data *fonts[NFONTS];
125 int sizes[NFONTS];
126 /*
127 * Pointers to the first and last line of the paragraph. The
128 * line structures are linked into a list, which runs from
129 * `first' to `last' as might be expected. However, the list
130 * does not terminate there: first->prev will end up pointing
131 * to the last line of the previous paragraph in most cases,
132 * and likewise last->next will point to the first line of the
133 * next paragraph.
134 */
135 line_data *first; /* first line in paragraph */
136 line_data *last; /* last line in paragraph */
137 };
138
139 struct line_data_Tag {
140 /*
141 * The parent paragraph.
142 */
143 para_data *pdata;
144 /*
145 * Pointers to join lines into a linked list.
146 */
147 line_data *prev;
148 line_data *next;
149 /*
150 * The extent of the text displayed on this line. Also mention
151 * its starting x position, and by how much the width of spaces
152 * needs to be adjusted for paragraph justification.
153 *
154 * (`last' may be NULL if it's more convenient.)
155 */
156 word *first;
157 word *last;
158 int xpos;
159 int space_adjust; /* for justifying paragraphs */
160 /*
161 * Auxiliary text: a section number in a margin, or a list item
162 * bullet or number. Also mention where to display this text
163 * relative to the left margin.
164 */
165 word *aux_text;
166 int aux_left_indent;
167 /*
168 * This line might have a non-negotiable page break before it.
169 * Also there will be space required above and below it; also I
170 * store the physical line height (defined as the maximum of
171 * the heights of the three fonts in the pdata) because it's
172 * easier than looking it up repeatedly during page breaking.
173 */
174 int page_break;
175 int space_before;
176 int space_after;
177 int line_height;
178 /*
179 * These fields are used in the page breaking algorithm.
180 */
181 int bestcost;
182 int shortfall, text, space;
183 line_data *page_last; /* last line on a page starting here */
184 /*
185 * After page breaking, we can assign an actual y-coordinate on
186 * the page to each line. Also we store a pointer back to the
187 * page structure itself.
188 */
189 int ypos;
190 page_data *page;
191 };
192
193 /*
194 * This data structure is constructed to describe each page of the
195 * printed output.
196 */
197 struct page_data_Tag {
198 /*
199 * Pointers to join pages into a linked list.
200 */
201 page_data *prev;
202 page_data *next;
203 /*
204 * The set of lines displayed on this page.
205 */
206 line_data *first_line;
207 line_data *last_line;
208 /*
209 * After text rendering: the set of actual pieces of text
210 * needing to be displayed on this page.
211 */
212 text_fragment *first_text;
213 text_fragment *last_text;
214 /*
215 * This spare pointer field is for use by the client backends.
216 */
217 void *spare;
218 };
219
220 struct text_fragment_Tag {
221 text_fragment *next;
222 int x, y;
223 font_encoding *fe;
224 int fontsize;
225 char *text;
226 };
227
228 /*
229 * Functions and data exported from psdata.c.
230 */
231 wchar_t ps_glyph_to_unicode(char const *glyph);
232 extern const char *const ps_std_glyphs[];
233 const int *ps_std_font_widths(char const *fontname);
234
235 #endif