diff --git a/components/express_way.py b/components/express_way.py index 66d3503..278a242 100644 --- a/components/express_way.py +++ b/components/express_way.py @@ -21,9 +21,11 @@ class ExpressWayBaseComponent: self.r = 8 * SCALE self.head_height = 12 * SCALE self.head_text_size = 10 * SCALE - self.head_text = str(' '.join(self.head_name)) if len(self.sub_name) or len(self.head_name) < 4 or len(self.main_name) > 4 else self.head_name - self.head_text_width = FontSize.get_font_width(self.head_text, FONT_PATH_A, self.head_text_size) - self.rect_width = max(self.main_text_width + self.sub_text_width, self.head_text_width * 1.2) + 2 * self.inset + self.head_text = self.head_name + self.rect_width = max(self.main_text_width + self.sub_text_width + 2 * self.inset, self.height()) + self.head_text_space = max((self.rect_width - 2 * self.inset - len(self.head_text) * self.head_text_size) / (len(self.head_text) + 1), 0) + head_ele = SpaceTextAComponent(self.head_text, self.head_text_color, self.head_text_size, self.head_text_space) + self.rect_width = max(self.rect_width, head_ele.width() + 2 * self.inset) def width(self): return self.rect_width @@ -53,8 +55,8 @@ class ExpressWayBaseComponent: fill=self.head_bg_color ) ) - head = TextAComponent(self.head_text, self.head_text_color, self.head_text_size) - head.draw(container, x + (self.rect_width - self.head_text_width) / 2, y + self.stroke_width + (self.head_height - self.head_text_size) / 2) + head = SpaceTextAComponent(self.head_text, self.head_text_color, self.head_text_size, self.head_text_space) + head.draw(container, x + (self.rect_width - head.width()) / 2, y + self.stroke_width + (self.head_height - self.head_text_size) / 2) big_text = TextBComponent(self.main_name, COLOR_WHITE, self.main_text_height) big_text.draw(container, name_start_x, y + self.head_height) diff --git a/components/text.py b/components/text.py index 38a43cd..eabc0ce 100644 --- a/components/text.py +++ b/components/text.py @@ -71,4 +71,37 @@ class BilingualTextComponent: 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) \ No newline at end of file + 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) \ No newline at end of file