+ fe = new_font_encoding(f);
+ fe->free_pos = 0xA1; /* only the top half is free */
+ f->latest_subfont = fe;
+
+ for (i = 0; i < nglyphs; i++) {
+ wchar_t ucs;
+ ucs = ps_glyph_to_unicode(f->info->glyphs[i]);
+ if (ucs >= 0x20 && ucs <= 0x7E) {
+ fe->vector[ucs] = f->info->glyphs[i];
+ fe->indices[ucs] = i;
+ fe->to_unicode[ucs] = ucs;
+ f->subfont_map[i].subfont = fe;
+ f->subfont_map[i].position = ucs;
+ } else {
+ /*
+ * This character is not yet assigned to a subfont.
+ */
+ f->subfont_map[i].subfont = NULL;
+ f->subfont_map[i].position = 0;
+ }
+ }
+
+ 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->info->kerns, &wantkp, NULL);
+ if (kp == NULL)
+ return 0;
+ return kp->kern;
+}
+
+static int find_lig(font_data *font, int lindex, int rindex)
+{
+ ligature wantlig;
+ ligature const *lig;
+
+ if (lindex == 0xFFFF || rindex == 0xFFFF)
+ return 0xFFFF;
+ wantlig.left = lindex;
+ wantlig.right = rindex;
+ lig = find234(font->info->ligs, &wantlig, NULL);
+ if (lig == NULL)
+ return 0xFFFF;
+ return lig->lig;
+}
+
+static int utoglyph(font_info const *fi, wchar_t u) {
+ return (u < 0 || u > 0xFFFF ? 0xFFFF : fi->bmp[u]);
+}
+
+static int string_width(font_data *font, wchar_t const *string, int *errs,
+ unsigned flags)
+{
+ int width = 0;
+ int nindex, index, oindex, lindex;
+
+ if (errs)
+ *errs = 0;
+
+ oindex = 0xFFFF;
+ index = utoglyph(font->info, *string);
+ for (; *string; string++) {
+ nindex = utoglyph(font->info, string[1]);
+
+ if (index == 0xFFFF) {
+ if (errs)
+ *errs = 1;
+ } else {
+ if (!(flags & RS_NOLIG) &&
+ (lindex = find_lig(font, index, nindex)) != 0xFFFF) {
+ index = lindex;
+ continue;
+ }
+ width += find_kern(font, oindex, index) +
+ font->info->widths[index];
+ }
+ oindex = index;
+ index = nindex;
+ }
+
+ return width;
+}
+
+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) {
+ int w = 0;
+ while (text && text != end) {
+ w += paper_width_internal(vctx, text, nspaces);
+ text = text->next;
+ }
+ return w;
+}
+
+static int paper_width_internal(void *vctx, word *word, int *nspaces)
+{
+ struct paper_width_ctx *ctx = (struct paper_width_ctx *)vctx;
+ int style, type, findex, width, errs;
+ wchar_t *str;
+ unsigned flags = 0;
+
+ switch (word->type) {
+ case word_HyperLink:
+ case word_HyperEnd:
+ case word_UpperXref:
+ case word_LowerXref:
+ case word_PageXref:
+ case word_XrefEnd:
+ case word_IndexRef:
+ return 0;
+ }
+
+ style = towordstyle(word->type);
+ type = removeattr(word->type);
+
+ findex = (style == word_Normal ? FONT_NORMAL :
+ style == word_Emph ? FONT_EMPH :
+ FONT_CODE);
+
+ if (style == word_Code || style == word_WeakCode) flags |= RS_NOLIG;
+
+ if (type == word_Normal) {
+ str = word->text;
+ } else if (type == word_WhiteSpace) {
+ if (findex != FONT_CODE) {
+ if (nspaces)
+ (*nspaces)++;
+ return ctx->minspacewidth;
+ } else
+ str = L" ";
+ } else /* if (type == word_Quote) */ {
+ if (word->aux == quote_Open)
+ str = ctx->conf->lquote;
+ else
+ str = ctx->conf->rquote;
+ }
+
+ width = string_width(ctx->pdata->fonts[findex], str, &errs, flags);
+
+ if (errs && word->alt)
+ return paper_width_list(vctx, word->alt, NULL, nspaces);
+ else
+ return ctx->pdata->sizes[findex] * width;
+}
+
+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, paper_conf *conf)
+{
+ struct paper_width_ctx ctx;
+
+ ctx.pdata = pdata;
+ ctx.minspacewidth =
+ (pdata->sizes[FONT_NORMAL] *
+ string_width(pdata->fonts[FONT_NORMAL], L" ", NULL, 0));
+ 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, paper_conf *conf)
+{
+ wrappedline *wrapping, *p;
+ int spacewidth;
+ struct paper_width_ctx ctx;
+ int line_height;
+
+ /*
+ * We're going to need to store the line height in every line
+ * structure we generate.
+ */
+ {
+ int i;
+ line_height = 0;
+ for (i = 0; i < NFONTS; i++)
+ if (line_height < pdata->sizes[i])
+ line_height = pdata->sizes[i];
+ line_height *= UNITS_PER_PT;
+ }
+
+ spacewidth = (pdata->sizes[FONT_NORMAL] *
+ string_width(pdata->fonts[FONT_NORMAL], L" ", NULL, 0));
+ if (spacewidth == 0) {
+ /*
+ * A font without a space?! Disturbing. I hope this never
+ * 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] * UNITS_PER_PT / 2;
+ }
+
+ /*
+ * I'm going to set the _minimum_ space width to 3/5 of the
+ * standard one, and use the standard one as the optimum.
+ */
+ ctx.minspacewidth = spacewidth * 3 / 5;
+ ctx.pdata = pdata;
+ ctx.conf = conf;
+
+ wrapping = wrap_para(words, w - i1, w - i2, paper_width, &ctx, spacewidth);
+
+ /*
+ * Having done the wrapping, we now concoct a set of line_data
+ * structures.
+ */
+ pdata->first = pdata->last = NULL;
+
+ for (p = wrapping; p; p = p->next) {
+ line_data *ldata;
+ word *wd;
+ int len, wid, spaces;
+
+ ldata = snew(line_data);
+
+ ldata->pdata = pdata;
+ ldata->first = p->begin;
+ ldata->end = p->end;
+ ldata->line_height = line_height;
+
+ ldata->xpos = (p == wrapping ? i1 : i2);
+
+ if (pdata->last) {
+ pdata->last->next = ldata;
+ ldata->prev = pdata->last;
+ } else {
+ pdata->first = ldata;
+ ldata->prev = NULL;
+ }
+ ldata->next = NULL;
+ pdata->last = ldata;
+
+ spaces = 0;
+ len = paper_width_list(&ctx, ldata->first, ldata->end, &spaces);
+ wid = (p == wrapping ? w - i1 : w - i2);
+ wd = ldata->first;
+
+ ldata->hshortfall = wid - len;
+ ldata->nspaces = spaces;
+ /*
+ * This tells us how much the space width needs to
+ * change from _min_spacewidth. But we want to store
+ * its difference from the _natural_ space width, to
+ * make the text rendering easier.
+ */
+ ldata->hshortfall += ctx.minspacewidth * spaces;
+ ldata->hshortfall -= spacewidth * spaces;
+ ldata->real_shortfall = ldata->hshortfall;
+ /*
+ * Special case: on the last line of a paragraph, we
+ * never stretch spaces.
+ */
+ if (ldata->hshortfall > 0 && !p->next)
+ ldata->hshortfall = 0;
+
+ ldata->aux_text = NULL;
+ ldata->aux_text_2 = NULL;
+ ldata->aux_left_indent = 0;
+ ldata->penalty_before = ldata->penalty_after = 0;
+ }
+
+}
+
+static page_data *page_breaks(line_data *first, line_data *last,
+ int page_height, int ncols, int headspace)
+{
+ line_data *l, *m;
+ page_data *ph, *pt;
+ int n, n1, this_height;
+
+ /*
+ * Page breaking is done by a close analogue of the optimal
+ * paragraph wrapping algorithm used by wrap_para(). We work
+ * backwards from the end of the document line by line; for
+ * each line, we contemplate every possible number of lines we
+ * could put on a page starting with that line, determine a
+ * cost function for each one, add it to the pre-computed cost
+ * function for optimally page-breaking everything after that
+ * page, and pick the best option.
+ *
+ * This is made slightly more complex by the fact that we have
+ * a multi-column index with a heading at the top of the
+ * _first_ page, meaning that the first _ncols_ pages must have
+ * a different length. Hence, we must do the wrapping ncols+1
+ * times over, hypothetically trying to put every subsequence
+ * on every possible page.
+ *
+ * Since my line_data structures are only used for this
+ * purpose, I might as well just store the algorithm data
+ * directly in them.
+ */
+
+ for (l = last; l; l = l->prev) {
+ 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;
+ int cost;
+
+ n1 = (n < ncols ? n+1 : ncols);
+ if (n < ncols)
+ this_height = page_height - headspace;
+ else
+ this_height = page_height;
+
+ l->bestcost[n] = -1;
+ for (m = l; m; m = m->next) {
+ if (m != l && m->page_break)
+ break; /* we've gone as far as we can */
+
+ if (m != l) {
+ if (m->prev->space_after > 0)
+ space += m->prev->space_after;
+ else
+ text += m->prev->space_after;
+ }
+ if (m != l || m->page_break) {
+ if (m->space_before > 0)
+ space += m->space_before;
+ else
+ text += m->space_before;
+ }
+ text += m->line_height;
+ minheight = text + space;
+
+ if (m != l && minheight > this_height)
+ break;
+
+ /*
+ * If the space after this paragraph is _negative_
+ * (which means the next line is folded on to this
+ * one, which happens in the index), we absolutely
+ * cannot break here.
+ */
+ if (m->space_after >= 0) {
+
+ /*
+ * Compute the cost of this arrangement, as the
+ * square of the amount of wasted space on the
+ * page. Exception: if this is the last page
+ * before a mandatory break or the document
+ * end, we don't penalise a large blank area.
+ */
+ if (m != last && m->next && !m->next->page_break)
+ {
+ int x = (this_height - minheight) / FUNITS_PER_PT *
+ 4096.0;
+ int xf;
+
+ xf = x & 0xFF;
+ x >>= 8;
+
+ cost = x*x;
+ cost += (x * xf) >> 8;
+ } else
+ cost = 0;
+
+ if (m != last && m->next && !m->next->page_break) {
+ cost += m->penalty_after;
+ cost += m->next->penalty_before;
+ }
+
+ if (m != last && m->next && !m->next->page_break)
+ cost += m->next->bestcost[n1];
+ if (l->bestcost[n] == -1 || l->bestcost[n] > cost) {
+ /*
+ * This is the best option yet for this
+ * starting point.
+ */
+ l->bestcost[n] = cost;
+ if (m != last && m->next && !m->next->page_break)
+ l->vshortfall[n] = this_height - minheight;
+ else
+ l->vshortfall[n] = 0;
+ l->text[n] = text;
+ l->space[n] = space;
+ l->page_last[n] = m;
+ }
+ }
+
+ if (m == last)
+ break;
+ }
+ }
+ }
+
+ /*
+ * Now go through the line list forwards and assemble the
+ * actual pages.
+ */
+ ph = pt = NULL;
+
+ l = first;
+ n = 0;
+ while (l) {
+ page_data *page;
+ int text, space, head;
+
+ page = snew(page_data);
+ page->next = NULL;
+ page->prev = pt;
+ if (pt)
+ pt->next = page;
+ else
+ ph = page;
+ pt = page;
+
+ page->first_line = l;
+ page->last_line = l->page_last[n];
+
+ page->first_text = page->last_text = NULL;
+ page->first_xref = page->last_xref = NULL;
+ page->first_rect = page->last_rect = NULL;
+
+ /*
+ * Now assign a y-coordinate to each line on the page.
+ */
+ text = space = 0;
+ head = (n < ncols ? headspace : 0);
+ for (l = page->first_line; l; l = l->next) {
+ if (l != page->first_line) {
+ if (l->prev->space_after > 0)
+ space += l->prev->space_after;
+ else
+ text += l->prev->space_after;
+ }
+ if (l != page->first_line || l->page_break) {
+ if (l->space_before > 0)
+ space += l->space_before;
+ else
+ text += l->space_before;
+ }
+ text += l->line_height;
+
+ l->page = page;
+ 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;
+ }
+
+ l = page->last_line;
+ if (l == last)
+ break;
+ l = l->next;
+
+ n = (n < ncols ? n+1 : ncols);
+ }
+
+ return ph;
+}
+
+static void add_rect_to_page(page_data *page, int x, int y, int w, int h)
+{
+ rect *r = snew(rect);
+
+ r->next = NULL;
+ if (page->last_rect)
+ page->last_rect->next = r;
+ else
+ page->first_rect = r;
+ page->last_rect = r;
+
+ r->x = x;
+ r->y = y;
+ r->w = w;
+ r->h = h;
+}
+
+static void add_string_to_page(page_data *page, int x, int y,
+ font_encoding *fe, int size, char *text,
+ int width)
+{
+ text_fragment *frag;
+
+ frag = snew(text_fragment);
+ frag->next = NULL;
+
+ if (page->last_text)
+ page->last_text->next = frag;
+ else
+ page->first_text = frag;
+ page->last_text = frag;
+
+ frag->x = x;
+ frag->y = y;
+ frag->fe = fe;
+ frag->fontsize = size;
+ frag->text = dupstr(text);
+ frag->width = width;
+}
+
+/*
+ * Returns the updated x coordinate.
+ */
+static int render_string(page_data *page, font_data *font, int fontsize,
+ int x, int y, wchar_t *str, unsigned flags)
+{
+ char *text;
+ int textpos, textwid, kern, nglyph, glyph, oglyph, lig;
+ font_encoding *subfont = NULL, *sf;
+
+ text = snewn(1 + ustrlen(str), char);
+ textpos = textwid = 0;
+
+ glyph = 0xFFFF;
+ nglyph = utoglyph(font->info, *str);
+ while (*str) {
+ oglyph = glyph;
+ glyph = nglyph;
+ nglyph = utoglyph(font->info, str[1]);
+
+ if (glyph == 0xFFFF) {
+ str++;
+ continue; /* nothing more we can do here */
+ }
+
+ if (!(flags & RS_NOLIG) &&
+ (lig = find_lig(font, glyph, nglyph)) != 0xFFFF) {
+ nglyph = lig;
+ str++;
+ continue;
+ }
+
+ /*
+ * Find which subfont this character is going in.
+ */
+ sf = font->subfont_map[glyph].subfont;
+
+ if (!sf) {
+ int c;
+
+ /*
+ * This character is not yet in a subfont. Assign one.
+ */
+ if (font->latest_subfont->free_pos >= 0x100)
+ font->latest_subfont = new_font_encoding(font);
+
+ c = font->latest_subfont->free_pos++;
+ if (font->latest_subfont->free_pos == 0x7F)
+ font->latest_subfont->free_pos = 0xA1;
+
+ font->subfont_map[glyph].subfont = font->latest_subfont;
+ font->subfont_map[glyph].position = c;
+ font->latest_subfont->vector[c] = font->info->glyphs[glyph];
+ font->latest_subfont->indices[c] = glyph;
+ font->latest_subfont->to_unicode[c] = *str;
+
+ sf = font->latest_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,
+ textwid);
+ x += textwid + kern;
+ } else {
+ assert(textpos == 0);
+ }
+ textpos = 0;
+ textwid = 0;
+ subfont = sf;
+ }
+
+ text[textpos++] = font->subfont_map[glyph].position;
+ textwid += font->info->widths[glyph] * fontsize;
+
+ str++;
+ }
+
+ if (textpos > 0) {
+ text[textpos] = '\0';
+ add_string_to_page(page, x, y, subfont, fontsize, text, textwid);
+ x += textwid;
+ }
+
+ return x;
+}
+
+/*
+ * Returns the updated x coordinate.
+ */
+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, paper_conf *conf)
+{
+ while (text && text != text_end) {
+ int style, type, findex, errs;
+ wchar_t *str;
+ xref_dest dest;
+ unsigned flags = 0;
+
+ switch (text->type) {
+ /*
+ * Start a cross-reference.
+ */
+ 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, 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;
+
+ if (kwl) {
+ assert(kwl->para->private_data);
+ pdata = (para_data *) kwl->para->private_data;
+ dest.type = PAGE;
+ dest.page = pdata->first->page;
+ dest.url = NULL;
+ } else {
+ /*
+ * Shouldn't happen, but *shrug*
+ */
+ dest.type = NONE;
+ dest.page = NULL;
+ dest.url = NULL;
+ }
+ }
+ if (dest.type != NONE) {
+ *xr = snew(xref);
+ (*xr)->dest = dest; /* structure copy */
+ if (page->last_xref)
+ page->last_xref->next = *xr;
+ else
+ page->first_xref = *xr;
+ page->last_xref = *xr;
+ (*xr)->next = NULL;
+
+ /*
+ * FIXME: Ideally we should have, and use, some
+ * vertical font metric information here so that
+ * our cross-ref rectangle can take account of
+ * descenders and the font's cap height. This will
+ * do for the moment, but it isn't ideal.
+ */
+ (*xr)->lx = (*xr)->rx = x;
+ (*xr)->by = y;
+ (*xr)->ty = y + ldata->line_height;
+ }
+ goto nextword;
+
+ /*
+ * Finish extending a cross-reference box.
+ */
+ case word_HyperEnd:
+ case word_XrefEnd:
+ *xr = NULL;
+ goto nextword;
+
+ /*
+ * Add the current page number to the list of pages
+ * 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;
+
+ tag = index_findtag(idx, text->text);
+ if (!tag)
+ goto nextword;
+
+ for (i = 0; i < tag->nrefs; i++) {
+ indexentry *entry = tag->refs[i];
+ paper_idx *pi = (paper_idx *)entry->backend_data;
+
+ /*
+ * If the same index term is indexed twice
+ * within the same section, we only want to
+ * 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();
+ 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;
+ }
+ }
+ goto nextword;
+ }
+
+ style = towordstyle(text->type);
+ type = removeattr(text->type);
+
+ findex = (style == word_Normal ? FONT_NORMAL :
+ style == word_Emph ? FONT_EMPH :
+ FONT_CODE);
+
+ if (style == word_Code || style == word_WeakCode) flags |= RS_NOLIG;
+
+ if (type == word_Normal) {
+ str = text->text;
+ } else if (type == word_WhiteSpace) {
+ x += pdata->sizes[findex] *
+ string_width(pdata->fonts[findex], L" ", NULL, 0);
+ if (nspaces && findex != FONT_CODE) {
+ x += (*nspace+1) * shortfall / nspaces;
+ x -= *nspace * shortfall / nspaces;
+ (*nspace)++;
+ }
+ goto nextword;
+ } else /* if (type == word_Quote) */ {
+ if (text->aux == quote_Open)
+ str = conf->lquote;
+ else
+ str = conf->rquote;
+ }
+
+ (void) string_width(pdata->fonts[findex], str, &errs, flags);
+
+ if (errs && text->alt)
+ x = render_text(page, pdata, ldata, x, y, text->alt, NULL,
+ xr, shortfall, nspaces, nspace, keywords, idx,
+ conf);
+ else
+ x = render_string(page, pdata->fonts[findex],
+ pdata->sizes[findex], x, y, str, flags);
+
+ if (*xr)
+ (*xr)->rx = x;
+
+ nextword:
+ text = text->next;
+ }
+
+ return x;
+}
+
+/*
+ * 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,
+ paper_conf *conf)
+{
+ int nspace;
+ xref *xr;
+ int ret = 0;
+
+ if (ldata->aux_text) {
+ int x;
+ xr = NULL;
+ nspace = 0;
+ x = render_text(ldata->page, ldata->pdata, ldata,
+ left_x + ldata->aux_left_indent,
+ top_y - ldata->ypos,
+ ldata->aux_text, NULL, &xr, 0, 0, &nspace,
+ 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, conf);
+ }
+ nspace = 0;
+
+ if (ldata->first) {
+ /*
+ * There might be a cross-reference carried over from a
+ * previous line.
+ */
+ if (dest->type != NONE) {
+ xr = snew(xref);
+ xr->next = NULL;
+ xr->dest = *dest; /* structure copy */
+ if (ldata->page->last_xref)
+ ldata->page->last_xref->next = xr;
+ else
+ ldata->page->first_xref = xr;
+ ldata->page->last_xref = xr;
+ xr->lx = xr->rx = left_x + ldata->xpos;
+ xr->by = top_y - ldata->ypos;
+ xr->ty = top_y - ldata->ypos + ldata->line_height;
+ } else
+ xr = NULL;
+
+ {
+ int extra_indent, shortfall, spaces;
+ int just = ldata->pdata->justification;
+
+ /*
+ * All forms of justification become JUST when we have
+ * to squeeze the paragraph.
+ */
+ if (ldata->hshortfall < 0)
+ just = JUST;
+
+ switch (just) {
+ case JUST:
+ shortfall = ldata->hshortfall;
+ spaces = ldata->nspaces;
+ extra_indent = 0;
+ break;
+ case LEFT:
+ shortfall = spaces = extra_indent = 0;
+ break;
+ case RIGHT:
+ shortfall = spaces = 0;
+ extra_indent = ldata->real_shortfall;
+ break;
+ }
+
+ ret = render_text(ldata->page, ldata->pdata, ldata,
+ left_x + ldata->xpos + extra_indent,
+ top_y - ldata->ypos, ldata->first, ldata->end,
+ &xr, shortfall, spaces, &nspace,
+ keywords, idx, conf);
+ }
+
+ if (xr) {
+ /*
+ * There's a cross-reference continued on to the next line.
+ */
+ *dest = xr->dest;
+ } else
+ dest->type = NONE;
+ }
+
+ return ret;
+}
+
+static void render_para(para_data *pdata, paper_conf *conf,
+ keywordlist *keywords, indexdata *idx,
+ paragraph *index_placeholder, page_data *index_page)
+{
+ int last_x;
+ xref *cxref;
+ page_data *cxref_page;
+ xref_dest dest;
+ para_data *target;
+ line_data *ldata;
+
+ dest.type = NONE;
+ cxref = NULL;
+ cxref_page = NULL;
+
+ for (ldata = pdata->first; ldata; ldata = ldata->next) {
+ /*
+ * If this is a contents entry, we expect to have a single
+ * enormous cross-reference rectangle covering the whole
+ * thing. (Unless, of course, it spans multiple pages.)
+ */
+ if (pdata->contents_entry && ldata->page != cxref_page) {
+ cxref_page = ldata->page;
+ cxref = snew(xref);
+ cxref->next = NULL;
+ cxref->dest.type = PAGE;
+ if (pdata->contents_entry == index_placeholder) {
+ cxref->dest.page = index_page;
+ } else {
+ assert(pdata->contents_entry->private_data);
+ target = (para_data *)pdata->contents_entry->private_data;
+ cxref->dest.page = target->first->page;
+ }
+ cxref->dest.url = NULL;
+ if (ldata->page->last_xref)
+ ldata->page->last_xref->next = cxref;
+ else
+ ldata->page->first_xref = cxref;
+ ldata->page->last_xref = cxref;
+ cxref->lx = conf->left_margin;
+ cxref->rx = conf->paper_width - conf->right_margin;
+ cxref->ty = conf->paper_height - conf->top_margin
+ - ldata->ypos + ldata->line_height;
+ }
+ if (pdata->contents_entry) {
+ assert(cxref != NULL);
+ cxref->by = conf->paper_height - conf->top_margin
+ - ldata->ypos;
+ }
+
+ last_x = render_line(ldata, conf->left_margin,
+ conf->paper_height - conf->top_margin,
+ &dest, keywords, idx, conf);
+ if (ldata == pdata->last)
+ break;
+ }
+
+ /*
+ * If this is a contents entry, add leaders and a page
+ * number.
+ */
+ if (pdata->contents_entry) {
+ word *w;
+ wchar_t *num;
+ int wid;
+ int x;
+
+ if (pdata->contents_entry == index_placeholder) {
+ num = index_page->number;
+ } else {
+ assert(pdata->contents_entry->private_data);
+ target = (para_data *)pdata->contents_entry->private_data;
+ num = target->first->page->number;
+ }
+
+ w = fake_word(num);
+ wid = paper_width_simple(pdata, w, conf);
+ sfree(w);
+
+ 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)
+ render_string(pdata->last->page,
+ pdata->fonts[FONT_NORMAL],
+ pdata->sizes[FONT_NORMAL],
+ conf->left_margin + x,
+ (conf->paper_height - conf->top_margin -
+ pdata->last->ypos), L".", 0);
+
+ 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, 0);
+ }
+
+ /*
+ * Render any rectangle (chapter title underline or rule)
+ * that goes with this paragraph.
+ */
+ switch (pdata->rect_type) {
+ case RECT_CHAPTER_UNDERLINE:
+ add_rect_to_page(pdata->last->page,
+ conf->left_margin,
+ (conf->paper_height - conf->top_margin -
+ pdata->last->ypos -
+ conf->chapter_underline_depth),
+ conf->base_width,
+ conf->chapter_underline_thickness);
+ break;
+ case RECT_RULE:
+ add_rect_to_page(pdata->first->page,
+ conf->left_margin + pdata->first->xpos,
+ (conf->paper_height - conf->top_margin -
+ pdata->last->ypos -
+ pdata->last->line_height),
+ conf->base_width - pdata->first->xpos,
+ pdata->last->line_height);
+ break;
+ default: /* placate gcc */
+ break;
+ }
+}
+
+static para_data *code_paragraph(int indent, word *words, paper_conf *conf)
+{
+ para_data *pdata = snew(para_data);
+
+ /*
+ * For code paragraphs, I'm going to hack grievously and
+ * pretend the three normal fonts are the three code paragraph
+ * fonts.
+ */
+ setfont(pdata, &conf->fcode);
+
+ pdata->first = pdata->last = NULL;
+ pdata->outline_level = -1;
+ pdata->rect_type = RECT_NONE;
+ pdata->contents_entry = NULL;
+ pdata->justification = LEFT;
+
+ for (; words; words = words->next) {
+ wchar_t *t, *e, *start;
+ word *lhead = NULL, *ltail = NULL, *w;
+ line_data *ldata;
+ int prev = -1, curr;
+
+ t = words->text;
+ if (words->next && words->next->type == word_Emph) {
+ e = words->next->text;
+ words = words->next;
+ } else
+ e = NULL;
+
+ start = t;
+
+ while (*start) {
+ while (*t) {
+ if (!e || !*e)
+ curr = 0;
+ else if (*e == L'i')
+ curr = 1;
+ else if (*e == L'b')
+ curr = 2;
+ else
+ curr = 0;
+
+ if (prev < 0)
+ prev = curr;
+
+ if (curr != prev)
+ break;
+
+ t++;
+ if (e && *e)
+ e++;
+ }
+
+ /*
+ * We've isolated a maximal subsequence of the line
+ * which has the same emphasis. Form it into a word
+ * structure.
+ */
+ w = snew(word);
+ w->next = NULL;
+ w->alt = NULL;
+ w->type = (prev == 0 ? word_WeakCode :
+ prev == 1 ? word_Emph : word_Normal);
+ 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;
+
+ if (ltail)
+ ltail->next = w;
+ else
+ lhead = w;
+ ltail = w;
+
+ start = t;
+ prev = -1;
+ }
+
+ ldata = snew(line_data);
+
+ ldata->pdata = pdata;
+ ldata->first = lhead;
+ ldata->end = NULL;
+ ldata->line_height = conf->fcode.font_size * UNITS_PER_PT;
+
+ ldata->xpos = indent;
+
+ if (pdata->last) {
+ pdata->last->next = ldata;
+ ldata->prev = pdata->last;
+ } else {
+ pdata->first = ldata;
+ ldata->prev = NULL;
+ }
+ ldata->next = NULL;
+ pdata->last = ldata;
+
+ ldata->hshortfall = 0;
+ ldata->nspaces = 0;
+ ldata->aux_text = NULL;
+ ldata->aux_text_2 = NULL;
+ ldata->aux_left_indent = 0;
+ /* General opprobrium for breaking in a code paragraph. */
+ ldata->penalty_before = ldata->penalty_after = 50000;
+ }
+
+ standard_line_spacing(pdata, conf);
+
+ return pdata;
+}
+
+static para_data *rule_paragraph(int indent, paper_conf *conf)
+{
+ para_data *pdata = snew(para_data);
+ line_data *ldata;
+
+ ldata = snew(line_data);
+
+ ldata->pdata = pdata;
+ ldata->first = NULL;
+ ldata->end = NULL;
+ ldata->line_height = conf->rule_thickness;
+
+ ldata->xpos = indent;
+
+ ldata->prev = NULL;
+ ldata->next = NULL;
+
+ ldata->hshortfall = 0;
+ ldata->nspaces = 0;
+ ldata->aux_text = NULL;
+ ldata->aux_text_2 = NULL;
+ ldata->aux_left_indent = 0;
+
+ /*
+ * Better to break after a rule than before it
+ */
+ ldata->penalty_after += 100000;
+ ldata->penalty_before += -100000;
+
+ pdata->first = pdata->last = ldata;
+ pdata->outline_level = -1;
+ pdata->rect_type = RECT_RULE;
+ pdata->contents_entry = NULL;
+ pdata->justification = LEFT;
+
+ standard_line_spacing(pdata, conf);
+
+ return pdata;
+}
+
+/*
+ * Plain-text-like formatting for outline titles.
+ */
+static void paper_rdaddw(rdstring *rs, word *text) {
+ for (; text; text = text->next) switch (text->type) {
+ case word_HyperLink:
+ case word_HyperEnd:
+ case word_UpperXref:
+ case word_LowerXref:
+ case word_XrefEnd:
+ case word_IndexRef:
+ break;
+
+ case word_Normal:
+ case word_Emph:
+ case word_Code:
+ case word_WeakCode:
+ case word_WhiteSpace:
+ case word_EmphSpace:
+ case word_CodeSpace:
+ case word_WkCodeSpace:
+ case word_Quote:
+ case word_EmphQuote:
+ case word_CodeQuote:
+ case word_WkCodeQuote:
+ assert(text->type != word_CodeQuote &&
+ text->type != word_WkCodeQuote);
+ if (towordstyle(text->type) == word_Emph &&
+ (attraux(text->aux) == attr_First ||
+ attraux(text->aux) == attr_Only))
+ rdadd(rs, L'_'); /* FIXME: configurability */
+ else if (towordstyle(text->type) == word_Code &&
+ (attraux(text->aux) == attr_First ||
+ attraux(text->aux) == attr_Only))
+ rdadd(rs, L'\''); /* FIXME: configurability */
+ if (removeattr(text->type) == word_Normal) {
+ rdadds(rs, text->text);
+ } else if (removeattr(text->type) == word_WhiteSpace) {
+ rdadd(rs, L' ');
+ } else if (removeattr(text->type) == word_Quote) {
+ rdadd(rs, L'\''); /* fixme: configurability */
+ }
+ if (towordstyle(text->type) == word_Emph &&
+ (attraux(text->aux) == attr_Last ||
+ attraux(text->aux) == attr_Only))
+ rdadd(rs, L'_'); /* FIXME: configurability */
+ else if (towordstyle(text->type) == word_Code &&
+ (attraux(text->aux) == attr_Last ||
+ attraux(text->aux) == attr_Only))
+ rdadd(rs, L'\''); /* FIXME: configurability */
+ break;
+ }
+}
+
+static wchar_t *prepare_outline_title(word *first, wchar_t *separator,
+ word *second)
+{
+ rdstring rs = {0, 0, NULL};
+
+ if (first)
+ paper_rdaddw(&rs, first);
+ if (separator)
+ rdadds(&rs, separator);
+ if (second)
+ paper_rdaddw(&rs, second);
+
+ return rs.text;
+}
+
+static word *fake_word(wchar_t *text)
+{
+ word *ret = snew(word);
+ ret->next = NULL;
+ ret->alt = NULL;
+ ret->type = word_Normal;
+ ret->text = ustrdup(text);
+ ret->breaks = FALSE;
+ ret->aux = 0;
+ return ret;
+}
+
+static word *fake_space_word(void)
+{
+ word *ret = snew(word);
+ ret->next = NULL;
+ ret->alt = NULL;
+ ret->type = word_WhiteSpace;
+ ret->text = NULL;
+ ret->breaks = TRUE;
+ ret->aux = 0;
+ 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)
+{
+ word *ret;
+ word **wptr, *w;
+
+ wptr = &ret;
+
+ if (first) {
+ w = dup_word_list(first);
+ *wptr = w;
+ while (w->next)
+ w = w->next;
+ wptr = &w->next;
+ }
+
+ if (separator) {
+ w = fake_word(separator);
+ *wptr = w;
+ wptr = &w->next;
+ }
+
+ if (second) {
+ *wptr = dup_word_list(second);
+ }
+
+ return ret;
+}
+
+static void fold_into_page(page_data *dest, page_data *src, int right_shift)
+{
+ line_data *ldata;
+
+ if (!src->first_line)
+ return;
+
+ if (dest->last_line) {
+ dest->last_line->next = src->first_line;
+ src->first_line->prev = dest->last_line;
+ }
+ dest->last_line = src->last_line;
+
+ for (ldata = src->first_line; ldata; ldata = ldata->next) {
+ ldata->page = dest;
+ ldata->xpos += right_shift;
+
+ if (ldata == src->last_line)
+ break;
+ }