-
-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an example of filtering pre-installed fonts by language #2592
base: main
Are you sure you want to change the base?
Add an example of filtering pre-installed fonts by language #2592
Conversation
You need to run |
I think we can make this work without freetype if we fix a bug in font.c |
Line 885 should have:
|
Then we can rewrite example to be: """
This example demonstrates how to find fonts for each language.
"""
from typing import Iterator
from pygame.font import get_fonts, SysFont
import pygame
def enum_font_from_lang(lang) -> Iterator[str]:
"""
Enumerates fonts that are suitable for a specified language.
"""
text = DISCRIMINANTS[lang]
for font_name in get_fonts():
if can_render_text(font_name, text):
yield font_name
def can_render_text(font_name, text, *, font_size=14) -> bool:
"""
Whether a specified font is capable of rendering a specified text.
"""
try:
font = SysFont(font_name, font_size)
except AttributeError as e:
# AttributeError occurs on certain fonts, which may be a PyGame bug.
if e.args[0] == r"this style is unsupported for a bitmap font":
return False
else:
raise
for c in text:
font_metrics = font.metrics(c)
print(font_name, c, font_metrics)
if None in font_metrics:
return False
return True
DISCRIMINANTS = {
"ja": "経伝説あAB",
"ko": "안녕조AB",
"zh-Hans": "哪经传说AB",
"zh-Hant": "哪經傳說AB",
"zh": "哪經傳說经传说AB",
}
"""
You may have noticed the 'AB' in the values of this dictionary.
If you don't include ASCII characters, you might choose a font that cannot render them, such as a fallback font,
which is probably not what you want.
"""
def main():
pygame.init()
screen = pygame.display.set_mode((800, 800))
screen.fill("white")
x, y = 40, 10
spacing = 10
for font_name in enum_font_from_lang("ja"):
font = SysFont(font_name, 30)
rendered_text = font.render(
"経伝説 한글 经传说 ABC 經傳說 あいう", True, pygame.Color("black")
).convert_alpha()
rect = rendered_text.get_rect()
rect.topleft = (x, y)
screen.blit(rendered_text, rect)
y = y + rect.height + spacing
pygame.display.flip()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
clock.tick(4)
pygame.quit()
if __name__ == "__main__":
main() |
@yunline @MyreMylar Thank you but I'm not familiar enough with PyGame nor SDL to modify its code, so I shouldn't do that. |
No worries, I wasn't expecting you to solve this. I have along standing plan to gradually deprecate the freetype submodule so I will go to great lengths to keep it from expanding further throughout the codebase. Anyway I made a PR to look at this issue in font further here: |
Just to be clear, there is a long running confusion over pygame/pygame-ce having multiple font handling submodules ( What I would like is for pygame to have one clear user and developer story on handling fonts while wrapping the freetype library. In the present time, the Centralising will be a slow process of first making sure we can support all the use cases of the other submodules with code in The documentation for the |
@MyreMylar Thank you for clarification :) I'll rewrite the example without using the |
We also have a draft PR that adds is_char_defined to Font. #2178 If that feature lands would that become the preferred way of doing this? |
@Starbuck5 I feel like so. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example itself seems fine but is awaiting another PR. Should we merge it as is anyway?
I might need to add more comments to the example if you merge it as is, such as
|
This example demonstrates how to filter the pre-installed fonts on the system, by a language.