1 /****************************************************************************
2 * RRDtool 1.2.11 Copyright by Tobi Oetiker, 1997-2005
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
10 # define DPRINTF(...) fprintf(stderr, __VA_ARGS__);
17 #include FT_FREETYPE_H
24 /* lines are better drawn on the pixle than between pixles */
25 #define LINEOFFSET 0.5
27 #define USE_PDF_FAKE_ALPHA 1
28 #define USE_EPS_FAKE_ALPHA 1
30 typedef struct gfx_char_s *gfx_char;
32 FT_UInt index; /* glyph index */
33 FT_Vector pos; /* location from baseline in 26.6 */
34 FT_Glyph image; /* glyph bitmap */
37 typedef struct gfx_string_s *gfx_string;
41 int count; /* number of characters */
48 /* compute string bbox */
49 static void compute_string_bbox(gfx_string string);
51 /* create a freetype glyph string */
52 gfx_string gfx_string_create ( gfx_canvas_t *canvas, FT_Face face,
53 const char *text, int rotation, double tabwidth, double size);
55 /* create a freetype glyph string */
56 static void gfx_string_destroy ( gfx_string string );
59 gfx_node_t *gfx_new_node( gfx_canvas_t *canvas,enum gfx_en type){
60 gfx_node_t *node = art_new(gfx_node_t,1);
61 if (node == NULL) return NULL;
63 node->color = 0x0; /* color of element 0xRRGGBBAA alpha 0xff is solid*/
64 node->size =0.0; /* font size, line width */
65 node->path = NULL; /* path */
68 node->closed_path = 0;
69 node->filename = NULL; /* font or image filename */
72 node->y = 0.0; /* position */
74 node->halign = GFX_H_NULL; /* text alignement */
75 node->valign = GFX_V_NULL; /* text alignement */
78 if (canvas->lastnode != NULL){
79 canvas->lastnode->next = node;
81 if (canvas->firstnode == NULL){
82 canvas->firstnode = node;
84 canvas->lastnode = node;
88 gfx_canvas_t *gfx_new_canvas (void) {
89 gfx_canvas_t *canvas = art_new(gfx_canvas_t,1);
90 canvas->firstnode = NULL;
91 canvas->lastnode = NULL;
92 canvas->imgformat = IF_PNG; /* we default to PNG output */
93 canvas->interlaced = 0;
95 canvas->font_aa_threshold = -1.0;
96 canvas->aa_type = AA_NORMAL;
100 /* create a new line */
101 gfx_node_t *gfx_new_line(gfx_canvas_t *canvas,
102 double X0, double Y0,
103 double X1, double Y1,
104 double width, gfx_color_t color){
105 return gfx_new_dashed_line(canvas, X0, Y0, X1, Y1, width, color, 0, 0);
108 gfx_node_t *gfx_new_dashed_line(gfx_canvas_t *canvas,
109 double X0, double Y0,
110 double X1, double Y1,
111 double width, gfx_color_t color,
112 double dash_on, double dash_off){
116 node = gfx_new_node(canvas,GFX_LINE);
117 if (node == NULL) return NULL;
118 vec = art_new(ArtVpath, 3);
119 if (vec == NULL) return NULL;
120 vec[0].code = ART_MOVETO_OPEN; vec[0].x=X0+LINEOFFSET; vec[0].y=Y0+LINEOFFSET;
121 vec[1].code = ART_LINETO; vec[1].x=X1+LINEOFFSET; vec[1].y=Y1+LINEOFFSET;
122 vec[2].code = ART_END; vec[2].x=0;vec[2].y=0;
125 node->points_max = 3;
128 node->dash_on = dash_on;
129 node->dash_off = dash_off;
134 /* create a new area */
135 gfx_node_t *gfx_new_area (gfx_canvas_t *canvas,
136 double X0, double Y0,
137 double X1, double Y1,
138 double X2, double Y2,
143 node = gfx_new_node(canvas,GFX_AREA);
144 if (node == NULL) return NULL;
145 vec = art_new(ArtVpath, 5);
146 if (vec == NULL) return NULL;
147 vec[0].code = ART_MOVETO; vec[0].x=X0; vec[0].y=Y0;
148 vec[1].code = ART_LINETO; vec[1].x=X1; vec[1].y=Y1;
149 vec[2].code = ART_LINETO; vec[2].x=X2; vec[2].y=Y2;
150 vec[3].code = ART_LINETO; vec[3].x=X0; vec[3].y=Y0;
151 vec[4].code = ART_END; vec[4].x=0; vec[4].y=0;
154 node->points_max = 5;
161 /* add a point to a line or to an area */
162 int gfx_add_point (gfx_node_t *node,
164 if (node == NULL) return 1;
165 if (node->type == GFX_AREA) {
166 double X0 = node->path[0].x;
167 double Y0 = node->path[0].y;
169 art_vpath_add_point (&(node->path),
174 art_vpath_add_point (&(node->path),
179 art_vpath_add_point (&(node->path),
184 } else if (node->type == GFX_LINE) {
186 art_vpath_add_point (&(node->path),
190 x+LINEOFFSET,y+LINEOFFSET);
191 art_vpath_add_point (&(node->path),
198 /* can only add point to areas and lines */
204 void gfx_close_path (gfx_node_t *node) {
205 node->closed_path = 1;
206 if (node->path[0].code == ART_MOVETO_OPEN)
207 node->path[0].code = ART_MOVETO;
210 /* create a text node */
211 gfx_node_t *gfx_new_text (gfx_canvas_t *canvas,
212 double x, double y, gfx_color_t color,
213 char* font, double size,
214 double tabwidth, double angle,
215 enum gfx_h_align_en h_align,
216 enum gfx_v_align_en v_align,
218 gfx_node_t *node = gfx_new_node(canvas,GFX_TEXT);
220 node->text = strdup(text);
222 node->filename = strdup(font);
227 node->tabwidth = tabwidth;
228 node->halign = h_align;
229 node->valign = v_align;
231 /* debugging: show text anchor
232 green is along x-axis, red is downward y-axis */
234 double a = 2 * M_PI * -node->angle / 360.0;
235 double cos_a = cos(a);
236 double sin_a = sin(a);
240 x + len * cos_a, y - len * sin_a,
244 x + len * sin_a, y + len * cos_a,
251 int gfx_render(gfx_canvas_t *canvas,
252 art_u32 width, art_u32 height,
253 gfx_color_t background, FILE *fp){
254 switch (canvas->imgformat) {
256 return gfx_render_png (canvas, width, height, background, fp);
258 return gfx_render_svg (canvas, width, height, background, fp);
260 return gfx_render_eps (canvas, width, height, background, fp);
262 return gfx_render_pdf (canvas, width, height, background, fp);
268 static void gfx_string_destroy ( gfx_string string ) {
270 if (string->glyphs) {
271 for (n=0; n<string->num_glyphs; ++n)
272 FT_Done_Glyph (string->glyphs[n].image);
273 free (string->glyphs);
279 double gfx_get_text_width ( gfx_canvas_t *canvas,
280 double start, char* font, double size,
281 double tabwidth, char* text, int rotation){
282 switch (canvas->imgformat) {
284 return gfx_get_text_width_libart (canvas, start, font, size, tabwidth, text, rotation);
285 case IF_SVG: /* fall through */
288 return afm_get_text_width(start, font, size, tabwidth, text);
290 return size * strlen(text);
294 double gfx_get_text_width_libart (
295 gfx_canvas_t *canvas, double UNUSED(start), char* font, double size,
296 double tabwidth, char* text, int rotation ){
301 FT_Library library=NULL;
304 FT_Init_FreeType( &library );
305 error = FT_New_Face( library, font, 0, &face );
307 FT_Done_FreeType(library);
310 error = FT_Set_Char_Size(face, size*64,size*64, 100,100);
312 FT_Done_FreeType(library);
315 string = gfx_string_create( canvas, face, text, rotation, tabwidth, size );
316 text_width = string->width;
317 gfx_string_destroy(string);
318 FT_Done_FreeType(library);
319 return text_width/64;
322 static void gfx_libart_close_path(gfx_node_t *node, ArtVpath **vec)
324 /* libart must have end==start for closed paths,
325 even if using ART_MOVETO and not ART_MOVETO_OPEN
326 so add extra point which is the same as the starting point */
327 int points_max = node->points; /* scaled array has exact size */
328 int points = node->points - 1;
329 art_vpath_add_point (vec, &points, &points_max, ART_LINETO,
330 (**vec).x, (**vec).y);
331 art_vpath_add_point (vec, &points, &points_max, ART_END, 0, 0);
335 /* find bbox of a string */
336 static void compute_string_bbox(gfx_string string) {
340 bbox.xMin = bbox.yMin = 32000;
341 bbox.xMax = bbox.yMax = -32000;
342 for ( n = 0; n < string->num_glyphs; n++ ) {
344 FT_Glyph_Get_CBox( string->glyphs[n].image, ft_glyph_bbox_gridfit,
346 if (glyph_bbox.xMin < bbox.xMin) {
347 bbox.xMin = glyph_bbox.xMin;
349 if (glyph_bbox.yMin < bbox.yMin) {
350 bbox.yMin = glyph_bbox.yMin;
352 if (glyph_bbox.xMax > bbox.xMax) {
353 bbox.xMax = glyph_bbox.xMax;
355 if (glyph_bbox.yMax > bbox.yMax) {
356 bbox.yMax = glyph_bbox.yMax;
359 if ( bbox.xMin > bbox.xMax ) {
365 string->bbox.xMin = bbox.xMin;
366 string->bbox.xMax = bbox.xMax;
367 string->bbox.yMin = bbox.yMin;
368 string->bbox.yMax = bbox.yMax;
371 /* create a free type glyph string */
372 gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text,
373 int rotation, double tabwidth, double size )
376 FT_GlyphSlot slot = face->glyph; /* a small shortcut */
381 gfx_string string = (gfx_string) malloc (sizeof(struct gfx_string_s));
383 gfx_char glyph; /* current glyph in table */
390 size_t clen = strlen(text)+1;
391 cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
392 string->count=mbstowcs(cstr,text,clen);
393 if ( string->count == -1){
394 /* conversion did not work, so lets fall back to just use what we got */
395 string->count=clen-1;
396 for(n=0;text[n] != '\0';n++){
397 cstr[n]=(unsigned char)text[n];
401 char *cstr = strdup(text);
402 string->count = strlen (text);
405 ft_pen.x = 0; /* start at (0,0) !! */
411 string->glyphs = (gfx_char) calloc (string->count,sizeof(struct gfx_char_s));
412 string->num_glyphs = 0;
413 string->transform.xx = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
414 string->transform.xy = (FT_Fixed)(-sin(M_PI*(rotation)/180.0)*0x10000);
415 string->transform.yx = (FT_Fixed)( sin(M_PI*(rotation)/180.0)*0x10000);
416 string->transform.yy = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
418 use_kerning = FT_HAS_KERNING(face);
420 glyph = string->glyphs;
421 for (n=0; n<string->count;glyph++,n++) {
423 /* handle the tabs ...
424 have a witespace glyph inserted, but set its width such that the distance
425 of the new right edge is x times tabwidth from 0,0 where x is an integer. */
426 unsigned int letter = cstr[n];
427 letter = afm_fix_osx_charset(letter); /* unsafe macro */
430 if (letter == '\\' && n+1 < string->count && cstr[n+1] == 't'){
431 /* we have a tab here so skip the backslash and
432 set t to ' ' so that we get a white space */
441 /* initialize each struct gfx_char_s */
446 glyph->index = FT_Get_Char_Index( face, letter );
448 /* compute glyph origin */
449 if ( use_kerning && previous && glyph->index ) {
451 FT_Get_Kerning (face, previous, glyph->index,
452 ft_kerning_default, &kerning);
453 ft_pen.x += kerning.x;
454 ft_pen.y += kerning.y;
457 /* load the glyph image (in its native format) */
458 /* for now, we take a monochrome glyph bitmap */
459 error = FT_Load_Glyph (face, glyph->index, size > canvas->font_aa_threshold ?
460 canvas->aa_type == AA_NORMAL ? FT_LOAD_TARGET_NORMAL :
461 canvas->aa_type == AA_LIGHT ? FT_LOAD_TARGET_LIGHT :
462 FT_LOAD_TARGET_MONO : FT_LOAD_TARGET_MONO);
464 DPRINTF("couldn't load glyph: %c\n", letter)
467 error = FT_Get_Glyph (slot, &glyph->image);
469 DPRINTF("couldn't get glyph %c from slot %d\n", letter, (int)slot)
472 /* if we are in tabbing mode, we replace the tab with a space and shift the position
473 of the space so that its left edge is where the tab was supposed to land us */
475 /* we are in gridfitting mode so the calculations happen in 1/64 pixles */
476 ft_pen.x = tabwidth*64.0 * (float)(1 + (long)(ft_pen.x / (tabwidth * 64.0))) - slot->advance.x;
478 /* store current pen position */
479 glyph->pos.x = ft_pen.x;
480 glyph->pos.y = ft_pen.y;
483 ft_pen.x += slot->advance.x;
484 ft_pen.y += slot->advance.y;
488 FT_Vector_Transform (&vec, &string->transform);
489 error = FT_Glyph_Transform (glyph->image, &string->transform, &vec);
491 DPRINTF("couldn't transform glyph id %d\n", letter)
495 /* convert to a bitmap - destroy native image */
496 error = FT_Glyph_To_Bitmap (&glyph->image, size > canvas->font_aa_threshold ?
497 canvas->aa_type == AA_NORMAL ? FT_RENDER_MODE_NORMAL :
498 canvas->aa_type == AA_LIGHT ? FT_RENDER_MODE_LIGHT :
499 FT_RENDER_MODE_MONO : FT_RENDER_MODE_MONO, 0, 1);
501 DPRINTF("couldn't convert glyph id %d to bitmap\n", letter)
505 /* increment number of glyphs */
506 previous = glyph->index;
507 string->num_glyphs++;
510 /* printf ("number of glyphs = %d\n", string->num_glyphs);*/
511 compute_string_bbox( string );
512 /* the last character was a tab */
514 string->width = ft_pen.x;
516 string->width = string->bbox.xMax - string->bbox.xMin;
518 string->height = string->bbox.yMax - string->bbox.yMin;
523 static int gfx_save_png (art_u8 *buffer, FILE *fp,
524 long width, long height, long bytes_per_pixel);
525 /* render grafics into png image */
527 int gfx_render_png (gfx_canvas_t *canvas,
528 art_u32 width, art_u32 height,
529 gfx_color_t background, FILE *fp){
533 gfx_node_t *node = canvas->firstnode;
535 art_u8 red = background >> 24, green = (background >> 16) & 0xff;
536 art_u8 blue = (background >> 8) & 0xff, alpha = ( background & 0xff );
538 unsigned long pys_width = width * canvas->zoom;
539 unsigned long pys_height = height * canvas->zoom;
540 const int bytes_per_pixel = 4;
541 unsigned long rowstride = pys_width*bytes_per_pixel; /* bytes per pixel */
543 /* fill that buffer with out background color */
544 gfx_color_t *buffp = art_new (gfx_color_t, pys_width*pys_height);
545 art_u8 *buffer = (art_u8 *)buffp;
547 for (i=0;i<pys_width*pys_height;
549 *(buffp++)=background;
551 FT_Init_FreeType( &library );
553 switch (node->type) {
559 art_affine_scale(dst,canvas->zoom,canvas->zoom);
560 vec = art_vpath_affine_transform(node->path,dst);
561 if (node->closed_path)
562 gfx_libart_close_path(node, &vec);
563 /* gfx_round_scaled_coordinates(vec); */
564 /* pvec = art_vpath_perturb(vec);
566 if(node->type == GFX_LINE){
567 svp = art_svp_vpath_stroke ( vec, ART_PATH_STROKE_JOIN_ROUND,
568 ART_PATH_STROKE_CAP_ROUND,
569 node->size*canvas->zoom,4,0.25);
571 svp = art_svp_from_vpath ( vec );
572 /* this takes time and is unnecessary since we make
573 sure elsewhere that the areas are going clock-whise */
574 /* svpt = art_svp_uncross( svp );
576 svp = art_svp_rewind_uncrossed(svpt,ART_WIND_RULE_NONZERO);
581 /* this is from gnome since libart does not have this yet */
582 gnome_print_art_rgba_svp_alpha (svp ,0,0, pys_width, pys_height,
583 node->color, buffer, rowstride, NULL);
590 art_u8 fcolor[4],falpha;
594 FT_Vector vec; /* 26.6 */
596 float pen_x = 0.0 , pen_y = 0.0;
600 fcolor[0] = node->color >> 24;
601 fcolor[1] = (node->color >> 16) & 0xff;
602 fcolor[2] = (node->color >> 8) & 0xff;
603 falpha = node->color & 0xff;
604 error = FT_New_Face( library,
605 (char *)node->filename,
609 rrd_set_error("failed to load %s",node->filename);
613 error = FT_Set_Char_Size(face, /* handle to face object */
614 (long)(node->size*64),
615 (long)(node->size*64),
616 (long)(100*canvas->zoom),
617 (long)(100*canvas->zoom));
619 pen_x = node->x * canvas->zoom;
620 pen_y = node->y * canvas->zoom;
622 string = gfx_string_create (canvas, face, node->text, node->angle, node->tabwidth, node->size);
623 switch(node->halign){
624 case GFX_H_RIGHT: vec.x = -string->bbox.xMax;
626 case GFX_H_CENTER: vec.x = abs(string->bbox.xMax) >= abs(string->bbox.xMin) ?
627 -string->bbox.xMax/2:-string->bbox.xMin/2;
629 case GFX_H_LEFT: vec.x = -string->bbox.xMin;
631 case GFX_H_NULL: vec.x = 0;
635 switch(node->valign){
636 case GFX_V_TOP: vec.y = string->bbox.yMax;
638 case GFX_V_CENTER: vec.y = abs(string->bbox.yMax) >= abs(string->bbox.yMin) ?
639 string->bbox.yMax/2:string->bbox.yMin/2;
641 case GFX_V_BOTTOM: vec.y = 0;
643 case GFX_V_NULL: vec.y = 0;
648 glyph = string->glyphs;
649 for(n=0; n<string->num_glyphs; n++, glyph++) {
653 /* long buf_x,comp_n; */
654 /* make copy to transform */
655 if (! glyph->image) {
656 DPRINTF("no image\n")
659 error = FT_Glyph_Copy (glyph->image, &image);
661 DPRINTF("couldn't copy image\n")
667 FT_Vector_Transform (&vec, &string->transform);
669 bit = (FT_BitmapGlyph) image;
670 gr = bit->bitmap.num_grays -1;
672 buf_x = (pen_x + 0.5) + (double)bit->left;
673 comp_n = buf_x + bit->bitmap.width > pys_width ? pys_width - buf_x : bit->bitmap.width;
674 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
675 buf_x *= bytes_per_pixel ;
676 for (iy=0; iy < bit->bitmap.rows; iy++){
677 long buf_y = iy+(pen_y+0.5)-(double)bit->top;
678 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
680 for (ix=0;ix < bit->bitmap.width;ix++){
681 *(letter + (ix*bytes_per_pixel+3)) = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
683 art_rgba_rgba_composite(buffer + buf_y + buf_x ,letter,comp_n);
687 switch ( bit->bitmap.pixel_mode ) {
688 case FT_PIXEL_MODE_GRAY:
689 for (iy=0; iy < bit->bitmap.rows; iy++){
690 long buf_y = iy+(pen_y+0.5)-bit->top;
691 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
693 for (ix=0;ix < bit->bitmap.width;ix++){
694 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
697 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
698 buf_x *= bytes_per_pixel ;
699 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix);
701 fcolor[3] = (art_u8)((double)font_alpha / gr * falpha);
702 art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
708 case FT_PIXEL_MODE_MONO:
709 for (iy=0; iy < bit->bitmap.rows; iy++){
710 long buf_y = iy+(pen_y+0.5)-bit->top;
711 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
713 for (ix=0;ix < bit->bitmap.width;ix++){
714 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
716 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
717 buf_x *= bytes_per_pixel ;
718 if ( (fcolor[3] = falpha * ((*(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix/8) >> (7 - (ix % 8))) & 1)) > 0 )
719 art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
725 rrd_set_error("unknown freetype pixel mode: %d", bit->bitmap.pixel_mode);
730 for (iy=0; iy < bit->bitmap.rows; iy++){
731 long buf_y = iy+(pen_y+0.5)-bit->top;
732 if (buf_y < 0 || buf_y >= (long)pys_height) continue;
734 for (ix=0;ix < bit->bitmap.width;ix++){
735 long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
738 if (buf_x < 0 || buf_x >= (long)pys_width) continue;
739 buf_x *= bytes_per_pixel ;
740 font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
741 font_alpha = (art_u8)((double)font_alpha / gr * falpha);
742 for (iz = 0; iz < 3; iz++){
743 art_u8 *orig = buffer + buf_y + buf_x + iz;
744 *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
745 (double)fcolor[iz] / gr * (font_alpha));
750 FT_Done_Glyph (image);
752 gfx_string_destroy(string);
757 gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
759 FT_Done_FreeType( library );
763 /* free memory used by nodes this will also remove memory required for
764 associated paths and svcs ... but not for text strings */
766 gfx_destroy (gfx_canvas_t *canvas){
767 gfx_node_t *next,*node = canvas->firstnode;
770 art_free(node->path);
772 free(node->filename);
780 static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
781 png_structp png_ptr = NULL;
782 png_infop info_ptr = NULL;
784 png_bytep *row_pointers;
785 int rowstride = width * bytes_per_pixel;
791 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
796 row_pointers = (png_bytepp)png_malloc(png_ptr,
797 height*sizeof(png_bytep));
799 info_ptr = png_create_info_struct(png_ptr);
801 if (info_ptr == NULL)
803 png_free(png_ptr,row_pointers);
804 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
808 if (setjmp(png_jmpbuf(png_ptr)))
810 /* If we get here, we had a problem writing the file */
811 png_destroy_write_struct(&png_ptr, &info_ptr);
815 png_init_io(png_ptr, fp);
816 png_set_IHDR (png_ptr, info_ptr,width, height,
817 8, PNG_COLOR_TYPE_RGB_ALPHA,
819 PNG_COMPRESSION_TYPE_DEFAULT,
820 PNG_FILTER_TYPE_DEFAULT);
822 text[0].key = "Software";
823 text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
824 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
825 png_set_text (png_ptr, info_ptr, text, 1);
827 /* lets make this fast while ending up with some increass in image size */
828 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
829 /* png_set_filter(png_ptr,0,PNG_FILTER_SUB); */
830 png_set_compression_level(png_ptr,1);
831 /* png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY); */
833 png_set_filter(png_ptr,PNG_FILTER_TYPE_BASE,PNG_FILTER_SUB);
834 png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY);
835 png_set_compression_level(png_ptr,Z_BEST_SPEED); */
837 /* Write header data */
838 png_write_info (png_ptr, info_ptr);
839 for (i = 0; i < height; i++)
840 row_pointers[i] = (png_bytep) (buffer + i*rowstride);
842 png_write_image(png_ptr, row_pointers);
843 png_write_end(png_ptr, info_ptr);
844 png_free(png_ptr,row_pointers);
845 png_destroy_write_struct(&png_ptr, &info_ptr);
850 /* ----- COMMON ROUTINES for pdf, svg and eps */
851 #define min3(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
852 #define max3(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
854 #define PDF_CALC_DEBUG 0
856 typedef struct pdf_point
863 double ascender, descender, baselineY;
864 pdf_point sizep, minp, maxp;
865 double x, y, tdx, tdy;
866 double r, cos_r, sin_r;
867 double ma, mb, mc, md, mx, my; /* pdf coord matrix */
868 double tmx, tmy; /* last 2 coords of text coord matrix */
875 static void pdf_dump_calc(gfx_node_t *node, pdf_coords *g)
877 fprintf(stderr, "PDF CALC =============================\n");
878 fprintf(stderr, " '%s' at %f pt\n", node->text, node->size);
879 fprintf(stderr, " align h = %s, v = %s, sizep = %f, %f\n",
880 (node->halign == GFX_H_RIGHT ? "r" :
881 (node->halign == GFX_H_CENTER ? "c" :
882 (node->halign == GFX_H_LEFT ? "l" : "N"))),
883 (node->valign == GFX_V_TOP ? "t" :
884 (node->valign == GFX_V_CENTER ? "c" :
885 (node->valign == GFX_V_BOTTOM ? "b" : "N"))),
886 g->sizep.x, g->sizep.y);
887 fprintf(stderr, " r = %f = %f, cos = %f, sin = %f\n",
888 g->r, node->angle, g->cos_r, g->sin_r);
889 fprintf(stderr, " ascender = %f, descender = %f, baselineY = %f\n",
890 g->ascender, g->descender, g->baselineY);
891 fprintf(stderr, " sizep: %f, %f\n", g->sizep.x, g->sizep.y);
892 fprintf(stderr, " minp: %f, %f maxp = %f, %f\n",
893 g->minp.x, g->minp.y, g->maxp.x, g->maxp.y);
894 fprintf(stderr, " x = %f, y = %f\n", g->x, g->y);
895 fprintf(stderr, " tdx = %f, tdy = %f\n", g->tdx, g->tdy);
896 fprintf(stderr, " GM = %f, %f, %f, %f, %f, %f\n",
897 g->ma, g->mb, g->mc, g->md, g->mx, g->my);
898 fprintf(stderr, " TM = %f, %f, %f, %f, %f, %f\n",
899 g->ma, g->mb, g->mc, g->md, g->tmx, g->tmy);
904 #define PDF_DD(x) if (g->debug) x;
909 static void pdf_rotate(pdf_coords *g, pdf_point *p)
911 double x2 = g->cos_r * p->x - g->sin_r * p->y;
912 double y2 = g->sin_r * p->x + g->cos_r * p->y;
913 PDF_DD( fprintf(stderr, " rotate(%f, %f) -> %f, %f\n", p->x, p->y, x2, y2))
919 static void pdf_calc(int page_height, gfx_node_t *node, pdf_coords *g)
923 /* g->debug = !!strstr(node->text, "RevProxy-1") || !!strstr(node->text, "08:00"); */
924 g->debug = !!strstr(node->text, "sekunder") || !!strstr(node->text, "Web");
927 g->y = page_height - node->y;
929 g->r = 2 * M_PI * node->angle / 360.0;
930 g->cos_r = cos(g->r);
931 g->sin_r = sin(g->r);
937 g->ascender = afm_get_ascender(node->filename, node->size);
938 g->descender = afm_get_descender(node->filename, node->size);
939 g->sizep.x = afm_get_text_width(0, node->filename, node->size, node->tabwidth, node->text);
940 /* seems like libart ignores the descender when doing vertial-align = bottom,
941 so we do that too, to get labels v-aligning properly */
942 g->sizep.y = -g->ascender; /* + afm_get_descender(font->ps_font, node->size); */
943 g->baselineY = -g->ascender - g->sizep.y / 2;
944 a.x = g->sizep.x; a.y = g->sizep.y;
945 b.x = g->sizep.x; b.y = 0;
946 c.x = 0; c.y = g->sizep.y;
952 g->minp.x = min3(a.x, b.x, c.x);
953 g->minp.y = min3(a.y, b.y, c.y);
954 g->maxp.x = max3(a.x, b.x, c.x);
955 g->maxp.y = max3(a.y, b.y, c.y);
956 /* The alignment parameters in node->valign and node->halign
957 specifies the alignment in the non-rotated coordinate system
958 (very unlike pdf/postscript), which complicates matters.
960 switch (node->halign) {
961 case GFX_H_RIGHT: g->tdx = -g->maxp.x; break;
962 case GFX_H_CENTER: g->tdx = -(g->maxp.x + g->minp.x) / 2; break;
963 case GFX_H_LEFT: g->tdx = -g->minp.x; break;
964 case GFX_H_NULL: g->tdx = 0; break;
966 switch(node->valign){
967 case GFX_V_TOP: g->tdy = -g->maxp.y; break;
968 case GFX_V_CENTER: g->tdy = -(g->maxp.y + g->minp.y) / 2; break;
969 case GFX_V_BOTTOM: g->tdy = -g->minp.y; break;
970 case GFX_V_NULL: g->tdy = 0; break;
976 g->mx = g->x + g->tdx;
977 g->my = g->y + g->tdy;
978 g->tmx = g->mx - g->ascender * g->mc;
979 g->tmy = g->my - g->ascender * g->md;
980 PDF_DD(pdf_dump_calc(node, g))
983 /* ------- SVG -------
985 http://www.w3.org/TR/SVG/
987 static int svg_indent = 0;
988 static int svg_single_line = 0;
989 static const char *svg_default_font = "-dummy-";
990 typedef struct svg_dash
993 double dash_adjust, dash_len, dash_offset;
994 double adjusted_on, adjusted_off;
998 static void svg_print_indent(FILE *fp)
1001 for (i = svg_indent - svg_single_line; i > 0; i--) {
1007 static void svg_start_tag(FILE *fp, const char *name)
1009 svg_print_indent(fp);
1015 static void svg_close_tag_single_line(FILE *fp)
1021 static void svg_close_tag(FILE *fp)
1024 if (!svg_single_line)
1028 static void svg_end_tag(FILE *fp, const char *name)
1030 /* name is NULL if closing empty-node tag */
1032 if (svg_single_line)
1035 svg_print_indent(fp);
1045 static void svg_close_tag_empty_node(FILE *fp)
1047 svg_end_tag(fp, NULL);
1050 static void svg_write_text(FILE *fp, const char *text)
1052 #ifdef HAVE_MBSTOWCS
1054 wchar_t *p, *cstr, ch;
1058 clen = strlen(text) + 1;
1059 cstr = malloc(sizeof(wchar_t) * clen);
1060 text_count = mbstowcs(cstr, text, clen);
1061 if (text_count == -1)
1062 text_count = mbstowcs(cstr, "Enc-Err", 6);
1065 const unsigned char *p = text, ch;
1071 ch = afm_fix_osx_charset(ch); /* unsafe macro */
1074 #ifdef HAVE_MBSTOWCS
1078 case '&': fputs("&", fp); break;
1079 case '<': fputs("<", fp); break;
1080 case '>': fputs(">", fp); break;
1081 case '"': fputs(""", fp); break;
1084 if (p <= cstr + 1 || !*p || *p == 32)
1085 fputs(" ", fp); /* non-breaking space in unicode */
1088 } else if (ch < 32 || ch >= 127)
1089 fprintf(fp, "&#%d;", (int)ch);
1096 static void svg_format_number(char *buf, int bufsize, double d)
1098 /* omit decimals if integer to reduce filesize */
1100 snprintf(buf, bufsize, "%.2f", d);
1101 p = buf; /* doesn't trust snprintf return value */
1107 *p = '\0'; /* zap trailing zeros */
1111 *p = '\0'; /* zap trailing dot */
1116 static void svg_write_number(FILE *fp, double d)
1119 svg_format_number(buf, sizeof(buf), d);
1123 static int svg_color_is_black(int c)
1125 /* gfx_color_t is RRGGBBAA */
1126 return c == 0x000000FF;
1129 static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
1131 /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
1132 gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
1133 gfx_color_t opacity = c & 0xFF;
1134 fprintf(fp, " %s=\"", attr);
1135 if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
1136 /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
1137 fprintf(fp, "#%03lX",
1138 ( ((rrggbb >> 8) & 0xF00)
1139 | ((rrggbb >> 4) & 0x0F0)
1140 | ( rrggbb & 0x00F)));
1142 fprintf(fp, "#%06lX", rrggbb);
1145 if (opacity != 0xFF) {
1146 fprintf(fp, " opacity=\"");
1147 svg_write_number(fp, opacity / 255.0);
1152 static void svg_get_dash(gfx_node_t *node, svg_dash *d)
1156 if (node->dash_on <= 0 || node->dash_off <= 0) {
1161 d->dash_len = node->dash_on + node->dash_off;
1162 /* dash on/off adjustment due to round caps */
1163 d->dash_adjust = 0.8 * node->size;
1164 d->adjusted_on = node->dash_on - d->dash_adjust;
1165 if (d->adjusted_on < 0.01)
1166 d->adjusted_on = 0.01;
1167 d->adjusted_off = d->dash_len - d->adjusted_on;
1168 /* dash offset calc */
1169 if (node->path[0].x == node->path[1].x) /* only good for horz/vert lines */
1170 offset = node->path[0].y;
1172 offset = node->path[0].x;
1173 mult = (int)fabs(offset / d->dash_len);
1174 d->dash_offset = offset - mult * d->dash_len;
1175 if (node->path[0].x < node->path[1].x || node->path[0].y < node->path[1].y)
1176 d->dash_offset = d->dash_len - d->dash_offset;
1179 static int svg_dash_equal(svg_dash *a, svg_dash *b)
1181 if (a->dash_enable != b->dash_enable)
1183 if (a->adjusted_on != b->adjusted_on)
1185 if (a->adjusted_off != b->adjusted_off)
1187 /* rest of properties will be the same when on+off are */
1191 static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
1194 svg_get_dash(node, &dash_info);
1195 fputs(" stroke-width=\"", fp);
1196 svg_write_number(fp, node->size);
1198 svg_write_color(fp, node->color, "stroke");
1199 fputs(" fill=\"none\"", fp);
1200 if (dash_info.dash_enable) {
1201 if (dash_info.dash_offset != 0) {
1202 fputs(" stroke-dashoffset=\"", fp);
1203 svg_write_number(fp, dash_info.dash_offset);
1206 fputs(" stroke-dasharray=\"", fp);
1207 svg_write_number(fp, dash_info.adjusted_on);
1209 svg_write_number(fp, dash_info.adjusted_off);
1214 static int svg_is_int_step(double a, double b)
1216 double diff = fabs(a - b);
1217 return floor(diff) == diff;
1220 static int svg_path_straight_segment(FILE *fp,
1221 double lastA, double currentA, double currentB,
1223 int segment_idx, int isx, char absChar, char relChar)
1225 if (!svg_is_int_step(lastA, currentA)) {
1227 svg_write_number(fp, currentA);
1230 if (segment_idx < node->points - 1) {
1231 ArtVpath *vec = node->path + segment_idx + 1;
1232 if (vec->code == ART_LINETO) {
1233 double nextA = (isx ? vec->x : vec->y) - LINEOFFSET;
1234 double nextB = (isx ? vec->y : vec->x) - LINEOFFSET;
1235 if (nextB == currentB
1236 && ((currentA >= lastA) == (nextA >= currentA))
1237 && svg_is_int_step(currentA, nextA)) {
1238 return 1; /* skip to next as it is a straight line */
1243 svg_write_number(fp, currentA - lastA);
1247 static void svg_path(FILE *fp, gfx_node_t *node, int multi)
1250 double lastX = 0, lastY = 0;
1251 /* for straight lines <path..> tags take less space than
1252 <line..> tags because of the efficient packing
1253 in the 'd' attribute */
1254 svg_start_tag(fp, "path");
1256 svg_common_path_attributes(fp, node);
1258 /* specification of the 'd' attribute: */
1259 /* http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation */
1260 for (i = 0; i < node->points; i++) {
1261 ArtVpath *vec = node->path + i;
1262 double x = vec->x - LINEOFFSET;
1263 double y = vec->y - LINEOFFSET;
1264 switch (vec->code) {
1265 case ART_MOVETO_OPEN: /* fall-through */
1268 svg_write_number(fp, x);
1270 svg_write_number(fp, y);
1273 /* try optimize filesize by using minimal lineto commands */
1274 /* without introducing rounding errors. */
1276 if (svg_path_straight_segment(fp, lastY, y, x, node, i, 0, 'V', 'v'))
1278 } else if (y == lastY) {
1279 if (svg_path_straight_segment(fp, lastX, x, y, node, i, 1, 'H', 'h'))
1283 svg_write_number(fp, x);
1285 svg_write_number(fp, y);
1288 case ART_CURVETO: break; /* unsupported */
1289 case ART_END: break; /* nop */
1294 if (node->closed_path)
1297 svg_close_tag_empty_node(fp);
1300 static void svg_multi_path(FILE *fp, gfx_node_t **nodeR)
1302 /* optimize for multiple paths with the same color, penwidth, etc. */
1304 gfx_node_t *node = *nodeR;
1305 gfx_node_t *next = node->next;
1307 if (next->type != node->type
1308 || next->size != node->size
1309 || next->color != node->color
1310 || next->dash_on != node->dash_on
1311 || next->dash_off != node->dash_off)
1317 svg_path(fp, node, 0);
1320 svg_start_tag(fp, "g");
1321 svg_common_path_attributes(fp, node);
1323 while (num && node) {
1324 svg_path(fp, node, 1);
1330 svg_end_tag(fp, "g");
1333 static void svg_area(FILE *fp, gfx_node_t *node)
1336 double startX = 0, startY = 0;
1337 svg_start_tag(fp, "polygon");
1339 svg_write_color(fp, node->color, "fill");
1340 fputs(" points=\"", fp);
1341 for (i = 0; i < node->points; i++) {
1342 ArtVpath *vec = node->path + i;
1343 double x = vec->x - LINEOFFSET;
1344 double y = vec->y - LINEOFFSET;
1345 switch (vec->code) {
1346 case ART_MOVETO_OPEN: /* fall-through */
1348 svg_write_number(fp, x);
1350 svg_write_number(fp, y);
1355 if (i == node->points - 2
1356 && node->path[i + 1].code == ART_END
1357 && fabs(x - startX) < 0.001 && fabs(y - startY) < 0.001) {
1358 break; /* poly area always closed, no need for last point */
1361 svg_write_number(fp, x);
1363 svg_write_number(fp, y);
1365 case ART_CURVETO: break; /* unsupported */
1366 case ART_END: break; /* nop */
1370 svg_close_tag_empty_node(fp);
1373 static void svg_text(FILE *fp, gfx_node_t *node)
1376 const char *fontname;
1377 /* as svg has 0,0 in top-left corner (like most screens) instead of
1378 bottom-left corner like pdf and eps, we have to fake the coords
1379 using offset and inverse sin(r) value */
1380 int page_height = 1000;
1381 pdf_calc(page_height, node, &g);
1382 if (node->angle != 0) {
1383 svg_start_tag(fp, "g");
1384 /* can't use svg_write_number as 2 decimals is far from enough to avoid
1386 fprintf(fp, " transform=\"matrix(%f,%f,%f,%f,%f,%f)\"",
1387 g.ma, -g.mb, -g.mc, g.md, g.tmx, page_height - g.tmy);
1390 svg_start_tag(fp, "text");
1393 svg_write_number(fp, g.tmx);
1394 fputs("\" y=\"", fp);
1395 svg_write_number(fp, page_height - g.tmy);
1398 fontname = afm_get_font_name(node->filename);
1399 if (strcmp(fontname, svg_default_font))
1400 fprintf(fp, " font-family=\"%s\"", fontname);
1401 fputs(" font-size=\"", fp);
1402 svg_write_number(fp, node->size);
1404 if (!svg_color_is_black(node->color))
1405 svg_write_color(fp, node->color, "fill");
1406 svg_close_tag_single_line(fp);
1407 /* support for node->tabwidth missing */
1408 svg_write_text(fp, node->text);
1409 svg_end_tag(fp, "text");
1410 if (node->angle != 0)
1411 svg_end_tag(fp, "g");
1414 int gfx_render_svg (gfx_canvas_t *canvas,
1415 art_u32 width, art_u32 height,
1416 gfx_color_t background, FILE *fp){
1417 gfx_node_t *node = canvas->firstnode;
1418 /* Find the first font used, and assume it is the mostly used
1419 one. It reduces the number of font-familty attributes. */
1421 if (node->type == GFX_TEXT && node->filename) {
1422 svg_default_font = afm_get_font_name(node->filename);
1428 "<?xml version=\"1.0\" standalone=\"no\"?>\n"
1429 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
1430 " \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
1432 " SVG file created by\n"
1433 " RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
1435 " The width/height attributes in the outhermost svg node\n"
1436 " are just default sizes for the browser which is used\n"
1437 " if the svg file is openened directly without being\n"
1438 " embedded in an html file.\n"
1439 " The viewBox is the local coord system for rrdtool.\n"
1441 svg_start_tag(fp, "svg");
1442 fputs(" width=\"", fp);
1443 svg_write_number(fp, width * canvas->zoom);
1444 fputs("\" height=\"", fp);
1445 svg_write_number(fp, height * canvas->zoom);
1446 fputs("\" x=\"0\" y=\"0\" viewBox=\"", fp);
1447 svg_write_number(fp, -LINEOFFSET);
1449 svg_write_number(fp, -LINEOFFSET);
1451 svg_write_number(fp, width - LINEOFFSET);
1453 svg_write_number(fp, height - LINEOFFSET);
1454 fputs("\" preserveAspectRatio=\"xMidYMid\"", fp);
1455 fprintf(fp, " font-family=\"%s\"", svg_default_font); /* default font */
1456 fputs(" stroke-linecap=\"round\" stroke-linejoin=\"round\"", fp);
1457 fputs(" xmlns=\"http://www.w3.org/2000/svg\"", fp);
1458 fputs(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"", fp);
1460 svg_start_tag(fp, "rect");
1461 fprintf(fp, " x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"", width, height);
1462 svg_write_color(fp, background, "fill");
1463 svg_close_tag_empty_node(fp);
1464 node = canvas->firstnode;
1466 switch (node->type) {
1468 svg_multi_path(fp, &node);
1478 svg_end_tag(fp, "svg");
1482 /* ------- EPS -------
1483 EPS and Postscript references:
1484 http://partners.adobe.com/asn/developer/technotes/postscript.html
1487 typedef struct eps_font
1489 const char *ps_font;
1491 struct eps_font *next;
1494 typedef struct eps_state
1497 gfx_canvas_t *canvas;
1498 art_u32 page_width, page_height;
1499 eps_font *font_list;
1505 int linecap, linejoin;
1509 static void eps_set_color(eps_state *state, gfx_color_t color)
1511 #if USE_EPS_FAKE_ALPHA
1514 /* gfx_color_t is RRGGBBAA */
1515 if (state->color == color)
1517 #if USE_EPS_FAKE_ALPHA
1518 a1 = (color & 255) / 255.0;
1519 a2 = 255 * (1 - a1);
1520 #define eps_color_calc(x) (int)( ((x) & 255) * a1 + a2)
1522 #define eps_color_calc(x) (int)( (x) & 255)
1524 /* gfx_color_t is RRGGBBAA */
1525 if (state->color == color)
1527 fprintf(state->fp, "%d %d %d Rgb\n",
1528 eps_color_calc(color >> 24),
1529 eps_color_calc(color >> 16),
1530 eps_color_calc(color >> 8));
1531 state->color = color;
1534 static int eps_add_font(eps_state *state, gfx_node_t *node)
1536 /* The fonts list could be postponed to the end using
1537 (atend), but let's be nice and have them in the header. */
1538 const char *ps_font = afm_get_font_postscript_name(node->filename);
1540 for (ef = state->font_list; ef; ef = ef->next) {
1541 if (!strcmp(ps_font, ef->ps_font))
1544 ef = malloc(sizeof(eps_font));
1546 rrd_set_error("malloc for eps_font");
1549 ef->next = state->font_list;
1550 ef->ps_font = ps_font;
1551 state->font_list = ef;
1555 static void eps_list_fonts(eps_state *state, const char *dscName)
1558 int lineLen = strlen(dscName);
1559 if (!state->font_list)
1561 fputs(dscName, state->fp);
1562 for (ef = state->font_list; ef; ef = ef->next) {
1563 int nameLen = strlen(ef->ps_font);
1564 if (lineLen + nameLen > 100 && lineLen) {
1565 fputs("\n", state->fp);
1566 fputs("%%- \n", state->fp);
1569 fputs(" ", state->fp);
1572 fputs(ef->ps_font, state->fp);
1575 fputs("\n", state->fp);
1578 static void eps_define_fonts(eps_state *state)
1581 if (!state->font_list)
1583 for (ef = state->font_list; ef; ef = ef->next) {
1584 /* PostScript¨ LANGUAGE REFERENCE third edition
1588 "/%s findfont dup length dict begin\n"
1589 "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
1590 "/Encoding ISOLatin1Encoding def\n"
1592 "/%s-ISOLatin1 exch definefont pop\n"
1593 "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
1594 ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
1598 static int eps_prologue(eps_state *state)
1602 "%!PS-Adobe-3.0 EPSF-3.0\n"
1603 "%%Creator: RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
1604 /* can't like weird chars here */
1605 "%%Title: (RRDtool output)\n"
1606 "%%DocumentData: Clean7Bit\n"
1608 fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
1609 state->page_width, state->page_height);
1610 for (node = state->canvas->firstnode; node; node = node->next) {
1611 if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
1614 eps_list_fonts(state, "%%DocumentFonts:");
1615 eps_list_fonts(state, "%%DocumentNeededFonts:");
1619 "%%EndProlog\n" /* must have, or BoundingBox is ignored */
1620 "/bd { bind def } bind def\n"
1622 fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
1624 "/X2 {X exch X exch} bd\n"
1625 "/M {X2 moveto} bd\n"
1626 "/L {X2 lineto} bd\n"
1630 "/CP {closepath} bd\n"
1631 "/WS {setlinewidth stroke} bd\n"
1634 "/T2 {concat 0 0 moveto show grestore} bd\n"
1635 "/T {moveto show} bd\n"
1636 "/Rgb { 255.0 div 3 1 roll\n"
1637 " 255.0 div 3 1 roll \n"
1638 " 255.0 div 3 1 roll setrgbcolor } bd\n"
1640 eps_define_fonts(state);
1644 static void eps_clear_dash(eps_state *state)
1646 if (!state->has_dash)
1648 state->has_dash = 0;
1649 fputs("[1 0] 0 setdash\n", state->fp);
1652 static void eps_write_linearea(eps_state *state, gfx_node_t *node)
1655 FILE *fp = state->fp;
1657 int clearDashIfAny = 1;
1658 eps_set_color(state, node->color);
1659 if (node->type == GFX_LINE) {
1661 if (state->linecap != 1) {
1662 fputs("1 setlinecap\n", fp);
1665 if (state->linejoin != 1) {
1666 fputs("1 setlinejoin\n", fp);
1667 state->linejoin = 1;
1669 svg_get_dash(node, &dash_info);
1670 if (dash_info.dash_enable) {
1672 state->has_dash = 1;
1674 svg_write_number(fp, dash_info.adjusted_on);
1676 svg_write_number(fp, dash_info.adjusted_off);
1678 svg_write_number(fp, dash_info.dash_offset);
1679 fputs(" setdash\n", fp);
1683 eps_clear_dash(state);
1684 for (i = 0; i < node->points; i++) {
1685 ArtVpath *vec = node->path + i;
1687 double y = state->page_height - vec->y;
1688 if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
1689 useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
1694 switch (vec->code) {
1695 case ART_MOVETO_OPEN: /* fall-through */
1697 svg_write_number(fp, x);
1699 svg_write_number(fp, y);
1701 fputs(useOffset ? "M\n" : "m\n", fp);
1704 svg_write_number(fp, x);
1706 svg_write_number(fp, y);
1708 fputs(useOffset ? "L\n" : "l\n", fp);
1710 case ART_CURVETO: break; /* unsupported */
1711 case ART_END: break; /* nop */
1714 if (node->type == GFX_LINE) {
1715 if (node->closed_path)
1717 if (node->size != state->line_width) {
1718 state->line_width = node->size;
1719 svg_write_number(fp, state->line_width);
1729 static void eps_write_text(eps_state *state, gfx_node_t *node)
1731 FILE *fp = state->fp;
1732 const char *ps_font = afm_get_font_postscript_name(node->filename);
1735 #ifdef HAVE_MBSTOWCS
1737 wchar_t *p, *cstr, ch;
1741 clen = strlen(node->text) + 1;
1742 cstr = malloc(sizeof(wchar_t) * clen);
1743 text_count = mbstowcs(cstr, node->text, clen);
1744 if (text_count == -1)
1745 text_count = mbstowcs(cstr, "Enc-Err", 6);
1748 const unsigned char *p = node->text, ch;
1752 pdf_calc(state->page_height, node, &g);
1753 eps_set_color(state, node->color);
1754 if (strcmp(ps_font, state->font) || node->size != state->font_size) {
1755 state->font = ps_font;
1756 state->font_size = node->size;
1757 svg_write_number(fp, state->font_size);
1758 fprintf(fp, " SetFont-%s\n", state->font);
1768 ch = afm_fix_osx_charset(ch); /* unsafe macro */
1769 if (++lineLen > 70) {
1770 fputs("\\\n", fp); /* backslash and \n */
1793 } else if (ch >= 126 || ch < 32) {
1794 fprintf(fp, "\\%03o", (unsigned int)ch);
1802 #ifdef HAVE_MBSTOWCS
1806 /* can't use svg_write_number as 2 decimals is far from enough to avoid
1808 fprintf(fp, ") [%f %f %f %f %f %f] T2\n",
1809 g.ma, g.mb, g.mc, g.md, g.tmx, g.tmy);
1812 svg_write_number(fp, g.tmx);
1814 svg_write_number(fp, g.tmy);
1819 static int eps_write_content(eps_state *state)
1822 fputs("%\n", state->fp);
1823 for (node = state->canvas->firstnode; node; node = node->next) {
1824 switch (node->type) {
1827 eps_write_linearea(state, node);
1830 eps_write_text(state, node);
1837 int gfx_render_eps (gfx_canvas_t *canvas,
1838 art_u32 width, art_u32 height,
1839 gfx_color_t background, FILE *fp){
1840 struct eps_state state;
1842 state.canvas = canvas;
1843 state.page_width = width;
1844 state.page_height = height;
1845 state.font = "no-default-font";
1846 state.font_size = -1;
1847 state.color = 0; /* black */
1848 state.font_list = NULL;
1850 state.linejoin = -1;
1852 state.line_width = 1;
1853 if (eps_prologue(&state) == -1)
1855 eps_set_color(&state, background);
1856 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
1857 height, width, height, width);
1858 if (eps_write_content(&state) == -1)
1860 fputs("showpage\n", fp);
1861 fputs("%%EOF\n", fp);
1862 while (state.font_list) {
1863 eps_font *next = state.font_list->next;
1864 free(state.font_list);
1865 state.font_list = next;
1870 /* ------- PDF -------
1871 PDF references page:
1872 http://partners.adobe.com/public/developer/pdf/index_reference.html
1875 typedef struct pdf_buffer
1877 int id, is_obj, is_dict, is_stream, pdf_file_pos;
1879 int alloc_size, current_size;
1880 struct pdf_buffer *previous_buffer, *next_buffer;
1881 struct pdf_state *state;
1884 typedef struct pdf_font
1886 const char *ps_font;
1888 struct pdf_font *next;
1891 typedef struct pdf_state
1894 gfx_canvas_t *canvas;
1895 art_u32 page_width, page_height;
1896 pdf_font *font_list;
1897 pdf_buffer *first_buffer, *last_buffer;
1901 gfx_color_t stroke_color, fill_color;
1906 int linecap, linejoin;
1909 pdf_buffer pdf_header;
1910 pdf_buffer info_obj, catalog_obj, pages_obj, page1_obj;
1911 pdf_buffer fontsdict_obj;
1912 pdf_buffer graph_stream;
1915 static void pdf_init_buffer(pdf_state *state, pdf_buffer *buf)
1917 int initial_size = 32;
1920 buf->alloc_size = 0;
1921 buf->current_size = 0;
1922 buf->data = (char*)malloc(initial_size);
1924 buf->previous_buffer = NULL;
1925 buf->next_buffer = NULL;
1926 if (buf->data == NULL) {
1927 rrd_set_error("malloc for pdf_buffer data");
1928 state->has_failed = 1;
1931 buf->alloc_size = initial_size;
1932 if (state->last_buffer)
1933 state->last_buffer->next_buffer = buf;
1934 if (state->first_buffer == NULL)
1935 state->first_buffer = buf;
1936 buf->previous_buffer = state->last_buffer;
1937 state->last_buffer = buf;
1940 static void pdf_put(pdf_buffer *buf, const char *text, int len)
1944 if (buf->alloc_size < buf->current_size + len) {
1945 int new_size = buf->alloc_size;
1947 while (new_size < buf->current_size + len)
1949 new_buf = (char*)malloc(new_size);
1950 if (new_buf == NULL) {
1951 rrd_set_error("re-malloc for pdf_buffer data");
1952 buf->state->has_failed = 1;
1955 memcpy(new_buf, buf->data, buf->current_size);
1957 buf->data = new_buf;
1958 buf->alloc_size = new_size;
1960 memcpy(buf->data + buf->current_size, text, len);
1961 buf->current_size += len;
1964 static void pdf_put_char(pdf_buffer *buf, char c)
1966 if (buf->alloc_size >= buf->current_size + 1) {
1967 buf->data[buf->current_size++] = c;
1971 pdf_put(buf, tmp, 1);
1975 static void pdf_puts(pdf_buffer *buf, const char *text)
1977 pdf_put(buf, text, strlen(text));
1980 static void pdf_indent(pdf_buffer *buf)
1982 pdf_puts(buf, "\t");
1985 static void pdf_putsi(pdf_buffer *buf, const char *text)
1988 pdf_puts(buf, text);
1991 static void pdf_putint(pdf_buffer *buf, int i)
1994 sprintf(tmp, "%d", i);
1998 static void pdf_putnumber(pdf_buffer *buf, double d)
2001 svg_format_number(tmp, sizeof(tmp), d);
2005 static void pdf_put_string_contents_wide(pdf_buffer *buf, const afm_char *text)
2007 const afm_char *p = text;
2010 ch = afm_fix_osx_charset(ch); /* unsafe macro */
2015 pdf_puts(buf, "\\(");
2018 pdf_puts(buf, "\\)");
2021 pdf_puts(buf, "\\\\");
2024 pdf_puts(buf, "\\n");
2027 pdf_puts(buf, "\\r");
2030 pdf_puts(buf, "\\t");
2034 pdf_put_char(buf, '?');
2035 } else if (ch >= 126 || ch < 32) {
2036 pdf_put_char(buf, ch);
2037 } else if (ch >= 0 && ch <= 255) {
2039 snprintf(tmp, sizeof(tmp), "\\%03o", (int)ch);
2047 static void pdf_put_string_contents(pdf_buffer *buf, const char *text)
2049 #ifdef HAVE_MBSTOWCS
2050 size_t clen = strlen(text) + 1;
2051 wchar_t *cstr = malloc(sizeof(wchar_t) * clen);
2052 int text_count = mbstowcs(cstr, text, clen);
2053 if (text_count == -1)
2054 text_count = mbstowcs(cstr, "Enc-Err", 6);
2055 pdf_put_string_contents_wide(buf, cstr);
2058 fprintf(stderr, "Decoding utf8 for '%s'\n", text);
2061 fprintf(stderr, "sz wc = %d\n", sizeof(wchar_t));
2063 fprintf(stderr, " %d = %c versus %d = %c\n", *p, (char)*p, 255 & (int)*pp, *pp);
2071 pdf_put_string_contents_wide(buf, text);
2075 static void pdf_init_object(pdf_state *state, pdf_buffer *buf)
2077 pdf_init_buffer(state, buf);
2078 buf->id = ++state->last_obj_id;
2083 static void pdf_init_dict(pdf_state *state, pdf_buffer *buf)
2085 pdf_init_object(state, buf);
2089 static void pdf_set_color(pdf_buffer *buf, gfx_color_t color,
2090 gfx_color_t *current_color, const char *op)
2092 #if USE_PDF_FAKE_ALPHA
2095 /* gfx_color_t is RRGGBBAA */
2096 if (*current_color == color)
2098 #if USE_PDF_FAKE_ALPHA
2099 a1 = (color & 255) / 255.0;
2101 #define pdf_color_calc(x) ( ((x) & 255) / 255.0 * a1 + a2)
2103 #define pdf_color_calc(x) ( ((x) & 255) / 255.0)
2105 pdf_putnumber(buf, pdf_color_calc(color >> 24));
2107 pdf_putnumber(buf, pdf_color_calc(color >> 16));
2109 pdf_putnumber(buf, pdf_color_calc(color >> 8));
2112 pdf_puts(buf, "\n");
2113 *current_color = color;
2116 static void pdf_set_stroke_color(pdf_buffer *buf, gfx_color_t color)
2118 pdf_set_color(buf, color, &buf->state->stroke_color, "RG");
2121 static void pdf_set_fill_color(pdf_buffer *buf, gfx_color_t color)
2123 pdf_set_color(buf, color, &buf->state->fill_color, "rg");
2126 static pdf_font *pdf_find_font(pdf_state *state, gfx_node_t *node)
2128 const char *ps_font = afm_get_font_postscript_name(node->filename);
2130 for (ef = state->font_list; ef; ef = ef->next) {
2131 if (!strcmp(ps_font, ef->ps_font))
2137 static void pdf_add_font(pdf_state *state, gfx_node_t *node)
2139 pdf_font *ef = pdf_find_font(state, node);
2142 ef = malloc(sizeof(pdf_font));
2144 rrd_set_error("malloc for pdf_font");
2145 state->has_failed = 1;
2148 pdf_init_dict(state, &ef->obj);
2149 ef->next = state->font_list;
2150 ef->ps_font = afm_get_font_postscript_name(node->filename);
2151 state->font_list = ef;
2153 pdf_putsi(&state->fontsdict_obj, "/F");
2154 pdf_putint(&state->fontsdict_obj, ef->obj.id);
2155 pdf_puts(&state->fontsdict_obj, " ");
2156 pdf_putint(&state->fontsdict_obj, ef->obj.id);
2157 pdf_puts(&state->fontsdict_obj, " 0 R\n");
2159 pdf_putsi(&ef->obj, "/Type /Font\n");
2160 pdf_putsi(&ef->obj, "/Subtype /Type1\n");
2161 pdf_putsi(&ef->obj, "/Name /F");
2162 pdf_putint(&ef->obj, ef->obj.id);
2163 pdf_puts(&ef->obj, "\n");
2164 pdf_putsi(&ef->obj, "/BaseFont /");
2165 pdf_puts(&ef->obj, ef->ps_font);
2166 pdf_puts(&ef->obj, "\n");
2167 pdf_putsi(&ef->obj, "/Encoding /WinAnsiEncoding\n");
2168 /* 'Cp1252' (this is latin 1 extended with 27 characters;
2169 the encoding is also known as 'winansi')
2170 http://www.lowagie.com/iText/tutorial/ch09.html */
2173 static void pdf_create_fonts(pdf_state *state)
2176 for (node = state->canvas->firstnode; node; node = node->next) {
2177 if (node->type == GFX_TEXT)
2178 pdf_add_font(state, node);
2182 static void pdf_write_linearea(pdf_state *state, gfx_node_t *node)
2185 pdf_buffer *s = &state->graph_stream;
2186 if (node->type == GFX_LINE) {
2188 svg_get_dash(node, &dash_info);
2189 if (!svg_dash_equal(&dash_info, &state->dash)) {
2190 state->dash = dash_info;
2191 if (dash_info.dash_enable) {
2193 pdf_putnumber(s, dash_info.adjusted_on);
2195 pdf_putnumber(s, dash_info.adjusted_off);
2197 pdf_putnumber(s, dash_info.dash_offset);
2198 pdf_puts(s, " d\n");
2200 pdf_puts(s, "[] 0 d\n");
2203 pdf_set_stroke_color(s, node->color);
2204 if (state->linecap != 1) {
2205 pdf_puts(s, "1 j\n");
2208 if (state->linejoin != 1) {
2209 pdf_puts(s, "1 J\n");
2210 state->linejoin = 1;
2212 if (node->size != state->line_width) {
2213 state->line_width = node->size;
2214 pdf_putnumber(s, state->line_width);
2215 pdf_puts(s, " w\n");
2218 pdf_set_fill_color(s, node->color);
2220 for (i = 0; i < node->points; i++) {
2221 ArtVpath *vec = node->path + i;
2223 double y = state->page_height - vec->y;
2224 if (node->type == GFX_AREA) {
2225 x += LINEOFFSET; /* adjust for libart handling of areas */
2228 switch (vec->code) {
2229 case ART_MOVETO_OPEN: /* fall-through */
2231 pdf_putnumber(s, x);
2233 pdf_putnumber(s, y);
2234 pdf_puts(s, " m\n");
2237 pdf_putnumber(s, x);
2239 pdf_putnumber(s, y);
2240 pdf_puts(s, " l\n");
2242 case ART_CURVETO: break; /* unsupported */
2243 case ART_END: break; /* nop */
2246 if (node->type == GFX_LINE) {
2247 pdf_puts(s, node->closed_path ? "s\n" : "S\n");
2254 static void pdf_write_matrix(pdf_state *state, gfx_node_t *node, pdf_coords *g, int useTM)
2257 pdf_buffer *s = &state->graph_stream;
2258 if (node->angle == 0) {
2259 pdf_puts(s, "1 0 0 1 ");
2260 pdf_putnumber(s, useTM ? g->tmx : g->mx);
2262 pdf_putnumber(s, useTM ? g->tmy : g->my);
2264 /* can't use svg_write_number as 2 decimals is far from enough to avoid
2266 sprintf(tmp, "%f %f %f %f %f %f",
2267 g->ma, g->mb, g->mc, g->md,
2268 useTM ? g->tmx : g->mx,
2269 useTM ? g->tmy : g->my);
2274 static void pdf_write_text(pdf_state *state, gfx_node_t *node,
2275 int last_was_text, int next_is_text)
2278 pdf_buffer *s = &state->graph_stream;
2279 pdf_font *font = pdf_find_font(state, node);
2281 rrd_set_error("font disappeared");
2282 state->has_failed = 1;
2285 pdf_calc(state->page_height, node, &g);
2287 pdf_puts(s, "q % debug green box\n");
2288 pdf_write_matrix(state, node, &g, 0);
2289 pdf_puts(s, " cm\n");
2290 pdf_set_fill_color(s, 0x90FF9000);
2291 pdf_puts(s, "0 0.4 0 rg\n");
2292 pdf_puts(s, "0 0 ");
2293 pdf_putnumber(s, g.sizep.x);
2295 pdf_putnumber(s, g.sizep.y);
2296 pdf_puts(s, " re\n");
2300 pdf_set_fill_color(s, node->color);
2301 if (PDF_CALC_DEBUG || !last_was_text)
2302 pdf_puts(s, "BT\n");
2303 if (state->font_id != font->obj.id || node->size != state->font_size) {
2304 state->font_id = font->obj.id;
2305 state->font_size = node->size;
2307 pdf_putint(s, font->obj.id);
2309 pdf_putnumber(s, node->size);
2310 pdf_puts(s, " Tf\n");
2312 pdf_write_matrix(state, node, &g, 1);
2313 pdf_puts(s, " Tm\n");
2315 pdf_put_string_contents(s, node->text);
2316 pdf_puts(s, ") Tj\n");
2317 if (PDF_CALC_DEBUG || !next_is_text)
2318 pdf_puts(s, "ET\n");
2321 static void pdf_write_content(pdf_state *state)
2324 int last_was_text = 0, next_is_text;
2325 for (node = state->canvas->firstnode; node; node = node->next) {
2326 switch (node->type) {
2329 pdf_write_linearea(state, node);
2332 next_is_text = node->next && node->next->type == GFX_TEXT;
2333 pdf_write_text(state, node, last_was_text, next_is_text);
2336 last_was_text = node->type == GFX_TEXT;
2340 static void pdf_init_document(pdf_state *state)
2342 pdf_init_buffer(state, &state->pdf_header);
2343 pdf_init_dict(state, &state->catalog_obj);
2344 pdf_init_dict(state, &state->info_obj);
2345 pdf_init_dict(state, &state->pages_obj);
2346 pdf_init_dict(state, &state->page1_obj);
2347 pdf_init_dict(state, &state->fontsdict_obj);
2348 pdf_create_fonts(state);
2349 if (state->has_failed)
2351 /* make stream last object in file */
2352 pdf_init_object(state, &state->graph_stream);
2353 state->graph_stream.is_stream = 1;
2356 static void pdf_setup_document(pdf_state *state)
2358 const char *creator = "RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch";
2359 /* all objects created by now, so init code can reference them */
2361 pdf_puts(&state->pdf_header, "%PDF-1.3\n");
2362 /* following 8 bit comment is recommended by Adobe for
2363 indicating binary file to file transfer applications */
2364 pdf_puts(&state->pdf_header, "%\xE2\xE3\xCF\xD3\n");
2366 pdf_putsi(&state->info_obj, "/Creator (");
2367 pdf_put_string_contents(&state->info_obj, creator);
2368 pdf_puts(&state->info_obj, ")\n");
2370 pdf_putsi(&state->catalog_obj, "/Type /Catalog\n");
2371 pdf_putsi(&state->catalog_obj, "/Pages ");
2372 pdf_putint(&state->catalog_obj, state->pages_obj.id);
2373 pdf_puts(&state->catalog_obj, " 0 R\n");
2375 pdf_putsi(&state->pages_obj, "/Type /Pages\n");
2376 pdf_putsi(&state->pages_obj, "/Kids [");
2377 pdf_putint(&state->pages_obj, state->page1_obj.id);
2378 pdf_puts(&state->pages_obj, " 0 R]\n");
2379 pdf_putsi(&state->pages_obj, "/Count 1\n");
2381 pdf_putsi(&state->page1_obj, "/Type /Page\n");
2382 pdf_putsi(&state->page1_obj, "/Parent ");
2383 pdf_putint(&state->page1_obj, state->pages_obj.id);
2384 pdf_puts(&state->page1_obj, " 0 R\n");
2385 pdf_putsi(&state->page1_obj, "/MediaBox [0 0 ");
2386 pdf_putint(&state->page1_obj, state->page_width);
2387 pdf_puts(&state->page1_obj, " ");
2388 pdf_putint(&state->page1_obj, state->page_height);
2389 pdf_puts(&state->page1_obj, "]\n");
2390 pdf_putsi(&state->page1_obj, "/Contents ");
2391 pdf_putint(&state->page1_obj, state->graph_stream.id);
2392 pdf_puts(&state->page1_obj, " 0 R\n");
2393 pdf_putsi(&state->page1_obj, "/Resources << /Font ");
2394 pdf_putint(&state->page1_obj, state->fontsdict_obj.id);
2395 pdf_puts(&state->page1_obj, " 0 R >>\n");
2398 static void pdf_write_string_to_file(pdf_state *state, const char *text)
2400 fputs(text, state->fp);
2401 state->pdf_file_pos += strlen(text);
2404 static void pdf_write_buf_to_file(pdf_state *state, pdf_buffer *buf)
2407 buf->pdf_file_pos = state->pdf_file_pos;
2409 snprintf(tmp, sizeof(tmp), "%d 0 obj\n", buf->id);
2410 pdf_write_string_to_file(state, tmp);
2413 pdf_write_string_to_file(state, "<<\n");
2414 if (buf->is_stream) {
2415 snprintf(tmp, sizeof(tmp), "<< /Length %d >>\n", buf->current_size);
2416 pdf_write_string_to_file(state, tmp);
2417 pdf_write_string_to_file(state, "stream\n");
2419 fwrite(buf->data, 1, buf->current_size, state->fp);
2420 state->pdf_file_pos += buf->current_size;
2422 pdf_write_string_to_file(state, "endstream\n");
2424 pdf_write_string_to_file(state, ">>\n");
2426 pdf_write_string_to_file(state, "endobj\n");
2429 static void pdf_write_to_file(pdf_state *state)
2431 pdf_buffer *buf = state->first_buffer;
2433 state->pdf_file_pos = 0;
2434 pdf_write_buf_to_file(state, &state->pdf_header);
2437 pdf_write_buf_to_file(state, buf);
2438 buf = buf->next_buffer;
2440 xref_pos = state->pdf_file_pos;
2441 fprintf(state->fp, "xref\n");
2442 fprintf(state->fp, "%d %d\n", 0, state->last_obj_id + 1);
2443 /* TOC lines must be exactly 20 bytes including \n */
2444 fprintf(state->fp, "%010d %05d f\x20\n", 0, 65535);
2445 for (buf = state->first_buffer; buf; buf = buf->next_buffer) {
2447 fprintf(state->fp, "%010d %05d n\x20\n", buf->pdf_file_pos, 0);
2449 fprintf(state->fp, "trailer\n");
2450 fprintf(state->fp, "<<\n");
2451 fprintf(state->fp, "\t/Size %d\n", state->last_obj_id + 1);
2452 fprintf(state->fp, "\t/Root %d 0 R\n", state->catalog_obj.id);
2453 fprintf(state->fp, "\t/Info %d 0 R\n", state->info_obj.id);
2454 fprintf(state->fp, ">>\n");
2455 fprintf(state->fp, "startxref\n");
2456 fprintf(state->fp, "%d\n", xref_pos);
2457 fputs("%%EOF\n", state->fp);
2460 static void pdf_free_resources(pdf_state *state)
2462 pdf_buffer *buf = state->first_buffer;
2466 buf->alloc_size = buf->current_size = 0;
2467 buf = buf->next_buffer;
2469 while (state->font_list) {
2470 pdf_font *next = state->font_list->next;
2471 free(state->font_list);
2472 state->font_list = next;
2476 int gfx_render_pdf (gfx_canvas_t *canvas,
2477 art_u32 width, art_u32 height,
2478 gfx_color_t UNUSED(background), FILE *fp){
2479 struct pdf_state state;
2480 memset(&state, 0, sizeof(pdf_state));
2482 state.canvas = canvas;
2483 state.page_width = width;
2484 state.page_height = height;
2486 state.font_size = -1;
2487 state.font_list = NULL;
2489 state.linejoin = -1;
2490 pdf_init_document(&state);
2492 pdf_set_color(&state, background);
2493 fprintf(fp, "0 0 M 0 %d L %d %d L %d 0 L fill\n",
2494 height, width, height, width);
2496 if (!state.has_failed)
2497 pdf_write_content(&state);
2498 if (!state.has_failed)
2499 pdf_setup_document(&state);
2500 if (!state.has_failed)
2501 pdf_write_to_file(&state);
2502 pdf_free_resources(&state);
2503 return state.has_failed ? -1 : 0;