Debian Codegraph @

tests/test_api.py

raw symbols SCIP
1 import os
2 import io
3 import sys
4 import tempfile
5 import base64
6 import zlib
7 import shutil
8 import sysconfig
9
10 import cairo
11 import pytest
12
13
14 def test_get_include():
15 include = cairo.get_include()
16 assert isinstance(include, str)
17 assert os.path.exists(include)
18 assert os.path.isdir(include)
19
20
21 def test_version():
22 cairo.cairo_version()
23 cairo.cairo_version_string()
24
25 assert cairo.CAIRO_VERSION == cairo.cairo_version()
26 assert cairo.CAIRO_VERSION_STRING == cairo.cairo_version_string()
27 ver_tuple = (cairo.CAIRO_VERSION_MAJOR, cairo.CAIRO_VERSION_MINOR,
28 cairo.CAIRO_VERSION_MICRO)
29 assert tuple(map(int, cairo.CAIRO_VERSION_STRING.split("."))) == \
30 ver_tuple
31
32
33 def test_show_unicode_text():
34 width, height = 300, 300
35 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
36 ctx = cairo.Context(surface)
37
38 ctx.scale(width, height)
39 ctx.set_line_width(0.04)
40
41 ctx.select_font_face(
42 "Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
43 ctx.set_font_size(0.20)
44 ctx.move_to(0.05, 0.5)
45 ctx.show_text("ēxāmple.")
46
47
48 def test_unicode_filenames():
49 # FIXME: cairo does not support wchar on Windows and byte support is
50 # missing under Python 3
51
52 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
53 dirname = tempfile.mkdtemp()
54 old_dir = os.getcwd()
55 try:
56 os.chdir(dirname)
57 surface.write_to_png("foobar")
58 new = cairo.ImageSurface.create_from_png("foobar")
59 assert surface.get_data() == new.get_data()
60 finally:
61 os.chdir(old_dir)
62 shutil.rmtree(dirname)
63
64
65 def test_surface_has_show_text_glyphs():
66 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
67 assert not surface.has_show_text_glyphs()
68 surface.finish()
69 with pytest.raises(cairo.Error):
70 surface.has_show_text_glyphs()
71
72
73 def test_context():
74 f, w, h = cairo.FORMAT_ARGB32, 100, 100
75 s = cairo.ImageSurface(f, w, h)
76 ctx = cairo.Context(s)
77 ctx.set_source_rgb(1.0, 1.0, 1.0)
78 ctx.set_operator(cairo.OPERATOR_SOURCE)
79 ctx.paint()
80
81
82 def test_surface():
83 # TypeError: The Surface type cannot be instantiated
84 with pytest.raises(TypeError):
85 cairo.Surface()
86
87 f, w, h = cairo.FORMAT_ARGB32, 100, 100
88 s = cairo.ImageSurface(f, w, h)
89 assert s.get_format() == f
90 assert s.get_width() == w
91 assert s.get_height() == h
92
93 with tempfile.TemporaryFile(mode='w+b') as f:
94 cairo.PDFSurface(f, 100, 100)
95
96 with tempfile.TemporaryFile(mode='w+b') as f:
97 cairo.PSSurface(f, 100, 100)
98
99 s = cairo.RecordingSurface(cairo.CONTENT_COLOR, None)
100 s = cairo.RecordingSurface(cairo.CONTENT_COLOR, (1, 1, 10, 10))
101
102 with tempfile.TemporaryFile(mode='w+b') as f:
103 cairo.SVGSurface(f, 100, 100)
104
105
106 def test_surface_destroy_before_context():
107 for kind in [cairo.PDFSurface, cairo.PSSurface]:
108 surface = kind(io.BytesIO(), 1, 1)
109 ctx = cairo.Context(surface)
110 del surface
111 ctx.paint()
112
113
114 def test_surface_destroy_before_surface_pattern():
115 surface = cairo.PDFSurface(io.BytesIO(), 1, 1)
116 pattern = cairo.SurfacePattern(surface)
117 del surface
118 ctx = cairo.Context(pattern.get_surface())
119 ctx.paint()
120
121
122 def test_recording_surface_get_extents():
123 surface = cairo.RecordingSurface(cairo.CONTENT_COLOR, None)
124 assert surface.get_extents() is None
125
126 surface = cairo.RecordingSurface(cairo.CONTENT_COLOR, (1, 2, 3, 4))
127 assert surface.get_extents() == (1, 2, 3, 4)
128
129 surface = cairo.RecordingSurface(cairo.CONTENT_COLOR, (1, 2, 3, 4))
130 surface.finish()
131 assert surface.get_extents() == (1, 2, 3, 4)
132
133
134 def test_image_surface_get_data():
135 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 3, 3)
136 ctx = cairo.Context(surface)
137 ctx.paint()
138 surface.flush()
139 buf = surface.get_data()
140 assert buf
141 assert len(buf) == 4 * 3 * 3
142 assert len(bytes(buf)) == len(buf)
143 buf[0:1] = b"\x42"
144
145 newbuf = surface.get_data()
146 assert newbuf[0:1] == b"\x42"
147 ctx.set_source_rgba(1, 1, 1, 1)
148 ctx.paint()
149 surface.flush()
150 assert newbuf[0:1] == b"\xff"
151
152
153 def test_surface_file_obj_error():
154
155 class Fail:
156
157 def write(self, data):
158 if data:
159 raise OSError
160
161 cairo.PDFSurface(Fail(), 100, 100)
162 cairo.PSSurface(Fail(), 100, 100)
163
164
165 def test_text():
166 pass
167
168
169 def test_region():
170 a = cairo.Region()
171 assert a.is_empty() is True
172 assert a.num_rectangles() == 0
173
174 b = cairo.RectangleInt(1, 2, 10, 12)
175 with pytest.raises(TypeError):
176 hash(b)
177 assert repr(b) == "cairo.RectangleInt(x=1, y=2, width=10, height=12)"
178 assert eval(repr(b)) == b
179 assert isinstance(repr(b), str)
180 d = cairo.RectangleInt(1, 1, 10, 12)
181 e = cairo.RectangleInt(1, 3, 8, 12)
182 assert (b.x, b.y, b.width, b.height) == (1, 2, 10, 12)
183 c = cairo.Region((b, e))
184 assert not c.is_empty()
185 assert c.num_rectangles() == 2
186 assert c.get_rectangle(1).y == 14
187
188 ex = c.get_extents()
189 assert ex == cairo.RectangleInt(1, 2, 10, 13)
190 assert c.contains_rectangle(d) == cairo.REGION_OVERLAP_PART
191
192 c.translate(10, 20)
193 assert c.contains_rectangle(d) == cairo.REGION_OVERLAP_OUT
194 assert c.get_rectangle(1) == cairo.RectangleInt(11, 34, 8, 1)
195
196 cp = c.copy()
197 assert c.num_rectangles() == cp.num_rectangles()
198 assert c.get_rectangle(0) == cp.get_rectangle(0)
199 assert c == cp
200 assert 3 != c
201 assert c != "test"
202
203 c = cairo.Region((b, e))
204 c.intersect(d)
205 assert c.num_rectangles() == 1
206 assert c.get_rectangle(0) == cairo.RectangleInt(1, 2, 10, 11)
207
208 c = cairo.Region((b, e))
209 c.subtract(d)
210 assert c.num_rectangles() == 2
211 assert c == cairo.Region([
212 cairo.RectangleInt(1, 13, 10, 1),
213 cairo.RectangleInt(1, 14, 8, 1),
214 ])
215
216 d = cairo.Region(d)
217 c = cairo.Region((b, e))
218 c.subtract(d)
219 assert c.num_rectangles() == 2
220 assert c.get_rectangle(0) == cairo.RectangleInt(1, 13, 10, 1)
221
222 c = cairo.Region((b, e))
223 c.union(d)
224 assert c.num_rectangles() == 2
225 assert c == cairo.Region([
226 cairo.RectangleInt(1, 1, 10, 13),
227 cairo.RectangleInt(1, 14, 8, 1),
228 ])
229
230 c = cairo.Region((b, e))
231 c.xor(d)
232 assert c.num_rectangles() == 3
233 assert c == cairo.Region([
234 cairo.RectangleInt(1, 1, 10, 1),
235 cairo.RectangleInt(1, 14, 8, 1),
236 cairo.RectangleInt(1, 13, 10, 1),
237 ])
238
239
240 def test_constants():
241 assert cairo.REGION_OVERLAP_IN == 0
242 assert cairo.REGION_OVERLAP_OUT == 1
243 assert cairo.REGION_OVERLAP_PART == 2
244
245 assert cairo.ANTIALIAS_FAST == 4
246 assert cairo.ANTIALIAS_GOOD == 5
247 assert cairo.ANTIALIAS_BEST == 6
248
249 assert cairo.OPERATOR_MULTIPLY == 14
250 assert cairo.OPERATOR_SCREEN == 15
251 assert cairo.OPERATOR_OVERLAY == 16
252 assert cairo.OPERATOR_DARKEN == 17
253 assert cairo.OPERATOR_LIGHTEN == 18
254 assert cairo.OPERATOR_COLOR_DODGE == 19
255 assert cairo.OPERATOR_COLOR_BURN == 20
256 assert cairo.OPERATOR_HARD_LIGHT == 21
257 assert cairo.OPERATOR_SOFT_LIGHT == 22
258 assert cairo.OPERATOR_DIFFERENCE == 23
259 assert cairo.OPERATOR_EXCLUSION == 24
260 assert cairo.OPERATOR_HSL_HUE == 25
261 assert cairo.OPERATOR_HSL_SATURATION == 26
262 assert cairo.OPERATOR_HSL_COLOR == 27
263 assert cairo.OPERATOR_HSL_LUMINOSITY == 28
264
265 assert cairo.FORMAT_INVALID == -1
266 assert cairo.FORMAT_RGB30 == 5
267
268 assert cairo.MIME_TYPE_JPEG == "image/jpeg"
269
270 assert cairo.SVG_VERSION_1_1 == 0
271 assert cairo.SVG_VERSION_1_2 == 1
272
273
274 @pytest.mark.skipif(not hasattr(sys, "getrefcount"), reason="PyPy")
275 def test_surface_get_set_mime_data_references():
276 surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1)
277 v = memoryview(b"bla")
278 x = v[:1]
279 recfcount_v = sys.getrefcount(v)
280 recfcount_x = sys.getrefcount(x)
281 assert recfcount_v == recfcount_x
282 surface.set_mime_data("foo", v)
283 surface.set_mime_data("foo2", v)
284 surface.set_mime_data("foo3", x)
285 assert surface.get_mime_data("foo") is v
286 assert surface.get_mime_data("foo2") is v
287 assert surface.get_mime_data("foo3") is x
288 surface.set_mime_data("foo", None)
289 surface.set_mime_data("foo2", None)
290 surface.set_mime_data("foo3", None)
291 surface.finish()
292 assert sys.getrefcount(v) == recfcount_v
293 assert sys.getrefcount(x) == recfcount_x
294
295
296 @pytest.mark.skipif(
297 sysconfig.get_platform().startswith("win"), reason="msvc fixme")
298 def test_surface_mime_data_for_pdf():
299 jpeg_bytes = zlib.decompress(base64.b64decode(
300 b'eJz7f+P/AwYBLzdPNwZGRkYGDyBk+H+bwRnEowj8P8TAzcHACDJHkOH/EQYRIBsV'
301 b'cP6/xcDBCBJlrLcHqRBAV8EAVcHIylSPVwGbPQEFjPaK9XDrBAipBSq4CQB9jiS0'
302 ))
303
304 file_like = io.BytesIO()
305 surface = cairo.PDFSurface(file_like, 3, 3)
306 context = cairo.Context(surface)
307 image = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1)
308 image.set_mime_data(cairo.MIME_TYPE_JPEG, jpeg_bytes)
309 context.set_source_surface(image, 0, 0)
310 context.paint()
311 surface.finish()
312 assert jpeg_bytes in file_like.getvalue()