File: test_font.py

package info (click to toggle)
pycairo 1.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,984 kB
  • sloc: ansic: 8,873; python: 3,688; makefile: 32; sh: 4
file content (328 lines) | stat: -rw-r--r-- 11,072 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import platform

import cairo
import pytest


@pytest.fixture
def font_options():
    surface = cairo.ImageSurface(0, 10, 10)
    return surface.get_font_options()


@pytest.fixture
def font_face():
    surface = cairo.ImageSurface(0, 10, 10)
    context = cairo.Context(surface)
    return context.get_font_face()


@pytest.fixture
def scaled_font(font_face, font_options):
    return cairo.ScaledFont(
        font_face, cairo.Matrix(), cairo.Matrix(), font_options)


@pytest.mark.skipif(not hasattr(cairo.FontOptions, "set_custom_palette_color"),
                    reason="too old cairo")
def test_font_options_custom_palette_color(font_options):
    font_options.set_custom_palette_color(42, 0.25, 0.5, 0.75, 1.0)
    with pytest.raises(cairo.Error) as exc_info:
        font_options.get_custom_palette_color(24)
    assert exc_info.value.status == cairo.Status.INVALID_INDEX
    assert font_options.get_custom_palette_color(42) == (0.25, 0.5, 0.75, 1.0)
    assert isinstance(font_options.get_custom_palette_color(42), tuple)


@pytest.mark.skipif(not hasattr(cairo.FontOptions, "set_color_mode"),
                    reason="too old cairo")
def test_font_options_set_color_mode(font_options):
    font_options.set_color_mode(cairo.ColorMode.COLOR)
    assert font_options.get_color_mode() == cairo.ColorMode.COLOR
    with pytest.raises(TypeError):
        font_options.set_color_mode(object())


@pytest.mark.skipif(not hasattr(cairo.FontOptions, "get_color_mode"),
                    reason="too old cairo")
def test_font_options_get_color_mode(font_options):
    assert font_options.get_color_mode() == cairo.ColorMode.DEFAULT
    assert isinstance(font_options.get_color_mode(), cairo.ColorMode)


@pytest.mark.skipif(not hasattr(cairo.FontOptions, "set_color_palette"),
                    reason="too old cairo")
def test_font_options_set_color_palette(font_options):
    font_options.set_color_palette(42)
    assert font_options.get_color_palette() == 42


@pytest.mark.skipif(not hasattr(cairo.FontOptions, "get_color_palette"),
                    reason="too old cairo")
def test_font_options_get_color_palette(font_options):
    assert font_options.get_color_palette() == cairo.COLOR_PALETTE_DEFAULT


@pytest.mark.skipif(not hasattr(cairo.FontOptions, "get_variations"),
                    reason="too old cairo")
def test_font_options_variations(font_options):
    assert font_options.get_variations() is None
    font_options.set_variations("foo")
    assert font_options.get_variations() == "foo"
    font_options.set_variations(None)
    assert font_options.get_variations() is None

    with pytest.raises(TypeError):
        font_options.set_variations(1)

    with pytest.raises(TypeError):
        font_options.set_variations("foo", "bar")

    with pytest.raises(TypeError):
        font_options.set_variations()


def test_font_options():
    assert isinstance(cairo.FontOptions(), cairo.FontOptions)
    with pytest.raises(TypeError):
        cairo.FontOptions(object())


def test_font_options_copy_equal():
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1)
    font_options = surface.get_font_options()
    font_options.set_hint_metrics(cairo.HINT_METRICS_DEFAULT)
    new = font_options.copy()
    assert font_options.equal(new)
    assert new.get_hint_metrics() == cairo.HINT_METRICS_DEFAULT
    font_options.set_hint_metrics(cairo.HINT_METRICS_ON)
    assert not font_options.equal(new)
    assert new.get_hint_metrics() == cairo.HINT_METRICS_DEFAULT
    with pytest.raises(TypeError):
        font_options.equal(object())


