forked from beeware/toga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonts.py
73 lines (64 loc) · 2.37 KB
/
fonts.py
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
from toga.fonts import (
BOLD,
CURSIVE,
FANTASY,
ITALIC,
MESSAGE,
MONOSPACE,
NORMAL,
OBLIQUE,
SANS_SERIF,
SERIF,
SMALL_CAPS,
SYSTEM,
SYSTEM_DEFAULT_FONT_SIZE,
)
from toga_iOS.libs import (
UIFontDescriptorTraitBold,
UIFontDescriptorTraitItalic,
)
class FontMixin:
supports_custom_fonts = True
supports_custom_variable_fonts = False
def assert_font_options(self, weight=NORMAL, style=NORMAL, variant=NORMAL):
# Cocoa's FANTASY (Papyrus) and CURSIVE (Snell Roundhand) system
# fonts don't have any bold/italic variants.
if str(self.font.familyName) == "Papyrus":
print("Ignoring options on FANTASY system font")
return
elif str(self.font.familyName) == "Snell Roundhand":
print("Ignoring options on CURSIVE system font")
return
traits = self.font.fontDescriptor.symbolicTraits
assert (BOLD if traits & UIFontDescriptorTraitBold else NORMAL) == weight
if style == OBLIQUE:
print("Interpreting OBLIQUE font as ITALIC")
assert bool(traits & UIFontDescriptorTraitItalic)
else:
assert ITALIC if traits & UIFontDescriptorTraitItalic else NORMAL == style
if variant == SMALL_CAPS:
print("Ignoring SMALL CAPS font test")
else:
assert NORMAL == variant
def assert_font_size(self, expected):
if expected == SYSTEM_DEFAULT_FONT_SIZE:
assert self.font.pointSize == 17
else:
assert self.font.pointSize == expected * 96 / 72
def assert_font_family(self, expected):
assert str(self.font.familyName) == {
# System and Message fonts use internal names
SYSTEM: ".AppleSystemUIFont",
MESSAGE: ".AppleSystemUIFont",
# Known fonts use pre-registered names
CURSIVE: "Snell Roundhand",
FANTASY: "Papyrus",
MONOSPACE: "Courier New",
SANS_SERIF: "Helvetica",
SERIF: "Times New Roman",
# Most other fonts we can just use the family name;
# however, the Font Awesome font has a different
# internal Postscript name, which *doesn't* include
# the "solid" weight component.
"Font Awesome 5 Free Solid": "Font Awesome 5 Free",
}.get(expected, expected)