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));
627 pen_x = node->x * canvas->zoom;
628 pen_y = node->y * canvas->zoom;
630 string = gfx_string_create (canvas, face, node->text, node->angle, node->tabwidth, node->size);
633 switch(node->halign){
634 case GFX_H_RIGHT: vec.x = -string->bbox.xMax;
636 case GFX_H_CENTER: vec.x = abs(string->bbox.xMax) >= abs(string->bbox.xMin) ?
637 -string->bbox.xMax/2:-string->bbox.xMin/2;
639 case GFX_H_LEFT: vec.x = -string->bbox.xMin;
641 case GFX_H_NULL: vec.x = 0;
645 switch(node->valign){
646 case GFX_V_TOP: vec.y = string->bbox.yMax;
648 case GFX_V_CENTER: vec.y = abs(string->bbox.yMax) >= abs(string->bbox.yMin) ?
649 string->bbox.yMax/2:string->bbox.yMin/2;
651 case GFX_V_BOTTOM: vec.y = 0;
653 case GFX_V_NULL: vec.y = 0;
658 glyph = string->glyphs;
659 for(n=0; n<string->num_glyphs; n++, glyph++) {
663 /* long buf_x,comp_n; */
664 /* make copy to transform */
665 if (! glyph->image) {
666 RRDPRINTF("no image\n")
669 error = FT_Glyph_Copy (glyph->image, &image);
671 RRDPRINTF("couldn't copy image\n")
677 FT_Vector_Transform (&vec, &string->transform);
679 bit = (FT_BitmapGlyph) image;
680 gr = bit->bitmap.num_grays -1;
682 buf_x = (pen_x + 0.5) + (double)bit->left;
683 comp_n = buf_x + bit->bitmap.width > pys_width ? pys_width - buf_x : bit->bitmap.width;
684 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
685 buf_x *= bytes_per_pixel ;
686 for (iy=0; iy < bit->bitmap.rows; iy++){
687 long buf_y = iy+(pen_y+0.5)-(double)bit->top;
688 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
690 for (ix=0;ix < bit->bitmap.width;ix++){
691 *(letter + (ix*bytes_per_pixel+3)) = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
693 art_rgba_rgba_composite(buffer + buf_y + buf_x ,letter,comp_n);
697 switch ( bit->bitmap.pixel_mode ) {
698 case FT_PIXEL_MODE_GRAY:
699 for (iy=0; iy < bit->bitmap.rows; iy++){
700 long buf_y = iy+(pen_y+0.5)-bit->top;
701 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
703 for (ix=0;ix < bit->bitmap.width;ix++){
704 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
707 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
708 buf_x *= bytes_per_pixel ;
709 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix);
711 fcolor[3] = (art_u8)((double)font_alpha / gr * falpha);
712 art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
718 case FT_PIXEL_MODE_MONO:
719 for (iy=0; iy < bit->bitmap.rows; iy++){
720 long buf_y = iy+(pen_y+0.5)-bit->top;
721 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
723 for (ix=0;ix < bit->bitmap.width;ix++){
724 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
726 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
727 buf_x *= bytes_per_pixel ;
728 if ( (fcolor[3] = falpha * ((*(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix/8) >> (7 - (ix % 8))) & 1)) > 0 )
729 art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
735 rrd_set_error("unknown freetype pixel mode: %d", bit->bitmap.pixel_mode);
740 for (iy=0; iy < bit->bitmap.rows; iy++){
741 long buf_y = iy+(pen_y+0.5)-bit->top;
742 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
744 for (ix=0;ix < bit->bitmap.width;ix++){
745 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
748 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
749 buf_x *= bytes_per_pixel ;
750 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
751 font_alpha = (art_u8)((double)font_alpha / gr * falpha);
752 for (iz = 0; iz < 3; iz++){
753 art_u8 *orig = buffer + buf_y + buf_x + iz;
754 *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
755 (double)fcolor[iz] / gr * (font_alpha));
760 FT_Done_Glyph (image);
762 gfx_string_destroy(string);
767 gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
769 FT_Done_FreeType( library );
773 /* free memory used by nodes this will also remove memory required for
774 associated paths and svcs ... but not for text strings */
776 gfx_destroy (gfx_canvas_t *canvas){
777 gfx_node_t *next,*node = canvas->firstnode;
780 art_free(node->path);
782 free(node->filename);
790 static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
791 png_structp png_ptr = NULL;
792 png_infop info_ptr = NULL;
794 png_bytep *row_pointers;
795 int rowstride = width * bytes_per_pixel;
801 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
806 row_pointers = (png_bytepp)png_malloc(png_ptr,
807 height*sizeof(png_bytep));
809 info_ptr = png_create_info_struct(png_ptr);
811 if (info_ptr == NULL)
813 png_free(png_ptr,row_pointers);
814 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
818 if (setjmp(png_jmpbuf(png_ptr)))
820 /* If we get here, we had a problem writing the file */
821 png_destroy_write_struct(&png_ptr, &info_ptr);
825 png_init_io(png_ptr, fp);
826 png_set_IHDR (png_ptr, info_ptr,width, height,
827 8, PNG_COLOR_TYPE_RGB_ALPHA,
829 PNG_COMPRESSION_TYPE_DEFAULT,
830 PNG_FILTER_TYPE_DEFAULT);
832 text[0].key = "Software";
833 text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
834 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
835 png_set_text (png_ptr, info_ptr, text, 1);
837 /* lets make this fast while ending up with some increass in image size */
838 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
839 /* png_set_filter(png_ptr,0,PNG_FILTER_SUB); */
840 png_set_compression_level(png_ptr,1);
841 /* png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY); */
843 png_set_filter(png_ptr,PNG_FILTER_TYPE_BASE,PNG_FILTER_SUB);
844 png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY);
845 png_set_compression_level(png_ptr,Z_BEST_SPEED); */
847 /* Write header data */
848 png_write_info (png_ptr, info_ptr);
849 for (i = 0; i < height; i++)
850 row_pointers[i] = (png_bytep) (buffer + i*rowstride);
852 png_write_image(png_ptr, row_pointers);
853 png_write_end(png_ptr, info_ptr);
854 png_free(png_ptr,row_pointers);
855 png_destroy_write_struct(&png_ptr, &info_ptr);
860 /* ----- COMMON ROUTINES for pdf, svg and eps */
861 #define min3(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
862 #define max3(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
864 #define PDF_CALC_DEBUG 0
866 typedef struct pdf_point
873 double ascender, descender, baselineY;
874 pdf_point sizep, minp, maxp;
875 double x, y, tdx, tdy;
876 double r, cos_r, sin_r;
877 double ma, mb, mc, md, mx, my; /* pdf coord matrix */
878 double tmx, tmy; /* last 2 coords of text coord matrix */
885 static void pdf_dump_calc(gfx_node_t *node, pdf_coords *g)
887 fprintf(stderr, "PDF CALC =============================\n");
888 fprintf(stderr, " '%s' at %f pt\n", node->text, node->size);
889 fprintf(stderr, " align h = %s, v = %s, sizep = %f, %f\n",
890 (node->halign == GFX_H_RIGHT ? "r" :
891 (node->halign == GFX_H_CENTER ? "c" :
892 (node->halign == GFX_H_LEFT ? "l" : "N"))),
893 (node->valign == GFX_V_TOP ? "t" :
894 (node->valign == GFX_V_CENTER ? "c" :
895 (node->valign == GFX_V_BOTTOM ? "b" : "N"))),
896 g->sizep.x, g->sizep.y);
897 fprintf(stderr, " r = %f = %f, cos = %f, sin = %f\n",
898 g->r, node->angle, g->cos_r, g->sin_r);
899 fprintf(stderr, " ascender = %f, descender = %f, baselineY = %f\n",
900 g->ascender, g->descender, g->baselineY);
901 fprintf(stderr, " sizep: %f, %f\n", g->sizep.x, g->sizep.y);
902 fprintf(stderr, " minp: %f, %f maxp = %f, %f\n",
903 g->minp.x, g->minp.y, g->maxp.x, g->maxp.y);
904 fprintf(stderr, " x = %f, y = %f\n", g->x, g->y);
905 fprintf(stderr, " tdx = %f, tdy = %f\n", g->tdx, g->tdy);
906 fprintf(stderr, " GM = %f, %f, %f, %f, %f, %f\n",
907 g->ma, g->mb, g->mc, g->md, g->mx, g->my);
908 fprintf(stderr, " TM = %f, %f, %f, %f, %f, %f\n",
909 g->ma, g->mb, g->mc, g->md, g->tmx, g->tmy);
914 #define PDF_DD(x) if (g->debug) x;
919 static void pdf_rotate(pdf_coords *g, pdf_point *p)
921 double x2 = g->cos_r * p->x - g->sin_r * p->y;
922 double y2 = g->sin_r * p->x + g->cos_r * p->y;
923 PDF_DD( fprintf(stderr, " rotate(%f, %f) -> %f, %f\n", p->x, p->y, x2, y2))
929 static void pdf_calc(int page_height, gfx_node_t *node, pdf_coords *g)
933 /* g->debug = !!strstr(node->text, "RevProxy-1") || !!strstr(node->text, "08:00"); */
934 g->debug = !!strstr(node->text, "sekunder") || !!strstr(node->text, "Web");
937 g->y = page_height - node->y;
939 g->r = 2 * M_PI * node->angle / 360.0;
940 g->cos_r = cos(g->r);
941 g->sin_r = sin(g->r);
947 g->ascender = afm_get_ascender(node->filename, node->size);
948 g->descender = afm_get_descender(node->filename, node->size);
949 g->sizep.x = afm_get_text_width(0, node->filename, node->size, node->tabwidth, node->text);
950 /* seems like libart ignores the descender when doing vertial-align = bottom,
951 so we do that too, to get labels v-aligning properly */
952 g->sizep.y = -g->ascender; /* + afm_get_descender(font->ps_font, node->size); */
953 g->baselineY = -g->ascender - g->sizep.y / 2;
954 a.x = g->sizep.x; a.y = g->sizep.y;
955 b.x = g->sizep.x; b.y = 0;
956 c.x = 0; c.y = g->sizep.y;
962 g->minp.x = min3(a.x, b.x, c.x);
963 g->minp.y = min3(a.y, b.y, c.y);
964 g->maxp.x = max3(a.x, b.x, c.x);
965 g->maxp.y = max3(a.y, b.y, c.y);
966 /* The alignment parameters in node->valign and node->halign
967 specifies the alignment in the non-rotated coordinate system
968 (very unlike pdf/postscript), which complicates matters.
970 switch (node->halign) {
971 case GFX_H_RIGHT: g->tdx = -g->maxp.x; break;
972 case GFX_H_CENTER: g->tdx = -(g->maxp.x + g->minp.x) / 2; break;
973 case GFX_H_LEFT: g->tdx = -g->minp.x; break;
974 case GFX_H_NULL: g->tdx = 0; break;
976 switch(node->valign){
977 case GFX_V_TOP: g->tdy = -g->maxp.y; break;
978 case GFX_V_CENTER: g->tdy = -(g->maxp.y + g->minp.y) / 2; break;
979 case GFX_V_BOTTOM: g->tdy = -g->minp.y; break;
980 case GFX_V_NULL: g->tdy = 0; break;
986 g->mx = g->x + g->tdx;
987 g->my = g->y + g->tdy;
988 g->tmx = g->mx - g->ascender * g->mc;
989 g->tmy = g->my - g->ascender * g->md;
990 PDF_DD(pdf_dump_calc(node, g))
993 /* ------- SVG -------
995 http://www.w3.org/TR/SVG/
997 static int svg_indent = 0;
998 static int svg_single_line = 0;
999 static const char *svg_default_font = "-dummy-";
1000 typedef struct svg_dash
1003 double dash_adjust, dash_len, dash_offset;
1004 double adjusted_on, adjusted_off;
1008 static void svg_print_indent(FILE *fp)
1011 for (i = svg_indent - svg_single_line; i > 0; i--) {
1017 static void svg_start_tag(FILE *fp, const char *name)
1019 svg_print_indent(fp);
1025 static void svg_close_tag_single_line(FILE *fp)
1031 static void svg_close_tag(FILE *fp)
1034 if (!svg_single_line)
1038 static void svg_end_tag(FILE *fp, const char *name)
1040 /* name is NULL if closing empty-node tag */
1042 if (svg_single_line)
1045 svg_print_indent(fp);
1055 static void svg_close_tag_empty_node(FILE *fp)
1057 svg_end_tag(fp, NULL);
1060 static void svg_write_text(FILE *fp, const char *text)
1062 #ifdef HAVE_MBSTOWCS
1064 wchar_t *p, *cstr, ch;
1068 clen = strlen(text) + 1;
1069 cstr = malloc(sizeof(wchar_t) * clen);
1070 text_count = mbstowcs(cstr, text, clen);
1071 if (text_count == -1)
1072 text_count = mbstowcs(cstr, "Enc-Err", 6);
1075 unsigned char *p = text;
1076 unsigned char *cstr;
1083 ch = afm_fix_osx_charset(ch); /* unsafe macro */
1086 #ifdef HAVE_MBSTOWCS
1090 case '&': fputs("&", fp); break;
1091 case '<': fputs("<", fp); break;
1092 case '>': fputs(">", fp); break;
1093 case '"': fputs(""", fp); break;
1096 #ifdef HAVE_MBSTOWCS
1097 if (p <= cstr + 1 || !*p || *p == 32)
1098 fputs(" ", fp); /* non-breaking space in unicode */
1102 } else if (ch < 32 || ch >= 127)
1103 fprintf(fp, "&#%d;", (int)ch);
1110 static void svg_format_number(char *buf, int bufsize, double d)
1112 /* omit decimals if integer to reduce filesize */
1114 snprintf(buf, bufsize, "%.2f", d);
1115 p = buf; /* doesn't trust snprintf return value */
1121 *p = '\0'; /* zap trailing zeros */
1125 *p = '\0'; /* zap trailing dot */
1130 static void svg_write_number(FILE *fp, double d)
1133 svg_format_number(buf, sizeof(buf), d);
1137 static int svg_color_is_black(int c)
1139 /* gfx_color_t is RRGGBBAA */
1140 return c == 0x000000FF;
1143 static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
1145 /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
1146 gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
1147 gfx_color_t opacity = c & 0xFF;
1148 fprintf(fp, " %s=\"", attr);
1149 if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
1150 /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
1151 fprintf(fp, "#%03lX",
1152 ( ((rrggbb >> 8) & 0xF00)
1153 | ((rrggbb >> 4) & 0x0F0)
1154 | ( rrggbb & 0x00F)));
1156 fprintf(fp, "#%06lX", rrggbb);
1159 if (opacity != 0xFF) {
1160 fprintf(fp, " opacity=\"");
1161 svg_write_number(fp, opacity / 255.0);
1166 static void svg_get_dash(gfx_node_t *node, svg_dash *d)
1170 if (node->dash_on <= 0 || node->dash_off <= 0) {
1175 d->dash_len = node->dash_on + node->dash_off;
1176 /* dash on/off adjustment due to round caps */
1177 d->dash_adjust = 0.8 * node->size;
1178 d->adjusted_on = node->dash_on - d->dash_adjust;
1179 if (d->adjusted_on < 0.01)
1180 d->adjusted_on = 0.01;
1181 d->adjusted_off = d->dash_len - d->adjusted_on;
1182 /* dash offset calc */
1183 if (node->path[0].x == node->path[1].x) /* only good for horz/vert lines */
1184 offset = node->path[0].y;
1186 offset = node->path[0].x;
1187 mult = (int)fabs(offset / d->dash_len);
1188 d->dash_offset = offset - mult * d->dash_len;
1189 if (node->path[0].x < node->path[1].x || node->path[0].y < node->path[1].y)
1190 d->dash_offset = d->dash_len - d->dash_offset;
1193 static int svg_dash_equal(svg_dash *a, svg_dash *b)
1195 if (a->dash_enable != b->dash_enable)
1197 if (a->adjusted_on != b->adjusted_on)
1199 if (a->adjusted_off != b->adjusted_off)
1201 /* rest of properties will be the same when on+off are */
1205 static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
1208 svg_get_dash(node, &dash_info);
1209 fputs(" stroke-width=\"", fp);
1210 svg_write_number(fp, node->size);
1212 svg_write_color(fp, node->color, "stroke");
1213 fputs(" fill=\"none\"", fp);
1214 if (dash_info.dash_enable) {
1215 if (dash_info.dash_offset != 0) {
1216 fputs(" stroke-dashoffset=\"", fp);
1217 svg_write_number(fp, dash_info.dash_offset);
1220 fputs(" stroke-dasharray=\"", fp);
1221 svg_write_number(fp, dash_info.adjusted_on);
1223 svg_write_number(fp, dash_info.adjusted_off);
1228 static int svg_is_int_step(double a, double b)
1230 double diff = fabs(a - b);
1231 return floor(diff) == diff;
1234 static int svg_path_straight_segment(FILE *fp,
1235 double lastA, double currentA, double currentB,
1237 int segment_idx, int isx, char absChar, char relChar)
1239 if (!svg_is_int_step(lastA, currentA)) {
1241 svg_write_number(fp, currentA);
1244 if (segment_idx < node->points - 1) {
1245 ArtVpath *vec = node->path + segment_idx + 1;
1246 if (vec->code == ART_LINETO) {
1247 double nextA = (isx ? vec->x : vec->y) - LINEOFFSET;
1248 double nextB = (isx ? vec->y : vec->x) - LINEOFFSET;
1249 if (nextB == currentB
1250 && ((currentA >= lastA) == (nextA >= currentA))
1251 && svg_is_int_step(currentA, nextA)) {
1252 return 1; /* skip to next as it is a straight line */
1257 svg_write_number(fp, currentA - lastA);
1261 static void svg_path(FILE *fp, gfx_node_t *node, int multi)
1264 double lastX = 0, lastY = 0;
1265 /* for straight lines <path..> tags take less space than
1266 <line..> tags because of the efficient packing
1267 in the 'd' attribute */
1268 svg_start_tag(fp, "path");
1270 svg_common_path_attributes(fp, node);
1272 /* specification of the 'd' attribute: */
1273 /* http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation */
1274 for (i = 0; i < node->points; i++) {
1275 ArtVpath *vec = node->path + i;
1276 double x = vec->x - LINEOFFSET;
1277 double y = vec->y - LINEOFFSET;
1278 switch (vec->code) {
1279 case ART_MOVETO_OPEN: /* fall-through */
1282 svg_write_number(fp, x);
1284 svg_write_number(fp, y);
1287 /* try optimize filesize by using minimal lineto commands */
1288 /* without introducing rounding errors. */
1290 if (svg_path_straight_segment(fp, lastY, y, x, node, i, 0, 'V', 'v'))
1292 } else if (y == lastY) {
1293 if (svg_path_straight_segment(fp, lastX, x, y, node, i, 1, 'H', 'h'))
1297 svg_write_number(fp, x);
1299 svg_write_number(fp, y);
1302 case ART_CURVETO: break; /* unsupported */
1303 case ART_END: break; /* nop */
1308 if (node->closed_path)
1311 svg_close_tag_empty_node(fp);
1314 static void svg_multi_path(FILE *fp, gfx_node_t **nodeR)
1316 /* optimize for multiple paths with the same color, penwidth, etc. */
1318 gfx_node_t *node = *nodeR;
1319 gfx_node_t *next = node->next;
1321 if (next->type != node->type
1322 || next->size != node->size
1323 || next->color != node->color
1324 || next->dash_on != node->dash_on
1325 || next->dash_off != node->dash_off)
1331 svg_path(fp, node, 0);
1334 svg_start_tag(fp, "g");
1335 svg_common_path_attributes(fp, node);
1337 while (num && node) {
1338 svg_path(fp, node, 1);
1344 svg_end_tag(fp, "g");
1347 static void svg_area(FILE *fp, gfx_node_t *node)
1350 double startX = 0, startY = 0;
1351 svg_start_tag(fp, "polygon");
1353 svg_write_color(fp, node->color, "fill");
1354 fputs(" points=\"", fp);
1355 for (i = 0; i < node->points; i++) {
1356 ArtVpath *vec = node->path + i;
1357 double x = vec->x - LINEOFFSET;
1358 double y = vec->y - LINEOFFSET;
1359 switch (vec->code) {
1360 case ART_MOVETO_OPEN: /* fall-through */
1362 svg_write_number(fp, x);
1364 svg_write_number(fp, y);
1369 if (i == node->points - 2
1370 && node->path[i + 1].code == ART_END
1371 && fabs(x - startX) < 0.001 && fabs(y - startY) < 0.001) {
1372 break; /* poly area always closed, no need for last point */
1375 svg_write_number(fp, x);
1377 svg_write_number(fp, y);
1379 case ART_CURVETO: break; /* unsupported */
1380 case ART_END: break; /* nop */
1384 svg_close_tag_empty_node(fp);
1387 static void svg_text(FILE *fp, gfx_node_t *node)
1390 const char *fontname;
1391 /* as svg has 0,0 in top-left corner (like most screens) instead of
1392 bottom-left corner like pdf and eps, we have to fake the coords
1393 using offset and inverse sin(r) value */
1394 int page_height = 1000;
1395 pdf_calc(page_height, node, &g);
1396 if (node->angle != 0) {
1397 svg_start_tag(fp, "g");
1398 /* can't use svg_write_number as 2 decimals is far from enough to avoid
1400 fprintf(fp, " transform=\"matrix(%f,%f,%f,%f,%f,%f)\"",
1401 g.ma, -g.mb, -g.mc, g.md, g.tmx, page_height - g.tmy);
1404 svg_start_tag(fp, "text");
1407 svg_write_number(fp, g.tmx);
1408 fputs("\" y=\"", fp);
1409 svg_write_number(fp, page_height - g.tmy);
1412 fontname = afm_get_font_name(node->filename);
1413 if (strcmp(fontname, svg_default_font))
1414 fprintf(fp, " font-family=\"%s\"", fontname);
1415 fputs(" font-size=\"", fp);
1416 svg_write_number(fp, node->size);
1418 if (!svg_color_is_black(node->color))
1419 svg_write_color(fp, node->color, "fill");
1420 svg_close_tag_single_line(fp);
1421 /* support for node->tabwidth missing */
1422 svg_write_text(fp, node->text);
1423 svg_end_tag(fp, "text");
1424 if (node->angle != 0)
1425 svg_end_tag(fp, "g");
1428 int gfx_render_svg (gfx_canvas_t *canvas,
1429 art_u32 width, art_u32 height,
1430 gfx_color_t background, FILE *fp){
1431 gfx_node_t *node = canvas->firstnode;
1432 /* Find the first font used, and assume it is the mostly used
1433 one. It reduces the number of font-familty attributes. */
1435 if (node->type == GFX_TEXT && node->filename) {
1436 svg_default_font = afm_get_font_name(node->filename);
1442 "<?xml version=\"1.0\" standalone=\"no\"?>\n"
1443 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
1444 " \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
1446 " SVG file created by\n"
1447 " RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
1449 " The width/height attributes in the outhermost svg node\n"
1450 " are just default sizes for the browser which is used\n"
1451 " if the svg file is openened directly without being\n"
1452 " embedded in an html file.\n"
1453 " The viewBox is the local coord system for rrdtool.\n"
1455 svg_start_tag(fp, "svg");
1456 fputs(" width=\"", fp);
1457 svg_write_number(fp, width * canvas->zoom);
1458 fputs("\" height=\"", fp);
1459 svg_write_number(fp, height * canvas->zoom);
1460 fputs("\" x=\"0\" y=\"0\" viewBox=\"", fp);
1461 svg_write_number(fp, -LINEOFFSET);
1463 svg_write_number(fp, -LINEOFFSET);
1465 svg_write_number(fp, width - LINEOFFSET);
1467 svg_write_number(fp, height - LINEOFFSET);
1468 fputs("\" preserveAspectRatio=\"xMidYMid\"", fp);
1469 fprintf(fp, " font-family=\"%s\"", svg_default_font); /* default font */
1470 fputs(" stroke-linecap=\"round\" stroke-linejoin=\"round\"", fp);
1471 fputs(" xmlns=\"http://www.w3.org/2000/svg\"", fp);
1472 fputs(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"", fp);
1474 svg_start_tag(fp, "rect");
1475 fprintf(fp, " x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"", width, height);
1476 svg_write_color(fp, background, "fill");
1477 svg_close_tag_empty_node(fp);
1478 node = canvas->firstnode;
1480 switch (node->type) {
1482 svg_multi_path(fp, &node);
1492 svg_end_tag(fp, "svg");
1496 /* ------- EPS -------
1497 EPS and Postscript references:
1498 http://partners.adobe.com/asn/developer/technotes/postscript.html
1501 typedef struct eps_font
1503 const char *ps_font;
1505 struct eps_font *next;
1508 typedef struct eps_state
1511 gfx_canvas_t *canvas;
1512 art_u32 page_width, page_height;
1513 eps_font *font_list;
1519 int linecap, linejoin;
1523 static void eps_set_color(eps_state *state, gfx_color_t color)
1525 #if USE_EPS_FAKE_ALPHA
1528 /* gfx_color_t is RRGGBBAA */
1529 if (state->color == color)
1531 #if USE_EPS_FAKE_ALPHA
1532 a1 = (color & 255) / 255.0;
1533 a2 = 255 * (1 - a1);
1534 #define eps_color_calc(x) (int)( ((x) & 255) * a1 + a2)
1536 #define eps_color_calc(x) (int)( (x) & 255)
1538 /* gfx_color_t is RRGGBBAA */
1539 if (state->color == color)
1541 fprintf(state->fp, "%d %d %d Rgb\n",
1542 eps_color_calc(color >> 24),
1543 eps_color_calc(color >> 16),
1544 eps_color_calc(color >> 8));
1545 state->color = color;
1548 static int eps_add_font(eps_state *state, gfx_node_t *node)
1550 /* The fonts list could be postponed to the end using
1551 (atend), but let's be nice and have them in the header. */
1552 const char *ps_font = afm_get_font_postscript_name(node->filename);
1554 for (ef = state->font_list; ef; ef = ef->next) {
1555 if (!strcmp(ps_font, ef->ps_font))
1558 ef = malloc(sizeof(eps_font));
1560 rrd_set_error("malloc for eps_font");
1563 ef->next = state->font_list;
1564 ef->ps_font = ps_font;
1565 state->font_list = ef;
1569 static void eps_list_fonts(eps_state *state, const char *dscName)
1572 int lineLen = strlen(dscName);
1573 if (!state->font_list)
1575 fputs(dscName, state->fp);
1576 for (ef = state->font_list; ef; ef = ef->next) {
1577 int nameLen = strlen(ef->ps_font);
1578 if (lineLen + nameLen > 100 && lineLen) {
1579 fputs("\n", state->fp);
1580 fputs("%%- \n", state->fp);
1583 fputs(" ", state->fp);
1586 fputs(ef->ps_font, state->fp);
1589 fputs("\n", state->fp);
1592 static void eps_define_fonts(eps_state *state)
1595 if (!state->font_list)
1597 for (ef = state->font_list; ef; ef = ef->next) {
1598 /* PostScript¨ LANGUAGE REFERENCE third edition
1602 "/%s findfont dup length dict begin\n"
1603 "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
1604 "/Encoding ISOLatin1Encoding def\n"
1606 "/%s-ISOLatin1 exch definefont pop\n"
1607 "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
1608 ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
1612 static int eps_prologue(eps_state *state)
1616 "%!PS-Adobe-3.0 EPSF-3.0\n"
1617 "%%Creator: RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
1618 /* can't like weird chars here */
1619 "%%Title: (RRDtool output)\n"
1620 "%%DocumentData: Clean7Bit\n"
1622 fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
1623 state->page_width, state->page_height);
1624 for (node = state->canvas->firstnode; node; node = node->next) {
1625 if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
1628 eps_list_fonts(state, "%%DocumentFonts:");
1629 eps_list_fonts(state, "%%DocumentNeededFonts:");
1633 "%%EndProlog\n" /* must have, or BoundingBox is ignored */
1634 "/bd { bind def } bind def\n"
1636 fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
1638 "/X2 {X exch X exch} bd\n"
1639 "/M {X2 moveto} bd\n"
1640 "/L {X2 lineto} bd\n"
1644 "/CP {closepath} bd\n"
1645 "/WS {setlinewidth stroke} bd\n"
1648 "/T2 {concat 0 0 moveto show grestore} bd\n"
1649 "/T {moveto show} bd\n"
1650 "/Rgb { 255.0 div 3 1 roll\n"
1651 " 255.0 div 3 1 roll \n"
1652 " 255.0 div 3 1 roll setrgbcolor } bd\n"
1654 eps_define_fonts(state);
1658 static void eps_clear_dash(eps_state *state)
1660 if (!state->has_dash)
1662 state->has_dash = 0;
1663 fputs("[1 0] 0 setdash\n", state->fp);
1666 static void eps_write_linearea(eps_state *state, gfx_node_t *node)
1669 FILE *fp = state->fp;
1671 int clearDashIfAny = 1;
1672 eps_set_color(state, node->color);
1673 if (node->type == GFX_LINE) {
1675 if (state->linecap != 1) {
1676 fputs("1 setlinecap\n", fp);
1679 if (state->linejoin != 1) {
1680 fputs("1 setlinejoin\n", fp);
1681 state->linejoin = 1;
1683 svg_get_dash(node, &dash_info);
1684 if (dash_info.dash_enable) {
1686 state->has_dash = 1;
1688 svg_write_number(fp, dash_info.adjusted_on);
1690 svg_write_number(fp, dash_info.adjusted_off);
1692 svg_write_number(fp, dash_info.dash_offset);
1693 fputs(" setdash\n", fp);
1697 eps_clear_dash(state);
1698 for (i = 0; i < node->points; i++) {
1699 ArtVpath *vec = node->path + i;
1701 double y = state->page_height - vec->y;
1702 if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
1703 useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
1708 switch (vec->code) {
1709 case ART_MOVETO_OPEN: /* fall-through */
1711 svg_write_number(fp, x);
1713 svg_write_number(fp, y);
1715 fputs(useOffset ? "M\n" : "m\n", fp);
1718 svg_write_number(fp, x);
1720 svg_write_number(fp, y);
1722 fputs(useOffset ? "L\n" : "l\n", fp);
1724 case ART_CURVETO: break; /* unsupported */
1725 case ART_END: break; /* nop */
1728 if (node->type == GFX_LINE) {
1729 if (node->closed_path)
1731 if (node->size != state->line_width) {
1732 state->line_width = node->size;
1733 svg_write_number(fp, state->line_width);
1743 static void eps_write_text(eps_state *state, gfx_node_t *node)
1745 FILE *fp = state->fp;
1746 const char *ps_font = afm_get_font_postscript_name(node->filename);
1749 #ifdef HAVE_MBSTOWCS
1751 wchar_t *p, *cstr, ch;
1755 clen = strlen(node->text) + 1;
1756 cstr = malloc(sizeof(wchar_t) * clen);
1757 text_count = mbstowcs(cstr, node->text, clen);
1758 if (text_count == -1)
1759 text_count = mbstowcs(cstr, "Enc-Err", 6);
1762 const unsigned char *p = node->text;
1767 pdf_calc(state->page_height, node, &g);
1768 eps_set_color(state, node->color);
1769 if (strcmp(ps_font, state->font) || node->size != state->font_size) {
1770 state->font = ps_font;
1771 state->font_size = node->size;
1772 svg_write_number(fp, state->font_size);
1773 fprintf(fp, " SetFont-%s\n", state->font);
1783 ch = afm_fix_osx_charset(ch); /* unsafe macro */
1784 if (++lineLen > 70) {
1785 fputs("\\\n", fp); /* backslash and \n */
1808 } else if (ch >= 126 || ch < 32) {
1809 fprintf(fp, "\\%03o", (unsigned int)ch);
1817 #ifdef HAVE_MBSTOWCS
1821 /* can't use svg_write_number as 2 decimals is far from enough to avoid
1823 fprintf(fp, ") [%f %f %f %f %f %f] T2\n",
1824 g.ma, g.mb, g.mc, g.md, g.tmx, g.tmy);
1827 svg_write_number(fp, g.tmx);
1829 svg_write_number(fp, g.tmy);
1834 static int eps_write_content(eps_state *state)
1837 fputs("%\n", state->fp);
1838 for (node = state->canvas->firstnode; node; node = node->next) {
1839 switch (node->type) {
1842 eps_write_linearea(state, node);
1845 eps_write_text(state, node);
1852 int gfx_render_eps (gfx_canvas_t *canvas,
1853 art_u32 width, art_u32 height,
1854 gfx_color_t background, FILE *fp){
1855 struct eps_state state;
1857 state.canvas = canvas;
1858 state.page_width = width;
1859 state.page_height = height;
1860 state.font = "no-default-font";
1861 state.font_size = -1;
1862 state.color = 0; /* black */
1863 state.font_list = NULL;
1865 state.linejoin = -1;
1867 state.line_width = 1;
1868 if (eps_prologue(&state) == -1)
1870 eps_set_color(&state, background);
1871 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
1872 height, width, height, width);
1873 if (eps_write_content(&state) == -1)
1875 fputs("showpage\n", fp);
1876 fputs("%%EOF\n", fp);
1877 while (state.font_list) {
1878 eps_font *next = state.font_list->next;
1879 free(state.font_list);
1880 state.font_list = next;
1885 /* ------- PDF -------
1886 PDF references page:
1887 http://partners.adobe.com/public/developer/pdf/index_reference.html
1890 typedef struct pdf_buffer
1892 int id, is_obj, is_dict, is_stream, pdf_file_pos;
1894 int alloc_size, current_size;
1895 struct pdf_buffer *previous_buffer, *next_buffer;
1896 struct pdf_state *state;
1899 typedef struct pdf_font
1901 const char *ps_font;
1903 struct pdf_font *next;
1906 typedef struct pdf_state
1909 gfx_canvas_t *canvas;
1910 art_u32 page_width, page_height;
1911 pdf_font *font_list;
1912 pdf_buffer *first_buffer, *last_buffer;
1916 gfx_color_t stroke_color, fill_color;
1921 int linecap, linejoin;
1924 pdf_buffer pdf_header;
1925 pdf_buffer info_obj, catalog_obj, pages_obj, page1_obj;
1926 pdf_buffer fontsdict_obj;
1927 pdf_buffer graph_stream;
1930 static void pdf_init_buffer(pdf_state *state, pdf_buffer *buf)
1932 int initial_size = 32;
1935 buf->alloc_size = 0;
1936 buf->current_size = 0;
1937 buf->data = (char*)malloc(initial_size);
1939 buf->previous_buffer = NULL;
1940 buf->next_buffer = NULL;
1941 if (buf->data == NULL) {
1942 rrd_set_error("malloc for pdf_buffer data");
1943 state->has_failed = 1;
1946 buf->alloc_size = initial_size;
1947 if (state->last_buffer)
1948 state->last_buffer->next_buffer = buf;
1949 if (state->first_buffer == NULL)
1950 state->first_buffer = buf;
1951 buf->previous_buffer = state->last_buffer;
1952 state->last_buffer = buf;
1955 static void pdf_put(pdf_buffer *buf, const char *text, int len)
1959 if (buf->alloc_size < buf->current_size + len) {
1960 int new_size = buf->alloc_size;
1962 while (new_size < buf->current_size + len)
1964 new_buf = (char*)malloc(new_size);
1965 if (new_buf == NULL) {
1966 rrd_set_error("re-malloc for pdf_buffer data");
1967 buf->state->has_failed = 1;
1970 memcpy(new_buf, buf->data, buf->current_size);
1972 buf->data = new_buf;
1973 buf->alloc_size = new_size;
1975 memcpy(buf->data + buf->current_size, text, len);
1976 buf->current_size += len;
1979 static void pdf_put_char(pdf_buffer *buf, char c)
1981 if (buf->alloc_size >= buf->current_size + 1) {
1982 buf->data[buf->current_size++] = c;
1986 pdf_put(buf, tmp, 1);
1990 static void pdf_puts(pdf_buffer *buf, const char *text)
1992 pdf_put(buf, text, strlen(text));
1995 static void pdf_indent(pdf_buffer *buf)
1997 pdf_puts(buf, "\t");
2000 static void pdf_putsi(pdf_buffer *buf, const char *text)
2003 pdf_puts(buf, text);
2006 static void pdf_putint(pdf_buffer *buf, int i)
2009 sprintf(tmp, "%d", i);
2013 static void pdf_putnumber(pdf_buffer *buf, double d)
2016 svg_format_number(tmp, sizeof(tmp), d);
2020 static void pdf_put_string_contents_wide(pdf_buffer *buf, const afm_char *text)
2022 const afm_char *p = text;
2025 ch = afm_fix_osx_charset(ch); /* unsafe macro */
2030 pdf_puts(buf, "\\(");
2033 pdf_puts(buf, "\\)");
2036 pdf_puts(buf, "\\\\");
2039 pdf_puts(buf, "\\n");
2042 pdf_puts(buf, "\\r");
2045 pdf_puts(buf, "\\t");
2049 pdf_put_char(buf, '?');
2050 } else if (ch > 125 || ch < 32) {
2051 pdf_put_char(buf, ch);
2054 snprintf(tmp, sizeof(tmp), "\\%03o", (int)ch);
2062 static void pdf_put_string_contents(pdf_buffer *buf, const char *text)
2064 #ifdef HAVE_MBSTOWCS
2065 size_t clen = strlen(text) + 1;
2066 wchar_t *cstr = malloc(sizeof(wchar_t) * clen);
2067 int text_count = mbstowcs(cstr, text, clen);
2068 if (text_count == -1)
2069 text_count = mbstowcs(cstr, "Enc-Err", 6);
2070 pdf_put_string_contents_wide(buf, cstr);
2073 fprintf(stderr, "Decoding utf8 for '%s'\n", text);
2076 fprintf(stderr, "sz wc = %d\n", sizeof(wchar_t));
2078 fprintf(stderr, " %d = %c versus %d = %c\n", *p, (char)*p, 255 & (int)*pp, *pp);
2086 pdf_put_string_contents_wide(buf, text);
2090 static void pdf_init_object(pdf_state *state, pdf_buffer *buf)
2092 pdf_init_buffer(state, buf);
2093 buf->id = ++state->last_obj_id;
2098 static void pdf_init_dict(pdf_state *state, pdf_buffer *buf)
2100 pdf_init_object(state, buf);
2104 static void pdf_set_color(pdf_buffer *buf, gfx_color_t color,
2105 gfx_color_t *current_color, const char *op)
2107 #if USE_PDF_FAKE_ALPHA
2110 /* gfx_color_t is RRGGBBAA */
2111 if (*current_color == color)
2113 #if USE_PDF_FAKE_ALPHA
2114 a1 = (color & 255) / 255.0;
2116 #define pdf_color_calc(x) ( ((x) & 255) / 255.0 * a1 + a2)
2118 #define pdf_color_calc(x) ( ((x) & 255) / 255.0)
2120 pdf_putnumber(buf, pdf_color_calc(color >> 24));
2122 pdf_putnumber(buf, pdf_color_calc(color >> 16));
2124 pdf_putnumber(buf, pdf_color_calc(color >> 8));
2127 pdf_puts(buf, "\n");
2128 *current_color = color;
2131 static void pdf_set_stroke_color(pdf_buffer *buf, gfx_color_t color)
2133 pdf_set_color(buf, color, &buf->state->stroke_color, "RG");
2136 static void pdf_set_fill_color(pdf_buffer *buf, gfx_color_t color)
2138 pdf_set_color(buf, color, &buf->state->fill_color, "rg");
2141 static pdf_font *pdf_find_font(pdf_state *state, gfx_node_t *node)
2143 const char *ps_font = afm_get_font_postscript_name(node->filename);
2145 for (ef = state->font_list; ef; ef = ef->next) {
2146 if (!strcmp(ps_font, ef->ps_font))
2152 static void pdf_add_font(pdf_state *state, gfx_node_t *node)
2154 pdf_font *ef = pdf_find_font(state, node);
2157 ef = malloc(sizeof(pdf_font));
2159 rrd_set_error("malloc for pdf_font");
2160 state->has_failed = 1;
2163 pdf_init_dict(state, &ef->obj);
2164 ef->next = state->font_list;
2165 ef->ps_font = afm_get_font_postscript_name(node->filename);
2166 state->font_list = ef;
2168 pdf_putsi(&state->fontsdict_obj, "/F");
2169 pdf_putint(&state->fontsdict_obj, ef->obj.id);
2170 pdf_puts(&state->fontsdict_obj, " ");
2171 pdf_putint(&state->fontsdict_obj, ef->obj.id);
2172 pdf_puts(&state->fontsdict_obj, " 0 R\n");
2174 pdf_putsi(&ef->obj, "/Type /Font\n");
2175 pdf_putsi(&ef->obj, "/Subtype /Type1\n");
2176 pdf_putsi(&ef->obj, "/Name /F");
2177 pdf_putint(&ef->obj, ef->obj.id);
2178 pdf_puts(&ef->obj, "\n");
2179 pdf_putsi(&ef->obj, "/BaseFont /");
2180 pdf_puts(&ef->obj, ef->ps_font);
2181 pdf_puts(&ef->obj, "\n");
2182 pdf_putsi(&ef->obj, "/Encoding /WinAnsiEncoding\n");
2183 /* 'Cp1252' (this is latin 1 extended with 27 characters;
2184 the encoding is also known as 'winansi')
2185 http://www.lowagie.com/iText/tutorial/ch09.html */
2188 static void pdf_create_fonts(pdf_state *state)
2191 for (node = state->canvas->firstnode; node; node = node->next) {
2192 if (node->type == GFX_TEXT)
2193 pdf_add_font(state, node);
2197 static void pdf_write_linearea(pdf_state *state, gfx_node_t *node)
2200 pdf_buffer *s = &state->graph_stream;
2201 if (node->type == GFX_LINE) {
2203 svg_get_dash(node, &dash_info);
2204 if (!svg_dash_equal(&dash_info, &state->dash)) {
2205 state->dash = dash_info;
2206 if (dash_info.dash_enable) {
2208 pdf_putnumber(s, dash_info.adjusted_on);
2210 pdf_putnumber(s, dash_info.adjusted_off);
2212 pdf_putnumber(s, dash_info.dash_offset);
2213 pdf_puts(s, " d\n");
2215 pdf_puts(s, "[] 0 d\n");
2218 pdf_set_stroke_color(s, node->color);
2219 if (state->linecap != 1) {
2220 pdf_puts(s, "1 j\n");
2223 if (state->linejoin != 1) {
2224 pdf_puts(s, "1 J\n");
2225 state->linejoin = 1;
2227 if (node->size != state->line_width) {
2228 state->line_width = node->size;
2229 pdf_putnumber(s, state->line_width);
2230 pdf_puts(s, " w\n");
2233 pdf_set_fill_color(s, node->color);
2235 for (i = 0; i < node->points; i++) {
2236 ArtVpath *vec = node->path + i;
2238 double y = state->page_height - vec->y;
2239 if (node->type == GFX_AREA) {
2240 x += LINEOFFSET; /* adjust for libart handling of areas */
2243 switch (vec->code) {
2244 case ART_MOVETO_OPEN: /* fall-through */
2246 pdf_putnumber(s, x);
2248 pdf_putnumber(s, y);
2249 pdf_puts(s, " m\n");
2252 pdf_putnumber(s, x);
2254 pdf_putnumber(s, y);
2255 pdf_puts(s, " l\n");
2257 case ART_CURVETO: break; /* unsupported */
2258 case ART_END: break; /* nop */
2261 if (node->type == GFX_LINE) {
2262 pdf_puts(s, node->closed_path ? "s\n" : "S\n");
2269 static void pdf_write_matrix(pdf_state *state, gfx_node_t *node, pdf_coords *g, int useTM)
2272 pdf_buffer *s = &state->graph_stream;
2273 if (node->angle == 0) {
2274 pdf_puts(s, "1 0 0 1 ");
2275 pdf_putnumber(s, useTM ? g->tmx : g->mx);
2277 pdf_putnumber(s, useTM ? g->tmy : g->my);
2279 /* can't use svg_write_number as 2 decimals is far from enough to avoid
2281 sprintf(tmp, "%f %f %f %f %f %f",
2282 g->ma, g->mb, g->mc, g->md,
2283 useTM ? g->tmx : g->mx,
2284 useTM ? g->tmy : g->my);
2289 static void pdf_write_text(pdf_state *state, gfx_node_t *node,
2290 int last_was_text, int next_is_text)
2293 pdf_buffer *s = &state->graph_stream;
2294 pdf_font *font = pdf_find_font(state, node);
2296 rrd_set_error("font disappeared");
2297 state->has_failed = 1;
2300 pdf_calc(state->page_height, node, &g);
2302 pdf_puts(s, "q % debug green box\n");
2303 pdf_write_matrix(state, node, &g, 0);
2304 pdf_puts(s, " cm\n");
2305 pdf_set_fill_color(s, 0x90FF9000);
2306 pdf_puts(s, "0 0.4 0 rg\n");
2307 pdf_puts(s, "0 0 ");
2308 pdf_putnumber(s, g.sizep.x);
2310 pdf_putnumber(s, g.sizep.y);
2311 pdf_puts(s, " re\n");
2315 pdf_set_fill_color(s, node->color);
2316 if (PDF_CALC_DEBUG || !last_was_text)
2317 pdf_puts(s, "BT\n");
2318 if (state->font_id != font->obj.id || node->size != state->font_size) {
2319 state->font_id = font->obj.id;
2320 state->font_size = node->size;
2322 pdf_putint(s, font->obj.id);
2324 pdf_putnumber(s, node->size);
2325 pdf_puts(s, " Tf\n");
2327 pdf_write_matrix(state, node, &g, 1);
2328 pdf_puts(s, " Tm\n");
2330 pdf_put_string_contents(s, node->text);
2331 pdf_puts(s, ") Tj\n");
2332 if (PDF_CALC_DEBUG || !next_is_text)
2333 pdf_puts(s, "ET\n");
2336 static void pdf_write_content(pdf_state *state)
2339 int last_was_text = 0, next_is_text;
2340 for (node = state->canvas->firstnode; node; node = node->next) {
2341 switch (node->type) {
2344 pdf_write_linearea(state, node);
2347 next_is_text = node->next && node->next->type == GFX_TEXT;
2348 pdf_write_text(state, node, last_was_text, next_is_text);
2351 last_was_text = node->type == GFX_TEXT;
2355 static void pdf_init_document(pdf_state *state)
2357 pdf_init_buffer(state, &state->pdf_header);
2358 pdf_init_dict(state, &state->catalog_obj);
2359 pdf_init_dict(state, &state->info_obj);
2360 pdf_init_dict(state, &state->pages_obj);
2361 pdf_init_dict(state, &state->page1_obj);
2362 pdf_init_dict(state, &state->fontsdict_obj);
2363 pdf_create_fonts(state);
2364 if (state->has_failed)
2366 /* make stream last object in file */
2367 pdf_init_object(state, &state->graph_stream);
2368 state->graph_stream.is_stream = 1;
2371 static void pdf_setup_document(pdf_state *state)
2373 const char *creator = "RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch";
2374 /* all objects created by now, so init code can reference them */
2376 pdf_puts(&state->pdf_header, "%PDF-1.3\n");
2377 /* following 8 bit comment is recommended by Adobe for
2378 indicating binary file to file transfer applications */
2379 pdf_puts(&state->pdf_header, "%\xE2\xE3\xCF\xD3\n");
2381 pdf_putsi(&state->info_obj, "/Creator (");
2382 pdf_put_string_contents(&state->info_obj, creator);
2383 pdf_puts(&state->info_obj, ")\n");
2385 pdf_putsi(&state->catalog_obj, "/Type /Catalog\n");
2386 pdf_putsi(&state->catalog_obj, "/Pages ");
2387 pdf_putint(&state->catalog_obj, state->pages_obj.id);
2388 pdf_puts(&state->catalog_obj, " 0 R\n");
2390 pdf_putsi(&state->pages_obj, "/Type /Pages\n");
2391 pdf_putsi(&state->pages_obj, "/Kids [");
2392 pdf_putint(&state->pages_obj, state->page1_obj.id);
2393 pdf_puts(&state->pages_obj, " 0 R]\n");
2394 pdf_putsi(&state->pages_obj, "/Count 1\n");
2396 pdf_putsi(&state->page1_obj, "/Type /Page\n");
2397 pdf_putsi(&state->page1_obj, "/Parent ");
2398 pdf_putint(&state->page1_obj, state->pages_obj.id);
2399 pdf_puts(&state->page1_obj, " 0 R\n");
2400 pdf_putsi(&state->page1_obj, "/MediaBox [0 0 ");
2401 pdf_putint(&state->page1_obj, state->page_width);
2402 pdf_puts(&state->page1_obj, " ");
2403 pdf_putint(&state->page1_obj, state->page_height);
2404 pdf_puts(&state->page1_obj, "]\n");
2405 pdf_putsi(&state->page1_obj, "/Contents ");
2406 pdf_putint(&state->page1_obj, state->graph_stream.id);
2407 pdf_puts(&state->page1_obj, " 0 R\n");
2408 pdf_putsi(&state->page1_obj, "/Resources << /Font ");
2409 pdf_putint(&state->page1_obj, state->fontsdict_obj.id);
2410 pdf_puts(&state->page1_obj, " 0 R >>\n");
2413 static void pdf_write_string_to_file(pdf_state *state, const char *text)
2415 fputs(text, state->fp);
2416 state->pdf_file_pos += strlen(text);
2419 static void pdf_write_buf_to_file(pdf_state *state, pdf_buffer *buf)
2422 buf->pdf_file_pos = state->pdf_file_pos;
2424 snprintf(tmp, sizeof(tmp), "%d 0 obj\n", buf->id);
2425 pdf_write_string_to_file(state, tmp);
2428 pdf_write_string_to_file(state, "<<\n");
2429 if (buf->is_stream) {
2430 snprintf(tmp, sizeof(tmp), "<< /Length %d >>\n", buf->current_size);
2431 pdf_write_string_to_file(state, tmp);
2432 pdf_write_string_to_file(state, "stream\n");
2434 fwrite(buf->data, 1, buf->current_size, state->fp);
2435 state->pdf_file_pos += buf->current_size;
2437 pdf_write_string_to_file(state, "endstream\n");
2439 pdf_write_string_to_file(state, ">>\n");
2441 pdf_write_string_to_file(state, "endobj\n");
2444 static void pdf_write_to_file(pdf_state *state)
2446 pdf_buffer *buf = state->first_buffer;
2448 state->pdf_file_pos = 0;
2449 pdf_write_buf_to_file(state, &state->pdf_header);
2452 pdf_write_buf_to_file(state, buf);
2453 buf = buf->next_buffer;
2455 xref_pos = state->pdf_file_pos;
2456 fprintf(state->fp, "xref\n");
2457 fprintf(state->fp, "%d %d\n", 0, state->last_obj_id + 1);
2458 /* TOC lines must be exactly 20 bytes including \n */
2459 fprintf(state->fp, "%010d %05d f\x20\n", 0, 65535);
2460 for (buf = state->first_buffer; buf; buf = buf->next_buffer) {
2462 fprintf(state->fp, "%010d %05d n\x20\n", buf->pdf_file_pos, 0);
2464 fprintf(state->fp, "trailer\n");
2465 fprintf(state->fp, "<<\n");
2466 fprintf(state->fp, "\t/Size %d\n", state->last_obj_id + 1);
2467 fprintf(state->fp, "\t/Root %d 0 R\n", state->catalog_obj.id);
2468 fprintf(state->fp, "\t/Info %d 0 R\n", state->info_obj.id);
2469 fprintf(state->fp, ">>\n");
2470 fprintf(state->fp, "startxref\n");
2471 fprintf(state->fp, "%d\n", xref_pos);
2472 fputs("%%EOF\n", state->fp);
2475 static void pdf_free_resources(pdf_state *state)
2477 pdf_buffer *buf = state->first_buffer;
2481 buf->alloc_size = buf->current_size = 0;
2482 buf = buf->next_buffer;
2484 while (state->font_list) {
2485 pdf_font *next = state->font_list->next;
2486 free(state->font_list);
2487 state->font_list = next;
2491 int gfx_render_pdf (gfx_canvas_t *canvas,
2492 art_u32 width, art_u32 height,
2493 gfx_color_t UNUSED(background), FILE *fp){
2494 struct pdf_state state;
2495 memset(&state, 0, sizeof(pdf_state));
2497 state.canvas = canvas;
2498 state.page_width = width;
2499 state.page_height = height;
2501 state.font_size = -1;
2502 state.font_list = NULL;
2504 state.linejoin = -1;
2505 pdf_init_document(&state);
2507 pdf_set_color(&state, background);
2508 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
2509 height, width, height, width);
2511 if (!state.has_failed)
2512 pdf_write_content(&state);
2513 if (!state.has_failed)
2514 pdf_setup_document(&state);
2515 if (!state.has_failed)
2516 pdf_write_to_file(&state);
2517 pdf_free_resources(&state);
2518 return state.has_failed ? -1 : 0;