Remove a bunch of unused variables spotted by Ubuntu 12.04's gcc.
[sgt/halibut] / bk_paper.c
index 64e44ed..e70ecf7 100644 (file)
  * 
  *  - 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
  * 
  *  - title pages
  * 
- *  - ability to import other Type 1 fonts
+ *  - ability to use Type 1 fonts without AFM files
  *     * 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
@@ -120,6 +112,7 @@ struct paper_conf_Tag {
     int pagenum_fontsize;
     int footer_distance;
     wchar_t *lquote, *rquote, *bullet;
+    wchar_t *contents_text, *index_text;
     /* These are derived from the above */
     int base_width;
     int page_height;
@@ -145,20 +138,24 @@ enum {
     word_PageXref = word_NotWordType + 1
 };
 
+/* Flags for render_string() */
+#define RS_NOLIG       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, 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);
+                        int x, int y, wchar_t *str, unsigned flags);
 static int render_line(line_data *ldata, int left_x, int top_y,
                       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 string_width(font_data *font, wchar_t const *string, int *errs);
+static int string_width(font_data *font, wchar_t const *string, int *errs,
+                       unsigned flags);
 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);
@@ -186,7 +183,7 @@ static int fonts_ok(wchar_t *string, ...)
     va_start(ap, string);
     while ( (font = va_arg(ap, font_data *)) != NULL) {
        int errs;
-       (void) string_width(font, string, &errs);
+       (void) string_width(font, string, &errs, 0);
        if (errs) {
            ret = FALSE;
            break;
@@ -283,6 +280,8 @@ static paper_conf paper_configure(paragraph *source, font_list *fontlist) {
     ret.lquote = L"\x2018\0\x2019\0'\0'\0\0";
     ret.rquote = uadv(ret.lquote);
     ret.bullet = L"\x2022\0-\0\0";
+    ret.contents_text = L"Contents";
+    ret.index_text = L"Index";
 
     /*
      * Two-pass configuration so that we can pick up global config
@@ -309,6 +308,10 @@ static paper_conf paper_configure(paragraph *source, font_list *fontlist) {
                    ret.lquote = uadv(p->keyword);
                    ret.rquote = uadv(ret.lquote);
                }
+           } else if (!ustricmp(p->keyword, L"contents")) {
+               ret.contents_text = uadv(p->keyword);
+           } else if (!ustricmp(p->keyword, L"index")) {
+               ret.index_text = uadv(p->keyword);
            } else if (!ustricmp(p->keyword, L"paper-bullet")) {
                ret.bullet = uadv(p->keyword);
            } else if (!ustricmp(p->keyword, L"paper-page-width")) {
@@ -547,7 +550,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords,
      */
     {
        word *contents_title;
-       contents_title = fake_word(L"Contents");
+       contents_title = fake_word(conf->contents_text);
 
        firstcont = make_para_data(para_UnnumberedChapter, 0, 0, 0,
                                   NULL, NULL, contents_title, conf);
@@ -615,7 +618,8 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords,
        if (has_index) {
            pdata = make_para_data(para_Normal, 0, 0,
                                   conf->contents_margin,
-                                  NULL, NULL, fake_word(L"Index"), conf);
+                                  NULL, NULL,
+                                  fake_word(conf->index_text), conf);
            pdata->next = NULL;
            pdata->contents_entry = &index_placeholder_para;
            lastcont->next = pdata;
@@ -835,7 +839,7 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords,
        /*
         * Create a set of paragraphs for the index.
         */
-       index_title = fake_word(L"Index");
+       index_title = fake_word(conf->index_text);
 
        firstidx = make_para_data(para_UnnumberedChapter, 0, 0, 0,
                                  NULL, NULL, index_title, conf);
@@ -1013,13 +1017,13 @@ void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords,
 
            width = conf->pagenum_fontsize *
                string_width(conf->fbase.fonts[FONT_NORMAL], page->number,
-                            NULL);
+                            NULL, 0);
 
            render_string(page, conf->fbase.fonts[FONT_NORMAL],
                          conf->pagenum_fontsize,
                          conf->left_margin + (conf->base_width - width)/2,
                          conf->bottom_margin - conf->footer_distance,
-                         page->number);
+                         page->number, 0);
        }
     }
 
