dsh-visualkit 0.0.0-stage → 0.0.1
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.
- package/CHANGELOG.md +60 -0
- package/LICENSE +26 -0
- package/README.md +427 -2
- package/THIRD_PARTY.md +55 -0
- package/cordis.patch.yml +29 -0
- package/data/assets/README.md +38 -0
- package/data/assets/icons/brands/bilibili.svg +1 -0
- package/data/assets/icons/brands/discord.svg +1 -0
- package/data/assets/icons/brands/neteasecloudmusic.svg +1 -0
- package/data/assets/icons/brands/qq.svg +1 -0
- package/data/assets/icons/brands/sinaweibo.svg +1 -0
- package/data/assets/icons/brands/steam.svg +1 -0
- package/data/assets/icons/brands/telegram.svg +1 -0
- package/data/assets/icons/brands/tiktok.svg +1 -0
- package/data/assets/icons/brands/twitch.svg +1 -0
- package/data/assets/icons/brands/wechat.svg +1 -0
- package/data/assets/icons/brands/x.svg +1 -0
- package/data/assets/icons/brands/youtube.svg +1 -0
- package/data/assets/icons/lucide/bell.svg +1 -0
- package/data/assets/icons/lucide/calendar.svg +1 -0
- package/data/assets/icons/lucide/clock.svg +1 -0
- package/data/assets/icons/lucide/gamepad-2.svg +1 -0
- package/data/assets/icons/lucide/gift.svg +1 -0
- package/data/assets/icons/lucide/heart.svg +1 -0
- package/data/assets/icons/lucide/mic.svg +1 -0
- package/data/assets/icons/lucide/play.svg +1 -0
- package/data/assets/icons/lucide/star.svg +1 -0
- package/data/assets/icons/lucide/video.svg +1 -0
- package/data/assets/icons/phosphor/LICENSE +21 -0
- package/data/assets/icons/phosphor/atom-duotone.svg +1 -0
- package/data/assets/icons/phosphor/binary-duotone.svg +1 -0
- package/data/assets/icons/phosphor/blueprint-duotone.svg +1 -0
- package/data/assets/icons/phosphor/circuitry-duotone.svg +1 -0
- package/data/assets/icons/phosphor/cloud-arrow-up-duotone.svg +1 -0
- package/data/assets/icons/phosphor/code-duotone.svg +1 -0
- package/data/assets/icons/phosphor/compass-duotone.svg +1 -0
- package/data/assets/icons/phosphor/cpu-duotone.svg +1 -0
- package/data/assets/icons/phosphor/cube-duotone.svg +1 -0
- package/data/assets/icons/phosphor/database-duotone.svg +1 -0
- package/data/assets/icons/phosphor/flask-duotone.svg +1 -0
- package/data/assets/icons/phosphor/frame-corners-duotone.svg +1 -0
- package/data/assets/icons/phosphor/function-duotone.svg +1 -0
- package/data/assets/icons/phosphor/hard-drives-duotone.svg +1 -0
- package/data/assets/icons/phosphor/hexagon-duotone.svg +1 -0
- package/data/assets/icons/phosphor/infinity-duotone.svg +1 -0
- package/data/assets/icons/phosphor/lightning-duotone.svg +1 -0
- package/data/assets/icons/phosphor/magic-wand-duotone.svg +1 -0
- package/data/assets/icons/phosphor/moon-stars-duotone.svg +1 -0
- package/data/assets/icons/phosphor/paint-brush-duotone.svg +1 -0
- package/data/assets/icons/phosphor/pen-nib-duotone.svg +1 -0
- package/data/assets/icons/phosphor/planet-duotone.svg +1 -0
- package/data/assets/icons/phosphor/rocket-launch-duotone.svg +1 -0
- package/data/assets/icons/phosphor/ruler-duotone.svg +1 -0
- package/data/assets/icons/phosphor/selection-plus-duotone.svg +1 -0
- package/data/assets/icons/phosphor/shapes-duotone.svg +1 -0
- package/data/assets/icons/phosphor/sparkle-duotone.svg +1 -0
- package/data/assets/icons/phosphor/sun-horizon-duotone.svg +1 -0
- package/data/assets/icons/phosphor/terminal-window-duotone.svg +1 -0
- package/data/assets/icons/phosphor/triangle-duotone.svg +1 -0
- package/data/assets/icons/phosphor/waveform-duotone.svg +1 -0
- package/data/assets/icons/tabler/alarm.svg +1 -0
- package/data/assets/icons/tabler/badge-ad.svg +1 -0
- package/data/assets/icons/tabler/bell.svg +1 -0
- package/data/assets/icons/tabler/bolt.svg +1 -0
- package/data/assets/icons/tabler/brand-bilibili.svg +1 -0
- package/data/assets/icons/tabler/brand-discord.svg +1 -0
- package/data/assets/icons/tabler/brand-qq.svg +1 -0
- package/data/assets/icons/tabler/brand-twitch.svg +1 -0
- package/data/assets/icons/tabler/brand-wechat.svg +1 -0
- package/data/assets/icons/tabler/brand-weibo.svg +1 -0
- package/data/assets/icons/tabler/brand-youtube.svg +1 -0
- package/data/assets/icons/tabler/broadcast.svg +1 -0
- package/data/assets/icons/tabler/calendar-event.svg +1 -0
- package/data/assets/icons/tabler/calendar-week.svg +1 -0
- package/data/assets/icons/tabler/clock-hour-3.svg +1 -0
- package/data/assets/icons/tabler/coin.svg +1 -0
- package/data/assets/icons/tabler/crown.svg +1 -0
- package/data/assets/icons/tabler/currency-yen.svg +1 -0
- package/data/assets/icons/tabler/device-gamepad-2.svg +1 -0
- package/data/assets/icons/tabler/diamond.svg +1 -0
- package/data/assets/icons/tabler/flame.svg +1 -0
- package/data/assets/icons/tabler/gift.svg +1 -0
- package/data/assets/icons/tabler/headphones.svg +1 -0
- package/data/assets/icons/tabler/heart-filled.svg +1 -0
- package/data/assets/icons/tabler/heart.svg +1 -0
- package/data/assets/icons/tabler/live-view.svg +1 -0
- package/data/assets/icons/tabler/microphone.svg +1 -0
- package/data/assets/icons/tabler/mood-smile.svg +1 -0
- package/data/assets/icons/tabler/mood-tongue.svg +1 -0
- package/data/assets/icons/tabler/movie.svg +1 -0
- package/data/assets/icons/tabler/player-play.svg +1 -0
- package/data/assets/icons/tabler/playlist.svg +1 -0
- package/data/assets/icons/tabler/rocket.svg +1 -0
- package/data/assets/icons/tabler/sparkles.svg +1 -0
- package/data/assets/icons/tabler/star-filled.svg +1 -0
- package/data/assets/icons/tabler/star.svg +1 -0
- package/data/assets/icons/tabler/tag.svg +1 -0
- package/data/assets/icons/tabler/thumb-up.svg +1 -0
- package/data/assets/icons/tabler/ticket.svg +1 -0
- package/data/assets/icons/tabler/video.svg +1 -0
- package/data/assets/illustrations/openpeeps/LICENSE +8 -0
- package/data/assets/illustrations/openpeeps/peep-10.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-100.svg +32 -0
- package/data/assets/illustrations/openpeeps/peep-101.svg +25 -0
- package/data/assets/illustrations/openpeeps/peep-102.svg +28 -0
- package/data/assets/illustrations/openpeeps/peep-103.svg +32 -0
- package/data/assets/illustrations/openpeeps/peep-105.svg +25 -0
- package/data/assets/illustrations/openpeeps/peep-12.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-13.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-16.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-17.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-22.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-25.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-26.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-27.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-30.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-31.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-34.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-38.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-39.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-4.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-41.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-44.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-47.svg +26 -0
- package/data/assets/illustrations/openpeeps/peep-48.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-50.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-51.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-53.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-54.svg +26 -0
- package/data/assets/illustrations/openpeeps/peep-55.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-56.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-58.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-61.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-64.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-68.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-69.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-7.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-72.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-76.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-77.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-80.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-81.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-83.svg +29 -0
- package/data/assets/manifest.json +1 -0
- package/data/assets/parts//342/206/223/346/212/225/345/275/261.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2711.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2712.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2713.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2714.png +0 -0
- package/data/assets/parts//345/217/214/351/251/254/345/260/276.png +0 -0
- package/data/assets/parts//345/217/214/351/251/254/345/260/2761.png +0 -0
- package/data/assets/parts//345/217/2211.png +0 -0
- package/data/assets/parts//345/217/2212.png +0 -0
- package/data/assets/parts//345/217/2213.png +0 -0
- package/data/assets/parts//345/217/221/345/234/210.png +0 -0
- package/data/assets/parts//345/217/221/345/244/2711.png +0 -0
- package/data/assets/parts//345/217/221/345/244/2712.png +0 -0
- package/data/assets/parts//345/217/243/346/260/264/345/267/276/345/211/215.png +0 -0
- package/data/assets/parts//345/217/243/346/260/264/345/267/276/345/220/216.png +0 -0
- package/data/assets/parts//345/217/263/344/270/212/347/234/274/347/235/221.png +0 -0
- package/data/assets/parts//345/217/263/346/211/213.png +0 -0
- package/data/assets/parts//345/217/263/346/211/2131.png +0 -0
- package/data/assets/parts//345/217/263/347/234/274/347/220/203.png +0 -0
- package/data/assets/parts//345/217/263/347/234/274/347/231/275.png +0 -0
- package/data/assets/parts//345/217/263/350/205/277.png +0 -0
- package/data/assets/parts//345/221/206/346/257/233.png +0 -0
- package/data/assets/parts//345/230/264/345/267/264.png +0 -0
- package/data/assets/parts//345/260/276/345/267/264.png +0 -0
- package/data/assets/parts//345/267/246/344/270/212/347/234/274/347/235/221.png +0 -0
- package/data/assets/parts//345/267/246/345/217/214/347/234/274/347/232/256.png +0 -0
- package/data/assets/parts//345/267/246/346/211/213.png +0 -0
- package/data/assets/parts//345/267/246/346/211/2131.png +0 -0
- package/data/assets/parts//345/267/246/347/234/274/347/220/203.png +0 -0
- package/data/assets/parts//345/267/246/347/234/274/347/231/275.png +0 -0
- package/data/assets/parts//345/267/246/350/205/277.png +0 -0
- package/data/assets/parts//346/212/225/345/275/261.png +0 -0
- package/data/assets/parts//347/234/211/346/257/233.png +0 -0
- package/data/assets/parts//347/234/274/347/275/251.png +0 -0
- package/data/assets/parts//350/200/263/346/234/2651.png +0 -0
- package/data/assets/parts//350/200/263/346/234/2652.png +0 -0
- package/data/assets/parts//350/204/221/345/243/263.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/345/211/215.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/345/220/216.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/351/273/221.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/351/273/2211.png +0 -0
- package/data/assets/parts//350/272/253/344/275/223.png +0 -0
- package/data/assets/parts//351/223/201/346/235/277.png +0 -0
- package/data/assets/parts//351/273/221/347/234/274/345/234/210.png +0 -0
- package/data/assets/parts//351/273/221/347/234/274/345/234/2102.png +0 -0
- package/data/assets/patterns/checker.svg +1 -0
- package/data/assets/patterns/circles-overlap.svg +1 -0
- package/data/assets/patterns/crosshatch.svg +1 -0
- package/data/assets/patterns/diagonal.svg +1 -0
- package/data/assets/patterns/dots.svg +1 -0
- package/data/assets/patterns/grid.svg +1 -0
- package/data/assets/patterns/halftone.svg +1 -0
- package/data/assets/patterns/plus.svg +1 -0
- package/data/assets/patterns/scales.svg +1 -0
- package/data/assets/patterns/triangles.svg +1 -0
- package/data/assets/patterns/waves.svg +1 -0
- package/data/assets/patterns/zigzag.svg +1 -0
- package/data/assets/stickers/openmoji/1F375.svg +12 -0
- package/data/assets/stickers/openmoji/1F9E8.svg +21 -0
- package/data/assets/stickers/twemoji/1f389.svg +1 -0
- package/data/assets/stickers/twemoji/1f3a4.svg +1 -0
- package/data/assets/stickers/twemoji/1f3a7.svg +1 -0
- package/data/assets/stickers/twemoji/1f3ae.svg +1 -0
- package/data/assets/stickers/twemoji/1f44b.svg +1 -0
- package/data/assets/stickers/twemoji/1f44d.svg +1 -0
- package/data/assets/stickers/twemoji/1f494.svg +1 -0
- package/data/assets/stickers/twemoji/1f4a2.svg +1 -0
- package/data/assets/stickers/twemoji/1f4a8.svg +1 -0
- package/data/assets/stickers/twemoji/1f4b5.svg +1 -0
- package/data/assets/stickers/twemoji/1f4d6.svg +1 -0
- package/data/assets/stickers/twemoji/1f525.svg +1 -0
- package/data/assets/stickers/twemoji/1f600.svg +1 -0
- package/data/assets/stickers/twemoji/1f602.svg +1 -0
- package/data/assets/stickers/twemoji/1f60d.svg +1 -0
- package/data/assets/stickers/twemoji/1f62d.svg +1 -0
- package/data/assets/stickers/twemoji/1f680.svg +1 -0
- package/data/assets/stickers/twemoji/1f979.svg +1 -0
- package/data/assets/stickers/twemoji/1f97a.svg +1 -0
- package/data/assets/stickers/twemoji/26a1.svg +1 -0
- package/data/assets/stickers/twemoji/2705.svg +1 -0
- package/data/assets/stickers/twemoji/274c.svg +1 -0
- package/data/assets/stickers/twemoji/2764.svg +1 -0
- package/data/assets/stickers/twemoji/2b50.svg +1 -0
- package/data/assets/stickers/twemoji_1f525.svg +1 -0
- package/data/assets/stickers/twemoji_1f600.svg +1 -0
- package/data/assets/stickers/twemoji_1f979.svg +1 -0
- package/data/assets/stickers/twemoji_2764.svg +1 -0
- package/data/assets/textures/grain-dark.png +0 -0
- package/data/assets/textures/grain-light.png +0 -0
- package/data/assets/textures/grain-warm.png +0 -0
- package/data/lucide-icons.json +1 -0
- package/data/packs/tabler.icons.json +1 -0
- package/icon.svg +26 -0
- package/lib/assets.js +228 -0
- package/lib/audit.js +284 -0
- package/lib/contrast-render.js +213 -0
- package/lib/css-rewrite.js +379 -0
- package/lib/detect.js +701 -0
- package/lib/diff.js +153 -0
- package/lib/emit.js +557 -0
- package/lib/html-context.js +315 -0
- package/lib/icon-render.js +107 -0
- package/lib/icondb.js +402 -0
- package/lib/image-normalize.js +140 -0
- package/lib/index.js +1289 -0
- package/lib/layouts/common.js +48 -0
- package/lib/layouts/image.js +1010 -0
- package/lib/layouts/index.js +10 -0
- package/lib/layouts/intent.js +66 -0
- package/lib/layouts/metrics-text.js +91 -0
- package/lib/layouts/png.js +75 -0
- package/lib/layouts/rules-contrast.js +575 -0
- package/lib/layouts/rules-punct.js +486 -0
- package/lib/layouts/rules-structure.js +182 -0
- package/lib/layouts/source.js +1961 -0
- package/lib/layouts/svg.js +1204 -0
- package/lib/morphcore.js +139 -0
- package/lib/preview.js +225 -0
- package/lib/render-html.js +255 -0
- package/lib/render.js +486 -0
- package/lib/rules.js +267 -0
- package/lib/shadow.js +83 -0
- package/lib/skip.js +56 -0
- package/lib/style.js +688 -0
- package/lib/svg.js +729 -0
- package/lib/taste-image.js +323 -0
- package/lib/taste.js +219 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +92 -4
- package/vendor/morphicons-adapters.iife.js +903 -0
- package/vendor/morphicons.iife.js +2441 -0
- package/vendor/resvg/LICENSE +373 -0
- package/vendor/resvg/index.d.ts +98 -0
- package/vendor/resvg/index.js +23 -0
- package/vendor/resvg/js-binding.d.ts +52 -0
- package/vendor/resvg/js-binding.js +1 -0
- package/vendor/resvg/package.json +138 -0
- package/vendor/resvg/resvgjs.win32-x64-msvc.node +0 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* layouts/index.js — 排版体检三模式的门面。
|
|
3
|
+
* auditSource(root) UI 源码(HTML/CSS/Vue/JSX)
|
|
4
|
+
* auditSvg(markup, {name}) 成品 SVG 的文字几何
|
|
5
|
+
* auditImage(buffer, {name}) 渲染后的 PNG 构图
|
|
6
|
+
* 三者返回同一形状(common.js finalize),可同尺度比较与统一渲染。
|
|
7
|
+
*/
|
|
8
|
+
export { auditSource } from "./source.js";
|
|
9
|
+
export { auditSvg } from "./svg.js";
|
|
10
|
+
export { auditImage } from "./image.js";
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* layouts/intent.js — `.design-intent.json` 声明豁免(与 taste_check taste 同一套语义)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:实测(2026-11-07,Flow-Pilot)里 h5/h6 被收敛到正文大小,CSS 里写得
|
|
5
|
+
* 很清楚「h5/h6 没有组件映射,收敛到正文大小,避免浏览器默认 0.83em 把字缩成脚注」——
|
|
6
|
+
* 这是有意的取舍,但体检只能看到「h5 与正文只差 1.13×」并报 error。
|
|
7
|
+
*
|
|
8
|
+
* 声明格式(与 taste 完全一致,一个项目只需一份):
|
|
9
|
+
* { "allow": ["S9", "structure-heading-first"], "reason": "h5/h6 未做组件映射" }
|
|
10
|
+
* `allow` 里的规则不计分、不出现在 findings,而是进 `exempted` 并保留原因——
|
|
11
|
+
* 让「有意偏离」可追溯,而不是把规则删掉。
|
|
12
|
+
*/
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
|
|
16
|
+
const INTENT_FILE = ".design-intent.json";
|
|
17
|
+
|
|
18
|
+
function normalize(value) {
|
|
19
|
+
return Array.isArray(value) ? value.filter((id) => typeof id === "string" && id !== "") : [];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 读声明文件。不存在 / JSON 坏掉都不报错(一个坏声明不该让整次体检失败)。
|
|
24
|
+
* @param {string|null|undefined} rootDir 扫描根目录(单文件时传它所在目录)
|
|
25
|
+
*/
|
|
26
|
+
export function readIntent(rootDir) {
|
|
27
|
+
if (typeof rootDir !== "string" || rootDir === "") return { found: false, allow: [] };
|
|
28
|
+
let raw;
|
|
29
|
+
try {
|
|
30
|
+
raw = readFileSync(join(rootDir, INTENT_FILE), "utf8");
|
|
31
|
+
} catch {
|
|
32
|
+
return { found: false, allow: [] };
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const parsed = JSON.parse(raw);
|
|
36
|
+
return {
|
|
37
|
+
found: true,
|
|
38
|
+
allow: normalize(parsed?.allow),
|
|
39
|
+
...(typeof parsed?.skin === "string" ? { skin: parsed.skin } : {}),
|
|
40
|
+
...(typeof parsed?.reason === "string" ? { reason: parsed.reason } : {})
|
|
41
|
+
};
|
|
42
|
+
} catch {
|
|
43
|
+
return { found: true, invalid: true, allow: [] };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 按 allow 名单过滤 findings。
|
|
49
|
+
* @returns {{kept: object[], exempted: string[], exemptionReason?: string}}
|
|
50
|
+
*/
|
|
51
|
+
export function applyAllow(findings, intent) {
|
|
52
|
+
const allow = new Set(normalize(intent?.allow));
|
|
53
|
+
if (allow.size === 0) return { kept: findings, exempted: [] };
|
|
54
|
+
const hit = new Set();
|
|
55
|
+
const kept = findings.filter((finding) => {
|
|
56
|
+
if (!allow.has(finding.rule)) return true;
|
|
57
|
+
hit.add(finding.rule);
|
|
58
|
+
return false;
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
kept,
|
|
62
|
+
// 只报真实命中过的(声明了一个不存在的规则不算豁免,就不列)
|
|
63
|
+
exempted: [...hit].sort(),
|
|
64
|
+
...(typeof intent?.reason === "string" ? { exemptionReason: intent.reason } : {})
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* layouts/metrics-text.js — 文字宽度度量(不引入字体引擎)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要这张表(实测数据,2026-11-07):
|
|
5
|
+
* 旧的「汉字 1.0em / 拉丁 0.55em」模型在纯英文上误差 **10.0%**,中英混排 4.4%;
|
|
6
|
+
* 而 G4 行长、G1 边距、G6 重叠都靠宽度,10% 误差足以让「85% 画布宽」这类阈值判错。
|
|
7
|
+
*
|
|
8
|
+
* 为什么不用 HarfBuzz-WASM(436 KB,精度 0.4%):
|
|
9
|
+
* 把 ASCII 32–126 的真实 advance width 离线抽一次做成表(下面 WIDTHS),
|
|
10
|
+
* 运行时只是查表 —— 与 HarfBuzz 精度差 **0.00–0.02%**,体积 383 字节,零依赖。
|
|
11
|
+
* 表从 Microsoft YaHei(msyh.ttc, upem 2048)抽取;换成别的字体拉丁字宽会差 ~3–6%,
|
|
12
|
+
* 仍比扁平 0.55 好一个数量级。
|
|
13
|
+
*
|
|
14
|
+
* 汉字与全角标点恒为 1.0em —— 不是假设,是字体约定:实测
|
|
15
|
+
* 「中 文 龘 , 。 、 : ;」全部 1.0000em,「()」2.0000em。所以 CJK 不需要表。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** ASCII 32–126 的 advance width(×1000,相对 em)。离线取自 msyh.ttc。 */
|
|
19
|
+
const WIDTHS = [
|
|
20
|
+
296, 313, 436, 638, 586, 890, 870, 256, 334, 334, 455, 742, 241, 433, 241, 427,
|
|
21
|
+
586, 586, 586, 586, 586, 586, 586, 586, 586, 586, 241, 241, 742, 742, 742, 483,
|
|
22
|
+
1031, 704, 627, 669, 762, 550, 531, 744, 773, 294, 396, 635, 513, 977, 813, 815,
|
|
23
|
+
612, 815, 653, 577, 573, 747, 676, 1018, 645, 604, 620, 334, 416, 334, 742, 448,
|
|
24
|
+
295, 553, 639, 501, 640, 567, 347, 640, 616, 266, 267, 544, 266, 937, 616, 636,
|
|
25
|
+
639, 640, 382, 463, 373, 616, 525, 790, 507, 529, 492, 334, 269, 334, 742
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/** 全角区段:CJK / 假名 / 韩文 / 全角标点 / emoji —— advance 恒为 1em。 */
|
|
29
|
+
const WIDE_RANGES = [
|
|
30
|
+
[0x1100, 0x115f],
|
|
31
|
+
[0x2e80, 0x303e],
|
|
32
|
+
[0x3041, 0x33ff],
|
|
33
|
+
[0x3400, 0x4dbf],
|
|
34
|
+
[0x4e00, 0x9fff],
|
|
35
|
+
[0xa000, 0xa4cf],
|
|
36
|
+
[0xac00, 0xd7a3],
|
|
37
|
+
[0xf900, 0xfaff],
|
|
38
|
+
[0xfe30, 0xfe4f],
|
|
39
|
+
[0xff00, 0xff60],
|
|
40
|
+
[0xffe0, 0xffe6],
|
|
41
|
+
[0x1f000, 0x1faff],
|
|
42
|
+
[0x20000, 0x3fffd]
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
/** 字母下伸部分/组合符等零宽字符(不推进行宽)。 */
|
|
46
|
+
const ZERO_WIDTH = new Set([0x200b, 0x200c, 0x200d, 0xfeff, 0x00ad]);
|
|
47
|
+
|
|
48
|
+
function isWide(codePoint) {
|
|
49
|
+
for (const [start, end] of WIDE_RANGES) if (codePoint >= start && codePoint <= end) return true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 单字符 advance width(单位 em)。
|
|
55
|
+
* ASCII 查表;全宽区段 1.0;零宽字符 0;其余(拉丁扩展/符号)用 0.55 兜底。
|
|
56
|
+
*/
|
|
57
|
+
export function charWidth(char) {
|
|
58
|
+
const cp = typeof char === "string" ? char.codePointAt(0) : char;
|
|
59
|
+
if (cp === undefined) return 0;
|
|
60
|
+
if (cp >= 32 && cp <= 126) return WIDTHS[cp - 32] / 1000;
|
|
61
|
+
if (ZERO_WIDTH.has(cp)) return 0;
|
|
62
|
+
if (isWide(cp)) return 1;
|
|
63
|
+
return 0.55;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 一段文字的估算宽度(单位 = 字号倍数;乘 fontSize 得用户单位)。
|
|
68
|
+
* 非法输入(null/数字/对象)按空串处理 —— 体检不该因为一个怪值崩掉。
|
|
69
|
+
*/
|
|
70
|
+
export function measureText(text) {
|
|
71
|
+
let width = 0;
|
|
72
|
+
for (const char of String(text ?? "")) width += charWidth(char);
|
|
73
|
+
return width;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 同 measureText,但直接返回像素/用户单位。 */
|
|
77
|
+
export function measureTextPx(text, fontSize) {
|
|
78
|
+
const size = Number(fontSize);
|
|
79
|
+
if (!Number.isFinite(size) || size <= 0) return 0;
|
|
80
|
+
return measureText(text) * size;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 一行里的全角字符数(G4 的「一行 45 字」上限用)。 */
|
|
84
|
+
export function countWideChars(text) {
|
|
85
|
+
let count = 0;
|
|
86
|
+
for (const char of String(text ?? "")) if (isWide(char.codePointAt(0))) count += 1;
|
|
87
|
+
return count;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 表自检:长度必须是 95(ASCII 32–126 全段)。 */
|
|
91
|
+
export const WIDTH_TABLE_SIZE = WIDTHS.length;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* layouts/png.js — 最小 PNG 解码器(无依赖,lib 内部用)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要自己解:排版体检的 image 模式要看像素(内容包围盒/边距/重心/对齐),
|
|
5
|
+
* 而 resvg 只输出不会回读。引 sharp/pngjs 会破坏「零运行时依赖」的发布包约束。
|
|
6
|
+
* 只支持 resvg 实际产出的 8-bit 非隔行 PNG(colorType 0/2/4/6)——ours,不是通用库。
|
|
7
|
+
*/
|
|
8
|
+
import { inflateSync } from "node:zlib";
|
|
9
|
+
|
|
10
|
+
const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
11
|
+
|
|
12
|
+
function paeth(a, b, c) {
|
|
13
|
+
const p = a + b - c;
|
|
14
|
+
const pa = Math.abs(p - a);
|
|
15
|
+
const pb = Math.abs(p - b);
|
|
16
|
+
const pc = Math.abs(p - c);
|
|
17
|
+
if (pa <= pb && pa <= pc) return a;
|
|
18
|
+
return pb <= pc ? b : c;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** PNG buffer → { width, height, channels, data }(data 为 RGBA/RGB 解码后的行优先字节)。 */
|
|
22
|
+
export function decodePng(input) {
|
|
23
|
+
const buf = Buffer.isBuffer(input) ? input : Buffer.from(input);
|
|
24
|
+
if (buf.length < 32 || !buf.subarray(0, 8).equals(SIGNATURE)) throw new Error("不是 PNG(签名不匹配)");
|
|
25
|
+
let offset = 8;
|
|
26
|
+
let ihdr = null;
|
|
27
|
+
const idat = [];
|
|
28
|
+
while (offset + 8 <= buf.length) {
|
|
29
|
+
const len = buf.readUInt32BE(offset);
|
|
30
|
+
const type = buf.toString("ascii", offset + 4, offset + 8);
|
|
31
|
+
const data = buf.subarray(offset + 8, offset + 8 + len);
|
|
32
|
+
if (type === "IHDR") ihdr = { width: data.readUInt32BE(0), height: data.readUInt32BE(4), depth: data[8], colorType: data[9], interlace: data[12] };
|
|
33
|
+
else if (type === "IDAT") idat.push(data);
|
|
34
|
+
offset += 12 + len;
|
|
35
|
+
if (type === "IEND") break;
|
|
36
|
+
}
|
|
37
|
+
if (ihdr === null) throw new Error("PNG 缺 IHDR");
|
|
38
|
+
if (ihdr.depth !== 8) throw new Error(`只支持 8-bit PNG(收到 ${ihdr.depth}-bit)`);
|
|
39
|
+
if (ihdr.interlace !== 0) throw new Error("不支持隔行 PNG");
|
|
40
|
+
const channels = { 0: 1, 2: 3, 4: 2, 6: 4 }[ihdr.colorType];
|
|
41
|
+
if (channels === undefined) throw new Error(`不支持的 colorType ${ihdr.colorType}`);
|
|
42
|
+
const raw = inflateSync(Buffer.concat(idat));
|
|
43
|
+
const stride = ihdr.width * channels;
|
|
44
|
+
const out = Buffer.alloc(stride * ihdr.height);
|
|
45
|
+
let pos = 0;
|
|
46
|
+
for (let y = 0; y < ihdr.height; y += 1) {
|
|
47
|
+
const filter = raw[pos++];
|
|
48
|
+
const row = y * stride;
|
|
49
|
+
for (let x = 0; x < stride; x += 1) {
|
|
50
|
+
const cur = raw[pos++];
|
|
51
|
+
const left = x >= channels ? out[row + x - channels] : 0;
|
|
52
|
+
const up = y > 0 ? out[row - stride + x] : 0;
|
|
53
|
+
const upLeft = y > 0 && x >= channels ? out[row - stride + x - channels] : 0;
|
|
54
|
+
let val;
|
|
55
|
+
if (filter === 0) val = cur;
|
|
56
|
+
else if (filter === 1) val = cur + left;
|
|
57
|
+
else if (filter === 2) val = cur + up;
|
|
58
|
+
else if (filter === 3) val = cur + ((left + up) >> 1);
|
|
59
|
+
else if (filter === 4) val = cur + paeth(left, up, upLeft);
|
|
60
|
+
else throw new Error(`未知 PNG 行滤波 ${filter}`);
|
|
61
|
+
out[row + x] = val & 0xff;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return { width: ihdr.width, height: ihdr.height, channels, data: out };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 取像素,统一返回 {r,g,b,a}。 */
|
|
68
|
+
export function pixelAt(img, x, y) {
|
|
69
|
+
const i = (y * img.width + x) * img.channels;
|
|
70
|
+
const d = img.data;
|
|
71
|
+
if (img.channels === 4) return { r: d[i], g: d[i + 1], b: d[i + 2], a: d[i + 3] };
|
|
72
|
+
if (img.channels === 3) return { r: d[i], g: d[i + 1], b: d[i + 2], a: 255 };
|
|
73
|
+
if (img.channels === 2) return { r: d[i], g: d[i], b: d[i], a: d[i + 1] };
|
|
74
|
+
return { r: d[i], g: d[i], b: d[i], a: d[i] };
|
|
75
|
+
}
|