File: pattern.lua

package info (click to toggle)
oocairo 1.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 712 kB
  • sloc: ansic: 3,352; makefile: 59; sh: 15
file content (175 lines) | stat: -rw-r--r-- 5,932 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require "test-setup"
require "lunit"
local Cairo = require "oocairo"

module("test.pattern", lunit.testcase, package.seeall)

function test_solid_rgb ()
    local pat = Cairo.pattern_create_rgb(0.25, 0.5, 0.75)
    assert_userdata(pat)
    assert_equal("cairo pattern object", pat._NAME)
    assert_equal("solid", pat:get_type())
    assert_equal(nil, pat:status(), "Error status on pattern")

    local r, g, b, a = pat:get_rgba()
    assert_equal(0.25, r)
    assert_equal(0.5, g)
    assert_equal(0.75, b)
    assert_equal(1, a)
end

function test_solid_rgba ()
    local pat = Cairo.pattern_create_rgba(0.25, 0.5, 0.75, 0.125)
    assert_userdata(pat)
    assert_equal("cairo pattern object", pat._NAME)
    assert_equal("solid", pat:get_type())
    assert_equal(nil, pat:status(), "Error status on pattern")

    local r, g, b, a = pat:get_rgba()
    assert_equal(0.25, r)
    assert_equal(0.5, g)
    assert_equal(0.75, b)
    assert_equal(0.125, a)
end

function test_surface ()
    local surface = Cairo.image_surface_create("rgb24", 23, 45)
    local pat = Cairo.pattern_create_for_surface(surface)
    assert_userdata(pat)
    assert_equal("cairo pattern object", pat._NAME)
    assert_equal("surface", pat:get_type())
    assert_equal(nil, pat:status(), "Error status on pattern")

    local gotsurface = pat:get_surface()
    assert_userdata(gotsurface)
    assert_equal("cairo surface object", gotsurface._NAME)
    local wd, ht = gotsurface:get_width(), gotsurface:get_height()
    assert_equal(23, wd)
    assert_equal(45, ht)
end

function test_linear_gradient ()
    local g = Cairo.pattern_create_linear(3, 4, 8, 9)
    assert_userdata(g)
    assert_equal("cairo pattern object", g._NAME)
    assert_equal("linear", g:get_type())
    assert_equal(nil, g:status(), "Error status on pattern")

    local x0, y0, x1, y1 = g:get_linear_points()
    assert_equal(3, x0)
    assert_equal(4, y0)
    assert_equal(8, x1)
    assert_equal(9, y1)
end

function test_radial_gradient ()
    local g = Cairo.pattern_create_radial(3, 4, 5, 8, 9, 10)
    assert_userdata(g)
    assert_equal("cairo pattern object", g._NAME)
    assert_equal(nil, g:status(), "Error status on pattern")
    assert_equal("radial", g:get_type())

    local x0, y0, r0, x1, y1, r1 = g:get_radial_circles()
    assert_equal(3, x0)
    assert_equal(4, y0)
    assert_equal(5, r0)
    assert_equal(8, x1)
    assert_equal(9, y1)
    assert_equal(10, r1)
end

function test_wrong_pattern_type ()
    local pat = Cairo.pattern_create_radial(3, 4, 5, 8, 9, 10)
    assert_error("not linear gradient, get points", function ()
        pat:get_linear_points()
    end)

    pat = Cairo.pattern_create_linear(3, 4, 8, 9)
    assert_error("not radial gradient, get circles", function ()
        pat:get_radial_circles()
    end)

    assert_error("not surface gradient, get surface", function ()
        pat:get_surface()
    end)
end

local function check_color_stop (i, stop, offset, r, g, b, a)
    assert_table(stop, "table for color stop " .. i)
    assert_equal(stop[1], offset, "offset for color stop " .. i)
    assert_equal(stop[2], r, "red for color stop " .. i)
    assert_equal(stop[3], g, "green for color stop " .. i)
    assert_equal(stop[4], b, "blue for color stop " .. i)
    assert_equal(stop[5], a, "alpha for color stop " .. i)
end

function test_add_color_stop ()
    local pat = Cairo.pattern_create_linear(3, 4, 8, 9)
    pat:add_color_stop_rgb(0, 0.25, 0.5, 0.75)
    pat:add_color_stop_rgb(0.125, 1, 0, 1)
    pat:add_color_stop_rgba(0.875, 0, 1, 0, 0.5)
    pat:add_color_stop_rgba(1, 1, 1, 1, 1)

    local stops = pat:get_color_stops()
    assert_table(stops)
    assert_equal(4, #stops)
    check_color_stop(1, stops[1], 0, 0.25, 0.5, 0.75, 1)
    check_color_stop(2, stops[2], 0.125, 1, 0, 1, 1)
    check_color_stop(3, stops[3], 0.875, 0, 1, 0, 0.5)
    check_color_stop(4, stops[4], 1, 1, 1, 1, 1)
end

function test_extend ()
    local pat = Cairo.pattern_create_linear(3, 4, 8, 9)
    assert_error("bad value", function () pat:set_extend("foo") end)
    assert_error("missing value", function () pat:set_extend(nil) end)
    assert_equal("pad", pat:get_extend(), "default intact after error")
    for _, v in ipairs{ "none", "repeat", "reflect", "pad" } do
        pat:set_extend(v)
        assert_equal(v, pat:get_extend())
    end
end

function test_filter ()
    local pat = Cairo.pattern_create_linear(3, 4, 8, 9)
    assert_error("bad value", function () pat:set_filter("foo") end)
    assert_error("missing value", function () pat:set_filter(nil) end)
    assert_equal("good", pat:get_filter(), "default intact after error")
    for _, v in ipairs{
        "fast", "good", "best", "nearest", "bilinear", "gaussian"
    } do
        pat:set_filter(v)
        assert_equal(v, pat:get_filter())
    end
end

function test_not_gradient_pattern ()
    local pat = Cairo.pattern_create_rgb(0.25, 0.5, 0.75)
    assert_error("add_color_stop_rgb on non-gradient pattern",
                 function () pat:add_color_stop_rgb(0, 0.1, 0.2, 0.3) end)
    assert_error("add_color_stop_rgba on non-gradient pattern",
                 function () pat:add_color_stop_rgba(0, 0.1, 0.2, 0.3, 0.4) end)
end

function test_equality ()
    -- Different userdatas, same Cairo object.
    local surface = Cairo.image_surface_create("rgb24", 23, 45)
    local pattern1 = Cairo.pattern_create_rgb(0.25, 0.5, 0.75)
    local cr = Cairo.context_create(surface)
    cr:set_source(pattern1)
    local pattern2 = cr:get_source()
    assert_true(pattern1 == pattern2)

    -- Different userdatas, different Cairo objects.
    local pattern3 = Cairo.pattern_create_rgb(0.25, 0.5, 0.75)
    assert_false(pattern1 == pattern3)
end

function test_double_gc ()
    local surface = Cairo.image_surface_create("rgb24", 23, 45)
    local pattern = Cairo.pattern_create_rgb(0.25, 0.5, 0.75)
    pattern:__gc()
    pattern:__gc()
end

-- vi:ts=4 sw=4 expandtab