| 1 |
import platform |
| 2 |
|
| 3 |
import cairo |
| 4 |
import pytest |
| 5 |
|
| 6 |
|
| 7 |
@pytest.fixture |
| 8 |
def font_options(): |
| 9 |
surface = cairo.ImageSurface(0, 10, 10) |
| 10 |
return surface.get_font_options() |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture |
| 14 |
def font_face(): |
| 15 |
surface = cairo.ImageSurface(0, 10, 10) |
| 16 |
context = cairo.Context(surface) |
| 17 |
return context.get_font_face() |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def scaled_font(font_face, font_options): |
| 22 |
return cairo.ScaledFont( |
| 23 |
font_face, cairo.Matrix(), cairo.Matrix(), font_options) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.skipif(not hasattr(cairo.FontOptions, "set_custom_palette_color"), |
| 27 |
reason="too old cairo") |
| 28 |
def test_font_options_custom_palette_color(font_options): |
| 29 |
font_options.set_custom_palette_color(42, 0.25, 0.5, 0.75, 1.0) |
| 30 |
with pytest.raises(cairo.Error) as exc_info: |
| 31 |
font_options.get_custom_palette_color(24) |
| 32 |
assert exc_info.value.status == cairo.Status.INVALID_INDEX |
| 33 |
assert font_options.get_custom_palette_color(42) == (0.25, 0.5, 0.75, 1.0) |
| 34 |
assert isinstance(font_options.get_custom_palette_color(42), tuple) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.skipif(not hasattr(cairo.FontOptions, "set_color_mode"), |
| 38 |
reason="too old cairo") |
| 39 |
def test_font_options_set_color_mode(font_options): |
| 40 |
font_options.set_color_mode(cairo.ColorMode.COLOR) |
| 41 |
assert font_options.get_color_mode() == cairo.ColorMode.COLOR |
| 42 |
with pytest.raises(TypeError): |
| 43 |
font_options.set_color_mode(object()) |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.skipif(not hasattr(cairo.FontOptions, "get_color_mode"), |
| 47 |
reason="too old cairo") |
| 48 |
def test_font_options_get_color_mode(font_options): |
| 49 |
assert font_options.get_color_mode() == cairo.ColorMode.DEFAULT |
| 50 |
assert isinstance(font_options.get_color_mode(), cairo.ColorMode) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.skipif(not hasattr(cairo.FontOptions, "set_color_palette"), |
| 54 |
reason="too old cairo") |
| 55 |
def test_font_options_set_color_palette(font_options): |
| 56 |
font_options.set_color_palette(42) |
| 57 |
assert font_options.get_color_palette() == 42 |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.skipif(not hasattr(cairo.FontOptions, "get_color_palette"), |
| 61 |
reason="too old cairo") |
| 62 |
def test_font_options_get_color_palette(font_options): |
| 63 |
assert font_options.get_color_palette() == cairo.COLOR_PALETTE_DEFAULT |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.skipif(not hasattr(cairo.FontOptions, "get_variations"), |
| 67 |
reason="too old cairo") |
| 68 |
def test_font_options_variations(font_options): |
| 69 |
assert font_options.get_variations() is None |
| 70 |
font_options.set_variations("foo") |
| 71 |
assert font_options.get_variations() == "foo" |
| 72 |
font_options.set_variations(None) |
| 73 |
assert font_options.get_variations() is None |
| 74 |
|
| 75 |
with pytest.raises(TypeError): |
| 76 |
font_options.set_variations(1) |
| 77 |
|
| 78 |
with pytest.raises(TypeError): |
| 79 |
font_options.set_variations("foo", "bar") |
| 80 |
|
| 81 |
with pytest.raises(TypeError): |
| 82 |
font_options.set_variations() |
| 83 |
|
| 84 |
|
| 85 |
def test_font_options(): |
| 86 |
assert isinstance(cairo.FontOptions(), cairo.FontOptions) |
| 87 |
with pytest.raises(TypeError): |
| 88 |
cairo.FontOptions(object()) |
| 89 |
|
| 90 |
|
| 91 |
def test_font_options_copy_equal(): |
| 92 |
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) |
| 93 |
font_options = surface.get_font_options() |
| 94 |
font_options.set_hint_metrics(cairo.HINT_METRICS_DEFAULT) |
| 95 |
new = font_options.copy() |
| 96 |
assert font_options.equal(new) |
| 97 |
assert new.get_hint_metrics() == cairo.HINT_METRICS_DEFAULT |
| 98 |
font_options.set_hint_metrics(cairo.HINT_METRICS_ON) |
| 99 |
assert not font_options.equal(new) |
| 100 |
assert new.get_hint_metrics() == cairo.HINT_METRICS_DEFAULT |
| 101 |
with pytest.raises(TypeError): |
| 102 |
font_options.equal(object()) |
| 103 |
|
| 104 |
|
| 105 |
def test_font_options_hash(): |
| 106 |
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) |
| 107 |
font_options = surface.get_font_options() |
| 108 |
assert font_options.hash() == font_options.hash() |
| 109 |
assert isinstance(font_options.hash(), int) |
| 110 |
|
| 111 |
|
| 112 |
def test_font_options_merge(): |
| 113 |
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) |
| 114 |
font_options = surface.get_font_options() |
| 115 |
font_options.set_hint_metrics(cairo.HINT_METRICS_DEFAULT) |
| 116 |
new = font_options.copy() |
| 117 |
new.set_hint_metrics(cairo.HINT_METRICS_ON) |
| 118 |
font_options.merge(new) |
| 119 |
assert font_options.get_hint_metrics() == cairo.HINT_METRICS_ON |
| 120 |
with pytest.raises(TypeError): |
| 121 |
font_options.merge(object()) |
| 122 |
|
| 123 |
|
| 124 |
def test_font_options_hashable_protocol(): |
| 125 |
|
| 126 |
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) |
| 127 |
font_options = surface.get_font_options() |
| 128 |
assert font_options == font_options.copy() |
| 129 |
assert not font_options != font_options.copy() |
| 130 |
font_options.set_hint_metrics(cairo.HINT_METRICS_DEFAULT) |
| 131 |
different = font_options.copy() |
| 132 |
different.set_hint_metrics(cairo.HINT_METRICS_ON) |
| 133 |
assert font_options != different |
| 134 |
assert not font_options == different |
| 135 |
assert font_options != object() |
| 136 |
|
| 137 |
|
| 138 |
with pytest.raises(TypeError): |
| 139 |
font_options < font_options |
| 140 |
assert font_options.__gt__(font_options) is NotImplemented |
| 141 |
|
| 142 |
|
| 143 |
def test_font_options_set_antialias(font_options): |
| 144 |
font_options.set_antialias(cairo.Antialias.GRAY) |
| 145 |
assert font_options.get_antialias() == cairo.Antialias.GRAY |
| 146 |
with pytest.raises(TypeError): |
| 147 |
font_options.set_antialias(object()) |
| 148 |
|
| 149 |
|
| 150 |
def test_font_options_set_hint_metrics(font_options): |
| 151 |
font_options.set_hint_metrics(cairo.HintMetrics.OFF) |
| 152 |
assert font_options.get_hint_metrics() == cairo.HintMetrics.OFF |
| 153 |
with pytest.raises(TypeError): |
| 154 |
font_options.set_hint_metrics(object()) |
| 155 |
|
| 156 |
|
| 157 |
def test_font_options_set_hint_style(font_options): |
| 158 |
font_options.set_hint_style(cairo.HintStyle.SLIGHT) |
| 159 |
assert font_options.get_hint_style() == cairo.HintStyle.SLIGHT |
| 160 |
with pytest.raises(TypeError): |
| 161 |
font_options.set_hint_style(object()) |
| 162 |
|
| 163 |
|
| 164 |
def test_font_options_set_subpixel_order(font_options): |
| 165 |
font_options.set_subpixel_order(cairo.SubpixelOrder.VRGB) |
| 166 |
assert font_options.get_subpixel_order() == cairo.SubpixelOrder.VRGB |
| 167 |
with pytest.raises(TypeError): |
| 168 |
font_options.set_subpixel_order(object()) |
| 169 |
|
| 170 |
|
| 171 |
def test_font_face(font_face): |
| 172 |
with pytest.raises(TypeError): |
| 173 |
cairo.FontFace() |
| 174 |
|
| 175 |
assert font_face == font_face |
| 176 |
assert font_face != object() |
| 177 |
|
| 178 |
|
| 179 |
def test_font_face_cmp_hash(): |
| 180 |
surface = cairo.ImageSurface(0, 10, 10) |
| 181 |
context = cairo.Context(surface) |
| 182 |
ff = context.get_font_face() |
| 183 |
other = context.get_font_face() |
| 184 |
assert ff == other |
| 185 |
assert not ff != other |
| 186 |
assert hash(ff) == hash(other) |
| 187 |
|
| 188 |
sf = context.get_scaled_font() |
| 189 |
other = context.get_scaled_font() |
| 190 |
assert sf == other |
| 191 |
assert not sf != other |
| 192 |
assert hash(sf) == hash(other) |
| 193 |
|
| 194 |
fo = context.get_font_options() |
| 195 |
|
| 196 |
|
| 197 |
with pytest.raises(TypeError): |
| 198 |
hash(fo) |
| 199 |
|
| 200 |
|
| 201 |
def test_scaled_font(scaled_font): |
| 202 |
with pytest.raises(TypeError): |
| 203 |
cairo.ScaledFont() |
| 204 |
|
| 205 |
assert scaled_font == scaled_font |
| 206 |
assert scaled_font != object() |
| 207 |
|
| 208 |
|
| 209 |
def test_scaled_font_extents(scaled_font): |
| 210 |
assert isinstance(scaled_font.extents(), tuple) |
| 211 |
|
| 212 |
|
| 213 |
def test_scaled_font_get_font_face(scaled_font): |
| 214 |
assert isinstance(scaled_font.get_font_face(), cairo.FontFace) |
| 215 |
|
| 216 |
|
| 217 |
def test_scaled_font_get_scale_matrix(scaled_font): |
| 218 |
assert isinstance(scaled_font.get_scale_matrix(), cairo.Matrix) |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy") |
| 223 |
def test_scaled_font_text_extents(scaled_font): |
| 224 |
with pytest.raises(TypeError): |
| 225 |
scaled_font.text_extents(object()) |
| 226 |
|
| 227 |
|
| 228 |
def test_scaled_font_glyph_extents(scaled_font): |
| 229 |
with pytest.raises(TypeError): |
| 230 |
scaled_font.glyph_extents(object()) |
| 231 |
with pytest.raises(TypeError): |
| 232 |
scaled_font.glyph_extents([object()]) |
| 233 |
with pytest.raises(TypeError): |
| 234 |
scaled_font.glyph_extents() |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy") |
| 239 |
def test_toy_font_face(): |
| 240 |
with pytest.raises(TypeError): |
| 241 |
cairo.ToyFontFace(object()) |
| 242 |
|
| 243 |
|
| 244 |
def test_toy_font_get_family(): |
| 245 |
font_face = cairo.ToyFontFace("") |
| 246 |
assert isinstance(font_face.get_family(), str) |
| 247 |
font_face = cairo.ToyFontFace("serif") |
| 248 |
assert isinstance(font_face.get_family(), str) |
| 249 |
font_face = cairo.ToyFontFace("sans-serif") |
| 250 |
assert isinstance(font_face.get_family(), str) |
| 251 |
|
| 252 |
|
| 253 |
def test_toy_font_get_slant(): |
| 254 |
font_face = cairo.ToyFontFace("") |
| 255 |
assert font_face.get_slant() == cairo.FontSlant.NORMAL |
| 256 |
assert isinstance(font_face.get_slant(), cairo.FontSlant) |
| 257 |
|
| 258 |
|
| 259 |
def test_toy_font_get_weight(): |
| 260 |
font_face = cairo.ToyFontFace("") |
| 261 |
assert font_face.get_weight() == cairo.FontWeight.NORMAL |
| 262 |
assert isinstance(font_face.get_weight(), cairo.FontWeight) |
| 263 |
|
| 264 |
|
| 265 |
def test_font_options_get_antialias(font_options): |
| 266 |
assert font_options.get_antialias() == cairo.Antialias.DEFAULT |
| 267 |
assert isinstance(font_options.get_antialias(), cairo.Antialias) |
| 268 |
|
| 269 |
|
| 270 |
def test_font_options_get_hint_metrics(font_options): |
| 271 |
assert font_options.get_hint_metrics() == cairo.HintMetrics.ON |
| 272 |
assert isinstance(font_options.get_hint_metrics(), cairo.HintMetrics) |
| 273 |
|
| 274 |
|
| 275 |
def test_font_options_get_hint_style(font_options): |
| 276 |
assert font_options.get_hint_style() == cairo.HintStyle.DEFAULT |
| 277 |
assert isinstance(font_options.get_hint_style(), cairo.HintStyle) |
| 278 |
|
| 279 |
|
| 280 |
def test_font_options_get_subpixel_order(font_options): |
| 281 |
assert font_options.get_subpixel_order() == cairo.SubpixelOrder.DEFAULT |
| 282 |
assert isinstance(font_options.get_subpixel_order(), cairo.SubpixelOrder) |
| 283 |
|
| 284 |
|
| 285 |
def test_scaled_font_get_ctm(): |
| 286 |
surface = cairo.ImageSurface(0, 10, 10) |
| 287 |
ctx = cairo.Context(surface) |
| 288 |
sf = ctx.get_scaled_font() |
| 289 |
matrix = sf.get_ctm() |
| 290 |
assert isinstance(matrix, cairo.Matrix) |
| 291 |
|
| 292 |
|
| 293 |
def test_scaled_font_get_font_matrix(): |
| 294 |
surface = cairo.ImageSurface(0, 10, 10) |
| 295 |
ctx = cairo.Context(surface) |
| 296 |
sf = ctx.get_scaled_font() |
| 297 |
matrix = sf.get_font_matrix() |
| 298 |
assert isinstance(matrix, cairo.Matrix) |
| 299 |
|
| 300 |
|
| 301 |
def test_scaled_font_get_font_options(): |
| 302 |
surface = cairo.ImageSurface(0, 10, 10) |
| 303 |
ctx = cairo.Context(surface) |
| 304 |
sf = ctx.get_scaled_font() |
| 305 |
font_options = sf.get_font_options() |
| 306 |
assert isinstance(font_options, cairo.FontOptions) |
| 307 |
|
| 308 |
|
| 309 |
def test_scaled_font_text_to_glyphs(): |
| 310 |
surface = cairo.ImageSurface(0, 10, 10) |
| 311 |
ctx = cairo.Context(surface) |
| 312 |
sf = ctx.get_scaled_font() |
| 313 |
assert sf.text_to_glyphs(0, 0, "") == ([], [], 0) |
| 314 |
glyphs, clusters, flags = sf.text_to_glyphs(0, 0, "a") |
| 315 |
assert sf.text_to_glyphs(0, 0, "a", True) == (glyphs, clusters, flags) |
| 316 |
assert len(glyphs) == 1 |
| 317 |
assert isinstance(glyphs[0], cairo.Glyph) |
| 318 |
assert len(clusters) == 1 |
| 319 |
assert isinstance(clusters[0], cairo.TextCluster) |
| 320 |
assert flags == 0 |
| 321 |
assert sf.text_to_glyphs(0, 0, "a", False) == glyphs |
| 322 |
glyphs, clusters, flags = sf.text_to_glyphs(0, 0, "a b") |
| 323 |
assert len(glyphs) == 3 |
| 324 |
assert glyphs[0] != glyphs[1] |
| 325 |
assert len(clusters) == 3 |
| 326 |
|
| 327 |
with pytest.raises(TypeError): |
| 328 |
sf.text_to_glyphs(object()) |