From b1113db9422e04b1d0ebd0dbb9255908d042957f Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Wed, 16 Oct 2019 18:42:22 +0100 Subject: [PATCH] Make text elements compatable with HTML in SVG Correctly encodes less-than, greater-than and ampersand. Fixes #167 reported by Ian Jeffray --- backend/svg.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/backend/svg.c b/backend/svg.c index 745e66a6..2f462748 100644 --- a/backend/svg.c +++ b/backend/svg.c @@ -41,6 +41,57 @@ #include "common.h" +void make_html_friendly(unsigned char * string, unsigned char * html_version) { + /* Convert text into HTML friendly format by doing the following: + * > becomes > + * < becomes < + * & becomes & + */ + + int i, html_pos; + + html_pos = 0; + html_version[html_pos] = '\0'; + + for (i = 0; i < ustrlen(string); i++) { + switch(string[i]) { + case '>': + html_version[html_pos] = '&'; + html_version[html_pos + 1] = 'g'; + html_version[html_pos + 2] = 't'; + html_version[html_pos + 3] = ';'; + html_pos += 4; + html_version[html_pos] = '\0'; + break; + + case '<': + html_version[html_pos] = '&'; + html_version[html_pos + 1] = 'l'; + html_version[html_pos + 2] = 't'; + html_version[html_pos + 3] = ';'; + html_pos += 4; + html_version[html_pos] = '\0'; + break; + + case '&': + html_version[html_pos] = '&'; + html_version[html_pos + 1] = 'a'; + html_version[html_pos + 2] = 'm'; + html_version[html_pos + 3] = 'p'; + html_version[html_pos + 4] = ';'; + html_pos += 5; + html_version[html_pos] = '\0'; + break; + + default: + html_version[html_pos] = string[i]; + html_pos++; + html_version[html_pos] = '\0'; + break; + } + } +} + int svg_plot(struct zint_symbol *symbol) { FILE *fsvg; int error_number = 0; @@ -52,6 +103,12 @@ int svg_plot(struct zint_symbol *symbol) { struct zint_vector_hexagon *hex; struct zint_vector_circle *circle; struct zint_vector_string *string; + +#ifndef _MSC_VER + unsigned char html_string[200]; +#else + unsigned char* html_string = (unsigned char*) _alloca(200); +#endif /* Check for no created vector set */ /* E-Mail Christian Schmitz 2019-09-10: reason unknown Ticket #164*/ @@ -122,7 +179,8 @@ int svg_plot(struct zint_symbol *symbol) { while (string) { fprintf(fsvg, " x, string->y); fprintf(fsvg, " font-family=\"Helvetica\" font-size=\"%.1f\" fill=\"#%s\" >\n", string->fsize, symbol->fgcolour); - fprintf(fsvg, " %s\n", string->text); + make_html_friendly(string->text, html_string); + fprintf(fsvg, " %s\n", html_string); fprintf(fsvg, " \n"); string = string->next; }