/* svg.c - Scalable Vector Graphics */ /* libzint - the open source barcode library Copyright (C) 2009 - 2020 Robin Stuart Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* vim: set ts=4 sw=4 et : */ #include #include #include #include #ifdef _MSC_VER #include #endif #include "common.h" void pick_colour(int colour, char colour_code[]) { switch(colour) { case 1: // Cyan strcpy(colour_code, "00ffff"); break; case 2: // Blue strcpy(colour_code, "0000ff"); break; case 3: // Magenta strcpy(colour_code, "ff00ff"); break; case 4: // Red strcpy(colour_code, "ff0000"); break; case 5: // Yellow strcpy(colour_code, "ffff00"); break; case 6: // Green strcpy(colour_code, "00ff00"); break; case 8: // White strcpy(colour_code, "ffffff"); break; default: // Black strcpy(colour_code, "000000"); break; } } static void make_html_friendly(unsigned char * string, char * html_version) { /* Converts text to use HTML entity codes */ int i, html_pos; html_pos = 0; html_version[html_pos] = '\0'; for (i = 0; i < (int) ustrlen(string); i++) { switch(string[i]) { case '>': strcat(html_version, ">"); html_pos += 4; break; case '<': strcat(html_version, "<"); html_pos += 4; break; case '&': strcat(html_version, "&"); html_pos += 5; break; case '"': strcat(html_version, """); html_pos += 6; break; case '\'': strcat(html_version, "'"); html_pos += 6; break; default: html_version[html_pos] = string[i]; html_pos++; html_version[html_pos] = '\0'; break; } } } INTERNAL int svg_plot(struct zint_symbol *symbol) { FILE *fsvg; int error_number = 0; const char *locale = NULL; float ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy; float radius; int i; char fgcolour_string[7]; char bgcolour_string[7]; int bg_alpha = 0xff; int fg_alpha = 0xff; const char *font_family = "Helvetica, sans-serif"; int bold; struct zint_vector_rect *rect; struct zint_vector_hexagon *hex; struct zint_vector_circle *circle; struct zint_vector_string *string; char colour_code[7]; int html_len; #ifdef _MSC_VER char* html_string; #endif for (i = 0; i < 6; i++) { fgcolour_string[i] = symbol->fgcolour[i]; bgcolour_string[i] = symbol->bgcolour[i]; } fgcolour_string[6] = '\0'; bgcolour_string[6] = '\0'; if (strlen(symbol->fgcolour) > 6) { fg_alpha = (16 * ctoi(symbol->fgcolour[6])) + ctoi(symbol->fgcolour[7]); } if (strlen(symbol->bgcolour) > 6) { bg_alpha = (16 * ctoi(symbol->bgcolour[6])) + ctoi(symbol->bgcolour[7]); } html_len = strlen((char *)symbol->text) + 1; for (i = 0; i < (int) strlen((char *)symbol->text); i++) { switch(symbol->text[i]) { case '>': case '<': case '"': case '&': case '\'': html_len += 6; break; } } #ifndef _MSC_VER char html_string[html_len]; #else html_string = (char*) _alloca(html_len); #endif /* Check for no created vector set */ /* E-Mail Christian Schmitz 2019-09-10: reason unknown Ticket #164*/ if (symbol->vector == NULL) { return ZINT_ERROR_INVALID_DATA; } if (symbol->output_options & BARCODE_STDOUT) { fsvg = stdout; } else { fsvg = fopen(symbol->outfile, "w"); } if (fsvg == NULL) { strcpy(symbol->errtxt, "680: Could not open output file"); return ZINT_ERROR_FILE_ACCESS; } locale = setlocale(LC_ALL, "C"); /* Start writing the header */ fprintf(fsvg, "\n"); fprintf(fsvg, "\n"); fprintf(fsvg, "vector->width), (int) ceil(symbol->vector->height)); fprintf(fsvg, " xmlns=\"http://www.w3.org/2000/svg\">\n"); fprintf(fsvg, " Zint Generated Symbol\n"); fprintf(fsvg, " \n"); fprintf(fsvg, "\n \n", fgcolour_string); if (bg_alpha != 0) { fprintf(fsvg, " vector->width), (int) ceil(symbol->vector->height), bgcolour_string); if (bg_alpha != 0xff) { fprintf(fsvg, " opacity=\"%.3f\"", (float) bg_alpha / 255.0); } fprintf(fsvg, " />\n"); } rect = symbol->vector->rectangles; while (rect) { fprintf(fsvg, " x, rect->y, rect->width, rect->height); if (rect->colour != -1) { pick_colour(rect->colour, colour_code); fprintf(fsvg, " fill=\"#%s\"", colour_code); } if (fg_alpha != 0xff) { fprintf(fsvg, " opacity=\"%.3f\"", (float) fg_alpha / 255.0); } fprintf(fsvg, " />\n"); rect = rect->next; } hex = symbol->vector->hexagons; while (hex) { radius = hex->diameter / 2.0; if ((hex->rotation == 0) || (hex->rotation == 180)) { ay = hex->y + (1.0 * radius); by = hex->y + (0.5 * radius); cy = hex->y - (0.5 * radius); dy = hex->y - (1.0 * radius); ey = hex->y - (0.5 * radius); fy = hex->y + (0.5 * radius); ax = hex->x; bx = hex->x + (0.86 * radius); cx = hex->x + (0.86 * radius); dx = hex->x; ex = hex->x - (0.86 * radius); fx = hex->x - (0.86 * radius); } else { ay = hex->y; by = hex->y + (0.86 * radius); cy = hex->y + (0.86 * radius); dy = hex->y; ey = hex->y - (0.86 * radius); fy = hex->y - (0.86 * radius); ax = hex->x - (1.0 * radius); bx = hex->x - (0.5 * radius); cx = hex->x + (0.5 * radius); dx = hex->x + (1.0 * radius); ex = hex->x + (0.5 * radius); fx = hex->x - (0.5 * radius); } fprintf(fsvg, " \n"); hex = hex->next; } circle = symbol->vector->circles; while (circle) { fprintf(fsvg, " x, circle->y, circle->diameter / 2.0); if (circle->colour) { fprintf(fsvg, " fill=\"#%s\"", bgcolour_string); if (bg_alpha != 0xff) { // This doesn't work how the user is likely to expect - more work needed! fprintf(fsvg, " opacity=\"%.3f\"", (float) bg_alpha / 255.0); } } else { if (fg_alpha != 0xff) { fprintf(fsvg, " opacity=\"%.3f\"", (float) fg_alpha / 255.0); } } fprintf(fsvg, " />\n"); circle = circle->next; } bold = (symbol->output_options & BOLD_TEXT) && (!is_extendable(symbol->symbology) || (symbol->output_options & SMALL_TEXT)); string = symbol->vector->strings; while (string) { const char *halign = string->halign == 2 ? "end" : string->halign == 1 ? "start" : "middle"; fprintf(fsvg, " x, string->y, halign); fprintf(fsvg, " font-family=\"%s\" font-size=\"%.1f\"", font_family, string->fsize); if (bold) { fprintf(fsvg, " font-weight=\"bold\""); } if (fg_alpha != 0xff) { fprintf(fsvg, " opacity=\"%.3f\"", (float) fg_alpha / 255.0); } if (string->rotation != 0) { fprintf(fsvg, " transform=\"rotate(%d,%.2f,%.2f)\"", string->rotation, string->x, string->y); } fprintf(fsvg, " >\n"); make_html_friendly(string->text, html_string); fprintf(fsvg, " %s\n", html_string); fprintf(fsvg, " \n"); string = string->next; } fprintf(fsvg, " \n"); fprintf(fsvg, "\n"); if (symbol->output_options & BARCODE_STDOUT) { fflush(fsvg); } else { fclose(fsvg); } if (locale) setlocale(LC_ALL, locale); return error_number; }