From c0da61af66fac9a796ab01f4e2bb9f0c52049aca Mon Sep 17 00:00:00 2001 From: Zhang Muyu Date: Fri, 15 Sep 2023 00:28:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=84=E4=BB=B6=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_rect.py | 76 +++++++---- test/utils/components/common_road.py | 27 ++-- test/utils/components/express_way.py | 73 +++++++++++ test/utils/components/rect.py | 26 ++-- test/utils/components/special_area.py | 41 ++++++ test/utils/components/text.py | 18 +-- test/utils/factory.py | 182 -------------------------- 7 files changed, 198 insertions(+), 245 deletions(-) create mode 100644 test/utils/components/express_way.py create mode 100644 test/utils/components/special_area.py delete mode 100644 test/utils/factory.py diff --git a/test/test_rect.py b/test/test_rect.py index b56da29..109ae93 100644 --- a/test/test_rect.py +++ b/test/test_rect.py @@ -3,11 +3,14 @@ import cairosvg import os from lxml import etree + from utils.font_size import * -from utils.factory import * + from utils.components.text import * from utils.components.rect import * from utils.components.common_road import * +from utils.components.special_area import * +from utils.components.express_way import * # config width = 500 @@ -16,38 +19,59 @@ height = 500 # rect dwg = svgwrite.Drawing('rect.svg', profile='tiny', width=width, height=height) -rect = DoubleStrokeRectComponent(container=dwg, width=width, height=height, main_color='#1f963b', stroke_color='#ffffff', stroke_width=4, r=16) -rect.draw(0, 0) +rect = DoubleStrokeRectComponent(width=width, height=height, main_color='#1f963b', stroke_color='#ffffff', stroke_width=4, r=16) +rect.draw(dwg, 0, 0) -g_road = CommonRoadNationalComponent(dwg, 'G503') -g_road.draw(50, 20) +g_road = CommonRoadNationalComponent('G503') +g_road.draw(dwg, 50, 20) -p_road = CommonRoadProvincialComponent(dwg, 'S210') -p_road.draw(50, 80) +p_road = CommonRoadProvincialComponent('S210') +p_road.draw(dwg, 50, 80) -c_road = CommonRoadTownshipComponent(dwg, 'X021') -c_road.draw(50, 140) +c_road = CommonRoadTownshipComponent('X021') +c_road.draw(dwg, 50, 140) -t_road = CommonRoadTownshipComponent(dwg, 'Y101') -t_road.draw(50, 200) +t_road = CommonRoadTownshipComponent('Y101') +t_road.draw(dwg, 50, 200) -SpecialAreaFactory.street(dwg, 200, 20, '北四环西路') -SpecialAreaFactory.tourist(dwg, 200, 80, '湖滨旅游区') -ExpressWayFactory.national(dwg, 200, 140, 'G25', '01') -ExpressWayFactory.national(dwg, 300, 140, 'G1', '') -ExpressWayFactory.national(dwg, 375, 140, 'G10', '15') -ExpressWayFactory.national(dwg, 200, 200, 'G55', '') -ExpressWayFactory.national(dwg, 300, 200, 'G11', '') -ExpressWayFactory.national(dwg, 375, 200, 'G99', '02') -ExpressWayFactory.provincial(dwg, 50, 260, '京', 'S50', '') -ExpressWayFactory.provincial(dwg, 150, 260, '京津冀', 'S3801', '') -ExpressWayFactory.provincial(dwg, 300, 260, '粤', 'S99', '55') +street = StreetComponent('北四环西路') +street.draw(dwg, 200, 20) -text_a = TextAComponent(dwg, '北京', '#ffffff', 50) -text_a.draw(50, 320) +tourist = TouristComponent('湖滨旅游区') +tourist.draw(dwg, 200, 80) -text_b = TextBComponent(dwg, 'X201363', '#ffffff', 50) -text_b.draw(50, 380) +g2501 = NationalExpressWayComponent('G25', '01') +g2501.draw(dwg, 200, 140) + +g1 = NationalExpressWayComponent('G1', '') +g1.draw(dwg, 300, 140) + +g10 = NationalExpressWayComponent('G10', '') +g10.draw(dwg, 375, 140) + +g55 = NationalExpressWayComponent('G55', '') +g55.draw(dwg, 200, 200) + +g11 = NationalExpressWayComponent('G11', '') +g11.draw(dwg, 300, 200) + +g9902 = NationalExpressWayComponent('G99', '02') +g9902.draw(dwg, 375, 200) + +s50 = ProvincialExpressWayComponent('京', 'S50', '') +s50.draw(dwg, 50, 260) + +s3801 = ProvincialExpressWayComponent('京津冀', 'S3801', '') +s3801.draw(dwg, 150, 260) + +s9955 = ProvincialExpressWayComponent('粤', 'S99', '55') +s9955.draw(dwg, 300, 260) + +text_a = TextAComponent('北京', '#ffffff', 50) +text_a.draw(dwg, 50, 320) + +text_b = TextBComponent('X201363', '#ffffff', 50) +text_b.draw(dwg, 50, 380) # export dwg.save() diff --git a/test/utils/components/common_road.py b/test/utils/components/common_road.py index 67a75f2..2ac3da2 100644 --- a/test/utils/components/common_road.py +++ b/test/utils/components/common_road.py @@ -2,10 +2,10 @@ from utils.font_size import FontSize from utils.components.text import * from utils.components.rect import * + class CommonRoadBaseComponent: - def __init__(self, container, name, main_color, stroke_color) -> None: - self.container = container + def __init__(self, name, main_color, stroke_color) -> None: self.name = name self.main_color = main_color self.stroke_color = stroke_color @@ -21,27 +21,26 @@ class CommonRoadBaseComponent: def height(self): return self.p_height - def draw(self, x, y): - print(self.name, self.width(), self.height()) - rect = SingleStrokeRectComponent(self.container, self.width(), self.p_height, self.main_color, self.stroke_color, self.stroke_width, self.r) - rect.draw(x, y) - text = TextBComponent(self.container, self.name, self.stroke_color, self.p_height) - text.draw(x + self.inset, y) + def draw(self, container, x, y): + rect = SingleStrokeRectComponent(self.width(), self.p_height, self.main_color, self.stroke_color, self.stroke_width, self.r) + rect.draw(container, x, y) + text = TextBComponent(self.name, self.stroke_color, self.p_height) + text.draw(container, x + self.inset, y) class CommonRoadNationalComponent(CommonRoadBaseComponent): - def __init__(self, container, name) -> None: - super().__init__(container, name, '#e71420', '#ffffff') + def __init__(self, name) -> None: + super().__init__(name, '#e71420', '#ffffff') class CommonRoadProvincialComponent(CommonRoadBaseComponent): - def __init__(self, container, name) -> None: - super().__init__(container, name, '#f4ec17', '#000000') + def __init__(self, name) -> None: + super().__init__(name, '#f4ec17', '#000000') class CommonRoadTownshipComponent(CommonRoadBaseComponent): - def __init__(self, container, name) -> None: - super().__init__(container, name, '#ffffff', '#000000') \ No newline at end of file + def __init__(self, name) -> None: + super().__init__(name, '#ffffff', '#000000') \ No newline at end of file diff --git a/test/utils/components/express_way.py b/test/utils/components/express_way.py new file mode 100644 index 0000000..dbe5b47 --- /dev/null +++ b/test/utils/components/express_way.py @@ -0,0 +1,73 @@ +from utils.font_size import FontSize +from utils.components.text import * +from utils.components.rect import * + + +class ExpressWayBaseComponent: + + def __init__(self, head_name, main_name, sub_name, head_bg_color, head_text_color) -> None: + self.head_name = head_name + self.main_name = main_name + self.sub_name = sub_name + self.head_bg_color = head_bg_color + self.head_text_color = head_text_color + self.main_text_height = 40 + self.main_text_width = FontSize.get_font_width(self.main_name, 'resources/B.ttf', self.main_text_height) + self.sub_text_width = FontSize.get_font_width(self.sub_name, 'resources/B.ttf', 24) + self.inset = 5 + self.stroke_width = 2 + self.r = 8 + self.head_height = 12 + 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, 'resources/A.ttf', 10) + self.rect_width = max(self.main_text_width + self.sub_text_width, self.head_text_width * 1.2) + 2 * self.inset + + def width(self): + return self.rect_width + + def height(self): + return self.main_text_height + self.head_height + + def draw(self, container, x, y): + name_start_x = x + (self.rect_width - (self.main_text_width + self.sub_text_width)) / 2 + main_rect = SingleStrokeRectComponent(self.rect_width, self.main_text_height + self.head_height, '#1f963b', '#ffffff', self.stroke_width, self.r) + main_rect.draw(container, x, y) + container.add( + container.rect( + insert=(x + self.stroke_width, y + self.stroke_width), + size=(self.rect_width - 2 * self.stroke_width, self.head_height), + rx=self.r - self.stroke_width, + ry=self.r - self.stroke_width, + fill=self.head_bg_color + ) + ) + container.add( + container.rect( + insert=(x + self.stroke_width, y + self.head_height - self.r + 2 * self.stroke_width), + size=(self.rect_width - 2 * self.stroke_width, self.r - self.stroke_width), + rx=0, + ry=0, + fill=self.head_bg_color + ) + ) + head = TextAComponent(self.head_text, self.head_text_color, 10) + head.draw(container, x + (self.rect_width - self.head_text_width) / 2, y + self.stroke_width + (self.head_height - 10) / 2) + + big_text = TextBComponent(self.main_name, '#ffffff', self.main_text_height) + big_text.draw(container, name_start_x, y + self.head_height) + + if len(self.sub_name): + small_text = TextBComponent(self.sub_name, '#ffffff', 24) + small_text.draw(container, name_start_x + self.main_text_width, y + self.head_height + self.main_text_height * 0.33) + + +class NationalExpressWayComponent(ExpressWayBaseComponent): + + def __init__(self, main_name, sub_name) -> None: + super().__init__('国家高速', main_name, sub_name, '#e71727', '#ffffff') + + +class ProvincialExpressWayComponent(ExpressWayBaseComponent): + + def __init__(self, prov, main_name, sub_name) -> None: + super().__init__('{}高速'.format(prov), main_name, sub_name, '#f4ec17', '#000000') \ No newline at end of file diff --git a/test/utils/components/rect.py b/test/utils/components/rect.py index afd8c89..8f72529 100644 --- a/test/utils/components/rect.py +++ b/test/utils/components/rect.py @@ -1,7 +1,6 @@ class SingleStrokeRectComponent: - def __init__(self, container, width, height, main_color, stroke_color, stroke_width, r) -> None: - self.container = container + def __init__(self, width, height, main_color, stroke_color, stroke_width, r) -> None: self.p_width = width self.p_height = height self.main_color = main_color @@ -15,9 +14,9 @@ class SingleStrokeRectComponent: def height(self): return self.p_height - def draw(self, x, y): - self.container.add( - self.container.rect( + def draw(self, container, x, y): + container.add( + container.rect( insert=(x, y), size=(self.p_width, self.p_height), rx=self.r, @@ -25,8 +24,8 @@ class SingleStrokeRectComponent: fill=self.stroke_color ) ) - self.container.add( - self.container.rect( + container.add( + container.rect( insert=(x + self.stroke_width, y + self.stroke_width), size=(self.p_width - 2 * self.stroke_width, self.p_height - 2 * self.stroke_width), rx=self.r - self.stroke_width, @@ -38,8 +37,7 @@ class SingleStrokeRectComponent: class DoubleStrokeRectComponent: - def __init__(self, container, width, height, main_color, stroke_color, stroke_width, r) -> None: - self.container = container + def __init__(self, width, height, main_color, stroke_color, stroke_width, r) -> None: self.p_width = width self.p_height = height self.main_color = main_color @@ -53,11 +51,11 @@ class DoubleStrokeRectComponent: def height(self): return self.p_height - def draw(self, x, y): - outer = SingleStrokeRectComponent(self.container, self.p_width, self.p_height, self.stroke_color, self.main_color, self.stroke_width, self.r) - outer.draw(x, y) - self.container.add( - self.container.rect( + def draw(self, container, x, y): + outer = SingleStrokeRectComponent(self.p_width, self.p_height, self.stroke_color, self.main_color, self.stroke_width, self.r) + outer.draw(container, x, y) + container.add( + container.rect( insert=(x + 2 * self.stroke_width, y + 2 * self.stroke_width), size=(self.p_width - 4 * self.stroke_width, self.p_height - 4 * self.stroke_width), rx=self.r - 2 * self.stroke_width, diff --git a/test/utils/components/special_area.py b/test/utils/components/special_area.py new file mode 100644 index 0000000..1d86a67 --- /dev/null +++ b/test/utils/components/special_area.py @@ -0,0 +1,41 @@ +from utils.font_size import FontSize +from utils.components.text import * +from utils.components.rect import * + + +class SpecialAreaBaseComponent: + + def __init__(self, name, main_color, stroke_color) -> None: + self.name = name + self.main_color = main_color + self.stroke_color = stroke_color + self.p_height = 50 + self.inset = 5 + self.text_height = self.p_height - 2 * self.inset + self.stroke_width = 2 + self.r = 8 + + def width(self): + text_width = FontSize.get_font_width(self.name, 'resources/A.ttf', self.text_height) + return text_width + 2 * self.inset + + def height(self): + return self.p_height + + def draw(self, container, x, y): + rect = SingleStrokeRectComponent(self.width(), self.height(), self.main_color, self.stroke_color, self.stroke_width, self.r) + rect.draw(container, x, y) + text = TextAComponent(self.name, self.stroke_color, self.text_height) + text.draw(container, x + self.inset, y + self.inset) + + +class StreetComponent(SpecialAreaBaseComponent): + + def __init__(self, name) -> None: + super().__init__(name, '#1f4da0', '#ffffff') + + +class TouristComponent(SpecialAreaBaseComponent): + + def __init__(self, name) -> None: + super().__init__(name, '#a36927', '#ffffff') \ No newline at end of file diff --git a/test/utils/components/text.py b/test/utils/components/text.py index 1bb638a..19caacf 100644 --- a/test/utils/components/text.py +++ b/test/utils/components/text.py @@ -1,9 +1,9 @@ from utils.font_size import FontSize + class TextBaseComponent: - def __init__(self, container, text, color, size, font_path, font_name) -> None: - self.container = container + def __init__(self, text, color, size, font_path, font_name) -> None: self.text = text self.font_path = font_path self.font_name = font_name @@ -16,11 +16,11 @@ class TextBaseComponent: def height(self): return self.size - def draw(self, x, y): + def draw(self, container, x, y): x_loc = x y_loc = y + self.size * 0.8 - self.container.add( - self.container.text( + container.add( + container.text( self.text, insert=(x_loc, y_loc), fill=self.color, @@ -32,11 +32,11 @@ class TextBaseComponent: class TextAComponent(TextBaseComponent): - def __init__(self, container, text, color, size) -> None: - super().__init__(container, text, color, size, 'resources/A.ttf', 'A型交通标志专用字体') + def __init__(self, text, color, size) -> None: + super().__init__(text, color, size, 'resources/A.ttf', 'A型交通标志专用字体') class TextBComponent(TextBaseComponent): - def __init__(self, container, text, color, size) -> None: - super().__init__(container, text, color, size, 'resources/B.ttf', 'B型交通标志专用字体') + def __init__(self, text, color, size) -> None: + super().__init__(text, color, size, 'resources/B.ttf', 'B型交通标志专用字体') diff --git a/test/utils/factory.py b/test/utils/factory.py deleted file mode 100644 index 97ffacb..0000000 --- a/test/utils/factory.py +++ /dev/null @@ -1,182 +0,0 @@ -from utils.font_size import FontSize - -class TextFactory: - - def type_a_text(container, text, color, size, x, y): - return TextFactory.text(container, text, 'A型交通标志专用字体', color, size, x, y) - - def type_b_text(container, text, color, size, x, y): - return TextFactory.text(container, text, 'B型交通标志专用字体', color, size, x, y) - - def text(container, text, font, color, size, x, y): - x_loc = x - y_loc = y + size * 0.8 - return container.text( - text, - insert=(x_loc, y_loc), - fill=color, - font_family=font, - font_size=size - ) - - -class RectFactory: - - def single_stroke_rect(container, width, height, main_color, stroke_color, stroke_width, r, x, y): - container.add( - container.rect( - insert=(x, y), - size=(width, height), - rx=r, - ry=r, - fill=stroke_color - ) - ) - container.add( - container.rect( - insert=(x + stroke_width, y + stroke_width), - size=(width - 2 * stroke_width, height - 2 * stroke_width), - rx=r - stroke_width, - ry=r - stroke_width, - fill=main_color - ) - ) - - def double_stroke_rect(container, width, height, main_color, stroke_color, stroke_width, r, x, y): - RectFactory.single_stroke_rect( - container, - width, - height, - stroke_color, - main_color, - stroke_width, - r, - x, - y - ) - container.add( - container.rect( - insert=(x + 2 * stroke_width, y + 2 * stroke_width), - size=(width - 4 * stroke_width, height - 4 * stroke_width), - rx=r - 2 * stroke_width, - ry=r - 2 * stroke_width, - fill=main_color - ) - ) - - -class CommonRoadFactory: - - def base(container, x, y, name, main_color, stroke_color): - height = 50 - inset = 10 - stroke_width = 2 - r = 8 - text_width = FontSize.get_font_width(name, 'resources/B.ttf', height) - rect_width = text_width + inset * 2 - RectFactory.single_stroke_rect(container, rect_width, height, main_color, stroke_color, stroke_width, r, x, y) - container.add(TextFactory.type_b_text(container, name, stroke_color, height, x + inset, y)) - - def national(container, x, y, name): - CommonRoadFactory.base( - container, - x, - y, - name, - '#e71420', - '#ffffff' - ) - - def provincial(container, x, y, name): - CommonRoadFactory.base( - container, - x, - y, - name, - '#f4ec17', - '#000000' - ) - - def county(container, x, y, name): - CommonRoadFactory.base( - container, - x, - y, - name, - '#ffffff', - '#000000' - ) - - def township(container, x, y, name): - CommonRoadFactory.base( - container, - x, - y, - name, - '#ffffff', - '#000000' - ) - -class SpecialAreaFactory: - - def base(container, x, y, name, main_color, stroke_color): - height = 50 - inset = 5 - text_height = height - 2 * inset - stroke_width = 2 - r = 8 - text_width = FontSize.get_font_width(name, 'resources/A.ttf', text_height) - rect_width = text_width + 2 * inset - RectFactory.single_stroke_rect(container, rect_width, height, main_color, stroke_color, stroke_width, r, x, y) - container.add(TextFactory.type_a_text(container, name, stroke_color, text_height, x + inset, y + inset)) - - def street(container, x, y, name): - SpecialAreaFactory.base(container, x, y, name, '#1f4da0', '#ffffff') - - def tourist(container, x, y, name): - SpecialAreaFactory.base(container, x, y, name, '#a36927', '#ffffff') - - -class ExpressWayFactory: - - def base(container, x, y, head_name, main_name, sub_name, head_bg_color, head_text_color): - main_text_height = 40 - main_text_width = FontSize.get_font_width(main_name, 'resources/B.ttf', main_text_height) - sub_text_width = FontSize.get_font_width(sub_name, 'resources/B.ttf', 24) - inset = 5 - stroke_width = 2 - r = 8 - head_height = 12 - head_text = str(' '.join(head_name)) if len(sub_name) or len(head_name) < 4 or len(main_name) > 4 else head_name - head_text_width = FontSize.get_font_width(head_text, 'resources/A.ttf', 10) - rect_width = max(main_text_width + sub_text_width, head_text_width * 1.2) + 2 * inset - RectFactory.single_stroke_rect(container, rect_width, main_text_height + head_height, '#1f963b', '#ffffff', stroke_width, r, x, y) - container.add( - container.rect( - insert=(x + stroke_width, y + stroke_width), - size=(rect_width - 2 * stroke_width, head_height), - rx=r - stroke_width, - ry=r - stroke_width, - fill=head_bg_color - ) - ) - container.add( - container.rect( - insert=(x + stroke_width, y + head_height - r + 2 * stroke_width), - size=(rect_width - 2 * stroke_width, r - stroke_width), - rx=0, - ry=0, - fill=head_bg_color - ) - ) - name_start_x = x + (rect_width - (main_text_width + sub_text_width)) / 2 - container.add(TextFactory.type_a_text(container, head_text, head_text_color, 10, x + (rect_width - head_text_width) / 2, y + stroke_width + (head_height - 10) / 2)) - container.add(TextFactory.type_b_text(container, main_name, '#ffffff', main_text_height, name_start_x, y + head_height)) - if len(sub_name): - container.add(TextFactory.type_b_text(container, sub_name, '#ffffff', 24, name_start_x + main_text_width, y + head_height + main_text_height * 0.33)) - - def national(container, x, y, main_name, sub_name): - ExpressWayFactory.base(container, x, y, '国家高速', main_name, sub_name, '#e71727', '#ffffff') - - def provincial(container, x, y, prov, main_name, sub_name): - ExpressWayFactory.base(container, x, y, '{}高速'.format(prov), main_name, sub_name, '#f4ec17', '#000000') \ No newline at end of file