-int gfx_add_point (gfx_node_t *node,
- double x, double y){
- if (node == NULL) return 1;
- if (node->type == GFX_AREA) {
- double X0 = node->path[0].x;
- double Y0 = node->path[0].y;
- node->points -= 2;
- art_vpath_add_point (&(node->path),
- &(node->points),
- &(node->points_max),
- ART_LINETO,
- x,y);
- art_vpath_add_point (&(node->path),
- &(node->points),
- &(node->points_max),
- ART_LINETO,
- X0,Y0);
- art_vpath_add_point (&(node->path),
- &(node->points),
- &(node->points_max),
- ART_END,
- 0,0);
- } else if (node->type == GFX_LINE) {
- node->points -= 1;
- art_vpath_add_point (&(node->path),
- &(node->points),
- &(node->points_max),
- ART_LINETO,
- x+LINEOFFSET,y+LINEOFFSET);
- art_vpath_add_point (&(node->path),
- &(node->points),
- &(node->points_max),
- ART_END,
- 0,0);
-
- } else {
- /* can only add point to areas and lines */
- return 1;
- }
- return 0;
-}
-
-void gfx_close_path (gfx_node_t *node) {
- node->closed_path = 1;
- if (node->path[0].code == ART_MOVETO_OPEN)
- node->path[0].code = ART_MOVETO;
-}
-
-/* create a text node */
-gfx_node_t *gfx_new_text (gfx_canvas_t *canvas,
- double x, double y, gfx_color_t color,
- char* font, double size,
- double tabwidth, double angle,
- enum gfx_h_align_en h_align,
- enum gfx_v_align_en v_align,
- char* text){
- gfx_node_t *node = gfx_new_node(canvas,GFX_TEXT);
-
- node->text = strdup(text);
- node->size = size;
- node->filename = strdup(font);
- node->x = x;
- node->y = y;
- node->angle = angle;
- node->color = color;
- node->tabwidth = tabwidth;
- node->halign = h_align;
- node->valign = v_align;
-#if 0
- /* debugging: show text anchor
- green is along x-axis, red is downward y-axis */
- if (1) {
- double a = 2 * M_PI * -node->angle / 360.0;
- double cos_a = cos(a);
- double sin_a = sin(a);
- double len = 3;
- gfx_new_line(canvas,
- x, y,
- x + len * cos_a, y - len * sin_a,
- 0.2, 0x00FF0000);
- gfx_new_line(canvas,
- x, y,
- x + len * sin_a, y + len * cos_a,
- 0.2, 0xFF000000);
- }
-#endif
- return node;
-}
-
-int gfx_render(gfx_canvas_t *canvas,
- art_u32 width, art_u32 height,
- gfx_color_t background, FILE *fp){
- switch (canvas->imgformat) {
- case IF_PNG:
- return gfx_render_png (canvas, width, height, background, fp);
- case IF_SVG:
- return gfx_render_svg (canvas, width, height, background, fp);
- case IF_EPS:
- return gfx_render_eps (canvas, width, height, background, fp);
- case IF_PDF:
- return gfx_render_pdf (canvas, width, height, background, fp);
- default:
- return -1;
- }
-}
-
-static void gfx_string_destroy ( gfx_string string ) {
- unsigned int n;
- if (string->glyphs) {
- for (n=0; n<string->num_glyphs; ++n)
- FT_Done_Glyph (string->glyphs[n].image);
- free (string->glyphs);
- }
- free (string);
-}
-
-
-double gfx_get_text_width ( gfx_canvas_t *canvas,
- double start, char* font, double size,
- double tabwidth, char* text, int rotation){
- switch (canvas->imgformat) {
- case IF_PNG:
- return gfx_get_text_width_libart (canvas, start, font, size, tabwidth, text, rotation);
- case IF_SVG: /* fall through */
- case IF_EPS:
- case IF_PDF:
- return afm_get_text_width(start, font, size, tabwidth, text);
- default:
- return size * strlen(text);
- }
-}
-
-double gfx_get_text_width_libart (
- gfx_canvas_t *canvas, double UNUSED(start), char* font, double size,
- double tabwidth, char* text, int rotation ){
-
- int error;
- double text_width=0;
- FT_Face face;
- FT_Library library=NULL;
- gfx_string string;
-
- FT_Init_FreeType( &library );
- error = FT_New_Face( library, font, 0, &face );
- if ( error ) return -1;
- error = FT_Set_Char_Size(face, size*64,size*64, 100,100);
- if ( error ) return -1;
-
- string = gfx_string_create( canvas, face, text, rotation, tabwidth, size );
- text_width = string->width;
- gfx_string_destroy(string);
- FT_Done_FreeType(library);
- return text_width/64;
-}
-
-static void gfx_libart_close_path(gfx_node_t *node, ArtVpath **vec)
-{
- /* libart must have end==start for closed paths,
- even if using ART_MOVETO and not ART_MOVETO_OPEN
- so add extra point which is the same as the starting point */
- int points_max = node->points; /* scaled array has exact size */
- int points = node->points - 1;
- art_vpath_add_point (vec, &points, &points_max, ART_LINETO,
- (**vec).x, (**vec).y);
- art_vpath_add_point (vec, &points, &points_max, ART_END, 0, 0);
-}
-
-
-/* find bbox of a string */
-static void compute_string_bbox(gfx_string string) {
- unsigned int n;
- FT_BBox bbox;
-
- bbox.xMin = bbox.yMin = 32000;
- bbox.xMax = bbox.yMax = -32000;
- for ( n = 0; n < string->num_glyphs; n++ ) {
- FT_BBox glyph_bbox;
- FT_Glyph_Get_CBox( string->glyphs[n].image, ft_glyph_bbox_gridfit,
- &glyph_bbox );
- if (glyph_bbox.xMin < bbox.xMin) {
- bbox.xMin = glyph_bbox.xMin;
- }
- if (glyph_bbox.yMin < bbox.yMin) {
- bbox.yMin = glyph_bbox.yMin;
- }
- if (glyph_bbox.xMax > bbox.xMax) {
- bbox.xMax = glyph_bbox.xMax;
- }
- if (glyph_bbox.yMax > bbox.yMax) {
- bbox.yMax = glyph_bbox.yMax;
- }
- }
- if ( bbox.xMin > bbox.xMax ) {
- bbox.xMin = 0;
- bbox.yMin = 0;
- bbox.xMax = 0;
- bbox.yMax = 0;
- }
- string->bbox.xMin = bbox.xMin;
- string->bbox.xMax = bbox.xMax;
- string->bbox.yMin = bbox.yMin;
- string->bbox.yMax = bbox.yMax;
-}
-
-/* create a free type glyph string */
-gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text,
- int rotation, double tabwidth, double size )
-{
-
- FT_GlyphSlot slot = face->glyph; /* a small shortcut */
- FT_Bool use_kerning;
- FT_UInt previous;
- FT_Vector ft_pen;
-
- gfx_string string = (gfx_string) malloc (sizeof(struct gfx_string_s));
-
- gfx_char glyph; /* current glyph in table */
- int n;
- int error;
- int gottab = 0;
-
-#ifdef HAVE_MBSTOWCS
- wchar_t *cstr;
- size_t clen = strlen(text)+1;
- cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
- string->count=mbstowcs(cstr,text,clen);
- if ( string->count == -1){
- /* conversion did not work, so lets fall back to just use what we got */
- string->count=clen-1;
- for(n=0;text[n] != '\0';n++){
- cstr[n]=text[n];
- }
- }
-#else
- char *cstr = strdup(text);
- string->count = strlen (text);
-#endif
-
- ft_pen.x = 0; /* start at (0,0) !! */
- ft_pen.y = 0;
-
-
- string->width = 0;
- string->height = 0;
- string->glyphs = (gfx_char) calloc (string->count,sizeof(struct gfx_char_s));
- string->num_glyphs = 0;
- string->transform.xx = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
- string->transform.xy = (FT_Fixed)(-sin(M_PI*(rotation)/180.0)*0x10000);
- string->transform.yx = (FT_Fixed)( sin(M_PI*(rotation)/180.0)*0x10000);
- string->transform.yy = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
-
- use_kerning = FT_HAS_KERNING(face);
- previous = 0;
- glyph = string->glyphs;
- for (n=0; n<string->count;glyph++,n++) {
- FT_Vector vec;
- /* handle the tabs ...
- have a witespace glyph inserted, but set its width such that the distance
- of the new right edge is x times tabwidth from 0,0 where x is an integer. */
- unsigned int letter = cstr[n];
- letter = afm_fix_osx_charset(letter); /* unsafe macro */
-
- gottab = 0;
- if (letter == '\\' && n+1 < string->count && cstr[n+1] == 't'){
- /* we have a tab here so skip the backslash and
- set t to ' ' so that we get a white space */
- gottab = 1;
- n++;
- letter = ' ';
- }
- if (letter == '\t'){
- letter = ' ';
- gottab = 1 ;
- }
- /* initialize each struct gfx_char_s */
- glyph->index = 0;
- glyph->pos.x = 0;
- glyph->pos.y = 0;
- glyph->image = NULL;
- glyph->index = FT_Get_Char_Index( face, letter );
-
- /* compute glyph origin */
- if ( use_kerning && previous && glyph->index ) {
- FT_Vector kerning;
- FT_Get_Kerning (face, previous, glyph->index,
- ft_kerning_default, &kerning);
- ft_pen.x += kerning.x;
- ft_pen.y += kerning.y;
- }
-
- /* load the glyph image (in its native format) */
- /* for now, we take a monochrome glyph bitmap */
- error = FT_Load_Glyph (face, glyph->index, size > canvas->font_aa_threshold ?
- canvas->aa_type == AA_NORMAL ? FT_LOAD_TARGET_NORMAL :
- canvas->aa_type == AA_LIGHT ? FT_LOAD_TARGET_LIGHT :
- FT_LOAD_TARGET_MONO : FT_LOAD_TARGET_MONO);
- if (error) {
- DPRINTF("couldn't load glyph: %c\n", letter)
- continue;
- }
- error = FT_Get_Glyph (slot, &glyph->image);
- if (error) {
- DPRINTF("couldn't get glyph %c from slot %d\n", letter, (int)slot)
- continue;
- }
- /* if we are in tabbing mode, we replace the tab with a space and shift the position
- of the space so that its left edge is where the tab was supposed to land us */
- if (gottab){
- /* we are in gridfitting mode so the calculations happen in 1/64 pixles */
- ft_pen.x = tabwidth*64.0 * (float)(1 + (long)(ft_pen.x / (tabwidth * 64.0))) - slot->advance.x;
- }
- /* store current pen position */
- glyph->pos.x = ft_pen.x;
- glyph->pos.y = ft_pen.y;
-
-
- ft_pen.x += slot->advance.x;
- ft_pen.y += slot->advance.y;
-
- /* rotate glyph */
- vec = glyph->pos;
- FT_Vector_Transform (&vec, &string->transform);
- error = FT_Glyph_Transform (glyph->image, &string->transform, &vec);
- if (error) {
- DPRINTF("couldn't transform glyph id %d\n", letter)
- continue;
- }
-
- /* convert to a bitmap - destroy native image */
- error = FT_Glyph_To_Bitmap (&glyph->image, size > canvas->font_aa_threshold ?
- canvas->aa_type == AA_NORMAL ? FT_RENDER_MODE_NORMAL :
- canvas->aa_type == AA_LIGHT ? FT_RENDER_MODE_LIGHT :
- FT_RENDER_MODE_MONO : FT_RENDER_MODE_MONO, 0, 1);
- if (error) {
- DPRINTF("couldn't convert glyph id %d to bitmap\n", letter)
- continue;
- }
-
- /* increment number of glyphs */
- previous = glyph->index;
- string->num_glyphs++;
- }
- free(cstr);
-/* printf ("number of glyphs = %d\n", string->num_glyphs);*/
- compute_string_bbox( string );
- /* the last character was a tab */
- /* if (gottab) { */
- string->width = ft_pen.x;
- /* } else {
- string->width = string->bbox.xMax - string->bbox.xMin;
- } */
- string->height = string->bbox.yMax - string->bbox.yMin;
- return string;
-}
-
-
-static int gfx_save_png (art_u8 *buffer, FILE *fp,
- long width, long height, long bytes_per_pixel);
-/* render grafics into png image */
-
-int gfx_render_png (gfx_canvas_t *canvas,
- art_u32 width, art_u32 height,
- gfx_color_t background, FILE *fp){
-
-
- FT_Library library;
- gfx_node_t *node = canvas->firstnode;
- /*
- art_u8 red = background >> 24, green = (background >> 16) & 0xff;
- art_u8 blue = (background >> 8) & 0xff, alpha = ( background & 0xff );
- */
- unsigned long pys_width = width * canvas->zoom;
- unsigned long pys_height = height * canvas->zoom;
- const int bytes_per_pixel = 4;
- unsigned long rowstride = pys_width*bytes_per_pixel; /* bytes per pixel */
-
- /* fill that buffer with out background color */
- gfx_color_t *buffp = art_new (gfx_color_t, pys_width*pys_height);
- art_u8 *buffer = (art_u8 *)buffp;
- unsigned long i;
- for (i=0;i<pys_width*pys_height;
- i++){
- *(buffp++)=background;
- }
- FT_Init_FreeType( &library );
- while(node){
- switch (node->type) {
- case GFX_LINE:
- case GFX_AREA: {
- ArtVpath *vec;
- double dst[6];
- ArtSVP *svp;
- art_affine_scale(dst,canvas->zoom,canvas->zoom);
- vec = art_vpath_affine_transform(node->path,dst);
- if (node->closed_path)
- gfx_libart_close_path(node, &vec);
- /* gfx_round_scaled_coordinates(vec); */
- /* pvec = art_vpath_perturb(vec);
- art_free(vec); */
- if(node->type == GFX_LINE){
- svp = art_svp_vpath_stroke ( vec, ART_PATH_STROKE_JOIN_ROUND,
- ART_PATH_STROKE_CAP_ROUND,
- node->size*canvas->zoom,4,0.25);
- } else {
- svp = art_svp_from_vpath ( vec );
- /* this takes time and is unnecessary since we make
- sure elsewhere that the areas are going clock-whise */
- /* svpt = art_svp_uncross( svp );
- art_svp_free(svp);
- svp = art_svp_rewind_uncrossed(svpt,ART_WIND_RULE_NONZERO);
- art_svp_free(svpt);
- */
- }
- art_free(vec);
- /* this is from gnome since libart does not have this yet */
- gnome_print_art_rgba_svp_alpha (svp ,0,0, pys_width, pys_height,
- node->color, buffer, rowstride, NULL);
- art_svp_free(svp);
- break;
- }
- case GFX_TEXT: {
- unsigned int n;
- int error;
- art_u8 fcolor[4],falpha;
- FT_Face face;
- gfx_char glyph;
- gfx_string string;
- FT_Vector vec; /* 26.6 */
-
- float pen_x = 0.0 , pen_y = 0.0;
- /* double x,y; */
- long ix,iy;
-
- fcolor[0] = node->color >> 24;
- fcolor[1] = (node->color >> 16) & 0xff;
- fcolor[2] = (node->color >> 8) & 0xff;
- falpha = node->color & 0xff;
- error = FT_New_Face( library,
- (char *)node->filename,
- 0,
- &face );
- if ( error ) {
- rrd_set_error("failed to load %s",node->filename);
- break;
- }
- error = FT_Set_Char_Size(face, /* handle to face object */
- (long)(node->size*64),
- (long)(node->size*64),
- (long)(100*canvas->zoom),
- (long)(100*canvas->zoom));
- if ( error ) break;
- pen_x = node->x * canvas->zoom;
- pen_y = node->y * canvas->zoom;
-
- string = gfx_string_create (canvas, face, node->text, node->angle, node->tabwidth, node->size);
- switch(node->halign){
- case GFX_H_RIGHT: vec.x = -string->bbox.xMax;
- break;
- case GFX_H_CENTER: vec.x = abs(string->bbox.xMax) >= abs(string->bbox.xMin) ?
- -string->bbox.xMax/2:-string->bbox.xMin/2;
- break;
- case GFX_H_LEFT: vec.x = -string->bbox.xMin;
- break;
- case GFX_H_NULL: vec.x = 0;
- break;
- }
-
- switch(node->valign){
- case GFX_V_TOP: vec.y = string->bbox.yMax;
- break;
- case GFX_V_CENTER: vec.y = abs(string->bbox.yMax) >= abs(string->bbox.yMin) ?
- string->bbox.yMax/2:string->bbox.yMin/2;
- break;
- case GFX_V_BOTTOM: vec.y = 0;
- break;
- case GFX_V_NULL: vec.y = 0;
- break;
- }
- pen_x += vec.x/64;
- pen_y += vec.y/64;
- glyph = string->glyphs;
- for(n=0; n<string->num_glyphs; n++, glyph++) {
- int gr;
- FT_Glyph image;
- FT_BitmapGlyph bit;
- /* long buf_x,comp_n; */
- /* make copy to transform */
- if (! glyph->image) {
- DPRINTF("no image\n")
- continue;
- }
- error = FT_Glyph_Copy (glyph->image, &image);
- if (error) {
- DPRINTF("couldn't copy image\n")
- continue;
- }
-
- /* transform it */
- vec = glyph->pos;
- FT_Vector_Transform (&vec, &string->transform);
-
- bit = (FT_BitmapGlyph) image;
- gr = bit->bitmap.num_grays -1;
-/*
- buf_x = (pen_x + 0.5) + (double)bit->left;
- comp_n = buf_x + bit->bitmap.width > pys_width ? pys_width - buf_x : bit->bitmap.width;
- if (buf_x < 0 || buf_x >= (long)pys_width) continue;
- buf_x *= bytes_per_pixel ;
- for (iy=0; iy < bit->bitmap.rows; iy++){
- long buf_y = iy+(pen_y+0.5)-(double)bit->top;
- if (buf_y < 0 || buf_y >= (long)pys_height) continue;
- buf_y *= rowstride;
- for (ix=0;ix < bit->bitmap.width;ix++){
- *(letter + (ix*bytes_per_pixel+3)) = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
- }
- art_rgba_rgba_composite(buffer + buf_y + buf_x ,letter,comp_n);
- }
- art_free(letter);
-*/
- switch ( bit->bitmap.pixel_mode ) {
- case FT_PIXEL_MODE_GRAY:
- for (iy=0; iy < bit->bitmap.rows; iy++){
- long buf_y = iy+(pen_y+0.5)-bit->top;
- if (buf_y < 0 || buf_y >= (long)pys_height) continue;
- buf_y *= rowstride;
- for (ix=0;ix < bit->bitmap.width;ix++){
- long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
- art_u8 font_alpha;
-
- if (buf_x < 0 || buf_x >= (long)pys_width) continue;
- buf_x *= bytes_per_pixel ;
- font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix);
- if (font_alpha > 0){
- fcolor[3] = (art_u8)((double)font_alpha / gr * falpha);
- art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
- }
- }
- }
- break;
-
- case FT_PIXEL_MODE_MONO:
- for (iy=0; iy < bit->bitmap.rows; iy++){
- long buf_y = iy+(pen_y+0.5)-bit->top;
- if (buf_y < 0 || buf_y >= (long)pys_height) continue;
- buf_y *= rowstride;
- for (ix=0;ix < bit->bitmap.width;ix++){
- long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
-
- if (buf_x < 0 || buf_x >= (long)pys_width) continue;
- buf_x *= bytes_per_pixel ;
- if ( (fcolor[3] = falpha * ((*(bit->bitmap.buffer + iy * bit->bitmap.pitch + ix/8) >> (7 - (ix % 8))) & 1)) > 0 )
- art_rgba_rgba_composite(buffer + buf_y + buf_x ,fcolor,1);
- }
- }
- break;
-
- default:
- rrd_set_error("unknown freetype pixel mode: %d", bit->bitmap.pixel_mode);
- break;
- }
-
-/*
- for (iy=0; iy < bit->bitmap.rows; iy++){
- long buf_y = iy+(pen_y+0.5)-bit->top;
- if (buf_y < 0 || buf_y >= (long)pys_height) continue;
- buf_y *= rowstride;
- for (ix=0;ix < bit->bitmap.width;ix++){
- long buf_x = ix + (pen_x + 0.5) + (double)bit->left ;
- art_u8 font_alpha;
-
- if (buf_x < 0 || buf_x >= (long)pys_width) continue;
- buf_x *= bytes_per_pixel ;
- font_alpha = *(bit->bitmap.buffer + iy * bit->bitmap.width + ix);
- font_alpha = (art_u8)((double)font_alpha / gr * falpha);
- for (iz = 0; iz < 3; iz++){
- art_u8 *orig = buffer + buf_y + buf_x + iz;
- *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
- (double)fcolor[iz] / gr * (font_alpha));
- }
- }
- }
-*/
- FT_Done_Glyph (image);
- }
- gfx_string_destroy(string);
- }
- }
- node = node->next;
- }
- gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
- art_free(buffer);
- FT_Done_FreeType( library );
- return 0;
-}
-
-/* free memory used by nodes this will also remove memory required for
- associated paths and svcs ... but not for text strings */
-int
-gfx_destroy (gfx_canvas_t *canvas){
- gfx_node_t *next,*node = canvas->firstnode;
- while(node){
- next = node->next;
- art_free(node->path);
- free(node->text);
- free(node->filename);
- art_free(node);
- node = next;
- }
- art_free(canvas);
- return 0;
-}
-
-static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
- png_structp png_ptr = NULL;
- png_infop info_ptr = NULL;
- int i;
- png_bytep *row_pointers;
- int rowstride = width * bytes_per_pixel;
- png_text text[2];
-
- if (fp == NULL)
- return (1);
-
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
- if (png_ptr == NULL)
- {
- return (1);
- }
- row_pointers = (png_bytepp)png_malloc(png_ptr,
- height*sizeof(png_bytep));
-
- info_ptr = png_create_info_struct(png_ptr);
-
- if (info_ptr == NULL)
- {
- png_free(png_ptr,row_pointers);
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- return (1);
- }
-
- if (setjmp(png_jmpbuf(png_ptr)))
- {
- /* If we get here, we had a problem writing the file */
- png_destroy_write_struct(&png_ptr, &info_ptr);
- return (1);
- }
-
- png_init_io(png_ptr, fp);
- png_set_IHDR (png_ptr, info_ptr,width, height,
- 8, PNG_COLOR_TYPE_RGB_ALPHA,
- PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_DEFAULT,
- PNG_FILTER_TYPE_DEFAULT);
-
- text[0].key = "Software";
- text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
- text[0].compression = PNG_TEXT_COMPRESSION_NONE;
- png_set_text (png_ptr, info_ptr, text, 1);
-
- /* lets make this fast while ending up with some increass in image size */
- png_set_filter(png_ptr,0,PNG_FILTER_NONE);
- /* png_set_filter(png_ptr,0,PNG_FILTER_SUB); */
- png_set_compression_level(png_ptr,1);
- /* png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY); */
- /*
- png_set_filter(png_ptr,PNG_FILTER_TYPE_BASE,PNG_FILTER_SUB);
- png_set_compression_strategy(png_ptr,Z_HUFFMAN_ONLY);
- png_set_compression_level(png_ptr,Z_BEST_SPEED); */
-
- /* Write header data */
- png_write_info (png_ptr, info_ptr);
- for (i = 0; i < height; i++)
- row_pointers[i] = (png_bytep) (buffer + i*rowstride);
-
- png_write_image(png_ptr, row_pointers);
- png_write_end(png_ptr, info_ptr);
- png_free(png_ptr,row_pointers);
- png_destroy_write_struct(&png_ptr, &info_ptr);
- return 1;
-}
-
-
-/* ----- COMMON ROUTINES for pdf, svg and eps */
-#define min3(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
-#define max3(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
-
-#define PDF_CALC_DEBUG 0
-
-typedef struct pdf_point
-{
- double x, y;
-} pdf_point;
-
-typedef struct
-{
- double ascender, descender, baselineY;
- pdf_point sizep, minp, maxp;
- double x, y, tdx, tdy;
- double r, cos_r, sin_r;
- double ma, mb, mc, md, mx, my; /* pdf coord matrix */
- double tmx, tmy; /* last 2 coords of text coord matrix */
-#if PDF_CALC_DEBUG
- int debug;
-#endif
-} pdf_coords;
-
-#if PDF_CALC_DEBUG
-static void pdf_dump_calc(gfx_node_t *node, pdf_coords *g)
-{
- fprintf(stderr, "PDF CALC =============================\n");
- fprintf(stderr, " '%s' at %f pt\n", node->text, node->size);
- fprintf(stderr, " align h = %s, v = %s, sizep = %f, %f\n",
- (node->halign == GFX_H_RIGHT ? "r" :
- (node->halign == GFX_H_CENTER ? "c" :
- (node->halign == GFX_H_LEFT ? "l" : "N"))),
- (node->valign == GFX_V_TOP ? "t" :
- (node->valign == GFX_V_CENTER ? "c" :
- (node->valign == GFX_V_BOTTOM ? "b" : "N"))),
- g->sizep.x, g->sizep.y);
- fprintf(stderr, " r = %f = %f, cos = %f, sin = %f\n",
- g->r, node->angle, g->cos_r, g->sin_r);
- fprintf(stderr, " ascender = %f, descender = %f, baselineY = %f\n",
- g->ascender, g->descender, g->baselineY);
- fprintf(stderr, " sizep: %f, %f\n", g->sizep.x, g->sizep.y);
- fprintf(stderr, " minp: %f, %f maxp = %f, %f\n",
- g->minp.x, g->minp.y, g->maxp.x, g->maxp.y);
- fprintf(stderr, " x = %f, y = %f\n", g->x, g->y);
- fprintf(stderr, " tdx = %f, tdy = %f\n", g->tdx, g->tdy);
- fprintf(stderr, " GM = %f, %f, %f, %f, %f, %f\n",
- g->ma, g->mb, g->mc, g->md, g->mx, g->my);
- fprintf(stderr, " TM = %f, %f, %f, %f, %f, %f\n",
- g->ma, g->mb, g->mc, g->md, g->tmx, g->tmy);
-}
-#endif
-
-#if PDF_CALC_DEBUG
-#define PDF_DD(x) if (g->debug) x;
-#else
-#define PDF_DD(x)
-#endif
-
-static void pdf_rotate(pdf_coords *g, pdf_point *p)
-{
- double x2 = g->cos_r * p->x - g->sin_r * p->y;
- double y2 = g->sin_r * p->x + g->cos_r * p->y;
- PDF_DD( fprintf(stderr, " rotate(%f, %f) -> %f, %f\n", p->x, p->y, x2, y2))
- p->x = x2;
- p->y = y2;
-}
-
-
-static void pdf_calc(int page_height, gfx_node_t *node, pdf_coords *g)
-{
- pdf_point a, b, c;
-#if PDF_CALC_DEBUG
- /* g->debug = !!strstr(node->text, "RevProxy-1") || !!strstr(node->text, "08:00"); */
- g->debug = !!strstr(node->text, "sekunder") || !!strstr(node->text, "Web");
-#endif
- g->x = node->x;
- g->y = page_height - node->y;
- if (node->angle) {
- g->r = 2 * M_PI * node->angle / 360.0;
- g->cos_r = cos(g->r);
- g->sin_r = sin(g->r);
- } else {
- g->r = 0;
- g->cos_r = 1;
- g->sin_r = 0;
- }
- g->ascender = afm_get_ascender(node->filename, node->size);
- g->descender = afm_get_descender(node->filename, node->size);
- g->sizep.x = afm_get_text_width(0, node->filename, node->size, node->tabwidth, node->text);
- /* seems like libart ignores the descender when doing vertial-align = bottom,
- so we do that too, to get labels v-aligning properly */
- g->sizep.y = -g->ascender; /* + afm_get_descender(font->ps_font, node->size); */
- g->baselineY = -g->ascender - g->sizep.y / 2;
- a.x = g->sizep.x; a.y = g->sizep.y;
- b.x = g->sizep.x; b.y = 0;
- c.x = 0; c.y = g->sizep.y;
- if (node->angle) {
- pdf_rotate(g, &a);
- pdf_rotate(g, &b);
- pdf_rotate(g, &c);
- }
- g->minp.x = min3(a.x, b.x, c.x);
- g->minp.y = min3(a.y, b.y, c.y);
- g->maxp.x = max3(a.x, b.x, c.x);
- g->maxp.y = max3(a.y, b.y, c.y);
- /* The alignment parameters in node->valign and node->halign
- specifies the alignment in the non-rotated coordinate system
- (very unlike pdf/postscript), which complicates matters.
- */
- switch (node->halign) {
- case GFX_H_RIGHT: g->tdx = -g->maxp.x; break;
- case GFX_H_CENTER: g->tdx = -(g->maxp.x + g->minp.x) / 2; break;
- case GFX_H_LEFT: g->tdx = -g->minp.x; break;
- case GFX_H_NULL: g->tdx = 0; break;
- }
- switch(node->valign){
- case GFX_V_TOP: g->tdy = -g->maxp.y; break;
- case GFX_V_CENTER: g->tdy = -(g->maxp.y + g->minp.y) / 2; break;
- case GFX_V_BOTTOM: g->tdy = -g->minp.y; break;
- case GFX_V_NULL: g->tdy = 0; break;
- }
- g->ma = g->cos_r;
- g->mb = g->sin_r;
- g->mc = -g->sin_r;
- g->md = g->cos_r;
- g->mx = g->x + g->tdx;
- g->my = g->y + g->tdy;
- g->tmx = g->mx - g->ascender * g->mc;
- g->tmy = g->my - g->ascender * g->md;
- PDF_DD(pdf_dump_calc(node, g))
-}
-
-/* ------- SVG -------
- SVG reference:
- http://www.w3.org/TR/SVG/
-*/
-static int svg_indent = 0;
-static int svg_single_line = 0;
-static const char *svg_default_font = "-dummy-";
-typedef struct svg_dash
-{
- int dash_enable;
- double dash_adjust, dash_len, dash_offset;
- double adjusted_on, adjusted_off;
-} svg_dash;
-
-
-static void svg_print_indent(FILE *fp)
-{
- int i;
- for (i = svg_indent - svg_single_line; i > 0; i--) {
- putc(' ', fp);
- putc(' ', fp);
- }
-}
-
-static void svg_start_tag(FILE *fp, const char *name)
-{
- svg_print_indent(fp);
- putc('<', fp);
- fputs(name, fp);
- svg_indent++;
-}
-
-static void svg_close_tag_single_line(FILE *fp)
-{
- svg_single_line++;
- putc('>', fp);
-}
-
-static void svg_close_tag(FILE *fp)
-{
- putc('>', fp);
- if (!svg_single_line)
- putc('\n', fp);
-}
-
-static void svg_end_tag(FILE *fp, const char *name)
-{
- /* name is NULL if closing empty-node tag */
- svg_indent--;
- if (svg_single_line)
- svg_single_line--;
- else if (name)
- svg_print_indent(fp);
- if (name != NULL) {
- fputs("</", fp);
- fputs(name, fp);
- } else {
- putc('/', fp);
- }
- svg_close_tag(fp);
-}
-
-static void svg_close_tag_empty_node(FILE *fp)
-{
- svg_end_tag(fp, NULL);
-}
-
-static void svg_write_text(FILE *fp, const char *text)
-{
-#ifdef HAVE_MBSTOWCS
- size_t clen;
- wchar_t *p, *cstr, ch;
- int text_count;
- if (!text)
- return;
- clen = strlen(text) + 1;
- cstr = malloc(sizeof(wchar_t) * clen);
- text_count = mbstowcs(cstr, text, clen);
- if (text_count == -1)
- text_count = mbstowcs(cstr, "Enc-Err", 6);
- p = cstr;
-#else
- const unsigned char *p = text, ch;
- if (!p)
- return;
-#endif
- while (1) {
- ch = *p++;
- ch = afm_fix_osx_charset(ch); /* unsafe macro */
- switch (ch) {
- case 0:
-#ifdef HAVE_MBSTOWCS
- free(cstr);
-#endif
- return;
- case '&': fputs("&", fp); break;
- case '<': fputs("<", fp); break;
- case '>': fputs(">", fp); break;
- case '"': fputs(""", fp); break;
- default:
- if (ch == 32) {
- if (p <= cstr + 1 || !*p || *p == 32)
- fputs(" ", fp); /* non-breaking space in unicode */
- else
- fputc(32, fp);
- } else if (ch < 32 || ch >= 127)
- fprintf(fp, "&#%d;", (int)ch);
- else
- putc((char)ch, fp);
- }
- }
-}
-
-static void svg_format_number(char *buf, int bufsize, double d)
-{
- /* omit decimals if integer to reduce filesize */
- char *p;
- snprintf(buf, bufsize, "%.2f", d);
- p = buf; /* doesn't trust snprintf return value */
- while (*p)
- p++;
- while (--p > buf) {
- char ch = *p;
- if (ch == '0') {
- *p = '\0'; /* zap trailing zeros */
- continue;
- }
- if (ch == '.')
- *p = '\0'; /* zap trailing dot */
- break;
- }
-}
-
-static void svg_write_number(FILE *fp, double d)
-{
- char buf[60];
- svg_format_number(buf, sizeof(buf), d);
- fputs(buf, fp);
-}
-
-static int svg_color_is_black(int c)
-{
- /* gfx_color_t is RRGGBBAA */
- return c == 0x000000FF;
-}
-
-static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
-{
- /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
- gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
- gfx_color_t opacity = c & 0xFF;
- fprintf(fp, " %s=\"", attr);
- if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
- /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
- fprintf(fp, "#%03lX",
- ( ((rrggbb >> 8) & 0xF00)
- | ((rrggbb >> 4) & 0x0F0)
- | ( rrggbb & 0x00F)));
- } else {
- fprintf(fp, "#%06lX", rrggbb);
- }
- fputs("\"", fp);
- if (opacity != 0xFF) {
- fprintf(fp, " opacity=\"");
- svg_write_number(fp, opacity / 255.0);
- fputs("\"", fp);
- }
-}
-
-static void svg_get_dash(gfx_node_t *node, svg_dash *d)
-{
- double offset;
- int mult;
- if (node->dash_on <= 0 || node->dash_off <= 0) {
- d->dash_enable = 0;
- return;
- }
- d->dash_enable = 1;
- d->dash_len = node->dash_on + node->dash_off;
- /* dash on/off adjustment due to round caps */
- d->dash_adjust = 0.8 * node->size;
- d->adjusted_on = node->dash_on - d->dash_adjust;
- if (d->adjusted_on < 0.01)
- d->adjusted_on = 0.01;
- d->adjusted_off = d->dash_len - d->adjusted_on;
- /* dash offset calc */
- if (node->path[0].x == node->path[1].x) /* only good for horz/vert lines */
- offset = node->path[0].y;
- else
- offset = node->path[0].x;
- mult = (int)fabs(offset / d->dash_len);
- d->dash_offset = offset - mult * d->dash_len;
- if (node->path[0].x < node->path[1].x || node->path[0].y < node->path[1].y)
- d->dash_offset = d->dash_len - d->dash_offset;
-}
-
-static int svg_dash_equal(svg_dash *a, svg_dash *b)
-{
- if (a->dash_enable != b->dash_enable)
- return 0;
- if (a->adjusted_on != b->adjusted_on)
- return 0;
- if (a->adjusted_off != b->adjusted_off)
- return 0;
- /* rest of properties will be the same when on+off are */
- return 1;
-}
-
-static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
-{
- svg_dash dash_info;
- svg_get_dash(node, &dash_info);
- fputs(" stroke-width=\"", fp);
- svg_write_number(fp, node->size);
- fputs("\"", fp);
- svg_write_color(fp, node->color, "stroke");
- fputs(" fill=\"none\"", fp);
- if (dash_info.dash_enable) {
- if (dash_info.dash_offset != 0) {
- fputs(" stroke-dashoffset=\"", fp);
- svg_write_number(fp, dash_info.dash_offset);
- fputs("\"", fp);
- }
- fputs(" stroke-dasharray=\"", fp);
- svg_write_number(fp, dash_info.adjusted_on);
- fputs(",", fp);
- svg_write_number(fp, dash_info.adjusted_off);
- fputs("\"", fp);
- }
-}
-
-static int svg_is_int_step(double a, double b)
-{
- double diff = fabs(a - b);
- return floor(diff) == diff;
-}
-
-static int svg_path_straight_segment(FILE *fp,
- double lastA, double currentA, double currentB,
- gfx_node_t *node,
- int segment_idx, int isx, char absChar, char relChar)