X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/c65367738ae7b8bde8c52d01d82a19987bb4941d..60daa966e8090794ec307cddebf296f0cc0a3f20:/bk_paper.c diff --git a/bk_paper.c b/bk_paper.c index 720f088..aed648c 100644 --- a/bk_paper.c +++ b/bk_paper.c @@ -10,31 +10,74 @@ */ /* - * To be done: - * - * - index - * - * - header/footer? Page numbers at least would be handy. Fully - * configurable footer can wait, though. - * - * That should bring us to the same level of functionality that - * original-Halibut had, and the same in PDF plus the obvious - * interactive navigation features. After that, in future work: + * TODO in future work: * * - linearised PDF, perhaps? * + * - we should use PDFDocEncoding or Unicode for outline strings, + * now that I actually know how to do them. Probably easiest if + * I do this _after_ bringing in libcharset, since I can simply + * supply PDFDocEncoding in there. + * * - I'm uncertain of whether I need to include a ToUnicode CMap * in each of my font definitions in PDF. Currently things (by * which I mean cut and paste out of acroread) seem to be * working fairly happily without it, but I don't know. * + * - rather than the ugly aux_text mechanism for rendering chapter + * titles, we could actually build the correct word list and + * wrap it as a whole. + * + * - get vertical font metrics and use them to position the PDF + * xref boxes more pleasantly + * * - configurability + * * page header and footer should be configurable; we should + * be able to shift the page number elsewhere, and add other + * things such as the current chapter/section title and fixed + * text + * * remove the fixed mapping from heading levels to heading + * styles; offer a menu of styles from which the user can + * choose at every heading level + * * first-line indent in paragraphs + * * fixed text: `Contents', `Index', the colon-space and full + * stop in chapter title constructions + * * configurable location of contents? + * * certainly configurably _remove_ the contents, and possibly + * also the index + * * double-sided document switch? + * + means you have two header/footer formats which + * alternate + * + and means that mandatory page breaks before chapter + * titles should include a blank page if necessary to + * start the next section to a right-hand page * * - title pages + * + * - ability to import other Type 1 fonts + * * we need to parse the font to extract its metrics + * * then we pass the font bodily to both PS and PDF so it can + * be included in the output file + * + * - character substitution for better typography? + * * fi, fl, ffi, ffl ligatures + * * use real ellipsis rather than ... + * * a hyphen in a word by itself might prefer to be an en-dash + * * (Americans might even want a convenient way to use an + * em-dash) + * * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw! + * * substituting `minus' for `hyphen' in the standard encoding + * is probably preferable in Courier, though certainly not in + * the main text font + * * if I do do this lot, I'm rather inclined to at least try + * to think up a configurable way to do it so that Americans + * can do em-dash tricks without my intervention and other + * people can do other odd things too. */ #include #include +#include #include "halibut.h" #include "paper.h" @@ -50,6 +93,7 @@ struct paper_conf_Tag { int right_margin; int bottom_margin; int indent_list_bullet; + int indent_list_after; int indent_list; int indent_quote; int base_leading; @@ -66,6 +110,9 @@ struct paper_conf_Tag { int index_gutter; int index_cols; int index_minsep; + int pagenum_fontsize; + int footer_distance; + wchar_t *lquote, *rquote, *bullet; /* These are derived from the above */ int base_width; int page_height; @@ -89,19 +136,25 @@ struct paper_idx_Tag { page_data *lastpage; }; +enum { + word_PageXref = word_NotWordType + 1 +}; + static font_data *make_std_font(font_list *fontlist, char const *name); static void wrap_paragraph(para_data *pdata, word *words, - int w, int i1, int i2); + int w, int i1, int i2, paper_conf *conf); static page_data *page_breaks(line_data *first, line_data *last, int page_height, int ncols, int headspace); static int render_string(page_data *page, font_data *font, int fontsize, int x, int y, wchar_t *str); static int render_line(line_data *ldata, int left_x, int top_y, - xref_dest *dest, keywordlist *keywords, indexdata *idx); + xref_dest *dest, keywordlist *keywords, indexdata *idx, + paper_conf *conf); static void render_para(para_data *pdata, paper_conf *conf, keywordlist *keywords, indexdata *idx, paragraph *index_placeholder, page_data *index_page); -static int paper_width_simple(para_data *pdata, word *text); +static int string_width(font_data *font, wchar_t const *string, int *errs); +static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf); static para_data *code_paragraph(int indent, word *words, paper_conf *conf); static para_data *rule_paragraph(int indent, paper_conf *conf); static void add_rect_to_page(page_data *page, int x, int y, int w, int h); @@ -113,10 +166,223 @@ static wchar_t *prepare_outline_title(word *first, wchar_t *separator, word *second); static word *fake_word(wchar_t *text); static word *fake_space_word(void); +static word *fake_page_ref(page_data *page); +static word *fake_end_ref(void); static word *prepare_contents_title(word *first, wchar_t *separator, word *second); static void fold_into_page(page_data *dest, page_data *src, int right_shift); +static int fonts_ok(wchar_t *string, ...) +{ + font_data *font; + va_list ap; + int ret = TRUE; + + va_start(ap, string); + while ( (font = va_arg(ap, font_data *)) != NULL) { + int errs; + (void) string_width(font, string, &errs); + if (errs) { + ret = FALSE; + break; + } + } + va_end(ap); + + return ret; +} + +static paper_conf paper_configure(paragraph *source, font_list *fontlist) { + paragraph *p; + paper_conf ret; + + /* + * Defaults. + */ + ret.paper_width = 595 * UNITS_PER_PT; + ret.paper_height = 841 * UNITS_PER_PT; + ret.left_margin = 72 * UNITS_PER_PT; + ret.top_margin = 72 * UNITS_PER_PT; + ret.right_margin = 72 * UNITS_PER_PT; + ret.bottom_margin = 108 * UNITS_PER_PT; + ret.indent_list_bullet = 6 * UNITS_PER_PT; + ret.indent_list_after = 18 * UNITS_PER_PT; + ret.indent_quote = 18 * UNITS_PER_PT; + ret.base_leading = UNITS_PER_PT; + ret.base_para_spacing = 10 * UNITS_PER_PT; + ret.chapter_top_space = 72 * UNITS_PER_PT; + ret.sect_num_left_space = 12 * UNITS_PER_PT; + ret.chapter_underline_depth = 14 * UNITS_PER_PT; + ret.chapter_underline_thickness = 3 * UNITS_PER_PT; + ret.rule_thickness = 1 * UNITS_PER_PT; + ret.base_font_size = 12; + ret.contents_indent_step = 24 * UNITS_PER_PT; + ret.contents_margin = 84 * UNITS_PER_PT; + ret.leader_separation = 12 * UNITS_PER_PT; + ret.index_gutter = 36 * UNITS_PER_PT; + ret.index_cols = 2; + ret.index_minsep = 18 * UNITS_PER_PT; + ret.pagenum_fontsize = 12; + ret.footer_distance = 32 * UNITS_PER_PT; + ret.lquote = L"\x2018\0\x2019\0'\0'\0\0"; + ret.rquote = uadv(ret.lquote); + ret.bullet = L"\x2022\0-\0\0"; + + /* + * Two-pass configuration so that we can pick up global config + * (e.g. `quotes') before having it overridden by specific + * config (`paper-quotes'), irrespective of the order in which + * they occur. + */ + for (p = source; p; p = p->next) { + if (p->type == para_Config) { + if (!ustricmp(p->keyword, L"quotes")) { + if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { + ret.lquote = uadv(p->keyword); + ret.rquote = uadv(ret.lquote); + } + } + } + } + + for (p = source; p; p = p->next) { + p->private_data = NULL; + if (p->type == para_Config) { + if (!ustricmp(p->keyword, L"paper-quotes")) { + if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { + ret.lquote = uadv(p->keyword); + ret.rquote = uadv(ret.lquote); + } + } else if (!ustricmp(p->keyword, L"paper-bullet")) { + ret.bullet = uadv(p->keyword); + } else if (!ustricmp(p->keyword, L"paper-page-width")) { + ret.paper_width = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-page-height")) { + ret.paper_height = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-left-margin")) { + ret.left_margin = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-top-margin")) { + ret.top_margin = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-right-margin")) { + ret.right_margin = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-bottom-margin")) { + ret.bottom_margin = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-list-indent")) { + ret.indent_list_bullet = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-listitem-indent")) { + ret.indent_list = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-quote-indent")) { + ret.indent_quote = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-base-leading")) { + ret.base_leading = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-base-para-spacing")) { + ret.base_para_spacing = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-chapter-top-space")) { + ret.chapter_top_space = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-sect-num-left-space")) { + ret.sect_num_left_space = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-chapter-underline-depth")) { + ret.chapter_underline_depth = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-chapter-underline-thickness")) { + ret.chapter_underline_thickness = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-rule-thickness")) { + ret.rule_thickness = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-contents-indent-step")) { + ret.contents_indent_step = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-contents-margin")) { + ret.contents_margin = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-leader-separation")) { + ret.leader_separation = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-index-gutter")) { + ret.index_gutter = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-index-minsep")) { + ret.index_minsep = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-footer-distance")) { + ret.footer_distance = + (int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-base-font-size")) { + ret.base_font_size = + utoi(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-index-columns")) { + ret.index_cols = + utoi(uadv(p->keyword)); + } else if (!ustricmp(p->keyword, L"paper-pagenum-font-size")) { + ret.pagenum_fontsize = + utoi(uadv(p->keyword)); + } + } + } + + /* + * Set up the derived fields in the conf structure. + */ + + ret.base_width = + ret.paper_width - ret.left_margin - ret.right_margin; + ret.page_height = + ret.paper_height - ret.top_margin - ret.bottom_margin; + ret.indent_list = ret.indent_list_bullet + ret.indent_list_after; + ret.index_colwidth = + (ret.base_width - (ret.index_cols-1) * ret.index_gutter) + / ret.index_cols; + + /* + * Set up the font structures. + */ + ret.tr = make_std_font(fontlist, "Times-Roman"); + ret.ti = make_std_font(fontlist, "Times-Italic"); + ret.hr = make_std_font(fontlist, "Helvetica-Bold"); + ret.hi = make_std_font(fontlist, "Helvetica-BoldOblique"); + ret.cr = make_std_font(fontlist, "Courier"); + ret.co = make_std_font(fontlist, "Courier-Oblique"); + ret.cb = make_std_font(fontlist, "Courier-Bold"); + + /* + * Now process fallbacks on quote characters and bullets. We + * use string_width() to determine whether all of the relevant + * fonts contain the same character, and fall back whenever we + * find a character which not all of them support. + */ + + /* Quote characters need not be supported in the fixed code fonts, + * but must be in the title and body fonts. */ + while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && + (!fonts_ok(ret.lquote, ret.tr, ret.ti, ret.hr, ret.hi, NULL) || + !fonts_ok(ret.rquote, ret.tr, ret.ti, ret.hr, ret.hi, NULL))) { + ret.lquote = uadv(ret.rquote); + ret.rquote = uadv(ret.lquote); + } + + /* The bullet character only needs to be supported in the normal body + * font (not even in italics). */ + while (*ret.bullet && *uadv(ret.bullet) && + !fonts_ok(ret.bullet, ret.tr, NULL)) + ret.bullet = uadv(ret.bullet); + + return ret; +} + void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, indexdata *idx) { paragraph *p; @@ -127,62 +393,17 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, line_data *firstline, *lastline, *firstcontline, *lastcontline; page_data *pages; font_list *fontlist; - paper_conf *conf; + paper_conf *conf, ourconf; int has_index; int pagenum; paragraph index_placeholder_para; page_data *first_index_page; - /* - * FIXME: All these things ought to become configurable. - */ - conf = mknew(paper_conf); - conf->paper_width = 595 * 4096; - conf->paper_height = 841 * 4096; - conf->left_margin = 72 * 4096; - conf->top_margin = 72 * 4096; - conf->right_margin = 72 * 4096; - conf->bottom_margin = 108 * 4096; - conf->indent_list_bullet = 6 * 4096; - conf->indent_list = 24 * 4096; - conf->indent_quote = 18 * 4096; - conf->base_leading = 4096; - conf->base_para_spacing = 10 * 4096; - conf->chapter_top_space = 72 * 4096; - conf->sect_num_left_space = 12 * 4096; - conf->chapter_underline_depth = 14 * 4096; - conf->chapter_underline_thickness = 3 * 4096; - conf->rule_thickness = 1 * 4096; - conf->base_font_size = 12; - conf->contents_indent_step = 24 * 4096; - conf->contents_margin = 84 * 4096; - conf->leader_separation = 12 * 4096; - conf->index_gutter = 36 * 4096; - conf->index_cols = 2; - conf->index_minsep = 18 * 4096; - - conf->base_width = - conf->paper_width - conf->left_margin - conf->right_margin; - conf->page_height = - conf->paper_height - conf->top_margin - conf->bottom_margin; - conf->index_colwidth = - (conf->base_width - (conf->index_cols-1) * conf->index_gutter) - / conf->index_cols; - - IGNORE(idx); /* FIXME */ - - /* - * First, set up some font structures. - */ - fontlist = mknew(font_list); + fontlist = snew(font_list); fontlist->head = fontlist->tail = NULL; - conf->tr = make_std_font(fontlist, "Times-Roman"); - conf->ti = make_std_font(fontlist, "Times-Italic"); - conf->hr = make_std_font(fontlist, "Helvetica-Bold"); - conf->hi = make_std_font(fontlist, "Helvetica-BoldOblique"); - conf->cr = make_std_font(fontlist, "Courier"); - conf->co = make_std_font(fontlist, "Courier-Oblique"); - conf->cb = make_std_font(fontlist, "Courier-Bold"); + + ourconf = paper_configure(sourceform, fontlist); + conf = &ourconf; /* * Set up a data structure to collect page numbers for each @@ -195,7 +416,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, has_index = FALSE; for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { - paper_idx *pi = mknew(paper_idx); + paper_idx *pi = snew(paper_idx); has_index = TRUE; @@ -457,11 +678,11 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, for (page = pages; page; page = page->next) { sprintf(buf, "%d", ++pagenum); - page->number = ufroma_dup(buf); + page->number = ufroma_dup(buf, CS_ASCII); } if (has_index) { - first_index_page = mknew(page_data); + first_index_page = snew(page_data); first_index_page->next = first_index_page->prev = NULL; first_index_page->first_line = NULL; first_index_page->last_line = NULL; @@ -471,7 +692,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, /* And don't forget the as-yet-uncreated index. */ sprintf(buf, "%d", ++pagenum); - first_index_page->number = ufroma_dup(buf); + first_index_page->number = ufroma_dup(buf, CS_ASCII); } } @@ -511,6 +732,9 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, paper_idx *pi = (paper_idx *)entry->backend_data; para_data *text, *pages; + if (!pi->words) + continue; + text = make_para_data(para_Normal, 0, 0, conf->base_width - conf->index_colwidth, NULL, NULL, entry->text, conf); @@ -627,7 +851,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, for (page = ipages->next; page; page = page->next) { char buf[40]; sprintf(buf, "%d", ++pagenum); - page->number = ufroma_dup(buf); + page->number = ufroma_dup(buf, CS_ASCII); } /* @@ -660,10 +884,33 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, } /* + * Draw the headers and footers. + * + * FIXME: this should be fully configurable, but for the moment + * I'm just going to put in page numbers in the centre of a + * footer and leave it at that. + */ + { + page_data *page; + + for (page = pages; page; page = page->next) { + int width; + + width = conf->pagenum_fontsize * + string_width(conf->tr, page->number, NULL); + + render_string(page, conf->tr, conf->pagenum_fontsize, + conf->left_margin + (conf->base_width - width)/2, + conf->bottom_margin - conf->footer_distance, + page->number); + } + } + + /* * Start putting together the overall document structure we're * going to return. */ - doc = mknew(document); + doc = snew(document); doc->fonts = fontlist; doc->pages = pages; doc->paper_width = conf->paper_width; @@ -678,7 +925,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, { int osize = 20; - doc->outline_elements = mknewa(outline_element, osize); + doc->outline_elements = snewn(osize, outline_element); doc->n_outline_elements = 0; /* First find the title. */ @@ -697,7 +944,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, if (doc->n_outline_elements >= osize) { osize += 20; doc->outline_elements = - resize(doc->outline_elements, osize); + sresize(doc->outline_elements, osize, outline_element); } doc->outline_elements[doc->n_outline_elements].level = @@ -708,8 +955,6 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords, } } - sfree(conf); - return doc; } @@ -722,7 +967,7 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, int extra_indent, firstline_indent, aux_indent; word *aux, *aux2; - pdata = mknew(para_data); + pdata = snew(para_data); pdata->outline_level = -1; pdata->outline_title = NULL; pdata->rect_type = RECT_NONE; @@ -829,7 +1074,7 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, int len; aux = pkwtext2; - len = paper_width_simple(pdata, pkwtext2); + len = paper_width_simple(pdata, pkwtext2, conf); aux_indent = -len - conf->sect_num_left_space; pdata->outline_title = @@ -839,8 +1084,8 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, aux2 = fake_word(L": "); aux_indent = 0; - firstline_indent += paper_width_simple(pdata, aux); - firstline_indent += paper_width_simple(pdata, aux2); + firstline_indent += paper_width_simple(pdata, aux, conf); + firstline_indent += paper_width_simple(pdata, aux2, conf); pdata->outline_title = prepare_outline_title(pkwtext, L": ", pwords); @@ -849,10 +1094,9 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, case para_Bullet: /* - * Auxiliary text consisting of a bullet. (FIXME: - * configurable bullet.) + * Auxiliary text consisting of a bullet. */ - aux = fake_word(L"\x2022"); + aux = fake_word(conf->bullet); aux_indent = indent + conf->indent_list_bullet; break; @@ -874,8 +1118,8 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, aux = pkwtext; aux2 = fake_word(L" "); aux_indent = indent; - firstline_indent += paper_width_simple(pdata, aux); - firstline_indent += paper_width_simple(pdata, aux2); + firstline_indent += paper_width_simple(pdata, aux, conf); + firstline_indent += paper_width_simple(pdata, aux2, conf); break; } @@ -886,7 +1130,7 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin, wrap_paragraph(pdata, pwords, conf->base_width - rmargin, indent + firstline_indent, - indent + extra_indent); + indent + extra_indent, conf); pdata->first->aux_text = aux; pdata->first->aux_text_2 = aux2; @@ -983,7 +1227,7 @@ static font_encoding *new_font_encoding(font_data *font) font_encoding *fe; int i; - fe = mknew(font_encoding); + fe = snew(font_encoding); fe->next = NULL; if (font->list->tail) @@ -1004,28 +1248,49 @@ static font_encoding *new_font_encoding(font_data *font) return fe; } +static int kern_cmp(void *a, void *b) +{ + kern_pair const *ka = a, *kb = b; + + if (ka->left < kb->left) + return -1; + if (ka->left > kb->left) + return 1; + if (ka->right < kb->right) + return -1; + if (ka->right > kb->right) + return 1; + return 0; +} + static font_data *make_std_font(font_list *fontlist, char const *name) { const int *widths; + const kern_pair *kerns; int nglyphs; font_data *f; font_encoding *fe; int i; + /* XXXKERN */ widths = ps_std_font_widths(name); - if (!widths) + kerns = ps_std_font_kerns(name); + if (!widths || !kerns) return NULL; for (nglyphs = 0; ps_std_glyphs[nglyphs] != NULL; nglyphs++); - f = mknew(font_data); + f = snew(font_data); f->list = fontlist; f->name = name; f->nglyphs = nglyphs; f->glyphs = ps_std_glyphs; f->widths = widths; - f->subfont_map = mknewa(subfont_map_entry, nglyphs); + f->kerns = newtree234(kern_cmp); + for (;kerns->left != 0xFFFF; kerns++) + add234(f->kerns, (void *)kerns); + f->subfont_map = snewn(nglyphs, subfont_map_entry); /* * Our first subfont will contain all of US-ASCII. This isn't @@ -1064,23 +1329,42 @@ static font_data *make_std_font(font_list *fontlist, char const *name) return f; } +/* NB: arguments are glyph numbers from font->bmp. */ +static int find_kern(font_data *font, int lindex, int rindex) +{ + kern_pair wantkp; + kern_pair const *kp; + + if (lindex == 0xFFFF || rindex == 0xFFFF) + return 0; + wantkp.left = lindex; + wantkp.right = rindex; + kp = find234(font->kerns, &wantkp, NULL); + if (kp == NULL) + return 0; + return kp->kern; +} + static int string_width(font_data *font, wchar_t const *string, int *errs) { int width = 0; + int index, oindex; if (errs) *errs = 0; + oindex = 0xFFFF; for (; *string; string++) { - int index; + index = (*string < 0 || *string > 0xFFFF ? 0xFFFF : + font->bmp[*string]); - index = font->bmp[(unsigned short)*string]; if (index == 0xFFFF) { if (errs) *errs = 1; } else { - width += font->widths[index]; + width += find_kern(font, oindex, index) + font->widths[index]; } + oindex = index; } return width; @@ -1091,6 +1375,7 @@ static int paper_width_internal(void *vctx, word *word, int *nspaces); struct paper_width_ctx { int minspacewidth; para_data *pdata; + paper_conf *conf; }; static int paper_width_list(void *vctx, word *text, word *end, int *nspaces) { @@ -1113,6 +1398,7 @@ static int paper_width_internal(void *vctx, word *word, int *nspaces) case word_HyperEnd: case word_UpperXref: case word_LowerXref: + case word_PageXref: case word_XrefEnd: case word_IndexRef: return 0; @@ -1136,9 +1422,9 @@ static int paper_width_internal(void *vctx, word *word, int *nspaces) str = L" "; } else /* if (type == word_Quote) */ { if (word->aux == quote_Open) - str = L"\x2018"; /* FIXME: configurability! */ + str = ctx->conf->lquote; else - str = L"\x2019"; /* FIXME: configurability! */ + str = ctx->conf->rquote; } width = string_width(ctx->pdata->fonts[findex], str, &errs); @@ -1154,7 +1440,7 @@ static int paper_width(void *vctx, word *word) return paper_width_internal(vctx, word, NULL); } -static int paper_width_simple(para_data *pdata, word *text) +static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf) { struct paper_width_ctx ctx; @@ -1162,12 +1448,13 @@ static int paper_width_simple(para_data *pdata, word *text) ctx.minspacewidth = (pdata->sizes[FONT_NORMAL] * string_width(pdata->fonts[FONT_NORMAL], L" ", NULL)); + ctx.conf = conf; return paper_width_list(&ctx, text, NULL, NULL); } static void wrap_paragraph(para_data *pdata, word *words, - int w, int i1, int i2) + int w, int i1, int i2, paper_conf *conf) { wrappedline *wrapping, *p; int spacewidth; @@ -1184,7 +1471,7 @@ static void wrap_paragraph(para_data *pdata, word *words, for (i = 0; i < NFONTS; i++) if (line_height < pdata->sizes[i]) line_height = pdata->sizes[i]; - line_height *= 4096; + line_height *= UNITS_PER_PT; } spacewidth = (pdata->sizes[FONT_NORMAL] * @@ -1195,7 +1482,7 @@ static void wrap_paragraph(para_data *pdata, word *words, * comes up, but I'll make a random guess anyway and set my * space width to half the point size. */ - spacewidth = pdata->sizes[FONT_NORMAL] * 4096 / 2; + spacewidth = pdata->sizes[FONT_NORMAL] * UNITS_PER_PT / 2; } /* @@ -1204,6 +1491,7 @@ static void wrap_paragraph(para_data *pdata, word *words, */ ctx.minspacewidth = spacewidth * 3 / 5; ctx.pdata = pdata; + ctx.conf = conf; wrapping = wrap_para(words, w - i1, w - i2, paper_width, &ctx, spacewidth); @@ -1218,7 +1506,7 @@ static void wrap_paragraph(para_data *pdata, word *words, word *wd; int len, wid, spaces; - ldata = mknew(line_data); + ldata = snew(line_data); ldata->pdata = pdata; ldata->first = p->begin; @@ -1298,11 +1586,11 @@ static page_data *page_breaks(line_data *first, line_data *last, */ for (l = last; l; l = l->prev) { - l->bestcost = mknewa(int, ncols+1); - l->vshortfall = mknewa(int, ncols+1); - l->text = mknewa(int, ncols+1); - l->space = mknewa(int, ncols+1); - l->page_last = mknewa(line_data *, ncols+1); + l->bestcost = snewn(ncols+1, int); + l->vshortfall = snewn(ncols+1, int); + l->text = snewn(ncols+1, int); + l->space = snewn(ncols+1, int); + l->page_last = snewn(ncols+1, line_data *); for (n = 0; n <= ncols; n++) { int minheight, text = 0, space = 0; @@ -1354,7 +1642,8 @@ static page_data *page_breaks(line_data *first, line_data *last, */ if (m != last && m->next && !m->next->page_break) { - int x = this_height - minheight; + int x = (this_height - minheight) / FUNITS_PER_PT * + 4096.0; int xf; xf = x & 0xFF; @@ -1406,7 +1695,7 @@ static page_data *page_breaks(line_data *first, line_data *last, page_data *page; int text, space, head; - page = mknew(page_data); + page = snew(page_data); page->next = NULL; page->prev = pt; if (pt) @@ -1443,9 +1732,11 @@ static page_data *page_breaks(line_data *first, line_data *last, text += l->line_height; l->page = page; - l->ypos = text + space + head + - space * (float)page->first_line->vshortfall[n] / - page->first_line->space[n]; + l->ypos = text + space + head; + if (page->first_line->space[n]) { + l->ypos += space * (float)page->first_line->vshortfall[n] / + page->first_line->space[n]; + } if (l == page->last_line) break; @@ -1464,7 +1755,7 @@ static page_data *page_breaks(line_data *first, line_data *last, static void add_rect_to_page(page_data *page, int x, int y, int w, int h) { - rect *r = mknew(rect); + rect *r = snew(rect); r->next = NULL; if (page->last_rect) @@ -1480,11 +1771,12 @@ static void add_rect_to_page(page_data *page, int x, int y, int w, int h) } static void add_string_to_page(page_data *page, int x, int y, - font_encoding *fe, int size, char *text) + font_encoding *fe, int size, char *text, + int width) { text_fragment *frag; - frag = mknew(text_fragment); + frag = snew(text_fragment); frag->next = NULL; if (page->last_text) @@ -1498,6 +1790,7 @@ static void add_string_to_page(page_data *page, int x, int y, frag->fe = fe; frag->fontsize = size; frag->text = dupstr(text); + frag->width = width; } /* @@ -1507,17 +1800,22 @@ static int render_string(page_data *page, font_data *font, int fontsize, int x, int y, wchar_t *str) { char *text; - int textpos, textwid, glyph; + int textpos, textwid, kern, glyph, oglyph; font_encoding *subfont = NULL, *sf; - text = mknewa(char, 1 + ustrlen(str)); + text = snewn(1 + ustrlen(str), char); textpos = textwid = 0; + glyph = 0xFFFF; while (*str) { - glyph = font->bmp[*str]; + oglyph = glyph; + glyph = (*str < 0 || *str > 0xFFFF ? 0xFFFF : + font->bmp[*str]); - if (glyph == 0xFFFF) + if (glyph == 0xFFFF) { + str++; continue; /* nothing more we can do here */ + } /* * Find which subfont this character is going in. @@ -1546,15 +1844,19 @@ static int render_string(page_data *page, font_data *font, int fontsize, sf = font->latest_subfont; } - if (!subfont || sf != subfont) { + kern = find_kern(font, oglyph, glyph) * fontsize; + + if (!subfont || sf != subfont || kern) { if (subfont) { text[textpos] = '\0'; - add_string_to_page(page, x, y, subfont, fontsize, text); - x += textwid; + add_string_to_page(page, x, y, subfont, fontsize, text, + textwid); + x += textwid + kern; } else { assert(textpos == 0); } textpos = 0; + textwid = 0; subfont = sf; } @@ -1566,7 +1868,7 @@ static int render_string(page_data *page, font_data *font, int fontsize, if (textpos > 0) { text[textpos] = '\0'; - add_string_to_page(page, x, y, subfont, fontsize, text); + add_string_to_page(page, x, y, subfont, fontsize, text, textwid); x += textwid; } @@ -1579,7 +1881,7 @@ static int render_string(page_data *page, font_data *font, int fontsize, static int render_text(page_data *page, para_data *pdata, line_data *ldata, int x, int y, word *text, word *text_end, xref **xr, int shortfall, int nspaces, int *nspace, - keywordlist *keywords, indexdata *idx) + keywordlist *keywords, indexdata *idx, paper_conf *conf) { while (text && text != text_end) { int style, type, findex, errs; @@ -1593,11 +1895,16 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata, case word_HyperLink: case word_UpperXref: case word_LowerXref: + case word_PageXref: if (text->type == word_HyperLink) { dest.type = URL; - dest.url = utoa_dup(text->text); + dest.url = utoa_dup(text->text, CS_ASCII); dest.page = NULL; + } else if (text->type == word_PageXref) { + dest.type = PAGE; + dest.url = NULL; + dest.page = (page_data *)text->private_data; } else { keyword *kwl = kw_lookup(keywords, text->text); para_data *pdata; @@ -1618,7 +1925,7 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata, } } if (dest.type != NONE) { - *xr = mknew(xref); + *xr = snew(xref); (*xr)->dest = dest; /* structure copy */ if (page->last_xref) page->last_xref->next = *xr; @@ -1653,7 +1960,10 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata, * referenced by an index entry. */ case word_IndexRef: - { + /* + * We don't create index references in contents entries. + */ + if (!pdata->contents_entry) { indextag *tag; int i; @@ -1671,17 +1981,23 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata, * mention it once in the index. */ if (pi->lastpage != page) { + word **wp; + if (pi->lastword) { pi->lastword = pi->lastword->next = fake_word(L","); pi->lastword = pi->lastword->next = fake_space_word(); - pi->lastword = pi->lastword->next = - fake_word(page->number); - } else { - pi->lastword = pi->words = - fake_word(page->number); - } + wp = &pi->lastword->next; + } else + wp = &pi->words; + + pi->lastword = *wp = + fake_page_ref(page); + pi->lastword = pi->lastword->next = + fake_word(page->number); + pi->lastword = pi->lastword->next = + fake_end_ref(); } pi->lastpage = page; @@ -1710,16 +2026,17 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata, goto nextword; } else /* if (type == word_Quote) */ { if (text->aux == quote_Open) - str = L"\x2018"; /* FIXME: configurability! */ + str = conf->lquote; else - str = L"\x2019"; /* FIXME: configurability! */ + str = conf->rquote; } (void) string_width(pdata->fonts[findex], str, &errs); if (errs && text->alt) x = render_text(page, pdata, ldata, x, y, text->alt, NULL, - xr, shortfall, nspaces, nspace, keywords, idx); + xr, shortfall, nspaces, nspace, keywords, idx, + conf); else x = render_string(page, pdata->fonts[findex], pdata->sizes[findex], x, y, str); @@ -1738,7 +2055,8 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata, * Returns the last x position used on the line. */ static int render_line(line_data *ldata, int left_x, int top_y, - xref_dest *dest, keywordlist *keywords, indexdata *idx) + xref_dest *dest, keywordlist *keywords, indexdata *idx, + paper_conf *conf) { int nspace; xref *xr; @@ -1752,12 +2070,12 @@ static int render_line(line_data *ldata, int left_x, int top_y, left_x + ldata->aux_left_indent, top_y - ldata->ypos, ldata->aux_text, NULL, &xr, 0, 0, &nspace, - keywords, idx); + keywords, idx, conf); if (ldata->aux_text_2) render_text(ldata->page, ldata->pdata, ldata, x, top_y - ldata->ypos, ldata->aux_text_2, NULL, &xr, 0, 0, &nspace, - keywords, idx); + keywords, idx, conf); } nspace = 0; @@ -1767,7 +2085,7 @@ static int render_line(line_data *ldata, int left_x, int top_y, * previous line. */ if (dest->type != NONE) { - xr = mknew(xref); + xr = snew(xref); xr->next = NULL; xr->dest = *dest; /* structure copy */ if (ldata->page->last_xref) @@ -1811,7 +2129,7 @@ static int render_line(line_data *ldata, int left_x, int top_y, left_x + ldata->xpos + extra_indent, top_y - ldata->ypos, ldata->first, ldata->end, &xr, shortfall, spaces, &nspace, - keywords, idx); + keywords, idx, conf); } if (xr) { @@ -1849,7 +2167,7 @@ static void render_para(para_data *pdata, paper_conf *conf, */ if (pdata->contents_entry && ldata->page != cxref_page) { cxref_page = ldata->page; - cxref = mknew(xref); + cxref = snew(xref); cxref->next = NULL; cxref->dest.type = PAGE; if (pdata->contents_entry == index_placeholder) { @@ -1878,7 +2196,7 @@ static void render_para(para_data *pdata, paper_conf *conf, last_x = render_line(ldata, conf->left_margin, conf->paper_height - conf->top_margin, - &dest, keywords, idx); + &dest, keywords, idx, conf); if (ldata == pdata->last) break; } @@ -1902,16 +2220,9 @@ static void render_para(para_data *pdata, paper_conf *conf, } w = fake_word(num); - wid = paper_width_simple(pdata, w); + wid = paper_width_simple(pdata, w, conf); sfree(w); - render_string(pdata->last->page, - pdata->fonts[FONT_NORMAL], - pdata->sizes[FONT_NORMAL], - conf->paper_width - conf->right_margin - wid, - (conf->paper_height - conf->top_margin - - pdata->last->ypos), num); - for (x = 0; x < conf->base_width; x += conf->leader_separation) if (x - conf->leader_separation > last_x - conf->left_margin && x + conf->leader_separation < conf->base_width - wid) @@ -1921,6 +2232,13 @@ static void render_para(para_data *pdata, paper_conf *conf, conf->left_margin + x, (conf->paper_height - conf->top_margin - pdata->last->ypos), L"."); + + render_string(pdata->last->page, + pdata->fonts[FONT_NORMAL], + pdata->sizes[FONT_NORMAL], + conf->paper_width - conf->right_margin - wid, + (conf->paper_height - conf->top_margin - + pdata->last->ypos), num); } /* @@ -1953,7 +2271,7 @@ static void render_para(para_data *pdata, paper_conf *conf, static para_data *code_paragraph(int indent, word *words, paper_conf *conf) { - para_data *pdata = mknew(para_data); + para_data *pdata = snew(para_data); /* * For code paragraphs, I'm going to hack grievously and @@ -2015,12 +2333,12 @@ static para_data *code_paragraph(int indent, word *words, paper_conf *conf) * which has the same emphasis. Form it into a word * structure. */ - w = mknew(word); + w = snew(word); w->next = NULL; w->alt = NULL; w->type = (prev == 0 ? word_WeakCode : prev == 1 ? word_Emph : word_Normal); - w->text = mknewa(wchar_t, t-start+1); + w->text = snewn(t-start+1, wchar_t); memcpy(w->text, start, (t-start) * sizeof(wchar_t)); w->text[t-start] = '\0'; w->breaks = FALSE; @@ -2035,12 +2353,12 @@ static para_data *code_paragraph(int indent, word *words, paper_conf *conf) prev = -1; } - ldata = mknew(line_data); + ldata = snew(line_data); ldata->pdata = pdata; ldata->first = lhead; ldata->end = NULL; - ldata->line_height = conf->base_font_size * 4096; + ldata->line_height = conf->base_font_size * UNITS_PER_PT; ldata->xpos = indent; @@ -2070,10 +2388,10 @@ static para_data *code_paragraph(int indent, word *words, paper_conf *conf) static para_data *rule_paragraph(int indent, paper_conf *conf) { - para_data *pdata = mknew(para_data); + para_data *pdata = snew(para_data); line_data *ldata; - ldata = mknew(line_data); + ldata = snew(line_data); ldata->pdata = pdata; ldata->first = NULL; @@ -2179,7 +2497,7 @@ static wchar_t *prepare_outline_title(word *first, wchar_t *separator, static word *fake_word(wchar_t *text) { - word *ret = mknew(word); + word *ret = snew(word); ret->next = NULL; ret->alt = NULL; ret->type = word_Normal; @@ -2191,7 +2509,7 @@ static word *fake_word(wchar_t *text) static word *fake_space_word(void) { - word *ret = mknew(word); + word *ret = snew(word); ret->next = NULL; ret->alt = NULL; ret->type = word_WhiteSpace; @@ -2201,6 +2519,31 @@ static word *fake_space_word(void) return ret; } +static word *fake_page_ref(page_data *page) +{ + word *ret = snew(word); + ret->next = NULL; + ret->alt = NULL; + ret->type = word_PageXref; + ret->text = NULL; + ret->breaks = FALSE; + ret->aux = 0; + ret->private_data = page; + return ret; +} + +static word *fake_end_ref(void) +{ + word *ret = snew(word); + ret->next = NULL; + ret->alt = NULL; + ret->type = word_XrefEnd; + ret->text = NULL; + ret->breaks = FALSE; + ret->aux = 0; + return ret; +} + static word *prepare_contents_title(word *first, wchar_t *separator, word *second) {