summaryrefslogtreecommitdiff
path: root/src/sliceitoff/text/text.py
blob: 8c13004f4c1804d7b3ec02bf0b31559f9f996c41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pygame
from random import randrange

from images import Fonts
from display import Scaling, EGA_COLORS

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 * (1_000 - randrange(2_000)),
                Scaling.factor * (1_000 - randrange(2_000)))
                
    def update(self, explode = 0, **kwargs):
        if explode:
            self.rect = pygame.Rect(
                    self.rect.x + self.direction[0] * explode,
                    self.rect.y + self.direction[1] * explode,
                    self.rect.w,
                    self.rect.h)
            self.direction = (
                    self.direction[0] * 0.95,
                    self.direction[1] * 0.95 + 0.3)
            

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
        color = 0xf
        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
            ch = ord(ch_txt)
            if ch in range(0xe0,0xf0):
                color = ch - 0xe0
                continue
            font_key = (font, w, color)
            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)
                scaled_fonts[font_key][ch].fill(
                        EGA_COLORS[color],
                        special_flags = pygame.BLEND_RGBA_MULT)
            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