ps-features.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright © 2006 Red Hat, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Red Hat, Inc. not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Red Hat, Inc. makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Carl D. Worth <cworth@cworth.org>
  24. */
  25. #include "cairo-test.h"
  26. #include <stdio.h>
  27. #include <cairo.h>
  28. #include <cairo-ps.h>
  29. /* This test exists to test the various features of cairo-ps.h.
  30. *
  31. * Currently, this test exercises the following function calls:
  32. *
  33. * cairo_ps_surface_set_size
  34. * cairo_ps_surface_dsc_comment
  35. * cairo_ps_surface_dsc_begin_setup
  36. * cairo_ps_surface_dsc_begin_page_setup
  37. */
  38. #define INCHES_TO_POINTS(in) ((in) * 72.0)
  39. #define MM_TO_POINTS(mm) ((mm) / 25.4 * 72.0)
  40. #define TEXT_SIZE 12
  41. #define BASENAME "ps-features.out"
  42. static struct {
  43. const char *page_size;
  44. const char *page_size_alias;
  45. const char *orientation;
  46. double width_in_points;
  47. double height_in_points;
  48. } pages[] = {
  49. {"na_letter_8.5x11in", "letter", "portrait",
  50. INCHES_TO_POINTS(8.5), INCHES_TO_POINTS(11)},
  51. {"na_letter_8.5x11in", "letter", "landscape",
  52. INCHES_TO_POINTS(11), INCHES_TO_POINTS(8.5)},
  53. {"iso_a4_210x297mm", "a4", "portrait",
  54. MM_TO_POINTS(210), MM_TO_POINTS(297)},
  55. {"iso_a4_210x297mm", "a4", "landscape",
  56. MM_TO_POINTS(297), MM_TO_POINTS(210)},
  57. {"iso_a5_148x210mm", "a5", "portrait",
  58. MM_TO_POINTS(148), MM_TO_POINTS(210)},
  59. {"iso_a5_148x210mm", "a5", "landscape",
  60. MM_TO_POINTS(210), MM_TO_POINTS(148)},
  61. {"iso_a6_105x148mm", "a6", "portrait",
  62. MM_TO_POINTS(105), MM_TO_POINTS(148)},
  63. {"iso_a6_105x148mm", "a6", "landscape",
  64. MM_TO_POINTS(148), MM_TO_POINTS(105)},
  65. {"iso_a7_74x105mm", "a7", "portrait",
  66. MM_TO_POINTS(74), MM_TO_POINTS(105)},
  67. {"iso_a7_74x105mm", "a7", "landscape",
  68. MM_TO_POINTS(105), MM_TO_POINTS(74)},
  69. {"iso_a8_52x74mm", "a8", "portrait",
  70. MM_TO_POINTS(52), MM_TO_POINTS(74)},
  71. {"iso_a8_52x74mm", "a8", "landscape",
  72. MM_TO_POINTS(74), MM_TO_POINTS(52)},
  73. {"iso_a9_37x52mm", "a9", "portrait",
  74. MM_TO_POINTS(37), MM_TO_POINTS(52)},
  75. {"iso_a9_37x52mm", "a9", "landscape",
  76. MM_TO_POINTS(52), MM_TO_POINTS(37)},
  77. {"iso_a10_26x37mm", "a10", "portrait",
  78. MM_TO_POINTS(26), MM_TO_POINTS(37)},
  79. {"iso_a10_26x37mm", "a10", "landscape",
  80. MM_TO_POINTS(37), MM_TO_POINTS(26)}
  81. };
  82. static cairo_test_status_t
  83. preamble (cairo_test_context_t *ctx)
  84. {
  85. cairo_surface_t *surface;
  86. cairo_t *cr;
  87. cairo_status_t status;
  88. size_t i;
  89. char dsc[255];
  90. char *filename;
  91. const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
  92. if (! (cairo_test_is_target_enabled (ctx, "ps2") ||
  93. cairo_test_is_target_enabled (ctx, "ps3")))
  94. {
  95. return CAIRO_TEST_UNTESTED;
  96. }
  97. xasprintf (&filename, "%s/%s.ps", path, BASENAME);
  98. /* We demonstrate that the initial size doesn't matter (we're
  99. * passing 0,0), if we use cairo_ps_surface_set_size on the first
  100. * page. */
  101. surface = cairo_ps_surface_create (filename, 0, 0);
  102. cairo_ps_surface_dsc_comment (surface, "%%Title: ps-features");
  103. cairo_ps_surface_dsc_comment (surface, "%%Copyright: Copyright (C) 2006 Red Hat, Inc.");
  104. cairo_ps_surface_dsc_begin_setup (surface);
  105. cairo_ps_surface_dsc_comment (surface, "%%IncludeFeature: *PageSize letter");
  106. cairo_ps_surface_dsc_comment (surface, "%%IncludeFeature: *MediaColor White");
  107. cr = cairo_create (surface);
  108. cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
  109. CAIRO_FONT_SLANT_NORMAL,
  110. CAIRO_FONT_WEIGHT_NORMAL);
  111. cairo_set_font_size (cr, TEXT_SIZE);
  112. for (i = 0; i < ARRAY_LENGTH (pages); i++) {
  113. cairo_ps_surface_set_size (surface,
  114. pages[i].width_in_points,
  115. pages[i].height_in_points);
  116. cairo_ps_surface_dsc_begin_page_setup (surface);
  117. snprintf (dsc, 255, "%%IncludeFeature: *PageSize %s", pages[i].page_size_alias);
  118. cairo_ps_surface_dsc_comment (surface, dsc);
  119. if (i % 2) {
  120. snprintf (dsc, 255, "%%IncludeFeature: *MediaType Glossy");
  121. cairo_ps_surface_dsc_comment (surface, dsc);
  122. }
  123. cairo_move_to (cr, TEXT_SIZE, TEXT_SIZE);
  124. cairo_show_text (cr, pages[i].page_size);
  125. cairo_show_text (cr, " - ");
  126. cairo_show_text (cr, pages[i].orientation);
  127. cairo_show_page (cr);
  128. }
  129. status = cairo_status (cr);
  130. cairo_destroy (cr);
  131. cairo_surface_destroy (surface);
  132. if (status) {
  133. cairo_test_log (ctx, "Failed to create ps surface for file %s: %s\n",
  134. filename, cairo_status_to_string (status));
  135. free (filename);
  136. return CAIRO_TEST_FAILURE;
  137. }
  138. printf ("ps-features: Please check %s to ensure it looks/prints correctly.\n", filename);
  139. free (filename);
  140. return CAIRO_TEST_SUCCESS;
  141. }
  142. CAIRO_TEST (ps_features,
  143. "Check PS specific API",
  144. "ps, api", /* keywords */
  145. NULL, /* requirements */
  146. 0, 0,
  147. preamble, NULL)