File: sdlttf_test.py

package info (click to toggle)
pysdl2 0.9.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: python: 24,685; makefile: 36; sh: 8
file content (756 lines) | stat: -rw-r--r-- 30,648 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# -*- coding: utf-8 -*-
import os
import gc
import pytest
from struct import unpack
from ctypes import byref, c_int, c_uint16
import sdl2
from sdl2 import SDL_TRUE, SDL_FALSE, SDL_Color, surface, version, rwops
from sdl2.ext.compat import utf8

sdlttf = pytest.importorskip("sdl2.sdlttf")

parent_dir = os.path.dirname(os.path.abspath(__file__))
fontfile = os.path.join(parent_dir, "resources", "tuffy.ttf").encode("utf-8")
font_test_sizes = [6, 16, 26]

def _harfbuzz_version():
    major, minor, patch = c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_GetHarfBuzzVersion(byref(major), byref(minor), byref(patch))
    return (major.value, minor.value, patch.value)

def _has_harfbuzz():
    return _harfbuzz_version()[0] > 0

def to_utf16(x):
    # Converts a unicode Python string to a ctypes UTF-16 array
    strlen = len(x) + 1 # +1 for byte-order mark
    intstr = unpack('H' * strlen, utf8(x).encode('utf-16')) 
    intstr = intstr + (0, ) # Add null byte at end to terminate string
    return (c_uint16 * (strlen + 1))(*intstr)

@pytest.fixture(scope="module")
def with_sdl_ttf(with_sdl):
    ret = sdlttf.TTF_Init()
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    yield
    while sdlttf.TTF_WasInit() > 0:
        sdlttf.TTF_Quit()

@pytest.fixture()
def with_font(with_sdl_ttf):
    sdl2.SDL_ClearError()
    font = sdlttf.TTF_OpenFont(fontfile, 20)
    assert sdlttf.TTF_GetError() == b""
    assert font
    yield font
    sdlttf.TTF_CloseFont(font)


def test_TTF_Font():
    font = sdlttf.TTF_Font()
    assert isinstance(font, sdlttf.TTF_Font)

def test_TTF_InitQuit(with_sdl):
    # Test that init and quit actually work
    ret = sdlttf.TTF_Init()
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    assert sdlttf.TTF_WasInit()
    sdlttf.TTF_Quit()
    assert not sdlttf.TTF_WasInit()
    # Every time TTF_Init() is run, internal number increments by 1,
    # every time TTF_Quit() is run, internal number decrements by 1 and
    # only actually quits when internal number == 0
    sdlttf.TTF_Init()
    sdlttf.TTF_Init()
    sdlttf.TTF_Quit()
    assert sdlttf.TTF_WasInit()
    sdlttf.TTF_Quit()
    assert not sdlttf.TTF_WasInit()

def test_TTF_Linked_Version(with_sdl_ttf):
    v = sdlttf.TTF_Linked_Version()
    assert isinstance(v.contents, version.SDL_version)
    assert v.contents.major == 2
    assert v.contents.minor >= 0
    assert v.contents.patch >= 0
    t = (v.contents.major, v.contents.minor, v.contents.patch)
    assert t >= (2, 0, 12)
    assert t == sdlttf.dll.version_tuple

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_GetFreeTypeVersion(with_sdl_ttf):
    major, minor, patch = c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_GetFreeTypeVersion(byref(major), byref(minor), byref(patch))
    assert major.value > 0  # Only one guaranteed to be non-zero

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_GetHarfBuzzVersion(with_sdl_ttf):
    major, minor, patch = c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_GetHarfBuzzVersion(byref(major), byref(minor), byref(patch))
    assert major.value > 0  # Only one guaranteed to be non-zero

def test_TTF_ByteSwappedUNICODE(with_sdl_ttf):
    sdlttf.TTF_ByteSwappedUNICODE(0)
    sdlttf.TTF_ByteSwappedUNICODE(1)

def test_TTF_OpenCloseFont(with_sdl_ttf):
    for x in font_test_sizes:
        font = sdlttf.TTF_OpenFont(fontfile, x)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)

def test_TTF_OpenFontIndex(with_sdl_ttf):
    for x in font_test_sizes:
        font = sdlttf.TTF_OpenFontIndex(fontfile, x, 0)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)

