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