svg-parse.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #include <math.h>
  26. int svg_is_whitespace_or_comma(int c)
  27. {
  28. return (c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA) || (c == ',');
  29. }
  30. int svg_is_whitespace(int c)
  31. {
  32. return (c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA);
  33. }
  34. int svg_is_alpha(int c)
  35. {
  36. return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
  37. }
  38. int svg_is_digit(int c)
  39. {
  40. return (c >= '0' && c <= '9') ||
  41. (c == 'e') || (c == 'E') ||
  42. (c == '+') || (c == '-') || (c == '.');
  43. }
  44. const char *
  45. svg_lex_number(float *fp, const char *ss)
  46. {
  47. const char *s = ss;
  48. if (*s == '+' || *s == '-')
  49. ++s;
  50. while (*s >= '0' && *s <= '9')
  51. ++s;
  52. if (*s == '.') {
  53. ++s;
  54. while (*s >= '0' && *s <= '9')
  55. ++s;
  56. }
  57. if (*s == 'e' || *s == 'E') {
  58. ++s;
  59. if (*s == '+' || *s == '-')
  60. ++s;
  61. while (*s >= '0' && *s <= '9')
  62. ++s;
  63. }
  64. *fp = fz_atof(ss);
  65. return s;
  66. }
  67. float
  68. svg_parse_number(const char *str, float min, float max, float inherit)
  69. {
  70. float x;
  71. if (!strcmp(str, "inherit"))
  72. return inherit;
  73. x = fz_atof(str);
  74. if (x < min) return min;
  75. if (x > max) return max;
  76. return x;
  77. }
  78. float
  79. svg_parse_length(const char *str, float percent, float font_size)
  80. {
  81. char *end;
  82. float val;
  83. val = fz_strtof(str, &end);
  84. if (end == str)
  85. return 0; /* failed */
  86. if (!strcmp(end, "px")) return val;
  87. if (!strcmp(end, "pt")) return val * 1.0f;
  88. if (!strcmp(end, "pc")) return val * 12.0f;
  89. if (!strcmp(end, "mm")) return val * 2.83464567f;
  90. if (!strcmp(end, "cm")) return val * 28.3464567f;
  91. if (!strcmp(end, "in")) return val * 72.0f;
  92. if (!strcmp(end, "em")) return val * font_size;
  93. if (!strcmp(end, "ex")) return val * font_size * 0.5f;
  94. if (!strcmp(end, "%"))
  95. return val * percent * 0.01f;
  96. if (end[0] == 0)
  97. return val;
  98. return 0;
  99. }
  100. /* Return angle in degrees */
  101. float
  102. svg_parse_angle(const char *str)
  103. {
  104. char *end;
  105. float val;
  106. val = fz_strtof(str, &end);
  107. if (end == str)
  108. return 0; /* failed */
  109. if (!strcmp(end, "deg"))
  110. return val;
  111. if (!strcmp(end, "grad"))
  112. return val * 0.9f;
  113. if (!strcmp(end, "rad"))
  114. return val * FZ_RADIAN;
  115. return val;
  116. }
  117. /* Coordinate transformations */
  118. fz_matrix
  119. svg_parse_transform(fz_context *ctx, svg_document *doc, const char *str, fz_matrix transform)
  120. {
  121. char keyword[20];
  122. int keywordlen;
  123. float args[6];
  124. int nargs;
  125. nargs = 0;
  126. keywordlen = 0;
  127. while (*str)
  128. {
  129. while (svg_is_whitespace_or_comma(*str))
  130. str ++;
  131. if (*str == 0)
  132. break;
  133. /*
  134. * Parse keyword and opening parenthesis.
  135. */
  136. keywordlen = 0;
  137. while (svg_is_alpha(*str) && keywordlen < (int)sizeof(keyword) - 1)
  138. keyword[keywordlen++] = *str++;
  139. keyword[keywordlen] = 0;
  140. if (keywordlen == 0)
  141. fz_throw(ctx, FZ_ERROR_SYNTAX, "expected keyword in transform attribute");
  142. while (svg_is_whitespace(*str))
  143. str ++;
  144. if (*str != '(')
  145. fz_throw(ctx, FZ_ERROR_SYNTAX, "expected opening parenthesis in transform attribute");
  146. str ++;
  147. /*
  148. * Parse list of numbers until closing parenthesis
  149. */
  150. nargs = 0;
  151. while (*str && *str != ')' && nargs < 6)
  152. {
  153. while (svg_is_whitespace_or_comma(*str))
  154. str ++;
  155. if (svg_is_digit(*str))
  156. str = svg_lex_number(&args[nargs++], str);
  157. else
  158. break;
  159. }
  160. if (*str != ')')
  161. fz_throw(ctx, FZ_ERROR_SYNTAX, "expected closing parenthesis in transform attribute");
  162. str ++;
  163. /*
  164. * Execute the transform.
  165. */
  166. if (!strcmp(keyword, "matrix"))
  167. {
  168. if (nargs != 6)
  169. fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to matrix(): %d", nargs);
  170. transform = fz_concat(fz_make_matrix(args[0], args[1], args[2], args[3], args[4], args[5]), transform);
  171. }
  172. else if (!strcmp(keyword, "translate"))
  173. {
  174. if (nargs == 1)
  175. transform = fz_concat(fz_translate(args[0], 0), transform);
  176. else if (nargs == 2)
  177. transform = fz_concat(fz_translate(args[0], args[1]), transform);
  178. else
  179. fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to translate(): %d", nargs);
  180. }
  181. else if (!strcmp(keyword, "scale"))
  182. {
  183. if (nargs == 1)
  184. transform = fz_concat(fz_scale(args[0], args[0]), transform);
  185. else if (nargs == 2)
  186. transform = fz_concat(fz_scale(args[0], args[1]), transform);
  187. else
  188. fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to scale(): %d", nargs);
  189. }
  190. else if (!strcmp(keyword, "rotate"))
  191. {
  192. if (nargs == 1)
  193. transform = fz_concat(fz_rotate(args[0]), transform);
  194. else if (nargs == 3)
  195. {
  196. transform = fz_concat(fz_translate(args[1], args[2]), transform);
  197. transform = fz_concat(fz_rotate(args[0]), transform);
  198. transform = fz_concat(fz_translate(-args[1], -args[2]), transform);
  199. }
  200. else
  201. fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to rotate(): %d", nargs);
  202. }
  203. else if (!strcmp(keyword, "skewX"))
  204. {
  205. if (nargs != 1)
  206. fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewX(): %d", nargs);
  207. transform = fz_concat(fz_make_matrix(1, 0, tanf(args[0] * FZ_DEGREE), 1, 0, 0), transform);
  208. }
  209. else if (!strcmp(keyword, "skewY"))
  210. {
  211. if (nargs != 1)
  212. fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewY(): %d", nargs);
  213. transform = fz_concat(fz_make_matrix(1, tanf(args[0] * FZ_DEGREE), 0, 1, 0, 0), transform);
  214. }
  215. else
  216. {
  217. fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown transform function: %s", keyword);
  218. }
  219. }
  220. return transform;
  221. }
  222. float
  223. svg_parse_number_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att, float number)
  224. {
  225. if (style)
  226. {
  227. char *end, *p = strstr(style, att);
  228. if (p)
  229. {
  230. size_t n = strlen(att);
  231. if (p[n] == ':')
  232. {
  233. p += n + 1;
  234. while (*p && svg_is_whitespace(*p))
  235. ++p;
  236. number = fz_strtof(p, &end);
  237. if (end[0] == 'i' && end[1] == 'n') return number * 72;
  238. if (end[0] == 'c' && end[1] == 'm') return number * 7200 / 254;
  239. if (end[0] == 'm' && end[1] == 'm') return number * 720 / 254;
  240. if (end[0] == 'p' && end[1] == 'c') return number * 12;
  241. }
  242. }
  243. }
  244. return number;
  245. }
  246. int
  247. svg_parse_enum_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att,
  248. int ecount, const char *etable[], int value)
  249. {
  250. char buf[100], *end, *p;
  251. int i;
  252. if (style)
  253. {
  254. p = strstr(style, att);
  255. if (p)
  256. {
  257. size_t n = strlen(att);
  258. if (p[n] == ':')
  259. {
  260. p += n + 1;
  261. while (*p && svg_is_whitespace(*p))
  262. ++p;
  263. fz_strlcpy(buf, p, sizeof buf);
  264. end = strchr(buf, ';');
  265. if (end)
  266. *end = 0;
  267. for (i = 0; i < ecount; ++i)
  268. if (!strcmp(etable[i], buf))
  269. return i;
  270. }
  271. }
  272. }
  273. return value;
  274. }
  275. char *
  276. svg_parse_string_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att,
  277. char *buf, int buf_size, const char *value)
  278. {
  279. char *end, *p, quote;
  280. if (style)
  281. {
  282. p = strstr(style, att);
  283. if (p)
  284. {
  285. size_t n = strlen(att);
  286. if (p[n] == ':')
  287. {
  288. p += n + 1;
  289. while (*p && svg_is_whitespace(*p))
  290. ++p;
  291. quote = *p;
  292. if (quote == '\'' || quote == '"')
  293. {
  294. fz_strlcpy(buf, p+1, buf_size);
  295. end = strchr(buf, quote);
  296. }
  297. else
  298. {
  299. fz_strlcpy(buf, p, buf_size);
  300. end = strchr(buf, ';');
  301. }
  302. if (end)
  303. *end = 0;
  304. return buf;
  305. }
  306. }
  307. }
  308. fz_strlcpy(buf, value, buf_size);
  309. return buf;
  310. }