*
* - 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
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
+#include <stdlib.h>
#include "halibut.h"
#include "paper.h"
typedef struct paper_conf_Tag paper_conf;
typedef struct paper_idx_Tag paper_idx;
+typedef struct {
+ font_data *fonts[NFONTS];
+ int font_size;
+} font_cfg;
+
struct paper_conf_Tag {
int paper_width;
int paper_height;
int chapter_underline_depth;
int chapter_underline_thickness;
int rule_thickness;
- int base_font_size;
+ font_cfg fbase, fcode, ftitle, fchapter, *fsect;
+ int nfsect;
int contents_indent_step;
int contents_margin;
int leader_separation;
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;
int index_colwidth;
- /* Fonts used in the configuration */
- font_data *tr, *ti, *hr, *hi, *cr, *co, *cb;
};
struct paper_idx_Tag {
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);
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;
return ret;
}
+static void paper_cfg_fonts(font_data **fonts, font_list *fontlist,
+ wchar_t *wp, filepos *fpos) {
+ font_data *f;
+ char *fn;
+ int i;
+
+ for (i = 0; i < NFONTS && *wp; i++, wp = uadv(wp)) {
+ fn = utoa_dup(wp, CS_ASCII);
+ f = make_std_font(fontlist, fn);
+ if (f)
+ fonts[i] = f;
+ else
+ /* FIXME: proper error */
+ error(err_nofont, fpos, wp);
+ }
+}
+
static paper_conf paper_configure(paragraph *source, font_list *fontlist) {
paragraph *p;
paper_conf ret;
/*
* Defaults.
*/
- ret.paper_width = 595 * 4096;
- ret.paper_height = 841 * 4096;
- ret.left_margin = 72 * 4096;
- ret.top_margin = 72 * 4096;
- ret.right_margin = 72 * 4096;
- ret.bottom_margin = 108 * 4096;
- ret.indent_list_bullet = 6 * 4096;
- ret.indent_list_after = 18 * 4096;
- ret.indent_quote = 18 * 4096;
- ret.base_leading = 4096;
- ret.base_para_spacing = 10 * 4096;
- ret.chapter_top_space = 72 * 4096;
- ret.sect_num_left_space = 12 * 4096;
- ret.chapter_underline_depth = 14 * 4096;
- ret.chapter_underline_thickness = 3 * 4096;
- ret.rule_thickness = 1 * 4096;
- ret.base_font_size = 12;
- ret.contents_indent_step = 24 * 4096;
- ret.contents_margin = 84 * 4096;
- ret.leader_separation = 12 * 4096;
- ret.index_gutter = 36 * 4096;
+ ret.paper_width = 595 * UNITS_PER_PT;
+ ret.paper_height = 842 * 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.fbase.font_size = 12;
+ ret.fbase.fonts[FONT_NORMAL] = make_std_font(fontlist, "Times-Roman");
+ ret.fbase.fonts[FONT_EMPH] = make_std_font(fontlist, "Times-Italic");
+ ret.fbase.fonts[FONT_CODE] = make_std_font(fontlist, "Courier");
+ ret.fcode.font_size = 12;
+ ret.fcode.fonts[FONT_NORMAL] = make_std_font(fontlist, "Courier-Bold");
+ ret.fcode.fonts[FONT_EMPH] = make_std_font(fontlist, "Courier-Oblique");
+ ret.fcode.fonts[FONT_CODE] = make_std_font(fontlist, "Courier");
+ ret.ftitle.font_size = 24;
+ ret.ftitle.fonts[FONT_NORMAL] = make_std_font(fontlist, "Helvetica-Bold");
+ ret.ftitle.fonts[FONT_EMPH] =
+ make_std_font(fontlist, "Helvetica-BoldOblique");
+ ret.ftitle.fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold");
+ ret.fchapter.font_size = 20;
+ ret.fchapter.fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold");
+ ret.fchapter.fonts[FONT_EMPH] =
+ make_std_font(fontlist, "Helvetica-BoldOblique");
+ ret.fchapter.fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold");
+ ret.nfsect = 3;
+ ret.fsect = snewn(ret.nfsect, font_cfg);
+ ret.fsect[0].font_size = 16;
+ ret.fsect[0].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold");
+ ret.fsect[0].fonts[FONT_EMPH] =
+ make_std_font(fontlist, "Helvetica-BoldOblique");
+ ret.fsect[0].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold");
+ ret.fsect[1].font_size = 14;
+ ret.fsect[1].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold");
+ ret.fsect[1].fonts[FONT_EMPH] =
+ make_std_font(fontlist, "Helvetica-BoldOblique");
+ ret.fsect[1].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold");
+ ret.fsect[2].font_size = 13;
+ ret.fsect[2].fonts[FONT_NORMAL]= make_std_font(fontlist, "Helvetica-Bold");
+ ret.fsect[2].fonts[FONT_EMPH] =
+ make_std_font(fontlist, "Helvetica-BoldOblique");
+ ret.fsect[2].fonts[FONT_CODE] = make_std_font(fontlist, "Courier-Bold");
+ 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 * 4096;
+ ret.index_minsep = 18 * UNITS_PER_PT;
ret.pagenum_fontsize = 12;
- ret.footer_distance = 32 * 4096;
+ 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";
+ ret.contents_text = L"Contents";
+ ret.index_text = L"Index";
/*
* Two-pass configuration so that we can pick up global config
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")) {
ret.paper_width =
- (int) 0.5 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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 + 4096.0 * utof(uadv(p->keyword));
+ (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));
+ ret.fbase.font_size = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-index-columns")) {
- ret.index_cols =
- utoi(uadv(p->keyword));
+ ret.index_cols = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-pagenum-font-size")) {
- ret.pagenum_fontsize =
- utoi(uadv(p->keyword));
- }
+ ret.pagenum_fontsize = utoi(uadv(p->keyword));
+ } else if (!ustricmp(p->keyword, L"paper-base-fonts")) {
+ paper_cfg_fonts(ret.fbase.fonts, fontlist, uadv(p->keyword),
+ &p->fpos);
+ } else if (!ustricmp(p->keyword, L"paper-code-font-size")) {
+ ret.fcode.font_size = utoi(uadv(p->keyword));
+ } else if (!ustricmp(p->keyword, L"paper-code-fonts")) {
+ paper_cfg_fonts(ret.fcode.fonts, fontlist, uadv(p->keyword),
+ &p->fpos);
+ } else if (!ustricmp(p->keyword, L"paper-title-font-size")) {
+ ret.ftitle.font_size = utoi(uadv(p->keyword));
+ } else if (!ustricmp(p->keyword, L"paper-title-fonts")) {
+ paper_cfg_fonts(ret.ftitle.fonts, fontlist, uadv(p->keyword),
+ &p->fpos);
+ } else if (!ustricmp(p->keyword, L"paper-chapter-font-size")) {
+ ret.fchapter.font_size = utoi(uadv(p->keyword));
+ } else if (!ustricmp(p->keyword, L"paper-chapter-fonts")) {
+ paper_cfg_fonts(ret.fchapter.fonts, fontlist, uadv(p->keyword),
+ &p->fpos);
+ } else if (!ustricmp(p->keyword, L"paper-section-font-size")) {
+ wchar_t *q = uadv(p->keyword);
+ int n = 0;
+ if (uisdigit(*q)) {
+ n = utoi(q);
+ q = uadv(q);
+ }
+ if (n >= ret.nfsect) {
+ int i;
+ ret.fsect = sresize(ret.fsect, n+1, font_cfg);
+ for (i = ret.nfsect; i <= n; i++)
+ ret.fsect[i] = ret.fsect[ret.nfsect-1];
+ ret.nfsect = n+1;
+ }
+ ret.fsect[n].font_size = utoi(q);
+ } else if (!ustricmp(p->keyword, L"paper-section-fonts")) {
+ wchar_t *q = uadv(p->keyword);
+ int n = 0;
+ if (uisdigit(*q)) {
+ n = utoi(q);
+ q = uadv(q);
+ }
+ if (n >= ret.nfsect) {
+ int i;
+ ret.fsect = sresize(ret.fsect, n+1, font_cfg);
+ for (i = ret.nfsect; i <= n; i++)
+ ret.fsect[i] = ret.fsect[ret.nfsect-1];
+ ret.nfsect = n+1;
+ }
+ paper_cfg_fonts(ret.fsect[n].fonts, fontlist, q, &p->fpos);
+ }
}
}
/ 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
/* 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))) {
+ while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote))) {
+ int n;
+ if (fonts_ok(ret.lquote,
+ ret.fbase.fonts[FONT_NORMAL],
+ ret.fbase.fonts[FONT_EMPH],
+ ret.ftitle.fonts[FONT_NORMAL],
+ ret.ftitle.fonts[FONT_EMPH],
+ ret.fchapter.fonts[FONT_NORMAL],
+ ret.fchapter.fonts[FONT_EMPH], NULL) &&
+ fonts_ok(ret.rquote,
+ ret.fbase.fonts[FONT_NORMAL],
+ ret.fbase.fonts[FONT_EMPH],
+ ret.ftitle.fonts[FONT_NORMAL],
+ ret.ftitle.fonts[FONT_EMPH],
+ ret.fchapter.fonts[FONT_NORMAL],
+ ret.fchapter.fonts[FONT_EMPH], NULL)) {
+ for (n = 0; n < ret.nfsect; n++)
+ if (!fonts_ok(ret.lquote,
+ ret.fsect[n].fonts[FONT_NORMAL],
+ ret.fsect[n].fonts[FONT_EMPH], NULL) ||
+ !fonts_ok(ret.rquote,
+ ret.fsect[n].fonts[FONT_NORMAL],
+ ret.fsect[n].fonts[FONT_EMPH], NULL))
+ break;
+ if (n == ret.nfsect)
+ break;
+ }
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))
+ !fonts_ok(ret.bullet, ret.fbase.fonts[FONT_NORMAL], NULL))
ret.bullet = uadv(ret.bullet);
return ret;
paragraph index_placeholder_para;
page_data *first_index_page;
+ init_std_fonts();
fontlist = snew(font_list);
fontlist->head = fontlist->tail = NULL;
*/
{
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);
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;
/*
* 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);
int width;
width = conf->pagenum_fontsize *
- string_width(conf->tr, page->number, NULL);
+ string_width(conf->fbase.fonts[FONT_NORMAL], page->number,
+ NULL, 0);
- render_string(page, conf->tr, conf->pagenum_fontsize,
+ 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);
}
}
return doc;
}
+static void setfont(para_data *p, font_cfg *f) {
+ int i;
+
+ for (i = 0; i < NFONTS; i++) {
+ p->fonts[i] = f->fonts[i];
+ p->sizes[i] = f->font_size;
+ }
+}
+
static para_data *make_para_data(int ptype, int paux, int indent, int rmargin,
word *pkwtext, word *pkwtext2, word *pwords,
paper_conf *conf)
pdata->rect_type = RECT_NONE;
pdata->contents_entry = NULL;
pdata->justification = JUST;
+ pdata->extraflags = 0;
/*
* Choose fonts for this paragraph.
- *
- * FIXME: All of this ought to be completely
- * user-configurable.
*/
switch (ptype) {
case para_Title:
- pdata->fonts[FONT_NORMAL] = conf->hr;
- pdata->sizes[FONT_NORMAL] = 24;
- pdata->fonts[FONT_EMPH] = conf->hi;
- pdata->sizes[FONT_EMPH] = 24;
- pdata->fonts[FONT_CODE] = conf->cb;
- pdata->sizes[FONT_CODE] = 24;
+ setfont(pdata, &conf->ftitle);
pdata->outline_level = 0;
break;
case para_Chapter:
case para_Appendix:
case para_UnnumberedChapter:
- pdata->fonts[FONT_NORMAL] = conf->hr;
- pdata->sizes[FONT_NORMAL] = 20;
- pdata->fonts[FONT_EMPH] = conf->hi;
- pdata->sizes[FONT_EMPH] = 20;
- pdata->fonts[FONT_CODE] = conf->cb;
- pdata->sizes[FONT_CODE] = 20;
+ setfont(pdata, &conf->fchapter);
pdata->outline_level = 1;
break;
case para_Heading:
case para_Subsect:
- pdata->fonts[FONT_NORMAL] = conf->hr;
- pdata->fonts[FONT_EMPH] = conf->hi;
- pdata->fonts[FONT_CODE] = conf->cb;
- pdata->sizes[FONT_NORMAL] =
- pdata->sizes[FONT_EMPH] =
- pdata->sizes[FONT_CODE] =
- (paux == 0 ? 16 : paux == 1 ? 14 : 13);
+ setfont(pdata,
+ &conf->fsect[paux >= conf->nfsect ? conf->nfsect - 1 : paux]);
pdata->outline_level = 2 + paux;
break;
case para_DescribedThing:
case para_Description:
case para_Copyright:
- pdata->fonts[FONT_NORMAL] = conf->tr;
- pdata->sizes[FONT_NORMAL] = 12;
- pdata->fonts[FONT_EMPH] = conf->ti;
- pdata->sizes[FONT_EMPH] = 12;
- pdata->fonts[FONT_CODE] = conf->cr;
- pdata->sizes[FONT_CODE] = 12;
+ setfont(pdata, &conf->fbase);
break;
}
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;
+
+ 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;
+}
+
+int lig_cmp(void *a, void *b)
+{
+ ligature const *la = a, *lb = b;
+
+ 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;
+}
+
+static int utoglyph(font_info const *fi, wchar_t u) {
+ return (u < 0 || u > 0xFFFF ? NOGLYPH : fi->bmp[u]);
+}
+
static font_data *make_std_font(font_list *fontlist, char const *name)
{
- const int *widths;
- int nglyphs;
+ font_info const *fi;
font_data *f;
font_encoding *fe;
int i;
- widths = ps_std_font_widths(name);
- if (!widths)
- return NULL;
+ for (fe = fontlist->head; fe; fe = fe->next)
+ if (strcmp(fe->font->info->name, name) == 0)
+ return fe->font;
- for (nglyphs = 0; ps_std_glyphs[nglyphs] != NULL; nglyphs++);
+ for (fi = all_fonts; fi; fi = fi->next)
+ if (strcmp(fi->name, name) == 0) break;
+ if (!fi) return NULL;
f = snew(font_data);
f->list = fontlist;
- f->name = name;
- f->nglyphs = nglyphs;
- f->glyphs = ps_std_glyphs;
- f->widths = widths;
- f->subfont_map = snewn(nglyphs, subfont_map_entry);
+ f->info = fi;
+ f->subfont_map = newtree234(sfmap_cmp);
/*
* Our first subfont will contain all of US-ASCII. This isn't
fe->free_pos = 0xA1; /* only the top half is free */
f->latest_subfont = fe;
- for (i = 0; i < (int)lenof(f->bmp); i++)
- f->bmp[i] = 0xFFFF;
-
- for (i = 0; i < nglyphs; i++) {
- wchar_t ucs;
- ucs = ps_glyph_to_unicode(f->glyphs[i]);
- assert(ucs != 0xFFFF);
- f->bmp[ucs] = i;
- if (ucs >= 0x20 && ucs <= 0x7E) {
- fe->vector[ucs] = f->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;
}
-static int string_width(font_data *font, wchar_t const *string, int *errs)
+/* 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 == NOGLYPH || rindex == NOGLYPH)
+ 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 == 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 nindex, index, oindex, lindex;
if (errs)
*errs = 0;
+ oindex = NOGLYPH;
+ index = utoglyph(font->info, *string);
for (; *string; string++) {
- int index;
+ nindex = utoglyph(font->info, string[1]);
- index = font->bmp[(unsigned short)*string];
- if (index == 0xFFFF) {
+ if (index == NOGLYPH) {
if (errs)
*errs = 1;
} else {
- width += font->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;
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:
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) {
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);
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);
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] *
- 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
* 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;
}
/*
*/
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;
* 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, glyph;
+ 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 = NOGLYPH;
+ nglyph = utoglyph(font->info, *str);
while (*str) {
- glyph = font->bmp[*str];
+ oglyph = glyph;
+ 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;
+ sme = encode_glyph(glyph, *str, font);
+ sf = sme->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);
+ kern = find_kern(font, oglyph, glyph) * fontsize;
- 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->glyphs[glyph];
- font->latest_subfont->indices[c] = glyph;
- font->latest_subfont->to_unicode[c] = *str;
-
- sf = font->latest_subfont;
- }
-
- if (!subfont || sf != subfont) {
+ if (!subfont || sf != subfont || kern) {
if (subfont) {
text[textpos] = '\0';
add_string_to_page(page, x, y, subfont, fontsize, text,
textwid);
- x += textwid;
+ x += textwid + kern;
} else {
assert(textpos == 0);
}
textpos = 0;
+ textwid = 0;
subfont = sf;
}
- text[textpos++] = font->subfont_map[glyph].position;
- textwid += font->widths[glyph] * fontsize;
+ text[textpos++] = sme->position;
+ textwid += find_width(font, glyph) * fontsize;
str++;
}
int style, type, findex, errs;
wchar_t *str;
xref_dest dest;
+ unsigned flags = 0;
switch (text->type) {
/*
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;
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,
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;
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);
}
/*
* pretend the three normal fonts are the three code paragraph
* fonts.
*/
- pdata->fonts[FONT_NORMAL] = conf->cb;
- pdata->fonts[FONT_EMPH] = conf->co;
- pdata->fonts[FONT_CODE] = conf->cr;
- pdata->sizes[FONT_NORMAL] =
- pdata->sizes[FONT_EMPH] =
- pdata->sizes[FONT_CODE] = 12;
+ 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;
+ pdata->extraflags = RS_NOLIG;
for (; words; words = words->next) {
wchar_t *t, *e, *start;
ldata->pdata = pdata;
ldata->first = lhead;
ldata->end = NULL;
- ldata->line_height = conf->base_font_size * 4096;
+ ldata->line_height = conf->fcode.font_size * UNITS_PER_PT;
ldata->xpos = indent;
pdata->rect_type = RECT_RULE;
pdata->contents_entry = NULL;
pdata->justification = LEFT;
+ pdata->extraflags = 0;
standard_line_spacing(pdata, conf);