def test_TTF_OpenFontRW(with_sdl_ttf):
    fp = open(fontfile, "rb")
    fontrw = rwops.rw_from_object(fp)
    for x in font_test_sizes:
        fp.seek(0)
        font = sdlttf.TTF_OpenFontRW(fontrw, 0, x)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)
    fp.close()

def test_TTF_OpenFontIndexRW(with_sdl_ttf):
    fp = open(fontfile, "rb")
    fontrw = rwops.rw_from_object(fp)
    for x in font_test_sizes:
        fp.seek(0)
        font = sdlttf.TTF_OpenFontIndexRW(fontrw, 0, x, 0)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)
    fp.close()

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_OpenFontDPI(with_sdl_ttf):
    # This actually tests for DPI having an effect on the text, the other
    # OpenFontDPI tests just test simple loading
    w1, h1, w2, h2 = c_int(0), c_int(0), c_int(0), c_int(0)
    font = sdlttf.TTF_OpenFontDPI(fontfile, 30, 80, 80)
    assert sdlttf.TTF_GetError() == b""
    assert isinstance(font.contents, sdlttf.TTF_Font)
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w1), byref(h1))
    sdlttf.TTF_CloseFont(font)
    font = sdlttf.TTF_OpenFontDPI(fontfile, 30, 100, 60)
    assert sdlttf.TTF_GetError() == b""
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    sdlttf.TTF_CloseFont(font)
    # Make sure text size differs between DPIs
    assert w2.value > w1.value
    assert h2.value < h1.value

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_OpenFontIndexDPI(with_sdl_ttf):
    test_dpi_sizes = [(50, 50), (80, 40), (100, 100)]
    for hdpi, vdpi in test_dpi_sizes:
        font = sdlttf.TTF_OpenFontIndexDPI(fontfile, 30, 0, hdpi, vdpi)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_OpenFontDPIRW(with_sdl_ttf):
    test_dpi_sizes = [(50, 50), (80, 40), (100, 100)]
    fp = open(fontfile, "rb")
    fontrw = rwops.rw_from_object(fp)
    for hdpi, vdpi in test_dpi_sizes:
        font = sdlttf.TTF_OpenFontDPIRW(fontrw, 0, 30, hdpi, vdpi)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)    

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_OpenFontIndexDPIRW(with_sdl_ttf):
    test_dpi_sizes = [(50, 50), (80, 40), (100, 100)]
    fp = open(fontfile, "rb")
    fontrw = rwops.rw_from_object(fp)
    for hdpi, vdpi in test_dpi_sizes:
        font = sdlttf.TTF_OpenFontIndexDPIRW(fontrw, 0, 30, 0, hdpi, vdpi)
        assert sdlttf.TTF_GetError() == b""
        assert isinstance(font.contents, sdlttf.TTF_Font)
        sdlttf.TTF_CloseFont(font)  

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_SetFontSize(with_font):
    if _harfbuzz_version() >= (4, 4, 0):
        # Character spacing currently broken for some reason
        pytest.skip("Incompatible HarfBuzz")
    font = with_font
    w1, h1, w2, h2 = c_int(0), c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w1), byref(h1))
    # Increase font size and make sure it works (original size is 20pt)
    ret = sdlttf.TTF_SetFontSize(font, 30)
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    assert w2.value > w1.value
    assert h2.value > h1.value
    # Decrease font size and make sure it works
    ret = sdlttf.TTF_SetFontSize(font, 10)
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    assert w2.value < w1.value
    assert h2.value < h1.value

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_SetFontSizeDPI(with_font):
    if _harfbuzz_version() >= (4, 4, 0):
        # Character spacing currently broken for some reason
        pytest.skip("Incompatible HarfBuzz")
    font = with_font
    w1, h1, w2, h2 = c_int(0), c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w1), byref(h1))
    # Adjust and make sure it works (original DPI is 72)
    ret = sdlttf.TTF_SetFontSizeDPI(font, 20, 100, 50)
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    assert w2.value > w1.value
    assert h2.value < h1.value
    # Resize the text with a different DPI and see if it works
    ret = sdlttf.TTF_SetFontSizeDPI(font, 20, 50, 100)
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    assert w2.value < w1.value
    assert h2.value > h1.value

