| 1 | /* |
| 2 | * Paper printing pre-backend for Halibut. |
| 3 | * |
| 4 | * This module does all the processing common to both PostScript |
| 5 | * and PDF output: selecting fonts, line wrapping and page breaking |
| 6 | * in accordance with font metrics, laying out the contents and |
| 7 | * index pages, generally doing all the page layout. After this, |
| 8 | * bk_ps.c and bk_pdf.c should only need to do linear translations |
| 9 | * into their literal output format. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * TODO in future work: |
| 14 | * |
| 15 | * - linearised PDF, perhaps? |
| 16 | * |
| 17 | * - we should use PDFDocEncoding or Unicode for outline strings, |
| 18 | * now that I actually know how to do them. Probably easiest if |
| 19 | * I do this _after_ bringing in libcharset, since I can simply |
| 20 | * supply PDFDocEncoding in there. |
| 21 | * |
| 22 | * - I'm uncertain of whether I need to include a ToUnicode CMap |
| 23 | * in each of my font definitions in PDF. Currently things (by |
| 24 | * which I mean cut and paste out of acroread) seem to be |
| 25 | * working fairly happily without it, but I don't know. |
| 26 | * |
| 27 | * - rather than the ugly aux_text mechanism for rendering chapter |
| 28 | * titles, we could actually build the correct word list and |
| 29 | * wrap it as a whole. |
| 30 | * |
| 31 | * - get vertical font metrics and use them to position the PDF |
| 32 | * xref boxes more pleasantly |
| 33 | * |
| 34 | * - configurability |
| 35 | * * page header and footer should be configurable; we should |
| 36 | * be able to shift the page number elsewhere, and add other |
| 37 | * things such as the current chapter/section title and fixed |
| 38 | * text |
| 39 | * * remove the fixed mapping from heading levels to heading |
| 40 | * styles; offer a menu of styles from which the user can |
| 41 | * choose at every heading level |
| 42 | * * first-line indent in paragraphs |
| 43 | * * fixed text: `Contents', `Index', the colon-space and full |
| 44 | * stop in chapter title constructions |
| 45 | * * configurable location of contents? |
| 46 | * * certainly configurably _remove_ the contents, and possibly |
| 47 | * also the index |
| 48 | * * double-sided document switch? |
| 49 | * + means you have two header/footer formats which |
| 50 | * alternate |
| 51 | * + and means that mandatory page breaks before chapter |
| 52 | * titles should include a blank page if necessary to |
| 53 | * start the next section to a right-hand page |
| 54 | * |
| 55 | * - title pages |
| 56 | * |
| 57 | * - ability to import other Type 1 fonts |
| 58 | * * we need to parse the font to extract its metrics |
| 59 | * * then we pass the font bodily to both PS and PDF so it can |
| 60 | * be included in the output file |
| 61 | * |
| 62 | * - character substitution for better typography? |
| 63 | * * fi, fl, ffi, ffl ligatures |
| 64 | * * use real ellipsis rather than ... |
| 65 | * * a hyphen in a word by itself might prefer to be an en-dash |
| 66 | * * (Americans might even want a convenient way to use an |
| 67 | * em-dash) |
| 68 | * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw! |
| 69 | * * substituting `minus' for `hyphen' in the standard encoding |
| 70 | * is probably preferable in Courier, though certainly not in |
| 71 | * the main text font |
| 72 | * * if I do do this lot, I'm rather inclined to at least try |
| 73 | * to think up a configurable way to do it so that Americans |
| 74 | * can do em-dash tricks without my intervention and other |
| 75 | * people can do other odd things too. |
| 76 | */ |
| 77 | |
| 78 | #include <assert.h> |
| 79 | #include <stdio.h> |
| 80 | #include <stdarg.h> |
| 81 | |
| 82 | #include "halibut.h" |
| 83 | #include "paper.h" |
| 84 | |
| 85 | typedef struct paper_conf_Tag paper_conf; |
| 86 | typedef struct paper_idx_Tag paper_idx; |
| 87 | |
| 88 | typedef struct { |
| 89 | font_data *fonts[NFONTS]; |
| 90 | int font_size; |
| 91 | } font_cfg; |
| 92 | |
| 93 | struct paper_conf_Tag { |
| 94 | int paper_width; |
| 95 | int paper_height; |
| 96 | int left_margin; |
| 97 | int top_margin; |
| 98 | int right_margin; |
| 99 | int bottom_margin; |
| 100 | int indent_list_bullet; |
| 101 | int indent_list_after; |
| 102 | int indent_list; |
| 103 | int indent_quote; |
| 104 | int base_leading; |
| 105 | int base_para_spacing; |
| 106 | int chapter_top_space; |
| 107 | int sect_num_left_space; |
| 108 | int chapter_underline_depth; |
| 109 | int chapter_underline_thickness; |
| 110 | int rule_thickness; |
| 111 | font_cfg fbase, fcode, ftitle, fchapter, *fsect; |
| 112 | int nfsect; |
| 113 | int contents_indent_step; |
| 114 | int contents_margin; |
| 115 | int leader_separation; |
| 116 | int index_gutter; |
| 117 | int index_cols; |
| 118 | int index_minsep; |
| 119 | int pagenum_fontsize; |
| 120 | int footer_distance; |
| 121 | wchar_t *lquote, *rquote, *bullet; |
| 122 | /* These are derived from the above */ |
| 123 | int base_width; |
| 124 | int page_height; |
| 125 | int index_colwidth; |
| 126 | }; |
| 127 | |
| 128 | struct paper_idx_Tag { |
| 129 | /* |
| 130 | * Word list giving the page numbers on which this index entry |
| 131 | * appears. Also the last word in the list, for ease of |
| 132 | * construction. |
| 133 | */ |
| 134 | word *words; |
| 135 | word *lastword; |
| 136 | /* |
| 137 | * The last page added to the list (so we can ensure we don't |
| 138 | * add one twice). |
| 139 | */ |
| 140 | page_data *lastpage; |
| 141 | }; |
| 142 | |
| 143 | enum { |
| 144 | word_PageXref = word_NotWordType + 1 |
| 145 | }; |
| 146 | |
| 147 | static font_data *make_std_font(font_list *fontlist, char const *name); |
| 148 | static void wrap_paragraph(para_data *pdata, word *words, |
| 149 | int w, int i1, int i2, paper_conf *conf); |
| 150 | static page_data *page_breaks(line_data *first, line_data *last, |
| 151 | int page_height, int ncols, int headspace); |
| 152 | static int render_string(page_data *page, font_data *font, int fontsize, |
| 153 | int x, int y, wchar_t *str); |
| 154 | static int render_line(line_data *ldata, int left_x, int top_y, |
| 155 | xref_dest *dest, keywordlist *keywords, indexdata *idx, |
| 156 | paper_conf *conf); |
| 157 | static void render_para(para_data *pdata, paper_conf *conf, |
| 158 | keywordlist *keywords, indexdata *idx, |
| 159 | paragraph *index_placeholder, page_data *index_page); |
| 160 | static int string_width(font_data *font, wchar_t const *string, int *errs); |
| 161 | static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf); |
| 162 | static para_data *code_paragraph(int indent, word *words, paper_conf *conf); |
| 163 | static para_data *rule_paragraph(int indent, paper_conf *conf); |
| 164 | static void add_rect_to_page(page_data *page, int x, int y, int w, int h); |
| 165 | static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, |
| 166 | word *pkwtext, word *pkwtext2, word *pwords, |
| 167 | paper_conf *conf); |
| 168 | static void standard_line_spacing(para_data *pdata, paper_conf *conf); |
| 169 | static wchar_t *prepare_outline_title(word *first, wchar_t *separator, |
| 170 | word *second); |
| 171 | static word *fake_word(wchar_t *text); |
| 172 | static word *fake_space_word(void); |
| 173 | static word *fake_page_ref(page_data *page); |
| 174 | static word *fake_end_ref(void); |
| 175 | static word *prepare_contents_title(word *first, wchar_t *separator, |
| 176 | word *second); |
| 177 | static void fold_into_page(page_data *dest, page_data *src, int right_shift); |
| 178 | |
| 179 | static int fonts_ok(wchar_t *string, ...) |
| 180 | { |
| 181 | font_data *font; |
| 182 | va_list ap; |
| 183 | int ret = TRUE; |
| 184 | |
| 185 | va_start(ap, string); |
| 186 | while ( (font = va_arg(ap, font_data *)) != NULL) { |
| 187 | int errs; |
| 188 | (void) string_width(font, string, &errs); |
| 189 | if (errs) { |
| 190 | ret = FALSE; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | va_end(ap); |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static void paper_cfg_fonts(font_data **fonts, font_list *fontlist, |
| 200 | wchar_t *wp, filepos *fpos) { |
| 201 | font_data *f; |
| 202 | char *fn; |
| 203 | int i; |
| 204 | |
| 205 | for (i = 0; i < NFONTS && *wp; i++, wp = uadv(wp)) { |
| 206 | fn = utoa_dup(wp, CS_ASCII); |
| 207 | f = make_std_font(fontlist, fn); |
| 208 | if (f) |
| 209 | fonts[i] = f; |
| 210 | else |
| 211 | /* FIXME: proper error */ |
| 212 | error(err_nofont, fpos, wp); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static paper_conf paper_configure(paragraph *source, font_list *fontlist) { |
| 217 | paragraph *p; |
| 218 | paper_conf ret; |
| 219 | |
| 220 | /* |
| 221 | * Defaults. |
| 222 | */ |
| 223 | ret.paper_width = 595 * UNITS_PER_PT; |
| 224 | ret.paper_height = 841 * UNITS_PER_PT; |
| 225 | ret.left_margin = 72 * UNITS_PER_PT; |
| 226 | ret.top_margin = 72 * UNITS_PER_PT; |
| 227 | ret.right_margin = 72 * UNITS_PER_PT; |
| 228 | ret.bottom_margin = 108 * UNITS_PER_PT; |
| 229 | ret.indent_list_bullet = 6 * UNITS_PER_PT; |
| 230 | ret.indent_list_after = 18 * UNITS_PER_PT; |
| 231 | ret.indent_quote = 18 * UNITS_PER_PT; |
| 232 | ret.base_leading = UNITS_PER_PT; |
| 233 | ret.base_para_spacing = 10 * UNITS_PER_PT; |
| 234 | ret.chapter_top_space = 72 * UNITS_PER_PT; |
| 235 | ret.sect_num_left_space = 12 * UNITS_PER_PT; |
| 236 | ret.chapter_underline_depth = 14 * UNITS_PER_PT; |
| 237 | ret.chapter_underline_thickness = 3 * UNITS_PER_PT; |
| 238 | ret.rule_thickness = 1 * UNITS_PER_PT; |
| 239 | ret.fbase.font_size = 12; |
| 240 | ret.fbase.fonts[FONT_NORMAL] = make_std_font(fontlist, "Times-Roman"); |
| 241 | ret.fbase.fonts[FONT_EMPH] = make_std_font(fontlist, "Times-Italic"); |
| 242 | ret.fbase.fonts[FONT_CODE] = make_std_font(fontlist, "Courier"); |
| 243 | ret.fcode.font_size = 12; |
| 244 | ret.fcode.fonts[FONT_NORMAL] = make_std_font(fontlist, "Courier-Bold"); |
| 245 | ret.fcode.fonts[FONT_EMPH] = make_std_font(fontlist, "Courier-Oblique"); |
| 246 | ret.fcode.fonts[FONT_CODE] = make_std_font(fontlist, "Courier"); |
| 247 | ret.ftitle.font_size = 24; |
| 248 | ret.ftitle.fonts[FONT_NORMAL] = make_std_font(fontlist, "Helvetica-Bold"); |
| 249 | ret.ftitle.fonts[FONT_EMPH] = |
| 250 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
| 251 | ret.ftitle.fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
| 252 | ret.fchapter.font_size = 20; |
| 253 | ret.fchapter.fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
| 254 | ret.fchapter.fonts[FONT_EMPH] = |
| 255 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
| 256 | ret.fchapter.fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
| 257 | ret.nfsect = 3; |
| 258 | ret.fsect = snewn(ret.nfsect, font_cfg); |
| 259 | ret.fsect[0].font_size = 16; |
| 260 | ret.fsect[0].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
| 261 | ret.fsect[0].fonts[FONT_EMPH] = |
| 262 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
| 263 | ret.fsect[0].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
| 264 | ret.fsect[1].font_size = 14; |
| 265 | ret.fsect[1].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
| 266 | ret.fsect[1].fonts[FONT_EMPH] = |
| 267 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
| 268 | ret.fsect[1].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
| 269 | ret.fsect[2].font_size = 13; |
| 270 | ret.fsect[2].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold"); |
| 271 | ret.fsect[2].fonts[FONT_EMPH] = |
| 272 | make_std_font(fontlist, "Helvetica-BoldOblique"); |
| 273 | ret.fsect[2].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold"); |
| 274 | ret.contents_indent_step = 24 * UNITS_PER_PT; |
| 275 | ret.contents_margin = 84 * UNITS_PER_PT; |
| 276 | ret.leader_separation = 12 * UNITS_PER_PT; |
| 277 | ret.index_gutter = 36 * UNITS_PER_PT; |
| 278 | ret.index_cols = 2; |
| 279 | ret.index_minsep = 18 * UNITS_PER_PT; |
| 280 | ret.pagenum_fontsize = 12; |
| 281 | ret.footer_distance = 32 * UNITS_PER_PT; |
| 282 | ret.lquote = L"\x2018\0\x2019\0'\0'\0\0"; |
| 283 | ret.rquote = uadv(ret.lquote); |
| 284 | ret.bullet = L"\x2022\0-\0\0"; |
| 285 | |
| 286 | /* |
| 287 | * Two-pass configuration so that we can pick up global config |
| 288 | * (e.g. `quotes') before having it overridden by specific |
| 289 | * config (`paper-quotes'), irrespective of the order in which |
| 290 | * they occur. |
| 291 | */ |
| 292 | for (p = source; p; p = p->next) { |
| 293 | if (p->type == para_Config) { |
| 294 | if (!ustricmp(p->keyword, L"quotes")) { |
| 295 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 296 | ret.lquote = uadv(p->keyword); |
| 297 | ret.rquote = uadv(ret.lquote); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | for (p = source; p; p = p->next) { |
| 304 | p->private_data = NULL; |
| 305 | if (p->type == para_Config) { |
| 306 | if (!ustricmp(p->keyword, L"paper-quotes")) { |
| 307 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 308 | ret.lquote = uadv(p->keyword); |
| 309 | ret.rquote = uadv(ret.lquote); |
| 310 | } |
| 311 | } else if (!ustricmp(p->keyword, L"paper-bullet")) { |
| 312 | ret.bullet = uadv(p->keyword); |
| 313 | } else if (!ustricmp(p->keyword, L"paper-page-width")) { |
| 314 | ret.paper_width = |
| 315 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 316 | } else if (!ustricmp(p->keyword, L"paper-page-height")) { |
| 317 | ret.paper_height = |
| 318 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 319 | } else if (!ustricmp(p->keyword, L"paper-left-margin")) { |
| 320 | ret.left_margin = |
| 321 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 322 | } else if (!ustricmp(p->keyword, L"paper-top-margin")) { |
| 323 | ret.top_margin = |
| 324 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 325 | } else if (!ustricmp(p->keyword, L"paper-right-margin")) { |
| 326 | ret.right_margin = |
| 327 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 328 | } else if (!ustricmp(p->keyword, L"paper-bottom-margin")) { |
| 329 | ret.bottom_margin = |
| 330 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 331 | } else if (!ustricmp(p->keyword, L"paper-list-indent")) { |
| 332 | ret.indent_list_bullet = |
| 333 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 334 | } else if (!ustricmp(p->keyword, L"paper-listitem-indent")) { |
| 335 | ret.indent_list = |
| 336 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 337 | } else if (!ustricmp(p->keyword, L"paper-quote-indent")) { |
| 338 | ret.indent_quote = |
| 339 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 340 | } else if (!ustricmp(p->keyword, L"paper-base-leading")) { |
| 341 | ret.base_leading = |
| 342 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 343 | } else if (!ustricmp(p->keyword, L"paper-base-para-spacing")) { |
| 344 | ret.base_para_spacing = |
| 345 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 346 | } else if (!ustricmp(p->keyword, L"paper-chapter-top-space")) { |
| 347 | ret.chapter_top_space = |
| 348 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 349 | } else if (!ustricmp(p->keyword, L"paper-sect-num-left-space")) { |
| 350 | ret.sect_num_left_space = |
| 351 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 352 | } else if (!ustricmp(p->keyword, L"paper-chapter-underline-depth")) { |
| 353 | ret.chapter_underline_depth = |
| 354 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 355 | } else if (!ustricmp(p->keyword, L"paper-chapter-underline-thickness")) { |
| 356 | ret.chapter_underline_thickness = |
| 357 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 358 | } else if (!ustricmp(p->keyword, L"paper-rule-thickness")) { |
| 359 | ret.rule_thickness = |
| 360 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 361 | } else if (!ustricmp(p->keyword, L"paper-contents-indent-step")) { |
| 362 | ret.contents_indent_step = |
| 363 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 364 | } else if (!ustricmp(p->keyword, L"paper-contents-margin")) { |
| 365 | ret.contents_margin = |
| 366 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 367 | } else if (!ustricmp(p->keyword, L"paper-leader-separation")) { |
| 368 | ret.leader_separation = |
| 369 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 370 | } else if (!ustricmp(p->keyword, L"paper-index-gutter")) { |
| 371 | ret.index_gutter = |
| 372 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 373 | } else if (!ustricmp(p->keyword, L"paper-index-minsep")) { |
| 374 | ret.index_minsep = |
| 375 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 376 | } else if (!ustricmp(p->keyword, L"paper-footer-distance")) { |
| 377 | ret.footer_distance = |
| 378 | (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); |
| 379 | } else if (!ustricmp(p->keyword, L"paper-base-font-size")) { |
| 380 | ret.fbase.font_size = utoi(uadv(p->keyword)); |
| 381 | } else if (!ustricmp(p->keyword, L"paper-index-columns")) { |
| 382 | ret.index_cols = utoi(uadv(p->keyword)); |
| 383 | } else if (!ustricmp(p->keyword, L"paper-pagenum-font-size")) { |
| 384 | ret.pagenum_fontsize = utoi(uadv(p->keyword)); |
| 385 | } else if (!ustricmp(p->keyword, L"paper-base-fonts")) { |
| 386 | paper_cfg_fonts(ret.fbase.fonts, fontlist, uadv(p->keyword), |
| 387 | &p->fpos); |
| 388 | } else if (!ustricmp(p->keyword, L"paper-code-font-size")) { |
| 389 | ret.fcode.font_size = utoi(uadv(p->keyword)); |
| 390 | } else if (!ustricmp(p->keyword, L"paper-code-fonts")) { |
| 391 | paper_cfg_fonts(ret.fcode.fonts, fontlist, uadv(p->keyword), |
| 392 | &p->fpos); |
| 393 | } else if (!ustricmp(p->keyword, L"paper-title-font-size")) { |
| 394 | ret.ftitle.font_size = utoi(uadv(p->keyword)); |
| 395 | } else if (!ustricmp(p->keyword, L"paper-title-fonts")) { |
| 396 | paper_cfg_fonts(ret.ftitle.fonts, fontlist, uadv(p->keyword), |
| 397 | &p->fpos); |
| 398 | } else if (!ustricmp(p->keyword, L"paper-chapter-font-size")) { |
| 399 | ret.ftitle.font_size = utoi(uadv(p->keyword)); |
| 400 | } else if (!ustricmp(p->keyword, L"paper-chapter-fonts")) { |
| 401 | paper_cfg_fonts(ret.ftitle.fonts, fontlist, uadv(p->keyword), |
| 402 | &p->fpos); |
| 403 | } else if (!ustricmp(p->keyword, L"paper-section-font-size")) { |
| 404 | wchar_t *q = uadv(p->keyword); |
| 405 | int n = 0; |
| 406 | if (uisdigit(*q)) { |
| 407 | n = utoi(q); |
| 408 | q = uadv(q); |
| 409 | } |
| 410 | if (n >= ret.nfsect) { |
| 411 | int i; |
| 412 | ret.fsect = sresize(ret.fsect, n+1, font_cfg); |
| 413 | for (i = ret.nfsect; i <= n; i++) |
| 414 | ret.fsect[i] = ret.fsect[ret.nfsect-1]; |
| 415 | ret.nfsect = n+1; |
| 416 | } |
| 417 | ret.fsect[n].font_size = utoi(q); |
| 418 | } else if (!ustricmp(p->keyword, L"paper-section-fonts")) { |
| 419 | wchar_t *q = uadv(p->keyword); |
| 420 | int n = 0; |
| 421 | if (uisdigit(*q)) { |
| 422 | n = utoi(q); |
| 423 | q = uadv(q); |
| 424 | } |
| 425 | if (n >= ret.nfsect) { |
| 426 | int i; |
| 427 | ret.fsect = sresize(ret.fsect, n+1, font_cfg); |
| 428 | for (i = ret.nfsect; i <= n; i++) |
| 429 | ret.fsect[i] = ret.fsect[ret.nfsect-1]; |
| 430 | ret.nfsect = n+1; |
| 431 | } |
| 432 | paper_cfg_fonts(ret.fsect[n].fonts, fontlist, q, &p->fpos); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Set up the derived fields in the conf structure. |
| 439 | */ |
| 440 | |
| 441 | ret.base_width = |
| 442 | ret.paper_width - ret.left_margin - ret.right_margin; |
| 443 | ret.page_height = |
| 444 | ret.paper_height - ret.top_margin - ret.bottom_margin; |
| 445 | ret.indent_list = ret.indent_list_bullet + ret.indent_list_after; |
| 446 | ret.index_colwidth = |
| 447 | (ret.base_width - (ret.index_cols-1) * ret.index_gutter) |
| 448 | / ret.index_cols; |
| 449 | |
| 450 | /* |
| 451 | * Now process fallbacks on quote characters and bullets. We |
| 452 | * use string_width() to determine whether all of the relevant |
| 453 | * fonts contain the same character, and fall back whenever we |
| 454 | * find a character which not all of them support. |
| 455 | */ |
| 456 | |
| 457 | /* Quote characters need not be supported in the fixed code fonts, |
| 458 | * but must be in the title and body fonts. */ |
| 459 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote))) { |
| 460 | int n; |
| 461 | if (!fonts_ok(ret.lquote, |
| 462 | ret.fbase.fonts[FONT_NORMAL], |
| 463 | ret.fbase.fonts[FONT_EMPH], |
| 464 | ret.ftitle.fonts[FONT_NORMAL], |
| 465 | ret.ftitle.fonts[FONT_EMPH], |
| 466 | ret.fchapter.fonts[FONT_NORMAL], |
| 467 | ret.fchapter.fonts[FONT_EMPH], NULL) || |
| 468 | !fonts_ok(ret.rquote, |
| 469 | ret.fbase.fonts[FONT_NORMAL], |
| 470 | ret.fbase.fonts[FONT_EMPH], |
| 471 | ret.ftitle.fonts[FONT_NORMAL], |
| 472 | ret.ftitle.fonts[FONT_EMPH], |
| 473 | ret.fchapter.fonts[FONT_NORMAL], |
| 474 | ret.fchapter.fonts[FONT_EMPH], NULL)) |
| 475 | break; |
| 476 | for (n = 0; n < ret.nfsect; n++) |
| 477 | if (!fonts_ok(ret.lquote, |
| 478 | ret.fsect[n].fonts[FONT_NORMAL], |
| 479 | ret.fsect[n].fonts[FONT_EMPH], NULL) || |
| 480 | !fonts_ok(ret.rquote, |
| 481 | ret.fsect[n].fonts[FONT_NORMAL], |
| 482 | ret.fsect[n].fonts[FONT_EMPH], NULL)) |
| 483 | break; |
| 484 | ret.lquote = uadv(ret.rquote); |
| 485 | ret.rquote = uadv(ret.lquote); |
| 486 | } |
| 487 | |
| 488 | /* The bullet character only needs to be supported in the normal body |
| 489 | * font (not even in italics). */ |
| 490 | while (*ret.bullet && *uadv(ret.bullet) && |
| 491 | !fonts_ok(ret.bullet, ret.fbase.fonts[FONT_NORMAL], NULL)) |
| 492 | ret.bullet = uadv(ret.bullet); |
| 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, |
| 498 | indexdata *idx) { |
| 499 | paragraph *p; |
| 500 | document *doc; |
| 501 | int indent, used_contents; |
| 502 | para_data *pdata, *firstpara = NULL, *lastpara = NULL; |
| 503 | para_data *firstcont, *lastcont; |
| 504 | line_data *firstline, *lastline, *firstcontline, *lastcontline; |
| 505 | page_data *pages; |
| 506 | font_list *fontlist; |
| 507 | paper_conf *conf, ourconf; |
| 508 | int has_index; |
| 509 | int pagenum; |
| 510 | paragraph index_placeholder_para; |
| 511 | page_data *first_index_page; |
| 512 | |
| 513 | fontlist = snew(font_list); |
| 514 | fontlist->head = fontlist->tail = NULL; |
| 515 | |
| 516 | ourconf = paper_configure(sourceform, fontlist); |
| 517 | conf = &ourconf; |
| 518 | |
| 519 | /* |
| 520 | * Set up a data structure to collect page numbers for each |
| 521 | * index entry. |
| 522 | */ |
| 523 | { |
| 524 | int i; |
| 525 | indexentry *entry; |
| 526 | |
| 527 | has_index = FALSE; |
| 528 | |
| 529 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
| 530 | paper_idx *pi = snew(paper_idx); |
| 531 | |
| 532 | has_index = TRUE; |
| 533 | |
| 534 | pi->words = pi->lastword = NULL; |
| 535 | pi->lastpage = NULL; |
| 536 | |
| 537 | entry->backend_data = pi; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Format the contents entry for each heading. |
| 543 | */ |
| 544 | { |
| 545 | word *contents_title; |
| 546 | contents_title = fake_word(L"Contents"); |
| 547 | |
| 548 | firstcont = make_para_data(para_UnnumberedChapter, 0, 0, 0, |
| 549 | NULL, NULL, contents_title, conf); |
| 550 | lastcont = firstcont; |
| 551 | lastcont->next = NULL; |
| 552 | firstcontline = firstcont->first; |
| 553 | lastcontline = lastcont->last; |
| 554 | for (p = sourceform; p; p = p->next) { |
| 555 | word *words; |
| 556 | int indent; |
| 557 | |
| 558 | switch (p->type) { |
| 559 | case para_Chapter: |
| 560 | case para_Appendix: |
| 561 | case para_UnnumberedChapter: |
| 562 | case para_Heading: |
| 563 | case para_Subsect: |
| 564 | switch (p->type) { |
| 565 | case para_Chapter: |
| 566 | case para_Appendix: |
| 567 | words = prepare_contents_title(p->kwtext, L": ", p->words); |
| 568 | indent = 0; |
| 569 | break; |
| 570 | case para_UnnumberedChapter: |
| 571 | words = prepare_contents_title(NULL, NULL, p->words); |
| 572 | indent = 0; |
| 573 | break; |
| 574 | case para_Heading: |
| 575 | case para_Subsect: |
| 576 | words = prepare_contents_title(p->kwtext2, L" ", p->words); |
| 577 | indent = (p->aux + 1) * conf->contents_indent_step; |
| 578 | break; |
| 579 | } |
| 580 | pdata = make_para_data(para_Normal, p->aux, indent, |
| 581 | conf->contents_margin, |
| 582 | NULL, NULL, words, conf); |
| 583 | pdata->next = NULL; |
| 584 | pdata->contents_entry = p; |
| 585 | lastcont->next = pdata; |
| 586 | lastcont = pdata; |
| 587 | |
| 588 | /* |
| 589 | * Link all contents line structures together into |
| 590 | * a big list. |
| 591 | */ |
| 592 | if (pdata->first) { |
| 593 | if (lastcontline) { |
| 594 | lastcontline->next = pdata->first; |
| 595 | pdata->first->prev = lastcontline; |
| 596 | } else { |
| 597 | firstcontline = pdata->first; |
| 598 | pdata->first->prev = NULL; |
| 599 | } |
| 600 | lastcontline = pdata->last; |
| 601 | lastcontline->next = NULL; |
| 602 | } |
| 603 | |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * And one extra one, for the index. |
| 610 | */ |
| 611 | if (has_index) { |
| 612 | pdata = make_para_data(para_Normal, 0, 0, |
| 613 | conf->contents_margin, |
| 614 | NULL, NULL, fake_word(L"Index"), conf); |
| 615 | pdata->next = NULL; |
| 616 | pdata->contents_entry = &index_placeholder_para; |
| 617 | lastcont->next = pdata; |
| 618 | lastcont = pdata; |
| 619 | |
| 620 | if (pdata->first) { |
| 621 | if (lastcontline) { |
| 622 | lastcontline->next = pdata->first; |
| 623 | pdata->first->prev = lastcontline; |
| 624 | } else { |
| 625 | firstcontline = pdata->first; |
| 626 | pdata->first->prev = NULL; |
| 627 | } |
| 628 | lastcontline = pdata->last; |
| 629 | lastcontline->next = NULL; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Do the main paragraph formatting. |
| 636 | */ |
| 637 | indent = 0; |
| 638 | used_contents = FALSE; |
| 639 | firstline = lastline = NULL; |
| 640 | for (p = sourceform; p; p = p->next) { |
| 641 | p->private_data = NULL; |
| 642 | |
| 643 | switch (p->type) { |
| 644 | /* |
| 645 | * These paragraph types are either invisible or don't |
| 646 | * define text in the normal sense. Either way, they |
| 647 | * don't require wrapping. |
| 648 | */ |
| 649 | case para_IM: |
| 650 | case para_BR: |
| 651 | case para_Biblio: |
| 652 | case para_NotParaType: |
| 653 | case para_Config: |
| 654 | case para_VersionID: |
| 655 | case para_NoCite: |
| 656 | break; |
| 657 | |
| 658 | /* |
| 659 | * These paragraph types don't require wrapping, but |
| 660 | * they do affect the line width to which we wrap the |
| 661 | * rest of the paragraphs, so we need to pay attention. |
| 662 | */ |
| 663 | case para_LcontPush: |
| 664 | indent += conf->indent_list; break; |
| 665 | case para_LcontPop: |
| 666 | indent -= conf->indent_list; assert(indent >= 0); break; |
| 667 | case para_QuotePush: |
| 668 | indent += conf->indent_quote; break; |
| 669 | case para_QuotePop: |
| 670 | indent -= conf->indent_quote; assert(indent >= 0); break; |
| 671 | |
| 672 | /* |
| 673 | * This paragraph type is special. Process it |
| 674 | * specially. |
| 675 | */ |
| 676 | case para_Code: |
| 677 | pdata = code_paragraph(indent, p->words, conf); |
| 678 | p->private_data = pdata; |
| 679 | if (pdata->first != pdata->last) { |
| 680 | pdata->first->penalty_after += 100000; |
| 681 | pdata->last->penalty_before += 100000; |
| 682 | } |
| 683 | break; |
| 684 | |
| 685 | /* |
| 686 | * This paragraph is also special. |
| 687 | */ |
| 688 | case para_Rule: |
| 689 | pdata = rule_paragraph(indent, conf); |
| 690 | p->private_data = pdata; |
| 691 | break; |
| 692 | |
| 693 | /* |
| 694 | * All of these paragraph types require wrapping in the |
| 695 | * ordinary way. So we must supply a set of fonts, a |
| 696 | * line width and auxiliary information (e.g. bullet |
| 697 | * text) for each one. |
| 698 | */ |
| 699 | case para_Chapter: |
| 700 | case para_Appendix: |
| 701 | case para_UnnumberedChapter: |
| 702 | case para_Heading: |
| 703 | case para_Subsect: |
| 704 | case para_Normal: |
| 705 | case para_BiblioCited: |
| 706 | case para_Bullet: |
| 707 | case para_NumberedList: |
| 708 | case para_DescribedThing: |
| 709 | case para_Description: |
| 710 | case para_Copyright: |
| 711 | case para_Title: |
| 712 | pdata = make_para_data(p->type, p->aux, indent, 0, |
| 713 | p->kwtext, p->kwtext2, p->words, conf); |
| 714 | |
| 715 | p->private_data = pdata; |
| 716 | |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | if (p->private_data) { |
| 721 | pdata = (para_data *)p->private_data; |
| 722 | |
| 723 | /* |
| 724 | * If this is the first non-title heading, we link the |
| 725 | * contents section in before it. |
| 726 | */ |
| 727 | if (!used_contents && pdata->outline_level > 0) { |
| 728 | used_contents = TRUE; |
| 729 | if (lastpara) |
| 730 | lastpara->next = firstcont; |
| 731 | else |
| 732 | firstpara = firstcont; |
| 733 | lastpara = lastcont; |
| 734 | assert(lastpara->next == NULL); |
| 735 | |
| 736 | if (lastline) { |
| 737 | lastline->next = firstcontline; |
| 738 | firstcontline->prev = lastline; |
| 739 | } else { |
| 740 | firstline = firstcontline; |
| 741 | firstcontline->prev = NULL; |
| 742 | } |
| 743 | assert(lastcontline != NULL); |
| 744 | lastline = lastcontline; |
| 745 | lastline->next = NULL; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Link all line structures together into a big list. |
| 750 | */ |
| 751 | if (pdata->first) { |
| 752 | if (lastline) { |
| 753 | lastline->next = pdata->first; |
| 754 | pdata->first->prev = lastline; |
| 755 | } else { |
| 756 | firstline = pdata->first; |
| 757 | pdata->first->prev = NULL; |
| 758 | } |
| 759 | lastline = pdata->last; |
| 760 | lastline->next = NULL; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Link all paragraph structures together similarly. |
| 765 | */ |
| 766 | pdata->next = NULL; |
| 767 | if (lastpara) |
| 768 | lastpara->next = pdata; |
| 769 | else |
| 770 | firstpara = pdata; |
| 771 | lastpara = pdata; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Now we have an enormous linked list of every line of text in |
| 777 | * the document. Break it up into pages. |
| 778 | */ |
| 779 | pages = page_breaks(firstline, lastline, conf->page_height, 0, 0); |
| 780 | |
| 781 | /* |
| 782 | * Number the pages. |
| 783 | */ |
| 784 | { |
| 785 | char buf[40]; |
| 786 | page_data *page; |
| 787 | |
| 788 | pagenum = 0; |
| 789 | |
| 790 | for (page = pages; page; page = page->next) { |
| 791 | sprintf(buf, "%d", ++pagenum); |
| 792 | page->number = ufroma_dup(buf, CS_ASCII); |
| 793 | } |
| 794 | |
| 795 | if (has_index) { |
| 796 | first_index_page = snew(page_data); |
| 797 | first_index_page->next = first_index_page->prev = NULL; |
| 798 | first_index_page->first_line = NULL; |
| 799 | first_index_page->last_line = NULL; |
| 800 | first_index_page->first_text = first_index_page->last_text = NULL; |
| 801 | first_index_page->first_xref = first_index_page->last_xref = NULL; |
| 802 | first_index_page->first_rect = first_index_page->last_rect = NULL; |
| 803 | |
| 804 | /* And don't forget the as-yet-uncreated index. */ |
| 805 | sprintf(buf, "%d", ++pagenum); |
| 806 | first_index_page->number = ufroma_dup(buf, CS_ASCII); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Now we're ready to actually lay out the pages. We do this by |
| 812 | * looping over _paragraphs_, since we may need to track cross- |
| 813 | * references between lines and even across pages. |
| 814 | */ |
| 815 | for (pdata = firstpara; pdata; pdata = pdata->next) |
| 816 | render_para(pdata, conf, keywords, idx, |
| 817 | &index_placeholder_para, first_index_page); |
| 818 | |
| 819 | /* |
| 820 | * Now we've laid out the main body pages, we should have |
| 821 | * acquired a full set of page numbers for the index. |
| 822 | */ |
| 823 | if (has_index) { |
| 824 | int i; |
| 825 | indexentry *entry; |
| 826 | word *index_title; |
| 827 | para_data *firstidx, *lastidx; |
| 828 | line_data *firstidxline, *lastidxline, *ldata; |
| 829 | page_data *ipages, *ipages2, *page; |
| 830 | |
| 831 | /* |
| 832 | * Create a set of paragraphs for the index. |
| 833 | */ |
| 834 | index_title = fake_word(L"Index"); |
| 835 | |
| 836 | firstidx = make_para_data(para_UnnumberedChapter, 0, 0, 0, |
| 837 | NULL, NULL, index_title, conf); |
| 838 | lastidx = firstidx; |
| 839 | lastidx->next = NULL; |
| 840 | firstidxline = firstidx->first; |
| 841 | lastidxline = lastidx->last; |
| 842 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
| 843 | paper_idx *pi = (paper_idx *)entry->backend_data; |
| 844 | para_data *text, *pages; |
| 845 | |
| 846 | if (!pi->words) |
| 847 | continue; |
| 848 | |
| 849 | text = make_para_data(para_Normal, 0, 0, |
| 850 | conf->base_width - conf->index_colwidth, |
| 851 | NULL, NULL, entry->text, conf); |
| 852 | |
| 853 | pages = make_para_data(para_Normal, 0, 0, |
| 854 | conf->base_width - conf->index_colwidth, |
| 855 | NULL, NULL, pi->words, conf); |
| 856 | |
| 857 | text->justification = LEFT; |
| 858 | pages->justification = RIGHT; |
| 859 | text->last->space_after = pages->first->space_before = |
| 860 | conf->base_leading / 2; |
| 861 | |
| 862 | pages->last->space_after = text->first->space_before = |
| 863 | conf->base_leading; |
| 864 | |
| 865 | assert(text->first); |
| 866 | assert(pages->first); |
| 867 | assert(lastidxline); |
| 868 | assert(lastidx); |
| 869 | |
| 870 | /* |
| 871 | * If feasible, fold the two halves of the index entry |
| 872 | * together. |
| 873 | */ |
| 874 | if (text->last->real_shortfall + pages->first->real_shortfall > |
| 875 | conf->index_colwidth + conf->index_minsep) { |
| 876 | text->last->space_after = -1; |
| 877 | pages->first->space_before = -pages->first->line_height+1; |
| 878 | } |
| 879 | |
| 880 | lastidx->next = text; |
| 881 | text->next = pages; |
| 882 | pages->next = NULL; |
| 883 | lastidx = pages; |
| 884 | |
| 885 | /* |
| 886 | * Link all index line structures together into |
| 887 | * a big list. |
| 888 | */ |
| 889 | text->last->next = pages->first; |
| 890 | pages->first->prev = text->last; |
| 891 | |
| 892 | lastidxline->next = text->first; |
| 893 | text->first->prev = lastidxline; |
| 894 | |
| 895 | lastidxline = pages->last; |
| 896 | |
| 897 | /* |
| 898 | * Breaking an index entry anywhere is so bad that I |
| 899 | * think I'm going to forbid it totally. |
| 900 | */ |
| 901 | for (ldata = text->first; ldata && ldata->next; |
| 902 | ldata = ldata->next) { |
| 903 | ldata->next->space_before += ldata->space_after + 1; |
| 904 | ldata->space_after = -1; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Now break the index into pages. |
| 910 | */ |
| 911 | ipages = page_breaks(firstidxline, firstidxline, conf->page_height, |
| 912 | 0, 0); |
| 913 | ipages2 = page_breaks(firstidxline->next, lastidxline, |
| 914 | conf->page_height, |
| 915 | conf->index_cols, |
| 916 | firstidxline->space_before + |
| 917 | firstidxline->line_height + |
| 918 | firstidxline->space_after); |
| 919 | |
| 920 | /* |
| 921 | * This will have put each _column_ of the index on a |
| 922 | * separate page, which isn't what we want. Fold the pages |
| 923 | * back together. |
| 924 | */ |
| 925 | page = ipages2; |
| 926 | while (page) { |
| 927 | int i; |
| 928 | |
| 929 | for (i = 1; i < conf->index_cols; i++) |
| 930 | if (page->next) { |
| 931 | page_data *tpage; |
| 932 | |
| 933 | fold_into_page(page, page->next, |
| 934 | i * (conf->index_colwidth + |
| 935 | conf->index_gutter)); |
| 936 | tpage = page->next; |
| 937 | page->next = page->next->next; |
| 938 | if (page->next) |
| 939 | page->next->prev = page; |
| 940 | sfree(tpage); |
| 941 | } |
| 942 | |
| 943 | page = page->next; |
| 944 | } |
| 945 | /* Also fold the heading on to the same page as the index items. */ |
| 946 | fold_into_page(ipages, ipages2, 0); |
| 947 | ipages->next = ipages2->next; |
| 948 | if (ipages->next) |
| 949 | ipages->next->prev = ipages; |
| 950 | sfree(ipages2); |
| 951 | fold_into_page(first_index_page, ipages, 0); |
| 952 | first_index_page->next = ipages->next; |
| 953 | if (first_index_page->next) |
| 954 | first_index_page->next->prev = first_index_page; |
| 955 | sfree(ipages); |
| 956 | ipages = first_index_page; |
| 957 | |
| 958 | /* |
| 959 | * Number the index pages, except the already-numbered |
| 960 | * first one. |
| 961 | */ |
| 962 | for (page = ipages->next; page; page = page->next) { |
| 963 | char buf[40]; |
| 964 | sprintf(buf, "%d", ++pagenum); |
| 965 | page->number = ufroma_dup(buf, CS_ASCII); |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * Render the index pages. |
| 970 | */ |
| 971 | for (pdata = firstidx; pdata; pdata = pdata->next) |
| 972 | render_para(pdata, conf, keywords, idx, |
| 973 | &index_placeholder_para, first_index_page); |
| 974 | |
| 975 | /* |
| 976 | * Link the index page list on to the end of the main page |
| 977 | * list. |
| 978 | */ |
| 979 | if (!pages) |
| 980 | pages = ipages; |
| 981 | else { |
| 982 | for (page = pages; page->next; page = page->next); |
| 983 | page->next = ipages; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * Same with the paragraph list, which will cause the index |
| 988 | * to be mentioned in the document outline. |
| 989 | */ |
| 990 | if (!firstpara) |
| 991 | firstpara = firstidx; |
| 992 | else |
| 993 | lastpara->next = firstidx; |
| 994 | lastpara = lastidx; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Draw the headers and footers. |
| 999 | * |
| 1000 | * FIXME: this should be fully configurable, but for the moment |
| 1001 | * I'm just going to put in page numbers in the centre of a |
| 1002 | * footer and leave it at that. |
| 1003 | */ |
| 1004 | { |
| 1005 | page_data *page; |
| 1006 | |
| 1007 | for (page = pages; page; page = page->next) { |
| 1008 | int width; |
| 1009 | |
| 1010 | width = conf->pagenum_fontsize * |
| 1011 | string_width(conf->fbase.fonts[FONT_NORMAL], page->number, |
| 1012 | NULL); |
| 1013 | |
| 1014 | render_string(page, conf->fbase.fonts[FONT_NORMAL], |
| 1015 | conf->pagenum_fontsize, |
| 1016 | conf->left_margin + (conf->base_width - width)/2, |
| 1017 | conf->bottom_margin - conf->footer_distance, |
| 1018 | page->number); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Start putting together the overall document structure we're |
| 1024 | * going to return. |
| 1025 | */ |
| 1026 | doc = snew(document); |
| 1027 | doc->fonts = fontlist; |
| 1028 | doc->pages = pages; |
| 1029 | doc->paper_width = conf->paper_width; |
| 1030 | doc->paper_height = conf->paper_height; |
| 1031 | |
| 1032 | /* |
| 1033 | * Collect the section heading paragraphs into a document |
| 1034 | * outline. This is slightly fiddly because the Title paragraph |
| 1035 | * isn't required to be at the start, although all the others |
| 1036 | * must be in order. |
| 1037 | */ |
| 1038 | { |
| 1039 | int osize = 20; |
| 1040 | |
| 1041 | doc->outline_elements = snewn(osize, outline_element); |
| 1042 | doc->n_outline_elements = 0; |
| 1043 | |
| 1044 | /* First find the title. */ |
| 1045 | for (pdata = firstpara; pdata; pdata = pdata->next) { |
| 1046 | if (pdata->outline_level == 0) { |
| 1047 | doc->outline_elements[0].level = 0; |
| 1048 | doc->outline_elements[0].pdata = pdata; |
| 1049 | doc->n_outline_elements++; |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /* Then collect the rest. */ |
| 1055 | for (pdata = firstpara; pdata; pdata = pdata->next) { |
| 1056 | if (pdata->outline_level > 0) { |
| 1057 | if (doc->n_outline_elements >= osize) { |
| 1058 | osize += 20; |
| 1059 | doc->outline_elements = |
| 1060 | sresize(doc->outline_elements, osize, outline_element); |
| 1061 | } |
| 1062 | |
| 1063 | doc->outline_elements[doc->n_outline_elements].level = |
| 1064 | pdata->outline_level; |
| 1065 | doc->outline_elements[doc->n_outline_elements].pdata = pdata; |
| 1066 | doc->n_outline_elements++; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return doc; |
| 1072 | } |
| 1073 | |
| 1074 | static void setfont(para_data *p, font_cfg *f) { |
| 1075 | int i; |
| 1076 | |
| 1077 | for (i = 0; i < NFONTS; i++) { |
| 1078 | p->fonts[i] = f->fonts[i]; |
| 1079 | p->sizes[i] = f->font_size; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, |
| 1084 | word *pkwtext, word *pkwtext2, word *pwords, |
| 1085 | paper_conf *conf) |
| 1086 | { |
| 1087 | para_data *pdata; |
| 1088 | line_data *ldata; |
| 1089 | int extra_indent, firstline_indent, aux_indent; |
| 1090 | word *aux, *aux2; |
| 1091 | |
| 1092 | pdata = snew(para_data); |
| 1093 | pdata->outline_level = -1; |
| 1094 | pdata->outline_title = NULL; |
| 1095 | pdata->rect_type = RECT_NONE; |
| 1096 | pdata->contents_entry = NULL; |
| 1097 | pdata->justification = JUST; |
| 1098 | |
| 1099 | /* |
| 1100 | * Choose fonts for this paragraph. |
| 1101 | */ |
| 1102 | switch (ptype) { |
| 1103 | case para_Title: |
| 1104 | setfont(pdata, &conf->ftitle); |
| 1105 | pdata->outline_level = 0; |
| 1106 | break; |
| 1107 | |
| 1108 | case para_Chapter: |
| 1109 | case para_Appendix: |
| 1110 | case para_UnnumberedChapter: |
| 1111 | setfont(pdata, &conf->fchapter); |
| 1112 | pdata->outline_level = 1; |
| 1113 | break; |
| 1114 | |
| 1115 | case para_Heading: |
| 1116 | case para_Subsect: |
| 1117 | setfont(pdata, |
| 1118 | &conf->fsect[paux >= conf->nfsect ? conf->nfsect - 1 : paux]); |
| 1119 | pdata->outline_level = 2 + paux; |
| 1120 | break; |
| 1121 | |
| 1122 | case para_Normal: |
| 1123 | case para_BiblioCited: |
| 1124 | case para_Bullet: |
| 1125 | case para_NumberedList: |
| 1126 | case para_DescribedThing: |
| 1127 | case para_Description: |
| 1128 | case para_Copyright: |
| 1129 | setfont(pdata, &conf->fbase); |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Also select an indentation level depending on the |
| 1135 | * paragraph type (list paragraphs other than |
| 1136 | * para_DescribedThing need extra indent). |
| 1137 | * |
| 1138 | * (FIXME: Perhaps at some point we might even arrange |
| 1139 | * for the user to be able to request indented first |
| 1140 | * lines in paragraphs.) |
| 1141 | */ |
| 1142 | if (ptype == para_Bullet || |
| 1143 | ptype == para_NumberedList || |
| 1144 | ptype == para_Description) { |
| 1145 | extra_indent = firstline_indent = conf->indent_list; |
| 1146 | } else { |
| 1147 | extra_indent = firstline_indent = 0; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Find the auxiliary text for this paragraph. |
| 1152 | */ |
| 1153 | aux = aux2 = NULL; |
| 1154 | aux_indent = 0; |
| 1155 | |
| 1156 | switch (ptype) { |
| 1157 | case para_Chapter: |
| 1158 | case para_Appendix: |
| 1159 | case para_Heading: |
| 1160 | case para_Subsect: |
| 1161 | /* |
| 1162 | * For some heading styles (FIXME: be able to |
| 1163 | * configure which), the auxiliary text contains |
| 1164 | * the chapter number and is arranged to be |
| 1165 | * right-aligned a few points left of the primary |
| 1166 | * margin. For other styles, the auxiliary text is |
| 1167 | * the full chapter _name_ and takes up space |
| 1168 | * within the (wrapped) chapter title, meaning that |
| 1169 | * we must move the first line indent over to make |
| 1170 | * space for it. |
| 1171 | */ |
| 1172 | if (ptype == para_Heading || ptype == para_Subsect) { |
| 1173 | int len; |
| 1174 | |
| 1175 | aux = pkwtext2; |
| 1176 | len = paper_width_simple(pdata, pkwtext2, conf); |
| 1177 | aux_indent = -len - conf->sect_num_left_space; |
| 1178 | |
| 1179 | pdata->outline_title = |
| 1180 | prepare_outline_title(pkwtext2, L" ", pwords); |
| 1181 | } else { |
| 1182 | aux = pkwtext; |
| 1183 | aux2 = fake_word(L": "); |
| 1184 | aux_indent = 0; |
| 1185 | |
| 1186 | firstline_indent += paper_width_simple(pdata, aux, conf); |
| 1187 | firstline_indent += paper_width_simple(pdata, aux2, conf); |
| 1188 | |
| 1189 | pdata->outline_title = |
| 1190 | prepare_outline_title(pkwtext, L": ", pwords); |
| 1191 | } |
| 1192 | break; |
| 1193 | |
| 1194 | case para_Bullet: |
| 1195 | /* |
| 1196 | * Auxiliary text consisting of a bullet. |
| 1197 | */ |
| 1198 | aux = fake_word(conf->bullet); |
| 1199 | aux_indent = indent + conf->indent_list_bullet; |
| 1200 | break; |
| 1201 | |
| 1202 | case para_NumberedList: |
| 1203 | /* |
| 1204 | * Auxiliary text consisting of the number followed |
| 1205 | * by a (FIXME: configurable) full stop. |
| 1206 | */ |
| 1207 | aux = pkwtext; |
| 1208 | aux2 = fake_word(L"."); |
| 1209 | aux_indent = indent + conf->indent_list_bullet; |
| 1210 | break; |
| 1211 | |
| 1212 | case para_BiblioCited: |
| 1213 | /* |
| 1214 | * Auxiliary text consisting of the bibliography |
| 1215 | * reference text, and a trailing space. |
| 1216 | */ |
| 1217 | aux = pkwtext; |
| 1218 | aux2 = fake_word(L" "); |
| 1219 | aux_indent = indent; |
| 1220 | firstline_indent += paper_width_simple(pdata, aux, conf); |
| 1221 | firstline_indent += paper_width_simple(pdata, aux2, conf); |
| 1222 | break; |
| 1223 | } |
| 1224 | |
| 1225 | if (pdata->outline_level >= 0 && !pdata->outline_title) { |
| 1226 | pdata->outline_title = |
| 1227 | prepare_outline_title(NULL, NULL, pwords); |
| 1228 | } |
| 1229 | |
| 1230 | wrap_paragraph(pdata, pwords, conf->base_width - rmargin, |
| 1231 | indent + firstline_indent, |
| 1232 | indent + extra_indent, conf); |
| 1233 | |
| 1234 | pdata->first->aux_text = aux; |
| 1235 | pdata->first->aux_text_2 = aux2; |
| 1236 | pdata->first->aux_left_indent = aux_indent; |
| 1237 | |
| 1238 | /* |
| 1239 | * Line breaking penalties. |
| 1240 | */ |
| 1241 | switch (ptype) { |
| 1242 | case para_Chapter: |
| 1243 | case para_Appendix: |
| 1244 | case para_Heading: |
| 1245 | case para_Subsect: |
| 1246 | case para_UnnumberedChapter: |
| 1247 | /* |
| 1248 | * Fixed and large penalty for breaking straight |
| 1249 | * after a heading; corresponding bonus for |
| 1250 | * breaking straight before. |
| 1251 | */ |
| 1252 | pdata->first->penalty_before = -500000; |
| 1253 | pdata->last->penalty_after = 500000; |
| 1254 | for (ldata = pdata->first; ldata; ldata = ldata->next) |
| 1255 | ldata->penalty_after = 500000; |
| 1256 | break; |
| 1257 | |
| 1258 | case para_DescribedThing: |
| 1259 | /* |
| 1260 | * This is treated a bit like a small heading: |
| 1261 | * there's a penalty for breaking after it (i.e. |
| 1262 | * between it and its description), and a bonus for |
| 1263 | * breaking before it (actually _between_ list |
| 1264 | * items). |
| 1265 | */ |
| 1266 | pdata->first->penalty_before = -200000; |
| 1267 | pdata->last->penalty_after = 200000; |
| 1268 | break; |
| 1269 | |
| 1270 | default: |
| 1271 | /* |
| 1272 | * Most paragraph types: widow/orphan control by |
| 1273 | * discouraging breaking one line from the end of |
| 1274 | * any paragraph. |
| 1275 | */ |
| 1276 | if (pdata->first != pdata->last) { |
| 1277 | pdata->first->penalty_after = 100000; |
| 1278 | pdata->last->penalty_before = 100000; |
| 1279 | } |
| 1280 | break; |
| 1281 | } |
| 1282 | |
| 1283 | standard_line_spacing(pdata, conf); |
| 1284 | |
| 1285 | /* |
| 1286 | * Some kinds of section heading require a page break before |
| 1287 | * them and an underline after. |
| 1288 | */ |
| 1289 | if (ptype == para_Title || |
| 1290 | ptype == para_Chapter || |
| 1291 | ptype == para_Appendix || |
| 1292 | ptype == para_UnnumberedChapter) { |
| 1293 | pdata->first->page_break = TRUE; |
| 1294 | pdata->first->space_before = conf->chapter_top_space; |
| 1295 | pdata->last->space_after += |
| 1296 | (conf->chapter_underline_depth + |
| 1297 | conf->chapter_underline_thickness); |
| 1298 | pdata->rect_type = RECT_CHAPTER_UNDERLINE; |
| 1299 | } |
| 1300 | |
| 1301 | return pdata; |
| 1302 | } |
| 1303 | |
| 1304 | static void standard_line_spacing(para_data *pdata, paper_conf *conf) |
| 1305 | { |
| 1306 | line_data *ldata; |
| 1307 | |
| 1308 | /* |
| 1309 | * Set the line spacing for each line in this paragraph. |
| 1310 | */ |
| 1311 | for (ldata = pdata->first; ldata; ldata = ldata->next) { |
| 1312 | if (ldata == pdata->first) |
| 1313 | ldata->space_before = conf->base_para_spacing / 2; |
| 1314 | else |
| 1315 | ldata->space_before = conf->base_leading / 2; |
| 1316 | if (ldata == pdata->last) |
| 1317 | ldata->space_after = conf->base_para_spacing / 2; |
| 1318 | else |
| 1319 | ldata->space_after = conf->base_leading / 2; |
| 1320 | ldata->page_break = FALSE; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | static font_encoding *new_font_encoding(font_data *font) |
| 1325 | { |
| 1326 | font_encoding *fe; |
| 1327 | int i; |
| 1328 | |
| 1329 | fe = snew(font_encoding); |
| 1330 | fe->next = NULL; |
| 1331 | |
| 1332 | if (font->list->tail) |
| 1333 | font->list->tail->next = fe; |
| 1334 | else |
| 1335 | font->list->head = fe; |
| 1336 | font->list->tail = fe; |
| 1337 | |
| 1338 | fe->font = font; |
| 1339 | fe->free_pos = 0x21; |
| 1340 | |
| 1341 | for (i = 0; i < 256; i++) { |
| 1342 | fe->vector[i] = NULL; |
| 1343 | fe->indices[i] = -1; |
| 1344 | fe->to_unicode[i] = 0xFFFF; |
| 1345 | } |
| 1346 | |
| 1347 | return fe; |
| 1348 | } |
| 1349 | |
| 1350 | static int kern_cmp(void *a, void *b) |
| 1351 | { |
| 1352 | kern_pair const *ka = a, *kb = b; |
| 1353 | |
| 1354 | if (ka->left < kb->left) |
| 1355 | return -1; |
| 1356 | if (ka->left > kb->left) |
| 1357 | return 1; |
| 1358 | if (ka->right < kb->right) |
| 1359 | return -1; |
| 1360 | if (ka->right > kb->right) |
| 1361 | return 1; |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | static font_data *make_std_font(font_list *fontlist, char const *name) |
| 1366 | { |
| 1367 | const int *widths; |
| 1368 | const kern_pair *kerns; |
| 1369 | int nglyphs; |
| 1370 | font_data *f; |
| 1371 | font_encoding *fe; |
| 1372 | int i; |
| 1373 | |
| 1374 | /* XXXKERN */ |
| 1375 | widths = ps_std_font_widths(name); |
| 1376 | kerns = ps_std_font_kerns(name); |
| 1377 | if (!widths || !kerns) |
| 1378 | return NULL; |
| 1379 | |
| 1380 | for (nglyphs = 0; ps_std_glyphs[nglyphs] != NULL; nglyphs++); |
| 1381 | |
| 1382 | f = snew(font_data); |
| 1383 | |
| 1384 | f->list = fontlist; |
| 1385 | f->name = name; |
| 1386 | f->nglyphs = nglyphs; |
| 1387 | f->glyphs = ps_std_glyphs; |
| 1388 | f->widths = widths; |
| 1389 | f->kerns = newtree234(kern_cmp); |
| 1390 | for (;kerns->left != 0xFFFF; kerns++) |
| 1391 | add234(f->kerns, (void *)kerns); |
| 1392 | f->subfont_map = snewn(nglyphs, subfont_map_entry); |
| 1393 | |
| 1394 | /* |
| 1395 | * Our first subfont will contain all of US-ASCII. This isn't |
| 1396 | * really necessary - we could just create custom subfonts |
| 1397 | * precisely as the whim of render_string dictated - but |
| 1398 | * instinct suggests that it might be nice to have the text in |
| 1399 | * the output files look _marginally_ recognisable. |
| 1400 | */ |
| 1401 | fe = new_font_encoding(f); |
| 1402 | fe->free_pos = 0xA1; /* only the top half is free */ |
| 1403 | f->latest_subfont = fe; |
| 1404 | |
| 1405 | for (i = 0; i < (int)lenof(f->bmp); i++) |
| 1406 | f->bmp[i] = 0xFFFF; |
| 1407 | |
| 1408 | for (i = 0; i < nglyphs; i++) { |
| 1409 | wchar_t ucs; |
| 1410 | ucs = ps_glyph_to_unicode(f->glyphs[i]); |
| 1411 | assert(ucs != 0xFFFF); |
| 1412 | f->bmp[ucs] = i; |
| 1413 | if (ucs >= 0x20 && ucs <= 0x7E) { |
| 1414 | fe->vector[ucs] = f->glyphs[i]; |
| 1415 | fe->indices[ucs] = i; |
| 1416 | fe->to_unicode[ucs] = ucs; |
| 1417 | f->subfont_map[i].subfont = fe; |
| 1418 | f->subfont_map[i].position = ucs; |
| 1419 | } else { |
| 1420 | /* |
| 1421 | * This character is not yet assigned to a subfont. |
| 1422 | */ |
| 1423 | f->subfont_map[i].subfont = NULL; |
| 1424 | f->subfont_map[i].position = 0; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | return f; |
| 1429 | } |
| 1430 | |
| 1431 | /* NB: arguments are glyph numbers from font->bmp. */ |
| 1432 | static int find_kern(font_data *font, int lindex, int rindex) |
| 1433 | { |
| 1434 | kern_pair wantkp; |
| 1435 | kern_pair const *kp; |
| 1436 | |
| 1437 | if (lindex == 0xFFFF || rindex == 0xFFFF) |
| 1438 | return 0; |
| 1439 | wantkp.left = lindex; |
| 1440 | wantkp.right = rindex; |
| 1441 | kp = find234(font->kerns, &wantkp, NULL); |
| 1442 | if (kp == NULL) |
| 1443 | return 0; |
| 1444 | return kp->kern; |
| 1445 | } |
| 1446 | |
| 1447 | static int string_width(font_data *font, wchar_t const *string, int *errs) |
| 1448 | { |
| 1449 | int width = 0; |
| 1450 | int index, oindex; |
| 1451 | |
| 1452 | if (errs) |
| 1453 | *errs = 0; |
| 1454 | |
| 1455 | oindex = 0xFFFF; |
| 1456 | for (; *string; string++) { |
| 1457 | index = (*string < 0 || *string > 0xFFFF ? 0xFFFF : |
| 1458 | font->bmp[*string]); |
| 1459 | |
| 1460 | if (index == 0xFFFF) { |
| 1461 | if (errs) |
| 1462 | *errs = 1; |
| 1463 | } else { |
| 1464 | width += find_kern(font, oindex, index) + font->widths[index]; |
| 1465 | } |
| 1466 | oindex = index; |
| 1467 | } |
| 1468 | |
| 1469 | return width; |
| 1470 | } |
| 1471 | |
| 1472 | static int paper_width_internal(void *vctx, word *word, int *nspaces); |
| 1473 | |
| 1474 | struct paper_width_ctx { |
| 1475 | int minspacewidth; |
| 1476 | para_data *pdata; |
| 1477 | paper_conf *conf; |
| 1478 | }; |
| 1479 | |
| 1480 | static int paper_width_list(void *vctx, word *text, word *end, int *nspaces) { |
| 1481 | int w = 0; |
| 1482 | while (text && text != end) { |
| 1483 | w += paper_width_internal(vctx, text, nspaces); |
| 1484 | text = text->next; |
| 1485 | } |
| 1486 | return w; |
| 1487 | } |
| 1488 | |
| 1489 | static int paper_width_internal(void *vctx, word *word, int *nspaces) |
| 1490 | { |
| 1491 | struct paper_width_ctx *ctx = (struct paper_width_ctx *)vctx; |
| 1492 | int style, type, findex, width, errs; |
| 1493 | wchar_t *str; |
| 1494 | |
| 1495 | switch (word->type) { |
| 1496 | case word_HyperLink: |
| 1497 | case word_HyperEnd: |
| 1498 | case word_UpperXref: |
| 1499 | case word_LowerXref: |
| 1500 | case word_PageXref: |
| 1501 | case word_XrefEnd: |
| 1502 | case word_IndexRef: |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | style = towordstyle(word->type); |
| 1507 | type = removeattr(word->type); |
| 1508 | |
| 1509 | findex = (style == word_Normal ? FONT_NORMAL : |
| 1510 | style == word_Emph ? FONT_EMPH : |
| 1511 | FONT_CODE); |
| 1512 | |
| 1513 | if (type == word_Normal) { |
| 1514 | str = word->text; |
| 1515 | } else if (type == word_WhiteSpace) { |
| 1516 | if (findex != FONT_CODE) { |
| 1517 | if (nspaces) |
| 1518 | (*nspaces)++; |
| 1519 | return ctx->minspacewidth; |
| 1520 | } else |
| 1521 | str = L" "; |
| 1522 | } else /* if (type == word_Quote) */ { |
| 1523 | if (word->aux == quote_Open) |
| 1524 | str = ctx->conf->lquote; |
| 1525 | else |
| 1526 | str = ctx->conf->rquote; |
| 1527 | } |
| 1528 | |
| 1529 | width = string_width(ctx->pdata->fonts[findex], str, &errs); |
| 1530 | |
| 1531 | if (errs && word->alt) |
| 1532 | return paper_width_list(vctx, word->alt, NULL, nspaces); |
| 1533 | else |
| 1534 | return ctx->pdata->sizes[findex] * width; |
| 1535 | } |
| 1536 | |
| 1537 | static int paper_width(void *vctx, word *word) |
| 1538 | { |
| 1539 | return paper_width_internal(vctx, word, NULL); |
| 1540 | } |
| 1541 | |
| 1542 | static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf) |
| 1543 | { |
| 1544 | struct paper_width_ctx ctx; |
| 1545 | |
| 1546 | ctx.pdata = pdata; |
| 1547 | ctx.minspacewidth = |
| 1548 | (pdata->sizes[FONT_NORMAL] * |
| 1549 | string_width(pdata->fonts[FONT_NORMAL], L" ", NULL)); |
| 1550 | ctx.conf = conf; |
| 1551 | |
| 1552 | return paper_width_list(&ctx, text, NULL, NULL); |
| 1553 | } |
| 1554 | |
| 1555 | static void wrap_paragraph(para_data *pdata, word *words, |
| 1556 | int w, int i1, int i2, paper_conf *conf) |
| 1557 | { |
| 1558 | wrappedline *wrapping, *p; |
| 1559 | int spacewidth; |
| 1560 | struct paper_width_ctx ctx; |
| 1561 | int line_height; |
| 1562 | |
| 1563 | /* |
| 1564 | * We're going to need to store the line height in every line |
| 1565 | * structure we generate. |
| 1566 | */ |
| 1567 | { |
| 1568 | int i; |
| 1569 | line_height = 0; |
| 1570 | for (i = 0; i < NFONTS; i++) |
| 1571 | if (line_height < pdata->sizes[i]) |
| 1572 | line_height = pdata->sizes[i]; |
| 1573 | line_height *= UNITS_PER_PT; |
| 1574 | } |
| 1575 | |
| 1576 | spacewidth = (pdata->sizes[FONT_NORMAL] * |
| 1577 | string_width(pdata->fonts[FONT_NORMAL], L" ", NULL)); |
| 1578 | if (spacewidth == 0) { |
| 1579 | /* |
| 1580 | * A font without a space?! Disturbing. I hope this never |
| 1581 | * comes up, but I'll make a random guess anyway and set my |
| 1582 | * space width to half the point size. |
| 1583 | */ |
| 1584 | spacewidth = pdata->sizes[FONT_NORMAL] * UNITS_PER_PT / 2; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * I'm going to set the _minimum_ space width to 3/5 of the |
| 1589 | * standard one, and use the standard one as the optimum. |
| 1590 | */ |
| 1591 | ctx.minspacewidth = spacewidth * 3 / 5; |
| 1592 | ctx.pdata = pdata; |
| 1593 | ctx.conf = conf; |
| 1594 | |
| 1595 | wrapping = wrap_para(words, w - i1, w - i2, paper_width, &ctx, spacewidth); |
| 1596 | |
| 1597 | /* |
| 1598 | * Having done the wrapping, we now concoct a set of line_data |
| 1599 | * structures. |
| 1600 | */ |
| 1601 | pdata->first = pdata->last = NULL; |
| 1602 | |
| 1603 | for (p = wrapping; p; p = p->next) { |
| 1604 | line_data *ldata; |
| 1605 | word *wd; |
| 1606 | int len, wid, spaces; |
| 1607 | |
| 1608 | ldata = snew(line_data); |
| 1609 | |
| 1610 | ldata->pdata = pdata; |
| 1611 | ldata->first = p->begin; |
| 1612 | ldata->end = p->end; |
| 1613 | ldata->line_height = line_height; |
| 1614 | |
| 1615 | ldata->xpos = (p == wrapping ? i1 : i2); |
| 1616 | |
| 1617 | if (pdata->last) { |
| 1618 | pdata->last->next = ldata; |
| 1619 | ldata->prev = pdata->last; |
| 1620 | } else { |
| 1621 | pdata->first = ldata; |
| 1622 | ldata->prev = NULL; |
| 1623 | } |
| 1624 | ldata->next = NULL; |
| 1625 | pdata->last = ldata; |
| 1626 | |
| 1627 | spaces = 0; |
| 1628 | len = paper_width_list(&ctx, ldata->first, ldata->end, &spaces); |
| 1629 | wid = (p == wrapping ? w - i1 : w - i2); |
| 1630 | wd = ldata->first; |
| 1631 | |
| 1632 | ldata->hshortfall = wid - len; |
| 1633 | ldata->nspaces = spaces; |
| 1634 | /* |
| 1635 | * This tells us how much the space width needs to |
| 1636 | * change from _min_spacewidth. But we want to store |
| 1637 | * its difference from the _natural_ space width, to |
| 1638 | * make the text rendering easier. |
| 1639 | */ |
| 1640 | ldata->hshortfall += ctx.minspacewidth * spaces; |
| 1641 | ldata->hshortfall -= spacewidth * spaces; |
| 1642 | ldata->real_shortfall = ldata->hshortfall; |
| 1643 | /* |
| 1644 | * Special case: on the last line of a paragraph, we |
| 1645 | * never stretch spaces. |
| 1646 | */ |
| 1647 | if (ldata->hshortfall > 0 && !p->next) |
| 1648 | ldata->hshortfall = 0; |
| 1649 | |
| 1650 | ldata->aux_text = NULL; |
| 1651 | ldata->aux_text_2 = NULL; |
| 1652 | ldata->aux_left_indent = 0; |
| 1653 | ldata->penalty_before = ldata->penalty_after = 0; |
| 1654 | } |
| 1655 | |
| 1656 | } |
| 1657 | |
| 1658 | static page_data *page_breaks(line_data *first, line_data *last, |
| 1659 | int page_height, int ncols, int headspace) |
| 1660 | { |
| 1661 | line_data *l, *m; |
| 1662 | page_data *ph, *pt; |
| 1663 | int n, n1, this_height; |
| 1664 | |
| 1665 | /* |
| 1666 | * Page breaking is done by a close analogue of the optimal |
| 1667 | * paragraph wrapping algorithm used by wrap_para(). We work |
| 1668 | * backwards from the end of the document line by line; for |
| 1669 | * each line, we contemplate every possible number of lines we |
| 1670 | * could put on a page starting with that line, determine a |
| 1671 | * cost function for each one, add it to the pre-computed cost |
| 1672 | * function for optimally page-breaking everything after that |
| 1673 | * page, and pick the best option. |
| 1674 | * |
| 1675 | * This is made slightly more complex by the fact that we have |
| 1676 | * a multi-column index with a heading at the top of the |
| 1677 | * _first_ page, meaning that the first _ncols_ pages must have |
| 1678 | * a different length. Hence, we must do the wrapping ncols+1 |
| 1679 | * times over, hypothetically trying to put every subsequence |
| 1680 | * on every possible page. |
| 1681 | * |
| 1682 | * Since my line_data structures are only used for this |
| 1683 | * purpose, I might as well just store the algorithm data |
| 1684 | * directly in them. |
| 1685 | */ |
| 1686 | |
| 1687 | for (l = last; l; l = l->prev) { |
| 1688 | l->bestcost = snewn(ncols+1, int); |
| 1689 | l->vshortfall = snewn(ncols+1, int); |
| 1690 | l->text = snewn(ncols+1, int); |
| 1691 | l->space = snewn(ncols+1, int); |
| 1692 | l->page_last = snewn(ncols+1, line_data *); |
| 1693 | |
| 1694 | for (n = 0; n <= ncols; n++) { |
| 1695 | int minheight, text = 0, space = 0; |
| 1696 | int cost; |
| 1697 | |
| 1698 | n1 = (n < ncols ? n+1 : ncols); |
| 1699 | if (n < ncols) |
| 1700 | this_height = page_height - headspace; |
| 1701 | else |
| 1702 | this_height = page_height; |
| 1703 | |
| 1704 | l->bestcost[n] = -1; |
| 1705 | for (m = l; m; m = m->next) { |
| 1706 | if (m != l && m->page_break) |
| 1707 | break; /* we've gone as far as we can */ |
| 1708 | |
| 1709 | if (m != l) { |
| 1710 | if (m->prev->space_after > 0) |
| 1711 | space += m->prev->space_after; |
| 1712 | else |
| 1713 | text += m->prev->space_after; |
| 1714 | } |
| 1715 | if (m != l || m->page_break) { |
| 1716 | if (m->space_before > 0) |
| 1717 | space += m->space_before; |
| 1718 | else |
| 1719 | text += m->space_before; |
| 1720 | } |
| 1721 | text += m->line_height; |
| 1722 | minheight = text + space; |
| 1723 | |
| 1724 | if (m != l && minheight > this_height) |
| 1725 | break; |
| 1726 | |
| 1727 | /* |
| 1728 | * If the space after this paragraph is _negative_ |
| 1729 | * (which means the next line is folded on to this |
| 1730 | * one, which happens in the index), we absolutely |
| 1731 | * cannot break here. |
| 1732 | */ |
| 1733 | if (m->space_after >= 0) { |
| 1734 | |
| 1735 | /* |
| 1736 | * Compute the cost of this arrangement, as the |
| 1737 | * square of the amount of wasted space on the |
| 1738 | * page. Exception: if this is the last page |
| 1739 | * before a mandatory break or the document |
| 1740 | * end, we don't penalise a large blank area. |
| 1741 | */ |
| 1742 | if (m != last && m->next && !m->next->page_break) |
| 1743 | { |
| 1744 | int x = (this_height - minheight) / FUNITS_PER_PT * |
| 1745 | 4096.0; |
| 1746 | int xf; |
| 1747 | |
| 1748 | xf = x & 0xFF; |
| 1749 | x >>= 8; |
| 1750 | |
| 1751 | cost = x*x; |
| 1752 | cost += (x * xf) >> 8; |
| 1753 | } else |
| 1754 | cost = 0; |
| 1755 | |
| 1756 | if (m != last && m->next && !m->next->page_break) { |
| 1757 | cost += m->penalty_after; |
| 1758 | cost += m->next->penalty_before; |
| 1759 | } |
| 1760 | |
| 1761 | if (m != last && m->next && !m->next->page_break) |
| 1762 | cost += m->next->bestcost[n1]; |
| 1763 | if (l->bestcost[n] == -1 || l->bestcost[n] > cost) { |
| 1764 | /* |
| 1765 | * This is the best option yet for this |
| 1766 | * starting point. |
| 1767 | */ |
| 1768 | l->bestcost[n] = cost; |
| 1769 | if (m != last && m->next && !m->next->page_break) |
| 1770 | l->vshortfall[n] = this_height - minheight; |
| 1771 | else |
| 1772 | l->vshortfall[n] = 0; |
| 1773 | l->text[n] = text; |
| 1774 | l->space[n] = space; |
| 1775 | l->page_last[n] = m; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | if (m == last) |
| 1780 | break; |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * Now go through the line list forwards and assemble the |
| 1787 | * actual pages. |
| 1788 | */ |
| 1789 | ph = pt = NULL; |
| 1790 | |
| 1791 | l = first; |
| 1792 | n = 0; |
| 1793 | while (l) { |
| 1794 | page_data *page; |
| 1795 | int text, space, head; |
| 1796 | |
| 1797 | page = snew(page_data); |
| 1798 | page->next = NULL; |
| 1799 | page->prev = pt; |
| 1800 | if (pt) |
| 1801 | pt->next = page; |
| 1802 | else |
| 1803 | ph = page; |
| 1804 | pt = page; |
| 1805 | |
| 1806 | page->first_line = l; |
| 1807 | page->last_line = l->page_last[n]; |
| 1808 | |
| 1809 | page->first_text = page->last_text = NULL; |
| 1810 | page->first_xref = page->last_xref = NULL; |
| 1811 | page->first_rect = page->last_rect = NULL; |
| 1812 | |
| 1813 | /* |
| 1814 | * Now assign a y-coordinate to each line on the page. |
| 1815 | */ |
| 1816 | text = space = 0; |
| 1817 | head = (n < ncols ? headspace : 0); |
| 1818 | for (l = page->first_line; l; l = l->next) { |
| 1819 | if (l != page->first_line) { |
| 1820 | if (l->prev->space_after > 0) |
| 1821 | space += l->prev->space_after; |
| 1822 | else |
| 1823 | text += l->prev->space_after; |
| 1824 | } |
| 1825 | if (l != page->first_line || l->page_break) { |
| 1826 | if (l->space_before > 0) |
| 1827 | space += l->space_before; |
| 1828 | else |
| 1829 | text += l->space_before; |
| 1830 | } |
| 1831 | text += l->line_height; |
| 1832 | |
| 1833 | l->page = page; |
| 1834 | l->ypos = text + space + head; |
| 1835 | if (page->first_line->space[n]) { |
| 1836 | l->ypos += space * (float)page->first_line->vshortfall[n] / |
| 1837 | page->first_line->space[n]; |
| 1838 | } |
| 1839 | |
| 1840 | if (l == page->last_line) |
| 1841 | break; |
| 1842 | } |
| 1843 | |
| 1844 | l = page->last_line; |
| 1845 | if (l == last) |
| 1846 | break; |
| 1847 | l = l->next; |
| 1848 | |
| 1849 | n = (n < ncols ? n+1 : ncols); |
| 1850 | } |
| 1851 | |
| 1852 | return ph; |
| 1853 | } |
| 1854 | |
| 1855 | static void add_rect_to_page(page_data *page, int x, int y, int w, int h) |
| 1856 | { |
| 1857 | rect *r = snew(rect); |
| 1858 | |
| 1859 | r->next = NULL; |
| 1860 | if (page->last_rect) |
| 1861 | page->last_rect->next = r; |
| 1862 | else |
| 1863 | page->first_rect = r; |
| 1864 | page->last_rect = r; |
| 1865 | |
| 1866 | r->x = x; |
| 1867 | r->y = y; |
| 1868 | r->w = w; |
| 1869 | r->h = h; |
| 1870 | } |
| 1871 | |
| 1872 | static void add_string_to_page(page_data *page, int x, int y, |
| 1873 | font_encoding *fe, int size, char *text, |
| 1874 | int width) |
| 1875 | { |
| 1876 | text_fragment *frag; |
| 1877 | |
| 1878 | frag = snew(text_fragment); |
| 1879 | frag->next = NULL; |
| 1880 | |
| 1881 | if (page->last_text) |
| 1882 | page->last_text->next = frag; |
| 1883 | else |
| 1884 | page->first_text = frag; |
| 1885 | page->last_text = frag; |
| 1886 | |
| 1887 | frag->x = x; |
| 1888 | frag->y = y; |
| 1889 | frag->fe = fe; |
| 1890 | frag->fontsize = size; |
| 1891 | frag->text = dupstr(text); |
| 1892 | frag->width = width; |
| 1893 | } |
| 1894 | |
| 1895 | /* |
| 1896 | * Returns the updated x coordinate. |
| 1897 | */ |
| 1898 | static int render_string(page_data *page, font_data *font, int fontsize, |
| 1899 | int x, int y, wchar_t *str) |
| 1900 | { |
| 1901 | char *text; |
| 1902 | int textpos, textwid, kern, glyph, oglyph; |
| 1903 | font_encoding *subfont = NULL, *sf; |
| 1904 | |
| 1905 | text = snewn(1 + ustrlen(str), char); |
| 1906 | textpos = textwid = 0; |
| 1907 | |
| 1908 | glyph = 0xFFFF; |
| 1909 | while (*str) { |
| 1910 | oglyph = glyph; |
| 1911 | glyph = (*str < 0 || *str > 0xFFFF ? 0xFFFF : |
| 1912 | font->bmp[*str]); |
| 1913 | |
| 1914 | if (glyph == 0xFFFF) { |
| 1915 | str++; |
| 1916 | continue; /* nothing more we can do here */ |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * Find which subfont this character is going in. |
| 1921 | */ |
| 1922 | sf = font->subfont_map[glyph].subfont; |
| 1923 | |
| 1924 | if (!sf) { |
| 1925 | int c; |
| 1926 | |
| 1927 | /* |
| 1928 | * This character is not yet in a subfont. Assign one. |
| 1929 | */ |
| 1930 | if (font->latest_subfont->free_pos >= 0x100) |
| 1931 | font->latest_subfont = new_font_encoding(font); |
| 1932 | |
| 1933 | c = font->latest_subfont->free_pos++; |
| 1934 | if (font->latest_subfont->free_pos == 0x7F) |
| 1935 | font->latest_subfont->free_pos = 0xA1; |
| 1936 | |
| 1937 | font->subfont_map[glyph].subfont = font->latest_subfont; |
| 1938 | font->subfont_map[glyph].position = c; |
| 1939 | font->latest_subfont->vector[c] = font->glyphs[glyph]; |
| 1940 | font->latest_subfont->indices[c] = glyph; |
| 1941 | font->latest_subfont->to_unicode[c] = *str; |
| 1942 | |
| 1943 | sf = font->latest_subfont; |
| 1944 | } |
| 1945 | |
| 1946 | kern = find_kern(font, oglyph, glyph) * fontsize; |
| 1947 | |
| 1948 | if (!subfont || sf != subfont || kern) { |
| 1949 | if (subfont) { |
| 1950 | text[textpos] = '\0'; |
| 1951 | add_string_to_page(page, x, y, subfont, fontsize, text, |
| 1952 | textwid); |
| 1953 | x += textwid + kern; |
| 1954 | } else { |
| 1955 | assert(textpos == 0); |
| 1956 | } |
| 1957 | textpos = 0; |
| 1958 | textwid = 0; |
| 1959 | subfont = sf; |
| 1960 | } |
| 1961 | |
| 1962 | text[textpos++] = font->subfont_map[glyph].position; |
| 1963 | textwid += font->widths[glyph] * fontsize; |
| 1964 | |
| 1965 | str++; |
| 1966 | } |
| 1967 | |
| 1968 | if (textpos > 0) { |
| 1969 | text[textpos] = '\0'; |
| 1970 | add_string_to_page(page, x, y, subfont, fontsize, text, textwid); |
| 1971 | x += textwid; |
| 1972 | } |
| 1973 | |
| 1974 | return x; |
| 1975 | } |
| 1976 | |
| 1977 | /* |
| 1978 | * Returns the updated x coordinate. |
| 1979 | */ |
| 1980 | static int render_text(page_data *page, para_data *pdata, line_data *ldata, |
| 1981 | int x, int y, word *text, word *text_end, xref **xr, |
| 1982 | int shortfall, int nspaces, int *nspace, |
| 1983 | keywordlist *keywords, indexdata *idx, paper_conf *conf) |
| 1984 | { |
| 1985 | while (text && text != text_end) { |
| 1986 | int style, type, findex, errs; |
| 1987 | wchar_t *str; |
| 1988 | xref_dest dest; |
| 1989 | |
| 1990 | switch (text->type) { |
| 1991 | /* |
| 1992 | * Start a cross-reference. |
| 1993 | */ |
| 1994 | case word_HyperLink: |
| 1995 | case word_UpperXref: |
| 1996 | case word_LowerXref: |
| 1997 | case word_PageXref: |
| 1998 | |
| 1999 | if (text->type == word_HyperLink) { |
| 2000 | dest.type = URL; |
| 2001 | dest.url = utoa_dup(text->text, CS_ASCII); |
| 2002 | dest.page = NULL; |
| 2003 | } else if (text->type == word_PageXref) { |
| 2004 | dest.type = PAGE; |
| 2005 | dest.url = NULL; |
| 2006 | dest.page = (page_data *)text->private_data; |
| 2007 | } else { |
| 2008 | keyword *kwl = kw_lookup(keywords, text->text); |
| 2009 | para_data *pdata; |
| 2010 | |
| 2011 | if (kwl) { |
| 2012 | assert(kwl->para->private_data); |
| 2013 | pdata = (para_data *) kwl->para->private_data; |
| 2014 | dest.type = PAGE; |
| 2015 | dest.page = pdata->first->page; |
| 2016 | dest.url = NULL; |
| 2017 | } else { |
| 2018 | /* |
| 2019 | * Shouldn't happen, but *shrug* |
| 2020 | */ |
| 2021 | dest.type = NONE; |
| 2022 | dest.page = NULL; |
| 2023 | dest.url = NULL; |
| 2024 | } |
| 2025 | } |
| 2026 | if (dest.type != NONE) { |
| 2027 | *xr = snew(xref); |
| 2028 | (*xr)->dest = dest; /* structure copy */ |
| 2029 | if (page->last_xref) |
| 2030 | page->last_xref->next = *xr; |
| 2031 | else |
| 2032 | page->first_xref = *xr; |
| 2033 | page->last_xref = *xr; |
| 2034 | (*xr)->next = NULL; |
| 2035 | |
| 2036 | /* |
| 2037 | * FIXME: Ideally we should have, and use, some |
| 2038 | * vertical font metric information here so that |
| 2039 | * our cross-ref rectangle can take account of |
| 2040 | * descenders and the font's cap height. This will |
| 2041 | * do for the moment, but it isn't ideal. |
| 2042 | */ |
| 2043 | (*xr)->lx = (*xr)->rx = x; |
| 2044 | (*xr)->by = y; |
| 2045 | (*xr)->ty = y + ldata->line_height; |
| 2046 | } |
| 2047 | goto nextword; |
| 2048 | |
| 2049 | /* |
| 2050 | * Finish extending a cross-reference box. |
| 2051 | */ |
| 2052 | case word_HyperEnd: |
| 2053 | case word_XrefEnd: |
| 2054 | *xr = NULL; |
| 2055 | goto nextword; |
| 2056 | |
| 2057 | /* |
| 2058 | * Add the current page number to the list of pages |
| 2059 | * referenced by an index entry. |
| 2060 | */ |
| 2061 | case word_IndexRef: |
| 2062 | /* |
| 2063 | * We don't create index references in contents entries. |
| 2064 | */ |
| 2065 | if (!pdata->contents_entry) { |
| 2066 | indextag *tag; |
| 2067 | int i; |
| 2068 | |
| 2069 | tag = index_findtag(idx, text->text); |
| 2070 | if (!tag) |
| 2071 | goto nextword; |
| 2072 | |
| 2073 | for (i = 0; i < tag->nrefs; i++) { |
| 2074 | indexentry *entry = tag->refs[i]; |
| 2075 | paper_idx *pi = (paper_idx *)entry->backend_data; |
| 2076 | |
| 2077 | /* |
| 2078 | * If the same index term is indexed twice |
| 2079 | * within the same section, we only want to |
| 2080 | * mention it once in the index. |
| 2081 | */ |
| 2082 | if (pi->lastpage != page) { |
| 2083 | word **wp; |
| 2084 | |
| 2085 | if (pi->lastword) { |
| 2086 | pi->lastword = pi->lastword->next = |
| 2087 | fake_word(L","); |
| 2088 | pi->lastword = pi->lastword->next = |
| 2089 | fake_space_word(); |
| 2090 | wp = &pi->lastword->next; |
| 2091 | } else |
| 2092 | wp = &pi->words; |
| 2093 | |
| 2094 | pi->lastword = *wp = |
| 2095 | fake_page_ref(page); |
| 2096 | pi->lastword = pi->lastword->next = |
| 2097 | fake_word(page->number); |
| 2098 | pi->lastword = pi->lastword->next = |
| 2099 | fake_end_ref(); |
| 2100 | } |
| 2101 | |
| 2102 | pi->lastpage = page; |
| 2103 | } |
| 2104 | } |
| 2105 | goto nextword; |
| 2106 | } |
| 2107 | |
| 2108 | style = towordstyle(text->type); |
| 2109 | type = removeattr(text->type); |
| 2110 | |
| 2111 | findex = (style == word_Normal ? FONT_NORMAL : |
| 2112 | style == word_Emph ? FONT_EMPH : |
| 2113 | FONT_CODE); |
| 2114 | |
| 2115 | if (type == word_Normal) { |
| 2116 | str = text->text; |
| 2117 | } else if (type == word_WhiteSpace) { |
| 2118 | x += pdata->sizes[findex] * |
| 2119 | string_width(pdata->fonts[findex], L" ", NULL); |
| 2120 | if (nspaces && findex != FONT_CODE) { |
| 2121 | x += (*nspace+1) * shortfall / nspaces; |
| 2122 | x -= *nspace * shortfall / nspaces; |
| 2123 | (*nspace)++; |
| 2124 | } |
| 2125 | goto nextword; |
| 2126 | } else /* if (type == word_Quote) */ { |
| 2127 | if (text->aux == quote_Open) |
| 2128 | str = conf->lquote; |
| 2129 | else |
| 2130 | str = conf->rquote; |
| 2131 | } |
| 2132 | |
| 2133 | (void) string_width(pdata->fonts[findex], str, &errs); |
| 2134 | |
| 2135 | if (errs && text->alt) |
| 2136 | x = render_text(page, pdata, ldata, x, y, text->alt, NULL, |
| 2137 | xr, shortfall, nspaces, nspace, keywords, idx, |
| 2138 | conf); |
| 2139 | else |
| 2140 | x = render_string(page, pdata->fonts[findex], |
| 2141 | pdata->sizes[findex], x, y, str); |
| 2142 | |
| 2143 | if (*xr) |
| 2144 | (*xr)->rx = x; |
| 2145 | |
| 2146 | nextword: |
| 2147 | text = text->next; |
| 2148 | } |
| 2149 | |
| 2150 | return x; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * Returns the last x position used on the line. |
| 2155 | */ |
| 2156 | static int render_line(line_data *ldata, int left_x, int top_y, |
| 2157 | xref_dest *dest, keywordlist *keywords, indexdata *idx, |
| 2158 | paper_conf *conf) |
| 2159 | { |
| 2160 | int nspace; |
| 2161 | xref *xr; |
| 2162 | int ret = 0; |
| 2163 | |
| 2164 | if (ldata->aux_text) { |
| 2165 | int x; |
| 2166 | xr = NULL; |
| 2167 | nspace = 0; |
| 2168 | x = render_text(ldata->page, ldata->pdata, ldata, |
| 2169 | left_x + ldata->aux_left_indent, |
| 2170 | top_y - ldata->ypos, |
| 2171 | ldata->aux_text, NULL, &xr, 0, 0, &nspace, |
| 2172 | keywords, idx, conf); |
| 2173 | if (ldata->aux_text_2) |
| 2174 | render_text(ldata->page, ldata->pdata, ldata, |
| 2175 | x, top_y - ldata->ypos, |
| 2176 | ldata->aux_text_2, NULL, &xr, 0, 0, &nspace, |
| 2177 | keywords, idx, conf); |
| 2178 | } |
| 2179 | nspace = 0; |
| 2180 | |
| 2181 | if (ldata->first) { |
| 2182 | /* |
| 2183 | * There might be a cross-reference carried over from a |
| 2184 | * previous line. |
| 2185 | */ |
| 2186 | if (dest->type != NONE) { |
| 2187 | xr = snew(xref); |
| 2188 | xr->next = NULL; |
| 2189 | xr->dest = *dest; /* structure copy */ |
| 2190 | if (ldata->page->last_xref) |
| 2191 | ldata->page->last_xref->next = xr; |
| 2192 | else |
| 2193 | ldata->page->first_xref = xr; |
| 2194 | ldata->page->last_xref = xr; |
| 2195 | xr->lx = xr->rx = left_x + ldata->xpos; |
| 2196 | xr->by = top_y - ldata->ypos; |
| 2197 | xr->ty = top_y - ldata->ypos + ldata->line_height; |
| 2198 | } else |
| 2199 | xr = NULL; |
| 2200 | |
| 2201 | { |
| 2202 | int extra_indent, shortfall, spaces; |
| 2203 | int just = ldata->pdata->justification; |
| 2204 | |
| 2205 | /* |
| 2206 | * All forms of justification become JUST when we have |
| 2207 | * to squeeze the paragraph. |
| 2208 | */ |
| 2209 | if (ldata->hshortfall < 0) |
| 2210 | just = JUST; |
| 2211 | |
| 2212 | switch (just) { |
| 2213 | case JUST: |
| 2214 | shortfall = ldata->hshortfall; |
| 2215 | spaces = ldata->nspaces; |
| 2216 | extra_indent = 0; |
| 2217 | break; |
| 2218 | case LEFT: |
| 2219 | shortfall = spaces = extra_indent = 0; |
| 2220 | break; |
| 2221 | case RIGHT: |
| 2222 | shortfall = spaces = 0; |
| 2223 | extra_indent = ldata->real_shortfall; |
| 2224 | break; |
| 2225 | } |
| 2226 | |
| 2227 | ret = render_text(ldata->page, ldata->pdata, ldata, |
| 2228 | left_x + ldata->xpos + extra_indent, |
| 2229 | top_y - ldata->ypos, ldata->first, ldata->end, |
| 2230 | &xr, shortfall, spaces, &nspace, |
| 2231 | keywords, idx, conf); |
| 2232 | } |
| 2233 | |
| 2234 | if (xr) { |
| 2235 | /* |
| 2236 | * There's a cross-reference continued on to the next line. |
| 2237 | */ |
| 2238 | *dest = xr->dest; |
| 2239 | } else |
| 2240 | dest->type = NONE; |
| 2241 | } |
| 2242 | |
| 2243 | return ret; |
| 2244 | } |
| 2245 | |
| 2246 | static void render_para(para_data *pdata, paper_conf *conf, |
| 2247 | keywordlist *keywords, indexdata *idx, |
| 2248 | paragraph *index_placeholder, page_data *index_page) |
| 2249 | { |
| 2250 | int last_x; |
| 2251 | xref *cxref; |
| 2252 | page_data *cxref_page; |
| 2253 | xref_dest dest; |
| 2254 | para_data *target; |
| 2255 | line_data *ldata; |
| 2256 | |
| 2257 | dest.type = NONE; |
| 2258 | cxref = NULL; |
| 2259 | cxref_page = NULL; |
| 2260 | |
| 2261 | for (ldata = pdata->first; ldata; ldata = ldata->next) { |
| 2262 | /* |
| 2263 | * If this is a contents entry, we expect to have a single |
| 2264 | * enormous cross-reference rectangle covering the whole |
| 2265 | * thing. (Unless, of course, it spans multiple pages.) |
| 2266 | */ |
| 2267 | if (pdata->contents_entry && ldata->page != cxref_page) { |
| 2268 | cxref_page = ldata->page; |
| 2269 | cxref = snew(xref); |
| 2270 | cxref->next = NULL; |
| 2271 | cxref->dest.type = PAGE; |
| 2272 | if (pdata->contents_entry == index_placeholder) { |
| 2273 | cxref->dest.page = index_page; |
| 2274 | } else { |
| 2275 | assert(pdata->contents_entry->private_data); |
| 2276 | target = (para_data *)pdata->contents_entry->private_data; |
| 2277 | cxref->dest.page = target->first->page; |
| 2278 | } |
| 2279 | cxref->dest.url = NULL; |
| 2280 | if (ldata->page->last_xref) |
| 2281 | ldata->page->last_xref->next = cxref; |
| 2282 | else |
| 2283 | ldata->page->first_xref = cxref; |
| 2284 | ldata->page->last_xref = cxref; |
| 2285 | cxref->lx = conf->left_margin; |
| 2286 | cxref->rx = conf->paper_width - conf->right_margin; |
| 2287 | cxref->ty = conf->paper_height - conf->top_margin |
| 2288 | - ldata->ypos + ldata->line_height; |
| 2289 | } |
| 2290 | if (pdata->contents_entry) { |
| 2291 | assert(cxref != NULL); |
| 2292 | cxref->by = conf->paper_height - conf->top_margin |
| 2293 | - ldata->ypos; |
| 2294 | } |
| 2295 | |
| 2296 | last_x = render_line(ldata, conf->left_margin, |
| 2297 | conf->paper_height - conf->top_margin, |
| 2298 | &dest, keywords, idx, conf); |
| 2299 | if (ldata == pdata->last) |
| 2300 | break; |
| 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * If this is a contents entry, add leaders and a page |
| 2305 | * number. |
| 2306 | */ |
| 2307 | if (pdata->contents_entry) { |
| 2308 | word *w; |
| 2309 | wchar_t *num; |
| 2310 | int wid; |
| 2311 | int x; |
| 2312 | |
| 2313 | if (pdata->contents_entry == index_placeholder) { |
| 2314 | num = index_page->number; |
| 2315 | } else { |
| 2316 | assert(pdata->contents_entry->private_data); |
| 2317 | target = (para_data *)pdata->contents_entry->private_data; |
| 2318 | num = target->first->page->number; |
| 2319 | } |
| 2320 | |
| 2321 | w = fake_word(num); |
| 2322 | wid = paper_width_simple(pdata, w, conf); |
| 2323 | sfree(w); |
| 2324 | |
| 2325 | for (x = 0; x < conf->base_width; x += conf->leader_separation) |
| 2326 | if (x - conf->leader_separation > last_x - conf->left_margin && |
| 2327 | x + conf->leader_separation < conf->base_width - wid) |
| 2328 | render_string(pdata->last->page, |
| 2329 | pdata->fonts[FONT_NORMAL], |
| 2330 | pdata->sizes[FONT_NORMAL], |
| 2331 | conf->left_margin + x, |
| 2332 | (conf->paper_height - conf->top_margin - |
| 2333 | pdata->last->ypos), L"."); |
| 2334 | |
| 2335 | render_string(pdata->last->page, |
| 2336 | pdata->fonts[FONT_NORMAL], |
| 2337 | pdata->sizes[FONT_NORMAL], |
| 2338 | conf->paper_width - conf->right_margin - wid, |
| 2339 | (conf->paper_height - conf->top_margin - |
| 2340 | pdata->last->ypos), num); |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * Render any rectangle (chapter title underline or rule) |
| 2345 | * that goes with this paragraph. |
| 2346 | */ |
| 2347 | switch (pdata->rect_type) { |
| 2348 | case RECT_CHAPTER_UNDERLINE: |
| 2349 | add_rect_to_page(pdata->last->page, |
| 2350 | conf->left_margin, |
| 2351 | (conf->paper_height - conf->top_margin - |
| 2352 | pdata->last->ypos - |
| 2353 | conf->chapter_underline_depth), |
| 2354 | conf->base_width, |
| 2355 | conf->chapter_underline_thickness); |
| 2356 | break; |
| 2357 | case RECT_RULE: |
| 2358 | add_rect_to_page(pdata->first->page, |
| 2359 | conf->left_margin + pdata->first->xpos, |
| 2360 | (conf->paper_height - conf->top_margin - |
| 2361 | pdata->last->ypos - |
| 2362 | pdata->last->line_height), |
| 2363 | conf->base_width - pdata->first->xpos, |
| 2364 | pdata->last->line_height); |
| 2365 | break; |
| 2366 | default: /* placate gcc */ |
| 2367 | break; |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | static para_data *code_paragraph(int indent, word *words, paper_conf *conf) |
| 2372 | { |
| 2373 | para_data *pdata = snew(para_data); |
| 2374 | |
| 2375 | /* |
| 2376 | * For code paragraphs, I'm going to hack grievously and |
| 2377 | * pretend the three normal fonts are the three code paragraph |
| 2378 | * fonts. |
| 2379 | */ |
| 2380 | setfont(pdata, &conf->fcode); |
| 2381 | |
| 2382 | pdata->first = pdata->last = NULL; |
| 2383 | pdata->outline_level = -1; |
| 2384 | pdata->rect_type = RECT_NONE; |
| 2385 | pdata->contents_entry = NULL; |
| 2386 | pdata->justification = LEFT; |
| 2387 | |
| 2388 | for (; words; words = words->next) { |
| 2389 | wchar_t *t, *e, *start; |
| 2390 | word *lhead = NULL, *ltail = NULL, *w; |
| 2391 | line_data *ldata; |
| 2392 | int prev = -1, curr; |
| 2393 | |
| 2394 | t = words->text; |
| 2395 | if (words->next && words->next->type == word_Emph) { |
| 2396 | e = words->next->text; |
| 2397 | words = words->next; |
| 2398 | } else |
| 2399 | e = NULL; |
| 2400 | |
| 2401 | start = t; |
| 2402 | |
| 2403 | while (*start) { |
| 2404 | while (*t) { |
| 2405 | if (!e || !*e) |
| 2406 | curr = 0; |
| 2407 | else if (*e == L'i') |
| 2408 | curr = 1; |
| 2409 | else if (*e == L'b') |
| 2410 | curr = 2; |
| 2411 | else |
| 2412 | curr = 0; |
| 2413 | |
| 2414 | if (prev < 0) |
| 2415 | prev = curr; |
| 2416 | |
| 2417 | if (curr != prev) |
| 2418 | break; |
| 2419 | |
| 2420 | t++; |
| 2421 | if (e && *e) |
| 2422 | e++; |
| 2423 | } |
| 2424 | |
| 2425 | /* |
| 2426 | * We've isolated a maximal subsequence of the line |
| 2427 | * which has the same emphasis. Form it into a word |
| 2428 | * structure. |
| 2429 | */ |
| 2430 | w = snew(word); |
| 2431 | w->next = NULL; |
| 2432 | w->alt = NULL; |
| 2433 | w->type = (prev == 0 ? word_WeakCode : |
| 2434 | prev == 1 ? word_Emph : word_Normal); |
| 2435 | w->text = snewn(t-start+1, wchar_t); |
| 2436 | memcpy(w->text, start, (t-start) * sizeof(wchar_t)); |
| 2437 | w->text[t-start] = '\0'; |
| 2438 | w->breaks = FALSE; |
| 2439 | |
| 2440 | if (ltail) |
| 2441 | ltail->next = w; |
| 2442 | else |
| 2443 | lhead = w; |
| 2444 | ltail = w; |
| 2445 | |
| 2446 | start = t; |
| 2447 | prev = -1; |
| 2448 | } |
| 2449 | |
| 2450 | ldata = snew(line_data); |
| 2451 | |
| 2452 | ldata->pdata = pdata; |
| 2453 | ldata->first = lhead; |
| 2454 | ldata->end = NULL; |
| 2455 | ldata->line_height = conf->fcode.font_size * UNITS_PER_PT; |
| 2456 | |
| 2457 | ldata->xpos = indent; |
| 2458 | |
| 2459 | if (pdata->last) { |
| 2460 | pdata->last->next = ldata; |
| 2461 | ldata->prev = pdata->last; |
| 2462 | } else { |
| 2463 | pdata->first = ldata; |
| 2464 | ldata->prev = NULL; |
| 2465 | } |
| 2466 | ldata->next = NULL; |
| 2467 | pdata->last = ldata; |
| 2468 | |
| 2469 | ldata->hshortfall = 0; |
| 2470 | ldata->nspaces = 0; |
| 2471 | ldata->aux_text = NULL; |
| 2472 | ldata->aux_text_2 = NULL; |
| 2473 | ldata->aux_left_indent = 0; |
| 2474 | /* General opprobrium for breaking in a code paragraph. */ |
| 2475 | ldata->penalty_before = ldata->penalty_after = 50000; |
| 2476 | } |
| 2477 | |
| 2478 | standard_line_spacing(pdata, conf); |
| 2479 | |
| 2480 | return pdata; |
| 2481 | } |
| 2482 | |
| 2483 | static para_data *rule_paragraph(int indent, paper_conf *conf) |
| 2484 | { |
| 2485 | para_data *pdata = snew(para_data); |
| 2486 | line_data *ldata; |
| 2487 | |
| 2488 | ldata = snew(line_data); |
| 2489 | |
| 2490 | ldata->pdata = pdata; |
| 2491 | ldata->first = NULL; |
| 2492 | ldata->end = NULL; |
| 2493 | ldata->line_height = conf->rule_thickness; |
| 2494 | |
| 2495 | ldata->xpos = indent; |
| 2496 | |
| 2497 | ldata->prev = NULL; |
| 2498 | ldata->next = NULL; |
| 2499 | |
| 2500 | ldata->hshortfall = 0; |
| 2501 | ldata->nspaces = 0; |
| 2502 | ldata->aux_text = NULL; |
| 2503 | ldata->aux_text_2 = NULL; |
| 2504 | ldata->aux_left_indent = 0; |
| 2505 | |
| 2506 | /* |
| 2507 | * Better to break after a rule than before it |
| 2508 | */ |
| 2509 | ldata->penalty_after += 100000; |
| 2510 | ldata->penalty_before += -100000; |
| 2511 | |
| 2512 | pdata->first = pdata->last = ldata; |
| 2513 | pdata->outline_level = -1; |
| 2514 | pdata->rect_type = RECT_RULE; |
| 2515 | pdata->contents_entry = NULL; |
| 2516 | pdata->justification = LEFT; |
| 2517 | |
| 2518 | standard_line_spacing(pdata, conf); |
| 2519 | |
| 2520 | return pdata; |
| 2521 | } |
| 2522 | |
| 2523 | /* |
| 2524 | * Plain-text-like formatting for outline titles. |
| 2525 | */ |
| 2526 | static void paper_rdaddw(rdstring *rs, word *text) { |
| 2527 | for (; text; text = text->next) switch (text->type) { |
| 2528 | case word_HyperLink: |
| 2529 | case word_HyperEnd: |
| 2530 | case word_UpperXref: |
| 2531 | case word_LowerXref: |
| 2532 | case word_XrefEnd: |
| 2533 | case word_IndexRef: |
| 2534 | break; |
| 2535 | |
| 2536 | case word_Normal: |
| 2537 | case word_Emph: |
| 2538 | case word_Code: |
| 2539 | case word_WeakCode: |
| 2540 | case word_WhiteSpace: |
| 2541 | case word_EmphSpace: |
| 2542 | case word_CodeSpace: |
| 2543 | case word_WkCodeSpace: |
| 2544 | case word_Quote: |
| 2545 | case word_EmphQuote: |
| 2546 | case word_CodeQuote: |
| 2547 | case word_WkCodeQuote: |
| 2548 | assert(text->type != word_CodeQuote && |
| 2549 | text->type != word_WkCodeQuote); |
| 2550 | if (towordstyle(text->type) == word_Emph && |
| 2551 | (attraux(text->aux) == attr_First || |
| 2552 | attraux(text->aux) == attr_Only)) |
| 2553 | rdadd(rs, L'_'); /* FIXME: configurability */ |
| 2554 | else if (towordstyle(text->type) == word_Code && |
| 2555 | (attraux(text->aux) == attr_First || |
| 2556 | attraux(text->aux) == attr_Only)) |
| 2557 | rdadd(rs, L'\''); /* FIXME: configurability */ |
| 2558 | if (removeattr(text->type) == word_Normal) { |
| 2559 | rdadds(rs, text->text); |
| 2560 | } else if (removeattr(text->type) == word_WhiteSpace) { |
| 2561 | rdadd(rs, L' '); |
| 2562 | } else if (removeattr(text->type) == word_Quote) { |
| 2563 | rdadd(rs, L'\''); /* fixme: configurability */ |
| 2564 | } |
| 2565 | if (towordstyle(text->type) == word_Emph && |
| 2566 | (attraux(text->aux) == attr_Last || |
| 2567 | attraux(text->aux) == attr_Only)) |
| 2568 | rdadd(rs, L'_'); /* FIXME: configurability */ |
| 2569 | else if (towordstyle(text->type) == word_Code && |
| 2570 | (attraux(text->aux) == attr_Last || |
| 2571 | attraux(text->aux) == attr_Only)) |
| 2572 | rdadd(rs, L'\''); /* FIXME: configurability */ |
| 2573 | break; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | static wchar_t *prepare_outline_title(word *first, wchar_t *separator, |
| 2578 | word *second) |
| 2579 | { |
| 2580 | rdstring rs = {0, 0, NULL}; |
| 2581 | |
| 2582 | if (first) |
| 2583 | paper_rdaddw(&rs, first); |
| 2584 | if (separator) |
| 2585 | rdadds(&rs, separator); |
| 2586 | if (second) |
| 2587 | paper_rdaddw(&rs, second); |
| 2588 | |
| 2589 | return rs.text; |
| 2590 | } |
| 2591 | |
| 2592 | static word *fake_word(wchar_t *text) |
| 2593 | { |
| 2594 | word *ret = snew(word); |
| 2595 | ret->next = NULL; |
| 2596 | ret->alt = NULL; |
| 2597 | ret->type = word_Normal; |
| 2598 | ret->text = ustrdup(text); |
| 2599 | ret->breaks = FALSE; |
| 2600 | ret->aux = 0; |
| 2601 | return ret; |
| 2602 | } |
| 2603 | |
| 2604 | static word *fake_space_word(void) |
| 2605 | { |
| 2606 | word *ret = snew(word); |
| 2607 | ret->next = NULL; |
| 2608 | ret->alt = NULL; |
| 2609 | ret->type = word_WhiteSpace; |
| 2610 | ret->text = NULL; |
| 2611 | ret->breaks = TRUE; |
| 2612 | ret->aux = 0; |
| 2613 | return ret; |
| 2614 | } |
| 2615 | |
| 2616 | static word *fake_page_ref(page_data *page) |
| 2617 | { |
| 2618 | word *ret = snew(word); |
| 2619 | ret->next = NULL; |
| 2620 | ret->alt = NULL; |
| 2621 | ret->type = word_PageXref; |
| 2622 | ret->text = NULL; |
| 2623 | ret->breaks = FALSE; |
| 2624 | ret->aux = 0; |
| 2625 | ret->private_data = page; |
| 2626 | return ret; |
| 2627 | } |
| 2628 | |
| 2629 | static word *fake_end_ref(void) |
| 2630 | { |
| 2631 | word *ret = snew(word); |
| 2632 | ret->next = NULL; |
| 2633 | ret->alt = NULL; |
| 2634 | ret->type = word_XrefEnd; |
| 2635 | ret->text = NULL; |
| 2636 | ret->breaks = FALSE; |
| 2637 | ret->aux = 0; |
| 2638 | return ret; |
| 2639 | } |
| 2640 | |
| 2641 | static word *prepare_contents_title(word *first, wchar_t *separator, |
| 2642 | word *second) |
| 2643 | { |
| 2644 | word *ret; |
| 2645 | word **wptr, *w; |
| 2646 | |
| 2647 | wptr = &ret; |
| 2648 | |
| 2649 | if (first) { |
| 2650 | w = dup_word_list(first); |
| 2651 | *wptr = w; |
| 2652 | while (w->next) |
| 2653 | w = w->next; |
| 2654 | wptr = &w->next; |
| 2655 | } |
| 2656 | |
| 2657 | if (separator) { |
| 2658 | w = fake_word(separator); |
| 2659 | *wptr = w; |
| 2660 | wptr = &w->next; |
| 2661 | } |
| 2662 | |
| 2663 | if (second) { |
| 2664 | *wptr = dup_word_list(second); |
| 2665 | } |
| 2666 | |
| 2667 | return ret; |
| 2668 | } |
| 2669 | |
| 2670 | static void fold_into_page(page_data *dest, page_data *src, int right_shift) |
| 2671 | { |
| 2672 | line_data *ldata; |
| 2673 | |
| 2674 | if (!src->first_line) |
| 2675 | return; |
| 2676 | |
| 2677 | if (dest->last_line) { |
| 2678 | dest->last_line->next = src->first_line; |
| 2679 | src->first_line->prev = dest->last_line; |
| 2680 | } |
| 2681 | dest->last_line = src->last_line; |
| 2682 | |
| 2683 | for (ldata = src->first_line; ldata; ldata = ldata->next) { |
| 2684 | ldata->page = dest; |
| 2685 | ldata->xpos += right_shift; |
| 2686 | |
| 2687 | if (ldata == src->last_line) |
| 2688 | break; |
| 2689 | } |
| 2690 | } |