static void objstream(object *o, char const *text);
static void pdf_string(void (*add)(object *, char const *),
object *, char const *);
+static void pdf_string_len(void (*add)(object *, char const *),
+ object *, char const *, int);
static void objref(object *o, object *dest);
static void make_pages_node(object *node, object *parent, page_data *first,
filename = dupstr("output.pdf");
for (p = sourceform; p; p = p->next) {
- if (p->type == para_Config && p->parent) {
+ if (p->type == para_Config) {
if (!ustricmp(p->keyword, L"pdf-filename")) {
sfree(filename);
filename = dupstr(adv(p->origkeyword));
olist.number = 1;
cat = new_object(&olist);
- outlines = new_object(&olist);
+ if (doc->n_outline_elements > 0)
+ outlines = new_object(&olist);
+ else
+ outlines = NULL;
pages = new_object(&olist);
resources = new_object(&olist);
* The catalogue just contains references to the outlines and
* pages objects.
*/
- objtext(cat, "<<\n/Type /Catalog\n/Outlines ");
- objref(cat, outlines);
+ objtext(cat, "<<\n/Type /Catalog");
+ if (outlines) {
+ objtext(cat, "\n/Outlines ");
+ objref(cat, outlines);
+ }
objtext(cat, "\n/Pages ");
objref(cat, pages);
- objtext(cat, "\n/PageMode /UseOutlines\n>>\n");
+ if (outlines)
+ objtext(cat, "\n/PageMode /UseOutlines");
+ objtext(cat, "\n>>\n");
/*
* Set up the resources dictionary, which mostly means
width = 0.0;
else
width = fe->font->widths[fe->indices[i]];
- sprintf(buf, "%g\n", 1000.0 * width / 4096.0);
+ sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT);
objtext(widths, buf);
}
objtext(widths, "]\n");
* So we don't need a /Resources entry here.
*/
sprintf(buf, "/MediaBox [0 0 %g %g]\n",
- doc->paper_width / 4096.0, doc->paper_height / 4096.0);
+ doc->paper_width / FUNITS_PER_PT,
+ doc->paper_height / FUNITS_PER_PT);
objtext(opage, buf);
/*
*/
for (r = page->first_rect; r; r = r->next) {
char buf[512];
- sprintf(buf, "%g %g %g %g re f\n", r->x / 4096.0,
- r->y / 4096.0, r->w / 4096.0, r->h / 4096.0);
+ sprintf(buf, "%g %g %g %g re f\n",
+ r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
+ r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT);
objstream(cstr, buf);
}
*/
if (lx < 0) {
sprintf(buf, "1 0 0 1 %g %g Tm ",
- frag->x/4096.0, frag->y/4096.0);
+ frag->x/FUNITS_PER_PT, frag->y/FUNITS_PER_PT);
} else {
sprintf(buf, "%g %g Td ",
- (frag->x - lx)/4096.0, (frag->y - ly)/4096.0);
+ (frag->x - lx)/FUNITS_PER_PT,
+ (frag->y - ly)/FUNITS_PER_PT);
}
objstream(cstr, buf);
lx = x = frag->x;
if (frag->x != x) {
sprintf(buf, "%g",
(x - frag->x) * 1000.0 /
- (4096.0 * frag->fontsize));
+ (FUNITS_PER_PT * frag->fontsize));
objstream(cstr, buf);
}
pdf_string(objstream, cstr, frag->text);
objtext(annot, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
sprintf(buf, "%g %g %g %g",
- xr->lx / 4096.0, xr->by / 4096.0,
- xr->rx / 4096.0, xr->ty / 4096.0);
+ xr->lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT,
+ xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT);
objtext(annot, buf);
objtext(annot, "]\n/Border [0 0 0]\n");
/*
* Set up the outlines dictionary.
*/
- {
+ if (outlines) {
int topcount;
char buf[80];
static object *new_object(objlist *list)
{
- object *obj = mknew(object);
+ object *obj = snew(object);
obj->list = list;
* means that glyphs are identified by PS strings and hence font
* encoding can be managed independently of the supplied encoding
* of the font. However, in the document outline, the PDF spec
- * simply asks for ordinary text strings without mentioning what
- * character set they are supposed to be interpreted in.
- *
- * Therefore, for the moment, I'm going to assume they're US-ASCII
- * only. If anyone knows better, they should let me know :-/
+ * encodes in either PDFDocEncoding (a custom superset of
+ * ISO-8859-1) or UTF-16BE.
*/
-static int pdf_convert(wchar_t *s, char **result) {
- int doing = (result != 0);
- int ok = TRUE;
- char *p = NULL;
- int plen = 0, psize = 0;
-
- for (; *s; s++) {
- wchar_t c = *s;
- char outc;
-
- if (c >= 32 && c <= 126) {
- /* Char is OK. */
- outc = (char)c;
- } else {
- /* Char is not OK. */
- ok = FALSE;
- outc = 0xBF; /* approximate the good old DEC `uh?' */
- }
- if (doing) {
- if (plen >= psize) {
- psize = plen + 256;
- p = resize(p, psize);
- }
- p[plen++] = outc;
- }
+static char *pdf_outline_convert(wchar_t *s, int *len) {
+ char *ret;
+
+ ret = utoa_careful_dup(s, CS_PDF);
+
+ /*
+ * Very silly special case: if the returned string begins with
+ * FE FF, then the PDF reader will mistake it for a UTF-16BE
+ * string. So in this case we give up on PDFDocEncoding and
+ * encode it in UTF-16 straight away.
+ */
+ if (ret && ret[0] == '\xFE' && ret[1] == '\xFF') {
+ sfree(ret);
+ ret = NULL;
}
- if (doing) {
- p = resize(p, plen+1);
- p[plen] = '\0';
- *result = p;
+
+ if (!ret) {
+ ret = utoa_dup_len(s, CS_UTF16BE, len);
+ } else {
+ *len = strlen(ret);
}
- return ok;
+
+ return ret;
}
static int make_outline(object *parent, outline_element *items, int n,
while (n > 0) {
char *title;
+ int titlelen;
/*
* Here we expect to be sitting on an item at the given
*/
assert(items->level == level);
- pdf_convert(items->pdata->outline_title, &title);
+ title = pdf_outline_convert(items->pdata->outline_title, &titlelen);
totalcount++;
curr = new_object(parent->list);
if (!first) first = curr;
last = curr;
objtext(curr, "<<\n/Title ");
- pdf_string(objtext, curr, title);
+ pdf_string_len(objtext, curr, title, titlelen);
objtext(curr, "\n/Parent ");
objref(curr, parent);
objtext(curr, "\n/Dest [");
return ret;
}
-static void pdf_string(void (*add)(object *, char const *),
- object *o, char const *str)
+static void pdf_string_len(void (*add)(object *, char const *),
+ object *o, char const *str, int len)
{
char const *p;
add(o, "(");
- for (p = str; *p; p++) {
- char c[2];
- if (*p == '\\' || *p == '(' || *p == ')')
- add(o, "\\");
- c[0] = *p;
- c[1] = '\0';
+ for (p = str; len > 0; p++, len--) {
+ char c[10];
+ if (*p < ' ' || *p > '~') {
+ sprintf(c, "\\%03o", 0xFF & (int)*p);
+ } else {
+ int n = 0;
+ if (*p == '\\' || *p == '(' || *p == ')')
+ c[n++] = '\\';
+ c[n++] = *p;
+ c[n] = '\0';
+ }
add(o, c);
}
add(o, ")");
}
+
+static void pdf_string(void (*add)(object *, char const *),
+ object *o, char const *str)
+{
+ pdf_string_len(add, o, str, strlen(str));
+}