fallback-resolution.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright © 2006 Red Hat, Inc.
  3. * Copyright © 2008 Chris Wilson
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software
  6. * and its documentation for any purpose is hereby granted without
  7. * fee, provided that the above copyright notice appear in all copies
  8. * and that both that copyright notice and this permission notice
  9. * appear in supporting documentation, and that the name of
  10. * Red Hat, Inc. not be used in advertising or publicity pertaining to
  11. * distribution of the software without specific, written prior
  12. * permission. Red Hat, Inc. makes no representations about the
  13. * suitability of this software for any purpose. It is provided "as
  14. * is" without express or implied warranty.
  15. *
  16. * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  17. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  20. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  21. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  22. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23. *
  24. * Author: Carl D. Worth <cworth@cworth.org>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <cairo.h>
  33. #if CAIRO_HAS_PDF_SURFACE
  34. #include <cairo-pdf.h>
  35. #endif
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #endif
  40. #if HAVE_SYS_STAT_H
  41. #include <sys/stat.h>
  42. #endif
  43. #include "cairo-test.h"
  44. #include "buffer-diff.h"
  45. /* This test exists to test cairo_surface_set_fallback_resolution
  46. *
  47. * <behdad> one more thing.
  48. * if you can somehow incorporate cairo_show_page stuff in the
  49. * test suite. such that fallback-resolution can actually be
  50. * automated..
  51. * if we could get a callback on surface when that function is
  52. * called, we could do cool stuff like making other backends
  53. * draw a long strip of images, one for each page...
  54. */
  55. #define INCHES_TO_POINTS(in) ((in) * 72.0)
  56. #define SIZE INCHES_TO_POINTS(2)
  57. /* cairo_set_tolerance() is not respected by the PS/PDF backends currently */
  58. #define SET_TOLERANCE 0
  59. #define GENERATE_REFERENCE 0
  60. static void
  61. draw (cairo_t *cr, double width, double height)
  62. {
  63. const char *text = "cairo";
  64. cairo_text_extents_t extents;
  65. const double dash[2] = { 8, 16 };
  66. cairo_pattern_t *pattern;
  67. cairo_save (cr);
  68. cairo_new_path (cr);
  69. cairo_set_line_width (cr, .05 * SIZE / 2.0);
  70. cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
  71. 0.875 * SIZE / 2.0,
  72. 0, 2.0 * M_PI);
  73. cairo_stroke (cr);
  74. /* use dashes to demonstrate bugs:
  75. * https://bugs.freedesktop.org/show_bug.cgi?id=9189
  76. * https://bugs.freedesktop.org/show_bug.cgi?id=17223
  77. */
  78. cairo_save (cr);
  79. cairo_set_dash (cr, dash, 2, 0);
  80. cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
  81. 0.75 * SIZE / 2.0,
  82. 0, 2.0 * M_PI);
  83. cairo_stroke (cr);
  84. cairo_restore (cr);
  85. cairo_save (cr);
  86. cairo_rectangle (cr, 0, 0, SIZE/2, SIZE);
  87. cairo_clip (cr);
  88. cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
  89. 0.6 * SIZE / 2.0,
  90. 0, 2.0 * M_PI);
  91. cairo_fill (cr);
  92. cairo_restore (cr);
  93. /* use a pattern to exercise bug:
  94. * https://bugs.launchpad.net/inkscape/+bug/234546
  95. */
  96. cairo_save (cr);
  97. cairo_rectangle (cr, SIZE/2, 0, SIZE/2, SIZE);
  98. cairo_clip (cr);
  99. pattern = cairo_pattern_create_linear (SIZE/2, 0, SIZE, 0);
  100. cairo_pattern_add_color_stop_rgba (pattern, 0, 0, 0, 0, 1.);
  101. cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0, 0.);
  102. cairo_set_source (cr, pattern);
  103. cairo_pattern_destroy (pattern);
  104. cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
  105. 0.6 * SIZE / 2.0,
  106. 0, 2.0 * M_PI);
  107. cairo_fill (cr);
  108. cairo_restore (cr);
  109. cairo_set_source_rgb (cr, 1, 1, 1); /* white */
  110. cairo_set_font_size (cr, .25 * SIZE / 2.0);
  111. cairo_text_extents (cr, text, &extents);
  112. cairo_move_to (cr, (SIZE-extents.width)/2.0-extents.x_bearing,
  113. (SIZE-extents.height)/2.0-extents.y_bearing);
  114. cairo_show_text (cr, text);
  115. cairo_restore (cr);
  116. }
  117. static void
  118. _xunlink (const cairo_test_context_t *ctx, const char *pathname)
  119. {
  120. if (unlink (pathname) < 0 && errno != ENOENT) {
  121. cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
  122. pathname, strerror (errno));
  123. exit (1);
  124. }
  125. }
  126. static cairo_bool_t
  127. check_result (cairo_test_context_t *ctx,
  128. const cairo_boilerplate_target_t *target,
  129. const char *test_name,
  130. const char *base_name,
  131. cairo_surface_t *surface)
  132. {
  133. const char *format;
  134. char *ref_name;
  135. char *png_name;
  136. char *diff_name;
  137. cairo_surface_t *test_image, *ref_image, *diff_image;
  138. buffer_diff_result_t result;
  139. cairo_status_t status;
  140. cairo_bool_t ret;
  141. /* XXX log target, OUTPUT, REFERENCE, DIFFERENCE for index.html */
  142. if (target->finish_surface != NULL) {
  143. status = target->finish_surface (surface);
  144. if (status) {
  145. cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
  146. cairo_status_to_string (status));
  147. cairo_surface_destroy (surface);
  148. return FALSE;
  149. }
  150. }
  151. xasprintf (&png_name, "%s.out.png", base_name);
  152. xasprintf (&diff_name, "%s.diff.png", base_name);
  153. test_image = target->get_image_surface (surface, 0, SIZE, SIZE);
  154. if (cairo_surface_status (test_image)) {
  155. cairo_test_log (ctx, "Error: Failed to extract page: %s\n",
  156. cairo_status_to_string (cairo_surface_status (test_image)));
  157. cairo_surface_destroy (test_image);
  158. free (png_name);
  159. free (diff_name);
  160. return FALSE;
  161. }
  162. _xunlink (ctx, png_name);
  163. status = cairo_surface_write_to_png (test_image, png_name);
  164. if (status) {
  165. cairo_test_log (ctx, "Error: Failed to write output image: %s\n",
  166. cairo_status_to_string (status));
  167. cairo_surface_destroy (test_image);
  168. free (png_name);
  169. free (diff_name);
  170. return FALSE;
  171. }
  172. format = cairo_boilerplate_content_name (target->content);
  173. ref_name = cairo_test_reference_filename (ctx,
  174. base_name,
  175. test_name,
  176. target->name,
  177. target->basename,
  178. format,
  179. CAIRO_TEST_REF_SUFFIX,
  180. CAIRO_TEST_PNG_EXTENSION);
  181. if (ref_name == NULL) {
  182. cairo_test_log (ctx, "Error: Cannot find reference image for %s\n",
  183. base_name);
  184. cairo_surface_destroy (test_image);
  185. free (png_name);
  186. free (diff_name);
  187. return FALSE;
  188. }
  189. ref_image = cairo_test_get_reference_image (ctx, ref_name,
  190. target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
  191. if (cairo_surface_status (ref_image)) {
  192. cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
  193. ref_name,
  194. cairo_status_to_string (cairo_surface_status (ref_image)));
  195. cairo_surface_destroy (ref_image);
  196. cairo_surface_destroy (test_image);
  197. free (png_name);
  198. free (diff_name);
  199. free (ref_name);
  200. return FALSE;
  201. }
  202. diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
  203. SIZE, SIZE);
  204. ret = TRUE;
  205. status = image_diff (ctx,
  206. test_image, ref_image, diff_image,
  207. &result);
  208. _xunlink (ctx, diff_name);
  209. if (status) {
  210. cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
  211. cairo_status_to_string (status));
  212. ret = FALSE;
  213. } else if (image_diff_is_failure (&result, target->error_tolerance))
  214. {
  215. ret = FALSE;
  216. status = cairo_surface_write_to_png (diff_image, diff_name);
  217. if (status) {
  218. cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
  219. cairo_status_to_string (status));
  220. }
  221. }
  222. cairo_surface_destroy (test_image);
  223. cairo_surface_destroy (diff_image);
  224. free (png_name);
  225. free (diff_name);
  226. free (ref_name);
  227. return ret;
  228. }
  229. #if GENERATE_REFERENCE
  230. static void
  231. generate_reference (double ppi_x, double ppi_y, const char *filename)
  232. {
  233. cairo_surface_t *surface, *target;
  234. cairo_t *cr;
  235. cairo_status_t status;
  236. surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
  237. SIZE*ppi_x/72, SIZE*ppi_y/72);
  238. cr = cairo_create (surface);
  239. cairo_surface_destroy (surface);
  240. /* As we wish to mimic a PDF surface, copy across the default font options
  241. * from the PDF backend.
  242. */
  243. {
  244. cairo_surface_t *pdf;
  245. cairo_font_options_t *options;
  246. options = cairo_font_options_create ();
  247. #if CAIRO_HAS_PDF_SURFACE
  248. pdf = cairo_pdf_surface_create ("tmp.pdf", 1, 1);
  249. cairo_surface_get_font_options (pdf, options);
  250. cairo_surface_destroy (pdf);
  251. #endif
  252. cairo_set_font_options (cr, options);
  253. cairo_font_options_destroy (options);
  254. }
  255. #if SET_TOLERANCE
  256. cairo_set_tolerance (cr, 3.0);
  257. #endif
  258. cairo_save (cr); {
  259. cairo_set_source_rgb (cr, 1, 1, 1);
  260. cairo_paint (cr);
  261. } cairo_restore (cr);
  262. cairo_scale (cr, ppi_x/72., ppi_y/72.);
  263. draw (cr, SIZE, SIZE);
  264. surface = cairo_surface_reference (cairo_get_target (cr));
  265. cairo_destroy (cr);
  266. target = cairo_image_surface_create (CAIRO_FORMAT_RGB24, SIZE, SIZE);
  267. cr = cairo_create (target);
  268. cairo_scale (cr, 72./ppi_x, 72./ppi_y);
  269. cairo_set_source_surface (cr, surface, 0, 0);
  270. cairo_paint (cr);
  271. status = cairo_surface_write_to_png (cairo_get_target (cr), filename);
  272. cairo_destroy (cr);
  273. if (status) {
  274. fprintf (stderr, "Failed to generate reference image '%s': %s\n",
  275. filename, cairo_status_to_string (status));
  276. exit (1);
  277. }
  278. }
  279. #endif
  280. /* TODO: Split each ppi case out to its own CAIRO_TEST() test case */
  281. static cairo_test_status_t
  282. preamble (cairo_test_context_t *ctx)
  283. {
  284. cairo_t *cr;
  285. cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
  286. struct {
  287. double x, y;
  288. } ppi[] = {
  289. { 576, 576 },
  290. { 576, 72 },
  291. { 288, 288 },
  292. { 288, 72 },
  293. { 144, 144 },
  294. { 144, 72 },
  295. { 72, 576 },
  296. { 72, 288 },
  297. { 72, 144 },
  298. { 72, 72 },
  299. };
  300. unsigned int i;
  301. int n, num_ppi;
  302. const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
  303. num_ppi = ARRAY_LENGTH (ppi);
  304. #if GENERATE_REFERENCE
  305. for (n = 0; n < num_ppi; n++) {
  306. char *ref_name;
  307. xasprintf (&ref_name, "reference/fallback-resolution.ppi%gx%g.ref.png",
  308. ppi[n].x, ppi[n].y);
  309. generate_reference (ppi[n].x, ppi[n].y, ref_name);
  310. free (ref_name);
  311. }
  312. #endif
  313. for (i = 0; i < ctx->num_targets; i++) {
  314. const cairo_boilerplate_target_t *target = ctx->targets_to_test[i];
  315. cairo_surface_t *surface = NULL;
  316. char *base_name;
  317. void *closure;
  318. const char *format;
  319. cairo_status_t status;
  320. if (! target->is_vector)
  321. continue;
  322. if (! cairo_test_is_target_enabled (ctx, target->name))
  323. continue;
  324. format = cairo_boilerplate_content_name (target->content);
  325. xasprintf (&base_name, "%s/fallback-resolution.%s.%s",
  326. path, target->name,
  327. format);
  328. surface = (target->create_surface) (base_name,
  329. target->content,
  330. SIZE, SIZE,
  331. SIZE, SIZE,
  332. CAIRO_BOILERPLATE_MODE_TEST,
  333. &closure);
  334. if (surface == NULL) {
  335. free (base_name);
  336. continue;
  337. }
  338. if (ret == CAIRO_TEST_UNTESTED)
  339. ret = CAIRO_TEST_SUCCESS;
  340. cairo_surface_destroy (surface);
  341. if (target->cleanup)
  342. target->cleanup (closure);
  343. free (base_name);
  344. /* we need to recreate the surface for each resolution as we include
  345. * SVG in testing which does not support the paginated interface.
  346. */
  347. for (n = 0; n < num_ppi; n++) {
  348. char *test_name;
  349. cairo_bool_t pass;
  350. xasprintf (&test_name, "fallback-resolution.ppi%gx%g",
  351. ppi[n].x, ppi[n].y);
  352. xasprintf (&base_name, "%s/%s.%s.%s",
  353. path, test_name,
  354. target->name,
  355. format);
  356. surface = (target->create_surface) (base_name,
  357. target->content,
  358. SIZE + 25, SIZE + 25,
  359. SIZE + 25, SIZE + 25,
  360. CAIRO_BOILERPLATE_MODE_TEST,
  361. &closure);
  362. if (surface == NULL || cairo_surface_status (surface)) {
  363. cairo_test_log (ctx, "Failed to generate surface: %s.%s\n",
  364. target->name,
  365. format);
  366. free (base_name);
  367. free (test_name);
  368. ret = CAIRO_TEST_FAILURE;
  369. continue;
  370. }
  371. cairo_test_log (ctx,
  372. "Testing fallback-resolution %gx%g with %s target\n",
  373. ppi[n].x, ppi[n].y, target->name);
  374. printf ("%s:\t", base_name);
  375. fflush (stdout);
  376. if (target->force_fallbacks != NULL)
  377. target->force_fallbacks (surface, ppi[n].x, ppi[n].y);
  378. cr = cairo_create (surface);
  379. #if SET_TOLERANCE
  380. cairo_set_tolerance (cr, 3.0);
  381. #endif
  382. cairo_surface_set_device_offset (surface, 25, 25);
  383. cairo_save (cr); {
  384. cairo_set_source_rgb (cr, 1, 1, 1);
  385. cairo_paint (cr);
  386. } cairo_restore (cr);
  387. /* First draw the top half in a conventional way. */
  388. cairo_save (cr); {
  389. cairo_rectangle (cr, 0, 0, SIZE, SIZE / 2.0);
  390. cairo_clip (cr);
  391. draw (cr, SIZE, SIZE);
  392. } cairo_restore (cr);
  393. /* Then draw the bottom half in a separate group,
  394. * (exposing a bug in 1.6.4 with the group not being
  395. * rendered with the correct fallback resolution). */
  396. cairo_save (cr); {
  397. cairo_rectangle (cr, 0, SIZE / 2.0, SIZE, SIZE / 2.0);
  398. cairo_clip (cr);
  399. cairo_push_group (cr); {
  400. draw (cr, SIZE, SIZE);
  401. } cairo_pop_group_to_source (cr);
  402. cairo_paint (cr);
  403. } cairo_restore (cr);
  404. status = cairo_status (cr);
  405. cairo_destroy (cr);
  406. pass = FALSE;
  407. if (status) {
  408. cairo_test_log (ctx, "Error: Failed to create target surface: %s\n",
  409. cairo_status_to_string (status));
  410. ret = CAIRO_TEST_FAILURE;
  411. } else {
  412. /* extract the image and compare it to our reference */
  413. if (! check_result (ctx, target, test_name, base_name, surface))
  414. ret = CAIRO_TEST_FAILURE;
  415. else
  416. pass = TRUE;
  417. }
  418. cairo_surface_destroy (surface);
  419. if (target->cleanup)
  420. target->cleanup (closure);
  421. free (base_name);
  422. free (test_name);
  423. if (pass) {
  424. printf ("PASS\n");
  425. } else {
  426. printf ("FAIL\n");
  427. }
  428. fflush (stdout);
  429. }
  430. }
  431. return ret;
  432. }
  433. CAIRO_TEST (fallback_resolution,
  434. "Check handling of fallback resolutions",
  435. "fallback", /* keywords */
  436. NULL, /* requirements */
  437. 0, 0,
  438. preamble, NULL)