def test_TTF_GetSetFontStyle(with_font):
    normal = sdlttf.TTF_STYLE_NORMAL
    bold = sdlttf.TTF_STYLE_BOLD
    italic = sdlttf.TTF_STYLE_ITALIC
    underline = sdlttf.TTF_STYLE_UNDERLINE
    # Test out getting/setting different font styles
    font = with_font
    assert sdlttf.TTF_GetFontStyle(font) == normal
    sdlttf.TTF_SetFontStyle(font, bold)
    assert sdlttf.TTF_GetFontStyle(font) == bold
    sdlttf.TTF_SetFontStyle(font, bold | italic)
    assert sdlttf.TTF_GetFontStyle(font) == bold | italic
    sdlttf.TTF_SetFontStyle(font, bold | underline)
    assert sdlttf.TTF_GetFontStyle(font) == bold | underline

def test_TTF_GetSetFontOutline(with_font):
    font = with_font
    assert sdlttf.TTF_GetFontOutline(font) == 0
    for x in range(1, 11, 2):
        sdlttf.TTF_SetFontOutline(font, x)
        assert sdlttf.TTF_GetFontOutline(font) == x

def test_TTF_GetSetFontHinting(with_font):
    font = with_font
    hints = [
        sdlttf.TTF_HINTING_NORMAL, sdlttf.TTF_HINTING_LIGHT,
        sdlttf.TTF_HINTING_MONO, sdlttf.TTF_HINTING_NONE
    ]
    if sdlttf.dll.version_tuple >= (2, 0, 18):
        hints.append(sdlttf.TTF_HINTING_LIGHT_SUBPIXEL)
    assert sdlttf.TTF_GetFontHinting(font) == sdlttf.TTF_HINTING_NORMAL
    for hint in hints:
        sdlttf.TTF_SetFontHinting(font, hint)
        assert sdlttf.TTF_GetFontHinting(font) == hint

@pytest.mark.skipif(sdlttf.dll.version < 2200, reason="not available")
def test_TTF_GetSetFontWrappedAlign(with_font):
    font = with_font
    alignments = [
        sdlttf.TTF_WRAPPED_ALIGN_LEFT, sdlttf.TTF_WRAPPED_ALIGN_CENTER,
        sdlttf.TTF_WRAPPED_ALIGN_RIGHT,
    ]
    assert sdlttf.TTF_GetFontWrappedAlign(font) == sdlttf.TTF_WRAPPED_ALIGN_LEFT
    for align in alignments:
        sdlttf.TTF_SetFontWrappedAlign(font, align)
        assert sdlttf.TTF_GetFontWrappedAlign(font) == align

def test_TTF_FontHeight(with_sdl_ttf):
    last = cur = 0
    for ptsize in font_test_sizes:
        font = sdlttf.TTF_OpenFont(fontfile, ptsize)
        cur = sdlttf.TTF_FontHeight(font)
        assert cur >= last
        last = cur
        sdlttf.TTF_CloseFont(font)

def test_TTF_FontAscent(with_sdl_ttf):
    last = cur = 0
    for ptsize in font_test_sizes:
        font = sdlttf.TTF_OpenFont(fontfile, ptsize)
        cur = sdlttf.TTF_FontAscent(font)
        assert cur >= last
        last = cur
        sdlttf.TTF_CloseFont(font)

def test_TTF_FontDescent(with_sdl_ttf):
    last = cur = 0
    for ptsize in font_test_sizes:
        font = sdlttf.TTF_OpenFont(fontfile, ptsize)
        cur = sdlttf.TTF_FontDescent(font)
        assert cur <= last
        last = cur
        sdlttf.TTF_CloseFont(font)

def test_TTF_FontLineSkip(with_sdl_ttf):
    last = cur = 0
    for ptsize in font_test_sizes:
        font = sdlttf.TTF_OpenFont(fontfile, ptsize)
        cur = sdlttf.TTF_FontLineSkip(font)
        assert cur >= last
        last = cur
        sdlttf.TTF_CloseFont(font)

def test_TTF_GetSetFontKerning(with_font):
    font = with_font
    assert sdlttf.TTF_GetFontKerning(font) == 1
    sdlttf.TTF_SetFontKerning(font, 0)
    assert sdlttf.TTF_GetFontKerning(font) == 0
    sdlttf.TTF_SetFontKerning(font, 1)
    assert sdlttf.TTF_GetFontKerning(font) == 1
    sdlttf.TTF_SetFontKerning(font, 0)
    assert sdlttf.TTF_GetFontKerning(font) == 0

def test_TTF_FontFaces(with_font):
    font = with_font
    assert sdlttf.TTF_FontFaces(font) >= 1

