From d4e17c471ffa3ef23d67086bd6378f39c9067362 Mon Sep 17 00:00:00 2001 From: arif Date: Mon, 3 Nov 2008 20:40:08 +0000 Subject: [PATCH] font / selection / few memory leaks fix --- cmd/smyrna/draw.c | 159 ++++++++++++++++++++++++++++---------- cmd/smyrna/draw.h | 1 + cmd/smyrna/glexpose.c | 2 +- cmd/smyrna/gltemplate.c | 3 +- cmd/smyrna/selection.c | 8 +- cmd/smyrna/viewport.c | 67 +++++++++++----- lib/glcomp/glTexFontTGA.c | 5 -- lib/glcomp/glpangofont.c | 2 +- 8 files changed, 176 insertions(+), 71 deletions(-) diff --git a/cmd/smyrna/draw.c b/cmd/smyrna/draw.c index ae7de9592..6bf6406ac 100755 --- a/cmd/smyrna/draw.c +++ b/cmd/smyrna/draw.c @@ -439,8 +439,9 @@ static void SetStyle(xdot_op * op, int param) static void SetFont(xdot_op * op, int param) { -// view->FontName=ABSet(op->u.font.name); - view->FontSize = (int) op->u.font.size; + //activate the right font + view->fontset->activefont=add_font(view->fontset,op->u.font.name);//load or set active font + view->FontSize = (int) op->u.font.size; } static void InsertImage(xdot_op * op, int param) @@ -465,8 +466,7 @@ static void EmbedText(xdot_op * op, int param) if (param == 1) //selected fontColor(view->fontset->fonts[view->fontset->activefont],view->selectedNodeColor.R, view->selectedNodeColor.G, view->selectedNodeColor.B); - - fontDrawString(view->fontset->fonts[view->fontset->activefont],(int) (x - dx), op->u.text.y - (int) dy, + fontDrawString(view->fontset->fonts[view->fontset->activefont],(int) (x - dx), op->u.text.y - (int) dy, op->u.text.text, op->u.text.width); } @@ -627,22 +627,26 @@ static void drawXdot(xdot * xDot, int param, void *p) sdot_op *ops = (sdot_op *) (xDot->ops); sdot_op *op; - for (id = 0; id < xDot->cnt; id++) { - op = ops + id; - op->obj = p; - op->op.drawfunc(&(op->op), param); + for (id = 0; id < xDot->cnt; id++) + { + op = ops + id; + op->obj = p; + op->op.drawfunc(&(op->op), param); } if (OD_Preselected(p) == 1) - select_object(view->g[view->activeGraph], p); + select_object(view->g[view->activeGraph], p); OD_Preselected(p) = 0; } + + static void drawXdotwithattr(void *p, char *attr, int param) { xdot *xDot; - if ((xDot = parseXDotF(agget(p, attr), OpFns, sizeof(sdot_op)))) { - drawXdot(xDot, param, p); - freeXDot(xDot); + if ((xDot = parseXDotF(agget(p, attr), OpFns, sizeof(sdot_op)))) + { + drawXdot(xDot, param, p); + freeXDot(xDot); } } @@ -656,6 +660,8 @@ static void drawXdotwithattrs(void *e, int param) drawXdotwithattr(e, "_tldraw_", param); } + + void drawGraph(Agraph_t * g) { Agnode_t *v; @@ -663,44 +669,113 @@ void drawGraph(Agraph_t * g) Agraph_t *s; int param = 0; - for (s = agfstsubg(g); s; s = agnxtsubg(s)) { - OD_SelFlag(s) = 0; - if (OD_Selected(s) == 1) - param = 1; - else - param = 0; - drawXdotwithattrs(s, param); + for (s = agfstsubg(g); s; s = agnxtsubg(s)) + { + OD_SelFlag(s) = 0; + if (OD_Selected(s) == 1) + param = 1; + else + param = 0; + drawXdotwithattrs(s, param); } - for (v = agfstnode(g); v; v = agnxtnode(g, v)) { - if (OD_Selected(v) == 1) - param = 1; - else - param = 0; - OD_SelFlag(v) = 0; - drawXdotwithattr(v, "_draw_", param); - drawXdotwithattr(v, "_ldraw_", param); - for (e = agfstout(g, v); e; e = agnxtout(g, e)) { - OD_SelFlag(e) = 0; - if (OD_Selected(e) == 1) - param = 1; - else - param = 0; - - drawXdotwithattrs(e, param); + for (v = agfstnode(g); v; v = agnxtnode(g, v)) + { + if (OD_Selected(v) == 1) + param = 1; + else + param = 0; + OD_SelFlag(v) = 0; + drawXdotwithattr(v, "_draw_", param); //draw primitives + drawXdotwithattr(v, "_ldraw_", param);//label drawing + for (e = agfstout(g, v); e; e = agnxtout(g, e)) + { + OD_SelFlag(e) = 0; + if (OD_Selected(e) == 1) + param = 1; + else + param = 0; + drawXdotwithattrs(e, param); + } + } + if ((view->Selection.Active > 0) && (!view->SignalBlock)) + { + view->Selection.Active = 0; + drawGraph(g); + view->SignalBlock = 1; + glexpose(); + view->SignalBlock = 0; + } - } +} + + +/* + this function is used to cache fonts in view->fontset +*/ + +static void scanXdot(xdot * xDot, void *p) +{ + int id; + sdot_op *ops = (sdot_op *) (xDot->ops); + sdot_op *op; + + for (id = 0; id < xDot->cnt; id++) + { + op = ops + id; + op->obj = p; + if (op->op.kind==xd_font) + { + add_font(view->fontset,op->op.u.font.name);//load or set active font + } } - if ((view->Selection.Active > 0) && (!view->SignalBlock)) { - view->Selection.Active = 0; - drawGraph(g); - view->SignalBlock = 1; - glexpose(); - view->SignalBlock = 0; + +} + + +static void scanXdotwithattr(void *p, char *attr) +{ + xdot *xDot; + if ((xDot = parseXDotF(agget(p, attr), OpFns, sizeof(sdot_op)))) + { + scanXdot(xDot, p); + freeXDot(xDot); } +} +static void scanXdotwithattrs(void *e) +{ + scanXdotwithattr(e, "_draw_"); + scanXdotwithattr(e, "_ldraw_"); + scanXdotwithattr(e, "_hdraw_"); + scanXdotwithattr(e, "_tdraw_"); + scanXdotwithattr(e, "_hldraw_"); + scanXdotwithattr(e, "_tldraw_"); } + + +/* + iterate in nodes and edges to cache fonts, run this once or whenever a new font is added to the graph +*/ + + +void scanGraph(Agraph_t * g) +{ + Agnode_t *v; + Agedge_t *e; + Agraph_t *s; + for (v = agfstnode(g); v; v = agnxtnode(g, v)) + { + scanXdotwithattr(v, "_draw_"); + scanXdotwithattr(v, "_ldraw_"); + for (e = agfstout(g, v); e; e = agnxtout(g, e)) + { + scanXdotwithattrs(e); + } + } + +} int randomize_color(RGBColor * c, int brightness) { float R, B, G; diff --git a/cmd/smyrna/draw.h b/cmd/smyrna/draw.h index b0e4e0425..41b93c5e4 100755 --- a/cmd/smyrna/draw.h +++ b/cmd/smyrna/draw.h @@ -26,6 +26,7 @@ typedef struct { xdot_op op; void *obj; + void *font; //pointer to font in view->fontset int size; } sdot_op; diff --git a/cmd/smyrna/glexpose.c b/cmd/smyrna/glexpose.c index 2cba67e71..2b28ab47b 100644 --- a/cmd/smyrna/glexpose.c +++ b/cmd/smyrna/glexpose.c @@ -91,7 +91,7 @@ int glexpose_main(ViewInfo * view) draw_selection_box(view); drawBorders(view); drawRotatingTools(); - drawtestpoly(); +// drawtestpoly(); /*DEBUG*/ /* if (view->mouse.mouse_mode == MM_PAN) { diff --git a/cmd/smyrna/gltemplate.c b/cmd/smyrna/gltemplate.c index 2394231cb..1cc54fe7b 100755 --- a/cmd/smyrna/gltemplate.c +++ b/cmd/smyrna/gltemplate.c @@ -138,7 +138,7 @@ static void realize(GtkWidget * widget, gpointer data) #endif // g_print("loading font....%i\n", fontLoad(smyrna_font)); - add_font(view->fontset,"Vladimir script bold 16");//wired default font + add_font(view->fontset,"Times 16");//wired in default font /*** OpenGL BEGIN ***/ if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext)) return; @@ -323,6 +323,7 @@ static gboolean button_release_event(GtkWidget * widget, view->Selection.Active = 1; expose_event(view->drawing_area, NULL, NULL); } + if (view->mouse.mouse_mode == MM_MOVE) { if (GD_TopView(view->g[view->activeGraph]) == 0) move_nodes(view->g[view->activeGraph]); diff --git a/cmd/smyrna/selection.c b/cmd/smyrna/selection.c index 8b3f2e1c3..3ed973b81 100755 --- a/cmd/smyrna/selection.c +++ b/cmd/smyrna/selection.c @@ -629,10 +629,14 @@ int lineintersects(float X1, float X2, float Y1, float Y2) float x, y, m, iter; float RX, RY, RW, RH; int intersects, in; + if (view->mouse.mouse_mode == MM_SINGLE_SELECT) + { + RW = 10; + RH = 10; + } + RX = view->Selection.X; RY = view->Selection.Y; - RW = view->Selection.W; - RH = view->Selection.H; if ((is_point_in_rectangle(X1, Y1, RX, RY, RW, RH)) && (is_point_in_rectangle(X2, Y2, RX, RY, RW, RH))) return 1; diff --git a/cmd/smyrna/viewport.c b/cmd/smyrna/viewport.c index fbfb9bec4..7ee966ecc 100755 --- a/cmd/smyrna/viewport.c +++ b/cmd/smyrna/viewport.c @@ -34,6 +34,7 @@ /* Forward declarations */ static int init_object_custom_data(Agraph_t * graph, void *obj); +static void refresh_borders(Agraph_t* g); #define countof( array ) ( sizeof( array )/sizeof( array[0] ) ) @@ -57,8 +58,9 @@ void clear_viewport(ViewInfo * view) { int ind = 0; /*free topview if there is one */ - if (view->graphCount) { - cleartopview(view->Topview); + if (GD_TopView(view->g[view->activeGraph])) + cleartopview(view->Topview); + if (view->graphCount) { /*all cgraph graphs should be freed */ for (ind = 0; ind < view->graphCount; ind++) { agclose(view->g[ind]); @@ -667,7 +669,7 @@ static char* create_xdot_for_graph(Agraph_t * graph, int keeppos) } #ifdef _WIN32 - path = "\"C:/Program Files/Graphviz2.15/bin/dot.exe\" "; + path = "\"C:/Program Files/Graphviz 2.20/bin/dot.exe\" "; #else path = "dot "; #endif @@ -680,7 +682,7 @@ static char* create_xdot_for_graph(Agraph_t * graph, int keeppos) else cmd = RALLOC (buflen, cmd, char); } sprintf (cmd, FMT, path, fix, alg, dotfile, xdotfile); - r = system (cmd); + r = system (cmd); unlink (dotfile); free (dotfile); @@ -689,7 +691,11 @@ static char* create_xdot_for_graph(Agraph_t * graph, int keeppos) free (xdotfile); return 0; } - else return xdotfile; + else + { + return xdotfile; + + } } /* @@ -709,6 +715,7 @@ layoutGraph (Agraph_t *oldg, int keeppos, int closeold) if (closeold) agclose (oldg); newg = agread(input_file, NIL(Agdisc_t *)); + refresh_borders(newg); g_print ("xdot is being loaded\n"); fclose (input_file); unlink (infile); // Remove temp file @@ -723,26 +730,32 @@ static Agraph_t *loadGraph(char *filename) { Agraph_t *g; FILE *input_file; - if (!(input_file = fopen(filename, "r"))) { - g_print("Cannot open %s\n", filename); - return 0; + char* bf; + char buf[512]; + if (!(input_file = fopen(filename, "r"))) + { + g_print("Cannot open %s\n", filename); + return 0; } - if (!(g = agread(input_file, NIL(Agdisc_t *)))) { - g_print("Cannot read graph in %s\n", filename); - fclose (input_file); - return 0; + if (!(g = agread(input_file, NIL(Agdisc_t *)))) + { + g_print("Cannot read graph in %s\n", filename); + fclose (input_file); + return 0; } /* If no position info, run layout with -Txdot */ - if (!agattr(g, AGNODE, "pos", NULL)) { - g = layoutGraph (g, 0, 1); - if (!g) return 0; + if (!agattr(g, AGNODE, "pos", NULL)) + { + g = layoutGraph (g, 0, 1); + if (!g) return 0; } /* If position info but not xdot, set Topview mode */ - else if (!agattr(g, AGRAPH, "xdotversion", 0)) { - agattr(g, AGRAPH, "TopView", "1"); + else if (!agattr(g, AGRAPH, "xdotversion", 0)) + { + agattr(g, AGRAPH, "TopView", "1"); } attach_object_custom_data_to_graph(g); @@ -768,10 +781,26 @@ static Agraph_t *loadGraph(char *filename) } } #endif - if(strcasecmp(agget(g, "TopView"),"1")==0) - preparetopview(g, view->Topview); + bf=agget(g, "TopView"); + + if (bf) + { + if(strcasecmp(agget(g, "TopView"),"1")==0) + preparetopview(g, view->Topview); + } + else //set graph borders + { + refresh_borders(g); + scanGraph(g); //calling font caching routines once.... + } return g; } +static void refresh_borders(Agraph_t* g) +{ + sscanf(agget(g,"bb"),"%f,%f,%f,%f",&(view->bdxLeft),&(view->bdyBottom),&(view->bdxRight),&(view->bdyTop)); +} + + /* add_graph_to_viewport_from_file: * returns 1 if successfull else 0 diff --git a/lib/glcomp/glTexFontTGA.c b/lib/glcomp/glTexFontTGA.c index 709113488..62212b858 100755 --- a/lib/glcomp/glTexFontTGA.c +++ b/lib/glcomp/glTexFontTGA.c @@ -306,7 +306,6 @@ unsigned char *load_png_font(char* file_name,int *imageWidth,int *imageHeight) for (i=0; i < info_ptr->height; i++) { - printf ("Column:%i\n",i); for (ii=0;ii < png_get_rowbytes(png_ptr, info_ptr); ii=ii+pixeloffset) { imageData[c]=row_pointers[info_ptr->height-i-1][ii]; @@ -325,17 +324,13 @@ unsigned char *load_png_font(char* file_name,int *imageWidth,int *imageHeight) b1=row_pointers[info_ptr->height-i-1][ii+1]; b2=row_pointers[info_ptr->height-i-1][ii+2]; b3=row_pointers[info_ptr->height-i-1][ii+3]; - printf ("(%i,%i,%i,%i)",b0,b1,b2,b3); } c++; } - printf ("\n"); } - printf ("embedded bytes:%i\n",c); - //cleaning libpng mess png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); png_free(png_ptr, row_pointers); diff --git a/lib/glcomp/glpangofont.c b/lib/glcomp/glpangofont.c index d4cd7abc8..98ffdf2d0 100644 --- a/lib/glcomp/glpangofont.c +++ b/lib/glcomp/glpangofont.c @@ -45,7 +45,7 @@ PangoLayout * get_pango_layout (cairo_t *cr, desc = pango_font_description_from_string(fontdescription); // pango_font_description_set_family(desc, "CENTAUR.TTF"); - //pango_font_description_set_size (desc, DEFAULT_FONT_SIZE * PANGO_SCALE/2.0); + pango_font_description_set_size (desc, DEFAULT_FONT_SIZE * PANGO_SCALE/2.0); // pango_font_description_set_style (desc,PANGO_STYLE_ITALIC); if (! pango_parse_markup (markup_text, -1, '\0', &attr_list, &text, NULL, NULL)) -- 2.40.0