| 1 |
"""Tests for loading and accessing FreeType font faces.""" |
| 2 |
import pytest |
| 3 |
|
| 4 |
from tests.annotations import Platform, require_platform |
| 5 |
|
| 6 |
try: |
| 7 |
from pyglet.font.freetype import FreeTypeFace, FreeTypeMemoryFace |
| 8 |
from pyglet.font.fontconfig import get_fontconfig |
| 9 |
except ImportError: |
| 10 |
FreeTypeFace = None |
| 11 |
FreeTypeMemoryFace = None |
| 12 |
get_fontconfig = None |
| 13 |
|
| 14 |
|
| 15 |
@require_platform(Platform.LINUX) |
| 16 |
@pytest.mark.parametrize('font_file_name,font_name,bold,italic', [ |
| 17 |
('action_man.ttf', 'Action Man', False, False), |
| 18 |
('action_man_bold.ttf', 'Action Man', True, False), |
| 19 |
('action_man_bold_italic.ttf', 'Action Man', True, True), |
| 20 |
('action_man_italic.ttf', 'Action Man', False, True), |
| 21 |
]) |
| 22 |
def test_face_from_file(test_data, font_file_name, font_name, bold, italic): |
| 23 |
"""Test loading a font face directly from file.""" |
| 24 |
face = FreeTypeFace.from_file(test_data.get_file('fonts', font_file_name)) |
| 25 |
|
| 26 |
assert face.name == font_name |
| 27 |
assert face.family_name == font_name |
| 28 |
assert face.bold == bold |
| 29 |
assert face.italic == italic |
| 30 |
|
| 31 |
del face |
| 32 |
|
| 33 |
|
| 34 |
@require_platform(Platform.LINUX) |
| 35 |
@pytest.mark.parametrize('font_name,bold,italic', [ |
| 36 |
('Arial', False, False), |
| 37 |
('Arial', True, False), |
| 38 |
('Arial', False, True), |
| 39 |
('Arial', True, True), |
| 40 |
]) |
| 41 |
def test_face_from_fontconfig(font_name, bold, italic): |
| 42 |
"""Test loading a font face from the system using font config.""" |
| 43 |
match = get_fontconfig().find_font(font_name, 16, bold, italic) |
| 44 |
assert match is not None |
| 45 |
|
| 46 |
face = FreeTypeFace.from_fontconfig(match) |
| 47 |
|
| 48 |
assert face.bold == bold |
| 49 |
assert face.italic == italic |
| 50 |
|
| 51 |
del face |
| 52 |
|
| 53 |
|
| 54 |
@require_platform(Platform.LINUX) |
| 55 |
@pytest.mark.parametrize('font_file_name,font_name,bold,italic', [ |
| 56 |
('action_man.ttf', 'Action Man', False, False), |
| 57 |
('action_man_bold.ttf', 'Action Man', True, False), |
| 58 |
('action_man_bold_italic.ttf', 'Action Man', True, True), |
| 59 |
('action_man_italic.ttf', 'Action Man', False, True), |
| 60 |
]) |
| 61 |
def test_memory_face(test_data, font_file_name, font_name, bold, italic): |
| 62 |
"""Test loading a font into memory using FreeTypeMemoryFont.""" |
| 63 |
with open(test_data.get_file('fonts', font_file_name), 'rb') as font_file: |
| 64 |
font_data = font_file.read() |
| 65 |
font = FreeTypeMemoryFace(font_data, 0) |
| 66 |
|
| 67 |
assert font.name == font_name |
| 68 |
assert font.bold == bold |
| 69 |
assert font.italic == italic |
| 70 |
assert font.ft_face is not None |
| 71 |
|
| 72 |
del font |
| 73 |
|
| 74 |
|
| 75 |
@require_platform(Platform.LINUX) |
| 76 |
@pytest.mark.parametrize('font_file_name,size,dpi,ascent,descent', [ |
| 77 |
('action_man.ttf', 16, 96, 15, -4), |
| 78 |
('action_man.ttf', 10, 96, 9, -3), |
| 79 |
('action_man.ttf', 16, 72, 11, -3), |
| 80 |
('courR12-ISO8859-1.pcf', 16, 96, 12, -3), |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
]) |
| 85 |
def test_face_metrics(test_data, font_file_name, size, dpi, ascent, descent): |
| 86 |
"""Test a face from file and check the metrics.""" |
| 87 |
face = FreeTypeFace.from_file(test_data.get_file('fonts', font_file_name)) |
| 88 |
metrics = face.get_font_metrics(size, dpi) |
| 89 |
|
| 90 |
assert metrics.ascent == ascent |
| 91 |
assert metrics.descent == descent |
| 92 |
|
| 93 |
|
| 94 |
@require_platform(Platform.LINUX) |
| 95 |
@pytest.mark.parametrize('font_file_name,character,index', [ |
| 96 |
('action_man.ttf', 'a', 65), |
| 97 |
('action_man.ttf', 'A', 33), |
| 98 |
('action_man.ttf', '1', 17), |
| 99 |
('action_man.ttf', '#', 3), |
| 100 |
('action_man.ttf', 'b', 66), |
| 101 |
]) |
| 102 |
def test_character_index(test_data, font_file_name, character, index): |
| 103 |
"""Test getting the glyph index for a character.""" |
| 104 |
face = FreeTypeFace.from_file(test_data.get_file('fonts', font_file_name)) |
| 105 |
assert face.get_character_index(character) == index |
| 106 |
|
| 107 |
|
| 108 |
@require_platform(Platform.LINUX) |
| 109 |
@pytest.mark.parametrize('font_file_name,glyph_index', [ |
| 110 |
('action_man.ttf', 65), |
| 111 |
('action_man.ttf', 33), |
| 112 |
('action_man.ttf', 17), |
| 113 |
('action_man.ttf', 3), |
| 114 |
('action_man.ttf', 66), |
| 115 |
]) |
| 116 |
def test_get_glyph_slot(test_data, font_file_name, glyph_index): |
| 117 |
"""Test getting a glyph slot from the face.""" |
| 118 |
face = FreeTypeFace.from_file(test_data.get_file('fonts', font_file_name)) |
| 119 |
face.set_char_size(size=16, dpi=92) |
| 120 |
glyph = face.get_glyph_slot(glyph_index) |
| 121 |
|
| 122 |
assert glyph is not None |
| 123 |
|