]> granicus.if.org Git - graphviz/commitdiff
common taper: return 'stroke_t' by value
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 23 Dec 2022 21:50:19 +0000 (13:50 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 24 Dec 2022 17:52:53 +0000 (09:52 -0800)
Saves unnecessary dynamic allocation and caller clean up.

lib/common/emit.c
lib/common/render.h
lib/common/taper.c

index 4f959f7f1e53e8116bc226fc3b1b39c055ff9f31..d7687b13bee2adf10057f744c0cf3a173030e7c2 100644 (file)
@@ -2199,12 +2199,8 @@ static int multicolor (GVJ_t * job, edge_t * e, char** styles, char* colors, int
     return 0;
 }
 
-static void free_stroke (stroke_t* sp)
-{
-    if (sp) {
-       free (sp->vertices);
-       free (sp);
-    }
+static void free_stroke(stroke_t sp) {
+  free(sp.vertices);
 }
 
 typedef double (*radfunc_t)(double,double,double);
@@ -2323,15 +2319,14 @@ static void emit_edge_graphics(GVJ_t * job, edge_t * e, char** styles)
        color = pencolor;
 
        if (tapered) {
-           stroke_t* stp;
            if (*color == '\0') color = DEFAULT_COLOR;
            if (*fillcolor == '\0') fillcolor = DEFAULT_COLOR;
            gvrender_set_pencolor(job, "transparent");
            gvrender_set_fillcolor(job, color);
            bz = ED_spl(e)->list[0];
-           stp = taper (&bz, taperfun (e), penwidth, 0, 0);
-           gvrender_polygon(job, stp->vertices, stp->nvertices, TRUE);
-           free_stroke (stp);
+           stroke_t stp = taper(&bz, taperfun (e), penwidth, 0, 0);
+           gvrender_polygon(job, stp.vertices, stp.nvertices, TRUE);
+           free_stroke(stp);
            gvrender_set_pencolor(job, color);
            if (fillcolor != color)
                gvrender_set_fillcolor(job, fillcolor);
index fa6ddde426c7ef98ee5765291273cb85b35959ce..d8bcf10eab48cb04df6fe46d1169374a588579cd 100644 (file)
@@ -132,7 +132,7 @@ extern "C" {
     RENDER_API void shape_clip(node_t * n, pointf curve[4]);
     RENDER_API void make_simple_label (GVC_t * gvc, textlabel_t* rv);
     RENDER_API int stripedBox (GVJ_t * job, pointf* AF, char* clrs, int rotate);
-    RENDER_API stroke_t* taper (bezier*, double (*radfunc_t)(double,double,double), double initwid, int linejoin, int linecap);
+    RENDER_API stroke_t taper (bezier*, double (*radfunc_t)(double,double,double), double initwid, int linejoin, int linecap);
     RENDER_API pointf textspan_size(GVC_t * gvc, textspan_t * span);
     RENDER_API Dt_t * textfont_dict_open(GVC_t *gvc);
     RENDER_API void textfont_dict_close(GVC_t *gvc);
index 02dababda2af7adb8a8710341539a7754b7e2ba4..093a7d511c66ed6dafab67e81d7e4e857790a72d 100644 (file)
@@ -250,10 +250,8 @@ typedef double (*radfunc_t) (double curlen, double totallen, double initwid);
  * The linejoin and linecap parameters have roughly the same meaning as in postscript.
  *  - linejoin = 0 or 1 
  *  - linecap = 0 or 1 or 2 
- *
- * Calling function needs to free the allocated stroke_t.
  */
-stroke_t* taper (bezier* bez, radfunc_t radfunc, double initwid, int linejoin, int linecap)
+stroke_t taper (bezier* bez, radfunc_t radfunc, double initwid, int linejoin, int linecap)
 {
     int i, l, n;
     int pathcount, bevel;
@@ -266,7 +264,6 @@ stroke_t* taper (bezier* bez, radfunc_t radfunc, double initwid, int linejoin, i
     double lx, ly, ldir;
     double lineout=0, linerad=0, linelen=0;
     double theta, phi;
-    stroke_t* p;
 
     pathcount = arr->cnt;
     pathpoints = arr->pts;
@@ -351,7 +348,7 @@ stroke_t* taper (bezier* bez, radfunc_t radfunc, double initwid, int linejoin, i
     }
 
         /* draw line */
-    p = NEW(stroke_t);
+    stroke_t p = {0};
         /* side 1 */
     for (i = 0; i < pathcount; i++) {
        cur_point = pathpoints[i];
@@ -362,20 +359,20 @@ stroke_t* taper (bezier* bez, radfunc_t radfunc, double initwid, int linejoin, i
        bevel = cur_point.bevel;
        direction_2 = cur_point.dir2;
        if (i == 0) {
-           moveto (p, x+cos(direction)*lineout, y+sin(direction)*lineout);
+           moveto(&p, x+cos(direction)*lineout, y+sin(direction)*lineout);
        } else {
-           lineto (p, x+cos(direction)*lineout, y+sin(direction)*lineout);
+           lineto(&p, x+cos(direction)*lineout, y+sin(direction)*lineout);
        }
        if (bevel) {
-           drawbevel (x, y, lineout, TRUE, direction, direction_2, linejoin, p);
+           drawbevel(x, y, lineout, TRUE, direction, direction_2, linejoin, &p);
        }
     }
         /* end circle as needed */
     if (linecap == 1) {
-       arcn (p, x,y,lineout,direction,direction+D2R(180));
+       arcn(&p, x,y,lineout,direction,direction+D2R(180));
     } else {
        direction += D2R(180);
-       lineto (p, x+cos(direction)*lineout, y+sin(direction)*lineout);
+       lineto(&p, x+cos(direction)*lineout, y+sin(direction)*lineout);
     }
         /* side 2 */
     for (i = pathcount-2; i >= 0; i--) {
@@ -386,16 +383,16 @@ stroke_t* taper (bezier* bez, radfunc_t radfunc, double initwid, int linejoin, i
        lineout = cur_point.lout;
        bevel = cur_point.bevel;
        direction_2 = cur_point.dir2 + D2R(180);
-       lineto (p, x+cos(direction_2)*lineout, y+sin(direction_2)*lineout);
+       lineto(&p, x+cos(direction_2)*lineout, y+sin(direction_2)*lineout);
        if (bevel) { 
-           drawbevel (x, y, lineout, FALSE, direction, direction_2, linejoin, p);
+           drawbevel(x, y, lineout, FALSE, direction, direction_2, linejoin, &p);
        }
     }
         /* start circle if needed */
     if (linecap == 1) {
-       arcn (p, x,y,lineout,direction,direction+D2R(180));
+       arcn(&p, x,y,lineout,direction,direction+D2R(180));
     }
-    /* closepath (p); */
+    /* closepath(&p); */
     freeArr (arr);
     return p;
 }