svg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* svg.c - Scalable Vector Graphics */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2009-2024 Robin Stuart <rstuart114@gmail.com>
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. Neither the name of the project nor the names of its contributors
  14. may be used to endorse or promote products derived from this software
  15. without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  20. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. SUCH DAMAGE.
  27. */
  28. /* SPDX-License-Identifier: BSD-3-Clause */
  29. #include <errno.h>
  30. #include <math.h>
  31. #include <stdio.h>
  32. #include "common.h"
  33. #include "filemem.h"
  34. #include "output.h"
  35. #include "fonts/normal_woff2.h"
  36. #include "fonts/upcean_woff2.h"
  37. /* Convert Ultracode rectangle colour to RGB */
  38. static void svg_pick_colour(const int colour, char colour_code[7]) {
  39. const int idx = colour >= 1 && colour <= 8 ? colour - 1 : 6 /*black*/;
  40. static const char rgbs[8][7] = {
  41. "00ffff", /* 0: Cyan (1) */
  42. "0000ff", /* 1: Blue (2) */
  43. "ff00ff", /* 2: Magenta (3) */
  44. "ff0000", /* 3: Red (4) */
  45. "ffff00", /* 4: Yellow (5) */
  46. "00ff00", /* 5: Green (6) */
  47. "000000", /* 6: Black (7) */
  48. "ffffff", /* 7: White (8) */
  49. };
  50. strcpy(colour_code, rgbs[idx]);
  51. }
  52. /* Convert text to use HTML entity codes */
  53. static void svg_make_html_friendly(const unsigned char *string, char *html_version) {
  54. for (; *string; string++) {
  55. switch (*string) {
  56. case '>':
  57. strcpy(html_version, "&gt;");
  58. html_version += 4;
  59. break;
  60. case '<':
  61. strcpy(html_version, "&lt;");
  62. html_version += 4;
  63. break;
  64. case '&':
  65. strcpy(html_version, "&amp;");
  66. html_version += 5;
  67. break;
  68. case '"':
  69. strcpy(html_version, "&quot;");
  70. html_version += 6;
  71. break;
  72. case '\'':
  73. strcpy(html_version, "&apos;");
  74. html_version += 6;
  75. break;
  76. default:
  77. *html_version++ = *string;
  78. break;
  79. }
  80. }
  81. *html_version = '\0';
  82. }
  83. /* Helper to output floating point attribute */
  84. static void svg_put_fattrib(const char *prefix, const int dp, const float val, struct filemem *fmp) {
  85. fm_putsf(prefix, dp, val, fmp);
  86. fm_putc('"', fmp);
  87. }
  88. /* Helper to output opacity attribute attribute and close tag (maybe) */
  89. static void svg_put_opacity_close(const unsigned char alpha, const float val, const int close, struct filemem *fmp) {
  90. if (alpha != 0xff) {
  91. svg_put_fattrib(" opacity=\"", 3, val, fmp);
  92. }
  93. if (close) {
  94. fm_putc('/', fmp);
  95. }
  96. fm_puts(">\n", fmp);
  97. }
  98. INTERNAL int svg_plot(struct zint_symbol *symbol) {
  99. static const char normal_font_family[] = "Arimo";
  100. static const char upcean_font_family[] = "OCRB";
  101. struct filemem fm;
  102. struct filemem *const fmp = &fm;
  103. float previous_diameter;
  104. float radius, half_radius, half_sqrt3_radius;
  105. int i;
  106. char fgcolour_string[7];
  107. char bgcolour_string[7];
  108. unsigned char fgred, fggreen, fgblue, fg_alpha;
  109. unsigned char bgred, bggreen, bgblue, bg_alpha;
  110. float fg_alpha_opacity = 0.0f, bg_alpha_opacity = 0.0f; /* Suppress `-Wmaybe-uninitialized` */
  111. int bold;
  112. struct zint_vector_rect *rect;
  113. struct zint_vector_hexagon *hex;
  114. struct zint_vector_circle *circle;
  115. struct zint_vector_string *string;
  116. char colour_code[7];
  117. int len, html_len;
  118. const int upcean = is_upcean(symbol->symbology);
  119. char *html_string;
  120. (void) out_colour_get_rgb(symbol->fgcolour, &fgred, &fggreen, &fgblue, &fg_alpha);
  121. if (fg_alpha != 0xff) {
  122. fg_alpha_opacity = fg_alpha / 255.0f;
  123. }
  124. sprintf(fgcolour_string, "%02X%02X%02X", fgred, fggreen, fgblue);
  125. (void) out_colour_get_rgb(symbol->bgcolour, &bgred, &bggreen, &bgblue, &bg_alpha);
  126. if (bg_alpha != 0xff) {
  127. bg_alpha_opacity = bg_alpha / 255.0f;
  128. }
  129. sprintf(bgcolour_string, "%02X%02X%02X", bgred, bggreen, bgblue);
  130. len = (int) ustrlen(symbol->text);
  131. html_len = len + 1;
  132. for (i = 0; i < len; i++) {
  133. switch (symbol->text[i]) {
  134. case '>':
  135. case '<':
  136. case '"':
  137. case '&':
  138. case '\'':
  139. html_len += 6;
  140. break;
  141. }
  142. }
  143. if (symbol->output_options & EANUPC_GUARD_WHITESPACE) {
  144. html_len += 12; /* Allow for "<" & ">" */
  145. }
  146. html_string = (char *) z_alloca(html_len);
  147. /* Check for no created vector set */
  148. /* E-Mail Christian Schmitz 2019-09-10: reason unknown Ticket #164 */
  149. if (symbol->vector == NULL) {
  150. return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 681, "Vector header NULL");
  151. }
  152. if (!fm_open(fmp, symbol, "w")) {
  153. return errtxtf(ZINT_ERROR_FILE_ACCESS, symbol, 680, "Could not open SVG output file (%1$d: %2$s)", fmp->err,
  154. strerror(fmp->err));
  155. }
  156. /* Start writing the header */
  157. fm_puts("<?xml version=\"1.0\" standalone=\"no\"?>\n"
  158. "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
  159. fmp);
  160. fm_printf(fmp, "<svg width=\"%d\" height=\"%d\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n",
  161. (int) ceilf(symbol->vector->width), (int) ceilf(symbol->vector->height));
  162. fm_puts(" <desc>Zint Generated Symbol</desc>\n", fmp);
  163. if ((symbol->output_options & EMBED_VECTOR_FONT) && symbol->vector->strings) {
  164. /* Split into `puts()` rather than one very large `printf()` */
  165. fm_printf(fmp, " <style>@font-face {font-family:\"%s\"; src:url(data:font/woff2;base64,",
  166. upcean ? "OCRB" : "Arimo");
  167. fm_puts(upcean ? upcean_woff2 : normal_woff2, fmp);
  168. fm_puts(");}</style>\n", fmp);
  169. }
  170. fm_printf(fmp, " <g id=\"barcode\" fill=\"#%s\">\n", fgcolour_string);
  171. if (bg_alpha != 0) {
  172. fm_printf(fmp, " <rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\"",
  173. (int) ceilf(symbol->vector->width), (int) ceilf(symbol->vector->height), bgcolour_string);
  174. svg_put_opacity_close(bg_alpha, bg_alpha_opacity, 1 /*close*/, fmp);
  175. }
  176. if (symbol->vector->rectangles) {
  177. int current_colour = 0;
  178. rect = symbol->vector->rectangles;
  179. fm_puts(" <path d=\"", fmp);
  180. while (rect) {
  181. if (current_colour && rect->colour != current_colour) {
  182. fm_putc('"', fmp);
  183. if (current_colour != -1) {
  184. svg_pick_colour(current_colour, colour_code);
  185. fm_printf(fmp, " fill=\"#%s\"", colour_code);
  186. }
  187. svg_put_opacity_close(fg_alpha, fg_alpha_opacity, 1 /*close*/, fmp);
  188. fm_puts(" <path d=\"", fmp);
  189. }
  190. current_colour = rect->colour;
  191. fm_putsf("M", 2, rect->x, fmp);
  192. fm_putsf(" ", 2, rect->y, fmp);
  193. fm_putsf("h", 2, rect->width, fmp);
  194. fm_putsf("v", 2, rect->height, fmp);
  195. fm_putsf("h-", 2, rect->width, fmp);
  196. fm_puts("Z", fmp);
  197. rect = rect->next;
  198. }
  199. fm_putc('"', fmp);
  200. if (current_colour != -1) {
  201. svg_pick_colour(current_colour, colour_code);
  202. fm_printf(fmp, " fill=\"#%s\"", colour_code);
  203. }
  204. svg_put_opacity_close(fg_alpha, fg_alpha_opacity, 1 /*close*/, fmp);
  205. }
  206. if (symbol->vector->hexagons) {
  207. previous_diameter = radius = half_radius = half_sqrt3_radius = 0.0f;
  208. hex = symbol->vector->hexagons;
  209. fm_puts(" <path d=\"", fmp);
  210. while (hex) {
  211. if (previous_diameter != hex->diameter) {
  212. previous_diameter = hex->diameter;
  213. radius = 0.5f * previous_diameter;
  214. half_radius = 0.25f * previous_diameter;
  215. half_sqrt3_radius = 0.43301270189221932338f * previous_diameter;
  216. }
  217. if ((hex->rotation == 0) || (hex->rotation == 180)) {
  218. fm_putsf("M", 2, hex->x, fmp);
  219. fm_putsf(" ", 2, hex->y + radius, fmp);
  220. fm_putsf("L", 2, hex->x + half_sqrt3_radius, fmp);
  221. fm_putsf(" ", 2, hex->y + half_radius, fmp);
  222. fm_putsf("L", 2, hex->x + half_sqrt3_radius, fmp);
  223. fm_putsf(" ", 2, hex->y - half_radius, fmp);
  224. fm_putsf("L", 2, hex->x, fmp);
  225. fm_putsf(" ", 2, hex->y - radius, fmp);
  226. fm_putsf("L", 2, hex->x - half_sqrt3_radius, fmp);
  227. fm_putsf(" ", 2, hex->y - half_radius, fmp);
  228. fm_putsf("L", 2, hex->x - half_sqrt3_radius, fmp);
  229. fm_putsf(" ", 2, hex->y + half_radius, fmp);
  230. } else {
  231. fm_putsf("M", 2, hex->x - radius, fmp);
  232. fm_putsf(" ", 2, hex->y, fmp);
  233. fm_putsf("L", 2, hex->x - half_radius, fmp);
  234. fm_putsf(" ", 2, hex->y + half_sqrt3_radius, fmp);
  235. fm_putsf("L", 2, hex->x + half_radius, fmp);
  236. fm_putsf(" ", 2, hex->y + half_sqrt3_radius, fmp);
  237. fm_putsf("L", 2, hex->x + radius, fmp);
  238. fm_putsf(" ", 2, hex->y, fmp);
  239. fm_putsf("L", 2, hex->x + half_radius, fmp);
  240. fm_putsf(" ", 2, hex->y - half_sqrt3_radius, fmp);
  241. fm_putsf("L", 2, hex->x - half_radius, fmp);
  242. fm_putsf(" ", 2, hex->y - half_sqrt3_radius, fmp);
  243. }
  244. fm_putc('Z', fmp);
  245. hex = hex->next;
  246. }
  247. fm_putc('"', fmp);
  248. svg_put_opacity_close(fg_alpha, fg_alpha_opacity, 1 /*close*/, fmp);
  249. }
  250. previous_diameter = radius = 0.0f;
  251. circle = symbol->vector->circles;
  252. while (circle) {
  253. if (previous_diameter != circle->diameter) {
  254. previous_diameter = circle->diameter;
  255. radius = 0.5f * previous_diameter;
  256. }
  257. fm_puts(" <circle", fmp);
  258. svg_put_fattrib(" cx=\"", 2, circle->x, fmp);
  259. svg_put_fattrib(" cy=\"", 2, circle->y, fmp);
  260. svg_put_fattrib(" r=\"", circle->width ? 3 : 2, radius, fmp);
  261. if (circle->colour) { /* Legacy - no longer used */
  262. if (circle->width) {
  263. fm_printf(fmp, " stroke=\"#%s\"", bgcolour_string);
  264. svg_put_fattrib(" stroke-width=\"", 3, circle->width, fmp);
  265. fm_puts(" fill=\"none\"", fmp);
  266. } else {
  267. fm_printf(fmp, " fill=\"#%s\"", bgcolour_string);
  268. }
  269. /* This doesn't work how the user is likely to expect - more work needed! */
  270. svg_put_opacity_close(bg_alpha, bg_alpha_opacity, 1 /*close*/, fmp);
  271. } else {
  272. if (circle->width) {
  273. fm_printf(fmp, " stroke=\"#%s\"", fgcolour_string);
  274. svg_put_fattrib(" stroke-width=\"", 3, circle->width, fmp);
  275. fm_puts(" fill=\"none\"", fmp);
  276. }
  277. svg_put_opacity_close(fg_alpha, fg_alpha_opacity, 1 /*close*/, fmp);
  278. }
  279. circle = circle->next;
  280. }
  281. bold = (symbol->output_options & BOLD_TEXT) && !upcean;
  282. string = symbol->vector->strings;
  283. while (string) {
  284. const char *const halign = string->halign == 2 ? "end" : string->halign == 1 ? "start" : "middle";
  285. fm_puts(" <text", fmp);
  286. svg_put_fattrib(" x=\"", 2, string->x, fmp);
  287. svg_put_fattrib(" y=\"", 2, string->y, fmp);
  288. fm_printf(fmp, " text-anchor=\"%s\"", halign);
  289. if (upcean) {
  290. fm_printf(fmp, " font-family=\"%s, monospace\"", upcean_font_family);
  291. } else {
  292. fm_printf(fmp, " font-family=\"%s, Arial, sans-serif\"", normal_font_family);
  293. }
  294. svg_put_fattrib(" font-size=\"", 1, string->fsize, fmp);
  295. if (bold) {
  296. fm_puts(" font-weight=\"bold\"", fmp);
  297. }
  298. if (string->rotation != 0) {
  299. fm_printf(fmp, " transform=\"rotate(%d", string->rotation);
  300. fm_putsf(",", 2, string->x, fmp);
  301. fm_putsf(",", 2, string->y, fmp);
  302. fm_puts(")\"", fmp);
  303. }
  304. svg_put_opacity_close(fg_alpha, fg_alpha_opacity, 0 /*close*/, fmp);
  305. svg_make_html_friendly(string->text, html_string);
  306. fm_printf(fmp, " %s\n", html_string);
  307. fm_puts(" </text>\n", fmp);
  308. string = string->next;
  309. }
  310. fm_puts(" </g>\n"
  311. "</svg>\n", fmp);
  312. if (fm_error(fmp)) {
  313. errtxtf(0, symbol, 682, "Incomplete write to SVG output (%1$d: %2$s)", fmp->err, strerror(fmp->err));
  314. (void) fm_close(fmp, symbol);
  315. return ZINT_ERROR_FILE_WRITE;
  316. }
  317. if (!fm_close(fmp, symbol)) {
  318. return errtxtf(ZINT_ERROR_FILE_WRITE, symbol, 684, "Failure on closing SVG output file (%1$d: %2$s)",
  319. fmp->err, strerror(fmp->err));
  320. }
  321. return 0;
  322. }
  323. /* vim: set ts=4 sw=4 et : */