@x-cmd/draw 0.5.9 → 0.5.10

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.
@@ -234,6 +234,8 @@ export function renderButterfly(spec, o) {
234
234
  }
235
235
  else {
236
236
  /* ---- 上下版:类别是列,柱自中轴往上/往下长 */
237
+ // 柱端数值的盒子(折线的数值要躲开它 ⇒ 两个数字不叠在一起 ✗)
238
+ const barLabelBoxes = [];
237
239
  const labelH = 22;
238
240
  // 往下长的柱子,数值写在柱底外侧 → 底下要给数值留一条带,
239
241
  // 否则最长的那根柱(数值到头)会和类目标签叠在一起
@@ -308,7 +310,12 @@ export function renderButterfly(spec, o) {
308
310
  if (!(v > 0))
309
311
  return;
310
312
  const vy = dir < 0 ? centreY - totalLen - 6 : centreY + totalLen + 6;
311
- tbLabels.push(`<text x="${r2(cx)}" y="${r2(vy + (dir < 0 ? 0 : 10))}" text-anchor="middle" font-size="12" font-weight="600" fill="var(--d-text)" style="${TAB}">${esc(fmtFull(v))}</text>`);
313
+ const tv = fmtFull(v);
314
+ const ty2 = vy + (dir < 0 ? 0 : 10);
315
+ tbLabels.push(`<text x="${r2(cx)}" y="${r2(ty2)}" text-anchor="middle" font-size="12" font-weight="600" fill="var(--d-text)" style="${TAB}">${esc(tv)}</text>`);
316
+ // 柱端数值的盒子(折线的数值要躲开它 ⇒ 两个数字不叠在一起 ✗)
317
+ const tw2 = textW(tv, 12);
318
+ barLabelBoxes.push({ x0: cx - tw2 / 2, x1: cx + tw2 / 2, y0: ty2 - 12, y1: ty2 + 3 });
312
319
  };
313
320
  drawSide(left, leftTot, -1);
314
321
  drawSide(right, rightTot, 1);
@@ -325,10 +332,25 @@ export function renderButterfly(spec, o) {
325
332
  if (pts.length > 1) {
326
333
  parts.push(`<path d="${pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${r2(p[0])} ${r2(p[1])}`).join('')}" fill="none" stroke="var(--d-s9)" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`);
327
334
  pts.forEach((p) => parts.push(`<circle cx="${r2(p[0])}" cy="${r2(p[1])}" r="3" fill="var(--d-s9)" stroke="var(--d-card)" stroke-width="1.5"/>`));
335
+ // 线的数值:在点周围几个位置里挑一个**不撞柱端数值**的,并垫一块画布色
336
+ // (柱短的时候,柱端的数值就在点的旁边 ⇒ 两个数字叠在一起 ✗)
337
+ const cand = [[-16, 0], [-16, -18], [-16, 18], [14, 0], [14, -18], [14, 18]];
328
338
  line.values.forEach((v, i) => {
329
339
  const text = fmtFull(v);
330
340
  const tw = textW(text, 11);
331
- parts.push(`<rect x="${r2(pts[i][0] - tw / 2 - 4)}" y="${r2(pts[i][1] - 24)}" width="${r2(tw + 8)}" height="14" rx="4" fill="var(--d-card)" fill-opacity="0.92"/>`, `<text x="${r2(pts[i][0])}" y="${r2(pts[i][1] - 14)}" text-anchor="middle" font-size="11" font-weight="600" fill="var(--d-s9)" style="${TAB}">${esc(text)}</text>`);
341
+ let bx = pts[i][0] - tw / 2 - 4;
342
+ let by = pts[i][1] - 24;
343
+ for (const [dy, dx] of cand) {
344
+ const cx = pts[i][0] + dx - tw / 2 - 4;
345
+ const cy = pts[i][1] + dy - 11;
346
+ const hit = barLabelBoxes.some((b) => cx < b.x1 && cx + tw + 8 > b.x0 && cy < b.y1 && cy + 14 > b.y0);
347
+ if (!hit) {
348
+ bx = cx;
349
+ by = cy;
350
+ break;
351
+ }
352
+ }
353
+ parts.push(`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(tw + 8)}" height="14" rx="4" fill="var(--d-card)" fill-opacity="0.92"/>`, `<text x="${r2(bx + 4 + tw / 2)}" y="${r2(by + 11)}" text-anchor="middle" font-size="11" font-weight="600" fill="var(--d-s9)" style="${TAB}">${esc(text)}</text>`);
332
354
  });
333
355
  }
334
356
  // 柱端的数值画在折线之上
@@ -16,6 +16,7 @@
16
16
  // 出来的 SVG 自足,浏览器和 PNG 光栅化一个样。
17
17
  import { ApiError, clip } from '../specs/base.js';
18
18
  import { THEMES, FONT_STACK, withBackground, withFont, setSize, ensureSvgNs, ensureViewBox, } from './source-theme.js';
19
+ import { textW } from './svg.js';
19
20
  import { parseAbc, keyAlterations, beatsOf, transposeKey, AbcError } from './abc-parse.js';
20
21
  /* ---------------------------------------------------------------- 布局常数 */
21
22
  /** 一个音符占多宽 */
@@ -77,8 +78,14 @@ export async function renderJianpu(src, opts = {}) {
77
78
  head.push(`<text x="86" y="26" font-size="15" fill="${theme.muted}">${meter ? `${meter[1]}/${meter[2]}` : tune.meter}</text>`);
78
79
  if (tune.tempo)
79
80
  head.push(`<text x="140" y="26" font-size="14" fill="${theme.muted}">${esc(tune.tempo)}</text>`);
80
- if (tune.title)
81
- head.push(`<text x="${(opts.width ?? 560) / 2}" y="26" text-anchor="middle" font-size="16" font-weight="600" fill="${theme.ink}">${esc(tune.title)}</text>`);
81
+ // 标题:在**头部剩下的地方**里居中(原来在整个画布居中 ⇒ 标题长了就压到拍号/速度上 ✗)
82
+ if (tune.title) {
83
+ const headEnd = 140 + (tune.tempo ? 60 : 0); // 拍号/速度占的那一段
84
+ const tw = textW(tune.title, 16);
85
+ const avail = (opts.width ?? 560) - headEnd - 16;
86
+ const tx = tw < avail ? headEnd + avail / 2 : (opts.width ?? 560) / 2;
87
+ head.push(`<text x="${r2(tx)}" y="26" text-anchor="middle" font-size="16" font-weight="600" fill="${theme.ink}">${esc(tune.title)}</text>`);
88
+ }
82
89
  // 正文:一格一个音符
83
90
  const cells = [];
84
91
  const state = { ...keyAlts };
@@ -88,10 +95,13 @@ export async function renderJianpu(src, opts = {}) {
88
95
  const bar = () => {
89
96
  cells.push(`<line x1="${r2(x - 5)}" y1="${y - 22}" x2="${r2(x - 5)}" y2="${y + 18}" stroke="${theme.line}" stroke-width="1"/>`);
90
97
  };
98
+ // 每行几个音符:按画布宽算(死用 PER_LINE ⇒ 窄画布上音符跑出右边界 ✗)
99
+ const width = opts.width ?? 560;
100
+ const perLine = Math.max(1, Math.min(PER_LINE, Math.floor((width - 28) / SLOT)));
91
101
  // 谱杠的位置(ABC 里小节线前面的音符个数)
92
102
  const barAt = new Set(tune.bars);
93
103
  tune.notes.forEach((n, i) => {
94
- if (col >= PER_LINE) {
104
+ if (col >= perLine) {
95
105
  col = 0;
96
106
  y += LINE_GAP;
97
107
  x = 14;
@@ -103,7 +113,6 @@ export async function renderJianpu(src, opts = {}) {
103
113
  col++;
104
114
  });
105
115
  bar();
106
- const width = opts.width ?? 560;
107
116
  const height = y + 40;
108
117
  let out = `<svg xmlns="http://www.w3.org/2000/svg" width="${r2(width)}" height="${r2(height)}" viewBox="0 0 ${r2(width)} ${r2(height)}">`;
109
118
  out += head.join('');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.5.9",
3
+ "version": "0.5.10",
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",