bro
This commit is contained in:
1 parent
de41a02749
commit
9af09cebd3
764 files changed
+54218
-78859
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
"""pygame.examples.font_viewer
|
||||
""" pygame.examples.font_viewer
|
||||
Scroll through your system fonts from a list of surfaces or one huge buffer.
|
||||
|
||||
This example exhibits:
|
||||
@@ -22,11 +22,10 @@ Keyboard Controls:
|
||||
* Press up or down to scroll
|
||||
* Press escape to exit
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import os
|
||||
|
||||
import pygame
|
||||
import pygame as pg
|
||||
|
||||
use_big_surface = False # draw into large buffer and save png file
|
||||
|
||||
@@ -42,15 +41,16 @@ class FontViewer:
|
||||
MOUSE_SCROLL_SPEED = 50
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
pg.init()
|
||||
|
||||
# create a window that uses 80 percent of the screen
|
||||
info = pygame.display.Info()
|
||||
self.screen_size = (int(info.current_w * 0.8), int(info.current_h * 0.8))
|
||||
pygame.display.set_mode(self.screen_size)
|
||||
self.font_size = self.screen_size[1] // 16
|
||||
info = pg.display.Info()
|
||||
w = info.current_w
|
||||
h = info.current_h
|
||||
pg.display.set_mode((int(w * 0.8), int(h * 0.8)))
|
||||
self.font_size = h // 20
|
||||
|
||||
self.clock = pygame.Clock()
|
||||
self.clock = pg.time.Clock()
|
||||
self.y_offset = 0
|
||||
self.grabbed = False
|
||||
self.render_fonts("&N abcDEF789")
|
||||
@@ -70,10 +70,11 @@ class FontViewer:
|
||||
path = ""
|
||||
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
|
||||
path = os.path.join(sys.argv[1], "")
|
||||
fonts = []
|
||||
if os.path.exists(path):
|
||||
fonts = [font for font in os.listdir(path) if font.endswith(".ttf")]
|
||||
return fonts or pygame.font.get_fonts(), path
|
||||
else:
|
||||
fonts = pg.font.get_fonts()
|
||||
return fonts, path
|
||||
|
||||
def render_fonts(self, text="A display of font &N"):
|
||||
"""
|
||||
@@ -91,27 +92,23 @@ class FontViewer:
|
||||
total_height = 0
|
||||
max_width = 0
|
||||
|
||||
load_font = pygame.Font if path else pygame.font.SysFont
|
||||
load_font = pg.font.Font if path else pg.font.SysFont
|
||||
|
||||
# display instructions at the top of the display
|
||||
font = pygame.font.SysFont(pygame.font.get_default_font(), font_size)
|
||||
font.align = pygame.FONT_CENTER
|
||||
instructions = (
|
||||
"Use the scroll wheel or click and drag to scroll up "
|
||||
"and down. Fonts that don't use the Latin Alphabet "
|
||||
"might render incorrectly. Here are your "
|
||||
f"{len(fonts)} fonts"
|
||||
font = pg.font.SysFont(pg.font.get_default_font(), font_size)
|
||||
lines = (
|
||||
"Use the scroll wheel or click and drag",
|
||||
"to scroll up and down.",
|
||||
"Fonts that don't use the Latin Alphabet",
|
||||
"might render incorrectly.",
|
||||
f"Here are your {len(fonts)} fonts",
|
||||
"",
|
||||
)
|
||||
surf = font.render(
|
||||
instructions,
|
||||
True,
|
||||
instruction_color,
|
||||
self.back_color,
|
||||
self.screen_size[0] - 20,
|
||||
)
|
||||
font_surfaces.append((surf, total_height))
|
||||
total_height += surf.get_height()
|
||||
max_width = max(max_width, surf.get_width())
|
||||
for line in lines:
|
||||
surf = font.render(line, 1, instruction_color, self.back_color)
|
||||
font_surfaces.append((surf, total_height))
|
||||
total_height += surf.get_height()
|
||||
max_width = max(max_width, surf.get_width())
|
||||
|
||||
# render all the fonts and store them with the total height
|
||||
for name in sorted(fonts):
|
||||
@@ -121,10 +118,8 @@ class FontViewer:
|
||||
continue
|
||||
line = text.replace("&N", name)
|
||||
try:
|
||||
surf = font.render(
|
||||
line, True, color, self.back_color, self.screen_size[0] - 20
|
||||
)
|
||||
except pygame.error as e:
|
||||
surf = font.render(line, 1, color, self.back_color)
|
||||
except pg.error as e:
|
||||
print(e)
|
||||
break
|
||||
|
||||
@@ -136,16 +131,16 @@ class FontViewer:
|
||||
self.total_height = total_height
|
||||
self.max_width = max_width
|
||||
self.font_surfaces = font_surfaces
|
||||
self.max_y = total_height - pygame.display.get_surface().get_height()
|
||||
self.max_y = total_height - pg.display.get_surface().get_height()
|
||||
|
||||
def display_fonts(self):
|
||||
"""
|
||||
Display the visible fonts based on the y_offset value(updated in
|
||||
handle_events) and the height of the pygame window.
|
||||
"""
|
||||
pygame.display.set_caption("Font Viewer")
|
||||
display = pygame.display.get_surface()
|
||||
clock = pygame.Clock()
|
||||
pg.display.set_caption("Font Viewer")
|
||||
display = pg.display.get_surface()
|
||||
clock = pg.time.Clock()
|
||||
center = display.get_width() // 2
|
||||
|
||||
while True:
|
||||
@@ -157,12 +152,12 @@ class FontViewer:
|
||||
bottom >= self.y_offset
|
||||
and top <= self.y_offset + display.get_height()
|
||||
):
|
||||
x = center - surface.get_width() / 2
|
||||
x = center - surface.get_width() // 2
|
||||
display.blit(surface, (x, top - self.y_offset))
|
||||
# get input and update the screen
|
||||
if not self.handle_events():
|
||||
break
|
||||
pygame.display.flip()
|
||||
pg.display.flip()
|
||||
clock.tick(30)
|
||||
|
||||
def render_surface(self):
|
||||
@@ -175,7 +170,7 @@ class FontViewer:
|
||||
be useful for testing large surfaces.
|
||||
"""
|
||||
|
||||
large_surface = pygame.surface.Surface(
|
||||
large_surface = pg.surface.Surface(
|
||||
(self.max_width, self.total_height)
|
||||
).convert()
|
||||
large_surface.fill(self.back_color)
|
||||
@@ -197,9 +192,7 @@ class FontViewer:
|
||||
x = center - int(w / 2)
|
||||
large_surface.blit(surface, (x, y))
|
||||
y += surface.get_height()
|
||||
self.max_y = (
|
||||
large_surface.get_height() - pygame.display.get_surface().get_height()
|
||||
)
|
||||
self.max_y = large_surface.get_height() - pg.display.get_surface().get_height()
|
||||
self.surface = large_surface
|
||||
|
||||
def display_surface(self, time=10):
|
||||
@@ -208,12 +201,12 @@ class FontViewer:
|
||||
based on the y_offset value(set in handle_events) and the height of the
|
||||
pygame window.
|
||||
"""
|
||||
screen = pygame.display.get_surface()
|
||||
screen = pg.display.get_surface()
|
||||
|
||||
# Create a Rect equal to size of screen. Then we can just change its
|
||||
# top attribute to draw the desired part of the rendered font surface
|
||||
# to the display surface
|
||||
rect = pygame.rect.Rect(
|
||||
rect = pg.rect.Rect(
|
||||
0,
|
||||
0,
|
||||
self.surface.get_width(),
|
||||
@@ -228,11 +221,11 @@ class FontViewer:
|
||||
screen.fill(self.back_color)
|
||||
rect.top = self.y_offset
|
||||
screen.blit(self.surface, (x, 0), rect)
|
||||
pygame.display.flip()
|
||||
pg.display.flip()
|
||||
self.clock.tick(20)
|
||||
|
||||
def save_png(self, name="font_viewer.png"):
|
||||
pygame.image.save(self.surface, name)
|
||||
pg.image.save(self.surface, name)
|
||||
file_size = os.path.getsize(name) // 1024
|
||||
print(f"font surface saved to {name}\nsize: {file_size:,}Kb")
|
||||
|
||||
@@ -243,37 +236,37 @@ class FontViewer:
|
||||
changed based on mouse and keyboard input. display_fonts() and
|
||||
display_surface() use the y_offset to scroll display.
|
||||
"""
|
||||
events = pygame.event.get()
|
||||
events = pg.event.get()
|
||||
for e in events:
|
||||
if e.type == pygame.QUIT:
|
||||
if e.type == pg.QUIT:
|
||||
return False
|
||||
elif e.type == pygame.KEYDOWN:
|
||||
if e.key == pygame.K_ESCAPE:
|
||||
elif e.type == pg.KEYDOWN:
|
||||
if e.key == pg.K_ESCAPE:
|
||||
return False
|
||||
elif e.type == pygame.MOUSEWHEEL:
|
||||
elif e.type == pg.MOUSEWHEEL:
|
||||
self.y_offset += e.y * self.MOUSE_SCROLL_SPEED * -1
|
||||
elif e.type == pygame.MOUSEBUTTONDOWN:
|
||||
elif e.type == pg.MOUSEBUTTONDOWN:
|
||||
# enter dragging mode on mouse down
|
||||
self.grabbed = True
|
||||
pygame.event.set_grab(True)
|
||||
elif e.type == pygame.MOUSEBUTTONUP:
|
||||
pg.event.set_grab(True)
|
||||
elif e.type == pg.MOUSEBUTTONUP:
|
||||
# exit drag mode on mouse up
|
||||
self.grabbed = False
|
||||
pygame.event.set_grab(False)
|
||||
pg.event.set_grab(False)
|
||||
|
||||
# allow simple accelerated scrolling with the keyboard
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_UP]:
|
||||
keys = pg.key.get_pressed()
|
||||
if keys[pg.K_UP]:
|
||||
self.key_held += 1
|
||||
self.y_offset -= int(self.KEY_SCROLL_SPEED * (self.key_held // 10))
|
||||
elif keys[pygame.K_DOWN]:
|
||||
elif keys[pg.K_DOWN]:
|
||||
self.key_held += 1
|
||||
self.y_offset += int(self.KEY_SCROLL_SPEED * (self.key_held // 10))
|
||||
else:
|
||||
self.key_held = 20
|
||||
|
||||
# set the y_offset for scrolling and keep it between 0 and max_y
|
||||
y = pygame.mouse.get_rel()[1]
|
||||
y = pg.mouse.get_rel()[1]
|
||||
if y and self.grabbed:
|
||||
self.y_offset -= y
|
||||
|
||||
@@ -281,6 +274,5 @@ class FontViewer:
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
viewer = FontViewer()
|
||||
pygame.quit()
|
||||
viewer = FontViewer()
|
||||
pg.quit()
|
||||
Reference in new issue
Block a user