@@ -1099,6 +1103,7 @@ static para_data *make_para_data(int ptype, int paux, int indent, int rmargin,
     pdata->rect_type = RECT_NONE;
     pdata->contents_entry = NULL;
     pdata->justification = JUST;
+    pdata->extraflags = 0;
 
     /*
      * Choose fonts for this paragraph.
@@ -1343,14 +1348,80 @@ static font_encoding *new_font_encoding(font_data *font)
     fe->free_pos = 0x21;
 
     for (i = 0; i < 256; i++) {
-       fe->vector[i] = NULL;
-       fe->indices[i] = -1;
+       fe->vector[i] = NOGLYPH;
        fe->to_unicode[i] = 0xFFFF;
     }
 
     return fe;
 }
 
+static subfont_map_entry *encode_glyph_at(glyph g, wchar_t u,
+                                         font_encoding *fe, int pos)
+{
+    subfont_map_entry *sme = snew(subfont_map_entry);
+
+    sme->subfont = fe;
+    sme->position = pos;
+    fe->vector[pos] = g;
+    fe->to_unicode[pos] = u;
+    add234(fe->font->subfont_map, sme);
+    return sme;
+}
+
+static int new_sfmap_cmp(void *a, void *b)
+{
+    glyph ga = *(glyph *)a;
+    subfont_map_entry *sb = b;
+    glyph gb = sb->subfont->vector[sb->position];
+
+    if (ga < gb) return -1;
+    if (ga > gb) return 1;
+    return 0;
+}
+
+static subfont_map_entry *encode_glyph(glyph g, wchar_t u, font_data *font)
+{
+    subfont_map_entry *sme;
+    int c;
+
+    sme = find234(font->subfont_map, &g, new_sfmap_cmp);
+    if (sme) return sme;
+
+    /*
+     * 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;
+
+    return encode_glyph_at(g, u, font->latest_subfont, c);
+}
+
+static int sfmap_cmp(void *a, void *b)
+{
+    subfont_map_entry *sa = a, *sb = b;
+    glyph ga = sa->subfont->vector[sa->position];
+    glyph gb = sb->subfont->vector[sb->position];
+
+    if (ga < gb) return -1;
+    if (ga > gb) return 1;
+    return 0;
+}
+
+int width_cmp(void *a, void *b)
+{
+    glyph_width const *wa = a, *wb = b;
+
+    if (wa->glyph < wb->glyph)
+       return -1;
+    if (wa->glyph > wb->glyph)
+       return 1;
+    return 0;
+}
+
 int kern_cmp(void *a, void *b)
 {
     kern_pair const *ka = a, *kb = b;
@@ -1366,50 +1437,35 @@ int kern_cmp(void *a, void *b)
     return 0;
 }
 
-/* This wouldn't be necessary if C had closures. */
-static font_info *glyph_cmp_fi;
-
-static int glyph_cmp(void const *a, void const *b)
+int lig_cmp(void *a, void *b)
 {
-    return strcmp(glyph_cmp_fi->glyphs[*(unsigned short *)a],
-                 glyph_cmp_fi->glyphs[*(unsigned short *)b]);
-}
+    ligature const *la = a, *lb = b;
 
-/*
- * Set up the glyphsbyname index for a font.
- */
-void font_index_glyphs(font_info *fi) {
-    int i;
+    if (la->left < lb->left)
+       return -1;
+    if (la->left > lb->left)
+       return 1;
+    if (la->right < lb->right)
+       return -1;
+    if (la->right > lb->right)
+       return 1;
+    return 0;
+}
 
-    fi->glyphsbyname = snewn(fi->nglyphs, unsigned short);
-    for (i = 0; i < fi->nglyphs; i++)
-       fi->glyphsbyname[i] = i;
-    glyph_cmp_fi = fi;
-    qsort(fi->glyphsbyname, fi->nglyphs, sizeof(fi->glyphsbyname[0]),
-         glyph_cmp);
+static int utoglyph(font_info const *fi, wchar_t u) {
+    return (u < 0 || u > 0xFFFF ? NOGLYPH : fi->bmp[u]);
 }
 
-int find_glyph(font_info *fi, char const *name) {
-    int i, j, k, r;
-
-    i = -1;
-    j = fi->nglyphs;
-    while (j-i > 1) {
-       k = (i + j) / 2;
-       r = strcmp(fi->glyphs[fi->glyphsbyname[k]], name);
-       if (r == 0)
-           return fi->glyphsbyname[k];
-       else if (r > 0)
-           j = k;
-       else
-           i = k;
-    }
-    return -1;
+void listfonts(void) {
+    font_info const *fi;
+
+    init_std_fonts();
+    for (fi = all_fonts; fi; fi = fi->next)
+       printf("%s\n", fi->name);
 }
 
 static font_data *make_std_font(font_list *fontlist, char const *name)
 {
-    int nglyphs;
     font_info const *fi;
     font_data *f;
     font_encoding *fe;
@@ -1427,8 +1483,7 @@ static font_data *make_std_font(font_list *fontlist, char const *name)
 
     f->list = fontlist;
     f->info = fi;
-    nglyphs = f->info->nglyphs;
-    f->subfont_map = snewn(nglyphs, subfont_map_entry);
+    f->subfont_map = newtree234(sfmap_cmp);
 
     /*
      * Our first subfont will contain all of US-ASCII. This isn't
@@ -1441,34 +1496,33 @@ static font_data *make_std_font(font_list *fontlist, char const *name)
     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;
-       }
+    for (i = 0x20; i <= 0x7E; i++) {
+       glyph g = utoglyph(fi, i);
+       if (g != NOGLYPH)
+           encode_glyph_at(g, i, fe, i);
     }
 
     return f;
 }
 
 /* NB: arguments are glyph numbers from font->bmp. */
+int find_width(font_data *font, glyph index)
+{
+    glyph_width wantw;
+    glyph_width const *w;
+
+    wantw.glyph = index;
+    w = find234(font->info->widths, &wantw, NULL);
+    if (!w) return 0;
+    return w->width;
+}
+
 static int find_kern(font_data *font, int lindex, int rindex)
 {
     kern_pair wantkp;
     kern_pair const *kp;
 
-    if (lindex == 0xFFFF || rindex == 0xFFFF)
+    if (lindex == NOGLYPH || rindex == NOGLYPH)
        return 0;
     wantkp.left = lindex;
     wantkp.right = rindex;
@@ -1478,27 +1532,48 @@ static int find_kern(font_data *font, int lindex, int rindex)
     return kp->kern;
 }
 
-static int string_width(font_data *font, wchar_t const *string, int *errs)
+static int find_lig(font_data *font, int lindex, int rindex)
+{
+    ligature wantlig;
+    ligature const *lig;
+
+    if (lindex == NOGLYPH || rindex == NOGLYPH)
+       return NOGLYPH;
+    wantlig.left = lindex;
+    wantlig.right = rindex;
+    lig = find234(font->info->ligs, &wantlig, NULL);
+    if (lig == NULL)
+       return NOGLYPH;
+    return lig->lig;
+}
+
+static int string_width(font_data *font, wchar_t const *string, int *errs,
+                       unsigned flags)
 {
     int width = 0;
-    int index, oindex;
+    int nindex, index, oindex, lindex;
 
     if (errs)
        *errs = 0;
 
-    oindex = 0xFFFF;
+    oindex = NOGLYPH;
+    index = utoglyph(font->info, *string);
     for (; *string; string++) {
-       index = (*string < 0 || *string > 0xFFFF ? 0xFFFF :
-                font->info->bmp[*string]);
+       nindex = utoglyph(font->info, string[1]);
 
-       if (index == 0xFFFF) {
+       if (index == NOGLYPH) {
            if (errs)
                *errs = 1;
        } else {
-           width += find_kern(font, oindex, index) +
-               font->info->widths[index];
+           if (!(flags & RS_NOLIG) &&
+               (lindex = find_lig(font, index, nindex)) != NOGLYPH) {
+               index = lindex;
+               continue;
+           }
+           width += find_kern(font, oindex, index) + find_width(font, index);
        }
        oindex = index;
+       index = nindex;
     }
 
     return width;
@@ -1526,6 +1601,7 @@ 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:
@@ -1545,6 +1621,8 @@ static int paper_width_internal(void *vctx, word *word, int *nspaces)
              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) {
@@ -1561,7 +1639,7 @@ static int paper_width_internal(void *vctx, word *word, int *nspaces)
            str = ctx->conf->rquote;
     }
 
-    width = string_width(ctx->pdata->fonts[findex], str, &errs);
+    width = string_width(ctx->pdata->fonts[findex], str, &errs, flags);
 
     if (errs && word->alt)
        return paper_width_list(vctx, word->alt, NULL, nspaces);
@@ -1581,7 +1659,7 @@ static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf)
     ctx.pdata = pdata;
     ctx.minspacewidth =
        (pdata->sizes[FONT_NORMAL] *
-        string_width(pdata->fonts[FONT_NORMAL], L" ", NULL));
+        string_width(pdata->fonts[FONT_NORMAL], L" ", NULL, 0));
     ctx.conf = conf;
 
     return paper_width_list(&ctx, text, NULL, NULL);
@@ -1609,7 +1687,7 @@ static void wrap_paragraph(para_data *pdata, word *words,
     }
 
     spacewidth = (pdata->sizes[FONT_NORMAL] *
-                 string_width(pdata->fonts[FONT_NORMAL], L" ", NULL));
+                 string_width(pdata->fonts[FONT_NORMAL], L" ", NULL, 0));
     if (spacewidth == 0) {
        /*
         * A font without a space?! Disturbing. I hope this never
@@ -1637,7 +1715,6 @@ static void wrap_paragraph(para_data *pdata, word *words,
 
     for (p = wrapping; p; p = p->next) {
        line_data *ldata;
-       word *wd;
        int len, wid, spaces;
 
        ldata = snew(line_data);
@@ -1662,7 +1739,6 @@ static void wrap_paragraph(para_data *pdata, word *words,
        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;
@@ -1931,52 +2007,40 @@ static void add_string_to_page(page_data *page, int x, int y,
  * Returns the updated x coordinate.
  */
 static int render_string(page_data *page, font_data *font, int fontsize,
-                        int x, int y, wchar_t *str)
+                        int x, int y, wchar_t *str, unsigned flags)
 {
     char *text;
-    int textpos, textwid, kern, glyph, oglyph;
+    int textpos, textwid, kern, nglyph, glyph, oglyph, lig;
     font_encoding *subfont = NULL, *sf;
+    subfont_map_entry *sme;
 
     text = snewn(1 + ustrlen(str), char);
     textpos = textwid = 0;
 
-    glyph = 0xFFFF;
+    glyph = NOGLYPH;
+    nglyph = utoglyph(font->info, *str);
     while (*str) {
        oglyph = glyph;
-       glyph = (*str < 0 || *str > 0xFFFF ? 0xFFFF :
-                font->info->bmp[*str]);
+       glyph = nglyph;
+       nglyph = utoglyph(font->info, str[1]);
 
-       if (glyph == 0xFFFF) {
+       if (glyph == NOGLYPH) {
            str++;
            continue;                  /* nothing more we can do here */
        }
 
+       if (!(flags & RS_NOLIG) &&
+           (lig = find_lig(font, glyph, nglyph)) != NOGLYPH) {
+           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;
-       }
+       sme = encode_glyph(glyph, *str, font);
+       sf = sme->subfont;
 
        kern = find_kern(font, oglyph, glyph) * fontsize;
 
@@ -1994,8 +2058,8 @@ static int render_string(page_data *page, font_data *font, int fontsize,
            subfont = sf;
        }
 
-       text[textpos++] = font->subfont_map[glyph].position;
-       textwid += font->info->widths[glyph] * fontsize;
+       text[textpos++] = sme->position;
+       textwid += find_width(font, glyph) * fontsize;
 
        str++;
     }
@@ -2021,6 +2085,7 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata,
        int style, type, findex, errs;
        wchar_t *str;
        xref_dest dest;
+       unsigned flags = 0;
 
        switch (text->type) {
            /*
@@ -2147,11 +2212,14 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata,
                  style == word_Emph ? FONT_EMPH :
                  FONT_CODE);
 
+       if (style == word_Code || style == word_WeakCode) flags |= RS_NOLIG;
+       flags |= pdata->extraflags;
+
        if (type == word_Normal) {
            str = text->text;
        } else if (type == word_WhiteSpace) {
            x += pdata->sizes[findex] *
-               string_width(pdata->fonts[findex], L" ", NULL);
+               string_width(pdata->fonts[findex], L" ", NULL, 0);
            if (nspaces && findex != FONT_CODE) {
                x += (*nspace+1) * shortfall / nspaces;
                x -= *nspace * shortfall / nspaces;
@@ -2165,7 +2233,7 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata,
                str = conf->rquote;
        }
 
-       (void) string_width(pdata->fonts[findex], str, &errs);
+       (void) string_width(pdata->fonts[findex], str, &errs, flags);
 
        if (errs && text->alt)
            x = render_text(page, pdata, ldata, x, y, text->alt, NULL,
@@ -2173,7 +2241,7 @@ static int render_text(page_data *page, para_data *pdata, line_data *ldata,
                            conf);
        else
            x = render_string(page, pdata->fonts[findex],
-                             pdata->sizes[findex], x, y, str);
+                             pdata->sizes[findex], x, y, str, flags);
 
        if (*xr)
            (*xr)->rx = x;
@@ -2365,14 +2433,14 @@ static void render_para(para_data *pdata, paper_conf *conf,
                              pdata->sizes[FONT_NORMAL],
                              conf->left_margin + x,
                              (conf->paper_height - conf->top_margin -
-                              pdata->last->ypos), L".");
+                              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);
+                      pdata->last->ypos), num, 0);
     }
 
     /*
@@ -2419,6 +2487,7 @@ static para_data *code_paragraph(int indent, word *words, paper_conf *conf)
     pdata->rect_type = RECT_NONE;
     pdata->contents_entry = NULL;
     pdata->justification = LEFT;
+    pdata->extraflags = RS_NOLIG;
 
     for (; words; words = words->next) {
        wchar_t *t, *e, *start;
@@ -2549,6 +2618,7 @@ static para_data *rule_paragraph(int indent, paper_conf *conf)
     pdata->rect_type = RECT_RULE;
     pdata->contents_entry = NULL;
     pdata->justification = LEFT;
+    pdata->extraflags = 0;
 
     standard_line_spacing(pdata, conf);