xps-common.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "xps-imp.h"
  24. #include <string.h>
  25. #include <stdio.h> /* for sscanf */
  26. #include <math.h> /* for pow */
  27. static inline int unhex(int a)
  28. {
  29. if (a >= 'A' && a <= 'F') return a - 'A' + 0xA;
  30. if (a >= 'a' && a <= 'f') return a - 'a' + 0xA;
  31. if (a >= '0' && a <= '9') return a - '0';
  32. return 0;
  33. }
  34. fz_xml *
  35. xps_lookup_alternate_content(fz_context *ctx, xps_document *doc, fz_xml *node)
  36. {
  37. for (node = fz_xml_down(node); node; node = fz_xml_next(node))
  38. {
  39. if (fz_xml_is_tag(node, "Choice") && fz_xml_att(node, "Requires"))
  40. {
  41. char list[64];
  42. char *next = list, *item;
  43. fz_strlcpy(list, fz_xml_att(node, "Requires"), sizeof(list));
  44. while ((item = fz_strsep(&next, " \t\r\n")) != NULL && (!*item || !strcmp(item, "xps")));
  45. if (!item)
  46. return fz_xml_down(node);
  47. }
  48. else if (fz_xml_is_tag(node, "Fallback"))
  49. return fz_xml_down(node);
  50. }
  51. return NULL;
  52. }
  53. void
  54. xps_parse_brush(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, fz_xml *node)
  55. {
  56. if (doc->cookie && doc->cookie->abort)
  57. return;
  58. /* SolidColorBrushes are handled in a special case and will never show up here */
  59. if (fz_xml_is_tag(node, "ImageBrush"))
  60. xps_parse_image_brush(ctx, doc, ctm, area, base_uri, dict, node);
  61. else if (fz_xml_is_tag(node, "VisualBrush"))
  62. xps_parse_visual_brush(ctx, doc, ctm, area, base_uri, dict, node);
  63. else if (fz_xml_is_tag(node, "LinearGradientBrush"))
  64. xps_parse_linear_gradient_brush(ctx, doc, ctm, area, base_uri, dict, node);
  65. else if (fz_xml_is_tag(node, "RadialGradientBrush"))
  66. xps_parse_radial_gradient_brush(ctx, doc, ctm, area, base_uri, dict, node);
  67. else
  68. fz_warn(ctx, "unknown brush tag");
  69. }
  70. void
  71. xps_parse_element(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, fz_xml *node)
  72. {
  73. if (doc->cookie && doc->cookie->abort)
  74. return;
  75. if (fz_xml_is_tag(node, "Path"))
  76. xps_parse_path(ctx, doc, ctm, base_uri, dict, node);
  77. if (fz_xml_is_tag(node, "Glyphs"))
  78. xps_parse_glyphs(ctx, doc, ctm, base_uri, dict, node);
  79. if (fz_xml_is_tag(node, "Canvas"))
  80. xps_parse_canvas(ctx, doc, ctm, area, base_uri, dict, node);
  81. if (fz_xml_is_tag(node, "AlternateContent"))
  82. {
  83. node = xps_lookup_alternate_content(ctx, doc, node);
  84. if (node)
  85. xps_parse_element(ctx, doc, ctm, area, base_uri, dict, node);
  86. }
  87. /* skip unknown tags (like Foo.Resources and similar) */
  88. }
  89. void
  90. xps_begin_opacity(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area,
  91. char *base_uri, xps_resource *dict,
  92. char *opacity_att, fz_xml *opacity_mask_tag)
  93. {
  94. fz_device *dev = doc->dev;
  95. float opacity;
  96. if (!opacity_att && !opacity_mask_tag)
  97. return;
  98. opacity = 1;
  99. if (opacity_att)
  100. opacity = fz_atof(opacity_att);
  101. if (fz_xml_is_tag(opacity_mask_tag, "SolidColorBrush"))
  102. {
  103. char *scb_opacity_att = fz_xml_att(opacity_mask_tag, "Opacity");
  104. char *scb_color_att = fz_xml_att(opacity_mask_tag, "Color");
  105. if (scb_opacity_att)
  106. opacity = opacity * fz_atof(scb_opacity_att);
  107. if (scb_color_att)
  108. {
  109. fz_colorspace *colorspace;
  110. float samples[FZ_MAX_COLORS];
  111. xps_parse_color(ctx, doc, base_uri, scb_color_att, &colorspace, samples);
  112. opacity = opacity * samples[0];
  113. }
  114. opacity_mask_tag = NULL;
  115. }
  116. if (doc->opacity_top + 1 < (int)nelem(doc->opacity))
  117. {
  118. doc->opacity[doc->opacity_top + 1] = doc->opacity[doc->opacity_top] * opacity;
  119. doc->opacity_top++;
  120. }
  121. if (opacity_mask_tag)
  122. {
  123. fz_begin_mask(ctx, dev, area, 0, NULL, NULL, fz_default_color_params);
  124. xps_parse_brush(ctx, doc, ctm, area, base_uri, dict, opacity_mask_tag);
  125. fz_end_mask(ctx, dev);
  126. }
  127. }
  128. void
  129. xps_end_opacity(fz_context *ctx, xps_document *doc, char *base_uri, xps_resource *dict,
  130. char *opacity_att, fz_xml *opacity_mask_tag)
  131. {
  132. fz_device *dev = doc->dev;
  133. if (!opacity_att && !opacity_mask_tag)
  134. return;
  135. if (doc->opacity_top > 0)
  136. doc->opacity_top--;
  137. if (opacity_mask_tag)
  138. {
  139. if (!fz_xml_is_tag(opacity_mask_tag, "SolidColorBrush"))
  140. fz_pop_clip(ctx, dev);
  141. }
  142. }
  143. static fz_matrix
  144. xps_parse_render_transform(fz_context *ctx, xps_document *doc, char *transform)
  145. {
  146. fz_matrix matrix;
  147. float args[6];
  148. char *s = transform;
  149. int i;
  150. args[0] = 1; args[1] = 0;
  151. args[2] = 0; args[3] = 1;
  152. args[4] = 0; args[5] = 0;
  153. for (i = 0; i < 6 && *s; i++)
  154. {
  155. args[i] = fz_atof(s);
  156. while (*s && *s != ',')
  157. s++;
  158. if (*s == ',')
  159. s++;
  160. }
  161. matrix.a = args[0]; matrix.b = args[1];
  162. matrix.c = args[2]; matrix.d = args[3];
  163. matrix.e = args[4]; matrix.f = args[5];
  164. return matrix;
  165. }
  166. static fz_matrix
  167. xps_parse_matrix_transform(fz_context *ctx, xps_document *doc, fz_xml *root)
  168. {
  169. if (fz_xml_is_tag(root, "MatrixTransform"))
  170. {
  171. char *transform = fz_xml_att(root, "Matrix");
  172. if (transform)
  173. return xps_parse_render_transform(ctx, doc, transform);
  174. }
  175. return fz_identity;
  176. }
  177. fz_matrix
  178. xps_parse_transform(fz_context *ctx, xps_document *doc, char *att, fz_xml *tag, fz_matrix ctm)
  179. {
  180. if (att)
  181. return fz_concat(xps_parse_render_transform(ctx, doc, att), ctm);
  182. if (tag)
  183. return fz_concat(xps_parse_matrix_transform(ctx, doc, tag), ctm);
  184. return ctm;
  185. }
  186. fz_rect
  187. xps_parse_rectangle(fz_context *ctx, xps_document *doc, char *text)
  188. {
  189. fz_rect rect;
  190. float args[4];
  191. char *s = text;
  192. int i;
  193. args[0] = 0; args[1] = 0;
  194. args[2] = 1; args[3] = 1;
  195. for (i = 0; i < 4 && *s; i++)
  196. {
  197. args[i] = fz_atof(s);
  198. while (*s && *s != ',')
  199. s++;
  200. if (*s == ',')
  201. s++;
  202. }
  203. rect.x0 = args[0];
  204. rect.y0 = args[1];
  205. rect.x1 = args[0] + args[2];
  206. rect.y1 = args[1] + args[3];
  207. return rect;
  208. }
  209. static int count_commas(char *s)
  210. {
  211. int n = 0;
  212. while (*s)
  213. {
  214. if (*s == ',')
  215. n ++;
  216. s ++;
  217. }
  218. return n;
  219. }
  220. static float sRGB_from_scRGB(float x)
  221. {
  222. if (x < 0.0031308f)
  223. return 12.92f * x;
  224. return 1.055f * powf(x, 1/2.4f) - 0.055f;
  225. }
  226. void
  227. xps_parse_color(fz_context *ctx, xps_document *doc, char *base_uri, char *string,
  228. fz_colorspace **csp, float *samples)
  229. {
  230. char *p;
  231. int i, n;
  232. char buf[1024];
  233. char *profile;
  234. *csp = fz_device_rgb(ctx);
  235. samples[0] = 1;
  236. samples[1] = 0;
  237. samples[2] = 0;
  238. samples[3] = 0;
  239. if (string[0] == '#')
  240. {
  241. size_t z = strlen(string);
  242. if (z == 9)
  243. {
  244. samples[0] = unhex(string[1]) * 16 + unhex(string[2]);
  245. samples[1] = unhex(string[3]) * 16 + unhex(string[4]);
  246. samples[2] = unhex(string[5]) * 16 + unhex(string[6]);
  247. samples[3] = unhex(string[7]) * 16 + unhex(string[8]);
  248. }
  249. else
  250. {
  251. samples[0] = 255;
  252. /* Use a macro to protect against overrunning the string. */
  253. #define UNHEX(idx) (idx < z ? unhex(string[idx]) : 0)
  254. samples[1] = UNHEX(1) * 16 + UNHEX(2);
  255. samples[2] = UNHEX(3) * 16 + UNHEX(4);
  256. samples[3] = UNHEX(5) * 16 + UNHEX(6);
  257. #undef UNHEX
  258. }
  259. samples[0] /= 255;
  260. samples[1] /= 255;
  261. samples[2] /= 255;
  262. samples[3] /= 255;
  263. }
  264. else if (string[0] == 's' && string[1] == 'c' && string[2] == '#')
  265. {
  266. if (count_commas(string) == 2)
  267. sscanf(string, "sc#%g,%g,%g", samples + 1, samples + 2, samples + 3);
  268. if (count_commas(string) == 3)
  269. sscanf(string, "sc#%g,%g,%g,%g", samples, samples + 1, samples + 2, samples + 3);
  270. /* Convert from scRGB gamma 1.0 to sRGB gamma */
  271. samples[1] = sRGB_from_scRGB(samples[1]);
  272. samples[2] = sRGB_from_scRGB(samples[2]);
  273. samples[3] = sRGB_from_scRGB(samples[3]);
  274. }
  275. else if (strstr(string, "ContextColor ") == string)
  276. {
  277. /* Crack the string for profile name and sample values */
  278. fz_strlcpy(buf, string, sizeof buf);
  279. profile = strchr(buf, ' ');
  280. if (!profile)
  281. {
  282. fz_warn(ctx, "cannot find icc profile uri in '%s'", string);
  283. return;
  284. }
  285. *profile++ = 0;
  286. p = strchr(profile, ' ');
  287. if (!p)
  288. {
  289. fz_warn(ctx, "cannot find component values in '%s'", profile);
  290. return;
  291. }
  292. *p++ = 0;
  293. n = count_commas(p) + 1;
  294. if (n > FZ_MAX_COLORS)
  295. {
  296. fz_warn(ctx, "ignoring %d color components (max %d allowed)", n - FZ_MAX_COLORS, FZ_MAX_COLORS);
  297. n = FZ_MAX_COLORS;
  298. }
  299. i = 0;
  300. while (i < n)
  301. {
  302. samples[i++] = fz_atof(p);
  303. p = strchr(p, ',');
  304. if (!p)
  305. break;
  306. p ++;
  307. if (*p == ' ')
  308. p ++;
  309. }
  310. while (i < n)
  311. {
  312. samples[i++] = 0;
  313. }
  314. /* TODO: load ICC profile */
  315. switch (n)
  316. {
  317. case 2: *csp = fz_device_gray(ctx); break;
  318. case 4: *csp = fz_device_rgb(ctx); break;
  319. case 5: *csp = fz_device_cmyk(ctx); break;
  320. default: *csp = fz_device_gray(ctx); break;
  321. }
  322. }
  323. }
  324. void
  325. xps_set_color(fz_context *ctx, xps_document *doc, fz_colorspace *colorspace, float *samples)
  326. {
  327. int i;
  328. int n = fz_colorspace_n(ctx, colorspace);
  329. doc->colorspace = colorspace;
  330. for (i = 0; i < n; i++)
  331. doc->color[i] = samples[i + 1];
  332. doc->alpha = samples[0] * doc->opacity[doc->opacity_top];
  333. }