def test_TTF_FontFaceIsFixedWidth(with_font):
    font = with_font
    assert not sdlttf.TTF_FontFaceIsFixedWidth(font)

def test_TTF_FontFaceFamilyName(with_font):
    font = with_font
    assert sdlttf.TTF_FontFaceFamilyName(font) == b"Tuffy"

def test_TTF_FontFaceStyleName(with_font):
    font = with_font
    assert sdlttf.TTF_FontFaceStyleName(font) == b"Regular"

def test_TTF_GlyphIsProvided(with_font):
    font = with_font
    assert isinstance(font.contents, sdlttf.TTF_Font)
    for ch in range(32, 127):
        assert sdlttf.TTF_GlyphIsProvided(font, ch)
    assert not sdlttf.TTF_GlyphIsProvided(font, 0)
    assert not sdlttf.TTF_GlyphIsProvided(font, 0x0ff9)

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_GlyphIsProvided32(with_font):
    font = with_font
    assert isinstance(font.contents, sdlttf.TTF_Font)
    for ch in range(32, 127):
        assert sdlttf.TTF_GlyphIsProvided32(font, ch)
    assert not sdlttf.TTF_GlyphIsProvided32(font, 0)
    assert not sdlttf.TTF_GlyphIsProvided32(font, 0x0ff9)

def test_TTF_GlyphMetrics(with_sdl_ttf):
    expected = {
        'A': [1, 25, 0, 29, 25],
        'j': [-3, 7, -9, 28, 9],
        '.': [2, 7, -1, 4, 8]
    }
    font = sdlttf.TTF_OpenFont(fontfile, 40)
    minX, maxX, minY, maxY = c_int(0), c_int(0), c_int(0), c_int(0)
    adv = c_int(0)
    for char in expected.keys():
        ret = sdlttf.TTF_GlyphMetrics(
            font, ord(char),
            byref(minX), byref(maxX), byref(minY), byref(maxY), byref(adv)
        )
        results = [x.value for x in (minX, maxX, minY, maxY, adv)]
        assert sdlttf.TTF_GetError() == b""
        assert ret == 0
        assert results == expected[char]
    sdlttf.TTF_CloseFont(font)

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_GlyphMetrics32(with_sdl_ttf):
    expected = {
        'A': [1, 25, 0, 29, 25],
        'j': [-3, 7, -9, 28, 9],
        '.': [2, 7, -1, 4, 8]
    }
    font = sdlttf.TTF_OpenFont(fontfile, 40)
    minX, maxX, minY, maxY = c_int(0), c_int(0), c_int(0), c_int(0)
    adv = c_int(0)
    for char in expected.keys():
        ret = sdlttf.TTF_GlyphMetrics32(
            font, ord(char),
            byref(minX), byref(maxX), byref(minY), byref(maxY), byref(adv)
        )
        results = [x.value for x in (minX, maxX, minY, maxY, adv)]
        assert sdlttf.TTF_GetError() == b""
        assert ret == 0
        assert results == expected[char]
    sdlttf.TTF_CloseFont(font)

def test_TTF_SizeText(with_font):
    font = with_font
    min_expected_w = 69     # SDL2_ttf 2.0.18
    max_expected_w = 70     # SDL2_ttf <= 2.0.15
    min_expected_h = 21     # SDL2_ttf 2.0.15 with FreeType 2.10.1
    max_expected_h = 25     # SDL2_ttf < 2.0.15
    w, h = c_int(0), c_int(0)
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w), byref(h))
    assert w.value >= min_expected_w
    assert w.value <= max_expected_w
    assert h.value >= min_expected_h
    assert h.value <= max_expected_h

def test_TTF_SizeUTF8(with_font):
    font = with_font
    min_expected_w = 72     # SDL2_ttf 2.0.18
    max_expected_w = 73     # SDL2_ttf <= 2.0.15
    min_expected_h = 21     # SDL2_ttf 2.0.15 with FreeType 2.10.1
    max_expected_h = 25     # SDL2_ttf < 2.0.15
    w, h = c_int(0), c_int(0)
    sdlttf.TTF_SizeUTF8(font, u"Hï thère!".encode('utf-8'), byref(w), byref(h))
    assert w.value >= min_expected_w
    assert w.value <= max_expected_w
    assert h.value >= min_expected_h
    assert h.value <= max_expected_h