def test_font_options_hash():
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1)
    font_options = surface.get_font_options()
    assert font_options.hash() == font_options.hash()
    assert isinstance(font_options.hash(), int)


def test_font_options_merge():
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1)
    font_options = surface.get_font_options()
    font_options.set_hint_metrics(cairo.HINT_METRICS_DEFAULT)
    new = font_options.copy()
    new.set_hint_metrics(cairo.HINT_METRICS_ON)
    font_options.merge(new)
    assert font_options.get_hint_metrics() == cairo.HINT_METRICS_ON
    with pytest.raises(TypeError):
        font_options.merge(object())


def test_font_options_hashable_protocol():
    # make sure __eq__ and __ne__ work
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1)
    font_options = surface.get_font_options()
    assert font_options == font_options.copy()
    assert not font_options != font_options.copy()
    font_options.set_hint_metrics(cairo.HINT_METRICS_DEFAULT)
    different = font_options.copy()
    different.set_hint_metrics(cairo.HINT_METRICS_ON)
    assert font_options != different
    assert not font_options == different
    assert font_options != object()

    # make sure the other operators are undefined
    with pytest.raises(TypeError):
        font_options < font_options
    assert font_options.__gt__(font_options) is NotImplemented


def test_font_options_set_antialias(font_options):
    font_options.set_antialias(cairo.Antialias.GRAY)
    assert font_options.get_antialias() == cairo.Antialias.GRAY
    with pytest.raises(TypeError):
        font_options.set_antialias(object())


def test_font_options_set_hint_metrics(font_options):
    font_options.set_hint_metrics(cairo.HintMetrics.OFF)
    assert font_options.get_hint_metrics() == cairo.HintMetrics.OFF
    with pytest.raises(TypeError):
        font_options.set_hint_metrics(object())


def test_font_options_set_hint_style(font_options):
    font_options.set_hint_style(cairo.HintStyle.SLIGHT)
    assert font_options.get_hint_style() == cairo.HintStyle.SLIGHT
    with pytest.raises(TypeError):
        font_options.set_hint_style(object())


def test_font_options_set_subpixel_order(font_options):
    font_options.set_subpixel_order(cairo.SubpixelOrder.VRGB)
    assert font_options.get_subpixel_order() == cairo.SubpixelOrder.VRGB
    with pytest.raises(TypeError):
        font_options.set_subpixel_order(object())


def test_font_face(font_face):
    with pytest.raises(TypeError):
        cairo.FontFace()

    assert font_face == font_face
    assert font_face != object()


def test_font_face_cmp_hash():
    surface = cairo.ImageSurface(0, 10, 10)
    context = cairo.Context(surface)
    ff = context.get_font_face()
    other = context.get_font_face()
    assert ff == other
    assert not ff != other
    assert hash(ff) == hash(other)

    sf = context.get_scaled_font()
    other = context.get_scaled_font()
    assert sf == other
    assert not sf != other
    assert hash(sf) == hash(other)

    fo = context.get_font_options()
    # FontOptions compare by their content and they are mutable, so not
    # hashable.
    with pytest.raises(TypeError):
        hash(fo)


def test_scaled_font(scaled_font):
    with pytest.raises(TypeError):
        cairo.ScaledFont()

    assert scaled_font == scaled_font
    assert scaled_font != object()


def test_scaled_font_extents(scaled_font):
    assert isinstance(scaled_font.extents(), tuple)


def test_scaled_font_get_font_face(scaled_font):
    assert isinstance(scaled_font.get_font_face(), cairo.FontFace)


def test_scaled_font_get_scale_matrix(scaled_font):
    assert isinstance(scaled_font.get_scale_matrix(), cairo.Matrix)


# https://foss.heptapod.net/pypy/pypy/-/issues/2741
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy")
def test_scaled_font_text_extents(scaled_font):
    with pytest.raises(TypeError):
        scaled_font.text_extents(object())


