Skip to content

Commit ccca226

Browse files
committed
Merge pull request #2180 from dr0id/enhancement/font/add-missing-properties
font: ftfont should pass same tests as font
1 parent 3bf4dff commit ccca226

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

src_py/ftfont.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pygame._freetype import _internal_mod_init
1717
from pygame.sysfont import match_font, get_fonts, SysFont as _SysFont
1818
from pygame import encode_file_path
19+
from pygame import draw
1920

2021

2122
class Font(_Font):
@@ -58,6 +59,10 @@ def __init__(self, file=None, size=20):
5859
self.pad = True
5960
self.ucs4 = True
6061
self.underline_adjustment = 1.0
62+
self.strikethrough = False
63+
lower_style_name = self.style_name.lower() if file is not None else ""
64+
self._bold = "bold" in lower_style_name
65+
self._italic = "italic" in lower_style_name or "slanted" in lower_style_name
6166

6267
def render(self, text, antialias, color, bgcolor=None):
6368
"""render(text, antialias, color, background=None) -> Surface
@@ -75,6 +80,13 @@ def render(self, text, antialias, color, bgcolor=None):
7580
self.antialiased = bool(antialias)
7681
try:
7782
s, _ = super().render(text, color, bgcolor)
83+
if self.strikethrough:
84+
s_size = s.get_size()
85+
asc = self.get_ascent()
86+
start_pos = (0, asc * 15 // 24)
87+
end_pos = (s_size[0], start_pos[1])
88+
width = max(s_size[1] // 20, 1)
89+
draw.line(s, color, start_pos, end_pos, width)
7890
return s
7991
finally:
8092
self.antialiased = save_antialiased
@@ -83,27 +95,29 @@ def set_bold(self, value):
8395
"""set_bold(bool) -> None
8496
enable fake rendering of bold text"""
8597

86-
self.wide = bool(value)
98+
if not self._bold:
99+
self.wide = bool(value)
87100

88101
def get_bold(self):
89102
"""get_bold() -> bool
90103
check if text will be rendered bold"""
91104

92-
return self.wide
105+
return self.wide or self._bold
93106

94107
bold = property(get_bold, set_bold)
95108

96109
def set_italic(self, value):
97110
"""set_italic(bool) -> None
98111
enable fake rendering of italic text"""
99112

100-
self.oblique = bool(value)
113+
if not self._italic:
114+
self.oblique = bool(value)
101115

102116
def get_italic(self):
103117
"""get_italic() -> bool
104118
check if the text will be rendered italic"""
105119

106-
return self.oblique
120+
return self.oblique or self._italic
107121

108122
italic = property(get_italic, set_italic)
109123

@@ -114,11 +128,21 @@ def set_underline(self, value):
114128
self.underline = bool(value)
115129

116130
def get_underline(self):
117-
"""set_bold(bool) -> None
118-
enable fake rendering of bold text"""
131+
"""get_underline() -> bool
132+
check if the text will be rendered with an underline"""
119133

120134
return self.underline
121135

136+
def set_strikethrough(self, value):
137+
"""set_strikethrough(bool) -> None
138+
control if the text is rendered struck through"""
139+
self.strikethrough = value
140+
141+
def get_strikethrough(self):
142+
"""get_strikethrough() -> bool
143+
check if the text will be rendered struck through"""
144+
return self.strikethrough
145+
122146
def metrics(self, text):
123147
"""metrics(text) -> list
124148
Gets the metrics for each character in the passed string."""

test/font_test.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def test_match_font_all_exist(self):
117117
for font in fonts:
118118
path = pygame_font.match_font(font)
119119
self.assertFalse(path is None)
120-
self.assertTrue(os.path.isabs(path) and os.path.isfile(path))
120+
self.assertTrue(
121+
os.path.isabs(path) and os.path.isfile(path),
122+
msg=f"path '{path}': exists: {os.path.exists(path)} is abs path:{os.path.isabs(path)} and is file:{os.path.isfile(path)} is link: {os.path.islink(path)} is mount: {os.path.ismount(path)} is dir: {os.path.isdir(path)}",
123+
)
121124

122125
def test_match_font_name(self):
123126
"""That match_font accepts names of various types"""
@@ -492,13 +495,12 @@ def test_set_underline(self):
492495
self.assertFalse(f.get_underline())
493496

494497
def test_set_strikethrough(self):
495-
if pygame_font.__name__ != "pygame.ftfont":
496-
f = pygame_font.Font(None, 20)
497-
self.assertFalse(f.get_strikethrough())
498-
f.set_strikethrough(True)
499-
self.assertTrue(f.get_strikethrough())
500-
f.set_strikethrough(False)
501-
self.assertFalse(f.get_strikethrough())
498+
f = pygame_font.Font(None, 20)
499+
self.assertFalse(f.get_strikethrough())
500+
f.set_strikethrough(True)
501+
self.assertTrue(f.get_strikethrough())
502+
f.set_strikethrough(False)
503+
self.assertFalse(f.get_strikethrough())
502504

503505
def test_bold_attr(self):
504506
f = pygame_font.Font(None, 20)
@@ -525,13 +527,12 @@ def test_set_underline_property(self):
525527
self.assertFalse(f.underline)
526528

527529
def test_set_strikethrough_property(self):
528-
if pygame_font.__name__ != "pygame.ftfont":
529-
f = pygame_font.Font(None, 20)
530-
self.assertFalse(f.strikethrough)
531-
f.strikethrough = True
532-
self.assertTrue(f.strikethrough)
533-
f.strikethrough = False
534-
self.assertFalse(f.strikethrough)
530+
f = pygame_font.Font(None, 20)
531+
self.assertFalse(f.strikethrough)
532+
f.strikethrough = True
533+
self.assertTrue(f.strikethrough)
534+
f.strikethrough = False
535+
self.assertFalse(f.strikethrough)
535536

536537
def test_set_align_property(self):
537538
if pygame_font.__name__ == "pygame.ftfont":
@@ -863,16 +864,14 @@ def query(
863864
f.set_bold(bold)
864865
f.set_italic(italic)
865866
f.set_underline(underline)
866-
if pygame_font.__name__ != "pygame.ftfont":
867-
f.set_strikethrough(strikethrough)
867+
f.set_strikethrough(strikethrough)
868868
s = f.render(text, antialiase, (0, 0, 0))
869869
screen.blit(s, (offset, y))
870870
y += s.get_size()[1] + spacing
871871
f.set_bold(False)
872872
f.set_italic(False)
873873
f.set_underline(False)
874-
if pygame_font.__name__ != "pygame.ftfont":
875-
f.set_strikethrough(False)
874+
f.set_strikethrough(False)
876875
s = f.render("(some comparison text)", False, (0, 0, 0))
877876
screen.blit(s, (offset, y))
878877
pygame.display.flip()
@@ -900,8 +899,7 @@ def test_underline(self):
900899
self.assertTrue(self.query(underline=True))
901900

902901
def test_strikethrough(self):
903-
if pygame_font.__name__ != "pygame.ftfont":
904-
self.assertTrue(self.query(strikethrough=True))
902+
self.assertTrue(self.query(strikethrough=True))
905903

906904
def test_antialiase(self):
907905
self.assertTrue(self.query(antialiase=True))
@@ -913,8 +911,7 @@ def test_italic_underline(self):
913911
self.assertTrue(self.query(italic=True, underline=True))
914912

915913
def test_bold_strikethrough(self):
916-
if pygame_font.__name__ != "pygame.ftfont":
917-
self.assertTrue(self.query(bold=True, strikethrough=True))
914+
self.assertTrue(self.query(bold=True, strikethrough=True))
918915

919916

920917
if __name__ == "__main__":

0 commit comments

Comments
 (0)