1 /****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
10 # define DPRINT(x) (void)(printf x, printf("\n"))
17 #include FT_FREETYPE_H
22 /* lines are better drawn on the pixle than between pixles */
23 #define LINEOFFSET 0.5
26 gfx_node_t *gfx_new_node( gfx_canvas_t *canvas,enum gfx_en type){
27 gfx_node_t *node = art_new(gfx_node_t,1);
28 if (node == NULL) return NULL;
30 node->color = 0x0; /* color of element 0xRRGGBBAA alpha 0xff is solid*/
31 node->size =0.0; /* font size, line width */
32 node->path = NULL; /* path */
35 node->closed_path = 0;
36 node->svp = NULL; /* svp */
37 node->filename = NULL; /* font or image filename */
40 node->y = 0.0; /* position */
42 node->halign = GFX_H_NULL; /* text alignement */
43 node->valign = GFX_V_NULL; /* text alignement */
46 if (canvas->lastnode != NULL){
47 canvas->lastnode->next = node;
49 if (canvas->firstnode == NULL){
50 canvas->firstnode = node;
52 canvas->lastnode = node;
56 gfx_canvas_t *gfx_new_canvas (void) {
57 gfx_canvas_t *canvas = art_new(gfx_canvas_t,1);
58 canvas->firstnode = NULL;
59 canvas->lastnode = NULL;
60 canvas->imgformat = IF_PNG; /* we default to PNG output */
61 canvas->interlaced = 0;
66 /* create a new line */
67 gfx_node_t *gfx_new_line(gfx_canvas_t *canvas,
70 double width, gfx_color_t color){
71 return gfx_new_dashed_line(canvas, X0, Y0, X1, Y1, width, color, 0, 0);
74 gfx_node_t *gfx_new_dashed_line(gfx_canvas_t *canvas,
77 double width, gfx_color_t color,
78 double dash_on, double dash_off){
82 node = gfx_new_node(canvas,GFX_LINE);
83 if (node == NULL) return NULL;
84 vec = art_new(ArtVpath, 3);
85 if (vec == NULL) return NULL;
86 vec[0].code = ART_MOVETO_OPEN; vec[0].x=X0+LINEOFFSET; vec[0].y=Y0+LINEOFFSET;
87 vec[1].code = ART_LINETO; vec[1].x=X1+LINEOFFSET; vec[1].y=Y1+LINEOFFSET;
88 vec[2].code = ART_END;
94 node->dash_on = dash_on;
95 node->dash_off = dash_off;
100 /* create a new area */
101 gfx_node_t *gfx_new_area (gfx_canvas_t *canvas,
102 double X0, double Y0,
103 double X1, double Y1,
104 double X2, double Y2,
109 node = gfx_new_node(canvas,GFX_AREA);
110 if (node == NULL) return NULL;
111 vec = art_new(ArtVpath, 5);
112 if (vec == NULL) return NULL;
113 vec[0].code = ART_MOVETO; vec[0].x=X0; vec[0].y=Y0;
114 vec[1].code = ART_LINETO; vec[1].x=X1; vec[1].y=Y1;
115 vec[2].code = ART_LINETO; vec[2].x=X2; vec[2].y=Y2;
116 vec[3].code = ART_LINETO; vec[3].x=X0; vec[3].y=Y0;
117 vec[4].code = ART_END;
120 node->points_max = 5;
127 /* add a point to a line or to an area */
128 int gfx_add_point (gfx_node_t *node,
130 if (node == NULL) return 1;
131 if (node->type == GFX_AREA) {
132 double X0 = node->path[0].x;
133 double Y0 = node->path[0].y;
135 art_vpath_add_point (&(node->path),
140 art_vpath_add_point (&(node->path),
145 art_vpath_add_point (&(node->path),
150 } else if (node->type == GFX_LINE) {
152 art_vpath_add_point (&(node->path),
156 x+LINEOFFSET,y+LINEOFFSET);
157 art_vpath_add_point (&(node->path),
164 /* can only add point to areas and lines */
170 void gfx_close_path (gfx_node_t *node) {
171 node->closed_path = 1;
174 /* create a text node */
175 gfx_node_t *gfx_new_text (gfx_canvas_t *canvas,
176 double x, double y, gfx_color_t color,
177 char* font, double size,
178 double tabwidth, double angle,
179 enum gfx_h_align_en h_align,
180 enum gfx_v_align_en v_align,
182 gfx_node_t *node = gfx_new_node(canvas,GFX_TEXT);
184 /* currently we only support 0 and 270 */
188 node->text = strdup(text);
190 node->filename = strdup(font);
195 node->tabwidth = tabwidth;
196 node->halign = h_align;
197 node->valign = v_align;
199 /* debugging: show text anchor */
200 gfx_new_line(canvas, x - 3, y + 0, x, y, 0.2, 0x00FF0000);
201 gfx_new_line(canvas, x - 0, y + 3, x, y, 0.2, 0x00FF0000);
206 int gfx_render(gfx_canvas_t *canvas,
207 art_u32 width, art_u32 height,
208 gfx_color_t background, FILE *fp){
209 switch (canvas->imgformat) {
211 return gfx_render_png (canvas, width, height, background, fp);
213 return gfx_render_svg (canvas, width, height, background, fp);
215 return gfx_render_eps (canvas, width, height, background, fp);
221 double gfx_get_text_width ( gfx_canvas_t *canvas,
222 double start, char* font, double size,
223 double tabwidth, char* text){
224 switch (canvas->imgformat) {
226 return gfx_get_text_width_libart (canvas, start, font, size, tabwidth, text);
227 case IF_SVG: /* fall through */
229 return afm_get_text_width(start, font, size, tabwidth, text);
231 return size * strlen(text);
235 double gfx_get_text_width_libart ( gfx_canvas_t *canvas,
236 double start, char* font, double size,
237 double tabwidth, char* text){
241 FT_UInt glyph_index=0;
245 FT_Library library=NULL;
247 FT_Init_FreeType( &library );
248 error = FT_New_Face( library, font, 0, &face );
249 if ( error ) return -1;
250 error = FT_Set_Char_Size(face, size*64,size*64, 100,100);
251 if ( error ) return -1;
253 use_kerning = FT_HAS_KERNING(face);
256 previous = glyph_index;
257 glyph_index = FT_Get_Char_Index( face, *text);
259 if (use_kerning && previous && glyph_index){
261 FT_Get_Kerning( face, previous, glyph_index,
263 text_width += (double)delta.x / 64.0;
266 error = FT_Load_Glyph( face, glyph_index, 0 );
268 FT_Done_FreeType(library);
272 text_width -= (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
274 text_width += (double)slot->metrics.horiAdvance / 64.0;
276 text_width -= (double)slot->metrics.horiAdvance / 64.0; /* remove last step */
277 text_width += (double)slot->metrics.width / 64.0; /* add just char width */
278 text_width += (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
279 FT_Done_FreeType(library);
283 static int gfx_save_png (art_u8 *buffer, FILE *fp,
284 long width, long height, long bytes_per_pixel);
285 /* render grafics into png image */
286 int gfx_render_png (gfx_canvas_t *canvas,
287 art_u32 width, art_u32 height,
288 gfx_color_t background, FILE *fp){
292 gfx_node_t *node = canvas->firstnode;
293 art_u8 red = background >> 24, green = (background >> 16) & 0xff;
294 art_u8 blue = (background >> 8) & 0xff, alpha = ( background & 0xff );
295 unsigned long pys_width = width * canvas->zoom;
296 unsigned long pys_height = height * canvas->zoom;
297 const int bytes_per_pixel = 3;
298 unsigned long rowstride = pys_width*bytes_per_pixel; /* bytes per pixel */
299 art_u8 *buffer = art_new (art_u8, rowstride*pys_height);
300 art_rgb_run_alpha (buffer, red, green, blue, alpha, pys_width*pys_height);
301 FT_Init_FreeType( &library );
303 switch (node->type) {
309 if (node->closed_path) {
310 /* libart uses end==start for closed as indicator of closed path */
311 gfx_add_point(node, node->path[0].x, node->path[0].y);
312 node->closed_path = 0;
314 art_affine_scale(dst,canvas->zoom,canvas->zoom);
315 vec = art_vpath_affine_transform(node->path,dst);
316 if(node->type == GFX_LINE){
317 svp = art_svp_vpath_stroke ( vec, ART_PATH_STROKE_JOIN_ROUND,
318 ART_PATH_STROKE_CAP_ROUND,
319 node->size*canvas->zoom,1,1);
321 svp = art_svp_from_vpath ( vec );
324 art_rgb_svp_alpha (svp ,0,0, pys_width, pys_height,
325 node->color, buffer, rowstride, NULL);
331 float text_width=0.0, text_height = 0.0;
333 art_u8 fcolor[3],falpha;
337 FT_UInt glyph_index=0;
340 float pen_x = 0.0 , pen_y = 0.0;
344 fcolor[0] = node->color >> 24;
345 fcolor[1] = (node->color >> 16) & 0xff;
346 fcolor[2] = (node->color >> 8) & 0xff;
347 falpha = node->color & 0xff;
348 error = FT_New_Face( library,
349 (char *)node->filename,
353 use_kerning = FT_HAS_KERNING(face);
355 error = FT_Set_Char_Size(face, /* handle to face object */
356 (long)(node->size*64),
357 (long)(node->size*64),
358 (long)(100*canvas->zoom),
359 (long)(100*canvas->zoom));
361 pen_x = node->x * canvas->zoom;
362 pen_y = node->y * canvas->zoom;
365 for(text=(unsigned char *)node->text;*text;text++) {
366 previous = glyph_index;
367 glyph_index = FT_Get_Char_Index( face, *text);
369 if (use_kerning && previous && glyph_index){
371 FT_Get_Kerning( face, previous, glyph_index,
373 text_width += (double)delta.x / 64.0;
376 error = FT_Load_Glyph( face, glyph_index, 0 );
379 pen_x -= (double)slot->metrics.horiBearingX / 64.0; /* adjust pos for first char */
380 text_width -= (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
382 if ( text_height < (double)slot->metrics.horiBearingY / 64.0 ) {
383 text_height = (double)slot->metrics.horiBearingY / 64.0;
385 text_width += (double)slot->metrics.horiAdvance / 64.0;
387 text_width -= (double)slot->metrics.horiAdvance / 64.0; /* remove last step */
388 text_width += (double)slot->metrics.width / 64.0; /* add just char width */
389 text_width += (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
391 switch(node->halign){
392 case GFX_H_RIGHT: pen_x -= text_width; break;
393 case GFX_H_CENTER: pen_x -= text_width / 2.0; break;
394 case GFX_H_LEFT: break;
395 case GFX_H_NULL: break;
398 switch(node->valign){
399 case GFX_V_TOP: pen_y += text_height; break;
400 case GFX_V_CENTER: pen_y += text_height / 2.0; break;
401 case GFX_V_BOTTOM: break;
402 case GFX_V_NULL: break;
406 for(text=(unsigned char *)node->text;*text;text++) {
408 previous = glyph_index;
409 glyph_index = FT_Get_Char_Index( face, *text);
411 if (use_kerning && previous && glyph_index){
413 FT_Get_Kerning( face, previous, glyph_index,
415 pen_x += (double)delta.x / 64.0;
418 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER );
420 gr = slot->bitmap.num_grays -1;
421 for (iy=0; iy < slot->bitmap.rows; iy++){
422 long buf_y = iy+(pen_y+0.5)-slot->bitmap_top;
423 if (buf_y < 0 || buf_y >= pys_height) continue;
425 for (ix=0;ix < slot->bitmap.width;ix++){
426 long buf_x = ix + (pen_x + 0.5) + (double)slot->bitmap_left ;
429 if (buf_x < 0 || buf_x >= pys_width) continue;
430 buf_x *= bytes_per_pixel ;
431 font_alpha = *(slot->bitmap.buffer + iy * slot->bitmap.width + ix);
432 font_alpha = (art_u8)((double)font_alpha / gr * falpha);
433 for (iz = 0; iz < 3; iz++){
434 art_u8 *orig = buffer + buf_y + buf_x + iz;
435 *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
436 (double)fcolor[iz] / gr * (font_alpha));
440 pen_x += (double)slot->metrics.horiAdvance / 64.0;
446 gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
448 FT_Done_FreeType( library );
452 /* free memory used by nodes this will also remove memory required for
453 associated paths and svcs ... but not for text strings */
455 gfx_destroy (gfx_canvas_t *canvas){
456 gfx_node_t *next,*node = canvas->firstnode;
459 art_free(node->path);
462 free(node->filename);
469 static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
470 png_structp png_ptr = NULL;
471 png_infop info_ptr = NULL;
473 png_bytep *row_pointers;
474 int rowstride = width * bytes_per_pixel;
480 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
485 row_pointers = (png_bytepp)png_malloc(png_ptr,
486 height*sizeof(png_bytep));
488 info_ptr = png_create_info_struct(png_ptr);
490 if (info_ptr == NULL)
492 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
496 if (setjmp(png_jmpbuf(png_ptr)))
498 /* If we get here, we had a problem writing the file */
499 png_destroy_write_struct(&png_ptr, &info_ptr);
503 png_init_io(png_ptr, fp);
504 png_set_IHDR (png_ptr, info_ptr,width, height,
505 8, PNG_COLOR_TYPE_RGB,
507 PNG_COMPRESSION_TYPE_DEFAULT,
508 PNG_FILTER_TYPE_DEFAULT);
510 text[0].key = "Software";
511 text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
512 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
513 png_set_text (png_ptr, info_ptr, text, 1);
515 /* Write header data */
516 png_write_info (png_ptr, info_ptr);
518 for (i = 0; i < height; i++)
519 row_pointers[i] = (png_bytep) (buffer + i*rowstride);
521 png_write_image(png_ptr, row_pointers);
522 png_write_end(png_ptr, info_ptr);
523 png_destroy_write_struct(&png_ptr, &info_ptr);
528 /* ------- SVG -------
530 http://www.w3.org/TR/SVG/
532 static int svg_indent = 0;
533 static int svg_single_line = 0;
534 static const char *svg_default_font = "Helvetica";
536 static void svg_print_indent(FILE *fp)
539 for (i = svg_indent - svg_single_line; i > 0; i--) {
545 static void svg_start_tag(FILE *fp, const char *name)
547 svg_print_indent(fp);
553 static void svg_close_tag_single_line(FILE *fp)
559 static void svg_close_tag(FILE *fp)
562 if (!svg_single_line)
566 static void svg_end_tag(FILE *fp, const char *name)
568 /* name is NULL if closing empty-node tag */
573 svg_print_indent(fp);
583 static void svg_close_tag_empty_node(FILE *fp)
585 svg_end_tag(fp, NULL);
588 static void svg_write_text(FILE *fp, const char *text)
590 const unsigned char *p, *start, *last;
592 p = (const unsigned char*)text;
595 /* trim leading spaces */
599 /* trim trailing spaces */
601 while ((ch = *p) != 0) {
606 /* encode trimmed text */
610 ch = afm_host2unicode(ch); /* unsafe macro */
612 case '&': fputs("&", fp); break;
613 case '<': fputs("<", fp); break;
614 case '>': fputs(">", fp); break;
615 case '"': fputs(""", fp); break;
618 fprintf(fp, "&#%d;", ch);
625 static void svg_write_number(FILE *fp, double d)
627 /* omit decimals if integer to reduce filesize */
629 snprintf(buf, sizeof(buf), "%.2f", d);
630 p = buf; /* doesn't trust snprintf return value */
636 *p = '\0'; /* zap trailing zeros */
640 *p = '\0'; /* zap trailing dot */
646 static int svg_color_is_black(int c)
648 /* gfx_color_t is RRGGBBAA */
649 return c == 0x000000FF;
652 static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
654 /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
655 gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
656 gfx_color_t opacity = c & 0xFF;
657 fprintf(fp, " %s=\"", attr);
658 if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
659 /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
660 fprintf(fp, "#%03lX",
661 ( ((rrggbb >> 8) & 0xF00)
662 | ((rrggbb >> 4) & 0x0F0)
663 | ( rrggbb & 0x00F)));
665 fprintf(fp, "#%06lX", rrggbb);
668 if (opacity != 0xFF) {
669 fprintf(fp, " stroke-opacity=\"");
670 svg_write_number(fp, opacity / 255.0);
675 static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
677 fputs(" stroke-width=\"", fp);
678 svg_write_number(fp, node->size);
680 svg_write_color(fp, node->color, "stroke");
681 fputs(" fill=\"none\"", fp);
682 if (node->dash_on > 0 && node->dash_off > 0) {
683 fputs(" stroke-dasharray=\"", fp);
684 svg_write_number(fp, node->dash_on);
686 svg_write_number(fp, node->dash_off);
691 static int svg_is_int_step(double a, double b)
693 double diff = fabs(a - b);
694 return floor(diff) == diff;
697 static int svg_path_straight_segment(FILE *fp,
698 double lastA, double currentA, double currentB,
700 int segment_idx, int isx, char absChar, char relChar)
702 if (!svg_is_int_step(lastA, currentA)) {
704 svg_write_number(fp, currentA);
707 if (segment_idx < node->points - 1) {
708 ArtVpath *vec = node->path + segment_idx + 1;
709 if (vec->code == ART_LINETO) {
710 double nextA = (isx ? vec->x : vec->y) - LINEOFFSET;
711 double nextB = (isx ? vec->y : vec->x) - LINEOFFSET;
712 if (nextB == currentB
713 && ((currentA >= lastA) == (nextA >= currentA))
714 && svg_is_int_step(currentA, nextA)) {
715 return 1; /* skip to next as it is a straight line */
720 svg_write_number(fp, currentA - lastA);
724 static void svg_path(FILE *fp, gfx_node_t *node, int multi)
727 double lastX = 0, lastY = 0;
728 /* for straight lines <path..> tags take less space than
729 <line..> tags because of the efficient packing
730 in the 'd' attribute */
731 svg_start_tag(fp, "path");
733 svg_common_path_attributes(fp, node);
735 /* specification of the 'd' attribute: */
736 /* http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation */
737 for (i = 0; i < node->points; i++) {
738 ArtVpath *vec = node->path + i;
739 double x = vec->x - LINEOFFSET;
740 double y = vec->y - LINEOFFSET;
742 case ART_MOVETO_OPEN: /* fall-through */
745 svg_write_number(fp, x);
747 svg_write_number(fp, y);
750 /* try optimize filesize by using minimal lineto commands */
751 /* without introducing rounding errors. */
753 if (svg_path_straight_segment(fp, lastY, y, x, node, i, 0, 'V', 'v'))
755 } else if (y == lastY) {
756 if (svg_path_straight_segment(fp, lastX, x, y, node, i, 1, 'H', 'h'))
760 svg_write_number(fp, x);
762 svg_write_number(fp, y);
765 case ART_CURVETO: break; /* unsupported */
766 case ART_END: break; /* nop */
771 if (node->closed_path)
774 svg_close_tag_empty_node(fp);
777 static void svg_multi_path(FILE *fp, gfx_node_t **nodeR)
779 /* optimize for multiple paths with the same color, penwidth, etc. */
781 gfx_node_t *node = *nodeR;
782 gfx_node_t *next = node->next;
784 if (next->type != node->type
785 || next->size != node->size
786 || next->color != node->color
787 || next->dash_on != node->dash_on
788 || next->dash_off != node->dash_off)
794 svg_path(fp, node, 0);
797 svg_start_tag(fp, "g");
798 svg_common_path_attributes(fp, node);
800 while (num && node) {
801 svg_path(fp, node, 1);
807 svg_end_tag(fp, "g");
810 static void svg_area(FILE *fp, gfx_node_t *node)
813 double startX = 0, startY = 0;
814 svg_start_tag(fp, "polygon");
816 svg_write_color(fp, node->color, "fill");
817 fputs(" points=\"", fp);
818 for (i = 0; i < node->points; i++) {
819 ArtVpath *vec = node->path + i;
820 double x = vec->x - LINEOFFSET;
821 double y = vec->y - LINEOFFSET;
823 case ART_MOVETO_OPEN: /* fall-through */
825 svg_write_number(fp, x);
827 svg_write_number(fp, y);
832 if (i == node->points - 2
833 && node->path[i + 1].code == ART_END
834 && fabs(x - startX) < 0.001 && fabs(y - startY) < 0.001) {
835 break; /* poly area always closed, no need for last point */
838 svg_write_number(fp, x);
840 svg_write_number(fp, y);
842 case ART_CURVETO: break; /* unsupported */
843 case ART_END: break; /* nop */
847 svg_close_tag_empty_node(fp);
850 static void svg_text(FILE *fp, gfx_node_t *node)
852 double x = node->x - LINEOFFSET;
853 double y = node->y - LINEOFFSET;
854 if (node->angle != 0) {
855 svg_start_tag(fp, "g");
856 fputs(" transform=\"translate(", fp);
857 svg_write_number(fp, x);
859 svg_write_number(fp, y);
860 fputs(") rotate(", fp);
861 svg_write_number(fp, node->angle);
866 switch (node->valign) {
867 case GFX_V_TOP: y += node->size; break;
868 case GFX_V_CENTER: y += node->size / 2; break;
869 case GFX_V_BOTTOM: break;
870 case GFX_V_NULL: break;
872 svg_start_tag(fp, "text");
874 svg_write_number(fp, x);
875 fputs("\" y=\"", fp);
876 svg_write_number(fp, y);
877 if (strcmp(node->filename, svg_default_font))
878 fprintf(fp, " font-family=\"%s\"", node->filename);
879 fputs("\" font-size=\"", fp);
880 svg_write_number(fp, node->size);
882 if (!svg_color_is_black(node->color))
883 svg_write_color(fp, node->color, "fill");
884 switch (node->halign) {
885 case GFX_H_RIGHT: fputs(" text-anchor=\"end\"", fp); break;
886 case GFX_H_CENTER: fputs(" text-anchor=\"middle\"", fp); break;
887 case GFX_H_LEFT: break;
888 case GFX_H_NULL: break;
890 svg_close_tag_single_line(fp);
891 /* support for node->tabwidth missing */
892 svg_write_text(fp, node->text);
893 svg_end_tag(fp, "text");
894 if (node->angle != 0)
895 svg_end_tag(fp, "g");
898 int gfx_render_svg (gfx_canvas_t *canvas,
899 art_u32 width, art_u32 height,
900 gfx_color_t background, FILE *fp){
901 gfx_node_t *node = canvas->firstnode;
903 "<?xml version=\"1.0\" standalone=\"no\"?>\n"
904 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
905 " \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
907 " SVG file created by RRDtool,\n"
908 " Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch\n"
910 " The width/height attributes in the outhermost svg node\n"
911 " are just default sizes for the browser which is used\n"
912 " if the svg file is openened directly without being\n"
913 " embedded in an html file.\n"
914 " The viewBox is the local coord system for rrdtool.\n"
916 svg_start_tag(fp, "svg");
917 fputs(" width=\"", fp);
918 svg_write_number(fp, width * canvas->zoom);
919 fputs("\" height=\"", fp);
920 svg_write_number(fp, height * canvas->zoom);
921 fputs("\" x=\"0\" y=\"0\" viewBox=\"", fp);
922 svg_write_number(fp, -LINEOFFSET);
924 svg_write_number(fp, -LINEOFFSET);
926 svg_write_number(fp, width - LINEOFFSET);
928 svg_write_number(fp, height - LINEOFFSET);
929 fputs("\" preserveAspectRatio=\"xMidYMid\"", fp);
930 fprintf(fp, " font-family=\"%s\"", svg_default_font); /* default font */
931 fputs(" stroke-linecap=\"round\" stroke-linejoin=\"round\"", fp);
933 svg_start_tag(fp, "rect");
934 fprintf(fp, " x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"", width, height);
935 svg_write_color(fp, background, "fill");
936 svg_close_tag_empty_node(fp);
938 switch (node->type) {
940 svg_multi_path(fp, &node);
950 svg_end_tag(fp, "svg");
954 /* ------- EPS -------
955 EPS and Postscript references:
956 http://partners.adobe.com/asn/developer/technotes/postscript.html
959 typedef struct eps_font
963 struct eps_font *next;
966 typedef struct eps_state
969 gfx_canvas_t *canvas;
970 art_u32 page_width, page_height;
977 int linecap, linejoin;
980 static void eps_set_color(eps_state *state, gfx_color_t color)
982 /* gfx_color_t is RRGGBBAA */
983 if (state->color == color)
985 fprintf(state->fp, "%d %d %d Rgb\n",
986 (int)((color >> 24) & 255),
987 (int)((color >> 16) & 255),
988 (int)((color >> 8) & 255));
989 state->color = color;
992 static int eps_add_font(eps_state *state, gfx_node_t *node)
994 /* The fonts list could be postponed to the end using
995 (atend), but let's be nice and have them in the header. */
996 const char *ps_font = afm_get_font_postscript_name(node->filename);
998 for (ef = state->font_list; ef; ef = ef->next) {
999 if (!strcmp(ps_font, ef->ps_font))
1002 ef = malloc(sizeof(eps_font));
1004 rrd_set_error("malloc for eps_font");
1007 ef->next = state->font_list;
1008 ef->ps_font = ps_font;
1009 state->font_list = ef;
1013 static void eps_list_fonts(eps_state *state, const char *dscName)
1016 int lineLen = strlen(dscName);
1017 if (!state->font_list)
1019 fputs(dscName, state->fp);
1020 for (ef = state->font_list; ef; ef = ef->next) {
1021 int nameLen = strlen(ef->ps_font);
1022 if (lineLen + nameLen > 100 && lineLen) {
1023 fputs("\n", state->fp);
1024 fputs("%%- \n", state->fp);
1027 fputs(" ", state->fp);
1030 fputs(ef->ps_font, state->fp);
1033 fputs("\n", state->fp);
1036 static void eps_define_fonts(eps_state *state)
1039 if (!state->font_list)
1041 for (ef = state->font_list; ef; ef = ef->next) {
1042 /* PostScript¨ LANGUAGE REFERENCE third edition
1046 "/%s findfont dup length dict begin\n"
1047 "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
1048 "/Encoding ISOLatin1Encoding def\n"
1050 "/%s-ISOLatin1 exch definefont pop\n"
1051 "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
1052 ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
1056 static int eps_prologue(eps_state *state)
1060 "%!PS-Adobe-3.0 EPSF-3.0\n"
1061 "%%Creator: RRDtool 1.1.x, Tobias Oetiker, http://tobi.oetiker.ch\n"
1062 /* can't like weird chars here */
1063 "%%Title: (RRDTool output)\n"
1064 "%%DocumentData: Clean7Bit\n"
1066 fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
1067 state->page_width, state->page_height);
1068 for (node = state->canvas->firstnode; node; node = node->next) {
1069 if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
1072 eps_list_fonts(state, "%%DocumentFonts:");
1073 eps_list_fonts(state, "%%DocumentNeededFonts:");
1077 "%%EndProlog\n" /* must have, or BoundingBox is ignored */
1078 "/bd { bind def } bind def\n"
1080 fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
1082 "/X2 {X exch X exch} bd\n"
1083 "/M {X2 moveto} bd\n"
1084 "/L {X2 lineto} bd\n"
1088 "/CP {closepath} bd\n"
1089 "/WS {setlinewidth stroke} bd\n"
1092 "/TaC {dup stringwidth pop neg 2 div 0 rmoveto } bd\n"
1093 "/TaR {dup stringwidth pop neg 0 rmoveto } bd\n"
1094 "/TL {moveto TaL show} bd\n"
1095 "/TC {moveto TaC show} bd\n"
1096 "/TR {moveto TaR show} bd\n"
1097 "/Rgb { 255.0 div 3 1 roll\n"
1098 " 255.0 div 3 1 roll \n"
1099 " 255.0 div 3 1 roll setrgbcolor } bd\n"
1101 eps_define_fonts(state);
1105 static void eps_write_linearea(eps_state *state, gfx_node_t *node)
1108 FILE *fp = state->fp;
1110 eps_set_color(state, node->color);
1111 if (node->type == GFX_LINE) {
1112 if (state->linecap != 1) {
1113 fputs("1 setlinecap\n", fp);
1116 if (state->linejoin != 1) {
1117 fputs("1 setlinejoin\n", fp);
1118 state->linejoin = 1;
1121 for (i = 0; i < node->points; i++) {
1122 ArtVpath *vec = node->path + i;
1124 double y = state->page_height - vec->y;
1125 if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
1126 useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
1131 switch (vec->code) {
1132 case ART_MOVETO_OPEN: /* fall-through */
1134 svg_write_number(fp, x);
1136 svg_write_number(fp, y);
1138 fputs(useOffset ? "M\n" : "m\n", fp);
1141 svg_write_number(fp, x);
1143 svg_write_number(fp, y);
1145 fputs(useOffset ? "L\n" : "l\n", fp);
1147 case ART_CURVETO: break; /* unsupported */
1148 case ART_END: break; /* nop */
1151 if (node->type == GFX_LINE) {
1152 if (node->closed_path)
1154 if (node->size != state->line_width) {
1155 state->line_width = node->size;
1156 svg_write_number(fp, state->line_width);
1166 static void eps_write_text(eps_state *state, gfx_node_t *node)
1168 FILE *fp = state->fp;
1169 const unsigned char *p;
1170 const char *ps_font = afm_get_font_postscript_name(node->filename);
1173 double y = state->page_height - node->y, ydelta = 0;
1175 eps_set_color(state, node->color);
1176 if (strcmp(ps_font, state->font) || node->size != state->font_size) {
1177 state->font = ps_font;
1178 state->font_size = node->size;
1179 svg_write_number(fp, state->font_size);
1180 fprintf(fp, " SetFont-%s\n", state->font);
1184 for (p = (const unsigned char*)node->text; *p; p++) {
1186 fputs("\\\n", fp); /* backslash and \n */
1201 fprintf(fp, "\\%03o", *p);
1208 switch(node->valign){
1209 case GFX_V_TOP: ydelta = -node->size; break;
1210 case GFX_V_CENTER: ydelta = -node->size / 3.0; break;
1211 case GFX_V_BOTTOM: break;
1212 case GFX_V_NULL: break;
1214 if (node->angle == 0)
1216 switch (node->halign) {
1217 case GFX_H_RIGHT: align = 'R'; break;
1218 case GFX_H_CENTER: align = 'C'; break;
1219 case GFX_H_LEFT: align= 'L'; break;
1220 case GFX_H_NULL: align= 'L'; break;
1222 if (node->angle != 0) {
1224 fputs(" gsave ", fp);
1225 svg_write_number(fp, x);
1227 svg_write_number(fp, y);
1228 fputs(" translate ", fp);
1229 svg_write_number(fp, -node->angle);
1230 fputs(" rotate 0 ", fp);
1231 svg_write_number(fp, ydelta);
1232 fputs(" moveto ", fp);
1233 fprintf(fp, "Ta%c", align);
1234 fputs(" show grestore\n", fp);
1236 svg_write_number(fp, x);
1238 svg_write_number(fp, y);
1245 static int eps_write_content(eps_state *state)
1248 fputs("%\n", state->fp);
1249 for (node = state->canvas->firstnode; node; node = node->next) {
1250 switch (node->type) {
1253 eps_write_linearea(state, node);
1256 eps_write_text(state, node);
1263 int gfx_render_eps (gfx_canvas_t *canvas,
1264 art_u32 width, art_u32 height,
1265 gfx_color_t background, FILE *fp){
1266 struct eps_state state;
1268 state.canvas = canvas;
1269 state.page_width = width;
1270 state.page_height = height;
1271 state.font = "no-default-font";
1272 state.font_size = -1;
1273 state.color = 0; /* black */
1274 state.font_list = NULL;
1276 state.linejoin = -1;
1277 if (eps_prologue(&state) == -1)
1279 eps_set_color(&state, background);
1280 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
1281 height, width, height, width);
1282 if (eps_write_content(&state) == -1)
1284 fputs("showpage\n", fp);
1285 fputs("%%EOF\n", fp);
1286 while (state.font_list) {
1287 eps_font *next = state.font_list->next;
1288 free(state.font_list);
1289 state.font_list = next;