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