1 /****************************************************************************
2 * RRDtool 1.2.19 Copyright by Tobi Oetiker, 1997-2007
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
9 /* stupid MSVC doesnt support variadic macros = no debug for now! */
14 # define RRDPRINTF(...) fprintf(stderr, __VA_ARGS__);
16 # define RRDPRINTF(...)
22 #include FT_FREETYPE_H
29 /* lines are better drawn on the pixle than between pixles */
30 #define LINEOFFSET 0.5
32 #define USE_PDF_FAKE_ALPHA 1
33 #define USE_EPS_FAKE_ALPHA 1
35 typedef struct gfx_char_s *gfx_char;
37 FT_UInt index; /* glyph index */
38 FT_Vector pos; /* location from baseline in 26.6 */
39 FT_Glyph image; /* glyph bitmap */
42 typedef struct gfx_string_s *gfx_string;
46 int count; /* number of characters */
53 /* compute string bbox */
54 static void compute_string_bbox(gfx_string string);
56 /* create a freetype glyph string */
57 gfx_string gfx_string_create ( gfx_canvas_t *canvas, FT_Face face,
58 const char *text, int rotation, double tabwidth, double size);
60 /* create a freetype glyph string */
61 static void gfx_string_destroy ( gfx_string string );
64 gfx_node_t *gfx_new_node( gfx_canvas_t *canvas,enum gfx_en type){
65 gfx_node_t *node = art_new(gfx_node_t,1);
66 if (node == NULL) return NULL;
68 node->color = 0x0; /* color of element 0xRRGGBBAA alpha 0xff is solid*/
69 node->size =0.0; /* font size, line width */
70 node->path = NULL; /* path */
73 node->closed_path = 0;
74 node->filename = NULL; /* font or image filename */
77 node->y = 0.0; /* position */
79 node->halign = GFX_H_NULL; /* text alignement */
80 node->valign = GFX_V_NULL; /* text alignement */
83 if (canvas->lastnode != NULL){
84 canvas->lastnode->next = node;
86 if (canvas->firstnode == NULL){
87 canvas->firstnode = node;
89 canvas->lastnode = node;
93 gfx_canvas_t *gfx_new_canvas (void) {
94 gfx_canvas_t *canvas = art_new(gfx_canvas_t,1);
95 canvas->firstnode = NULL;
96 canvas->lastnode = NULL;
97 canvas->imgformat = IF_PNG; /* we default to PNG output */
98 canvas->interlaced = 0;
100 canvas->font_aa_threshold = -1.0;
101 canvas->aa_type = AA_NORMAL;
105 /* create a new line */
106 gfx_node_t *gfx_new_line(gfx_canvas_t *canvas,
107 double X0, double Y0,
108 double X1, double Y1,
109 double width, gfx_color_t color){
110 return gfx_new_dashed_line(canvas, X0, Y0, X1, Y1, width, color, 0, 0);
113 gfx_node_t *gfx_new_dashed_line(gfx_canvas_t *canvas,
114 double X0, double Y0,
115 double X1, double Y1,
116 double width, gfx_color_t color,
117 double dash_on, double dash_off){
121 node = gfx_new_node(canvas,GFX_LINE);
122 if (node == NULL) return NULL;
123 vec = art_new(ArtVpath, 3);
124 if (vec == NULL) return NULL;
125 vec[0].code = ART_MOVETO_OPEN; vec[0].x=X0+LINEOFFSET; vec[0].y=Y0+LINEOFFSET;
126 vec[1].code = ART_LINETO; vec[1].x=X1+LINEOFFSET; vec[1].y=Y1+LINEOFFSET;
127 vec[2].code = ART_END; vec[2].x=0;vec[2].y=0;
130 node->points_max = 3;
133 node->dash_on = dash_on;
134 node->dash_off = dash_off;
139 /* create a new area */
140 gfx_node_t *gfx_new_area (gfx_canvas_t *canvas,
141 double X0, double Y0,
142 double X1, double Y1,
143 double X2, double Y2,
148 node = gfx_new_node(canvas,GFX_AREA);
149 if (node == NULL) return NULL;
150 vec = art_new(ArtVpath, 5);
151 if (vec == NULL) return NULL;
152 vec[0].code = ART_MOVETO; vec[0].x=X0; vec[0].y=Y0;
153 vec[1].code = ART_LINETO; vec[1].x=X1; vec[1].y=Y1;
154 vec[2].code = ART_LINETO; vec[2].x=X2; vec[2].y=Y2;
155 vec[3].code = ART_LINETO; vec[3].x=X0; vec[3].y=Y0;
156 vec[4].code = ART_END; vec[4].x=0; vec[4].y=0;
159 node->points_max = 5;
166 /* add a point to a line or to an area */
167 int gfx_add_point (gfx_node_t *node,
169 if (node == NULL) return 1;
170 if (node->type == GFX_AREA) {
171 double X0 = node->path[0].x;
172 double Y0 = node->path[0].y;
174 art_vpath_add_point (&(node->path),
179 art_vpath_add_point (&(node->path),
184 art_vpath_add_point (&(node->path),
189 } else if (node->type == GFX_LINE) {
191 art_vpath_add_point (&(node->path),
195 x+LINEOFFSET,y+LINEOFFSET);
196 art_vpath_add_point (&(node->path),
203 /* can only add point to areas and lines */
209 void gfx_close_path (gfx_node_t *node) {
210 node->closed_path = 1;
211 if (node->path[0].code == ART_MOVETO_OPEN)
212 node->path[0].code = ART_MOVETO;
215 /* create a text node */
216 gfx_node_t *gfx_new_text (gfx_canvas_t *canvas,
217 double x, double y, gfx_color_t color,
218 char* font, double size,
219 double tabwidth, double angle,
220 enum gfx_h_align_en h_align,
221 enum gfx_v_align_en v_align,
223 gfx_node_t *node = gfx_new_node(canvas,GFX_TEXT);
225 node->text = strdup(text);
227 node->filename = strdup(font);
232 node->tabwidth = tabwidth;
233 node->halign = h_align;
234 node->valign = v_align;
236 /* debugging: show text anchor
237 green is along x-axis, red is downward y-axis */
239 double a = 2 * M_PI * -node->angle / 360.0;
240 double cos_a = cos(a);
241 double sin_a = sin(a);
245 x + len * cos_a, y - len * sin_a,
249 x + len * sin_a, y + len * cos_a,
256 int gfx_render(gfx_canvas_t *canvas,
257 art_u32 width, art_u32 height,
258 gfx_color_t background, FILE *fp){
259 switch (canvas->imgformat) {
261 return gfx_render_png (canvas, width, height, background, fp);
263 return gfx_render_svg (canvas, width, height, background, fp);
265 return gfx_render_eps (canvas, width, height, background, fp);
267 return gfx_render_pdf (canvas, width, height, background, fp);
273 static void gfx_string_destroy ( gfx_string string ) {
275 if (string->glyphs) {
276 for (n=0; n<string->num_glyphs; ++n)
277 FT_Done_Glyph (string->glyphs[n].image);
278 free (string->glyphs);
284 double gfx_get_text_width ( gfx_canvas_t *canvas,
285 double start, char* font, double size,
286 double tabwidth, char* text, int rotation){
287 switch (canvas->imgformat) {
289 return gfx_get_text_width_libart (canvas, start, font, size, tabwidth, text, rotation);
290 case IF_SVG: /* fall through */
293 return afm_get_text_width(start, font, size, tabwidth, text);
295 return size * strlen(text);
299 double gfx_get_text_width_libart (
300 gfx_canvas_t *canvas, double UNUSED(start), char* font, double size,
301 double tabwidth, char* text, int rotation ){
306 FT_Library library=NULL;
309 FT_Init_FreeType( &library );
310 error = FT_New_Face( library, font, 0, &face );
312 FT_Done_FreeType(library);
315 error = FT_Set_Char_Size(face, size*64,size*64, 100,100);
317 FT_Done_FreeType(library);
320 string = gfx_string_create( canvas, face, text, rotation, tabwidth, size );
321 text_width = string->width;
322 gfx_string_destroy(string);
323 FT_Done_FreeType(library);
324 return text_width/64;
327 static void gfx_libart_close_path(gfx_node_t *node, ArtVpath **vec)
329 /* libart must have end==start for closed paths,
330 even if using ART_MOVETO and not ART_MOVETO_OPEN
331 so add extra point which is the same as the starting point */
332 int points_max = node->points; /* scaled array has exact size */
333 int points = node->points - 1;
334 art_vpath_add_point (vec, &points, &points_max, ART_LINETO,
335 (**vec).x, (**vec).y);
336 art_vpath_add_point (vec, &points, &points_max, ART_END, 0, 0);
340 /* find bbox of a string */
341 static void compute_string_bbox(gfx_string string) {
345 bbox.xMin = bbox.yMin = 32000;
346 bbox.xMax = bbox.yMax = -32000;
347 for ( n = 0; n < string->num_glyphs; n++ ) {
349 FT_Glyph_Get_CBox( string->glyphs[n].image, ft_glyph_bbox_gridfit,
351 if (glyph_bbox.xMin < bbox.xMin) {
352 bbox.xMin = glyph_bbox.xMin;
354 if (glyph_bbox.yMin < bbox.yMin) {
355 bbox.yMin = glyph_bbox.yMin;
357 if (glyph_bbox.xMax > bbox.xMax) {
358 bbox.xMax = glyph_bbox.xMax;
360 if (glyph_bbox.yMax > bbox.yMax) {
361 bbox.yMax = glyph_bbox.yMax;
364 if ( bbox.xMin > bbox.xMax ) {
370 string->bbox.xMin = bbox.xMin;
371 string->bbox.xMax = bbox.xMax;
372 string->bbox.yMin = bbox.yMin;
373 string->bbox.yMax = bbox.yMax;
376 /* create a free type glyph string */
377 gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text,
378 int rotation, double tabwidth, double size )
381 FT_GlyphSlot slot = face->glyph; /* a small shortcut */
386 gfx_string string = (gfx_string) malloc (sizeof(struct gfx_string_s));
388 gfx_char glyph; /* current glyph in table */
395 size_t clen = strlen(text)+1;
396 cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
397 string->count=mbstowcs(cstr,text,clen);
398 if ( string->count == -1){
399 /* conversion did not work, so lets fall back to just use what we got */
400 string->count=clen-1;
401 for(n=0;text[n] != '\0';n++){
402 cstr[n]=(unsigned char)text[n];
406 char *cstr = strdup(text);
407 string->count = strlen (text);
410 ft_pen.x = 0; /* start at (0,0) !! */
416 string->glyphs = (gfx_char) calloc (string->count,sizeof(struct gfx_char_s));
417 string->num_glyphs = 0;
418 string->transform.xx = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
419 string->transform.xy = (FT_Fixed)(-sin(M_PI*(rotation)/180.0)*0x10000);
420 string->transform.yx = (FT_Fixed)( sin(M_PI*(rotation)/180.0)*0x10000);
421 string->transform.yy = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
423 use_kerning = FT_HAS_KERNING(face);
425 glyph = string->glyphs;
426 for (n=0; n<string->count;glyph++,n++) {
428 /* handle the tabs ...
429 have a witespace glyph inserted, but set its width such that the distance
430 of the new right edge is x times tabwidth from 0,0 where x is an integer. */
431 unsigned int letter = cstr[n];
432 letter = afm_fix_osx_charset(letter); /* unsafe macro */
435 if (letter == '\\' && n+1 < string->count && cstr[n+1] == 't'){
436 /* we have a tab here so skip the backslash and
437 set t to ' ' so that we get a white space */
446 /* initialize each struct gfx_char_s */
451 glyph->index = FT_Get_Char_Index( face, letter );
453 /* compute glyph origin */
454 if ( use_kerning && previous && glyph->index ) {
456 FT_Get_Kerning (face, previous, glyph->index,
457 ft_kerning_default, &kerning);
458 ft_pen.x += kerning.x;
459 ft_pen.y += kerning.y;
462 /* load the glyph image (in its native format) */
463 /* for now, we take a monochrome glyph bitmap */
464 error = FT_Load_Glyph (face, glyph->index, size > canvas->font_aa_threshold ?
465 canvas->aa_type == AA_NORMAL ? FT_LOAD_TARGET_NORMAL :
466 canvas->aa_type == AA_LIGHT ? FT_LOAD_TARGET_LIGHT :
467 FT_LOAD_TARGET_MONO : FT_LOAD_TARGET_MONO);
469 RRDPRINTF("couldn't load glyph: %c\n", letter)
472 error = FT_Get_Glyph (slot, &glyph->image);
474 RRDPRINTF("couldn't get glyph %c from slot %d\n", letter, (int)slot)
477 /* if we are in tabbing mode, we replace the tab with a space and shift the position
478 of the space so that its left edge is where the tab was supposed to land us */
480 /* we are in gridfitting mode so the calculations happen in 1/64 pixles */
481 ft_pen.x = tabwidth*64.0 * (float)(1 + (long)(ft_pen.x / (tabwidth * 64.0))) - slot->advance.x;
483 /* store current pen position */
484 glyph->pos.x = ft_pen.x;
485 glyph->pos.y = ft_pen.y;
488 ft_pen.x += slot->advance.x;
489 ft_pen.y += slot->advance.y;
493 FT_Vector_Transform (&vec, &string->transform);
494 error = FT_Glyph_Transform (glyph->image, &string->transform, &vec);
496 RRDPRINTF("couldn't transform glyph id %d\n", letter)
500 /* convert to a bitmap - destroy native image */
501 error = FT_Glyph_To_Bitmap (&glyph->image, size > canvas->font_aa_threshold ?
502 canvas->aa_type == AA_NORMAL ? FT_RENDER_MODE_NORMAL :
503 canvas->aa_type == AA_LIGHT ? FT_RENDER_MODE_LIGHT :
504 FT_RENDER_MODE_MONO : FT_RENDER_MODE_MONO, 0, 1);
506 RRDPRINTF("couldn't convert glyph id %d to bitmap\n", letter)
510 /* increment number of glyphs */
511 previous = glyph->index;
512 string->num_glyphs++;
515 /* printf ("number of glyphs = %d\n", string->num_glyphs);*/
516 compute_string_bbox( string );
517 /* the last character was a tab */
519 string->width = ft_pen.x;
521 string->width = string->bbox.xMax - string->bbox.xMin;
523 string->height = string->bbox.yMax - string->bbox.yMin;
528 static int gfx_save_png (art_u8 *buffer, FILE *fp,
529 long width, long height, long bytes_per_pixel);
530 /* render grafics into png image */
532 int gfx_render_png (gfx_canvas_t *canvas,
533 art_u32 width, art_u32 height,
534 gfx_color_t background, FILE *fp){
538 gfx_node_t *node = canvas->firstnode;
540 art_u8 red = background >> 24, green = (background >> 16) & 0xff;
541 art_u8 blue = (background >> 8) & 0xff, alpha = ( background & 0xff );
543 unsigned long pys_width = width * canvas->zoom;
544 unsigned long pys_height = height * canvas->zoom;
545 const int bytes_per_pixel = 4;
546 unsigned long rowstride = pys_width*bytes_per_pixel; /* bytes per pixel */
548 /* fill that buffer with out background color */
549 gfx_color_t *buffp = art_new (gfx_color_t, pys_width*pys_height);
550 art_u8 *buffer = (art_u8 *)buffp;
552 for (i=0;i<pys_width*pys_height;
554 *(buffp++)=background;
556 FT_Init_FreeType( &library );
558 switch (node->type) {
564 art_affine_scale(dst,canvas->zoom,canvas->zoom);
565 vec = art_vpath_affine_transform(node->path,dst);
566 if (node->closed_path)
567 gfx_libart_close_path(node, &vec);
568 /* gfx_round_scaled_coordinates(vec); */
569 /* pvec = art_vpath_perturb(vec);
571 if(node->type == GFX_LINE){
572 svp = art_svp_vpath_stroke ( vec, ART_PATH_STROKE_JOIN_ROUND,
573 ART_PATH_STROKE_CAP_ROUND,
574 node->size*canvas->zoom,4,0.25);
576 svp = art_svp_from_vpath ( vec );
577 /* this takes time and is unnecessary since we make
578 sure elsewhere that the areas are going clock-whise */
579 /* svpt = art_svp_uncross( svp );
581 svp = art_svp_rewind_uncrossed(svpt,ART_WIND_RULE_NONZERO);
586 /* this is from gnome since libart does not have this yet */
587 gnome_print_art_rgba_svp_alpha (svp ,0,0, pys_width, pys_height,
588 node->color, buffer, rowstride, NULL);
595 art_u8 fcolor[4],falpha;
599 FT_Vector vec; /* 26.6 */
601 float pen_x = 0.0 , pen_y = 0.0;
605 fcolor[0] = node->color >> 24;
606 fcolor[1] = (node->color >> 16) & 0xff;
607 fcolor[2] = (node->color >> 8) & 0xff;
608 falpha = node->color & 0xff;
609 error = FT_New_Face( library,
610 (char *)node->filename,
614 rrd_set_error("failed to load %s",node->filename);
618 error = FT_Set_Char_Size(face, /* handle to face object */
619 (long)(node->size*64),
620 (long)(node->size*64),
621 (long)(100*canvas->zoom),
622 (long)(100*canvas->zoom));
624 pen_x = node->x * canvas->zoom;
625 pen_y = node->y * canvas->zoom;
627 string = gfx_string_create (canvas, face, node->text, node->angle, node->tabwidth, node->size);
628 switch(node->halign){
629 case GFX_H_RIGHT: vec.x = -string->bbox.xMax;
631 case GFX_H_CENTER: vec.x = abs(string->bbox.xMax) >= abs(string->bbox.xMin) ?
632 -string->bbox.xMax/2:-string->bbox.xMin/2;
634 case GFX_H_LEFT: vec.x = -string->bbox.xMin;
636 case GFX_H_NULL: vec.x = 0;
640 switch(node->valign){
641 case GFX_V_TOP: vec.y = string->bbox.yMax;
643 case GFX_V_CENTER: vec.y = abs(string->bbox.yMax) >= abs(string->bbox.yMin) ?
644 string->bbox.yMax/2:string->bbox.yMin/2;
646 case GFX_V_BOTTOM: vec.y = 0;
648 case GFX_V_NULL: vec.y = 0;
653 glyph = string->glyphs;
654 for(n=0; n<string->num_glyphs; n++, glyph++) {
658 /* long buf_x,comp_n; */
659 /* make copy to transform */
660 if (! glyph->image) {
661 RRDPRINTF("no image\n")
664 error = FT_Glyph_Copy (glyph->image, &image);
666 RRDPRINTF("couldn't copy image\n")
672 FT_Vector_Transform (&vec, &string->transform);
674 bit = (FT_BitmapGlyph) image;
675 gr = bit->bitmap.num_grays -1;
677 buf_x = (pen_x + 0.5) + (double)bit->left;
678 comp_n = buf_x + bit->bitmap.width > pys_width ? pys_width - buf_x : bit->bitmap.width;
679 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
680 buf_x *= bytes_per_pixel ;
681 for (iy=0; iy < bit->bitmap.rows; iy++){
682 long buf_y = iy+(pen_y+0.5)-(double)bit->top;
683 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
685 for (ix=0;ix < bit->bitmap.width;ix++){
686 *(letter + (ix*bytes_per_pixel+3)) = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
688 art_rgba_rgba_composite(buffer + buf_y + buf_x ,letter,comp_n);
692 switch ( bit->bitmap.pixel_mode ) {
693 case FT_PIXEL_MODE_GRAY:
694 for (iy=0; iy < bit->bitmap.rows; iy++){
695 long buf_y = iy+(pen_y+0.5)-bit->top;
696 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
698 for (ix=0;ix < bit->bitmap.width;ix++){
699 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
702 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
703 buf_x *= bytes_per_pixel ;
704 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix);
706 fcolor[3] = (art_u8)((double)font_alpha / gr * falpha);
707 art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
713 case FT_PIXEL_MODE_MONO:
714 for (iy=0; iy < bit->bitmap.rows; iy++){
715 long buf_y = iy+(pen_y+0.5)-bit->top;
716 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
718 for (ix=0;ix < bit->bitmap.width;ix++){
719 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
721 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
722 buf_x *= bytes_per_pixel ;
723 if ( (fcolor[3] = falpha * ((*(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix/8) >> (7 - (ix % 8))) & 1)) > 0 )
724 art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
730 rrd_set_error("unknown freetype pixel mode: %d", bit->bitmap.pixel_mode);
735 for (iy=0; iy < bit->bitmap.rows; iy++){
736 long buf_y = iy+(pen_y+0.5)-bit->top;
737 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
739 for (ix=0;ix < bit->bitmap.width;ix++){
740 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
743 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
744 buf_x *= bytes_per_pixel ;
745 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
746 font_alpha = (art_u8)((double)font_alpha / gr * falpha);
747 for (iz = 0; iz < 3; iz++){
748 art_u8 *orig = buffer + buf_y + buf_x + iz;
749 *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
750 (double)fcolor[iz] / gr * (font_alpha));
755 FT_Done_Glyph (image);
757 gfx_string_destroy(string);
762 gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
764 FT_Done_FreeType( library );
768 /* free memory used by nodes this will also remove memory required for
769 associated paths and svcs ... but not for text strings */
771 gfx_destroy (gfx_canvas_t *canvas){
772 gfx_node_t *next,*node = canvas->firstnode;
775 art_free(node->path);
777 free(node->filename);
785 static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
786 png_structp png_ptr = NULL;
787 png_infop info_ptr = NULL;
789 png_bytep *row_pointers;
790 int rowstride = width * bytes_per_pixel;
796 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
801 row_pointers = (png_bytepp)png_malloc(png_ptr,
802 height*sizeof(png_bytep));
804 info_ptr = png_create_info_struct(png_ptr);
806 if (info_ptr == NULL)
808 png_free(png_ptr,row_pointers);
809 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
813 if (setjmp(png_jmpbuf(png_ptr)))
815 /* If we get here, we had a problem writing the file */
816 png_destroy_write_struct(&png_ptr, &info_ptr);
820 png_init_io(png_ptr, fp);
821 png_set_IHDR (png_ptr, info_ptr,width, height,
822 8, PNG_COLOR_TYPE_RGB_ALPHA,
824 PNG_COMPRESSION_TYPE_DEFAULT,
825 PNG_FILTER_TYPE_DEFAULT);
827 text[0].key = "Software";
828 text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
829 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
830 png_set_text (png_ptr, info_ptr, text, 1);
832 /* lets make this fast while ending up with some increass in image size */
833 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
834 /* png_set_filter(png_ptr,0,PNG_FILTER_SUB); */
835 png_set_compression_level(png_ptr,1);
836 /* png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY); */
838 png_set_filter(png_ptr,PNG_FILTER_TYPE_BASE,PNG_FILTER_SUB);
839 png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY);
840 png_set_compression_level(png_ptr,Z_BEST_SPEED); */
842 /* Write header data */
843 png_write_info (png_ptr, info_ptr);
844 for (i = 0; i < height; i++)
845 row_pointers[i] = (png_bytep) (buffer + i*rowstride);
847 png_write_image(png_ptr, row_pointers);
848 png_write_end(png_ptr, info_ptr);
849 png_free(png_ptr,row_pointers);
850 png_destroy_write_struct(&png_ptr, &info_ptr);
855 /* ----- COMMON ROUTINES for pdf, svg and eps */
856 #define min3(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
857 #define max3(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
859 #define PDF_CALC_DEBUG 0
861 typedef struct pdf_point
868 double ascender, descender, baselineY;
869 pdf_point sizep, minp, maxp;
870 double x, y, tdx, tdy;
871 double r, cos_r, sin_r;
872 double ma, mb, mc, md, mx, my; /* pdf coord matrix */
873 double tmx, tmy; /* last 2 coords of text coord matrix */
880 static void pdf_dump_calc(gfx_node_t *node, pdf_coords *g)
882 fprintf(stderr, "PDF CALC =============================\n");
883 fprintf(stderr, " '%s' at %f pt\n", node->text, node->size);
884 fprintf(stderr, " align h = %s, v = %s, sizep = %f, %f\n",
885 (node->halign == GFX_H_RIGHT ? "r" :
886 (node->halign == GFX_H_CENTER ? "c" :
887 (node->halign == GFX_H_LEFT ? "l" : "N"))),
888 (node->valign == GFX_V_TOP ? "t" :
889 (node->valign == GFX_V_CENTER ? "c" :
890 (node->valign == GFX_V_BOTTOM ? "b" : "N"))),
891 g->sizep.x, g->sizep.y);
892 fprintf(stderr, " r = %f = %f, cos = %f, sin = %f\n",
893 g->r, node->angle, g->cos_r, g->sin_r);
894 fprintf(stderr, " ascender = %f, descender = %f, baselineY = %f\n",
895 g->ascender, g->descender, g->baselineY);
896 fprintf(stderr, " sizep: %f, %f\n", g->sizep.x, g->sizep.y);
897 fprintf(stderr, " minp: %f, %f maxp = %f, %f\n",
898 g->minp.x, g->minp.y, g->maxp.x, g->maxp.y);
899 fprintf(stderr, " x = %f, y = %f\n", g->x, g->y);
900 fprintf(stderr, " tdx = %f, tdy = %f\n", g->tdx, g->tdy);
901 fprintf(stderr, " GM = %f, %f, %f, %f, %f, %f\n",
902 g->ma, g->mb, g->mc, g->md, g->mx, g->my);
903 fprintf(stderr, " TM = %f, %f, %f, %f, %f, %f\n",
904 g->ma, g->mb, g->mc, g->md, g->tmx, g->tmy);
909 #define PDF_DD(x) if (g->debug) x;
914 static void pdf_rotate(pdf_coords *g, pdf_point *p)
916 double x2 = g->cos_r * p->x - g->sin_r * p->y;
917 double y2 = g->sin_r * p->x + g->cos_r * p->y;
918 PDF_DD( fprintf(stderr, " rotate(%f, %f) -> %f, %f\n", p->x, p->y, x2, y2))
924 static void pdf_calc(int page_height, gfx_node_t *node, pdf_coords *g)
928 /* g->debug = !!strstr(node->text, "RevProxy-1") || !!strstr(node->text, "08:00"); */
929 g->debug = !!strstr(node->text, "sekunder") || !!strstr(node->text, "Web");
932 g->y = page_height - node->y;
934 g->r = 2 * M_PI * node->angle / 360.0;
935 g->cos_r = cos(g->r);
936 g->sin_r = sin(g->r);
942 g->ascender = afm_get_ascender(node->filename, node->size);
943 g->descender = afm_get_descender(node->filename, node->size);
944 g->sizep.x = afm_get_text_width(0, node->filename, node->size, node->tabwidth, node->text);
945 /* seems like libart ignores the descender when doing vertial-align = bottom,
946 so we do that too, to get labels v-aligning properly */
947 g->sizep.y = -g->ascender; /* + afm_get_descender(font->ps_font, node->size); */
948 g->baselineY = -g->ascender - g->sizep.y / 2;
949 a.x = g->sizep.x; a.y = g->sizep.y;
950 b.x = g->sizep.x; b.y = 0;
951 c.x = 0; c.y = g->sizep.y;
957 g->minp.x = min3(a.x, b.x, c.x);
958 g->minp.y = min3(a.y, b.y, c.y);
959 g->maxp.x = max3(a.x, b.x, c.x);
960 g->maxp.y = max3(a.y, b.y, c.y);
961 /* The alignment parameters in node->valign and node->halign
962 specifies the alignment in the non-rotated coordinate system
963 (very unlike pdf/postscript), which complicates matters.
965 switch (node->halign) {
966 case GFX_H_RIGHT: g->tdx = -g->maxp.x; break;
967 case GFX_H_CENTER: g->tdx = -(g->maxp.x + g->minp.x) / 2; break;
968 case GFX_H_LEFT: g->tdx = -g->minp.x; break;
969 case GFX_H_NULL: g->tdx = 0; break;
971 switch(node->valign){
972 case GFX_V_TOP: g->tdy = -g->maxp.y; break;
973 case GFX_V_CENTER: g->tdy = -(g->maxp.y + g->minp.y) / 2; break;
974 case GFX_V_BOTTOM: g->tdy = -g->minp.y; break;
975 case GFX_V_NULL: g->tdy = 0; break;
981 g->mx = g->x + g->tdx;
982 g->my = g->y + g->tdy;
983 g->tmx = g->mx - g->ascender * g->mc;
984 g->tmy = g->my - g->ascender * g->md;
985 PDF_DD(pdf_dump_calc(node, g))
988 /* ------- SVG -------
990 http://www.w3.org/TR/SVG/
992 static int svg_indent = 0;
993 static int svg_single_line = 0;
994 static const char *svg_default_font = "-dummy-";
995 typedef struct svg_dash
998 double dash_adjust, dash_len, dash_offset;
999 double adjusted_on, adjusted_off;
1003 static void svg_print_indent(FILE *fp)
1006 for (i = svg_indent - svg_single_line; i > 0; i--) {
1012 static void svg_start_tag(FILE *fp, const char *name)
1014 svg_print_indent(fp);
1020 static void svg_close_tag_single_line(FILE *fp)
1026 static void svg_close_tag(FILE *fp)
1029 if (!svg_single_line)
1033 static void svg_end_tag(FILE *fp, const char *name)
1035 /* name is NULL if closing empty-node tag */
1037 if (svg_single_line)
1040 svg_print_indent(fp);
1050 static void svg_close_tag_empty_node(FILE *fp)
1052 svg_end_tag(fp, NULL);
1055 static void svg_write_text(FILE *fp, const char *text)
1057 #ifdef HAVE_MBSTOWCS
1059 wchar_t *p, *cstr, ch;
1063 clen = strlen(text) + 1;
1064 cstr = malloc(sizeof(wchar_t) * clen);
1065 text_count = mbstowcs(cstr, text, clen);
1066 if (text_count == -1)
1067 text_count = mbstowcs(cstr, "Enc-Err", 6);
1070 unsigned char *p = text;
1071 unsigned char *cstr;
1078 ch = afm_fix_osx_charset(ch); /* unsafe macro */
1081 #ifdef HAVE_MBSTOWCS
1085 case '&': fputs("&", fp); break;
1086 case '<': fputs("<", fp); break;
1087 case '>': fputs(">", fp); break;
1088 case '"': fputs(""", fp); break;
1091 #ifdef HAVE_MBSTOWCS
1092 if (p <= cstr + 1 || !*p || *p == 32)
1093 fputs(" ", fp); /* non-breaking space in unicode */
1097 } else if (ch < 32 || ch >= 127)
1098 fprintf(fp, "&#%d;", (int)ch);
1105 static void svg_format_number(char *buf, int bufsize, double d)
1107 /* omit decimals if integer to reduce filesize */
1109 snprintf(buf, bufsize, "%.2f", d);
1110 p = buf; /* doesn't trust snprintf return value */
1116 *p = '\0'; /* zap trailing zeros */
1120 *p = '\0'; /* zap trailing dot */
1125 static void svg_write_number(FILE *fp, double d)
1128 svg_format_number(buf, sizeof(buf), d);
1132 static int svg_color_is_black(int c)
1134 /* gfx_color_t is RRGGBBAA */
1135 return c == 0x000000FF;
1138 static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
1140 /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
1141 gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
1142 gfx_color_t opacity = c & 0xFF;
1143 fprintf(fp, " %s=\"", attr);
1144 if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
1145 /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
1146 fprintf(fp, "#%03lX",
1147 ( ((rrggbb >> 8) & 0xF00)
1148 | ((rrggbb >> 4) & 0x0F0)
1149 | ( rrggbb & 0x00F)));
1151 fprintf(fp, "#%06lX", rrggbb);
1154 if (opacity != 0xFF) {
1155 fprintf(fp, " opacity=\"");
1156 svg_write_number(fp, opacity / 255.0);
1161 static void svg_get_dash(gfx_node_t *node, svg_dash *d)
1165 if (node->dash_on <= 0 || node->dash_off <= 0) {
1170 d->dash_len = node->dash_on + node->dash_off;
1171 /* dash on/off adjustment due to round caps */
1172 d->dash_adjust = 0.8 * node->size;
1173 d->adjusted_on = node->dash_on - d->dash_adjust;
1174 if (d->adjusted_on < 0.01)
1175 d->adjusted_on = 0.01;
1176 d->adjusted_off = d->dash_len - d->adjusted_on;
1177 /* dash offset calc */
1178 if (node->path[0].x == node->path[1].x) /* only good for horz/vert lines */
1179 offset = node->path[0].y;
1181 offset = node->path[0].x;
1182 mult = (int)fabs(offset / d->dash_len);
1183 d->dash_offset = offset - mult * d->dash_len;
1184 if (node->path[0].x < node->path[1].x || node->path[0].y < node->path[1].y)
1185 d->dash_offset = d->dash_len - d->dash_offset;
1188 static int svg_dash_equal(svg_dash *a, svg_dash *b)
1190 if (a->dash_enable != b->dash_enable)
1192 if (a->adjusted_on != b->adjusted_on)
1194 if (a->adjusted_off != b->adjusted_off)
1196 /* rest of properties will be the same when on+off are */
1200 static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
1203 svg_get_dash(node, &dash_info);
1204 fputs(" stroke-width=\"", fp);
1205 svg_write_number(fp, node->size);
1207 svg_write_color(fp, node->color, "stroke");
1208 fputs(" fill=\"none\"", fp);
1209 if (dash_info.dash_enable) {
1210 if (dash_info.dash_offset != 0) {
1211 fputs(" stroke-dashoffset=\"", fp);
1212 svg_write_number(fp, dash_info.dash_offset);
1215 fputs(" stroke-dasharray=\"", fp);
1216 svg_write_number(fp, dash_info.adjusted_on);
1218 svg_write_number(fp, dash_info.adjusted_off);
1223 static int svg_is_int_step(double a, double b)
1225 double diff = fabs(a - b);
1226 return floor(diff) == diff;
1229 static int svg_path_straight_segment(FILE *fp,
1230 double lastA, double currentA, double currentB,
1232 int segment_idx, int isx, char absChar, char relChar)
1234 if (!svg_is_int_step(lastA, currentA)) {
1236 svg_write_number(fp, currentA);
1239 if (segment_idx < node->points - 1) {
1240 ArtVpath *vec = node->path + segment_idx + 1;
1241 if (vec->code == ART_LINETO) {
1242 double nextA = (isx ? vec->x : vec->y) - LINEOFFSET;
1243 double nextB = (isx ? vec->y : vec->x) - LINEOFFSET;
1244 if (nextB == currentB
1245 && ((currentA >= lastA) == (nextA >= currentA))
1246 && svg_is_int_step(currentA, nextA)) {
1247 return 1; /* skip to next as it is a straight line */
1252 svg_write_number(fp, currentA - lastA);
1256 static void svg_path(FILE *fp, gfx_node_t *node, int multi)
1259 double lastX = 0, lastY = 0;
1260 /* for straight lines <path..> tags take less space than
1261 <line..> tags because of the efficient packing
1262 in the 'd' attribute */
1263 svg_start_tag(fp, "path");
1265 svg_common_path_attributes(fp, node);
1267 /* specification of the 'd' attribute: */
1268 /* http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation */
1269 for (i = 0; i < node->points; i++) {
1270 ArtVpath *vec = node->path + i;
1271 double x = vec->x - LINEOFFSET;
1272 double y = vec->y - LINEOFFSET;
1273 switch (vec->code) {
1274 case ART_MOVETO_OPEN: /* fall-through */
1277 svg_write_number(fp, x);
1279 svg_write_number(fp, y);
1282 /* try optimize filesize by using minimal lineto commands */
1283 /* without introducing rounding errors. */
1285 if (svg_path_straight_segment(fp, lastY, y, x, node, i, 0, 'V', 'v'))
1287 } else if (y == lastY) {
1288 if (svg_path_straight_segment(fp, lastX, x, y, node, i, 1, 'H', 'h'))
1292 svg_write_number(fp, x);
1294 svg_write_number(fp, y);
1297 case ART_CURVETO: break; /* unsupported */
1298 case ART_END: break; /* nop */
1303 if (node->closed_path)
1306 svg_close_tag_empty_node(fp);
1309 static void svg_multi_path(FILE *fp, gfx_node_t **nodeR)
1311 /* optimize for multiple paths with the same color, penwidth, etc. */
1313 gfx_node_t *node = *nodeR;
1314 gfx_node_t *next = node->next;
1316 if (next->type != node->type
1317 || next->size != node->size
1318 || next->color != node->color
1319 || next->dash_on != node->dash_on
1320 || next->dash_off != node->dash_off)
1326 svg_path(fp, node, 0);
1329 svg_start_tag(fp, "g");
1330 svg_common_path_attributes(fp, node);
1332 while (num && node) {
1333 svg_path(fp, node, 1);
1339 svg_end_tag(fp, "g");
1342 static void svg_area(FILE *fp, gfx_node_t *node)
1345 double startX = 0, startY = 0;
1346 svg_start_tag(fp, "polygon");
1348 svg_write_color(fp, node->color, "fill");
1349 fputs(" points=\"", fp);
1350 for (i = 0; i < node->points; i++) {
1351 ArtVpath *vec = node->path + i;
1352 double x = vec->x - LINEOFFSET;
1353 double y = vec->y - LINEOFFSET;
1354 switch (vec->code) {
1355 case ART_MOVETO_OPEN: /* fall-through */
1357 svg_write_number(fp, x);
1359 svg_write_number(fp, y);
1364 if (i == node->points - 2
1365 && node->path[i + 1].code == ART_END
1366 && fabs(x - startX) < 0.001 && fabs(y - startY) < 0.001) {
1367 break; /* poly area always closed, no need for last point */
1370 svg_write_number(fp, x);
1372 svg_write_number(fp, y);
1374 case ART_CURVETO: break; /* unsupported */
1375 case ART_END: break; /* nop */
1379 svg_close_tag_empty_node(fp);
1382 static void svg_text(FILE *fp, gfx_node_t *node)
1385 const char *fontname;
1386 /* as svg has 0,0 in top-left corner (like most screens) instead of
1387 bottom-left corner like pdf and eps, we have to fake the coords
1388 using offset and inverse sin(r) value */
1389 int page_height = 1000;
1390 pdf_calc(page_height, node, &g);
1391 if (node->angle != 0) {
1392 svg_start_tag(fp, "g");
1393 /* can't use svg_write_number as 2 decimals is far from enough to avoid
1395 fprintf(fp, " transform=\"matrix(%f,%f,%f,%f,%f,%f)\"",
1396 g.ma, -g.mb, -g.mc, g.md, g.tmx, page_height - g.tmy);
1399 svg_start_tag(fp, "text");
1402 svg_write_number(fp, g.tmx);
1403 fputs("\" y=\"", fp);
1404 svg_write_number(fp, page_height - g.tmy);
1407 fontname = afm_get_font_name(node->filename);
1408 if (strcmp(fontname, svg_default_font))
1409 fprintf(fp, " font-family=\"%s\"", fontname);
1410 fputs(" font-size=\"", fp);
1411 svg_write_number(fp, node->size);
1413 if (!svg_color_is_black(node->color))
1414 svg_write_color(fp, node->color, "fill");
1415 svg_close_tag_single_line(fp);
1416 /* support for node->tabwidth missing */
1417 svg_write_text(fp, node->text);
1418 svg_end_tag(fp, "text");
1419 if (node->angle != 0)
1420 svg_end_tag(fp, "g");
1423 int gfx_render_svg (gfx_canvas_t *canvas,
1424 art_u32 width, art_u32 height,
1425 gfx_color_t background, FILE *fp){
1426 gfx_node_t *node = canvas->firstnode;
1427 /* Find the first font used, and assume it is the mostly used
1428 one. It reduces the number of font-familty attributes. */
1430 if (node->type == GFX_TEXT && node->filename) {
1431 svg_default_font = afm_get_font_name(node->filename);
1437 "<?xml version=\"1.0\" standalone=\"no\"?>\n"
1438 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
1439 " \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
1441 " SVG file created by\n"
1442 " RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
1444 " The width/height attributes in the outhermost svg node\n"
1445 " are just default sizes for the browser which is used\n"
1446 " if the svg file is openened directly without being\n"
1447 " embedded in an html file.\n"
1448 " The viewBox is the local coord system for rrdtool.\n"
1450 svg_start_tag(fp, "svg");
1451 fputs(" width=\"", fp);
1452 svg_write_number(fp, width * canvas->zoom);
1453 fputs("\" height=\"", fp);
1454 svg_write_number(fp, height * canvas->zoom);
1455 fputs("\" x=\"0\" y=\"0\" viewBox=\"", fp);
1456 svg_write_number(fp, -LINEOFFSET);
1458 svg_write_number(fp, -LINEOFFSET);
1460 svg_write_number(fp, width - LINEOFFSET);
1462 svg_write_number(fp, height - LINEOFFSET);
1463 fputs("\" preserveAspectRatio=\"xMidYMid\"", fp);
1464 fprintf(fp, " font-family=\"%s\"", svg_default_font); /* default font */
1465 fputs(" stroke-linecap=\"round\" stroke-linejoin=\"round\"", fp);
1466 fputs(" xmlns=\"http://www.w3.org/2000/svg\"", fp);
1467 fputs(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"", fp);
1469 svg_start_tag(fp, "rect");
1470 fprintf(fp, " x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"", width, height);
1471 svg_write_color(fp, background, "fill");
1472 svg_close_tag_empty_node(fp);
1473 node = canvas->firstnode;
1475 switch (node->type) {
1477 svg_multi_path(fp, &node);
1487 svg_end_tag(fp, "svg");
1491 /* ------- EPS -------
1492 EPS and Postscript references:
1493 http://partners.adobe.com/asn/developer/technotes/postscript.html
1496 typedef struct eps_font
1498 const char *ps_font;
1500 struct eps_font *next;
1503 typedef struct eps_state
1506 gfx_canvas_t *canvas;
1507 art_u32 page_width, page_height;
1508 eps_font *font_list;
1514 int linecap, linejoin;
1518 static void eps_set_color(eps_state *state, gfx_color_t color)
1520 #if USE_EPS_FAKE_ALPHA
1523 /* gfx_color_t is RRGGBBAA */
1524 if (state->color == color)
1526 #if USE_EPS_FAKE_ALPHA
1527 a1 = (color & 255) / 255.0;
1528 a2 = 255 * (1 - a1);
1529 #define eps_color_calc(x) (int)( ((x) & 255) * a1 + a2)
1531 #define eps_color_calc(x) (int)( (x) & 255)
1533 /* gfx_color_t is RRGGBBAA */
1534 if (state->color == color)
1536 fprintf(state->fp, "%d %d %d Rgb\n",
1537 eps_color_calc(color >> 24),
1538 eps_color_calc(color >> 16),
1539 eps_color_calc(color >> 8));
1540 state->color = color;
1543 static int eps_add_font(eps_state *state, gfx_node_t *node)
1545 /* The fonts list could be postponed to the end using
1546 (atend), but let's be nice and have them in the header. */
1547 const char *ps_font = afm_get_font_postscript_name(node->filename);
1549 for (ef = state->font_list; ef; ef = ef->next) {
1550 if (!strcmp(ps_font, ef->ps_font))
1553 ef = malloc(sizeof(eps_font));
1555 rrd_set_error("malloc for eps_font");
1558 ef->next = state->font_list;
1559 ef->ps_font = ps_font;
1560 state->font_list = ef;
1564 static void eps_list_fonts(eps_state *state, const char *dscName)
1567 int lineLen = strlen(dscName);
1568 if (!state->font_list)
1570 fputs(dscName, state->fp);
1571 for (ef = state->font_list; ef; ef = ef->next) {
1572 int nameLen = strlen(ef->ps_font);
1573 if (lineLen + nameLen > 100 && lineLen) {
1574 fputs("\n", state->fp);
1575 fputs("%%- \n", state->fp);
1578 fputs(" ", state->fp);
1581 fputs(ef->ps_font, state->fp);
1584 fputs("\n", state->fp);
1587 static void eps_define_fonts(eps_state *state)
1590 if (!state->font_list)
1592 for (ef = state->font_list; ef; ef = ef->next) {
1593 /* PostScript¨ LANGUAGE REFERENCE third edition
1597 "/%s findfont dup length dict begin\n"
1598 "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
1599 "/Encoding ISOLatin1Encoding def\n"
1601 "/%s-ISOLatin1 exch definefont pop\n"
1602 "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
1603 ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
1607 static int eps_prologue(eps_state *state)
1611 "%!PS-Adobe-3.0 EPSF-3.0\n"
1612 "%%Creator: RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
1613 /* can't like weird chars here */
1614 "%%Title: (RRDtool output)\n"
1615 "%%DocumentData: Clean7Bit\n"
1617 fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
1618 state->page_width, state->page_height);
1619 for (node = state->canvas->firstnode; node; node = node->next) {
1620 if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
1623 eps_list_fonts(state, "%%DocumentFonts:");
1624 eps_list_fonts(state, "%%DocumentNeededFonts:");
1628 "%%EndProlog\n" /* must have, or BoundingBox is ignored */
1629 "/bd { bind def } bind def\n"
1631 fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
1633 "/X2 {X exch X exch} bd\n"
1634 "/M {X2 moveto} bd\n"
1635 "/L {X2 lineto} bd\n"
1639 "/CP {closepath} bd\n"
1640 "/WS {setlinewidth stroke} bd\n"
1643 "/T2 {concat 0 0 moveto show grestore} bd\n"
1644 "/T {moveto show} bd\n"
1645 "/Rgb { 255.0 div 3 1 roll\n"
1646 " 255.0 div 3 1 roll \n"
1647 " 255.0 div 3 1 roll setrgbcolor } bd\n"
1649 eps_define_fonts(state);
1653 static void eps_clear_dash(eps_state *state)
1655 if (!state->has_dash)
1657 state->has_dash = 0;
1658 fputs("[1 0] 0 setdash\n", state->fp);
1661 static void eps_write_linearea(eps_state *state, gfx_node_t *node)
1664 FILE *fp = state->fp;
1666 int clearDashIfAny = 1;
1667 eps_set_color(state, node->color);
1668 if (node->type == GFX_LINE) {
1670 if (state->linecap != 1) {
1671 fputs("1 setlinecap\n", fp);
1674 if (state->linejoin != 1) {
1675 fputs("1 setlinejoin\n", fp);
1676 state->linejoin = 1;
1678 svg_get_dash(node, &dash_info);
1679 if (dash_info.dash_enable) {
1681 state->has_dash = 1;
1683 svg_write_number(fp, dash_info.adjusted_on);
1685 svg_write_number(fp, dash_info.adjusted_off);
1687 svg_write_number(fp, dash_info.dash_offset);
1688 fputs(" setdash\n", fp);
1692 eps_clear_dash(state);
1693 for (i = 0; i < node->points; i++) {
1694 ArtVpath *vec = node->path + i;
1696 double y = state->page_height - vec->y;
1697 if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
1698 useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
1703 switch (vec->code) {
1704 case ART_MOVETO_OPEN: /* fall-through */
1706 svg_write_number(fp, x);
1708 svg_write_number(fp, y);
1710 fputs(useOffset ? "M\n" : "m\n", fp);
1713 svg_write_number(fp, x);
1715 svg_write_number(fp, y);
1717 fputs(useOffset ? "L\n" : "l\n", fp);
1719 case ART_CURVETO: break; /* unsupported */
1720 case ART_END: break; /* nop */
1723 if (node->type == GFX_LINE) {
1724 if (node->closed_path)
1726 if (node->size != state->line_width) {
1727 state->line_width = node->size;
1728 svg_write_number(fp, state->line_width);
1738 static void eps_write_text(eps_state *state, gfx_node_t *node)
1740 FILE *fp = state->fp;
1741 const char *ps_font = afm_get_font_postscript_name(node->filename);
1744 #ifdef HAVE_MBSTOWCS
1746 wchar_t *p, *cstr, ch;
1750 clen = strlen(node->text) + 1;
1751 cstr = malloc(sizeof(wchar_t) * clen);
1752 text_count = mbstowcs(cstr, node->text, clen);
1753 if (text_count == -1)
1754 text_count = mbstowcs(cstr, "Enc-Err", 6);
1757 const unsigned char *p = node->text;
1762 pdf_calc(state->page_height, node, &g);
1763 eps_set_color(state, node->color);
1764 if (strcmp(ps_font, state->font) || node->size != state->font_size) {
1765 state->font = ps_font;
1766 state->font_size = node->size;
1767 svg_write_number(fp, state->font_size);
1768 fprintf(fp, " SetFont-%s\n", state->font);
1778 ch = afm_fix_osx_charset(ch); /* unsafe macro */
1779 if (++lineLen > 70) {
1780 fputs("\\\n", fp); /* backslash and \n */
1803 } else if (ch >= 126 || ch < 32) {
1804 fprintf(fp, "\\%03o", (unsigned int)ch);
1812 #ifdef HAVE_MBSTOWCS
1816 /* can't use svg_write_number as 2 decimals is far from enough to avoid
1818 fprintf(fp, ") [%f %f %f %f %f %f] T2\n",
1819 g.ma, g.mb, g.mc, g.md, g.tmx, g.tmy);
1822 svg_write_number(fp, g.tmx);
1824 svg_write_number(fp, g.tmy);
1829 static int eps_write_content(eps_state *state)
1832 fputs("%\n", state->fp);
1833 for (node = state->canvas->firstnode; node; node = node->next) {
1834 switch (node->type) {
1837 eps_write_linearea(state, node);
1840 eps_write_text(state, node);
1847 int gfx_render_eps (gfx_canvas_t *canvas,
1848 art_u32 width, art_u32 height,
1849 gfx_color_t background, FILE *fp){
1850 struct eps_state state;
1852 state.canvas = canvas;
1853 state.page_width = width;
1854 state.page_height = height;
1855 state.font = "no-default-font";
1856 state.font_size = -1;
1857 state.color = 0; /* black */
1858 state.font_list = NULL;
1860 state.linejoin = -1;
1862 state.line_width = 1;
1863 if (eps_prologue(&state) == -1)
1865 eps_set_color(&state, background);
1866 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
1867 height, width, height, width);
1868 if (eps_write_content(&state) == -1)
1870 fputs("showpage\n", fp);
1871 fputs("%%EOF\n", fp);
1872 while (state.font_list) {
1873 eps_font *next = state.font_list->next;
1874 free(state.font_list);
1875 state.font_list = next;
1880 /* ------- PDF -------
1881 PDF references page:
1882 http://partners.adobe.com/public/developer/pdf/index_reference.html
1885 typedef struct pdf_buffer
1887 int id, is_obj, is_dict, is_stream, pdf_file_pos;
1889 int alloc_size, current_size;
1890 struct pdf_buffer *previous_buffer, *next_buffer;
1891 struct pdf_state *state;
1894 typedef struct pdf_font
1896 const char *ps_font;
1898 struct pdf_font *next;
1901 typedef struct pdf_state
1904 gfx_canvas_t *canvas;
1905 art_u32 page_width, page_height;
1906 pdf_font *font_list;
1907 pdf_buffer *first_buffer, *last_buffer;
1911 gfx_color_t stroke_color, fill_color;
1916 int linecap, linejoin;
1919 pdf_buffer pdf_header;
1920 pdf_buffer info_obj, catalog_obj, pages_obj, page1_obj;
1921 pdf_buffer fontsdict_obj;
1922 pdf_buffer graph_stream;
1925 static void pdf_init_buffer(pdf_state *state, pdf_buffer *buf)
1927 int initial_size = 32;
1930 buf->alloc_size = 0;
1931 buf->current_size = 0;
1932 buf->data = (char*)malloc(initial_size);
1934 buf->previous_buffer = NULL;
1935 buf->next_buffer = NULL;
1936 if (buf->data == NULL) {
1937 rrd_set_error("malloc for pdf_buffer data");
1938 state->has_failed = 1;
1941 buf->alloc_size = initial_size;
1942 if (state->last_buffer)
1943 state->last_buffer->next_buffer = buf;
1944 if (state->first_buffer == NULL)
1945 state->first_buffer = buf;
1946 buf->previous_buffer = state->last_buffer;
1947 state->last_buffer = buf;
1950 static void pdf_put(pdf_buffer *buf, const char *text, int len)
1954 if (buf->alloc_size < buf->current_size + len) {
1955 int new_size = buf->alloc_size;
1957 while (new_size < buf->current_size + len)
1959 new_buf = (char*)malloc(new_size);
1960 if (new_buf == NULL) {
1961 rrd_set_error("re-malloc for pdf_buffer data");
1962 buf->state->has_failed = 1;
1965 memcpy(new_buf, buf->data, buf->current_size);
1967 buf->data = new_buf;
1968 buf->alloc_size = new_size;
1970 memcpy(buf->data + buf->current_size, text, len);
1971 buf->current_size += len;
1974 static void pdf_put_char(pdf_buffer *buf, char c)
1976 if (buf->alloc_size >= buf->current_size + 1) {
1977 buf->data[buf->current_size++] = c;
1981 pdf_put(buf, tmp, 1);
1985 static void pdf_puts(pdf_buffer *buf, const char *text)
1987 pdf_put(buf, text, strlen(text));
1990 static void pdf_indent(pdf_buffer *buf)
1992 pdf_puts(buf, "\t");
1995 static void pdf_putsi(pdf_buffer *buf, const char *text)
1998 pdf_puts(buf, text);
2001 static void pdf_putint(pdf_buffer *buf, int i)
2004 sprintf(tmp, "%d", i);
2008 static void pdf_putnumber(pdf_buffer *buf, double d)
2011 svg_format_number(tmp, sizeof(tmp), d);
2015 static void pdf_put_string_contents_wide(pdf_buffer *buf, const afm_char *text)
2017 const afm_char *p = text;
2020 ch = afm_fix_osx_charset(ch); /* unsafe macro */
2025 pdf_puts(buf, "\\(");
2028 pdf_puts(buf, "\\)");
2031 pdf_puts(buf, "\\\\");
2034 pdf_puts(buf, "\\n");
2037 pdf_puts(buf, "\\r");
2040 pdf_puts(buf, "\\t");
2044 pdf_put_char(buf, '?');
2045 } else if (ch > 125 || ch < 32) {
2046 pdf_put_char(buf, ch);
2049 snprintf(tmp, sizeof(tmp), "\\%03o", (int)ch);
2057 static void pdf_put_string_contents(pdf_buffer *buf, const char *text)
2059 #ifdef HAVE_MBSTOWCS
2060 size_t clen = strlen(text) + 1;
2061 wchar_t *cstr = malloc(sizeof(wchar_t) * clen);
2062 int text_count = mbstowcs(cstr, text, clen);
2063 if (text_count == -1)
2064 text_count = mbstowcs(cstr, "Enc-Err", 6);
2065 pdf_put_string_contents_wide(buf, cstr);
2068 fprintf(stderr, "Decoding utf8 for '%s'\n", text);
2071 fprintf(stderr, "sz wc = %d\n", sizeof(wchar_t));
2073 fprintf(stderr, " %d = %c versus %d = %c\n", *p, (char)*p, 255 & (int)*pp, *pp);
2081 pdf_put_string_contents_wide(buf, text);
2085 static void pdf_init_object(pdf_state *state, pdf_buffer *buf)
2087 pdf_init_buffer(state, buf);
2088 buf->id = ++state->last_obj_id;
2093 static void pdf_init_dict(pdf_state *state, pdf_buffer *buf)
2095 pdf_init_object(state, buf);
2099 static void pdf_set_color(pdf_buffer *buf, gfx_color_t color,
2100 gfx_color_t *current_color, const char *op)
2102 #if USE_PDF_FAKE_ALPHA
2105 /* gfx_color_t is RRGGBBAA */
2106 if (*current_color == color)
2108 #if USE_PDF_FAKE_ALPHA
2109 a1 = (color & 255) / 255.0;
2111 #define pdf_color_calc(x) ( ((x) & 255) / 255.0 * a1 + a2)
2113 #define pdf_color_calc(x) ( ((x) & 255) / 255.0)
2115 pdf_putnumber(buf, pdf_color_calc(color >> 24));
2117 pdf_putnumber(buf, pdf_color_calc(color >> 16));
2119 pdf_putnumber(buf, pdf_color_calc(color >> 8));
2122 pdf_puts(buf, "\n");
2123 *current_color = color;
2126 static void pdf_set_stroke_color(pdf_buffer *buf, gfx_color_t color)
2128 pdf_set_color(buf, color, &buf->state->stroke_color, "RG");
2131 static void pdf_set_fill_color(pdf_buffer *buf, gfx_color_t color)
2133 pdf_set_color(buf, color, &buf->state->fill_color, "rg");
2136 static pdf_font *pdf_find_font(pdf_state *state, gfx_node_t *node)
2138 const char *ps_font = afm_get_font_postscript_name(node->filename);
2140 for (ef = state->font_list; ef; ef = ef->next) {
2141 if (!strcmp(ps_font, ef->ps_font))
2147 static void pdf_add_font(pdf_state *state, gfx_node_t *node)
2149 pdf_font *ef = pdf_find_font(state, node);
2152 ef = malloc(sizeof(pdf_font));
2154 rrd_set_error("malloc for pdf_font");
2155 state->has_failed = 1;
2158 pdf_init_dict(state, &ef->obj);
2159 ef->next = state->font_list;
2160 ef->ps_font = afm_get_font_postscript_name(node->filename);
2161 state->font_list = ef;
2163 pdf_putsi(&state->fontsdict_obj, "/F");
2164 pdf_putint(&state->fontsdict_obj, ef->obj.id);
2165 pdf_puts(&state->fontsdict_obj, " ");
2166 pdf_putint(&state->fontsdict_obj, ef->obj.id);
2167 pdf_puts(&state->fontsdict_obj, " 0 R\n");
2169 pdf_putsi(&ef->obj, "/Type /Font\n");
2170 pdf_putsi(&ef->obj, "/Subtype /Type1\n");
2171 pdf_putsi(&ef->obj, "/Name /F");
2172 pdf_putint(&ef->obj, ef->obj.id);
2173 pdf_puts(&ef->obj, "\n");
2174 pdf_putsi(&ef->obj, "/BaseFont /");
2175 pdf_puts(&ef->obj, ef->ps_font);
2176 pdf_puts(&ef->obj, "\n");
2177 pdf_putsi(&ef->obj, "/Encoding /WinAnsiEncoding\n");
2178 /* 'Cp1252' (this is latin 1 extended with 27 characters;
2179 the encoding is also known as 'winansi')
2180 http://www.lowagie.com/iText/tutorial/ch09.html */
2183 static void pdf_create_fonts(pdf_state *state)
2186 for (node = state->canvas->firstnode; node; node = node->next) {
2187 if (node->type == GFX_TEXT)
2188 pdf_add_font(state, node);
2192 static void pdf_write_linearea(pdf_state *state, gfx_node_t *node)
2195 pdf_buffer *s = &state->graph_stream;
2196 if (node->type == GFX_LINE) {
2198 svg_get_dash(node, &dash_info);
2199 if (!svg_dash_equal(&dash_info, &state->dash)) {
2200 state->dash = dash_info;
2201 if (dash_info.dash_enable) {
2203 pdf_putnumber(s, dash_info.adjusted_on);
2205 pdf_putnumber(s, dash_info.adjusted_off);
2207 pdf_putnumber(s, dash_info.dash_offset);
2208 pdf_puts(s, " d\n");
2210 pdf_puts(s, "[] 0 d\n");
2213 pdf_set_stroke_color(s, node->color);
2214 if (state->linecap != 1) {
2215 pdf_puts(s, "1 j\n");
2218 if (state->linejoin != 1) {
2219 pdf_puts(s, "1 J\n");
2220 state->linejoin = 1;
2222 if (node->size != state->line_width) {
2223 state->line_width = node->size;
2224 pdf_putnumber(s, state->line_width);
2225 pdf_puts(s, " w\n");
2228 pdf_set_fill_color(s, node->color);
2230 for (i = 0; i < node->points; i++) {
2231 ArtVpath *vec = node->path + i;
2233 double y = state->page_height - vec->y;
2234 if (node->type == GFX_AREA) {
2235 x += LINEOFFSET; /* adjust for libart handling of areas */
2238 switch (vec->code) {
2239 case ART_MOVETO_OPEN: /* fall-through */
2241 pdf_putnumber(s, x);
2243 pdf_putnumber(s, y);
2244 pdf_puts(s, " m\n");
2247 pdf_putnumber(s, x);
2249 pdf_putnumber(s, y);
2250 pdf_puts(s, " l\n");
2252 case ART_CURVETO: break; /* unsupported */
2253 case ART_END: break; /* nop */
2256 if (node->type == GFX_LINE) {
2257 pdf_puts(s, node->closed_path ? "s\n" : "S\n");
2264 static void pdf_write_matrix(pdf_state *state, gfx_node_t *node, pdf_coords *g, int useTM)
2267 pdf_buffer *s = &state->graph_stream;
2268 if (node->angle == 0) {
2269 pdf_puts(s, "1 0 0 1 ");
2270 pdf_putnumber(s, useTM ? g->tmx : g->mx);
2272 pdf_putnumber(s, useTM ? g->tmy : g->my);
2274 /* can't use svg_write_number as 2 decimals is far from enough to avoid
2276 sprintf(tmp, "%f %f %f %f %f %f",
2277 g->ma, g->mb, g->mc, g->md,
2278 useTM ? g->tmx : g->mx,
2279 useTM ? g->tmy : g->my);
2284 static void pdf_write_text(pdf_state *state, gfx_node_t *node,
2285 int last_was_text, int next_is_text)
2288 pdf_buffer *s = &state->graph_stream;
2289 pdf_font *font = pdf_find_font(state, node);
2291 rrd_set_error("font disappeared");
2292 state->has_failed = 1;
2295 pdf_calc(state->page_height, node, &g);
2297 pdf_puts(s, "q % debug green box\n");
2298 pdf_write_matrix(state, node, &g, 0);
2299 pdf_puts(s, " cm\n");
2300 pdf_set_fill_color(s, 0x90FF9000);
2301 pdf_puts(s, "0 0.4 0 rg\n");
2302 pdf_puts(s, "0 0 ");
2303 pdf_putnumber(s, g.sizep.x);
2305 pdf_putnumber(s, g.sizep.y);
2306 pdf_puts(s, " re\n");
2310 pdf_set_fill_color(s, node->color);
2311 if (PDF_CALC_DEBUG || !last_was_text)
2312 pdf_puts(s, "BT\n");
2313 if (state->font_id != font->obj.id || node->size != state->font_size) {
2314 state->font_id = font->obj.id;
2315 state->font_size = node->size;
2317 pdf_putint(s, font->obj.id);
2319 pdf_putnumber(s, node->size);
2320 pdf_puts(s, " Tf\n");
2322 pdf_write_matrix(state, node, &g, 1);
2323 pdf_puts(s, " Tm\n");
2325 pdf_put_string_contents(s, node->text);
2326 pdf_puts(s, ") Tj\n");
2327 if (PDF_CALC_DEBUG || !next_is_text)
2328 pdf_puts(s, "ET\n");
2331 static void pdf_write_content(pdf_state *state)
2334 int last_was_text = 0, next_is_text;
2335 for (node = state->canvas->firstnode; node; node = node->next) {
2336 switch (node->type) {
2339 pdf_write_linearea(state, node);
2342 next_is_text = node->next && node->next->type == GFX_TEXT;
2343 pdf_write_text(state, node, last_was_text, next_is_text);
2346 last_was_text = node->type == GFX_TEXT;
2350 static void pdf_init_document(pdf_state *state)
2352 pdf_init_buffer(state, &state->pdf_header);
2353 pdf_init_dict(state, &state->catalog_obj);
2354 pdf_init_dict(state, &state->info_obj);
2355 pdf_init_dict(state, &state->pages_obj);
2356 pdf_init_dict(state, &state->page1_obj);
2357 pdf_init_dict(state, &state->fontsdict_obj);
2358 pdf_create_fonts(state);
2359 if (state->has_failed)
2361 /* make stream last object in file */
2362 pdf_init_object(state, &state->graph_stream);
2363 state->graph_stream.is_stream = 1;
2366 static void pdf_setup_document(pdf_state *state)
2368 const char *creator = "RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch";
2369 /* all objects created by now, so init code can reference them */
2371 pdf_puts(&state->pdf_header, "%PDF-1.3\n");
2372 /* following 8 bit comment is recommended by Adobe for
2373 indicating binary file to file transfer applications */
2374 pdf_puts(&state->pdf_header, "%\xE2\xE3\xCF\xD3\n");
2376 pdf_putsi(&state->info_obj, "/Creator (");
2377 pdf_put_string_contents(&state->info_obj, creator);
2378 pdf_puts(&state->info_obj, ")\n");
2380 pdf_putsi(&state->catalog_obj, "/Type /Catalog\n");
2381 pdf_putsi(&state->catalog_obj, "/Pages ");
2382 pdf_putint(&state->catalog_obj, state->pages_obj.id);
2383 pdf_puts(&state->catalog_obj, " 0 R\n");
2385 pdf_putsi(&state->pages_obj, "/Type /Pages\n");
2386 pdf_putsi(&state->pages_obj, "/Kids [");
2387 pdf_putint(&state->pages_obj, state->page1_obj.id);
2388 pdf_puts(&state->pages_obj, " 0 R]\n");
2389 pdf_putsi(&state->pages_obj, "/Count 1\n");
2391 pdf_putsi(&state->page1_obj, "/Type /Page\n");
2392 pdf_putsi(&state->page1_obj, "/Parent ");
2393 pdf_putint(&state->page1_obj, state->pages_obj.id);
2394 pdf_puts(&state->page1_obj, " 0 R\n");
2395 pdf_putsi(&state->page1_obj, "/MediaBox [0 0 ");
2396 pdf_putint(&state->page1_obj, state->page_width);
2397 pdf_puts(&state->page1_obj, " ");
2398 pdf_putint(&state->page1_obj, state->page_height);
2399 pdf_puts(&state->page1_obj, "]\n");
2400 pdf_putsi(&state->page1_obj, "/Contents ");
2401 pdf_putint(&state->page1_obj, state->graph_stream.id);
2402 pdf_puts(&state->page1_obj, " 0 R\n");
2403 pdf_putsi(&state->page1_obj, "/Resources << /Font ");
2404 pdf_putint(&state->page1_obj, state->fontsdict_obj.id);
2405 pdf_puts(&state->page1_obj, " 0 R >>\n");
2408 static void pdf_write_string_to_file(pdf_state *state, const char *text)
2410 fputs(text, state->fp);
2411 state->pdf_file_pos += strlen(text);
2414 static void pdf_write_buf_to_file(pdf_state *state, pdf_buffer *buf)
2417 buf->pdf_file_pos = state->pdf_file_pos;
2419 snprintf(tmp, sizeof(tmp), "%d 0 obj\n", buf->id);
2420 pdf_write_string_to_file(state, tmp);
2423 pdf_write_string_to_file(state, "<<\n");
2424 if (buf->is_stream) {
2425 snprintf(tmp, sizeof(tmp), "<< /Length %d >>\n", buf->current_size);
2426 pdf_write_string_to_file(state, tmp);
2427 pdf_write_string_to_file(state, "stream\n");
2429 fwrite(buf->data, 1, buf->current_size, state->fp);
2430 state->pdf_file_pos += buf->current_size;
2432 pdf_write_string_to_file(state, "endstream\n");
2434 pdf_write_string_to_file(state, ">>\n");
2436 pdf_write_string_to_file(state, "endobj\n");
2439 static void pdf_write_to_file(pdf_state *state)
2441 pdf_buffer *buf = state->first_buffer;
2443 state->pdf_file_pos = 0;
2444 pdf_write_buf_to_file(state, &state->pdf_header);
2447 pdf_write_buf_to_file(state, buf);
2448 buf = buf->next_buffer;
2450 xref_pos = state->pdf_file_pos;
2451 fprintf(state->fp, "xref\n");
2452 fprintf(state->fp, "%d %d\n", 0, state->last_obj_id + 1);
2453 /* TOC lines must be exactly 20 bytes including \n */
2454 fprintf(state->fp, "%010d %05d f\x20\n", 0, 65535);
2455 for (buf = state->first_buffer; buf; buf = buf->next_buffer) {
2457 fprintf(state->fp, "%010d %05d n\x20\n", buf->pdf_file_pos, 0);
2459 fprintf(state->fp, "trailer\n");
2460 fprintf(state->fp, "<<\n");
2461 fprintf(state->fp, "\t/Size %d\n", state->last_obj_id + 1);
2462 fprintf(state->fp, "\t/Root %d 0 R\n", state->catalog_obj.id);
2463 fprintf(state->fp, "\t/Info %d 0 R\n", state->info_obj.id);
2464 fprintf(state->fp, ">>\n");
2465 fprintf(state->fp, "startxref\n");
2466 fprintf(state->fp, "%d\n", xref_pos);
2467 fputs("%%EOF\n", state->fp);
2470 static void pdf_free_resources(pdf_state *state)
2472 pdf_buffer *buf = state->first_buffer;
2476 buf->alloc_size = buf->current_size = 0;
2477 buf = buf->next_buffer;
2479 while (state->font_list) {
2480 pdf_font *next = state->font_list->next;
2481 free(state->font_list);
2482 state->font_list = next;
2486 int gfx_render_pdf (gfx_canvas_t *canvas,
2487 art_u32 width, art_u32 height,
2488 gfx_color_t UNUSED(background), FILE *fp){
2489 struct pdf_state state;
2490 memset(&state, 0, sizeof(pdf_state));
2492 state.canvas = canvas;
2493 state.page_width = width;
2494 state.page_height = height;
2496 state.font_size = -1;
2497 state.font_list = NULL;
2499 state.linejoin = -1;
2500 pdf_init_document(&state);
2502 pdf_set_color(&state, background);
2503 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
2504 height, width, height, width);
2506 if (!state.has_failed)
2507 pdf_write_content(&state);
2508 if (!state.has_failed)
2509 pdf_setup_document(&state);
2510 if (!state.has_failed)
2511 pdf_write_to_file(&state);
2512 pdf_free_resources(&state);
2513 return state.has_failed ? -1 : 0;