From 12e0680e13e3b00fe085ff419abcef88dd512b23 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 3 May 2022 15:27:10 -0700 Subject: web: reuse elem array appending for drawing visits --- web/web_svg.c | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'web/web_svg.c') diff --git a/web/web_svg.c b/web/web_svg.c index 6524754..2f7dc07 100644 --- a/web/web_svg.c +++ b/web/web_svg.c @@ -20,19 +20,17 @@ #define C_WIRE_INNER "#d8e6ec" struct elem { - char *name; + char *ns, *name, *content; int n_attrs, size_attrs; char **attr_names, **attr_vals; - char *content; }; -struct elem *elem_init(char *name, int size_attrs) +struct elem *elem_init(char *ns, char *name, int size_attrs) { struct elem *e = malloc(sizeof(*e)); - size_t size = strlen(name) + 1; - e->name = malloc(size * sizeof(*(e->name))); - memcpy(e->name, name, size); + e->ns = ns ? strdup(ns) : NULL; + e->name = strdup(name); e->n_attrs = 0; e->size_attrs = size_attrs; @@ -43,6 +41,11 @@ struct elem *elem_init(char *name, int size_attrs) return e; } +static inline struct elem *svg_elem_init(char *name, int size_attrs) +{ + return elem_init("http://www.w3.org/2000/svg", name, size_attrs); +} + void elem_free(struct elem *e) { for(int i = 0; i < e->n_attrs; ++i) { @@ -67,13 +70,8 @@ void elem_add_attr(struct elem *e, char *name, char *val) e->size_attrs * sizeof(*(e->attr_vals))); } - size_t size = strlen(name) + 1; - e->attr_names[e->n_attrs] = malloc(size); - memcpy(e->attr_names[e->n_attrs], name, size); - - size = strlen(val) + 1; - e->attr_vals[e->n_attrs] = malloc(size); - memcpy(e->attr_vals[e->n_attrs++], val, size); + e->attr_names[e->n_attrs] = strdup(name); + e->attr_vals[e->n_attrs++] = strdup(val); } void elem_add_attr_double(struct elem *e, char *name, double val) @@ -95,7 +93,7 @@ struct elem *gen_arc(double a1, double a2, double r1, double r2, char *col) struct ccoords cc4 = pol_to_cart((struct pcoords){ .a = (a1 == a2 ? 1 : a2), .r = r2 }); - struct elem *e = elem_init("path", 2); + struct elem *e = svg_elem_init("path", 2); elem_add_attr(e, "fill", col); char buf[512]; @@ -131,7 +129,7 @@ struct elem *gen_ring(double r, double w, char *col) struct elem *gen_circle(double r, char *col) { - struct elem *e = elem_init("circle", 2); + struct elem *e = svg_elem_init("circle", 2); elem_add_attr(e, "fill", col); elem_add_attr_double(e, "r", r); @@ -143,7 +141,7 @@ struct elem *gen_line(double a, double r1, double r2, double w, char *col) struct ccoords cc1 = pol_to_cart((struct pcoords){ .a = a, .r = r1 }); struct ccoords cc2 = pol_to_cart((struct pcoords){ .a = a, .r = r2 }); - struct elem *e = elem_init("line", 6); + struct elem *e = svg_elem_init("line", 6); elem_add_attr(e, "stroke", col); elem_add_attr_double(e, "x1", cc1.x); elem_add_attr_double(e, "y1", cc1.y); @@ -185,7 +183,7 @@ int draw_numbers(int elemc, struct elem **elemv) int r = DIAMETER/2 - 33/2; for (int i = 0; i < 20; ++i) { - struct elem *e = elemv[elemc++] = elem_init("text", 5); + struct elem *e = elemv[elemc++] = svg_elem_init("text", 5); elem_add_attr(e, "font-size", "33"); elem_add_attr(e, "fill", "#fff"); elem_add_attr(e, "text-anchor", "middle"); @@ -208,11 +206,11 @@ int draw_numbers(int elemc, struct elem **elemv) void append_elemv(char *sel, int elemc, struct elem **elemv) { - EM_ASM({svgAppendElemv($0, $1, $2, $3, $4, $5, $6, $7)}, + EM_ASM({elemAppendElemv($0, $1, $2, $3, $4, $5, $6, $7, $8)}, sel, elemc, elemv, - offsetof(struct elem, name), offsetof(struct elem, n_attrs), - offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals), - offsetof(struct elem, content)); + offsetof(struct elem, ns), offsetof(struct elem, name), + offsetof(struct elem, content), offsetof(struct elem, n_attrs), + offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals)); } void svg_draw_board() -- cgit v1.2.3-70-g09d2