1 /****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
10 # define DPRINT(x) (void)(printf x, printf("\n"))
17 #include FT_FREETYPE_H
21 /* lines are better drawn on the pixle than between pixles */
22 #define LINEOFFSET 0.5
25 gfx_node_t *gfx_new_node( gfx_canvas_t *canvas,enum gfx_en type){
26 gfx_node_t *node = art_new(gfx_node_t,1);
27 if (node == NULL) return NULL;
29 node->color = 0x0; /* color of element 0xRRGGBBAA alpha 0xff is solid*/
30 node->size =0.0; /* font size, line width */
31 node->path = NULL; /* path */
34 node->svp = NULL; /* svp */
35 node->filename = NULL; /* font or image filename */
38 node->y = 0.0; /* position */
39 node->halign = GFX_H_NULL; /* text alignement */
40 node->valign = GFX_V_NULL; /* text alignement */
43 if (canvas->lastnode != NULL){
44 canvas->lastnode->next = node;
46 if (canvas->firstnode == NULL){
47 canvas->firstnode = node;
49 canvas->lastnode = node;
53 gfx_canvas_t *gfx_new_canvas (void) {
54 gfx_canvas_t *canvas = art_new(gfx_canvas_t,1);
55 canvas->firstnode = NULL;
56 canvas->lastnode = NULL;
60 /* create a new line */
61 gfx_node_t *gfx_new_line(gfx_canvas_t *canvas,
64 double width, gfx_color_t color){
68 node = gfx_new_node(canvas,GFX_LINE);
69 if (node == NULL) return NULL;
70 vec = art_new(ArtVpath, 3);
71 if (vec == NULL) return NULL;
72 vec[0].code = ART_MOVETO_OPEN; vec[0].x=x0+LINEOFFSET; vec[0].y=y0+LINEOFFSET;
73 vec[1].code = ART_LINETO; vec[1].x=x1+LINEOFFSET; vec[1].y=y1+LINEOFFSET;
74 vec[2].code = ART_END;
84 /* create a new area */
85 gfx_node_t *gfx_new_area (gfx_canvas_t *canvas,
93 node = gfx_new_node(canvas,GFX_AREA);
94 if (node == NULL) return NULL;
95 vec = art_new(ArtVpath, 5);
96 if (vec == NULL) return NULL;
97 vec[0].code = ART_MOVETO; vec[0].x=x0; vec[0].y=y0;
98 vec[1].code = ART_LINETO; vec[1].x=x1; vec[1].y=y1;
99 vec[2].code = ART_LINETO; vec[2].x=x2; vec[2].y=y2;
100 vec[3].code = ART_LINETO; vec[3].x=x0; vec[3].y=y0;
101 vec[4].code = ART_END;
104 node->points_max = 5;
111 /* add a point to a line or to an area */
112 int gfx_add_point (gfx_node_t *node,
114 if (node == NULL) return 1;
115 if (node->type == GFX_AREA) {
116 double x0 = node->path[0].x;
117 double y0 = node->path[0].y;
119 art_vpath_add_point (&(node->path),
124 art_vpath_add_point (&(node->path),
129 art_vpath_add_point (&(node->path),
134 } else if (node->type == GFX_LINE) {
136 art_vpath_add_point (&(node->path),
140 x+LINEOFFSET,y+LINEOFFSET);
141 art_vpath_add_point (&(node->path),
148 /* can only add point to areas and lines */
156 /* create a text node */
157 gfx_node_t *gfx_new_text (gfx_canvas_t *canvas,
158 double x, double y, gfx_color_t color,
159 char* font, double size,
160 double tabwidth, double angle,
161 enum gfx_h_align_en h_align,
162 enum gfx_v_align_en v_align,
164 gfx_node_t *node = gfx_new_node(canvas,GFX_TEXT);
166 /* currently we only support 0 and 270 */
170 node->text = strdup(text);
172 node->filename = strdup(font);
176 node->tabwidth = tabwidth;
177 node->halign = h_align;
178 node->valign = v_align;
182 double gfx_get_text_width ( double start, char* font, double size,
183 double tabwidth, char* text){
187 FT_UInt glyph_index=0;
191 FT_Library library=NULL;
193 FT_Init_FreeType( &library );
194 error = FT_New_Face( library, font, 0, &face );
195 if ( error ) return -1;
196 error = FT_Set_Char_Size(face, size*64,size*64, 100,100);
197 if ( error ) return -1;
199 use_kerning = FT_HAS_KERNING(face);
202 previous = glyph_index;
203 glyph_index = FT_Get_Char_Index( face, *text);
205 if (use_kerning && previous && glyph_index){
207 FT_Get_Kerning( face, previous, glyph_index,
209 text_width += (double)delta.x / 64.0;
212 error = FT_Load_Glyph( face, glyph_index, 0 );
214 text_width -= (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
216 text_width += (double)slot->metrics.horiAdvance / 64.0;
218 text_width -= (double)slot->metrics.horiAdvance / 64.0; /* remove last step */
219 text_width += (double)slot->metrics.width / 64.0; /* add just char width */
220 text_width += (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
227 static int gfx_save_png (art_u8 *buffer, FILE *fp,
228 long width, long height, long bytes_per_pixel);
229 /* render grafics into png image */
230 int gfx_render_png (gfx_canvas_t *canvas,
231 art_u32 width, art_u32 height,
233 gfx_color_t background, FILE *fp){
237 gfx_node_t *node = canvas->firstnode;
238 art_u8 red = background >> 24, green = (background >> 16) & 0xff;
239 art_u8 blue = (background >> 8) & 0xff, alpha = ( background & 0xff );
240 unsigned long pys_width = width * zoom;
241 unsigned long pys_height = height * zoom;
242 const int bytes_per_pixel = 3;
243 unsigned long rowstride = pys_width*bytes_per_pixel; /* bytes per pixel */
244 art_u8 *buffer = art_new (art_u8, rowstride*pys_height);
245 art_rgb_run_alpha (buffer, red, green, blue, alpha, pys_width*pys_height);
246 FT_Init_FreeType( &library );
248 switch (node->type) {
254 art_affine_scale(dst,zoom,zoom);
255 vec = art_vpath_affine_transform(node->path,dst);
256 if(node->type == GFX_LINE){
257 svp = art_svp_vpath_stroke ( vec, ART_PATH_STROKE_JOIN_ROUND,
258 ART_PATH_STROKE_CAP_ROUND,
259 node->size*zoom,1,1);
261 svp = art_svp_from_vpath ( vec );
264 art_rgb_svp_alpha (svp ,0,0, pys_width, pys_height,
265 node->color, buffer, rowstride, NULL);
271 float text_width=0.0, text_height = 0.0;
273 art_u8 fcolor[3],falpha;
277 FT_UInt glyph_index=0;
280 float pen_x = 0.0 , pen_y = 0.0;
284 fcolor[0] = node->color >> 24;
285 fcolor[1] = (node->color >> 16) & 0xff;
286 fcolor[2] = (node->color >> 8) & 0xff;
287 falpha = node->color & 0xff;
288 error = FT_New_Face( library,
289 (char *)node->filename,
292 use_kerning = FT_HAS_KERNING(face);
295 error = FT_Set_Char_Size(face, /* handle to face object */
296 (long)(node->size*64),
297 (long)(node->size*64),
300 pen_x = node->x * zoom;
301 pen_y = node->y * zoom;
304 for(text=(unsigned char *)node->text;*text;text++) {
305 previous = glyph_index;
306 glyph_index = FT_Get_Char_Index( face, *text);
308 if (use_kerning && previous && glyph_index){
310 FT_Get_Kerning( face, previous, glyph_index,
312 text_width += (double)delta.x / 64.0;
315 error = FT_Load_Glyph( face, glyph_index, 0 );
317 pen_x -= (double)slot->metrics.horiBearingX / 64.0; /* adjust pos for first char */
318 text_width -= (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
320 if ( text_height < (double)slot->metrics.horiBearingY / 64.0 ) {
321 text_height = (double)slot->metrics.horiBearingY / 64.0;
323 text_width += (double)slot->metrics.horiAdvance / 64.0;
325 text_width -= (double)slot->metrics.horiAdvance / 64.0; /* remove last step */
326 text_width += (double)slot->metrics.width / 64.0; /* add just char width */
327 text_width += (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
329 switch(node->halign){
330 case GFX_H_RIGHT: pen_x -= text_width; break;
331 case GFX_H_CENTER: pen_x -= text_width / 2.0; break;
332 case GFX_H_LEFT: break;
335 switch(node->valign){
336 case GFX_V_TOP: pen_y += text_height; break;
337 case GFX_V_CENTER: pen_y += text_height / 2.0; break;
338 case GFX_V_BOTTOM: break;
342 for(text=(unsigned char *)node->text;*text;text++) {
344 previous = glyph_index;
345 glyph_index = FT_Get_Char_Index( face, *text);
347 if (use_kerning && previous && glyph_index){
349 FT_Get_Kerning( face, previous, glyph_index,
351 pen_x += (double)delta.x / 64.0;
354 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER );
355 gr = slot->bitmap.num_grays -1;
356 for (iy=0; iy < slot->bitmap.rows; iy++){
357 long buf_y = iy+(pen_y+0.5)-slot->bitmap_top;
358 if (buf_y < 0 || buf_y >= pys_height) continue;
360 for (ix=0;ix < slot->bitmap.width;ix++){
361 long buf_x = ix + (pen_x + 0.5) + (double)slot->bitmap_left ;
364 if (buf_x < 0 || buf_x >= pys_width) continue;
365 buf_x *= bytes_per_pixel ;
366 font_alpha = *(slot->bitmap.buffer + iy * slot->bitmap.width + ix);
367 font_alpha = (art_u8)((double)font_alpha / gr * falpha);
368 for (iz = 0; iz < 3; iz++){
369 art_u8 *orig = buffer + buf_y + buf_x + iz;
370 *orig = (art_u8)((double)*orig / gr * ( gr - font_alpha) +
371 (double)fcolor[iz] / gr * (font_alpha));
375 pen_x += (double)slot->metrics.horiAdvance / 64.0;
381 gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
386 /* free memory used by nodes this will also remove memory required for
387 associated paths and svcs ... but not for text strings */
389 gfx_destroy (gfx_canvas_t *canvas){
390 gfx_node_t *next,*node = canvas->firstnode;
393 art_free(node->path);
396 free(node->filename);
403 static int gfx_save_png (art_u8 *buffer, FILE *fp, long width, long height, long bytes_per_pixel){
404 png_structp png_ptr = NULL;
405 png_infop info_ptr = NULL;
407 png_bytep *row_pointers;
408 int rowstride = width * bytes_per_pixel;
414 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
419 row_pointers = (png_bytepp)png_malloc(png_ptr,
420 height*sizeof(png_bytep));
422 info_ptr = png_create_info_struct(png_ptr);
424 if (info_ptr == NULL)
426 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
430 if (setjmp(png_jmpbuf(png_ptr)))
432 /* If we get here, we had a problem writing the file */
433 png_destroy_write_struct(&png_ptr, &info_ptr);
437 png_init_io(png_ptr, fp);
438 png_set_IHDR (png_ptr, info_ptr,width, height,
439 8, PNG_COLOR_TYPE_RGB,
441 PNG_COMPRESSION_TYPE_DEFAULT,
442 PNG_FILTER_TYPE_DEFAULT);
444 text[0].key = "Software";
445 text[0].text = "RRDtool, Tobias Oetiker <tobi@oetike.ch>, http://tobi.oetiker.ch";
446 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
447 png_set_text (png_ptr, info_ptr, text, 1);
449 /* Write header data */
450 png_write_info (png_ptr, info_ptr);
452 for (i = 0; i < height; i++)
453 row_pointers[i] = (png_bytep) (buffer + i*rowstride);
455 png_write_image(png_ptr, row_pointers);
456 png_write_end(png_ptr, info_ptr);
457 png_destroy_write_struct(&png_ptr, &info_ptr);