feat: 组件封装

This commit is contained in:
Zhang Muyu
2023-09-14 00:17:15 +08:00
parent dde02919fe
commit e9b2459fee
4 changed files with 181 additions and 6 deletions

View File

@@ -5,19 +5,32 @@ 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 *
# config
width = 500
height = 350
height = 500
# rect
dwg = svgwrite.Drawing('rect.svg', profile='tiny', width=width, height=height)
RectFactory.double_stroke_rect(container=dwg, width=width, height=height, main_color='#1f963b', stroke_color='#ffffff', stroke_width=4, r=16, x=0, y=0)
CommonRoadFactory.national(dwg, 50, 20, 'G503')
CommonRoadFactory.provincial(dwg, 50, 80, 'S210')
CommonRoadFactory.county(dwg, 50, 140, 'X021')
CommonRoadFactory.township(dwg, 50, 200, 'Y101')
rect = DoubleStrokeRectComponent(container=dwg, width=width, height=height, main_color='#1f963b', stroke_color='#ffffff', stroke_width=4, r=16)
rect.draw(0, 0)
g_road = CommonRoadNationalComponent(dwg, 'G503')
g_road.draw(50, 20)
p_road = CommonRoadProvincialComponent(dwg, 'S210')
p_road.draw(50, 80)
c_road = CommonRoadTownshipComponent(dwg, 'X021')
c_road.draw(50, 140)
t_road = CommonRoadTownshipComponent(dwg, 'Y101')
t_road.draw(50, 200)
SpecialAreaFactory.street(dwg, 200, 20, '北四环西路')
SpecialAreaFactory.tourist(dwg, 200, 80, '湖滨旅游区')
ExpressWayFactory.national(dwg, 200, 140, 'G25', '01')
@@ -30,6 +43,12 @@ ExpressWayFactory.provincial(dwg, 50, 260, '京', 'S50', '')
ExpressWayFactory.provincial(dwg, 150, 260, '京津冀', 'S3801', '')
ExpressWayFactory.provincial(dwg, 300, 260, '', 'S99', '55')
text_a = TextAComponent(dwg, '北京', '#ffffff', 50)
text_a.draw(50, 320)
text_b = TextBComponent(dwg, 'X201363', '#ffffff', 50)
text_b.draw(50, 380)
# export
dwg.save()

View File

@@ -0,0 +1,47 @@
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
self.name = name
self.main_color = main_color
self.stroke_color = stroke_color
self.p_height = 50
self.inset = 10
self.stroke_width = 2
self.r = 8
def width(self):
text_width = FontSize.get_font_width(self.name, 'resources/B.ttf', self.p_height)
return text_width + self.inset * 2
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)
class CommonRoadNationalComponent(CommonRoadBaseComponent):
def __init__(self, container, name) -> None:
super().__init__(container, name, '#e71420', '#ffffff')
class CommonRoadProvincialComponent(CommonRoadBaseComponent):
def __init__(self, container, name) -> None:
super().__init__(container, name, '#f4ec17', '#000000')
class CommonRoadTownshipComponent(CommonRoadBaseComponent):
def __init__(self, container, name) -> None:
super().__init__(container, name, '#ffffff', '#000000')

View File

@@ -0,0 +1,67 @@
class SingleStrokeRectComponent:
def __init__(self, container, width, height, main_color, stroke_color, stroke_width, r) -> None:
self.container = container
self.p_width = width
self.p_height = height
self.main_color = main_color
self.stroke_color = stroke_color
self.stroke_width = stroke_width
self.r = r
def width(self):
return self.p_width
def height(self):
return self.p_height
def draw(self, x, y):
self.container.add(
self.container.rect(
insert=(x, y),
size=(self.p_width, self.p_height),
rx=self.r,
ry=self.r,
fill=self.stroke_color
)
)
self.container.add(
self.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,
ry=self.r - self.stroke_width,
fill=self.main_color
)
)
class DoubleStrokeRectComponent:
def __init__(self, container, width, height, main_color, stroke_color, stroke_width, r) -> None:
self.container = container
self.p_width = width
self.p_height = height
self.main_color = main_color
self.stroke_color = stroke_color
self.stroke_width = stroke_width
self.r = r
def width(self):
return self.p_width
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(
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,
ry=self.r - 2 * self.stroke_width,
fill=self.main_color
)
)

View File

@@ -0,0 +1,42 @@
from utils.font_size import FontSize
class TextBaseComponent:
def __init__(self, container, text, color, size, font_path, font_name) -> None:
self.container = container
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, x, y):
x_loc = x
y_loc = y + self.size * 0.8
self.container.add(
self.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, container, text, color, size) -> None:
super().__init__(container, 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型交通标志专用字体')