diff options
author | Viljami Ilola <+@hix.fi> | 2024-04-21 00:03:22 +0300 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-04-21 00:03:22 +0300 |
commit | 9f808fa0e43ae903d6159848d1331dcccb91434d (patch) | |
tree | 4e45858383aff0399184395875966765bb9964cb /src/sliceitoff/text | |
parent | 360a680cdb03df60673581a8f187c4074a78dd21 (diff) |
fonts as object
Diffstat (limited to 'src/sliceitoff/text')
-rw-r--r-- | src/sliceitoff/text/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/text/fonts.py | 16 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 8 |
3 files changed, 17 insertions, 9 deletions
diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py index aed5723..3ec8752 100644 --- a/src/sliceitoff/text/__init__.py +++ b/src/sliceitoff/text/__init__.py @@ -1,4 +1,4 @@ """ text - fonts, letters, texts and related """ from .text import TextPage, LetterSprite, get_letter_surface -from .fonts import Font, Fonts +from .fonts import Font, fonts from .explode import ExplodingSprite diff --git a/src/sliceitoff/text/fonts.py b/src/sliceitoff/text/fonts.py index 9f267b3..8776530 100644 --- a/src/sliceitoff/text/fonts.py +++ b/src/sliceitoff/text/fonts.py @@ -6,17 +6,17 @@ DEBUG = os.getenv("DEBUG") class Fonts: """ Fonts - static class to store loaded fonts """ - fonts = {} + def __init__(self): + self.fonts = {} - @staticmethod - def load_fonts(base_path): + def init(self, base_path): """ loads fonts from list """ filename_fontlist = os.path.join(base_path, "fonts.lst") with open(filename_fontlist, "r", encoding="utf-8") as fontlist_file: for line in fontlist_file: name, *path = line.strip().split() filename = os.path.join(base_path, *path) - __class__.fonts[name] = Font(filename) + self.fonts[name] = Font(filename) return True @@ -42,3 +42,11 @@ class Font: def get(self, ch): """ Just get surface of the font size 8x16 max """ return self.surfaces[ch%256] + +# Initialize only one time +try: + # pylint: disable = used-before-assignment + # This is intented behaviour + fonts +except NameError: + fonts = Fonts() diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index 3dafa64..cd5b88a 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -2,7 +2,7 @@ import pygame from sliceitoff.display import Scaling, CGA_COLORS -from .fonts import Fonts +from .fonts import fonts from .explode import ExplodingSprite scaled_fonts = {} @@ -16,13 +16,13 @@ def get_letter_surface(font_key, ch): color: 0-15 as in CGA palette """ font, w, color = font_key - if font not in Fonts.fonts: + if font not in fonts.fonts: return None if font_key not in scaled_fonts: scaled_fonts[font_key]=[None for _ in range(256)] if scaled_fonts[font_key][ch] is None: scaled_fonts[font_key][ch] = pygame.transform.scale_by( - Fonts.fonts[font].get(ch), + fonts.fonts[font].get(ch), w/8 * Scaling.factor) scaled_fonts[font_key][ch].fill( CGA_COLORS[color], @@ -47,7 +47,7 @@ class TextPage(pygame.sprite.Group): pos Position of right top corner in internal cooordinates size Single character size (w,h) grid Space for a character (w,h) - font Font loaded in Fonts.fonts dict + font Font loaded in fonts.fonts dict """ # pylint: disable = too-many-arguments # all argumets necessaary |