1 /****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2003
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
10 # define DPRINT(x) (void)(printf x, printf("\n"))
17 #include FT_FREETYPE_H
23 /* lines are better drawn on the pixle than between pixles */
24 #define LINEOFFSET 0.5
26 typedef struct gfx_char_s *gfx_char;
28 FT_UInt index; /* glyph index */
29 FT_Vector pos; /* location from baseline in 26.6 */
30 FT_Glyph image; /* glyph bitmap */
33 typedef struct gfx_string_s *gfx_string;
37 size_t count; /* number of characters */
44 /* compute string bbox */
45 static void compute_string_bbox(gfx_string string);
47 /* create a freetype glyph string */
48 gfx_string gfx_string_create ( FT_Face face,
49 const char *text, int rotation);
51 /* create a freetype glyph string */
52 static void gfx_string_destroy ( gfx_string string );
55 gfx_node_t *gfx_new_node( gfx_canvas_t *canvas,enum gfx_en type){
56 gfx_node_t *node = art_new(gfx_node_t,1);
57 if (node == NULL) return NULL;
59 node->color = 0x0; /* color of element 0xRRGGBBAA alpha 0xff is solid*/
60 node->size =0.0; /* font size, line width */
61 node->path = NULL; /* path */
64 node->closed_path = 0;
65 node->svp = NULL; /* svp */
66 node->filename = NULL; /* font or image filename */
69 node->y = 0.0; /* position */
71 node->halign = GFX_H_NULL; /* text alignement */
72 node->valign = GFX_V_NULL; /* text alignement */
75 if (canvas->lastnode != NULL){
76 canvas->lastnode->next = node;
78 if (canvas->firstnode == NULL){
79 canvas->firstnode = node;
81 canvas->lastnode = node;
85 gfx_canvas_t *gfx_new_canvas (void) {
86 gfx_canvas_t *canvas = art_new(gfx_canvas_t,1);
87 canvas->firstnode = NULL;
88 canvas->lastnode = NULL;
89 canvas->imgformat = IF_PNG; /* we default to PNG output */
90 canvas->interlaced = 0;
95 /* create a new line */
96 gfx_node_t *gfx_new_line(gfx_canvas_t *canvas,
99 double width, gfx_color_t color){
100 return gfx_new_dashed_line(canvas, X0, Y0, X1, Y1, width, color, 0, 0);
103 gfx_node_t *gfx_new_dashed_line(gfx_canvas_t *canvas,
104 double X0, double Y0,
105 double X1, double Y1,
106 double width, gfx_color_t color,
107 double dash_on, double dash_off){
111 node = gfx_new_node(canvas,GFX_LINE);
112 if (node == NULL) return NULL;
113 vec = art_new(ArtVpath, 3);
114 if (vec == NULL) return NULL;
115 vec[0].code = ART_MOVETO_OPEN; vec[0].x=X0+LINEOFFSET; vec[0].y=Y0+LINEOFFSET;
116 vec[1].code = ART_LINETO; vec[1].x=X1+LINEOFFSET; vec[1].y=Y1+LINEOFFSET;
117 vec[2].code = ART_END;
120 node->points_max = 3;
123 node->dash_on = dash_on;
124 node->dash_off = dash_off;
129 /* create a new area */
130 gfx_node_t *gfx_new_area (gfx_canvas_t *canvas,
131 double X0, double Y0,
132 double X1, double Y1,
133 double X2, double Y2,
138 node = gfx_new_node(canvas,GFX_AREA);
139 if (node == NULL) return NULL;
140 vec = art_new(ArtVpath, 5);
141 if (vec == NULL) return NULL;
142 vec[0].code = ART_MOVETO; vec[0].x=X0; vec[0].y=Y0;
143 vec[1].code = ART_LINETO; vec[1].x=X1; vec[1].y=Y1;
144 vec[2].code = ART_LINETO; vec[2].x=X2; vec[2].y=Y2;
145 vec[3].code = ART_LINETO; vec[3].x=X0; vec[3].y=Y0;
146 vec[4].code = ART_END;
149 node->points_max = 5;
156 /* add a point to a line or to an area */
157 int gfx_add_point (gfx_node_t *node,
159 if (node == NULL) return 1;
160 if (node->type == GFX_AREA) {
161 double X0 = node->path[0].x;
162 double Y0 = node->path[0].y;
164 art_vpath_add_point (&(node->path),
169 art_vpath_add_point (&(node->path),
174 art_vpath_add_point (&(node->path),
179 } else if (node->type == GFX_LINE) {
181 art_vpath_add_point (&(node->path),
185 x+LINEOFFSET,y+LINEOFFSET);
186 art_vpath_add_point (&(node->path),
193 /* can only add point to areas and lines */
199 void gfx_close_path (gfx_node_t *node) {
200 node->closed_path = 1;
201 if (node->path[0].code == ART_MOVETO_OPEN)
202 node->path[0].code = ART_MOVETO;
205 /* create a text node */
206 gfx_node_t *gfx_new_text (gfx_canvas_t *canvas,
207 double x, double y, gfx_color_t color,
208 char* font, double size,
209 double tabwidth, double angle,
210 enum gfx_h_align_en h_align,
211 enum gfx_v_align_en v_align,
213 gfx_node_t *node = gfx_new_node(canvas,GFX_TEXT);
214 /* if (angle != 0.0){*/
215 /* currently we only support 0 and 270 */
219 node->text = strdup(text);
221 node->filename = strdup(font);
226 node->tabwidth = tabwidth;
227 node->halign = h_align;
228 node->valign = v_align;
230 /* debugging: show text anchor
231 green is along x-axis, red is downward y-axis */
233 double a = 2 * M_PI * -node->angle / 360.0;
234 double cos_a = cos(a);
235 double sin_a = sin(a);
239 x + len * cos_a, y - len * sin_a,
243 x + len * sin_a, y + len * cos_a,
250 int gfx_render(gfx_canvas_t *canvas,
251 art_u32 width, art_u32 height,
252 gfx_color_t background, FILE *fp){
253 switch (canvas->imgformat) {
255 return gfx_render_png (canvas, width, height, background, fp);
257 return gfx_render_svg (canvas, width, height, background, fp);
259 return gfx_render_eps (canvas, width, height, background, fp);
261 return gfx_render_pdf (canvas, width, height, background, fp);
267 static void gfx_string_destroy ( gfx_string string ) {
269 if (string->glyphs) {
270 for (n=0; n<string->num_glyphs; ++n)
271 FT_Done_Glyph (string->glyphs[n].image);
272 free (string->glyphs);
278 double gfx_get_text_width ( gfx_canvas_t *canvas,
279 double start, char* font, double size,
280 double tabwidth, char* text, int rotation){
281 switch (canvas->imgformat) {
283 return gfx_get_text_width_libart (canvas, start, font, size, tabwidth, text, rotation);
284 case IF_SVG: /* fall through */
287 return afm_get_text_width(start, font, size, tabwidth, text);
289 return size * strlen(text);
293 double gfx_get_text_width_libart ( gfx_canvas_t *canvas,
294 double start, char* font, double size,
295 double tabwidth, char* text, int rotation){
300 FT_Library library=NULL;
303 FT_Init_FreeType( &library );
304 error = FT_New_Face( library, font, 0, &face );
305 if ( error ) return -1;
306 error = FT_Set_Char_Size(face, size*64,size*64, 100,100);
307 if ( error ) return -1;
309 string = gfx_string_create( face, text, rotation);
310 text_width = string->width;
311 gfx_string_destroy(string);
312 FT_Done_FreeType(library);
313 return text_width/64;
316 static void gfx_libart_close_path(gfx_node_t *node, ArtVpath **vec)
318 /* libart must have end==start for closed paths,
319 even if using ART_MOVETO and not ART_MOVETO_OPEN
320 so add extra point which is the same as the starting point */
321 int points_max = node->points; /* scaled array has exact size */
322 int points = node->points - 1;
323 art_vpath_add_point (vec, &points, &points_max, ART_LINETO,
324 (**vec).x, (**vec).y);
325 art_vpath_add_point (vec, &points, &points_max, ART_END, 0, 0);
328 static void gfx_round_scaled_coordinates(ArtVpath *vec)
330 while (vec->code != ART_END) {
331 vec->x = floor(vec->x - LINEOFFSET + 0.5) + LINEOFFSET;
332 vec->y = floor(vec->y - LINEOFFSET + 0.5) + LINEOFFSET;
337 /* find bbox of a string */
338 static void compute_string_bbox(gfx_string string) {
342 bbox.xMin = bbox.yMin = 32000;
343 bbox.xMax = bbox.yMax = -32000;
344 for ( n = 0; n < string->num_glyphs; n++ ) {
346 FT_Glyph_Get_CBox( string->glyphs[n].image, ft_glyph_bbox_gridfit,
348 if (glyph_bbox.xMin < bbox.xMin) {
349 bbox.xMin = glyph_bbox.xMin;
351 if (glyph_bbox.yMin < bbox.yMin) {
352 bbox.yMin = glyph_bbox.yMin;
354 if (glyph_bbox.xMax > bbox.xMax) {
355 bbox.xMax = glyph_bbox.xMax;
357 if (glyph_bbox.yMax > bbox.yMax) {
358 bbox.yMax = glyph_bbox.yMax;
361 if ( bbox.xMin > bbox.xMax ) {
367 string->bbox.xMin = bbox.xMin;
368 string->bbox.xMax = bbox.xMax;
369 string->bbox.yMin = bbox.yMin;
370 string->bbox.yMax = bbox.yMax;
373 /* create a free type glyph string */
374 gfx_string gfx_string_create(FT_Face face,const char *text,
378 FT_GlyphSlot slot = face->glyph; /* a small shortcut */
384 gfx_char glyph; /* current glyph in table */
388 ft_pen.x = 0; /* start at (0,0) !! */
391 string = (gfx_string) malloc (sizeof(struct gfx_string_s));
394 string->count = strlen (text);
395 string->glyphs = (gfx_char) calloc (string->count,sizeof(struct gfx_char_s));
396 string->num_glyphs = 0;
397 string->transform.xx = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
398 string->transform.xy = (FT_Fixed)(-sin(M_PI*(rotation)/180.0)*0x10000);
399 string->transform.yx = (FT_Fixed)( sin(M_PI*(rotation)/180.0)*0x10000);
400 string->transform.yy = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
402 use_kerning = FT_HAS_KERNING(face);
404 glyph = string->glyphs;
405 for (n=0; n<string->count; n++, glyph++) {
408 /* initialize each struct gfx_char_s */
414 glyph->index = FT_Get_Char_Index( face, text[n] );
416 /* compute glyph origin */
417 if ( use_kerning && previous && glyph->index ) {
419 FT_Get_Kerning (face, previous, glyph->index,
420 ft_kerning_default, &kerning);
421 ft_pen.x += kerning.x;
422 ft_pen.y += kerning.y;
425 /* store current pen position */
426 glyph->pos.x = ft_pen.x;
427 glyph->pos.y = ft_pen.y;
429 /* load the glyph image (in its native format) */
430 /* for now, we take a monochrome glyph bitmap */
431 error = FT_Load_Glyph (face, glyph->index, FT_LOAD_DEFAULT);
433 fprintf (stderr, "couldn't load glyph: %c\n", text[n]);
436 error = FT_Get_Glyph (slot, &glyph->image);
438 fprintf (stderr, "couldn't get glyph from slot: %c\n", text[n]);
442 ft_pen.x += slot->advance.x;
443 ft_pen.y += slot->advance.y;
447 FT_Vector_Transform (&vec, &string->transform);
448 error = FT_Glyph_Transform (glyph->image, &string->transform, &vec);
450 fprintf (stderr, "couldn't transform glyph\n");
454 /* convert to a bitmap - destroy native image */
455 error = FT_Glyph_To_Bitmap (&glyph->image, ft_render_mode_normal, 0, 1);
457 fprintf (stderr, "couldn't convert glyph to bitmap\n");
461 /* increment number of glyphs */
462 previous = glyph->index;
463 string->num_glyphs++;
465 /* printf ("number of glyphs = %d\n", string->num_glyphs);*/
466 compute_string_bbox( string );
467 string->width = string->bbox.xMax - string->bbox.xMin;
468 string->height = string->bbox.yMax - string->bbox.yMin;
474 static int gfx_save_png (art_u8 *buffer, FILE *fp,
475 long width, long height, long bytes_per_pixel);
476 /* render grafics into png image */
478 int gfx_render_png (gfx_canvas_t *canvas,
479 art_u32 width, art_u32 height,
480 gfx_color_t background, FILE *fp){
484 gfx_node_t *node = canvas->firstnode;
485 art_u8 red = background >> 24, green = (background >> 16) & 0xff;
486 art_u8 blue = (background >> 8) & 0xff, alpha = ( background & 0xff );
487 unsigned long pys_width = width * canvas->zoom;
488 unsigned long pys_height = height * canvas->zoom;
489 const int bytes_per_pixel = 3;
490 unsigned long rowstride = pys_width*bytes_per_pixel; /* bytes per pixel */
491 art_u8 *buffer = art_new (art_u8, rowstride*pys_height);
492 art_rgb_run_alpha (buffer, red, green, blue, alpha, pys_width*pys_height);
493 FT_Init_FreeType( &library );
495 switch (node->type) {
501 art_affine_scale(dst,canvas->zoom,canvas->zoom);
502 vec = art_vpath_affine_transform(node->path,dst);
503 if (node->closed_path)
504 gfx_libart_close_path(node, &vec);
505 gfx_round_scaled_coordinates(vec);
506 if(node->type == GFX_LINE){
507 svp = art_svp_vpath_stroke ( vec, ART_PATH_STROKE_JOIN_ROUND,
508 ART_PATH_STROKE_CAP_ROUND,
509 node->size*canvas->zoom,1,1);
511 svp = art_svp_from_vpath ( vec );
514 art_rgb_svp_alpha (svp ,0,0, pys_width, pys_height,
515 node->color, buffer, rowstride, NULL);
522 art_u8 fcolor[3],falpha;
526 FT_Vector vec; /* 26.6 */
528 float pen_x = 0.0 , pen_y = 0.0;
532 fcolor[0] = node->color >> 24;
533 fcolor[1] = (node->color >> 16) & 0xff;
534 fcolor[2] = (node->color >> 8) & 0xff;
535 falpha = node->color & 0xff;
536 error = FT_New_Face( library,
537 (char *)node->filename,
542 error = FT_Set_Char_Size(face, /* handle to face object */
543 (long)(node->size*64),
544 (long)(node->size*64),
545 (long)(100*canvas->zoom),
546 (long)(100*canvas->zoom));
548 pen_x = node->x * canvas->zoom;
549 pen_y = node->y * canvas->zoom;
551 string = gfx_string_create (face, node->text, node->angle);
552 switch(node->halign){
553 case GFX_H_RIGHT: vec.x = -string->bbox.xMax;
555 case GFX_H_CENTER: vec.x = abs(string->bbox.xMax) >= abs(string->bbox.xMin) ?
556 -string->bbox.xMax/2:-string->bbox.xMin/2;
558 case GFX_H_LEFT: vec.x = -string->bbox.xMin;
560 case GFX_H_NULL: vec.x = 0;
564 switch(node->valign){
565 case GFX_V_TOP: vec.y = string->bbox.yMax;
567 case GFX_V_CENTER: vec.y = abs(string->bbox.yMax) >= abs(string->bbox.yMin) ?
568 string->bbox.yMax/2:string->bbox.yMin/2;
570 case GFX_V_BOTTOM: vec.y = 0;
572 case GFX_V_NULL: vec.y = 0;
577 glyph = string->glyphs;
578 for(n=0; n<string->num_glyphs; ++n, ++glyph) {
583 /* make copy to transform */
584 if (! glyph->image) {
585 fprintf (stderr, "no image\n");
588 error = FT_Glyph_Copy (glyph->image, &image);
590 fprintf (stderr, "couldn't copy image\n");
596 FT_Vector_Transform (&vec, &string->transform);
598 bit = (FT_BitmapGlyph) image;
600 gr = bit->bitmap.num_grays -1;
601 for (iy=0; iy < bit->bitmap.rows; iy++){
602 long buf_y = iy+(pen_y+0.5)-bit->top;
603 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
605 for (ix=0;ix < bit->bitmap.width;ix++){
606 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
609 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
610 buf_x *= bytes_per_pixel ;
611 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
612 font_alpha = (art_u8)((double)font_alpha / gr * falpha);
613 for (iz = 0; iz < 3; iz++){
614 art_u8 *orig = buffer + buf_y + buf_x + iz;
615 *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
616 (double)fcolor[iz] / gr * (font_alpha));
620 FT_Done_Glyph (image);
622 gfx_string_destroy(string);
627 gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
629 FT_Done_FreeType( library );
633 /* free memory used by nodes this will also remove memory required for
634 associated paths and svcs ... but not for text strings */
636 gfx_destroy (gfx_canvas_t *canvas){
637 gfx_node_t *next,*node = canvas->firstnode;
640 art_free(node->path);
643 free(node->filename);
650 static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
651 png_structp png_ptr = NULL;
652 png_infop info_ptr = NULL;
654 png_bytep *row_pointers;
655 int rowstride = width * bytes_per_pixel;
661 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
666 row_pointers = (png_bytepp)png_malloc(png_ptr,
667 height*sizeof(png_bytep));
669 info_ptr = png_create_info_struct(png_ptr);
671 if (info_ptr == NULL)
673 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
677 if (setjmp(png_jmpbuf(png_ptr)))
679 /* If we get here, we had a problem writing the file */
680 png_destroy_write_struct(&png_ptr, &info_ptr);
684 png_init_io(png_ptr, fp);
685 png_set_IHDR (png_ptr, info_ptr,width, height,
686 8, PNG_COLOR_TYPE_RGB,
688 PNG_COMPRESSION_TYPE_DEFAULT,
689 PNG_FILTER_TYPE_DEFAULT);
691 text[0].key = "Software";
692 text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
693 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
694 png_set_text (png_ptr, info_ptr, text, 1);
696 /* Write header data */
697 png_write_info (png_ptr, info_ptr);
699 for (i = 0; i < height; i++)
700 row_pointers[i] = (png_bytep) (buffer + i*rowstride);
702 png_write_image(png_ptr, row_pointers);
703 png_write_end(png_ptr, info_ptr);
704 png_destroy_write_struct(&png_ptr, &info_ptr);
709 /* ------- SVG -------
711 http://www.w3.org/TR/SVG/
713 static int svg_indent = 0;
714 static int svg_single_line = 0;
715 static const char *svg_default_font = "Helvetica";
716 typedef struct svg_dash
719 double dash_adjust, dash_len, dash_offset;
720 double adjusted_on, adjusted_off;
724 static void svg_print_indent(FILE *fp)
727 for (i = svg_indent - svg_single_line; i > 0; i--) {
733 static void svg_start_tag(FILE *fp, const char *name)
735 svg_print_indent(fp);
741 static void svg_close_tag_single_line(FILE *fp)
747 static void svg_close_tag(FILE *fp)
750 if (!svg_single_line)
754 static void svg_end_tag(FILE *fp, const char *name)
756 /* name is NULL if closing empty-node tag */
761 svg_print_indent(fp);
771 static void svg_close_tag_empty_node(FILE *fp)
773 svg_end_tag(fp, NULL);
776 static void svg_write_text(FILE *fp, const char *text)
778 const unsigned char *p, *start, *last;
780 p = (const unsigned char*)text;
783 /* trim leading spaces */
787 /* trim trailing spaces */
789 while ((ch = *p) != 0) {
794 /* encode trimmed text */
798 ch = afm_host2unicode(ch); /* unsafe macro */
800 case '&': fputs("&", fp); break;
801 case '<': fputs("<", fp); break;
802 case '>': fputs(">", fp); break;
803 case '"': fputs(""", fp); break;
806 fprintf(fp, "&#%d;", ch);
813 static void svg_format_number(char *buf, int bufsize, double d)
815 /* omit decimals if integer to reduce filesize */
817 snprintf(buf, bufsize, "%.2f", d);
818 p = buf; /* doesn't trust snprintf return value */
824 *p = '\0'; /* zap trailing zeros */
828 *p = '\0'; /* zap trailing dot */
833 static void svg_write_number(FILE *fp, double d)
836 svg_format_number(buf, sizeof(buf), d);
840 static int svg_color_is_black(int c)
842 /* gfx_color_t is RRGGBBAA */
843 return c == 0x000000FF;
846 static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
848 /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
849 gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
850 gfx_color_t opacity = c & 0xFF;
851 fprintf(fp, " %s=\"", attr);
852 if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
853 /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
854 fprintf(fp, "#%03lX",
855 ( ((rrggbb >> 8) & 0xF00)
856 | ((rrggbb >> 4) & 0x0F0)
857 | ( rrggbb & 0x00F)));
859 fprintf(fp, "#%06lX", rrggbb);
862 if (opacity != 0xFF) {
863 fprintf(fp, " stroke-opacity=\"");
864 svg_write_number(fp, opacity / 255.0);
869 static void svg_get_dash(gfx_node_t *node, svg_dash *d)
873 if (node->dash_on <= 0 || node->dash_off <= 0) {
878 d->dash_len = node->dash_on + node->dash_off;
879 /* dash on/off adjustment due to round caps */
880 d->dash_adjust = 0.8 * node->size;
881 d->adjusted_on = node->dash_on - d->dash_adjust;
882 if (d->adjusted_on < 0.01)
883 d->adjusted_on = 0.01;
884 d->adjusted_off = d->dash_len - d->adjusted_on;
885 /* dash offset calc */
886 if (node->path[0].x == node->path[1].x) /* only good for horz/vert lines */
887 offset = node->path[0].y;
889 offset = node->path[0].x;
890 mult = (int)fabs(offset / d->dash_len);
891 d->dash_offset = offset - mult * d->dash_len;
892 if (node->path[0].x < node->path[1].x || node->path[0].y < node->path[1].y)
893 d->dash_offset = d->dash_len - d->dash_offset;
896 static int svg_dash_equal(svg_dash *a, svg_dash *b)
898 if (a->dash_enable != b->dash_enable)
900 if (a->adjusted_on != b->adjusted_on)
902 if (a->adjusted_off != b->adjusted_off)
904 /* rest of properties will be the same when on+off are */
908 static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
911 svg_get_dash(node, &dash_info);
912 fputs(" stroke-width=\"", fp);
913 svg_write_number(fp, node->size);
915 svg_write_color(fp, node->color, "stroke");
916 fputs(" fill=\"none\"", fp);
917 if (dash_info.dash_enable) {
918 if (dash_info.dash_offset != 0) {
919 fputs(" stroke-dashoffset=\"", fp);
920 svg_write_number(fp, dash_info.dash_offset);
923 fputs(" stroke-dasharray=\"", fp);
924 svg_write_number(fp, dash_info.adjusted_on);
926 svg_write_number(fp, dash_info.adjusted_off);
931 static int svg_is_int_step(double a, double b)
933 double diff = fabs(a - b);
934 return floor(diff) == diff;
937 static int svg_path_straight_segment(FILE *fp,
938 double lastA, double currentA, double currentB,
940 int segment_idx, int isx, char absChar, char relChar)
942 if (!svg_is_int_step(lastA, currentA)) {
944 svg_write_number(fp, currentA);
947 if (segment_idx < node->points - 1) {
948 ArtVpath *vec = node->path + segment_idx + 1;
949 if (vec->code == ART_LINETO) {
950 double nextA = (isx ? vec->x : vec->y) - LINEOFFSET;
951 double nextB = (isx ? vec->y : vec->x) - LINEOFFSET;
952 if (nextB == currentB
953 && ((currentA >= lastA) == (nextA >= currentA))
954 && svg_is_int_step(currentA, nextA)) {
955 return 1; /* skip to next as it is a straight line */
960 svg_write_number(fp, currentA - lastA);
964 static void svg_path(FILE *fp, gfx_node_t *node, int multi)
967 double lastX = 0, lastY = 0;
968 /* for straight lines <path..> tags take less space than
969 <line..> tags because of the efficient packing
970 in the 'd' attribute */
971 svg_start_tag(fp, "path");
973 svg_common_path_attributes(fp, node);
975 /* specification of the 'd' attribute: */
976 /* http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation */
977 for (i = 0; i < node->points; i++) {
978 ArtVpath *vec = node->path + i;
979 double x = vec->x - LINEOFFSET;
980 double y = vec->y - LINEOFFSET;
982 case ART_MOVETO_OPEN: /* fall-through */
985 svg_write_number(fp, x);
987 svg_write_number(fp, y);
990 /* try optimize filesize by using minimal lineto commands */
991 /* without introducing rounding errors. */
993 if (svg_path_straight_segment(fp, lastY, y, x, node, i, 0, 'V', 'v'))
995 } else if (y == lastY) {
996 if (svg_path_straight_segment(fp, lastX, x, y, node, i, 1, 'H', 'h'))
1000 svg_write_number(fp, x);
1002 svg_write_number(fp, y);
1005 case ART_CURVETO: break; /* unsupported */
1006 case ART_END: break; /* nop */
1011 if (node->closed_path)
1014 svg_close_tag_empty_node(fp);
1017 static void svg_multi_path(FILE *fp, gfx_node_t **nodeR)
1019 /* optimize for multiple paths with the same color, penwidth, etc. */
1021 gfx_node_t *node = *nodeR;
1022 gfx_node_t *next = node->next;
1024 if (next->type != node->type
1025 || next->size != node->size
1026 || next->color != node->color
1027 || next->dash_on != node->dash_on
1028 || next->dash_off != node->dash_off)
1034 svg_path(fp, node, 0);
1037 svg_start_tag(fp, "g");
1038 svg_common_path_attributes(fp, node);
1040 while (num && node) {
1041 svg_path(fp, node, 1);
1047 svg_end_tag(fp, "g");
1050 static void svg_area(FILE *fp, gfx_node_t *node)
1053 double startX = 0, startY = 0;
1054 svg_start_tag(fp, "polygon");
1056 svg_write_color(fp, node->color, "fill");
1057 fputs(" points=\"", fp);
1058 for (i = 0; i < node->points; i++) {
1059 ArtVpath *vec = node->path + i;
1060 double x = vec->x - LINEOFFSET;
1061 double y = vec->y - LINEOFFSET;
1062 switch (vec->code) {
1063 case ART_MOVETO_OPEN: /* fall-through */
1065 svg_write_number(fp, x);
1067 svg_write_number(fp, y);
1072 if (i == node->points - 2
1073 && node->path[i + 1].code == ART_END
1074 && fabs(x - startX) < 0.001 && fabs(y - startY) < 0.001) {
1075 break; /* poly area always closed, no need for last point */
1078 svg_write_number(fp, x);
1080 svg_write_number(fp, y);
1082 case ART_CURVETO: break; /* unsupported */
1083 case ART_END: break; /* nop */
1087 svg_close_tag_empty_node(fp);
1090 static void svg_text(FILE *fp, gfx_node_t *node)
1092 double x = node->x - LINEOFFSET;
1093 double y = node->y - LINEOFFSET;
1094 if (node->angle != 0) {
1095 svg_start_tag(fp, "g");
1096 fputs(" transform=\"translate(", fp);
1097 svg_write_number(fp, x);
1099 svg_write_number(fp, y);
1100 fputs(") rotate(", fp);
1101 svg_write_number(fp, node->angle);
1106 switch (node->valign) {
1107 case GFX_V_TOP: y += node->size; break;
1108 case GFX_V_CENTER: y += node->size / 3; break;
1109 case GFX_V_BOTTOM: break;
1110 case GFX_V_NULL: break;
1112 svg_start_tag(fp, "text");
1114 svg_write_number(fp, x);
1115 fputs("\" y=\"", fp);
1116 svg_write_number(fp, y);
1118 /* if (strcmp(node->filename, svg_default_font))
1119 fprintf(fp, " font-family=\"%s\"", node->filename);
1121 fputs("\" font-family=\"Helvetica", fp);
1122 fputs("\" font-size=\"", fp);
1123 svg_write_number(fp, node->size);
1125 if (!svg_color_is_black(node->color))
1126 svg_write_color(fp, node->color, "fill");
1127 switch (node->halign) {
1128 case GFX_H_RIGHT: fputs(" text-anchor=\"end\"", fp); break;
1129 case GFX_H_CENTER: fputs(" text-anchor=\"middle\"", fp); break;
1130 case GFX_H_LEFT: break;
1131 case GFX_H_NULL: break;
1133 svg_close_tag_single_line(fp);
1134 /* support for node->tabwidth missing */
1135 svg_write_text(fp, node->text);
1136 svg_end_tag(fp, "text");
1137 if (node->angle != 0)
1138 svg_end_tag(fp, "g");
1141 int gfx_render_svg (gfx_canvas_t *canvas,
1142 art_u32 width, art_u32 height,
1143 gfx_color_t background, FILE *fp){
1144 gfx_node_t *node = canvas->firstnode;
1146 "<?xml version=\"1.0\" standalone=\"no\"?>\n"
1147 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
1148 " \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
1150 " SVG file created by RRDtool,\n"
1151 " Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch\n"
1153 " The width/height attributes in the outhermost svg node\n"
1154 " are just default sizes for the browser which is used\n"
1155 " if the svg file is openened directly without being\n"
1156 " embedded in an html file.\n"
1157 " The viewBox is the local coord system for rrdtool.\n"
1159 svg_start_tag(fp, "svg");
1160 fputs(" width=\"", fp);
1161 svg_write_number(fp, width * canvas->zoom);
1162 fputs("\" height=\"", fp);
1163 svg_write_number(fp, height * canvas->zoom);
1164 fputs("\" x=\"0\" y=\"0\" viewBox=\"", fp);
1165 svg_write_number(fp, -LINEOFFSET);
1167 svg_write_number(fp, -LINEOFFSET);
1169 svg_write_number(fp, width - LINEOFFSET);
1171 svg_write_number(fp, height - LINEOFFSET);
1172 fputs("\" preserveAspectRatio=\"xMidYMid\"", fp);
1173 fprintf(fp, " font-family=\"%s\"", svg_default_font); /* default font */
1174 fputs(" stroke-linecap=\"round\" stroke-linejoin=\"round\"", fp);
1176 svg_start_tag(fp, "rect");
1177 fprintf(fp, " x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"", width, height);
1178 svg_write_color(fp, background, "fill");
1179 svg_close_tag_empty_node(fp);
1181 switch (node->type) {
1183 svg_multi_path(fp, &node);
1193 svg_end_tag(fp, "svg");
1197 /* ------- EPS -------
1198 EPS and Postscript references:
1199 http://partners.adobe.com/asn/developer/technotes/postscript.html
1202 typedef struct eps_font
1204 const char *ps_font;
1206 struct eps_font *next;
1209 typedef struct eps_state
1212 gfx_canvas_t *canvas;
1213 art_u32 page_width, page_height;
1214 eps_font *font_list;
1220 int linecap, linejoin;
1224 static void eps_set_color(eps_state *state, gfx_color_t color)
1226 /* gfx_color_t is RRGGBBAA */
1227 if (state->color == color)
1229 fprintf(state->fp, "%d %d %d Rgb\n",
1230 (int)((color >> 24) & 255),
1231 (int)((color >> 16) & 255),
1232 (int)((color >> 8) & 255));
1233 state->color = color;
1236 static int eps_add_font(eps_state *state, gfx_node_t *node)
1238 /* The fonts list could be postponed to the end using
1239 (atend), but let's be nice and have them in the header. */
1240 const char *ps_font = afm_get_font_postscript_name(node->filename);
1242 for (ef = state->font_list; ef; ef = ef->next) {
1243 if (!strcmp(ps_font, ef->ps_font))
1246 ef = malloc(sizeof(eps_font));
1248 rrd_set_error("malloc for eps_font");
1251 ef->next = state->font_list;
1252 ef->ps_font = ps_font;
1253 state->font_list = ef;
1257 static void eps_list_fonts(eps_state *state, const char *dscName)
1260 int lineLen = strlen(dscName);
1261 if (!state->font_list)
1263 fputs(dscName, state->fp);
1264 for (ef = state->font_list; ef; ef = ef->next) {
1265 int nameLen = strlen(ef->ps_font);
1266 if (lineLen + nameLen > 100 && lineLen) {
1267 fputs("\n", state->fp);
1268 fputs("%%- \n", state->fp);
1271 fputs(" ", state->fp);
1274 fputs(ef->ps_font, state->fp);
1277 fputs("\n", state->fp);
1280 static void eps_define_fonts(eps_state *state)
1283 if (!state->font_list)
1285 for (ef = state->font_list; ef; ef = ef->next) {
1286 /* PostScript¨ LANGUAGE REFERENCE third edition
1290 "/%s findfont dup length dict begin\n"
1291 "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
1292 "/Encoding ISOLatin1Encoding def\n"
1294 "/%s-ISOLatin1 exch definefont pop\n"
1295 "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
1296 ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
1300 static int eps_prologue(eps_state *state)
1304 "%!PS-Adobe-3.0 EPSF-3.0\n"
1305 "%%Creator: RRDtool 1.1.x, Tobias Oetiker, http://tobi.oetiker.ch\n"
1306 /* can't like weird chars here */
1307 "%%Title: (RRDtool output)\n"
1308 "%%DocumentData: Clean7Bit\n"
1310 fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
1311 state->page_width, state->page_height);
1312 for (node = state->canvas->firstnode; node; node = node->next) {
1313 if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
1316 eps_list_fonts(state, "%%DocumentFonts:");
1317 eps_list_fonts(state, "%%DocumentNeededFonts:");
1321 "%%EndProlog\n" /* must have, or BoundingBox is ignored */
1322 "/bd { bind def } bind def\n"
1324 fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
1326 "/X2 {X exch X exch} bd\n"
1327 "/M {X2 moveto} bd\n"
1328 "/L {X2 lineto} bd\n"
1332 "/CP {closepath} bd\n"
1333 "/WS {setlinewidth stroke} bd\n"
1336 "/TaC {dup stringwidth pop neg 2 div 0 rmoveto } bd\n"
1337 "/TaR {dup stringwidth pop neg 0 rmoveto } bd\n"
1338 "/TL {moveto TaL show} bd\n"
1339 "/TC {moveto TaC show} bd\n"
1340 "/TR {moveto TaR show} bd\n"
1341 "/Rgb { 255.0 div 3 1 roll\n"
1342 " 255.0 div 3 1 roll \n"
1343 " 255.0 div 3 1 roll setrgbcolor } bd\n"
1345 eps_define_fonts(state);
1349 static void eps_clear_dash(eps_state *state)
1351 if (!state->has_dash)
1353 state->has_dash = 0;
1354 fputs("[1 0] 0 setdash\n", state->fp);
1357 static void eps_write_linearea(eps_state *state, gfx_node_t *node)
1360 FILE *fp = state->fp;
1362 int clearDashIfAny = 1;
1363 eps_set_color(state, node->color);
1364 if (node->type == GFX_LINE) {
1366 if (state->linecap != 1) {
1367 fputs("1 setlinecap\n", fp);
1370 if (state->linejoin != 1) {
1371 fputs("1 setlinejoin\n", fp);
1372 state->linejoin = 1;
1374 svg_get_dash(node, &dash_info);
1375 if (dash_info.dash_enable) {
1377 state->has_dash = 1;
1379 svg_write_number(fp, dash_info.adjusted_on);
1381 svg_write_number(fp, dash_info.adjusted_off);
1383 svg_write_number(fp, dash_info.dash_offset);
1384 fputs(" setdash\n", fp);
1388 eps_clear_dash(state);
1389 for (i = 0; i < node->points; i++) {
1390 ArtVpath *vec = node->path + i;
1392 double y = state->page_height - vec->y;
1393 if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
1394 useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
1399 switch (vec->code) {
1400 case ART_MOVETO_OPEN: /* fall-through */
1402 svg_write_number(fp, x);
1404 svg_write_number(fp, y);
1406 fputs(useOffset ? "M\n" : "m\n", fp);
1409 svg_write_number(fp, x);
1411 svg_write_number(fp, y);
1413 fputs(useOffset ? "L\n" : "l\n", fp);
1415 case ART_CURVETO: break; /* unsupported */
1416 case ART_END: break; /* nop */
1419 if (node->type == GFX_LINE) {
1420 if (node->closed_path)
1422 if (node->size != state->line_width) {
1423 state->line_width = node->size;
1424 svg_write_number(fp, state->line_width);
1434 static void eps_write_text(eps_state *state, gfx_node_t *node)
1436 FILE *fp = state->fp;
1437 const unsigned char *p;
1438 const char *ps_font = afm_get_font_postscript_name(node->filename);
1441 double y = state->page_height - node->y, ydelta = 0;
1443 eps_set_color(state, node->color);
1444 if (strcmp(ps_font, state->font) || node->size != state->font_size) {
1445 state->font = ps_font;
1446 state->font_size = node->size;
1447 svg_write_number(fp, state->font_size);
1448 fprintf(fp, " SetFont-%s\n", state->font);
1452 for (p = (const unsigned char*)node->text; *p; p++) {
1454 fputs("\\\n", fp); /* backslash and \n */
1469 fprintf(fp, "\\%03o", *p);
1476 switch(node->valign){
1477 case GFX_V_TOP: ydelta = -node->size; break;
1478 case GFX_V_CENTER: ydelta = -node->size / 3.0; break;
1479 case GFX_V_BOTTOM: break;
1480 case GFX_V_NULL: break;
1482 if (node->angle == 0)
1484 switch (node->halign) {
1485 case GFX_H_RIGHT: align = 'R'; break;
1486 case GFX_H_CENTER: align = 'C'; break;
1487 case GFX_H_LEFT: align= 'L'; break;
1488 case GFX_H_NULL: align= 'L'; break;
1490 if (node->angle != 0) {
1492 fputs(" gsave ", fp);
1493 svg_write_number(fp, x);
1495 svg_write_number(fp, y);
1496 fputs(" translate ", fp);
1497 svg_write_number(fp, -node->angle);
1498 fputs(" rotate 0 ", fp);
1499 svg_write_number(fp, ydelta);
1500 fputs(" moveto ", fp);
1501 fprintf(fp, "Ta%c", align);
1502 fputs(" show grestore\n", fp);
1504 svg_write_number(fp, x);
1506 svg_write_number(fp, y);
1513 static int eps_write_content(eps_state *state)
1516 fputs("%\n", state->fp);
1517 for (node = state->canvas->firstnode; node; node = node->next) {
1518 switch (node->type) {
1521 eps_write_linearea(state, node);
1524 eps_write_text(state, node);
1531 int gfx_render_eps (gfx_canvas_t *canvas,
1532 art_u32 width, art_u32 height,
1533 gfx_color_t background, FILE *fp){
1534 struct eps_state state;
1536 state.canvas = canvas;
1537 state.page_width = width;
1538 state.page_height = height;
1539 state.font = "no-default-font";
1540 state.font_size = -1;
1541 state.color = 0; /* black */
1542 state.font_list = NULL;
1544 state.linejoin = -1;
1546 if (eps_prologue(&state) == -1)
1548 eps_set_color(&state, background);
1549 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
1550 height, width, height, width);
1551 if (eps_write_content(&state) == -1)
1553 fputs("showpage\n", fp);
1554 fputs("%%EOF\n", fp);
1555 while (state.font_list) {
1556 eps_font *next = state.font_list->next;
1557 free(state.font_list);
1558 state.font_list = next;
1563 /* ------- PDF -------
1564 PDF references page:
1565 http://partners.adobe.com/asn/developer/technotes/acrobatpdf.html
1568 typedef struct pdf_buffer
1570 int id, is_obj, is_dict, is_stream, pdf_file_pos;
1572 int alloc_size, current_size;
1573 struct pdf_buffer *previous_buffer, *next_buffer;
1574 struct pdf_state *state;
1577 typedef struct pdf_font
1579 const char *ps_font;
1581 struct pdf_font *next;
1584 typedef struct pdf_state
1587 gfx_canvas_t *canvas;
1588 art_u32 page_width, page_height;
1589 pdf_font *font_list;
1590 pdf_buffer *first_buffer, *last_buffer;
1594 gfx_color_t stroke_color, fill_color;
1599 int linecap, linejoin;
1602 pdf_buffer pdf_header;
1603 pdf_buffer catalog_obj, pages_obj, page1_obj;
1604 pdf_buffer fontsdict_obj;
1605 pdf_buffer graph_stream;
1608 static void pdf_init_buffer(pdf_state *state, pdf_buffer *buf)
1610 int initial_size = 32;
1613 buf->alloc_size = 0;
1614 buf->current_size = 0;
1615 buf->data = (char*)malloc(initial_size);
1617 buf->previous_buffer = NULL;
1618 buf->next_buffer = NULL;
1619 if (buf->data == NULL) {
1620 rrd_set_error("malloc for pdf_buffer data");
1621 state->has_failed = 1;
1624 buf->alloc_size = initial_size;
1625 if (state->last_buffer)
1626 state->last_buffer->next_buffer = buf;
1627 if (state->first_buffer == NULL)
1628 state->first_buffer = buf;
1629 buf->previous_buffer = state->last_buffer;
1630 state->last_buffer = buf;
1633 static void pdf_put(pdf_buffer *buf, const char *text, int len)
1637 if (buf->alloc_size < buf->current_size + len) {
1638 int new_size = buf->alloc_size;
1640 while (new_size < buf->current_size + len)
1642 new_buf = (char*)malloc(new_size);
1643 if (new_buf == NULL) {
1644 rrd_set_error("re-malloc for pdf_buffer data");
1645 buf->state->has_failed = 1;
1648 memcpy(new_buf, buf->data, buf->current_size);
1650 buf->data = new_buf;
1651 buf->alloc_size = new_size;
1653 memcpy(buf->data + buf->current_size, text, len);
1654 buf->current_size += len;
1657 static void pdf_puts(pdf_buffer *buf, const char *text)
1659 pdf_put(buf, text, strlen(text));
1662 static void pdf_indent(pdf_buffer *buf)
1664 pdf_puts(buf, "\t");
1667 static void pdf_putsi(pdf_buffer *buf, const char *text)
1670 pdf_puts(buf, text);
1673 static void pdf_putint(pdf_buffer *buf, int i)
1676 sprintf(tmp, "%d", i);
1680 static void pdf_putnumber(pdf_buffer *buf, double d)
1683 svg_format_number(tmp, sizeof(tmp), d);
1687 static void pdf_init_object(pdf_state *state, pdf_buffer *buf)
1689 pdf_init_buffer(state, buf);
1690 buf->id = ++state->last_obj_id;
1695 static void pdf_init_dict(pdf_state *state, pdf_buffer *buf)
1697 pdf_init_object(state, buf);
1701 static void pdf_set_color(pdf_buffer *buf, gfx_color_t color,
1702 gfx_color_t *current_color, const char *op)
1704 /* gfx_color_t is RRGGBBAA */
1705 if (*current_color == color)
1707 pdf_putnumber(buf, ((color >> 24) & 255) / 255.0);
1709 pdf_putnumber(buf, ((color >> 16) & 255) / 255.0);
1711 pdf_putnumber(buf, ((color >> 8) & 255) / 255.0);
1714 pdf_puts(buf, "\n");
1715 *current_color = color;
1718 static void pdf_set_stroke_color(pdf_buffer *buf, gfx_color_t color)
1720 pdf_set_color(buf, color, &buf->state->stroke_color, "RG");
1723 static void pdf_set_fill_color(pdf_buffer *buf, gfx_color_t color)
1725 pdf_set_color(buf, color, &buf->state->fill_color, "rg");
1728 static pdf_font *pdf_find_font(pdf_state *state, gfx_node_t *node)
1730 const char *ps_font = afm_get_font_postscript_name(node->filename);
1732 for (ef = state->font_list; ef; ef = ef->next) {
1733 if (!strcmp(ps_font, ef->ps_font))
1739 static void pdf_add_font(pdf_state *state, gfx_node_t *node)
1741 pdf_font *ef = pdf_find_font(state, node);
1744 ef = malloc(sizeof(pdf_font));
1746 rrd_set_error("malloc for pdf_font");
1747 state->has_failed = 1;
1750 pdf_init_dict(state, &ef->obj);
1751 ef->next = state->font_list;
1752 ef->ps_font = afm_get_font_postscript_name(node->filename);
1753 state->font_list = ef;
1755 pdf_putsi(&state->fontsdict_obj, "/F");
1756 pdf_putint(&state->fontsdict_obj, ef->obj.id);
1757 pdf_puts(&state->fontsdict_obj, " ");
1758 pdf_putint(&state->fontsdict_obj, ef->obj.id);
1759 pdf_puts(&state->fontsdict_obj, " 0 R\n");
1761 pdf_putsi(&ef->obj, "/Type /Font\n");
1762 pdf_putsi(&ef->obj, "/Subtype /Type1\n");
1763 pdf_putsi(&ef->obj, "/Name /F");
1764 pdf_putint(&ef->obj, ef->obj.id);
1765 pdf_puts(&ef->obj, "\n");
1766 pdf_putsi(&ef->obj, "/BaseFont /");
1767 pdf_puts(&ef->obj, ef->ps_font);
1768 pdf_puts(&ef->obj, "\n");
1769 pdf_putsi(&ef->obj, "/Encoding /WinAnsiEncoding\n");
1770 /* 'Cp1252' (this is latin 1 extended with 27 characters;
1771 the encoding is also known as 'winansi')
1772 http://www.lowagie.com/iText/tutorial/ch09.html */
1775 static void pdf_create_fonts(pdf_state *state)
1778 for (node = state->canvas->firstnode; node; node = node->next) {
1779 if (node->type == GFX_TEXT)
1780 pdf_add_font(state, node);
1784 static void pdf_write_linearea(pdf_state *state, gfx_node_t *node)
1787 pdf_buffer *s = &state->graph_stream;
1788 if (node->type == GFX_LINE) {
1790 svg_get_dash(node, &dash_info);
1791 if (!svg_dash_equal(&dash_info, &state->dash)) {
1792 state->dash = dash_info;
1793 if (dash_info.dash_enable) {
1795 pdf_putnumber(s, dash_info.adjusted_on);
1797 pdf_putnumber(s, dash_info.adjusted_off);
1799 pdf_putnumber(s, dash_info.dash_offset);
1800 pdf_puts(s, " d\n");
1802 pdf_puts(s, "[] 0 d\n");
1805 pdf_set_stroke_color(s, node->color);
1806 if (state->linecap != 1) {
1807 pdf_puts(s, "1 j\n");
1810 if (state->linejoin != 1) {
1811 pdf_puts(s, "1 J\n");
1812 state->linejoin = 1;
1814 if (node->size != state->line_width) {
1815 state->line_width = node->size;
1816 pdf_putnumber(s, state->line_width);
1817 pdf_puts(s, " w\n");
1820 pdf_set_fill_color(s, node->color);
1822 for (i = 0; i < node->points; i++) {
1823 ArtVpath *vec = node->path + i;
1825 double y = state->page_height - vec->y;
1826 if (node->type == GFX_AREA) {
1827 x += LINEOFFSET; /* adjust for libart handling of areas */
1830 switch (vec->code) {
1831 case ART_MOVETO_OPEN: /* fall-through */
1833 pdf_putnumber(s, x);
1835 pdf_putnumber(s, y);
1836 pdf_puts(s, " m\n");
1839 pdf_putnumber(s, x);
1841 pdf_putnumber(s, y);
1842 pdf_puts(s, " l\n");
1844 case ART_CURVETO: break; /* unsupported */
1845 case ART_END: break; /* nop */
1848 if (node->type == GFX_LINE) {
1849 pdf_puts(s, node->closed_path ? "s\n" : "S\n");
1855 static void pdf_write_text(pdf_state *state, gfx_node_t *node,
1856 int last_was_text, int next_is_text)
1859 pdf_buffer *s = &state->graph_stream;
1860 const unsigned char *p;
1861 pdf_font *font = pdf_find_font(state, node);
1863 double y = state->page_height - node->y;
1864 double dx = 0, dy = 0;
1865 double cos_a = 0, sin_a = 0;
1867 rrd_set_error("font disappeared");
1868 state->has_failed = 1;
1871 switch(node->valign){
1872 case GFX_V_TOP: dy = -node->size; break;
1873 case GFX_V_CENTER: dy = -node->size / 3.0; break;
1874 case GFX_V_BOTTOM: break;
1875 case GFX_V_NULL: break;
1877 switch (node->halign) {
1878 case GFX_H_RIGHT: dx = -afm_get_text_width(0, font->ps_font,
1879 node->size, node->tabwidth, node->text);
1881 case GFX_H_CENTER: dx = -afm_get_text_width(0, font->ps_font,
1882 node->size, node->tabwidth, node->text) / 2;
1884 case GFX_H_LEFT: break;
1885 case GFX_H_NULL: break;
1887 pdf_set_fill_color(s, node->color);
1888 if (node->angle != 0) {
1889 double a = 2 * M_PI * -node->angle / 360.0;
1890 double new_x, new_y;
1893 new_x = cos_a * dx - sin_a * dy + x;
1894 new_y = sin_a * dx + cos_a * dy + y;
1902 pdf_puts(s, "BT\n");
1903 if (state->font_id != font->obj.id || node->size != state->font_size) {
1904 state->font_id = font->obj.id;
1905 state->font_size = node->size;
1907 pdf_putint(s, font->obj.id);
1909 pdf_putnumber(s, node->size);
1910 pdf_puts(s, " Tf\n");
1912 if (node->angle == 0) {
1913 pdf_puts(s, "1 0 0 1 ");
1915 pdf_putnumber(s, cos_a);
1917 pdf_putnumber(s, sin_a);
1919 pdf_putnumber(s, -sin_a);
1921 pdf_putnumber(s, cos_a);
1924 pdf_putnumber(s, x);
1926 pdf_putnumber(s, y);
1927 pdf_puts(s, " Tm\n");
1929 for (p = (const unsigned char*)node->text; *p; p++) {
1941 snprintf(tmp, sizeof(tmp), "\\%03o", *p);
1944 pdf_put(s, (const char*)p, 1);
1948 pdf_puts(s, ") Tj\n");
1950 pdf_puts(s, "ET\n");
1953 static void pdf_write_content(pdf_state *state)
1956 int last_was_text = 0, next_is_text;
1957 for (node = state->canvas->firstnode; node; node = node->next) {
1958 switch (node->type) {
1961 pdf_write_linearea(state, node);
1964 next_is_text = node->next && node->next->type == GFX_TEXT;
1965 pdf_write_text(state, node, last_was_text, next_is_text);
1968 last_was_text = node->type == GFX_TEXT;
1972 static void pdf_init_document(pdf_state *state)
1974 pdf_init_buffer(state, &state->pdf_header);
1975 pdf_init_dict(state, &state->catalog_obj);
1976 pdf_init_dict(state, &state->pages_obj);
1977 pdf_init_dict(state, &state->page1_obj);
1978 pdf_init_dict(state, &state->fontsdict_obj);
1979 pdf_create_fonts(state);
1980 if (state->has_failed)
1982 /* make stream last object in file */
1983 pdf_init_object(state, &state->graph_stream);
1984 state->graph_stream.is_stream = 1;
1987 static void pdf_setup_document(pdf_state *state)
1989 /* all objects created by now, so init code can reference them */
1991 pdf_puts(&state->pdf_header, "%PDF-1.3\n");
1992 /* following 8 bit comment is recommended by Adobe for
1993 indicating binary file to file transfer applications */
1994 pdf_puts(&state->pdf_header, "%\xE2\xE3\xCF\xD3\n");
1996 pdf_putsi(&state->catalog_obj, "/Type /Catalog\n");
1997 pdf_putsi(&state->catalog_obj, "/Pages ");
1998 pdf_putint(&state->catalog_obj, state->pages_obj.id);
1999 pdf_puts(&state->catalog_obj, " 0 R\n");
2001 pdf_putsi(&state->pages_obj, "/Type /Pages\n");
2002 pdf_putsi(&state->pages_obj, "/Kids [");
2003 pdf_putint(&state->pages_obj, state->page1_obj.id);
2004 pdf_puts(&state->pages_obj, " 0 R]\n");
2005 pdf_putsi(&state->pages_obj, "/Count 1\n");
2007 pdf_putsi(&state->page1_obj, "/Type /Page\n");
2008 pdf_putsi(&state->page1_obj, "/Parent ");
2009 pdf_putint(&state->page1_obj, state->pages_obj.id);
2010 pdf_puts(&state->page1_obj, " 0 R\n");
2011 pdf_putsi(&state->page1_obj, "/MediaBox [0 0 ");
2012 pdf_putint(&state->page1_obj, state->page_width);
2013 pdf_puts(&state->page1_obj, " ");
2014 pdf_putint(&state->page1_obj, state->page_height);
2015 pdf_puts(&state->page1_obj, "]\n");
2016 pdf_putsi(&state->page1_obj, "/Contents ");
2017 pdf_putint(&state->page1_obj, state->graph_stream.id);
2018 pdf_puts(&state->page1_obj, " 0 R\n");
2019 pdf_putsi(&state->page1_obj, "/Resources << /Font ");
2020 pdf_putint(&state->page1_obj, state->fontsdict_obj.id);
2021 pdf_puts(&state->page1_obj, " 0 R >>\n");
2024 static void pdf_write_string_to_file(pdf_state *state, const char *text)
2026 fputs(text, state->fp);
2027 state->pdf_file_pos += strlen(text);
2030 static void pdf_write_buf_to_file(pdf_state *state, pdf_buffer *buf)
2033 buf->pdf_file_pos = state->pdf_file_pos;
2035 snprintf(tmp, sizeof(tmp), "%d 0 obj\n", buf->id);
2036 pdf_write_string_to_file(state, tmp);
2039 pdf_write_string_to_file(state, "<<\n");
2040 if (buf->is_stream) {
2041 snprintf(tmp, sizeof(tmp), "<< /Length %d >>\n", buf->current_size);
2042 pdf_write_string_to_file(state, tmp);
2043 pdf_write_string_to_file(state, "stream\n");
2045 fwrite(buf->data, 1, buf->current_size, state->fp);
2046 state->pdf_file_pos += buf->current_size;
2048 pdf_write_string_to_file(state, "endstream\n");
2050 pdf_write_string_to_file(state, ">>\n");
2052 pdf_write_string_to_file(state, "endobj\n");
2055 static void pdf_write_to_file(pdf_state *state)
2057 pdf_buffer *buf = state->first_buffer;
2059 state->pdf_file_pos = 0;
2060 pdf_write_buf_to_file(state, &state->pdf_header);
2063 pdf_write_buf_to_file(state, buf);
2064 buf = buf->next_buffer;
2066 xref_pos = state->pdf_file_pos;
2067 fprintf(state->fp, "xref\n");
2068 fprintf(state->fp, "%d %d\n", 0, state->last_obj_id + 1);
2069 /* TOC lines must be exactly 20 bytes including \n */
2070 fprintf(state->fp, "%010d %05d f\x20\n", 0, 65535);
2071 for (buf = state->first_buffer; buf; buf = buf->next_buffer) {
2073 fprintf(state->fp, "%010d %05d n\x20\n", buf->pdf_file_pos, 0);
2075 fprintf(state->fp, "trailer\n");
2076 fprintf(state->fp, "<<\n");
2077 fprintf(state->fp, "\t/Size %d\n", state->last_obj_id + 1);
2078 fprintf(state->fp, "\t/Root %d 0 R\n", state->catalog_obj.id);
2079 fprintf(state->fp, ">>\n");
2080 fprintf(state->fp, "startxref\n");
2081 fprintf(state->fp, "%d\n", xref_pos);
2082 fputs("%%EOF\n", state->fp);
2085 static void pdf_free_resources(pdf_state *state)
2087 pdf_buffer *buf = state->first_buffer;
2091 buf->alloc_size = buf->current_size = 0;
2092 buf = buf->next_buffer;
2094 while (state->font_list) {
2095 pdf_font *next = state->font_list->next;
2096 free(state->font_list);
2097 state->font_list = next;
2101 int gfx_render_pdf (gfx_canvas_t *canvas,
2102 art_u32 width, art_u32 height,
2103 gfx_color_t background, FILE *fp){
2104 struct pdf_state state;
2105 memset(&state, 0, sizeof(pdf_state));
2107 state.canvas = canvas;
2108 state.page_width = width;
2109 state.page_height = height;
2111 state.font_size = -1;
2112 state.font_list = NULL;
2114 state.linejoin = -1;
2115 pdf_init_document(&state);
2117 pdf_set_color(&state, background);
2118 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
2119 height, width, height, width);
2121 if (!state.has_failed)
2122 pdf_write_content(&state);
2123 if (!state.has_failed)
2124 pdf_setup_document(&state);
2125 if (!state.has_failed)
2126 pdf_write_to_file(&state);
2127 pdf_free_resources(&state);
2128 return state.has_failed ? -1 : 0;