-
-
Notifications
You must be signed in to change notification settings - Fork 166
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 full unicode support to Font.metrics
#3328
Open
ankith26
wants to merge
1
commit into
main
Choose a base branch
from
ankith26-font-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To test this PR I wrote a script (albeit with some help from gpt) to test font rendering with a wide range of unicode points and tested that Code usedimport pygame
# Initialize Pygame
pygame.init()
FONT_SIZE = 30
# Set up the screen
WIDTH, HEIGHT = 1000, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame Font Metrics Demo (Noto Fonts)")
# Define font-character mapping
noto_font_samples = {
"Noto Sans": ["A", "é", "ñ", "Ω", "Ж"],
"Noto Serif": ["A", "ß", "Ж", "Σ", "λ"],
"Noto Sans CJK SC": ["字", "汉", "日本", "韓國", "學"],
"Noto Sans Arabic": ["العربية", "ب", "ك", "م", "ن"],
"Noto Sans Devanagari": ["नमस्ते", "श", "क", "त्र", "ध"],
"Noto Music": ["𝄞", "𝄢", "𝅘", "𝅘𝅥", "𝆑"],
"Noto Color Emoji": ["🤖", "🔥", "💡", "🎵", "🚀"],
"Noto Sans Math": ["∑", "∞", "∂", "∫", "∇"],
"Noto Sans Old Persian": ["𐎠", "𐎢", "𐎴", "𐎽", "𐎹"],
"Noto Sans Symbols": ["⚛", "♻", "⚔", "☯", "⚰"],
"Noto Sans Symbols 2": ["⯑", "⯒", "⯓", "⯔", "⯕"],
}
noto_font_to_script = {
"Noto Sans CJK SC": ("Hani", pygame.DIRECTION_LTR),
"Noto Sans Arabic": ("Arab", pygame.DIRECTION_RTL),
"Noto Sans Devanagari": ("Deva", pygame.DIRECTION_LTR),
"Noto Sans Old Persian": ("Xpeo", pygame.DIRECTION_LTR),
}
# Function to load a specific Noto font
def load_noto_font(font_name, size):
"""
Tries to load a font from the system by name.
"""
font_path = pygame.font.match_font(font_name)
if not font_path:
raise FileNotFoundError(font_name)
font = pygame.font.Font(font_path, size)
font_script_and_direction = noto_font_to_script.get(font_name)
if font_script_and_direction:
font.set_script(font_script_and_direction[0])
font.set_direction(font_script_and_direction[1])
return font
# Load fonts
loaded_fonts = {name: load_noto_font(name, FONT_SIZE) for name in noto_font_samples}
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Main loop
running = True
while running:
screen.fill(WHITE)
y_offset = 20 # Vertical spacing
for font_name, chars in noto_font_samples.items():
font = loaded_fonts.get(font_name)
# Display font name
title_surface = pygame.font.Font(None, 30).render(f"{font_name}:", True, BLACK)
screen.blit(title_surface, (20, y_offset))
x_offset = 250 # Horizontal spacing
for char in chars:
# Render character
text_surface = font.render(char, True, BLACK)
screen.blit(text_surface, (x_offset, y_offset))
# Get font metrics
metrics = font.metrics(char)
if metrics: # Ensure valid metric data
tot_advance = 0
for metric in metrics:
if not metric:
continue
min_x, max_x, min_y, max_y, x_advance = metric
pygame.draw.rect(
screen,
RED,
(
x_offset + tot_advance,
y_offset + min_y,
max_x - min_x,
max_y - min_y,
),
1,
)
tot_advance += x_advance
x_offset += 150 # Move right for the next character
y_offset += 70 # Move down for the next font
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
pygame.quit() Some issues I see with this results (technically none of these are faults of this PR)
|
a75cc9b
to
02d9c2b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Inspired by a conversation in #3326