def test_TTF_SizeUNICODE(with_font):
    font = with_font
    min_expected_w = 69     # SDL2_ttf 2.0.18
    max_expected_w = 70     # SDL2_ttf <= 2.0.15
    min_expected_h = 21     # SDL2_ttf 2.0.15 with FreeType 2.10.1
    max_expected_h = 25     # SDL2_ttf < 2.0.15
    w, h = c_int(0), c_int(0)
    strarr = to_utf16(u"Hi there!")
    sdlttf.TTF_SizeUNICODE(font, strarr, byref(w), byref(h))
    # For debug purposes
    #print(list(strarr))
    #print("w = {0}, h = {1}".format(w.value, h.value))
    assert w.value >= min_expected_w
    assert w.value <= max_expected_w
    assert h.value >= min_expected_h
    assert h.value <= max_expected_h

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_Measure(with_font):
    font = with_font
    extent, count = c_int(0), c_int(0)
    w, h = c_int(0), c_int(0)
    tst = b"This is a long line that should be wrapped!"
    # With TTF_MeasureText
    ret = sdlttf.TTF_MeasureText(font, tst, 200, byref(extent), byref(count))
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    assert all([x.value > 0 for x in (extent, count)])
    sdlttf.TTF_SizeText(font, tst[:count.value], byref(w), byref(h))
    assert extent.value == w.value
    # With TTF_MeasureUTF8
    ret = sdlttf.TTF_MeasureUTF8(font, tst, 180, byref(extent), byref(count))
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    assert all([x.value > 0 for x in (extent, count)])
    sdlttf.TTF_SizeUTF8(font, tst[:count.value], byref(w), byref(h))
    assert extent.value == w.value
    # With TTF_MeasureUNICODE
    strarr = to_utf16(tst)
    ret = sdlttf.TTF_MeasureUNICODE(font, strarr, 220, byref(extent), byref(count))
    assert sdlttf.TTF_GetError() == b""
    assert ret == 0
    assert all([x.value > 0 for x in (extent, count)])
    strarr = to_utf16(tst[:count.value])
    sdlttf.TTF_SizeUNICODE(font, strarr, byref(w), byref(h))
    assert extent.value == w.value