def test_scaled_font_glyph_extents(scaled_font):
    with pytest.raises(TypeError):
        scaled_font.glyph_extents(object())
    with pytest.raises(TypeError):
        scaled_font.glyph_extents([object()])
    with pytest.raises(TypeError):
        scaled_font.glyph_extents()


# https://foss.heptapod.net/pypy/pypy/-/issues/2741
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy")
def test_toy_font_face():
    with pytest.raises(TypeError):
        cairo.ToyFontFace(object())


def test_toy_font_get_family():
    font_face = cairo.ToyFontFace("")
    assert isinstance(font_face.get_family(), str)
    font_face = cairo.ToyFontFace("serif")
    assert isinstance(font_face.get_family(), str)
    font_face = cairo.ToyFontFace("sans-serif")
    assert isinstance(font_face.get_family(), str)


def test_toy_font_get_slant():
    font_face = cairo.ToyFontFace("")
    assert font_face.get_slant() == cairo.FontSlant.NORMAL
    assert isinstance(font_face.get_slant(), cairo.FontSlant)


def test_toy_font_get_weight():
    font_face = cairo.ToyFontFace("")
    assert font_face.get_weight() == cairo.FontWeight.NORMAL
    assert isinstance(font_face.get_weight(), cairo.FontWeight)


def test_font_options_get_antialias(font_options):
    assert font_options.get_antialias() == cairo.Antialias.DEFAULT
    assert isinstance(font_options.get_antialias(), cairo.Antialias)


def test_font_options_get_hint_metrics(font_options):
    assert font_options.get_hint_metrics() == cairo.HintMetrics.ON
    assert isinstance(font_options.get_hint_metrics(), cairo.HintMetrics)


def test_font_options_get_hint_style(font_options):
    assert font_options.get_hint_style() == cairo.HintStyle.DEFAULT
    assert isinstance(font_options.get_hint_style(), cairo.HintStyle)


def test_font_options_get_subpixel_order(font_options):
    assert font_options.get_subpixel_order() == cairo.SubpixelOrder.DEFAULT
    assert isinstance(font_options.get_subpixel_order(), cairo.SubpixelOrder)


def test_scaled_font_get_ctm():
    surface = cairo.ImageSurface(0, 10, 10)
    ctx = cairo.Context(surface)
    sf = ctx.get_scaled_font()
    matrix = sf.get_ctm()
    assert isinstance(matrix, cairo.Matrix)


def test_scaled_font_get_font_matrix():
    surface = cairo.ImageSurface(0, 10, 10)
    ctx = cairo.Context(surface)
    sf = ctx.get_scaled_font()
    matrix = sf.get_font_matrix()
    assert isinstance(matrix, cairo.Matrix)


def test_scaled_font_get_font_options():
    surface = cairo.ImageSurface(0, 10, 10)
    ctx = cairo.Context(surface)
    sf = ctx.get_scaled_font()
    font_options = sf.get_font_options()
    assert isinstance(font_options, cairo.FontOptions)


def test_scaled_font_text_to_glyphs():
    surface = cairo.ImageSurface(0, 10, 10)
    ctx = cairo.Context(surface)
    sf = ctx.get_scaled_font()
    assert sf.text_to_glyphs(0, 0, "") == ([], [], 0)
    glyphs, clusters, flags = sf.text_to_glyphs(0, 0, "a")
    assert sf.text_to_glyphs(0, 0, "a", True) == (glyphs, clusters, flags)
    assert len(glyphs) == 1
    assert isinstance(glyphs[0], cairo.Glyph)
    assert len(clusters) == 1
    assert isinstance(clusters[0], cairo.TextCluster)
    assert flags == 0
    assert sf.text_to_glyphs(0, 0, "a", False) == glyphs
    glyphs, clusters, flags = sf.text_to_glyphs(0, 0, "a b")
    assert len(glyphs) == 3
    assert glyphs[0] != glyphs[1]
    assert len(clusters) == 3

    with pytest.raises(TypeError):
        sf.text_to_glyphs(object())