diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-20 16:59:13 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-20 16:59:13 +0200 |
commit | 398eebe7829622c6983cb28528f2208b4d596f32 (patch) | |
tree | 411f5d2fa3b7fc8b5352bc3cda49621fbd858919 /src/sliceitoff/text | |
parent | 191110be33196866998da385fbc3107344d2bf73 (diff) |
text to srites. cache scaled font sprites...
Diffstat (limited to 'src/sliceitoff/text')
-rw-r--r-- | src/sliceitoff/text/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 52 |
2 files changed, 53 insertions, 0 deletions
diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py new file mode 100644 index 0000000..eb17c6e --- /dev/null +++ b/src/sliceitoff/text/__init__.py @@ -0,0 +1 @@ +from .text import TextPage diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py new file mode 100644 index 0000000..f01e517 --- /dev/null +++ b/src/sliceitoff/text/text.py @@ -0,0 +1,52 @@ +import pygame +from random import randrange + +from images import Fonts +from display import Scaling + +scaled_fonts = {} + +class LetterSprite(pygame.sprite.Sprite): + def __init__(self, image, pos): + super().__init__() + self.image = image + self.rect = self.image.get_rect().move(pos) + self.direction = ( + Scaling.factor * 40 * 1_000 + randrange(4_000), + Scaling.factor * 40 * 1_000 + randrange(4_000)) + +class TextPage(pygame.sprite.Group): + def __init__( + self, + text, + pos = (0,0), + size = (8_000,16_000), + grid = None, + font = 'lcd'): + super().__init__() + if grid == None: + grid = size + col, row = 0, 0 + x, y = pos + w, h = grid + for ch_txt in text: + if ch_txt == '\n': + row += 1 + col = 0 + continue + if ch_txt == '\t': + col = (col + 4) % 4 + continue + font_key = (font, w) + ch = ord(ch_txt) + if font_key not in scaled_fonts: + scaled_fonts[font_key]=[None for _ in range(256)] + if scaled_fonts[font_key][ch] == None: + scaled_fonts[font_key][ch] = pygame.transform.scale_by( + Fonts.fonts[font].get(ch), + size[0]/8 * Scaling.factor) + image = scaled_fonts[font_key][ch] + image_pos = Scaling.scale_to_display( (x+col*w, y+row*h) ) + self.add(LetterSprite(image, image_pos)) + col += 1 +
\ No newline at end of file |