def test_TTF_Render_Solid(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0)
    # Test TTF_RenderText_Solid
    sf = sdlttf.TTF_RenderText_Solid(font, b"Hi there!", color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUTF8_Solid
    teststr = u"Hï thère!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_Solid(font, teststr, color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUNICODE_Solid
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there!")
    sf = sdlttf.TTF_RenderUNICODE_Solid(font, strarr, color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph_Solid
    sf = sdlttf.TTF_RenderGlyph_Solid(font, ord("A"), color)
    assert isinstance(sf.contents, surface.SDL_Surface)

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_Render_Solid_Wrapped(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0, 255)
    # Test TTF_RenderText_Solid_Wrapped
    teststr = b"Hi there, this is a long line!"
    sf = sdlttf.TTF_RenderText_Solid_Wrapped(font, teststr, color, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUTF8_Solid_Wrapped
    teststr = u"Hï thère, this is a long line!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_Solid_Wrapped(font, teststr, color, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUNICODE_Solid_Wrapped
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there, this is a long line!")
    sf = sdlttf.TTF_RenderUNICODE_Solid_Wrapped(font, strarr, color, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30

def test_TTF_Render_Shaded(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0)
    bgcolor = SDL_Color(255, 255, 255)
    # Test TTF_RenderText_Shaded
    sf = sdlttf.TTF_RenderText_Shaded(font, b"Hi there!", color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUTF8_Shaded
    teststr = u"Hï thère!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_Shaded(font, teststr, color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUNICODE_Shaded
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there!")
    sf = sdlttf.TTF_RenderUNICODE_Shaded(font, strarr, color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph_Shaded
    sf = sdlttf.TTF_RenderGlyph_Shaded(font, ord("A"), color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_Render_Shaded_Wrapped(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0, 255)
    bgcolor = SDL_Color(255, 255, 255)
    # Test TTF_RenderText_Shaded_Wrapped
    teststr = b"Hi there, this is a long line!"
    sf = sdlttf.TTF_RenderText_Shaded_Wrapped(font, teststr, color, bgcolor, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUTF8_Shaded_Wrapped
    teststr = u"Hï thère, this is a long line!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_Shaded_Wrapped(font, teststr, color, bgcolor, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUNICODE_Shaded_Wrapped
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there, this is a long line!")
    sf = sdlttf.TTF_RenderUNICODE_Shaded_Wrapped(font, strarr, color, bgcolor, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30

def test_TTF_Render_Blended(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0, 255)
    # Test TTF_RenderText_Blended
    sf = sdlttf.TTF_RenderText_Blended(font, b"Hi there!", color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUTF8_Blended
    teststr = u"Hï thère!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_Blended(font, teststr, color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUNICODE_Blended
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there!")
    sf = sdlttf.TTF_RenderUNICODE_Blended(font, strarr, color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph_Blended
    sf = sdlttf.TTF_RenderGlyph_Blended(font, ord("A"), color)
    assert isinstance(sf.contents, surface.SDL_Surface)

def test_TTF_Render_Blended_Wrapped(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0, 255)
    # Test TTF_RenderText_Blended_Wrapped
    teststr = b"Hi there, this is a long line!"
    sf = sdlttf.TTF_RenderText_Blended_Wrapped(font, teststr, color, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUTF8_Blended_Wrapped
    teststr = u"Hï thère, this is a long line!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_Blended_Wrapped(font, teststr, color, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUNICODE_Blended_Wrapped
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there, this is a long line!")
    sf = sdlttf.TTF_RenderUNICODE_Blended_Wrapped(font, strarr, color, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30

@pytest.mark.skipif(sdlttf.dll.version < 2200, reason="not available")
def test_TTF_Render_LCD(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0)
    bgcolor = SDL_Color(255, 255, 255, 255)
    # Test TTF_RenderText_LCD
    sf = sdlttf.TTF_RenderText_LCD(font, b"Hi there!", color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUTF8_LCD
    teststr = u"Hï thère!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_LCD(font, teststr, color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderUNICODE_LCD
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there!")
    sf = sdlttf.TTF_RenderUNICODE_LCD(font, strarr, color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph_LCD
    sf = sdlttf.TTF_RenderGlyph_LCD(font, ord("A"), color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph32_LCD
    sf = sdlttf.TTF_RenderGlyph32_LCD(font, ord("A"), color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)

@pytest.mark.skipif(sdlttf.dll.version < 2200, reason="not available")
def test_TTF_Render_LCD_Wrapped(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0, 255)
    bgcolor = SDL_Color(255, 255, 255, 255)
    # Test TTF_RenderText_LCD_Wrapped
    teststr = b"Hi there, this is a long line!"
    sf = sdlttf.TTF_RenderText_LCD_Wrapped(font, teststr, color, bgcolor, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUTF8_LCD_Wrapped
    teststr = u"Hï thère, this is a long line!".encode('utf-8')
    sf = sdlttf.TTF_RenderUTF8_LCD_Wrapped(font, teststr, color, bgcolor, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30
    # Test TTF_RenderUNICODE_LCD_Wrapped
    # NOTE: no unicode chars because number -> glyph lookup is os-dependent
    strarr = to_utf16(u"Hi there, this is a long line!")
    sf = sdlttf.TTF_RenderUNICODE_LCD_Wrapped(font, strarr, color, bgcolor, 100)
    assert isinstance(sf.contents, surface.SDL_Surface)
    assert sf.contents.h > 30

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_RenderGlyph32(with_font):
    font = with_font
    color = SDL_Color(0, 0, 0)
    bgcolor = SDL_Color(255, 255, 255)
    # Test TTF_RenderGlyph32_Solid
    sf = sdlttf.TTF_RenderGlyph32_Solid(font, ord("A"), color)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph32_Shaded
    sf = sdlttf.TTF_RenderGlyph32_Shaded(font, ord("A"), color, bgcolor)
    assert isinstance(sf.contents, surface.SDL_Surface)
    # Test TTF_RenderGlyph32_Blended
    sf = sdlttf.TTF_RenderGlyph32_Blended(font, ord("A"), color)
    assert isinstance(sf.contents, surface.SDL_Surface)

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_SetDirection(with_font):
    font = with_font
    # Define HarfBuzz direction constants
    HB_DIRECTION_LTR = 4
    HB_DIRECTION_TTB = 6
    # Only available if compiled with HarfBuzz
    if not _has_harfbuzz():
        pytest.skip("No HarfBuzz")
    # Try setting the script direction
    ret = sdlttf.TTF_SetDirection(HB_DIRECTION_LTR)
    assert ret == 0
    # Try changing the script direction to see if it has any effect
    w1, h1, w2, h2 = c_int(0), c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w1), byref(h1))
    ret = sdlttf.TTF_SetDirection(HB_DIRECTION_TTB)
    assert ret == 0
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    sdlttf.TTF_SetDirection(HB_DIRECTION_LTR) # Reset direction
    assert w1.value > w2.value
    assert h1.value < h2.value

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_SetScript(with_font):
    # Only available if compiled with HarfBuzz
    if not _has_harfbuzz():
        pytest.skip("No HarfBuzz")
    # Try setting the script language to Arabic and back
    ret = sdlttf.TTF_SetScript(sdlttf.HB_TAG("A", "r", "a", "b"))
    assert ret == 0
    # NOTE: I have no clue how to write a proper test to see if this worked
    ret = sdlttf.TTF_SetScript(sdlttf.HB_TAG("Z", "y", "y", "y"))
    assert ret == 0

@pytest.mark.skipif(sdlttf.dll.version < 2200, reason="not available")
def test_TTF_SetFontDirection(with_font):
    font = with_font
    # Only available if compiled with HarfBuzz
    if not _has_harfbuzz():
        pytest.skip("No HarfBuzz")
    # Try setting the script direction
    ret = sdlttf.TTF_SetFontDirection(font, sdlttf.TTF_DIRECTION_LTR)
    assert ret == 0
    # Try changing the script direction to see if it has any effect
    w1, h1, w2, h2 = c_int(0), c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w1), byref(h1))
    ret = sdlttf.TTF_SetFontDirection(font, sdlttf.TTF_DIRECTION_TTB)
    assert ret == 0
    sdlttf.TTF_SizeText(font, b"Hi there!", byref(w2), byref(h2))
    assert w1.value > w2.value
    assert h1.value < h2.value

@pytest.mark.skipif(sdlttf.dll.version < 2200, reason="not available")
def test_TTF_SetFontScriptName(with_font):
    # NOTE: I have no clue how to write a proper test to see if this works
    font = with_font
    # Only available if compiled with HarfBuzz
    if not _has_harfbuzz():
        pytest.skip("No HarfBuzz")
    # Try setting the script language to Arabic
    ret = sdlttf.TTF_SetFontScriptName(font, b"Arab")
    assert ret == 0

@pytest.mark.skipif(sdlttf.dll.version < 2014, reason="not available")
def test_TTF_GetFontKerningSizeGlyphs(with_font):
    font = with_font
    # NOTE: Test font (tuffy) has no kerning info, so retval is always 0
    sz = sdlttf.TTF_GetFontKerningSizeGlyphs(font, ord("A"), ord("B"))
    assert sz == 0

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_GetFontKerningSizeGlyphs32(with_font):
    font = with_font
    # NOTE: Test font (tuffy) has no kerning info, so retval is always 0
    sz = sdlttf.TTF_GetFontKerningSizeGlyphs32(font, ord("A"), ord("B"))
    assert sz == 0

@pytest.mark.skipif(sdlttf.dll.version < 2018, reason="not available")
def test_TTF_GetSetFontSDF(with_font):
    font = with_font
    # Only available with FreeType >= 2.11.0
    major, minor, patch = c_int(0), c_int(0), c_int(0)
    sdlttf.TTF_GetFreeTypeVersion(byref(major), byref(minor), byref(patch))
    if minor.value < 11:
        pytest.skip("SDF not available")
    # Try enabling SDF rendering on a given font
    ret = sdlttf.TTF_SetFontSDF(font, SDL_TRUE)
    assert ret == 0
    assert sdlttf.TTF_GetFontSDF(font) == SDL_TRUE
    # Try disabling SDF rendering on a given font
    ret = sdlttf.TTF_SetFontSDF(font, SDL_FALSE)
    assert ret == 0
    assert sdlttf.TTF_GetFontSDF(font) == SDL_FALSE


def test_HB_TAG():
    test_scripts = {
        "Arab": 1098015074,
        "Mong": 1299148391,
        "Zyyy": 1517910393, # HB_SCRIPT_COMMON
        "Zzzz": 1517976186, # HB_SCRIPT_UNKNOWN
    }
    for script, expected in test_scripts.items():
        c1, c2, c3, c4 = script
        assert sdlttf.HB_TAG(c1, c2, c3, c4) == expected