From 250dee5951bcbf910893330bfb556df5295358fb Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 27 Nov 2021 14:03:47 -0800 Subject: [PATCH] fig plugin: replace 'fig_string' with 'gvputs_nonascii' This partially de-dupes some code (`mp_string` and `pic_string` are duplicates of `fig_string`), makes this plugin more thread safe (no static buffer is no longer used), and improves its efficiency (bytes are now written directly into the output instead of a temporary buffer, requiring dynamic allocation and two copies). Related to #2051. --- plugin/core/gvrender_core_fig.c | 46 +++------------------------------ 1 file changed, 4 insertions(+), 42 deletions(-) diff --git a/plugin/core/gvrender_core_fig.c b/plugin/core/gvrender_core_fig.c index 1df4db224..d6ee97f30 100644 --- a/plugin/core/gvrender_core_fig.c +++ b/plugin/core/gvrender_core_fig.c @@ -13,7 +13,6 @@ #include #include #include -#include #ifdef _WIN32 #include @@ -52,44 +51,6 @@ static void figptarray(GVJ_t *job, pointf * A, int n, int close) gvputs(job, "\n"); } -static char *fig_string(char *s) -{ - static char *buf = NULL; - static size_t bufsize = 0; - size_t pos = 0; - char *p; - char c; - - if (!buf) { - bufsize = 64; - buf = malloc(bufsize * sizeof(char)); - } - - p = buf; - while ((c = *s++)) { - if (pos > (bufsize - 8)) { - bufsize *= 2; - buf = realloc(buf, bufsize * sizeof(char)); - p = buf + pos; - } - if (isascii(c)) { - if (c == '\\') { - *p++ = '\\'; - pos++; - } - *p++ = c; - pos++; - } else { - *p++ = '\\'; - sprintf(p, "%03o", (unsigned)c); - p += 3; - pos += 4; - } - } - *p = '\0'; - return buf; -} - static int figColorResolve(int *new, unsigned char r, unsigned char g, unsigned char b) { @@ -292,10 +253,11 @@ static void fig_textspan(GVJ_t * job, pointf p, textspan_t * span) $A \\in M_0$\001 */ gvprintf(job, - "%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %d %d %s\\001\n", + "%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %d %d ", object_code, sub_type, color, depth, pen_style, font, - font_size, angle, font_flags, height, length, ROUND(p.x), ROUND((p.y-72.0)), - fig_string(span->str)); + font_size, angle, font_flags, height, length, ROUND(p.x), ROUND((p.y-72.0))); + gvputs_nonascii(job, span->str); + gvputs(job, "\\001\n"); } static void fig_ellipse(GVJ_t * job, pointf * A, int filled) -- 2.40.0