79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 | class SkiaEngine(DrawEngine):
"""基于 skia-python 的进程内栅格引擎。"""
name = "skia"
kind = "raster"
requires = ("skia", "numpy", "PIL")
description = "skia-python 进程内栅格化(无磁盘 I/O,抗锯齿质量高)"
def _new_canvas(self, width: int, height: int):
import skia
surface = skia.Surface(width, height)
canvas = surface.getCanvas()
canvas.clear(skia.ColorTRANSPARENT)
return surface, canvas
# ------------------------------------------------------------------
# 圆角矩形
# ------------------------------------------------------------------
def render_roundrect(self, spec: RoundRectSpec):
import skia
width = max(1, int(round(spec.width)))
height = max(1, int(round(spec.height)))
surface, canvas = self._new_canvas(width, height)
stroke_width = float(spec.outline_width or 0.0)
# SVG 的描边是居中的;把几何内缩半个线宽,描边外沿才正好贴合 0..width
inset = stroke_width / 2.0 if stroke_width > 0 else 0.0
rx = max(0.0, float(spec.rx) - inset)
ry = max(0.0, float(spec.effective_ry) - inset)
rect = skia.Rect.MakeXYWH(
inset, inset, max(0.0, width - 2 * inset), max(0.0, height - 2 * inset)
)
rrect = skia.RRect.MakeRectXY(rect, rx, ry)
fill = parse_color(spec.fill, spec.fill_opacity)
if fill:
canvas.drawRRect(
rrect, skia.Paint(AntiAlias=True, Color=skia.Color(*fill))
)
if stroke_width > 0:
paint = self._stroke_paint(spec, stroke_width, inset, height)
if paint is not None:
canvas.drawRRect(rrect, paint)
return _to_pil(surface.makeImageSnapshot())
def _stroke_paint(self, spec, stroke_width, inset, height):
import skia
outline = parse_color(spec.outline, spec.outline_opacity)
if not outline:
return None
outline2 = (
parse_color(spec.outline2, spec.outline2_opacity)
if spec.outline2
else None
)
paint = skia.Paint(
AntiAlias=True,
Style=skia.Paint.kStroke_Style,
StrokeWidth=stroke_width,
)
if outline2 and outline2 != outline:
paint.setShader(
skia.GradientShader.MakeLinear(
points=[(float(inset), float(inset)),
(float(inset), float(height - inset))],
colors=[skia.Color(*outline), skia.Color(*outline2)],
positions=[float(spec.gradient_stop1), float(spec.gradient_stop2)],
)
)
else:
paint.setColor(skia.Color(*outline))
return paint
# ------------------------------------------------------------------
# 滑块进度条槽
# ------------------------------------------------------------------
def render_track(self, spec: TrackSpec):
import skia
width = max(1, int(round(spec.width)))
height = max(1, int(round(spec.height)))
surface, canvas = self._new_canvas(width, height)
radius = max(0.0, float(spec.radius))
left_w = max(0.0, min(float(spec.width2), float(width)))
track = parse_color(spec.track_fill, spec.track_opacity)
if track and left_w > 0:
canvas.drawRRect(
skia.RRect.MakeRectXY(
skia.Rect.MakeXYWH(0, 0, left_w, height), radius, radius
),
skia.Paint(AntiAlias=True, Color=skia.Color(*track)),
)
rail = parse_color(spec.rail_fill, spec.rail_opacity)
rail_w = width - left_w
if rail and rail_w > 0:
canvas.drawRRect(
skia.RRect.MakeRectXY(
skia.Rect.MakeXYWH(left_w, 0, rail_w, height), radius, radius
),
skia.Paint(AntiAlias=True, Color=skia.Color(*rail)),
)
return _to_pil(surface.makeImageSnapshot())
# ------------------------------------------------------------------
# 滑块把手
# ------------------------------------------------------------------
def render_thumb(self, spec: ThumbSpec):
import skia
width = max(1, int(round(spec.width)))
height = max(1, int(round(spec.height)))
surface, canvas = self._new_canvas(width, height)
cx, cy = width / 2.0, height / 2.0
r1 = float(spec.r1)
r2 = float(spec.r2)
outline = parse_color(spec.outline, spec.outline_opacity)
outline2 = (
parse_color(spec.outline2, spec.outline2_opacity)
if spec.outline2
else None
)
if outline:
paint = skia.Paint(AntiAlias=True)
if outline2 and outline2 != outline:
# 与既有 SVG 实现一致:竖直线性渐变,stop 0.500208 / 0.954545
paint.setShader(
skia.GradientShader.MakeLinear(
points=[(r1, 1.0), (r1, r1 * 2.0 - 1.0)],
colors=[skia.Color(*outline), skia.Color(*outline2)],
positions=[0.500208, 0.954545],
)
)
else:
paint.setColor(skia.Color(*outline))
canvas.drawCircle(cx, cy, r1, paint)
fill = parse_color(spec.fill, spec.fill_opacity)
if fill:
canvas.drawCircle(
cx, cy, max(0.0, r1 - 1.0),
skia.Paint(AntiAlias=True, Color=skia.Color(*fill)),
)
inner = parse_color(spec.inner_fill, spec.inner_fill_opacity)
if inner:
canvas.drawCircle(
cx, cy, r2, skia.Paint(AntiAlias=True, Color=skia.Color(*inner))
)
return _to_pil(surface.makeImageSnapshot())
|