Files
2023-09-30 17:24:57 +08:00

107 lines
3.2 KiB
Python

from utils.font_size import FontSize
from settings import *
class TextBaseComponent:
def __init__(self, text, color, size, font_path, font_name) -> None:
self.text = text
self.font_path = font_path
self.font_name = font_name
self.color = color
self.size = size
def width(self):
return FontSize.get_font_width(self.text, self.font_path, self.size)
def height(self):
return self.size
def draw(self, container, x, y):
x_loc = x
y_loc = y + self.size * 0.8
container.add(
container.text(
self.text,
insert=(x_loc, y_loc),
fill=self.color,
font_family=self.font_name,
font_size=self.size
)
)
class TextAComponent(TextBaseComponent):
def __init__(self, text, color, size) -> None:
super().__init__(text, color, size, FONT_PATH_A, FONT_NAME_A)
class TextBComponent(TextBaseComponent):
def __init__(self, text, color, size) -> None:
super().__init__(text, color, size, FONT_PATH_B, FONT_NAME_B)
class TextCComponent(TextBaseComponent):
def __init__(self, text, color, size) -> None:
super().__init__(text, color, size, FONT_PATH_C, FONT_NAME_C)
class BilingualTextComponent:
def __init__(self, text, en, color, size) -> None:
self.text = text
self.en = en
self.color = color
self.size = size
self.en_size = int(size * 0.7)
self.text_width = FontSize.get_font_width(self.text, FONT_PATH_A, self.size)
self.en_width = FontSize.get_font_width(self.en, FONT_PATH_B, self.en_size)
self.line_space = self.size * 0.2
def width(self):
return max(self.text_width, self.en_width)
def height(self):
return self.size + self.en_size + self.line_space
def draw(self, container, x, y):
text = TextAComponent(self.text, self.color, self.size)
text.draw(container, x + (self.width() - self.text_width) / 2, y)
en = TextBComponent(self.en, self.color, self.en_size)
en.draw(container, x + (self.width() - self.en_width) / 2, y + self.line_space + self.size)
class SpaceTextAComponent:
def __init__(self, text, color, size, space_size) -> None:
self.text = text
self.color = color
self.size = size
self.space_size = space_size
def width(self):
text_width = sum([FontSize.get_font_width(word, FONT_PATH_A, self.size) for word in self.text])
space_width = self.space_size * (len(self.text) - 1)
return text_width + space_width
def height(self):
return self.size
def draw(self, container, x, y):
x_loc = x
y_loc = y + self.size * 0.8
for word in self.text:
container.add(
container.text(
word,
insert=(x_loc, y_loc),
fill=self.color,
font_family=FONT_NAME_A,
font_size=self.size
)
)
word_size = FontSize.get_font_width(word, FONT_PATH_A, self.size)
x_loc += (self.space_size + word_size)