pdf-features.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-pdf.h>
  29. /* This test exists to test the various features of cairo-pdf.h.
  30. *
  31. * Currently, this test exercises the following function calls:
  32. *
  33. * cairo_pdf_surface_set_size
  34. */
  35. #define INCHES_TO_POINTS(in) ((in) * 72.0)
  36. #define MM_TO_POINTS(mm) ((mm) / 25.4 * 72.0)
  37. #define TEXT_SIZE 12
  38. #define BASENAME "pdf-features.out"
  39. static struct {
  40. const char *page_size;
  41. const char *page_size_alias;
  42. const char *orientation;
  43. double width_in_points;
  44. double height_in_points;
  45. } pages[] = {
  46. {"na_letter_8.5x11in", "letter", "portrait",
  47. INCHES_TO_POINTS(8.5), INCHES_TO_POINTS(11)},
  48. {"na_letter_8.5x11in", "letter", "landscape",
  49. INCHES_TO_POINTS(11), INCHES_TO_POINTS(8.5)},
  50. {"iso_a4_210x297mm", "a4", "portrait",
  51. MM_TO_POINTS(210), MM_TO_POINTS(297)},
  52. {"iso_a4_210x297mm", "a4", "landscape",
  53. MM_TO_POINTS(297), MM_TO_POINTS(210)},
  54. {"iso_a5_148x210mm", "a5", "portrait",
  55. MM_TO_POINTS(148), MM_TO_POINTS(210)},
  56. {"iso_a5_148x210mm", "a5", "landscape",
  57. MM_TO_POINTS(210), MM_TO_POINTS(148)},
  58. {"iso_a6_105x148mm", "a6", "portrait",
  59. MM_TO_POINTS(105), MM_TO_POINTS(148)},
  60. {"iso_a6_105x148mm", "a6", "landscape",
  61. MM_TO_POINTS(148), MM_TO_POINTS(105)},
  62. {"iso_a7_74x105mm", "a7", "portrait",
  63. MM_TO_POINTS(74), MM_TO_POINTS(105)},
  64. {"iso_a7_74x105mm", "a7", "landscape",
  65. MM_TO_POINTS(105), MM_TO_POINTS(74)},
  66. {"iso_a8_52x74mm", "a8", "portrait",
  67. MM_TO_POINTS(52), MM_TO_POINTS(74)},
  68. {"iso_a8_52x74mm", "a8", "landscape",
  69. MM_TO_POINTS(74), MM_TO_POINTS(52)},
  70. {"iso_a9_37x52mm", "a9", "portrait",
  71. MM_TO_POINTS(37), MM_TO_POINTS(52)},
  72. {"iso_a9_37x52mm", "a9", "landscape",
  73. MM_TO_POINTS(52), MM_TO_POINTS(37)},
  74. {"iso_a10_26x37mm", "a10", "portrait",
  75. MM_TO_POINTS(26), MM_TO_POINTS(37)},
  76. {"iso_a10_26x37mm", "a10", "landscape",
  77. MM_TO_POINTS(37), MM_TO_POINTS(26)}
  78. };
  79. static cairo_test_status_t
  80. preamble (cairo_test_context_t *ctx)
  81. {
  82. cairo_surface_t *surface;
  83. cairo_t *cr;
  84. cairo_status_t status;
  85. size_t i;
  86. char *filename;
  87. const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
  88. if (! cairo_test_is_target_enabled (ctx, "pdf"))
  89. return CAIRO_TEST_UNTESTED;
  90. xasprintf (&filename, "%s/%s.pdf", path, BASENAME);
  91. /* The initial size passed here is the default size that will be
  92. * inheritable by each page. That is, any page for which this
  93. * initial size applies will not have its own /MediaBox entry in
  94. * its dictionary. */
  95. surface = cairo_pdf_surface_create (filename,
  96. INCHES_TO_POINTS(8.5),
  97. INCHES_TO_POINTS(11));
  98. cr = cairo_create (surface);
  99. cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
  100. CAIRO_FONT_SLANT_NORMAL,
  101. CAIRO_FONT_WEIGHT_NORMAL);
  102. cairo_set_font_size (cr, TEXT_SIZE);
  103. for (i = 0; i < ARRAY_LENGTH (pages); i++) {
  104. cairo_pdf_surface_set_size (surface,
  105. pages[i].width_in_points,
  106. pages[i].height_in_points);
  107. cairo_move_to (cr, TEXT_SIZE, TEXT_SIZE);
  108. cairo_show_text (cr, pages[i].page_size);
  109. cairo_show_text (cr, " - ");
  110. cairo_show_text (cr, pages[i].orientation);
  111. cairo_show_page (cr);
  112. }
  113. status = cairo_status (cr);
  114. cairo_destroy (cr);
  115. cairo_surface_destroy (surface);
  116. free (filename);
  117. if (status) {
  118. cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
  119. filename, cairo_status_to_string (status));
  120. return CAIRO_TEST_FAILURE;
  121. }
  122. printf ("pdf-features: Please check %s to ensure it looks/prints correctly.\n", filename);
  123. return CAIRO_TEST_SUCCESS;
  124. }
  125. CAIRO_TEST (pdf_features,
  126. "Check PDF specific API",
  127. "pdf", /* keywords */
  128. NULL, /* requirements */
  129. 0, 0,
  130. preamble, NULL)