dash-offset.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
  2. /*
  3. * Copyright 2009 Andrea Canciani
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use, copy,
  9. * modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Author: Andrea Canciani <ranma42@gmail.com>
  26. */
  27. #include "cairo-test.h"
  28. /* Lengths of the dashes of the dash patterns */
  29. static const double dashes[] = { 2, 2, 4, 4 };
  30. /* Dash offset in userspace units
  31. * They always grow by 2, so the dash pattern is
  32. * should be shifted by the same amount each time */
  33. static const double frac_offset[] = { 0, 2, 4, 6 };
  34. /* Dash offset relative to the whole dash pattern
  35. * This corresponds to the non-inverted part only if
  36. * the dash pattern has odd length, so the expected result
  37. * is the same for every int_offset if the pattern has
  38. * even length, and inverted each time (or shifted by half
  39. * period, which is the same) if the pattern has odd length. */
  40. static const double int_offset[] = { -2, -1, 0, 1, 2 };
  41. #define PAD 6
  42. #define STROKE_LENGTH 32
  43. #define IMAGE_WIDTH (PAD + (STROKE_LENGTH + PAD) * ARRAY_LENGTH (dashes))
  44. #define IMAGE_HEIGHT (PAD + PAD * ARRAY_LENGTH (int_offset) + PAD * ARRAY_LENGTH (frac_offset) * ARRAY_LENGTH (int_offset))
  45. static cairo_test_status_t
  46. draw (cairo_t *cr, int width, int height)
  47. {
  48. double total;
  49. size_t i, j, k;
  50. cairo_set_source_rgb (cr, 1, 1, 1);
  51. cairo_paint (cr);
  52. cairo_set_source_rgb (cr, 0, 0, 0);
  53. cairo_set_line_width (cr, 2);
  54. total = 0.0;
  55. for (k = 0; k < ARRAY_LENGTH (dashes); ++k) {
  56. total += dashes[k];
  57. for (i = 0; i < ARRAY_LENGTH (frac_offset); ++i) {
  58. for (j = 0; j < ARRAY_LENGTH (int_offset); ++j) {
  59. cairo_set_dash (cr, dashes, k + 1, frac_offset[i] + total * int_offset[j]);
  60. cairo_move_to (cr, (STROKE_LENGTH + PAD) * k + PAD, PAD * (i + j + ARRAY_LENGTH (frac_offset) * j + 1));
  61. cairo_line_to (cr, (STROKE_LENGTH + PAD) * (k + 1), PAD * (i + j + ARRAY_LENGTH (frac_offset) * j + 1));
  62. cairo_stroke (cr);
  63. }
  64. }
  65. }
  66. return CAIRO_TEST_SUCCESS;
  67. }
  68. CAIRO_TEST (dash_offset,
  69. "Tests dashes of different length with various offsets",
  70. "stroke, dash", /* keywords */
  71. NULL, /* requirements */
  72. IMAGE_WIDTH, IMAGE_HEIGHT,
  73. NULL, draw)