svg-color.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "svg-imp.h"
  24. #include <string.h>
  25. /* Color keywords (white, blue, fuchsia)
  26. * System color keywords (ActiveBorder, ButtonFace -- need to find reasonable defaults)
  27. * #fb0 (expand to #ffbb00)
  28. * #ffbb00
  29. * rgb(255,255,255)
  30. * rgb(100%,100%,100%)
  31. *
  32. * "red icc-color(profileName,255,0,0)" (not going to support for now)
  33. */
  34. struct
  35. {
  36. const char *name;
  37. float red, green, blue;
  38. }
  39. svg_predefined_colors[] =
  40. {
  41. { "aliceblue", 240, 248, 255 },
  42. { "antiquewhite", 250, 235, 215 },
  43. { "aqua", 0, 255, 255 },
  44. { "aquamarine", 127, 255, 212 },
  45. { "azure", 240, 255, 255 },
  46. { "beige", 245, 245, 220 },
  47. { "bisque", 255, 228, 196 },
  48. { "black", 0, 0, 0 },
  49. { "blanchedalmond", 255, 235, 205 },
  50. { "blue", 0, 0, 255 },
  51. { "blueviolet", 138, 43, 226 },
  52. { "brown", 165, 42, 42 },
  53. { "burlywood", 222, 184, 135 },
  54. { "cadetblue", 95, 158, 160 },
  55. { "chartreuse", 127, 255, 0 },
  56. { "chocolate", 210, 105, 30 },
  57. { "coral", 255, 127, 80 },
  58. { "cornflowerblue", 100, 149, 237 },
  59. { "cornsilk", 255, 248, 220 },
  60. { "crimson", 220, 20, 60 },
  61. { "cyan", 0, 255, 255 },
  62. { "darkblue", 0, 0, 139 },
  63. { "darkcyan", 0, 139, 139 },
  64. { "darkgoldenrod", 184, 134, 11 },
  65. { "darkgray", 169, 169, 169 },
  66. { "darkgreen", 0, 100, 0 },
  67. { "darkgrey", 169, 169, 169 },
  68. { "darkkhaki", 189, 183, 107 },
  69. { "darkmagenta", 139, 0, 139 },
  70. { "darkolivegreen", 85, 107, 47 },
  71. { "darkorange", 255, 140, 0 },
  72. { "darkorchid", 153, 50, 204 },
  73. { "darkred", 139, 0, 0 },
  74. { "darksalmon", 233, 150, 122 },
  75. { "darkseagreen", 143, 188, 143 },
  76. { "darkslateblue", 72, 61, 139 },
  77. { "darkslategray", 47, 79, 79 },
  78. { "darkslategrey", 47, 79, 79 },
  79. { "darkturquoise", 0, 206, 209 },
  80. { "darkviolet", 148, 0, 211 },
  81. { "deeppink", 255, 20, 147 },
  82. { "deepskyblue", 0, 191, 255 },
  83. { "dimgray", 105, 105, 105 },
  84. { "dimgrey", 105, 105, 105 },
  85. { "dodgerblue", 30, 144, 255 },
  86. { "firebrick", 178, 34, 34 },
  87. { "floralwhite", 255, 250, 240 },
  88. { "forestgreen", 34, 139, 34 },
  89. { "fuchsia", 255, 0, 255 },
  90. { "gainsboro", 220, 220, 220 },
  91. { "ghostwhite", 248, 248, 255 },
  92. { "gold", 255, 215, 0 },
  93. { "goldenrod", 218, 165, 32 },
  94. { "gray", 128, 128, 128 },
  95. { "green", 0, 128, 0 },
  96. { "greenyellow", 173, 255, 47 },
  97. { "grey", 128, 128, 128 },
  98. { "honeydew", 240, 255, 240 },
  99. { "hotpink", 255, 105, 180 },
  100. { "indianred", 205, 92, 92 },
  101. { "indigo", 75, 0, 130 },
  102. { "ivory", 255, 255, 240 },
  103. { "khaki", 240, 230, 140 },
  104. { "lavender", 230, 230, 250 },
  105. { "lavenderblush", 255, 240, 245 },
  106. { "lawngreen", 124, 252, 0 },
  107. { "lemonchiffon", 255, 250, 205 },
  108. { "lightblue", 173, 216, 230 },
  109. { "lightcoral", 240, 128, 128 },
  110. { "lightcyan", 224, 255, 255 },
  111. { "lightgoldenrodyellow", 250, 250, 210 },
  112. { "lightgray", 211, 211, 211 },
  113. { "lightgreen", 144, 238, 144 },
  114. { "lightgrey", 211, 211, 211 },
  115. { "lightpink", 255, 182, 193 },
  116. { "lightsalmon", 255, 160, 122 },
  117. { "lightseagreen", 32, 178, 170 },
  118. { "lightskyblue", 135, 206, 250 },
  119. { "lightslategray", 119, 136, 153 },
  120. { "lightslategrey", 119, 136, 153 },
  121. { "lightsteelblue", 176, 196, 222 },
  122. { "lightyellow", 255, 255, 224 },
  123. { "lime", 0, 255, 0 },
  124. { "limegreen", 50, 205, 50 },
  125. { "linen", 250, 240, 230 },
  126. { "magenta", 255, 0, 255 },
  127. { "maroon", 128, 0, 0 },
  128. { "mediumaquamarine", 102, 205, 170 },
  129. { "mediumblue", 0, 0, 205 },
  130. { "mediumorchid", 186, 85, 211 },
  131. { "mediumpurple", 147, 112, 219 },
  132. { "mediumseagreen", 60, 179, 113 },
  133. { "mediumslateblue", 123, 104, 238 },
  134. { "mediumspringgreen", 0, 250, 154 },
  135. { "mediumturquoise", 72, 209, 204 },
  136. { "mediumvioletred", 199, 21, 133 },
  137. { "midnightblue", 25, 25, 112 },
  138. { "mintcream", 245, 255, 250 },
  139. { "mistyrose", 255, 228, 225 },
  140. { "moccasin", 255, 228, 181 },
  141. { "navajowhite", 255, 222, 173 },
  142. { "navy", 0, 0, 128 },
  143. { "oldlace", 253, 245, 230 },
  144. { "olive", 128, 128, 0 },
  145. { "olivedrab", 107, 142, 35 },
  146. { "orange", 255, 165, 0 },
  147. { "orangered", 255, 69, 0 },
  148. { "orchid", 218, 112, 214 },
  149. { "palegoldenrod", 238, 232, 170 },
  150. { "palegreen", 152, 251, 152 },
  151. { "paleturquoise", 175, 238, 238 },
  152. { "palevioletred", 219, 112, 147 },
  153. { "papayawhip", 255, 239, 213 },
  154. { "peachpuff", 255, 218, 185 },
  155. { "peru", 205, 133, 63 },
  156. { "pink", 255, 192, 203 },
  157. { "plum", 221, 160, 221 },
  158. { "powderblue", 176, 224, 230 },
  159. { "purple", 128, 0, 128 },
  160. { "red", 255, 0, 0 },
  161. { "rosybrown", 188, 143, 143 },
  162. { "royalblue", 65, 105, 225 },
  163. { "saddlebrown", 139, 69, 19 },
  164. { "salmon", 250, 128, 114 },
  165. { "sandybrown", 244, 164, 96 },
  166. { "seagreen", 46, 139, 87 },
  167. { "seashell", 255, 245, 238 },
  168. { "sienna", 160, 82, 45 },
  169. { "silver", 192, 192, 192 },
  170. { "skyblue", 135, 206, 235 },
  171. { "slateblue", 106, 90, 205 },
  172. { "slategray", 112, 128, 144 },
  173. { "slategrey", 112, 128, 144 },
  174. { "snow", 255, 250, 250 },
  175. { "springgreen", 0, 255, 127 },
  176. { "steelblue", 70, 130, 180 },
  177. { "tan", 210, 180, 140 },
  178. { "teal", 0, 128, 128 },
  179. { "thistle", 216, 191, 216 },
  180. { "tomato", 255, 99, 71 },
  181. { "turquoise", 64, 224, 208 },
  182. { "violet", 238, 130, 238 },
  183. { "wheat", 245, 222, 179 },
  184. { "white", 255, 255, 255 },
  185. { "whitesmoke", 245, 245, 245 },
  186. { "yellow", 255, 255, 0 },
  187. { "yellowgreen", 154, 205, 50 },
  188. };
  189. static int unhex(int chr)
  190. {
  191. const char *hextable = "0123456789abcdef";
  192. return strchr(hextable, (chr|32)) - hextable;
  193. }
  194. static int ishex(int chr)
  195. {
  196. if (chr >= '0' && chr <= '9') return 1;
  197. if (chr >= 'A' && chr <= 'F') return 1;
  198. if (chr >= 'a' && chr <= 'f') return 1;
  199. return 0;
  200. }
  201. void
  202. svg_parse_color(fz_context *ctx, svg_document *doc, const char *str, float *rgb)
  203. {
  204. int i, l, m, r, cmp;
  205. size_t n;
  206. rgb[0] = 0.0f;
  207. rgb[1] = 0.0f;
  208. rgb[2] = 0.0f;
  209. /* Crack hex-coded RGB */
  210. if (str[0] == '#')
  211. {
  212. str ++;
  213. n = strlen(str);
  214. if (n == 3 || (n > 3 && !ishex(str[3])))
  215. {
  216. rgb[0] = (unhex(str[0]) * 16 + unhex(str[0])) / 255.0f;
  217. rgb[1] = (unhex(str[1]) * 16 + unhex(str[1])) / 255.0f;
  218. rgb[2] = (unhex(str[2]) * 16 + unhex(str[2])) / 255.0f;
  219. return;
  220. }
  221. if (n >= 6)
  222. {
  223. rgb[0] = (unhex(str[0]) * 16 + unhex(str[1])) / 255.0f;
  224. rgb[1] = (unhex(str[2]) * 16 + unhex(str[3])) / 255.0f;
  225. rgb[2] = (unhex(str[4]) * 16 + unhex(str[5])) / 255.0f;
  226. return;
  227. }
  228. return;
  229. }
  230. /* rgb(X,Y,Z) -- whitespace allowed around numbers */
  231. else if (strstr(str, "rgb("))
  232. {
  233. int numberlen = 0;
  234. char numberbuf[50];
  235. str = str + 4;
  236. for (i = 0; i < 3; i++)
  237. {
  238. while (svg_is_whitespace_or_comma(*str))
  239. str ++;
  240. if (svg_is_digit(*str))
  241. {
  242. numberlen = 0;
  243. while (svg_is_digit(*str) && numberlen < (int)sizeof(numberbuf) - 1)
  244. numberbuf[numberlen++] = *str++;
  245. numberbuf[numberlen] = 0;
  246. if (*str == '%')
  247. {
  248. str ++;
  249. rgb[i] = fz_atof(numberbuf) / 100.0f;
  250. }
  251. else
  252. {
  253. rgb[i] = fz_atof(numberbuf) / 255.0f;
  254. }
  255. }
  256. }
  257. return;
  258. }
  259. /* TODO: parse icc-profile(X,Y,Z,W) syntax */
  260. /* Search for a pre-defined color */
  261. else
  262. {
  263. char keyword[50], *p;
  264. fz_strlcpy(keyword, str, sizeof keyword);
  265. p = keyword;
  266. while (*p && *p >= 'a' && *p <= 'z')
  267. ++p;
  268. *p = 0;
  269. l = 0;
  270. r = nelem(svg_predefined_colors) - 1;
  271. while (l <= r)
  272. {
  273. m = (l + r) / 2;
  274. cmp = strcmp(svg_predefined_colors[m].name, keyword);
  275. if (cmp > 0)
  276. r = m - 1;
  277. else if (cmp < 0)
  278. l = m + 1;
  279. else
  280. {
  281. rgb[0] = svg_predefined_colors[m].red / 255.0f;
  282. rgb[1] = svg_predefined_colors[m].green / 255.0f;
  283. rgb[2] = svg_predefined_colors[m].blue / 255.0f;
  284. return;
  285. }
  286. }
  287. }
  288. }
  289. static int
  290. svg_parse_color_from_style_string(fz_context *ctx, svg_document *doc, const char *p, float color[3])
  291. {
  292. char buf[100], *e;
  293. while (*p && svg_is_whitespace(*p))
  294. ++p;
  295. fz_strlcpy(buf, p, sizeof buf);
  296. e = strchr(buf, ';');
  297. if (e)
  298. *e = 0;
  299. if (!strcmp(buf, "none"))
  300. return 0;
  301. svg_parse_color(ctx, doc, buf, color);
  302. return 1;
  303. }
  304. void
  305. svg_parse_color_from_style(fz_context *ctx, svg_document *doc, const char *str,
  306. int *fill_is_set, float fill[3],
  307. int *stroke_is_set, float stroke[3])
  308. {
  309. const char *p;
  310. p = strstr(str, "fill:");
  311. if (p)
  312. *fill_is_set = svg_parse_color_from_style_string(ctx, doc, p+5, fill);
  313. p = strstr(str, "stroke:");
  314. if (p)
  315. *stroke_is_set = svg_parse_color_from_style_string(ctx, doc, p+7, stroke);
  316. }