+
+/* ------- EPS -------
+ EPS and Postscript references:
+ http://partners.adobe.com/asn/developer/technotes/postscript.html
+*/
+
+typedef struct eps_font
+{
+ const char *ps_font;
+ int id;
+ struct eps_font *next;
+} eps_font;
+
+typedef struct eps_state
+{
+ FILE *fp;
+ gfx_canvas_t *canvas;
+ art_u32 page_width, page_height;
+ eps_font *font_list;
+ /*--*/
+ gfx_color_t color;
+ const char *font;
+ double font_size;
+ double line_width;
+ int linecap, linejoin;
+ int has_dash;
+} eps_state;
+
+static void eps_set_color(eps_state *state, gfx_color_t color)
+{
+#if USE_EPS_FAKE_ALPHA
+ double a1, a2;
+#endif
+ /* gfx_color_t is RRGGBBAA */
+ if (state->color == color)
+ return;
+#if USE_EPS_FAKE_ALPHA
+ a1 = (color & 255) / 255.0;
+ a2 = 255 * (1 - a1);
+#define eps_color_calc(x) (int)( ((x) & 255) * a1 + a2)
+#else
+#define eps_color_calc(x) (int)( (x) & 255)
+#endif
+ /* gfx_color_t is RRGGBBAA */
+ if (state->color == color)
+ return;
+ fprintf(state->fp, "%d %d %d Rgb\n",
+ eps_color_calc(color >> 24),
+ eps_color_calc(color >> 16),
+ eps_color_calc(color >> 8));
+ state->color = color;
+}
+
+static int eps_add_font(eps_state *state, gfx_node_t *node)
+{
+ /* The fonts list could be postponed to the end using
+ (atend), but let's be nice and have them in the header. */
+ const char *ps_font = afm_get_font_postscript_name(node->filename);
+ eps_font *ef;
+ for (ef = state->font_list; ef; ef = ef->next) {
+ if (!strcmp(ps_font, ef->ps_font))
+ return 0;
+ }
+ ef = malloc(sizeof(eps_font));
+ if (ef == NULL) {
+ rrd_set_error("malloc for eps_font");
+ return -1;
+ }
+ ef->next = state->font_list;
+ ef->ps_font = ps_font;
+ state->font_list = ef;
+ return 0;
+}
+
+static void eps_list_fonts(eps_state *state, const char *dscName)
+{
+ eps_font *ef;
+ int lineLen = strlen(dscName);
+ if (!state->font_list)
+ return;
+ fputs(dscName, state->fp);
+ for (ef = state->font_list; ef; ef = ef->next) {
+ int nameLen = strlen(ef->ps_font);
+ if (lineLen + nameLen > 100 && lineLen) {
+ fputs("\n", state->fp);
+ fputs("%%- \n", state->fp);
+ lineLen = 5;
+ } else {
+ fputs(" ", state->fp);
+ lineLen++;
+ }
+ fputs(ef->ps_font, state->fp);
+ lineLen += nameLen;
+ }
+ fputs("\n", state->fp);
+}
+
+static void eps_define_fonts(eps_state *state)
+{
+ eps_font *ef;
+ if (!state->font_list)
+ return;
+ for (ef = state->font_list; ef; ef = ef->next) {
+ /* PostScript¨ LANGUAGE REFERENCE third edition
+ page 349 */
+ fprintf(state->fp,
+ "%%\n"
+ "/%s findfont dup length dict begin\n"
+ "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
+ "/Encoding ISOLatin1Encoding def\n"
+ "currentdict end\n"
+ "/%s-ISOLatin1 exch definefont pop\n"
+ "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
+ ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
+ }
+}
+
+static int eps_prologue(eps_state *state)
+{
+ gfx_node_t *node;
+ fputs(
+ "%!PS-Adobe-3.0 EPSF-3.0\n"
+ "%%Creator: RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
+ /* can't like weird chars here */
+ "%%Title: (RRDtool output)\n"
+ "%%DocumentData: Clean7Bit\n"
+ "", state->fp);
+ fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
+ state->page_width, state->page_height);
+ for (node = state->canvas->firstnode; node; node = node->next) {
+ if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
+ return -1;
+ }
+ eps_list_fonts(state, "%%DocumentFonts:");
+ eps_list_fonts(state, "%%DocumentNeededFonts:");
+ fputs(
+ "%%EndComments\n"
+ "%%BeginProlog\n"
+ "%%EndProlog\n" /* must have, or BoundingBox is ignored */
+ "/bd { bind def } bind def\n"
+ "", state->fp);
+ fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
+ fputs(
+ "/X2 {X exch X exch} bd\n"
+ "/M {X2 moveto} bd\n"
+ "/L {X2 lineto} bd\n"
+ "/m {moveto} bd\n"
+ "/l {lineto} bd\n"
+ "/S {stroke} bd\n"
+ "/CP {closepath} bd\n"
+ "/WS {setlinewidth stroke} bd\n"
+ "/F {fill} bd\n"
+ "/T1 {gsave} bd\n"
+ "/T2 {concat 0 0 moveto show grestore} bd\n"
+ "/T {moveto show} bd\n"
+ "/Rgb { 255.0 div 3 1 roll\n"
+ " 255.0 div 3 1 roll \n"
+ " 255.0 div 3 1 roll setrgbcolor } bd\n"
+ "", state->fp);
+ eps_define_fonts(state);
+ return 0;
+}
+
+static void eps_clear_dash(eps_state *state)
+{
+ if (!state->has_dash)
+ return;
+ state->has_dash = 0;
+ fputs("[1 0] 0 setdash\n", state->fp);
+}
+
+static void eps_write_linearea(eps_state *state, gfx_node_t *node)
+{
+ int i;
+ FILE *fp = state->fp;
+ int useOffset = 0;
+ int clearDashIfAny = 1;
+ eps_set_color(state, node->color);
+ if (node->type == GFX_LINE) {
+ svg_dash dash_info;
+ if (state->linecap != 1) {
+ fputs("1 setlinecap\n", fp);
+ state->linecap = 1;
+ }
+ if (state->linejoin != 1) {
+ fputs("1 setlinejoin\n", fp);
+ state->linejoin = 1;
+ }
+ svg_get_dash(node, &dash_info);
+ if (dash_info.dash_enable) {
+ clearDashIfAny = 0;
+ state->has_dash = 1;
+ fputs("[", fp);
+ svg_write_number(fp, dash_info.adjusted_on);
+ fputs(" ", fp);
+ svg_write_number(fp, dash_info.adjusted_off);
+ fputs("] ", fp);
+ svg_write_number(fp, dash_info.dash_offset);
+ fputs(" setdash\n", fp);
+ }
+ }
+ if (clearDashIfAny)
+ eps_clear_dash(state);
+ for (i = 0; i < node->points; i++) {
+ ArtVpath *vec = node->path + i;
+ double x = vec->x;
+ double y = state->page_height - vec->y;
+ if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
+ useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
+ if (useOffset) {
+ x -= LINEOFFSET;
+ y -= LINEOFFSET;
+ }
+ switch (vec->code) {
+ case ART_MOVETO_OPEN: /* fall-through */
+ case ART_MOVETO:
+ svg_write_number(fp, x);
+ fputc(' ', fp);
+ svg_write_number(fp, y);
+ fputc(' ', fp);
+ fputs(useOffset ? "M\n" : "m\n", fp);
+ break;
+ case ART_LINETO:
+ svg_write_number(fp, x);
+ fputc(' ', fp);
+ svg_write_number(fp, y);
+ fputc(' ', fp);
+ fputs(useOffset ? "L\n" : "l\n", fp);
+ break;
+ case ART_CURVETO: break; /* unsupported */
+ case ART_END: break; /* nop */
+ }
+ }
+ if (node->type == GFX_LINE) {
+ if (node->closed_path)
+ fputs("CP ", fp);
+ if (node->size != state->line_width) {
+ state->line_width = node->size;
+ svg_write_number(fp, state->line_width);
+ fputs(" WS\n", fp);
+ } else {
+ fputs("S\n", fp);
+ }
+ } else {
+ fputs("F\n", fp);
+ }
+}
+
+static void eps_write_text(eps_state *state, gfx_node_t *node)
+{
+ FILE *fp = state->fp;
+ const char *ps_font = afm_get_font_postscript_name(node->filename);
+ int lineLen = 0;
+ pdf_coords g;
+#ifdef HAVE_MBSTOWCS
+ size_t clen;
+ wchar_t *p, *cstr, ch;
+ int text_count;
+ if (!node->text)
+ return;
+ clen = strlen(node->text) + 1;
+ cstr = malloc(sizeof(wchar_t) * clen);
+ text_count = mbstowcs(cstr, node->text, clen);
+ if (text_count == -1)
+ text_count = mbstowcs(cstr, "Enc-Err", 6);
+ p = cstr;
+#else
+ const unsigned char *p = node->text, ch;
+ if (!p)
+ return;
+#endif
+ pdf_calc(state->page_height, node, &g);
+ eps_set_color(state, node->color);
+ if (strcmp(ps_font, state->font) || node->size != state->font_size) {
+ state->font = ps_font;
+ state->font_size = node->size;
+ svg_write_number(fp, state->font_size);
+ fprintf(fp, " SetFont-%s\n", state->font);
+ }
+ if (node->angle)
+ fputs("T1 ", fp);
+ fputs("(", fp);
+ lineLen = 20;
+ while (1) {
+ ch = *p;
+ if (!ch)
+ break;
+ ch = afm_fix_osx_charset(ch); /* unsafe macro */
+ if (++lineLen > 70) {
+ fputs("\\\n", fp); /* backslash and \n */
+ lineLen = 0;
+ }
+ switch (ch) {
+ case '%':
+ case '(':
+ case ')':
+ case '\\':
+ fputc('\\', fp);
+ fputc(ch, fp);
+ break;
+ case '\n':
+ fputs("\\n", fp);
+ break;
+ case '\r':
+ fputs("\\r", fp);
+ break;
+ case '\t':
+ fputs("\\t", fp);
+ break;
+ default:
+ if (ch > 255) {
+ fputc('?', fp);
+ } else if (ch >= 126 || ch < 32) {
+ fprintf(fp, "\\%03o", (unsigned int)ch);
+ lineLen += 3;
+ } else {
+ fputc(ch, fp);
+ }
+ }
+ p++;
+ }
+#ifdef HAVE_MBSTOWCS
+ free(cstr);
+#endif
+ if (node->angle) {
+ /* can't use svg_write_number as 2 decimals is far from enough to avoid
+ skewed text */
+ fprintf(fp, ") [%f %f %f %f %f %f] T2\n",
+ g.ma, g.mb, g.mc, g.md, g.tmx, g.tmy);
+ } else {
+ fputs(") ", fp);
+ svg_write_number(fp, g.tmx);
+ fputs(" ", fp);
+ svg_write_number(fp, g.tmy);
+ fputs(" T\n", fp);
+ }
+}
+
+static int eps_write_content(eps_state *state)
+{
+ gfx_node_t *node;
+ fputs("%\n", state->fp);
+ for (node = state->canvas->firstnode; node; node = node->next) {
+ switch (node->type) {
+ case GFX_LINE:
+ case GFX_AREA:
+ eps_write_linearea(state, node);
+ break;
+ case GFX_TEXT:
+ eps_write_text(state, node);
+ break;
+ }
+ }
+ return 0;
+}
+
+int gfx_render_eps (gfx_canvas_t *canvas,
+ art_u32 width, art_u32 height,
+ gfx_color_t background, FILE *fp){
+ struct eps_state state;
+ state.fp = fp;
+ state.canvas = canvas;
+ state.page_width = width;
+ state.page_height = height;
+ state.font = "no-default-font";
+ state.font_size = -1;
+ state.color = 0; /* black */
+ state.font_list = NULL;
+ state.linecap = -1;
+ state.linejoin = -1;
+ state.has_dash = 0;
+ state.line_width = 1;
+ if (eps_prologue(&state) == -1)
+ return -1;
+ eps_set_color(&state, background);
+ fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
+ height, width, height, width);
+ if (eps_write_content(&state) == -1)
+ return 0;
+ fputs("showpage\n", fp);
+ fputs("%%EOF\n", fp);
+ while (state.font_list) {
+ eps_font *next = state.font_list->next;
+ free(state.font_list);
+ state.font_list = next;
+ }
+ return 0;
+}
+
+/* ------- PDF -------
+ PDF references page:
+ http://partners.adobe.com/public/developer/pdf/index_reference.html
+*/
+
+typedef struct pdf_buffer
+{
+ int id, is_obj, is_dict, is_stream, pdf_file_pos;
+ char *data;
+ int alloc_size, current_size;
+ struct pdf_buffer *previous_buffer, *next_buffer;
+ struct pdf_state *state;
+} pdf_buffer;
+
+typedef struct pdf_font
+{
+ const char *ps_font;
+ pdf_buffer obj;
+ struct pdf_font *next;
+} pdf_font;
+
+typedef struct pdf_state
+{
+ FILE *fp;
+ gfx_canvas_t *canvas;
+ art_u32 page_width, page_height;
+ pdf_font *font_list;
+ pdf_buffer *first_buffer, *last_buffer;
+ int pdf_file_pos;
+ int has_failed;
+ /*--*/
+ gfx_color_t stroke_color, fill_color;
+ int font_id;
+ double font_size;
+ double line_width;
+ svg_dash dash;
+ int linecap, linejoin;
+ int last_obj_id;
+ /*--*/
+ pdf_buffer pdf_header;
+ pdf_buffer info_obj, catalog_obj, pages_obj, page1_obj;
+ pdf_buffer fontsdict_obj;
+ pdf_buffer graph_stream;
+} pdf_state;
+
+static void pdf_init_buffer(pdf_state *state, pdf_buffer *buf)
+{
+ int initial_size = 32;
+ buf->state = state;
+ buf->id = -42;
+ buf->alloc_size = 0;
+ buf->current_size = 0;
+ buf->data = (char*)malloc(initial_size);
+ buf->is_obj = 0;
+ buf->previous_buffer = NULL;
+ buf->next_buffer = NULL;
+ if (buf->data == NULL) {
+ rrd_set_error("malloc for pdf_buffer data");
+ state->has_failed = 1;
+ return;
+ }
+ buf->alloc_size = initial_size;
+ if (state->last_buffer)
+ state->last_buffer->next_buffer = buf;
+ if (state->first_buffer == NULL)
+ state->first_buffer = buf;
+ buf->previous_buffer = state->last_buffer;
+ state->last_buffer = buf;
+}
+
+static void pdf_put(pdf_buffer *buf, const char *text, int len)
+{
+ if (len <= 0)
+ return;
+ if (buf->alloc_size < buf->current_size + len) {
+ int new_size = buf->alloc_size;
+ char *new_buf;
+ while (new_size < buf->current_size + len)
+ new_size *= 4;
+ new_buf = (char*)malloc(new_size);
+ if (new_buf == NULL) {
+ rrd_set_error("re-malloc for pdf_buffer data");
+ buf->state->has_failed = 1;
+ return;
+ }
+ memcpy(new_buf, buf->data, buf->current_size);
+ free(buf->data);
+ buf->data = new_buf;
+ buf->alloc_size = new_size;
+ }
+ memcpy(buf->data + buf->current_size, text, len);
+ buf->current_size += len;
+}
+
+static void pdf_put_char(pdf_buffer *buf, char c)
+{
+ if (buf->alloc_size >= buf->current_size + 1) {
+ buf->data[buf->current_size++] = c;
+ } else {
+ char tmp[1];
+ tmp[0] = (char)c;
+ pdf_put(buf, tmp, 1);
+ }
+}
+
+static void pdf_puts(pdf_buffer *buf, const char *text)
+{
+ pdf_put(buf, text, strlen(text));
+}
+
+static void pdf_indent(pdf_buffer *buf)
+{
+ pdf_puts(buf, "\t");
+}
+
+static void pdf_putsi(pdf_buffer *buf, const char *text)
+{
+ pdf_indent(buf);
+ pdf_puts(buf, text);
+}
+
+static void pdf_putint(pdf_buffer *buf, int i)
+{
+ char tmp[20];
+ sprintf(tmp, "%d", i);
+ pdf_puts(buf, tmp);
+}
+
+static void pdf_putnumber(pdf_buffer *buf, double d)
+{
+ char tmp[50];
+ svg_format_number(tmp, sizeof(tmp), d);
+ pdf_puts(buf, tmp);
+}
+
+static void pdf_put_string_contents_wide(pdf_buffer *buf, const afm_char *text)
+{
+ const afm_char *p = text;
+ while (1) {
+ afm_char ch = *p;
+ ch = afm_fix_osx_charset(ch); /* unsafe macro */
+ switch (ch) {
+ case 0:
+ return;
+ case '(':
+ pdf_puts(buf, "\\(");
+ break;
+ case ')':
+ pdf_puts(buf, "\\)");
+ break;
+ case '\\':
+ pdf_puts(buf, "\\\\");
+ break;
+ case '\n':
+ pdf_puts(buf, "\\n");
+ break;
+ case '\r':
+ pdf_puts(buf, "\\r");
+ break;
+ case '\t':
+ pdf_puts(buf, "\\t");
+ break;
+ default:
+ if (ch > 255) {
+ pdf_put_char(buf, '?');
+ } else if (ch >= 126 || ch < 32) {
+ pdf_put_char(buf, ch);
+ } else if (ch >= 0 && ch <= 255) {
+ char tmp[10];
+ snprintf(tmp, sizeof(tmp), "\\%03o", (int)ch);
+ pdf_puts(buf, tmp);
+ }
+ }
+ p++;
+ }
+}
+
+static void pdf_put_string_contents(pdf_buffer *buf, const char *text)
+{
+#ifdef HAVE_MBSTOWCS
+ size_t clen = strlen(text) + 1;
+ wchar_t *cstr = malloc(sizeof(wchar_t) * clen);
+ int text_count = mbstowcs(cstr, text, clen);
+ if (text_count == -1)
+ text_count = mbstowcs(cstr, "Enc-Err", 6);
+ pdf_put_string_contents_wide(buf, cstr);
+#if 0
+ if (*text == 'W') {
+ fprintf(stderr, "Decoding utf8 for '%s'\n", text);
+ wchar_t *p = cstr;
+ char *pp = text;
+ fprintf(stderr, "sz wc = %d\n", sizeof(wchar_t));
+ while (*p) {
+ fprintf(stderr, " %d = %c versus %d = %c\n", *p, (char)*p, 255 & (int)*pp, *pp);
+ p++;
+ pp++;
+ }
+ }
+#endif
+ free(cstr);
+#else
+ pdf_put_string_contents_wide(buf, text);
+#endif
+}
+
+static void pdf_init_object(pdf_state *state, pdf_buffer *buf)
+{
+ pdf_init_buffer(state, buf);
+ buf->id = ++state->last_obj_id;
+ buf->is_obj = 1;
+ buf->is_stream = 0;
+}
+
+static void pdf_init_dict(pdf_state *state, pdf_buffer *buf)
+{
+ pdf_init_object(state, buf);
+ buf->is_dict = 1;
+}
+
+static void pdf_set_color(pdf_buffer *buf, gfx_color_t color,
+ gfx_color_t *current_color, const char *op)
+{
+#if USE_PDF_FAKE_ALPHA
+ double a1, a2;
+#endif
+ /* gfx_color_t is RRGGBBAA */
+ if (*current_color == color)
+ return;
+#if USE_PDF_FAKE_ALPHA
+ a1 = (color & 255) / 255.0;
+ a2 = 1 - a1;
+#define pdf_color_calc(x) ( ((x) & 255) / 255.0 * a1 + a2)
+#else
+#define pdf_color_calc(x) ( ((x) & 255) / 255.0)
+#endif
+ pdf_putnumber(buf, pdf_color_calc(color >> 24));
+ pdf_puts(buf, " ");
+ pdf_putnumber(buf, pdf_color_calc(color >> 16));
+ pdf_puts(buf, " ");
+ pdf_putnumber(buf, pdf_color_calc(color >> 8));
+ pdf_puts(buf, " ");
+ pdf_puts(buf, op);
+ pdf_puts(buf, "\n");
+ *current_color = color;
+}
+
+static void pdf_set_stroke_color(pdf_buffer *buf, gfx_color_t color)
+{
+ pdf_set_color(buf, color, &buf->state->stroke_color, "RG");
+}
+
+static void pdf_set_fill_color(pdf_buffer *buf, gfx_color_t color)
+{
+ pdf_set_color(buf, color, &buf->state->fill_color, "rg");
+}
+
+static pdf_font *pdf_find_font(pdf_state *state, gfx_node_t *node)
+{
+ const char *ps_font = afm_get_font_postscript_name(node->filename);
+ pdf_font *ef;
+ for (ef = state->font_list; ef; ef = ef->next) {
+ if (!strcmp(ps_font, ef->ps_font))
+ return ef;
+ }
+ return NULL;
+}
+
+static void pdf_add_font(pdf_state *state, gfx_node_t *node)
+{
+ pdf_font *ef = pdf_find_font(state, node);
+ if (ef)
+ return;
+ ef = malloc(sizeof(pdf_font));
+ if (ef == NULL) {
+ rrd_set_error("malloc for pdf_font");
+ state->has_failed = 1;
+ return;
+ }
+ pdf_init_dict(state, &ef->obj);
+ ef->next = state->font_list;
+ ef->ps_font = afm_get_font_postscript_name(node->filename);
+ state->font_list = ef;
+ /* fonts dict */
+ pdf_putsi(&state->fontsdict_obj, "/F");
+ pdf_putint(&state->fontsdict_obj, ef->obj.id);
+ pdf_puts(&state->fontsdict_obj, " ");
+ pdf_putint(&state->fontsdict_obj, ef->obj.id);
+ pdf_puts(&state->fontsdict_obj, " 0 R\n");
+ /* fonts def */
+ pdf_putsi(&ef->obj, "/Type /Font\n");
+ pdf_putsi(&ef->obj, "/Subtype /Type1\n");
+ pdf_putsi(&ef->obj, "/Name /F");
+ pdf_putint(&ef->obj, ef->obj.id);
+ pdf_puts(&ef->obj, "\n");
+ pdf_putsi(&ef->obj, "/BaseFont /");
+ pdf_puts(&ef->obj, ef->ps_font);
+ pdf_puts(&ef->obj, "\n");
+ pdf_putsi(&ef->obj, "/Encoding /WinAnsiEncoding\n");
+ /* 'Cp1252' (this is latin 1 extended with 27 characters;
+ the encoding is also known as 'winansi')
+ http://www.lowagie.com/iText/tutorial/ch09.html */
+}
+
+static void pdf_create_fonts(pdf_state *state)
+{
+ gfx_node_t *node;
+ for (node = state->canvas->firstnode; node; node = node->next) {
+ if (node->type == GFX_TEXT)
+ pdf_add_font(state, node);
+ }
+}
+
+static void pdf_write_linearea(pdf_state *state, gfx_node_t *node)
+{
+ int i;
+ pdf_buffer *s = &state->graph_stream;
+ if (node->type == GFX_LINE) {
+ svg_dash dash_info;
+ svg_get_dash(node, &dash_info);
+ if (!svg_dash_equal(&dash_info, &state->dash)) {
+ state->dash = dash_info;
+ if (dash_info.dash_enable) {
+ pdf_puts(s, "[");
+ pdf_putnumber(s, dash_info.adjusted_on);
+ pdf_puts(s, " ");
+ pdf_putnumber(s, dash_info.adjusted_off);
+ pdf_puts(s, "] ");
+ pdf_putnumber(s, dash_info.dash_offset);
+ pdf_puts(s, " d\n");
+ } else {
+ pdf_puts(s, "[] 0 d\n");
+ }
+ }
+ pdf_set_stroke_color(s, node->color);
+ if (state->linecap != 1) {
+ pdf_puts(s, "1 j\n");
+ state->linecap = 1;
+ }
+ if (state->linejoin != 1) {
+ pdf_puts(s, "1 J\n");
+ state->linejoin = 1;
+ }
+ if (node->size != state->line_width) {
+ state->line_width = node->size;
+ pdf_putnumber(s, state->line_width);
+ pdf_puts(s, " w\n");
+ }
+ } else {
+ pdf_set_fill_color(s, node->color);
+ }
+ for (i = 0; i < node->points; i++) {
+ ArtVpath *vec = node->path + i;
+ double x = vec->x;
+ double y = state->page_height - vec->y;
+ if (node->type == GFX_AREA) {
+ x += LINEOFFSET; /* adjust for libart handling of areas */
+ y -= LINEOFFSET;
+ }
+ switch (vec->code) {
+ case ART_MOVETO_OPEN: /* fall-through */
+ case ART_MOVETO:
+ pdf_putnumber(s, x);
+ pdf_puts(s, " ");
+ pdf_putnumber(s, y);
+ pdf_puts(s, " m\n");
+ break;
+ case ART_LINETO:
+ pdf_putnumber(s, x);
+ pdf_puts(s, " ");
+ pdf_putnumber(s, y);
+ pdf_puts(s, " l\n");
+ break;
+ case ART_CURVETO: break; /* unsupported */
+ case ART_END: break; /* nop */
+ }
+ }
+ if (node->type == GFX_LINE) {
+ pdf_puts(s, node->closed_path ? "s\n" : "S\n");
+ } else {
+ pdf_puts(s, "f\n");
+ }
+}
+
+
+static void pdf_write_matrix(pdf_state *state, gfx_node_t *node, pdf_coords *g, int useTM)
+{
+ char tmp[150];
+ pdf_buffer *s = &state->graph_stream;
+ if (node->angle == 0) {
+ pdf_puts(s, "1 0 0 1 ");
+ pdf_putnumber(s, useTM ? g->tmx : g->mx);
+ pdf_puts(s, " ");
+ pdf_putnumber(s, useTM ? g->tmy : g->my);
+ } else {
+ /* can't use svg_write_number as 2 decimals is far from enough to avoid
+ skewed text */
+ sprintf(tmp, "%f %f %f %f %f %f",
+ g->ma, g->mb, g->mc, g->md,
+ useTM ? g->tmx : g->mx,
+ useTM ? g->tmy : g->my);
+ pdf_puts(s, tmp);
+ }
+}
+
+static void pdf_write_text(pdf_state *state, gfx_node_t *node,
+ int last_was_text, int next_is_text)
+{
+ pdf_coords g;
+ pdf_buffer *s = &state->graph_stream;
+ pdf_font *font = pdf_find_font(state, node);
+ if (font == NULL) {
+ rrd_set_error("font disappeared");
+ state->has_failed = 1;
+ return;
+ }
+ pdf_calc(state->page_height, node, &g);
+#if PDF_CALC_DEBUG
+ pdf_puts(s, "q % debug green box\n");
+ pdf_write_matrix(state, node, &g, 0);
+ pdf_puts(s, " cm\n");
+ pdf_set_fill_color(s, 0x90FF9000);
+ pdf_puts(s, "0 0.4 0 rg\n");
+ pdf_puts(s, "0 0 ");
+ pdf_putnumber(s, g.sizep.x);
+ pdf_puts(s, " ");
+ pdf_putnumber(s, g.sizep.y);
+ pdf_puts(s, " re\n");
+ pdf_puts(s, "f\n");
+ pdf_puts(s, "Q\n");
+#endif
+ pdf_set_fill_color(s, node->color);
+ if (PDF_CALC_DEBUG || !last_was_text)
+ pdf_puts(s, "BT\n");
+ if (state->font_id != font->obj.id || node->size != state->font_size) {
+ state->font_id = font->obj.id;
+ state->font_size = node->size;
+ pdf_puts(s, "/F");
+ pdf_putint(s, font->obj.id);
+ pdf_puts(s, " ");
+ pdf_putnumber(s, node->size);
+ pdf_puts(s, " Tf\n");
+ }
+ pdf_write_matrix(state, node, &g, 1);
+ pdf_puts(s, " Tm\n");
+ pdf_puts(s, "(");
+ pdf_put_string_contents(s, node->text);
+ pdf_puts(s, ") Tj\n");
+ if (PDF_CALC_DEBUG || !next_is_text)
+ pdf_puts(s, "ET\n");
+}
+
+static void pdf_write_content(pdf_state *state)
+{
+ gfx_node_t *node;
+ int last_was_text = 0, next_is_text;
+ for (node = state->canvas->firstnode; node; node = node->next) {
+ switch (node->type) {
+ case GFX_LINE:
+ case GFX_AREA:
+ pdf_write_linearea(state, node);
+ break;
+ case GFX_TEXT:
+ next_is_text = node->next && node->next->type == GFX_TEXT;
+ pdf_write_text(state, node, last_was_text, next_is_text);
+ break;
+ }
+ last_was_text = node->type == GFX_TEXT;
+ }
+}
+
+static void pdf_init_document(pdf_state *state)
+{
+ pdf_init_buffer(state, &state->pdf_header);
+ pdf_init_dict(state, &state->catalog_obj);
+ pdf_init_dict(state, &state->info_obj);
+ pdf_init_dict(state, &state->pages_obj);
+ pdf_init_dict(state, &state->page1_obj);
+ pdf_init_dict(state, &state->fontsdict_obj);
+ pdf_create_fonts(state);
+ if (state->has_failed)
+ return;
+ /* make stream last object in file */
+ pdf_init_object(state, &state->graph_stream);
+ state->graph_stream.is_stream = 1;
+}
+
+static void pdf_setup_document(pdf_state *state)
+{
+ const char *creator = "RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch";
+ /* all objects created by now, so init code can reference them */
+ /* HEADER */
+ pdf_puts(&state->pdf_header, "%PDF-1.3\n");
+ /* following 8 bit comment is recommended by Adobe for
+ indicating binary file to file transfer applications */
+ pdf_puts(&state->pdf_header, "%\xE2\xE3\xCF\xD3\n");
+ /* INFO */
+ pdf_putsi(&state->info_obj, "/Creator (");
+ pdf_put_string_contents(&state->info_obj, creator);
+ pdf_puts(&state->info_obj, ")\n");
+ /* CATALOG */
+ pdf_putsi(&state->catalog_obj, "/Type /Catalog\n");
+ pdf_putsi(&state->catalog_obj, "/Pages ");
+ pdf_putint(&state->catalog_obj, state->pages_obj.id);
+ pdf_puts(&state->catalog_obj, " 0 R\n");
+ /* PAGES */
+ pdf_putsi(&state->pages_obj, "/Type /Pages\n");
+ pdf_putsi(&state->pages_obj, "/Kids [");
+ pdf_putint(&state->pages_obj, state->page1_obj.id);
+ pdf_puts(&state->pages_obj, " 0 R]\n");
+ pdf_putsi(&state->pages_obj, "/Count 1\n");
+ /* PAGE 1 */
+ pdf_putsi(&state->page1_obj, "/Type /Page\n");
+ pdf_putsi(&state->page1_obj, "/Parent ");
+ pdf_putint(&state->page1_obj, state->pages_obj.id);
+ pdf_puts(&state->page1_obj, " 0 R\n");
+ pdf_putsi(&state->page1_obj, "/MediaBox [0 0 ");
+ pdf_putint(&state->page1_obj, state->page_width);
+ pdf_puts(&state->page1_obj, " ");
+ pdf_putint(&state->page1_obj, state->page_height);
+ pdf_puts(&state->page1_obj, "]\n");
+ pdf_putsi(&state->page1_obj, "/Contents ");
+ pdf_putint(&state->page1_obj, state->graph_stream.id);
+ pdf_puts(&state->page1_obj, " 0 R\n");
+ pdf_putsi(&state->page1_obj, "/Resources << /Font ");
+ pdf_putint(&state->page1_obj, state->fontsdict_obj.id);
+ pdf_puts(&state->page1_obj, " 0 R >>\n");
+}
+
+static void pdf_write_string_to_file(pdf_state *state, const char *text)
+{
+ fputs(text, state->fp);
+ state->pdf_file_pos += strlen(text);
+}
+
+static void pdf_write_buf_to_file(pdf_state *state, pdf_buffer *buf)
+{
+ char tmp[40];
+ buf->pdf_file_pos = state->pdf_file_pos;
+ if (buf->is_obj) {
+ snprintf(tmp, sizeof(tmp), "%d 0 obj\n", buf->id);
+ pdf_write_string_to_file(state, tmp);
+ }
+ if (buf->is_dict)
+ pdf_write_string_to_file(state, "<<\n");
+ if (buf->is_stream) {
+ snprintf(tmp, sizeof(tmp), "<< /Length %d >>\n", buf->current_size);
+ pdf_write_string_to_file(state, tmp);
+ pdf_write_string_to_file(state, "stream\n");
+ }
+ fwrite(buf->data, 1, buf->current_size, state->fp);
+ state->pdf_file_pos += buf->current_size;
+ if (buf->is_stream)
+ pdf_write_string_to_file(state, "endstream\n");
+ if (buf->is_dict)
+ pdf_write_string_to_file(state, ">>\n");
+ if (buf->is_obj)
+ pdf_write_string_to_file(state, "endobj\n");
+}
+
+static void pdf_write_to_file(pdf_state *state)
+{
+ pdf_buffer *buf = state->first_buffer;
+ int xref_pos;
+ state->pdf_file_pos = 0;
+ pdf_write_buf_to_file(state, &state->pdf_header);
+ while (buf) {
+ if (buf->is_obj)
+ pdf_write_buf_to_file(state, buf);
+ buf = buf->next_buffer;
+ }
+ xref_pos = state->pdf_file_pos;
+ fprintf(state->fp, "xref\n");
+ fprintf(state->fp, "%d %d\n", 0, state->last_obj_id + 1);
+ /* TOC lines must be exactly 20 bytes including \n */
+ fprintf(state->fp, "%010d %05d f\x20\n", 0, 65535);
+ for (buf = state->first_buffer; buf; buf = buf->next_buffer) {
+ if (buf->is_obj)
+ fprintf(state->fp, "%010d %05d n\x20\n", buf->pdf_file_pos, 0);
+ }
+ fprintf(state->fp, "trailer\n");
+ fprintf(state->fp, "<<\n");
+ fprintf(state->fp, "\t/Size %d\n", state->last_obj_id + 1);
+ fprintf(state->fp, "\t/Root %d 0 R\n", state->catalog_obj.id);
+ fprintf(state->fp, "\t/Info %d 0 R\n", state->info_obj.id);
+ fprintf(state->fp, ">>\n");
+ fprintf(state->fp, "startxref\n");
+ fprintf(state->fp, "%d\n", xref_pos);
+ fputs("%%EOF\n", state->fp);
+}
+
+static void pdf_free_resources(pdf_state *state)
+{
+ pdf_buffer *buf = state->first_buffer;
+ while (buf) {
+ free(buf->data);
+ buf->data = NULL;
+ buf->alloc_size = buf->current_size = 0;
+ buf = buf->next_buffer;
+ }
+ while (state->font_list) {
+ pdf_font *next = state->font_list->next;
+ free(state->font_list);
+ state->font_list = next;
+ }
+}
+
+int gfx_render_pdf (gfx_canvas_t *canvas,
+ art_u32 width, art_u32 height,
+ gfx_color_t UNUSED(background), FILE *fp){
+ struct pdf_state state;
+ memset(&state, 0, sizeof(pdf_state));
+ state.fp = fp;
+ state.canvas = canvas;
+ state.page_width = width;
+ state.page_height = height;
+ state.font_id = -1;
+ state.font_size = -1;
+ state.font_list = NULL;
+ state.linecap = -1;
+ state.linejoin = -1;
+ pdf_init_document(&state);
+ /*
+ pdf_set_color(&state, background);
+ fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
+ height, width, height, width);
+ */
+ if (!state.has_failed)
+ pdf_write_content(&state);
+ if (!state.has_failed)
+ pdf_setup_document(&state);
+ if (!state.has_failed)
+ pdf_write_to_file(&state);
+ pdf_free_resources(&state);
+ return state.has_failed ? -1 : 0;
+}
+