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

            
28
#include "cairo-test.h"
29

            
30
/* Lengths of the dashes of the dash patterns */
31
static const double dashes[] = { 2, 2, 4, 4 };
32
/* Dash offset in userspace units
33
 * They always grow by 2, so the dash pattern is
34
 * should be shifted by the same amount each time */
35
static const double frac_offset[] = { 0, 2, 4, 6 };
36
/* Dash offset relative to the whole dash pattern
37
 * This corresponds to the non-inverted part only if
38
 * the dash pattern has odd length, so the expected result
39
 * is the same for every int_offset if the pattern has
40
 * even length, and inverted each time (or shifted by half
41
 * period, which is the same) if the pattern has odd length. */
42
static const double int_offset[] = { -2, -1, 0, 1, 2 };
43

            
44
#define PAD 6
45
#define STROKE_LENGTH 32
46
#define IMAGE_WIDTH (PAD + (STROKE_LENGTH + PAD) * ARRAY_LENGTH (dashes))
47
#define IMAGE_HEIGHT (PAD + PAD * ARRAY_LENGTH (int_offset) + PAD * ARRAY_LENGTH (frac_offset) * ARRAY_LENGTH (int_offset))
48

            
49

            
50
static cairo_test_status_t
51
3
draw (cairo_t *cr, int width, int height)
52
{
53
    double total;
54
    size_t i, j, k;
55

            
56
3
    cairo_set_source_rgb (cr, 1, 1, 1);
57
3
    cairo_paint (cr);
58

            
59
3
    cairo_set_source_rgb (cr, 0, 0, 0);
60
3
    cairo_set_line_width (cr, 2);
61

            
62
3
    total = 0.0;
63
15
    for (k = 0; k < ARRAY_LENGTH (dashes); ++k) {
64
12
	total += dashes[k];
65
60
	for (i = 0; i < ARRAY_LENGTH (frac_offset); ++i) {
66
288
	    for (j = 0; j < ARRAY_LENGTH (int_offset); ++j) {
67
240
		cairo_set_dash (cr, dashes, k + 1, frac_offset[i] + total * int_offset[j]);
68
240
		cairo_move_to (cr, (STROKE_LENGTH + PAD) * k + PAD, PAD * (i + j + ARRAY_LENGTH (frac_offset) * j + 1));
69
240
		cairo_line_to (cr, (STROKE_LENGTH + PAD) * (k + 1), PAD * (i + j + ARRAY_LENGTH (frac_offset) * j + 1));
70
240
		cairo_stroke (cr);
71
	    }
72
	}
73
    }
74

            
75
3
    return CAIRO_TEST_SUCCESS;
76
}
77

            
78
1
CAIRO_TEST (dash_offset,
79
	    "Tests dashes of different length with various offsets",
80
	    "stroke, dash", /* keywords */
81
	    NULL, /* requirements */
82
	    IMAGE_WIDTH, IMAGE_HEIGHT,
83
	    NULL, draw)