@x-cmd/draw 0.5.2 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -26,8 +26,10 @@ export declare const SERIF_STACK = "'Times New Roman',Times,Georgia,serif";
|
|
|
26
26
|
* 垫一层底色 —— 图是独立的一张图,得有底,不然在白底页面上看不见深色的线。
|
|
27
27
|
*
|
|
28
28
|
* 底色垫在**最下面**:插到 <svg> 开头(第一个子元素之前)。
|
|
29
|
+
*
|
|
30
|
+
* @param y 画布左上角的 y(viewBox 的原点不是 0 时,底要从那儿开始铺)
|
|
29
31
|
*/
|
|
30
|
-
export declare function withBackground(svg: string, theme: SvgTheme, w: number, h: number): string;
|
|
32
|
+
export declare function withBackground(svg: string, theme: SvgTheme, w: number, h: number, y?: number): string;
|
|
31
33
|
/**
|
|
32
34
|
* 库里写死的颜色换成我们这套。
|
|
33
35
|
*
|
|
@@ -36,9 +36,11 @@ const r2 = (v) => Math.round(v * 100) / 100;
|
|
|
36
36
|
* 垫一层底色 —— 图是独立的一张图,得有底,不然在白底页面上看不见深色的线。
|
|
37
37
|
*
|
|
38
38
|
* 底色垫在**最下面**:插到 <svg> 开头(第一个子元素之前)。
|
|
39
|
+
*
|
|
40
|
+
* @param y 画布左上角的 y(viewBox 的原点不是 0 时,底要从那儿开始铺)
|
|
39
41
|
*/
|
|
40
|
-
export function withBackground(svg, theme, w, h) {
|
|
41
|
-
const rect = `<rect x="0" y="
|
|
42
|
+
export function withBackground(svg, theme, w, h, y = 0) {
|
|
43
|
+
const rect = `<rect x="0" y="${r2(y)}" width="${r2(w)}" height="${r2(h)}" fill="${theme.bg}"/>`;
|
|
42
44
|
const at = svg.indexOf('>');
|
|
43
45
|
if (at < 0)
|
|
44
46
|
return svg;
|
package/dist/render/wavedrom.js
CHANGED
|
@@ -74,10 +74,10 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
74
74
|
// 结构色换掉(波形的数据色不动):#000 是笔画/文字,#fff 是波形块的底
|
|
75
75
|
out = out.replace(/<style([^>]*)>([\s\S]*?)<\/style>/g, (_m, attrs, css) => {
|
|
76
76
|
let themed = css
|
|
77
|
-
.replace(/stroke
|
|
78
|
-
.replace(/color
|
|
79
|
-
.replace(/fill
|
|
80
|
-
.replace(/fill
|
|
77
|
+
.replace(/stroke:\s*#000\b/g, `stroke:${theme.ink}`)
|
|
78
|
+
.replace(/color:\s*#000\b/g, `color:${theme.ink}`)
|
|
79
|
+
.replace(/fill:\s*#000\b/g, `fill:${theme.ink}`)
|
|
80
|
+
.replace(/fill:\s*#fff;/g, `fill:${theme.panel};`);
|
|
81
81
|
// 文字没写 fill(默认黑)—— 给一条
|
|
82
82
|
themed = themed.replace(/text\{/, `text{fill:${theme.ink};`);
|
|
83
83
|
// 皮肤那个深蓝(信号名用它)在暗色底上太沉 —— 换成站上的强调色
|
|
@@ -88,6 +88,18 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
88
88
|
// wavedrom 的画法全写在 CSS 类里(.s1/.s2/…)—— 样式块会被出图那道工序剥掉,
|
|
89
89
|
// 所以先内联到元素上:图就自足了(没有 CSS),浏览器和 PNG 一个样。
|
|
90
90
|
out = inlineCss(out);
|
|
91
|
+
// 库还有一批评色**不写在样式表里**,是写死在属性/内联样式里的(#188):
|
|
92
|
+
// - 分段模拟波的折线:style="fill:none;stroke:#000;…"(render-piece-wise.js 里写死)
|
|
93
|
+
// - 位域图的格线:<g stroke="black" …>
|
|
94
|
+
// 这些是**结构色**,得跟主题走 —— 不然暗底上是纯黑,等于没画。
|
|
95
|
+
out = out
|
|
96
|
+
.replace(/stroke:\s*#000\b/gi, `stroke:${theme.ink}`)
|
|
97
|
+
.replace(/\bstroke="black"/gi, `stroke="${theme.line}"`)
|
|
98
|
+
.replace(/\bfill="black"/gi, `fill="${theme.ink}"`)
|
|
99
|
+
.replace(/\bcolor="black"/gi, `color="${theme.ink}"`);
|
|
100
|
+
// 没写填色的文字(位域图的位号/字段名):库靠样式表里的 text{} 填黑 ——
|
|
101
|
+
// 服务端没有那张样式表(样式表是浏览器那边注入的)⇒ 字是 SVG 默认的黑 ⇒ 暗底上等于没有。
|
|
102
|
+
out = out.replace(/<text\b([^>]*)>/g, (m, attrs) => /\bfill\s*[:=]/.test(attrs) ? m : `<text${attrs} fill="${theme.ink}">`);
|
|
91
103
|
// 数据块(head/body/tail)的颜色:皮肤的一个颜色类会**同时**用在
|
|
92
104
|
// 「波形的 0 块」和「数据块」上(.s7 是白底)—— 波形 0 块在暗色底上要换成卡片色,
|
|
93
105
|
// 但数据块得留住自己的颜色(官方演示里 head/body/tail 是彩色的)。
|
|
@@ -105,9 +117,134 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
105
117
|
if (scheme === 'dark')
|
|
106
118
|
out = out.replace(/#0041c4/g, theme.accent);
|
|
107
119
|
out = withFont(out);
|
|
108
|
-
|
|
109
|
-
|
|
120
|
+
// 分段模拟波(pw):画布要撑到能装下整条路径 —— 见 fitPieceWise()
|
|
121
|
+
const fitted = fitPieceWise(out, size);
|
|
122
|
+
out = fitted.svg;
|
|
123
|
+
out = withBackground(out, theme, fitted.w, fitted.h);
|
|
124
|
+
out = setSize(out, fitted.w, fitted.h);
|
|
110
125
|
out = ensureSvgNs(out);
|
|
111
126
|
out = ensureViewBox(out);
|
|
127
|
+
// viewBox 跟着画布走(内容整体往下挪了 dy ⇒ 画布要长出那一截)
|
|
128
|
+
out = out.replace(/viewBox="[^"]*"/, `viewBox="0 0 ${fitted.w} ${fitted.h}"`);
|
|
129
|
+
if (fitted.dy)
|
|
130
|
+
out = shiftContent(out, fitted.dy);
|
|
112
131
|
return out;
|
|
113
132
|
}
|
|
133
|
+
/** 每条路径命令吃几个数 */
|
|
134
|
+
const CMD_ARGS = { m: 2, l: 2, t: 2, h: 1, v: 1, q: 4, c: 6, s: 4, a: 7 };
|
|
135
|
+
/**
|
|
136
|
+
* 路径的坐标范围(相对它的父组)。
|
|
137
|
+
*
|
|
138
|
+
* 只求「画到哪里」,不求画得长什么样 ⇒ 曲线拿控制点当极值(略大,宁多勿少)。
|
|
139
|
+
*/
|
|
140
|
+
function pathExtent(d) {
|
|
141
|
+
const toks = d.match(/[a-zA-Z]|-?\d*\.?\d+(?:e[-+]?\d+)?/g) ?? [];
|
|
142
|
+
let x = 0;
|
|
143
|
+
let y = 0;
|
|
144
|
+
let x0 = 0;
|
|
145
|
+
let y0 = 0;
|
|
146
|
+
let x1 = 0;
|
|
147
|
+
let y1 = 0;
|
|
148
|
+
let seen = false;
|
|
149
|
+
const grow = (px, py) => {
|
|
150
|
+
if (!seen) {
|
|
151
|
+
x0 = x1 = px;
|
|
152
|
+
y0 = y1 = py;
|
|
153
|
+
seen = true;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
x0 = Math.min(x0, px);
|
|
157
|
+
x1 = Math.max(x1, px);
|
|
158
|
+
y0 = Math.min(y0, py);
|
|
159
|
+
y1 = Math.max(y1, py);
|
|
160
|
+
};
|
|
161
|
+
let cmd = '';
|
|
162
|
+
let i = 0;
|
|
163
|
+
while (i < toks.length) {
|
|
164
|
+
if (/[a-zA-Z]/.test(toks[i])) {
|
|
165
|
+
cmd = toks[i].toLowerCase();
|
|
166
|
+
i++;
|
|
167
|
+
}
|
|
168
|
+
const n = CMD_ARGS[cmd];
|
|
169
|
+
if (n === undefined)
|
|
170
|
+
break;
|
|
171
|
+
const args = toks.slice(i, i + n).map(Number);
|
|
172
|
+
if (args.length < n || args.some(Number.isNaN))
|
|
173
|
+
break;
|
|
174
|
+
i += n;
|
|
175
|
+
if (cmd === 'm') {
|
|
176
|
+
x = args[0];
|
|
177
|
+
y = args[1];
|
|
178
|
+
grow(x, y);
|
|
179
|
+
}
|
|
180
|
+
else if (cmd === 'l' || cmd === 't') {
|
|
181
|
+
x += args[0];
|
|
182
|
+
y += args[1];
|
|
183
|
+
grow(x, y);
|
|
184
|
+
}
|
|
185
|
+
else if (cmd === 'h') {
|
|
186
|
+
x += args[0];
|
|
187
|
+
grow(x, y);
|
|
188
|
+
}
|
|
189
|
+
else if (cmd === 'v') {
|
|
190
|
+
y += args[0];
|
|
191
|
+
grow(x, y);
|
|
192
|
+
}
|
|
193
|
+
else if (cmd === 'q' || cmd === 's') {
|
|
194
|
+
grow(x + args[0], y + args[1]);
|
|
195
|
+
x += args[2];
|
|
196
|
+
y += args[3];
|
|
197
|
+
grow(x, y);
|
|
198
|
+
}
|
|
199
|
+
else if (cmd === 'c') {
|
|
200
|
+
grow(x + args[0], y + args[1]);
|
|
201
|
+
grow(x + args[2], y + args[3]);
|
|
202
|
+
x += args[4];
|
|
203
|
+
y += args[5];
|
|
204
|
+
grow(x, y);
|
|
205
|
+
}
|
|
206
|
+
else if (cmd === 'a') {
|
|
207
|
+
x += args[5];
|
|
208
|
+
y += args[6];
|
|
209
|
+
grow(x, y);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return seen ? { x0, y0, x1, y1 } : null;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 分段模拟波(pw):把画布撑到能装下整条路径。
|
|
216
|
+
*
|
|
217
|
+
* 库把 d 路径按「两格宽、一格高」缩放后画在 lane 的起点,但 lane 的宽度是按
|
|
218
|
+
* wave 的**字符个数**算的 —— pw 没有字符 ⇒ lane 只有一格宽 ⇒ 路径大半落在
|
|
219
|
+
* 画布外(库自己的短处:render-piece-wise.js 只管画,不管留位)。
|
|
220
|
+
*
|
|
221
|
+
* 我们不改库的算法,只把画布撑开:路径画到哪,画布就到哪。
|
|
222
|
+
*/
|
|
223
|
+
function fitPieceWise(svg, size) {
|
|
224
|
+
// pw 的路径长这样:<g transform="translate(0,Y)"><path style="fill:none;stroke:…;stroke-width:1px;" d="…"/>
|
|
225
|
+
const m = /<g transform="translate\(0,(-?[\d.]+)\)">\s*<path[^>]*?stroke-width:1px[^>]*?d="([^"]+)"/.exec(svg);
|
|
226
|
+
if (!m)
|
|
227
|
+
return { svg, w: size.w, h: size.h, dy: 0 };
|
|
228
|
+
const ext = pathExtent(m[2]);
|
|
229
|
+
if (!ext)
|
|
230
|
+
return { svg, w: size.w, h: size.h, dy: 0 };
|
|
231
|
+
// lane 组自己的位移(标签列占多宽,全看它)
|
|
232
|
+
const lane = /<g transform="translate\((-?[\d.]+),(-?[\d.]+)\)" id="lanes/.exec(svg);
|
|
233
|
+
const lx = lane ? Number(lane[1]) : 0;
|
|
234
|
+
const ly = lane ? Number(lane[2]) : 0;
|
|
235
|
+
const gy = Number(m[1]);
|
|
236
|
+
const ax1 = lx + ext.x1;
|
|
237
|
+
const ay0 = ly + gy + ext.y0;
|
|
238
|
+
const ay1 = ly + gy + ext.y1;
|
|
239
|
+
// 路径可能跑到 y=0 之上 ⇒ 内容整体往下挪 dy,画布跟着长出那一截
|
|
240
|
+
const dy = Math.max(0, -ay0);
|
|
241
|
+
const w = Math.max(size.w, Math.ceil(ax1 + 6));
|
|
242
|
+
const h = Math.max(size.h, Math.ceil(ay1 + 4 + dy));
|
|
243
|
+
return { svg, w, h, dy };
|
|
244
|
+
}
|
|
245
|
+
/** 画布往上挪了 dy ⇒ 内容整体往下挪,不然上面那截看不见 */
|
|
246
|
+
function shiftContent(svg, dy) {
|
|
247
|
+
return svg
|
|
248
|
+
.replace(/<g id="(waves_[^"]*)">/, `<g transform="translate(0,${dy})" id="$1">`)
|
|
249
|
+
.replace(/<g id="(groups_[^"]*)">/, `<g transform="translate(0,${dy})" id="$1">`);
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|