feat(rust): rust-spike 后端转正——musl 静态二进制 2.2MB + scratch 镜像

- 零依赖 TTF/OTF 子集化 + woff2 编码,Windows 兼容修复(maxp maxZones、表 checksum)
- 字体 API 强缓存 max-age=86400 + ETag/304 协商兜底
- 发布链路切换 build_backend_rs,Dockerfile FROM scratch 仅含 webfont-rs + dist/
This commit is contained in:
崮生(子虚) 2026-11-04 19:07:23 +08:00
parent 78e0be7433
commit 61c1f56aac
20 changed files with 12638 additions and 5 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
# 只需要 dist/ 与 dist_backend/,其余全部排除(cargo target 116M、node_modules、字体库等不进 context)
*
!dist/
!dist_backend/

8
.gitignore vendored
View File

@ -47,3 +47,11 @@ packages/leafer-x-webfont/vendor
# 商业化文档仅本地保留,不入库(png 截图正常跟踪)
doc/*.md
.zig-cache
zig-out
target-perf
# 本地工具/工作区
.chatcode/
rust-spike/target-perf/

View File

@ -1,6 +1,12 @@
# rust 后端(musl 静态二进制)+ vite-ssg 前端静态资源,FROM scratch 无基础镜像
# 镜像内容 = webfont-rs(约2.2MB) + dist/,构建链见 package.json release 脚本:
# pnpm build(vite-ssg → dist/)
# pnpm build_backend_rs(cargo musl 静态编译 → dist_backend/webfont-rs)
FROM scratch
WORKDIR /home/
COPY dist_backend/app.lrt /home/app.lrt
COPY llrt /home/llrt
COPY dist_backend/webfont-rs /home/webfont-rs
COPY dist/ /home/dist/
CMD ["/home/llrt", "/home/app.lrt"]
# 服务器 compose 映射 8087:8087(compose 不可改),容器内固定监听 8087
ENV PORT=8087
EXPOSE 8087
CMD ["/home/webfont-rs"]

View File

@ -1,7 +1,7 @@
{
"name": "webfont",
"private": true,
"version": "1.14.6",
"version": "1.15.0",
"type": "module",
"scripts": {
"dev": "pnpx tsx scripts/dev-all.ts",
@ -11,11 +11,12 @@
"serve:llrt": "./llrt ./dist_backend/app.cjs",
"build": "vite-ssg build",
"build_backend": "pnpx tsx scripts/build-backend.ts",
"build_backend_rs": "cargo build --profile tinyfast --target x86_64-unknown-linux-musl --manifest-path rust-spike/Cargo.toml && cp rust-spike/target/x86_64-unknown-linux-musl/tinyfast/webfont-rs dist_backend/webfont-rs",
"lint:font-config": "pnpx tsx scripts/lint-font-config.ts",
"docker_build": "docker build -t llej0/web-font:${npm_package_version} -t llej0/web-font:latest .",
"docker_push": "docker push llej0/web-font:${npm_package_version} && docker push llej0/web-font:latest",
"preview": "vite preview",
"release": "pnpm build && pnpm build_backend && pnpm docker_build && pnpm docker_push"
"release": "pnpm build && pnpm build_backend_rs && pnpm docker_build && pnpm docker_push"
},
"dependencies": {
"@unhead/vue": "^3.4.3",

1
rust-spike/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

46
rust-spike/Cargo.lock generated Normal file
View File

@ -0,0 +1,46 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "alloc-no-stdlib"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a"
[[package]]
name = "alloc-stdlib"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1"
dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "brotli"
version = "9.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "627b7cfb0578f0eafce087a90fcd40add1231900b69e4d4de2d456260a4f5d81"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
"brotli-decompressor",
]
[[package]]
name = "brotli-decompressor"
version = "6.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34c6783f42c21c952ad7112b354f0ba2f10760e9decea5b1b60523d6e270faaf"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
]
[[package]]
name = "webfont-rs"
version = "0.1.0"
dependencies = [
"brotli",
]

28
rust-spike/Cargo.toml Normal file
View File

@ -0,0 +1,28 @@
[package]
name = "webfont-rs"
version = "0.1.0"
edition = "2024"
[dependencies]
brotli = "9"
[profile.release]
opt-level = 3
lto = "thin"
codegen-units = 1
panic = "unwind"
# 超小二进制发布 profile(目标 3):size 优化 + 全 LTO + 去符号。
# panic 保持 unwind:连接线程 panic 只断当前连接(abort 会因单个畸形请求打死整个服务)
# 禁 unsafe 红线不变;brotli crate 仍限 woff2.rs
[profile.tiny]
inherits = "release"
opt-level = "z"
lto = "fat"
codegen-units = 1
strip = "symbols"
# 小体积但保速变体:opt=3(tiny 的 opt=z 让子集化慢 1.3 倍,速度优先)
[profile.tinyfast]
inherits = "tiny"
opt-level = 3

36
rust-spike/PORTING.md Normal file
View File

@ -0,0 +1,36 @@
# rust-spike 移植规范(所有 Zig→Rust 对译代理必读)
## 使命
把 `zig-spike/src/*.zig` 逐模块对译为 `rust-spike/src/*.rs`。zig-spike 是 TS 后端子集化引擎的**已验证镜像**(280 用例对拍 🟢),语义疑问一律以对应 .zig 源码为准,**不要去读 backend/ TS 源码重新推导**。
## 字节级等价是验收标准
相同输入下,Rust 版输出必须与 Zig 版逐字节相同(差异须能解释并经确认)。Zig 版已含全部修复(locaRange u16 溢出、P1 锚定注入、P4 hinting 剥离、P6 name 重建、P7 hmtx 折叠、cmap BMP format12 修复等)。
## 只移植自己的模块
- 每个代理只写自己负责的一个 .rs 文件(可在 rust-spike/src/ 下建同名子目录组织子模块,如 src/gsub/)
- 不得修改:zig-spike/、backend/、rust-spike/src/main.rs、src/http.rs、src/cache.rs、其他代理的模块
- 桩接口签名是跨模块契约,**不得更改**(需要新增能力时在最终报告里提出,由主会话协调)
- 禁止任何 git stash/reset/checkout/commit 操作
## Rust 编码规范(gs 全局规范)
- `///` doc 注释放 item 上方,内容从 Zig 版 `///` 直接翻译;`//` 普通注释同样翻译
- snake_case 命名,与 Zig 对应关系肉眼可查(collectGids→collect_gids)
- 禁 `unsafe`、禁外部 crate(**例外:woff2.rs 允许 brotli crate**,选型见下)
- 整数转换:无损用 `as`/`try_from`;有损截断必须与 Zig `@intCast`/`@truncate` 语义逐一对齐并加注释
- let it crash:不变量违反直接 panic(对齐 Zig),预期失败走 Result
- 迭代优于手写下标(字节解析游标类代码可保留下标游标,与 Zig 对译一致优先)
## 工程约定
- `cargo build --release` 必须零错误零警告;`cargo test --release` 全绿
- 死代码警告:桩阶段未接线的 pub 能力用 `#[allow(dead_code)] // 原因` 标注
- 并行代理共用同一 cargo 工程,cargo 有文件锁会自动排队,放心构建
## 验证层级(按你能做的做到最深处)
1. cargo 编译零警告 + 单元测试
2. 模块级对拍(能独立测就测:如 woff2 可拿 Zig 服务产的 ttf 做 fixture,与 Zig 产的 woff2 字节比)
3. 端到端对拍由主会话在 ttf.rs 落地后统一做(HTTP 双实例字节 cmp,工具 rust-spike/verify-parity.sh)
## Zig 模块语义备忘(前人确认,勿踩)
- gsub:collect_reachable 返回空 Vec ≠ 错误(版本不支持退 seed 序);keep 列表必须保持插入序不可排序;subsetGPOS 返回 null = 透传原始字节
- cff:subsetCFF 返回 null = 非 CID CFF 不支持时的降级路径(透传)
- woff2:Zig 版静态链系统 libbrotli-dev(C brotli);TS 生产同样 C brotli;brotli 质量/参数以 woff2.zig 实际调用为准

View File

@ -0,0 +1 @@
{"totalRequests":488,"subsetRequests":485,"subsetCacheHits":265,"totalChars":11097,"tempUploads":0,"offlineSubsets":0,"offlineDownloads":0}

160
rust-spike/src/cache.rs Normal file
View File

@ -0,0 +1,160 @@
//! 进程级缓存(对译 zig-spike/src/main.zig 的 parsed_cache + subset_cache)
//!
//! - 预解析字体缓存:永不失效,get-or-parse 串行在锁内
//! - 子集产物 LRU:按字节容量淘汰,语义对齐 TS subsetCache(LruCache evictByBytes)
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use crate::ttf::Font;
/// 子集缓存默认容量 10MB(SUBSET_CACHE_MAX_SIZE 可覆盖,语义对齐 TS)
pub const DEFAULT_SUBSET_CACHE_MAX_BYTES: usize = 10 * 1024 * 1024;
/// 预解析字体缓存:path → 解析好的 Font(只读共享,Arc 派发)
pub struct FontCache {
map: Mutex<HashMap<String, Arc<Font>>>,
}
impl FontCache {
#[allow(dead_code)] // /api/stats 接入前未用
pub fn new() -> Self {
FontCache { map: Mutex::new(HashMap::new()) }
}
/// 命中零 IO + 零解析;未命中在锁内读文件并解析(二次检查避免重复解析,对齐 Zig getFont)
pub fn get_or_parse(&self, path: &str) -> Result<Arc<Font>, FontCacheError> {
let mut map = self.map.lock().unwrap();
if let Some(f) = map.get(path) {
return Ok(f.clone());
}
let data = std::fs::read(path).map_err(|_| FontCacheError::NotFound)?;
if data.len() > 100 * 1024 * 1024 {
return Err(FontCacheError::NotFound);
}
let font = Arc::new(Font::parse(data).map_err(|_| FontCacheError::ParseFailed)?);
map.insert(path.to_string(), font);
Ok(map.get(path).unwrap().clone())
}
/// 条目数(/api/stats 的 fontBufferCacheEntries 同位字段,口径注释见 Zig 版)
#[allow(dead_code)] // /api/stats 接入前未用
pub fn len(&self) -> usize {
self.map.lock().unwrap().len()
}
}
#[derive(Debug)]
pub enum FontCacheError {
/// 文件缺失 / 超过 100MB 上限(对外同 404)
NotFound,
/// 解析失败(对外 422)
ParseFailed,
}
/// LRU(按字节容量):读命中移到最新端;插入超容量从最旧端淘汰。
/// 值存 `Arc<[u8]>`——命中返回 Arc 快照,淘汰不影响正在写出的响应
/// (语义等价 Zig 版「HIT 响应期间持锁」,锁粒度更小)。
/// 顺序表用 VecDeque(条目量级 ~200,O(n) 触碰移动可忽略),对齐 scLookup 的 unlink+append。
pub struct SubsetCache {
inner: Mutex<SubsetCacheInner>,
}
struct SubsetCacheInner {
map: HashMap<String, Arc<[u8]>>,
/// 键的访问序:front = 最旧,back = 最新
order: VecDeque<String>,
used_bytes: usize,
max_bytes: usize,
}
impl SubsetCache {
pub fn new(max_bytes: usize) -> Self {
SubsetCache {
inner: Mutex::new(SubsetCacheInner {
map: HashMap::new(),
order: VecDeque::new(),
used_bytes: 0,
max_bytes,
}),
}
}
/// 查询:命中把键移到最新端并返回 Arc 快照
pub fn get(&self, key: &str) -> Option<Arc<[u8]>> {
let mut g = self.inner.lock().unwrap();
if !g.map.contains_key(key) {
return None;
}
Self::touch(&mut g, key);
g.map.get(key).cloned()
}
/// 插入:覆盖同键旧值;超容量从最旧端淘汰
pub fn insert(&self, key: &str, bytes: Vec<u8>) {
let mut g = self.inner.lock().unwrap();
let bytes: Arc<[u8]> = bytes.into();
if let Some(old) = g.map.insert(key.to_string(), bytes) {
g.used_bytes -= old.len();
} else {
g.order.push_back(key.to_string());
}
g.used_bytes += g.map.get(key).unwrap().len();
while g.used_bytes > g.max_bytes {
let Some(oldest) = g.order.pop_front() else { break };
if let Some(v) = g.map.remove(&oldest) {
g.used_bytes -= v.len();
}
}
}
/// 把键移到最新端(命中语义)
fn touch(g: &mut SubsetCacheInner, key: &str) {
if let Some(pos) = g.order.iter().position(|k| k == key) {
g.order.remove(pos);
g.order.push_back(key.to_string());
}
}
#[allow(dead_code)] // /api/stats 接入前未用
/// 条目数(/api/stats 的 subsetCacheEntries)
pub fn len(&self) -> usize {
self.inner.lock().unwrap().map.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lru_evicts_by_bytes() {
let c = SubsetCache::new(100);
c.insert("a", vec![0u8; 60]);
c.insert("b", vec![0u8; 30]);
assert_eq!(c.len(), 2);
c.insert("c", vec![0u8; 20]); // 90 → 110 > 100,淘汰 a
assert_eq!(c.len(), 2);
assert!(c.get("a").is_none());
assert!(c.get("b").is_some() && c.get("c").is_some());
}
#[test]
fn lru_touch_prevents_eviction() {
let c = SubsetCache::new(100);
c.insert("a", vec![0u8; 60]);
c.insert("b", vec![0u8; 30]);
c.get("a"); // a 变最新,b 最旧
c.insert("c", vec![0u8; 20]); // 淘汰 b
assert!(c.get("a").is_some() && c.get("b").is_none());
}
#[test]
fn insert_over_same_key_replaces_bytes() {
let c = SubsetCache::new(100);
c.insert("k", vec![0u8; 80]);
c.insert("k", vec![0u8; 10]); // 覆盖,不新增顺序位
assert_eq!(c.len(), 1);
assert_eq!(c.get("k").unwrap().len(), 10);
}
}

1976
rust-spike/src/cff.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,460 @@
//! glyph-match 评分引擎:字形光栅化 + Chamfer 相似度(对译 zig-spike/src/glyph_match.zig,
//! 语义对齐 backend/font_util/glyph_bitmap.ts)
//!
//! 64x64 归一化网格:TrueType 二次曲线 4 段细分 → 非零环绕扫描线填充 →
//! 紧致 bbox 归一化(12% 留边);比对用 Chamfer 3-4 距离变换 + ±2px 平移搜索。
//! 全 f64 光栅化(与 TS number 同精度),距离场 f32(与 Float32Array 同精度)。
use crate::ttf::{self, GlyfPt};
/// 归一化网格边长(与前端 segmenter.GRID 一致)
pub const GRID: usize = 64;
/// 归一化留边比例(与前端一致:12%)
const MARGIN_RATIO: f64 = 0.12;
/// 二次曲线细分段数(64 网格精度足够)
const MID_STEPS: usize = 4;
/// 64x64 二值位图(0/1)
pub type BitmapT = [u8; GRID * GRID];
/// 位图距离场
pub type DtT = [f32; GRID * GRID];
/// 二维点(多边形顶点,y 已翻转)
#[derive(Clone, Copy)]
struct Pt {
x: f64,
y: f64,
}
/// 生成位图(含 Chamfer 相似度)所需查询字形
pub struct QueryGlyph {
/// 首个码点(TS codePointAt(0) 语义)
pub code_point: u32,
/// 64x64 二值位图(0/1)
pub bitmap: Box<BitmapT>,
/// 位图距离场(解析后算一次,全库扫描复用)
pub dt: Box<DtT>,
/// 查询序号(回传给前端对位)
pub query_index: usize,
}
/// 查询字形原始输入(JSON 解析后的中间形态)
pub struct RawGlyph<'a> {
/// UTF-8 char 字段
pub char: &'a [u8],
/// 4096 长度的 01 串
pub bitmap: &'a [u8],
/// char 首码点(解码失败为 None,条目被过滤)
pub code_point: Option<u32>,
}
/// 二次贝塞尔细分 MID_STEPS 段追加(不含起点,含终点)
fn quad(p0: Pt, pc: Pt, p1: Pt, poly: &mut Vec<Pt>) {
for i in 1..=MID_STEPS {
let t = i as f64 / MID_STEPS as f64;
let mt = 1.0 - t;
poly.push(Pt {
x: mt * mt * p0.x + 2.0 * mt * t * pc.x + t * t * p1.x,
y: mt * mt * p0.y + 2.0 * mt * t * pc.y + t * t * p1.y,
});
}
}
/// 中点
fn mid(a: Pt, b: Pt) -> Pt {
Pt { x: (a.x + b.x) / 2.0, y: (a.y + b.y) / 2.0 }
}
/// 单条轮廓扁平化为多边形顶点序列(y 翻转 + 隐含中点 + 二次曲线细分)
fn flatten_contour(contour: &[GlyfPt]) -> Vec<Pt> {
if contour.len() < 2 {
return Vec::new();
}
let n = contour.len();
// pts = y 翻转
let pts: Vec<Pt> = contour
.iter()
.map(|p| Pt { x: p.x as f64, y: -(p.y as f64) })
.collect();
// 先找 on-curve 起点
let start_idx = contour.iter().position(|p| p.on_curve);
let Some(si) = start_idx else {
// 全 off-curve(极罕见):首尾中点为隐含起点
let start_pt = mid(pts[n - 1], pts[0]);
let mut poly = Vec::new();
poly.push(start_pt);
for i in 0..n {
let cur = pts[i];
let next = pts[(i + 1) % n];
if contour[i].on_curve {
poly.push(cur);
} else {
let end = if contour[(i + 1) % n].on_curve { next } else { mid(cur, next) };
quad(*poly.last().unwrap(), cur, end, &mut poly);
}
}
return poly;
};
// 正常路径:从 on-curve 起点绕一圈
let mut poly = Vec::new();
poly.push(pts[si]);
let mut prev_on = pts[si];
for k in 1..=n {
let i = (si + k) % n;
let cur = pts[i];
if contour[i].on_curve {
poly.push(cur);
prev_on = cur;
} else {
let next_idx = (i + 1) % n;
let end = if contour[next_idx].on_curve { pts[next_idx] } else { mid(cur, pts[next_idx]) };
quad(prev_on, cur, end, &mut poly);
// off-curve 段结束后的隐含 on-curve 点也要成为下一段的起点
poly.push(end);
prev_on = end;
}
}
poly
}
/// 紧致 bbox 归一化多边形到 64x64 内框(12% 留边,与前端 maskToNormalizedRGBA 同参)
fn normalize_polys(polys: &[Vec<Pt>]) -> Vec<Vec<Pt>> {
let mut min_x = f64::INFINITY;
let mut min_y = f64::INFINITY;
let mut max_x = f64::NEG_INFINITY;
let mut max_y = f64::NEG_INFINITY;
for poly in polys {
for p in poly {
if p.x < min_x {
min_x = p.x;
}
if p.x > max_x {
max_x = p.x;
}
if p.y < min_y {
min_y = p.y;
}
if p.y > max_y {
max_y = p.y;
}
}
}
let w = max_x - min_x;
let h = max_y - min_y;
// 退化(空轮廓/纯水平线)保护
if !(w > 0.0) || !(h > 0.0) {
return Vec::new();
}
let margin = GRID as f64 * MARGIN_RATIO;
let inner = GRID as f64 - margin * 2.0;
let scale = (inner / w).min(inner / h);
let dw = w * scale;
let dh = h * scale;
let ox = (GRID as f64 - dw) / 2.0 - min_x * scale;
let oy = (GRID as f64 - dh) / 2.0 - min_y * scale;
polys
.iter()
.map(|poly| {
poly.iter()
.map(|p| Pt { x: p.x * scale + ox, y: p.y * scale + oy })
.collect()
})
.collect()
}
/// 交点(x 坐标 + 环绕方向)
struct Crossing {
x: f64,
winding: i32,
}
/// 扫描线按非零环绕规则填充多边形集合(必须合并全部轮廓的交点,保留封闭留白)
fn scanline_fill(polys: &[Vec<Pt>]) -> BitmapT {
let mut grid: BitmapT = [0; GRID * GRID];
let mut crossings: Vec<Crossing> = Vec::new();
for y in 0..GRID {
let sy = y as f64 + 0.5;
crossings.clear();
for poly in polys {
if poly.len() < 3 {
continue;
}
for (i, a) in poly.iter().enumerate() {
let b = poly[(i + 1) % poly.len()];
if a.y == b.y {
continue;
}
if (sy >= a.y && sy < b.y) || (sy >= b.y && sy < a.y) {
crossings.push(Crossing {
x: a.x + ((sy - a.y) / (b.y - a.y)) * (b.x - a.x),
winding: if a.y < b.y { 1 } else { -1 },
});
}
}
}
// 按 x 升序(稳定排序,对齐 Zig std.mem.sort)
crossings.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap_or(std::cmp::Ordering::Equal));
let mut winding: i32 = 0;
for i in 0..crossings.len().saturating_sub(1) {
winding += crossings[i].winding;
if winding == 0 {
continue;
}
// 对齐 Zig:x0 只向下钳 0、x1 只向上钳 GRID-1,越出网格的方向不钳
// (x0 > x1 时循环自然不执行),避免右边缘多填一格
let x0 = ((crossings[i].x - 0.5).ceil() as i64).max(0);
let x1 = ((crossings[i + 1].x - 0.5).floor() as i64).min(GRID as i64 - 1);
let mut x = x0;
while x <= x1 {
grid[y * GRID + x as usize] = 1;
x += 1;
}
}
}
grid
}
/// 距离变换(Chamfer 3-4 两遍扫描):每个 0 像素到最近 1 像素的距离
pub fn distance_transform(mask: &[u8], dt: &mut DtT) {
const INF: f32 = 1e9;
for i in 0..dt.len() {
dt[i] = if mask[i] != 0 { 0.0 } else { INF };
}
// 正向扫描(左上 → 右下)
for y in 0..GRID {
for x in 0..GRID {
let i = y * GRID + x;
let mut v = dt[i];
if x > 0 {
v = v.min(dt[i - 1] + 3.0);
}
if y > 0 {
v = v.min(dt[i - GRID] + 3.0);
}
if x > 0 && y > 0 {
v = v.min(dt[i - GRID - 1] + 4.0);
}
if x < GRID - 1 && y > 0 {
v = v.min(dt[i - GRID + 1] + 4.0);
}
dt[i] = v;
}
}
// 反向扫描(右下 → 左上)
let mut y = GRID - 1;
loop {
let mut x = GRID - 1;
loop {
let i = y * GRID + x;
let mut v = dt[i];
if x < GRID - 1 {
v = v.min(dt[i + 1] + 3.0);
}
if y < GRID - 1 {
v = v.min(dt[i + GRID] + 3.0);
}
if x < GRID - 1 && y < GRID - 1 {
v = v.min(dt[i + GRID + 1] + 4.0);
}
if x > 0 && y < GRID - 1 {
v = v.min(dt[i + GRID - 1] + 4.0);
}
dt[i] = v;
if x == 0 {
break;
}
x -= 1;
}
if y == 0 {
break;
}
y -= 1;
}
}
/// a 的笔画像素在 dt_b 距离场上的平均距离(越近越像);空掩码返回 -1
fn mean_distance(a: &[u8], dt_b: &[f32], dx: i64, dy: i64) -> f64 {
let mut sum: f64 = 0.0;
let mut count: u32 = 0;
for y in 0..GRID {
let sy = y as i64 + dy;
if sy < 0 || sy >= GRID as i64 {
continue;
}
for x in 0..GRID {
if a[y * GRID + x] == 0 {
continue;
}
let sx = x as i64 + dx;
if sx < 0 || sx >= GRID as i64 {
continue;
}
sum += dt_b[(sy * GRID as i64 + sx) as usize] as f64;
count += 1;
}
}
if count == 0 {
return -1.0;
}
sum / count as f64 / 3.0 // 除以 3 归一到像素单位
}
/// 容错字形相似度 —— 对称 Chamfer 距离 + ±2px 平移搜索,score = exp(-worst/σ),σ=4px
pub fn glyph_similarity(a: &[u8], b: &[u8], dt_a: &[f32], dt_b: &[f32]) -> f64 {
let mut worst = f64::INFINITY;
for dy in -2i64..=2 {
for dx in -2i64..=2 {
let d_ab = mean_distance(a, dt_b, dx, dy);
let d_ba = mean_distance(b, dt_a, -dx, -dy);
if d_ab < 0.0 || d_ba < 0.0 {
continue;
}
let m = d_ab.max(d_ba);
if m < worst {
worst = m;
}
}
}
if !worst.is_finite() {
return 0.0;
}
(-worst / 4.0).exp()
}
/// 从已解析字体取指定码点的归一化位图;缺字/空轮廓/全空位图返回 None(语义对齐 getGlyphBitmap)
pub fn get_glyph_bitmap(font: &ttf::Font, code_point: u32) -> Option<Box<BitmapT>> {
let gid = font.glyph_id(code_point)?;
let Ok(contours) = ttf::glyph_contours(font, gid, 0) else {
return None;
};
if contours.is_empty() {
return None;
}
let mut polys: Vec<Vec<Pt>> = Vec::new();
for c in &contours {
let poly = flatten_contour(c);
if poly.len() >= 3 {
polys.push(poly);
}
}
let normalized = normalize_polys(&polys);
if normalized.is_empty() {
return None;
}
let bitmap: Box<BitmapT> = Box::new(scanline_fill(&normalized));
// 全空位图视为缺字
if bitmap.iter().all(|&v| v == 0) {
return None;
}
Some(bitmap)
}
/// 解析查询字形列表:01 串 → 位图 + 距离场;非法位图/空 char 的条目被过滤(语义对齐 handler 过滤)
pub fn parse_query_glyphs(raw: &[RawGlyph<'_>]) -> Vec<QueryGlyph> {
let mut out: Vec<QueryGlyph> = Vec::new();
for (query_index, rg) in raw.iter().enumerate() {
if rg.bitmap.len() != GRID * GRID {
continue;
}
let mut bm: Box<BitmapT> = Box::new([0; GRID * GRID]);
let mut valid = true;
for (i, &c) in rg.bitmap.iter().enumerate() {
if c != b'0' && c != b'1' {
valid = false;
break;
}
bm[i] = c - b'0';
}
if !valid {
continue;
}
let Some(code_point) = rg.code_point else { continue };
let mut dt: Box<DtT> = Box::new([0.0; GRID * GRID]);
distance_transform(&bm[..], &mut dt);
out.push(QueryGlyph { code_point, bitmap: bm, dt, query_index });
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn distance_transform_zero_mask_is_all_inf() {
let mask = [0u8; GRID * GRID];
let mut dt: DtT = [0.0; GRID * GRID];
distance_transform(&mask, &mut dt);
assert!(dt.iter().all(|&v| v == 1e9));
}
#[test]
fn distance_transform_center_dot() {
let mut mask = [0u8; GRID * GRID];
mask[32 * GRID + 32] = 1;
let mut dt: DtT = [0.0; GRID * GRID];
distance_transform(&mask, &mut dt);
assert_eq!(dt[32 * GRID + 32], 0.0);
// 相邻 0 像素距离 3(Chamfer 3-4)
assert_eq!(dt[32 * GRID + 31], 3.0);
// 对角像素距离 4
assert_eq!(dt[31 * GRID + 31], 4.0);
}
#[test]
fn similarity_self_is_one() {
let mut mask = [0u8; GRID * GRID];
for y in 20..44 {
for x in 20..44 {
mask[y * GRID + x] = 1;
}
}
let mut dt: DtT = [0.0; GRID * GRID];
distance_transform(&mask, &mut dt);
let s = glyph_similarity(&mask, &mask, &dt, &dt);
assert!((s - 1.0).abs() < 1e-12);
}
#[test]
fn empty_similarity_is_zero() {
let mask = [0u8; GRID * GRID];
let mut dt: DtT = [0.0; GRID * GRID];
distance_transform(&mask, &mut dt);
assert_eq!(glyph_similarity(&mask, &mask, &dt, &dt), 0.0);
}
#[test]
fn parse_query_glyphs_filters_invalid() {
let raw = [
RawGlyph { char: b"a", bitmap: &[b'0'; 4096], code_point: Some(97) },
RawGlyph { char: b"b", bitmap: &[b'0'; 100], code_point: Some(98) }, // 长度不足
RawGlyph { char: b"c", bitmap: &[b'2'; 4096], code_point: Some(99) }, // 非法字符
RawGlyph { char: b"d", bitmap: &[b'1'; 4096], code_point: None }, // 无码点
];
let out = parse_query_glyphs(&raw);
assert_eq!(out.len(), 1);
assert_eq!(out[0].query_index, 0);
assert_eq!(out[0].code_point, 97);
}
#[test]
fn scanline_fill_rectangle() {
// 一个填满 [10,54)x[10,54) 的正方形(顶点在整数网格上,扫描线 y+0.5 命中内部)
let poly = vec![
Pt { x: 10.0, y: 10.0 },
Pt { x: 54.0, y: 10.0 },
Pt { x: 54.0, y: 54.0 },
Pt { x: 10.0, y: 54.0 },
];
let grid = scanline_fill(&[poly]);
assert_eq!(grid[30 * GRID + 30], 1);
// 交点恰在 x=10:x0=ceil(10-0.5)=10,右界 x1=floor(54-0.5)=53(不含 54)
assert_eq!(grid[10 * GRID + 10], 1);
assert_eq!(grid[10 * GRID + 54], 0);
assert_eq!(grid[9 * GRID + 30], 0); // y<10 行未命中
assert_eq!(grid[54 * GRID + 30], 0); // y>=54 行未命中
}
}

3214
rust-spike/src/gsub.rs Normal file

File diff suppressed because it is too large Load Diff

193
rust-spike/src/http.rs Normal file
View File

@ -0,0 +1,193 @@
//! HTTP 响应辅助(对译 zig-spike/src/main.zig 的 sendText/sendFont/sendJson/writeAll/urlDecode)
//! CORS 三头追加语义对齐 backend/app.ts corsMiddleware(OPTIONS 204 例外不带 CORS 头)
use std::io::{self, IoSlice, Write};
use std::net::TcpStream;
/// CORS 三头(顺序对齐 Zig 版,逐头追加)
const CORS: [&str; 3] = [
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers: Content-Type, Authorization",
];
/// 连接关闭头
fn conn_header(close: bool) -> &'static str {
if close { "close" } else { "keep-alive" }
}
/// iovec 式写出:write_vectored 单次系统调用(对齐 Zig writeAll 单次 net_write 的意图);
/// 各段零拷贝(IoSlice 借用),部分写时循环补发剩余区间
pub fn write_all(stream: &mut TcpStream, parts: &[&[u8]]) -> io::Result<()> {
let mut slices: Vec<IoSlice> = parts.iter().map(|p| IoSlice::new(p)).collect();
let mut total: usize = slices.iter().map(|s| s.len()).sum();
while total > 0 {
let n = stream.write_vectored(&slices)?;
if n == 0 {
return Err(io::Error::new(io::ErrorKind::WriteZero, "write_vectored 写入 0 字节"));
}
total -= n;
let mut rem = n;
for s in slices.iter_mut() {
let len = s.len();
if rem >= len {
rem -= len;
} else {
s.advance(rem);
break;
}
}
slices.retain(|s| !s.is_empty());
}
Ok(())
}
/// FNV-1a 64 字节哈希 → 强 ETag("\"16hex\"")
pub fn etag_of(bytes: &[u8]) -> String {
let mut h: u64 = 0xcbf29ce484222325;
for &b in bytes {
h ^= b as u64;
h = h.wrapping_mul(0x100000001b3);
}
format!("\"{h:016x}\"")
}
/// 纯文本响应(extra 可挂额外头如 Retry-After)
pub fn send_text(
stream: &mut TcpStream,
code: u16,
body: &str,
close: bool,
extra: &[&str],
) -> io::Result<()> {
let mut head = String::with_capacity(256 + body.len());
head.push_str(&format!(
"HTTP/1.1 {code} X\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\n",
body.len()
));
for h in extra {
head.push_str(h);
head.push_str("\r\n");
}
for h in CORS {
head.push_str(h);
head.push_str("\r\n");
}
head.push_str(&format!("Connection: {}\r\n\r\n", conn_header(close)));
write_all(stream, &[head.as_bytes(), body.as_bytes()])
}
#[allow(dead_code)] // R5' 路由接入前未用
/// JSON 错误响应(格式对齐 TS jsonResponse:application/json)
pub fn send_json(stream: &mut TcpStream, code: u16, body: &str, close: bool) -> io::Result<()> {
let head = format!(
"HTTP/1.1 {code} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\n",
body.len()
);
let mut head = head;
for h in CORS {
head.push_str(h);
head.push_str("\r\n");
}
head.push_str(&format!("Connection: {}\r\n\r\n", conn_header(close)));
write_all(stream, &[head.as_bytes(), body.as_bytes()])
}
/// 字体成功响应(强缓存 max-age=86400 + ETag 兜底;命中 If-None-Match 回 304 零字节)
/// 背景:字体子集内容不变,24h 强缓存使浏览器零回源;服务端字节变化靠 ETag 在协商时自愈
pub fn send_font(
stream: &mut TcpStream,
bytes: &[u8],
close: bool,
content_type: &str,
extra: &[&str],
timing: &[&str],
etag: &str,
if_none_match: Option<&str>,
) -> io::Result<()> {
// 弱 ETag 匹配(W/"x" 与 "x" 都认)
let matched = if_none_match.is_some_and(|v| {
v.split(',').any(|t| {
let t = t.trim();
t == etag
|| t == format!("W/{etag}")
|| (t.starts_with("W/") && &t[2..] == etag)
})
});
if matched {
let head = format!(
"HTTP/1.1 304 Not Modified\r\nContent-Length: 0\r\nCache-Control: public, max-age=86400\r\nETag: {etag}\r\n"
);
let mut head = head;
for h in timing.iter() {
head.push_str(h);
head.push_str("\r\n");
}
head.push_str(&format!("Connection: {}\r\n\r\n", conn_header(close)));
return write_all(stream, &[head.as_bytes()]);
}
let mut head = format!(
"HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nCache-Control: public, max-age=86400\r\nETag: {etag}\r\n",
bytes.len()
);
for h in extra.iter().chain(timing.iter()) {
head.push_str(h);
head.push_str("\r\n");
}
for h in CORS {
head.push_str(h);
head.push_str("\r\n");
}
head.push_str(&format!("Connection: {}\r\n\r\n", conn_header(close)));
write_all(stream, &[head.as_bytes(), bytes])
}
/// OPTIONS 预检 204(corsMiddleware 分支:无 CORS 头)
pub fn send_options(stream: &mut TcpStream, close: bool) -> io::Result<()> {
let head = format!(
"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: {}\r\n\r\n",
conn_header(close)
);
write_all(stream, &[head.as_bytes()])
}
/// URL 解码(对译 Zig urlDecode:%XX 十六进制 + '+'→空格;非法序列原样保留)
pub fn url_decode(s: &str) -> Vec<u8> {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
while i < b.len() {
if b[i] == b'%' && i + 2 < b.len() {
let hi = (b[i + 1] as char).to_digit(16);
let lo = (b[i + 2] as char).to_digit(16);
match (hi, lo) {
(Some(h), Some(l)) => {
out.push((h * 16 + l) as u8);
i += 3;
}
_ => {
out.push(b[i]);
i += 1;
}
}
} else if b[i] == b'+' { out.push(b' ');
i += 1;
} else {
out.push(b[i]);
i += 1;
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_decode_basic() {
assert_eq!(url_decode("%E5%AD%97%E4%BD%93"), "字体".as_bytes());
assert_eq!(url_decode("a+b"), b"a b");
assert_eq!(url_decode("100%"), b"100%"); // 截断的 % 原样保留
}
}

650
rust-spike/src/main.rs Normal file
View File

@ -0,0 +1,650 @@
//! webfont-rs spike:HTTP 服务 + TTF 子集化(对译 zig-spike/src/main.zig)
//! 用法:PORT=8090 FONT_DIR=../font ./webfont-rs
//! webfont-rs bench <font> <text> [iters] (纯计算微基准,输出 checksum 防 DCE)
//!
//! 语义对齐 backend/routes/subset.ts(缓存/响应头/错误码),以 zig-spike 版为第二 oracle。
mod cache;
mod cff;
mod glyph_match;
mod gsub;
mod http;
mod misc;
mod ttf;
mod woff2;
use std::io::Read;
use std::net::TcpListener;
use std::time::Instant;
use cache::{FontCache, SubsetCache, DEFAULT_SUBSET_CACHE_MAX_BYTES};
/// 字体目录列表(启动时确定):FONT_DIRS(冒号分隔)> FONT_DIR(单目录)> TS 默认三目录
const DEFAULT_FONT_DIRS: [&str; 3] = ["font/admin", "font", "font/temp"];
/// 共享服务状态
struct App {
font_dirs: Vec<String>,
fonts: FontCache,
subset_cache: SubsetCache,
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
match args[1].as_str() {
"bench" => return run_bench(&args[2..]),
"alloc-stats" => return run_alloc_stats(&args[2..]),
other => {
eprintln!("未知子命令: {other}(可用: bench, alloc-stats)");
std::process::exit(2);
}
}
}
run_server();
}
/// 计数分配器:wrap System 累加分配/释放次数与字节(alloc-stats 诊断用)
struct CountingAlloc;
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
static ALLOC_N: AtomicUsize = AtomicUsize::new(0);
static ALLOC_B: AtomicUsize = AtomicUsize::new(0);
static REALLOC_N: AtomicUsize = AtomicUsize::new(0);
static REALLOC_B: AtomicUsize = AtomicUsize::new(0);
static DEALLOC_N: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for CountingAlloc {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
ALLOC_N.fetch_add(1, Ordering::Relaxed);
ALLOC_B.fetch_add(l.size(), Ordering::Relaxed);
unsafe { System.alloc(l) }
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
DEALLOC_N.fetch_add(1, Ordering::Relaxed);
unsafe { System.dealloc(p, l) }
}
unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
REALLOC_N.fetch_add(1, Ordering::Relaxed);
REALLOC_B.fetch_add(n.saturating_sub(l.size()), Ordering::Relaxed);
unsafe { System.realloc(p, l, n) }
}
unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 {
ALLOC_N.fetch_add(1, Ordering::Relaxed);
ALLOC_B.fetch_add(l.size(), Ordering::Relaxed);
unsafe { System.alloc_zeroed(l) }
}
}
#[global_allocator]
static GLOBAL_ALLOC: CountingAlloc = CountingAlloc;
/// 诊断子命令:统计稳态单次 subset 的分配次数与字节量(性能优化期工具)
fn run_alloc_stats(args: &[String]) {
let (font_path, text, iters) = match args {
[f, t] => (f.as_str(), t.as_str(), 100usize),
[f, t, i] => (f.as_str(), t.as_str(), i.parse().unwrap_or(100)),
_ => {
eprintln!("用法: webfont-rs alloc-stats <font> <text> [iters]");
std::process::exit(2);
}
};
let data = std::fs::read(font_path).expect("read font");
let font = ttf::Font::parse(data).expect("parse");
for _ in 0..20 {
let _ = ttf::subset(&font, text).expect("subset");
}
ALLOC_N.store(0, Ordering::Relaxed);
ALLOC_B.store(0, Ordering::Relaxed);
REALLOC_N.store(0, Ordering::Relaxed);
REALLOC_B.store(0, Ordering::Relaxed);
DEALLOC_N.store(0, Ordering::Relaxed);
for _ in 0..iters {
let _ = ttf::subset(&font, text).expect("subset");
}
let it = iters as f64;
eprintln!(
"alloc: {:.1} 次/迭代, {:.0}B/迭代 | realloc: {:.1} 次/迭代, 搬移+{:.0}B/迭代 | dealloc: {:.1} 次/迭代",
ALLOC_N.load(Ordering::Relaxed) as f64 / it,
ALLOC_B.load(Ordering::Relaxed) as f64 / it,
REALLOC_N.load(Ordering::Relaxed) as f64 / it,
REALLOC_B.load(Ordering::Relaxed) as f64 / it,
DEALLOC_N.load(Ordering::Relaxed) as f64 / it,
);
}
fn run_server() {
let port: u16 = std::env::var("PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(8090);
let font_dirs = resolve_font_dirs();
let subset_max: usize = std::env::var("SUBSET_CACHE_MAX_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(DEFAULT_SUBSET_CACHE_MAX_BYTES);
let app = std::sync::Arc::new(App {
font_dirs,
fonts: FontCache::new(),
subset_cache: SubsetCache::new(subset_max),
});
// misc 初始化(配置解析/统计恢复/目录创建/后台线程,对齐 zig misc.init 在 listen 之前)
misc::init();
let listener = TcpListener::bind(("0.0.0.0", port)).expect("bind");
let dirs = app.font_dirs.join(":");
eprintln!("webfont-rs listening on {port}, font_dirs={dirs}");
for stream in listener.incoming() {
let Ok(stream) = stream else { continue };
let app = app.clone();
// 每连接一线程:keep-alive 长连接不阻塞其他客户端(对齐 Zig connThread 模型)
std::thread::spawn(move || {
let _ = conn_loop(&app, stream);
});
}
}
/// 解析字体目录配置(优先级:FONT_DIRS > FONT_DIR > 默认三目录)
fn resolve_font_dirs() -> Vec<String> {
if let Ok(s) = std::env::var("FONT_DIRS") {
let list: Vec<String> = s.split(':').filter(|d| !d.is_empty()).map(String::from).collect();
if !list.is_empty() {
return list;
}
}
if let Ok(d) = std::env::var("FONT_DIR") {
return vec![d];
}
DEFAULT_FONT_DIRS.iter().map(|s| s.to_string()).collect()
}
/// 请求行 + 头解析结果
struct Request<'a> {
method: &'a str,
path: &'a str,
query: &'a str,
body: &'a [u8],
close: bool,
/// If-None-Match 请求头(协商缓存 304 判定)
if_none_match: Option<String>,
}
/// 连接线程主体:keep-alive 循环,按 Content-Length 完整取帧后处理(对译 connThread/extractFrame)
fn conn_loop(app: &App, mut stream: std::net::TcpStream) -> std::io::Result<()> {
let mut buf: Vec<u8> = Vec::with_capacity(16 * 1024);
let mut rbuf = [0u8; 16 * 1024];
loop {
if let Some(total) = extract_frame(&buf) {
let req_bytes = &buf[..total];
let close = process_request(app, &mut stream, req_bytes)?;
// 移除已消费字节(剩余是下一请求的开头)
buf.drain(..total);
if close {
return Ok(());
}
continue;
}
if buf.len() > 1024 * 1024 {
let _ = http::send_text(&mut stream, 400, "request too large", true, &[]);
return Ok(());
}
let n = stream.read(&mut rbuf)?;
if n == 0 {
return Ok(()); // 客户端关闭
}
buf.extend_from_slice(&rbuf[..n]);
}
}
/// 判定缓冲里是否有完整请求帧(头结束 + Content-Length 全 body);无则 None(继续等读)
fn extract_frame(req: &[u8]) -> Option<usize> {
let hdr_end = find(req, b"\r\n\r\n")?;
let head = &req[..hdr_end];
let mut content_length = 0usize;
for h in head.split(|&b| b == b'\n') {
let h = trim_cr(h);
let Some(colon) = h.iter().position(|&b| b == b':') else { continue };
let name = &h[..colon];
if !name.eq_ignore_ascii_case(b"content-length") {
continue;
}
let mut v = &h[colon + 1..];
while let Some(&f) = v.first() {
if f == b' ' || f == b'\t' { v = &v[1..]; } else { break; }
}
content_length = std::str::from_utf8(v).ok()?.trim().parse().unwrap_or(0);
break;
}
let total = hdr_end + 4 + content_length;
if req.len() < total {
return None;
}
Some(total)
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|w| w == needle)
}
fn trim_cr(h: &[u8]) -> &[u8] {
match h.last() {
Some(&b'\r') => &h[..h.len() - 1],
_ => h,
}
}
/// 解析请求行 + 头(对译 parseRequest + headerWantsClose)
fn parse_request(req: &[u8]) -> Option<Request<'_>> {
let line_end = find(req, b"\r\n")?;
let line = &req[..line_end];
let sp1 = line.iter().position(|&b| b == b' ')?;
let method = &line[..sp1];
let rest = &line[sp1 + 1..];
let rest = &rest[rest.iter().position(|&b| b != b' ').unwrap_or(rest.len())..];
let sp2 = rest.iter().rposition(|&b| b == b' ')?;
let target = &rest[..sp2];
let qpos = target.iter().position(|&b| b == b'?');
let (path, query) = match qpos {
Some(q) => (&target[..q], &target[q + 1..]),
None => (target, &target[target.len()..]),
};
let close = header_wants_close(req);
let hdr_end = find(req, b"\r\n\r\n")?;
let if_none_match = header_value(req, b"if-none-match")
.and_then(|v| String::from_utf8(v.to_vec()).ok());
Some(Request {
if_none_match,
method: std::str::from_utf8(method).ok()?,
path: std::str::from_utf8(path).ok()?,
query: std::str::from_utf8(query).ok()?,
body: &req[hdr_end + 4..],
close,
})
}
/// 扫描请求头取指定头的值(大小写不敏感;找不到返 None)
fn header_value<'a>(req: &'a [u8], name: &[u8]) -> Option<&'a [u8]> {
for h in req.split(|&b| b == b'\n') {
let h = trim_cr(h);
if h.is_empty() {
break;
}
let Some(colon) = h.iter().position(|&b| b == b':') else { continue };
if !h[..colon].eq_ignore_ascii_case(name) {
continue;
}
let mut v = &h[colon + 1..];
while let Some(&f) = v.first() {
if f == b' ' || f == b'\t' { v = &v[1..]; } else { break; }
}
return Some(v);
}
None
}
/// 扫描请求头是否 Connection: close
fn header_wants_close(req: &[u8]) -> bool {
for h in req.split(|&b| b == b'\n') {
let h = trim_cr(h);
if h.is_empty() {
break; // 空行 = 头结束
}
let Some(colon) = h.iter().position(|&b| b == b':') else { continue };
if !h[..colon].eq_ignore_ascii_case(b"connection") {
continue;
}
let mut v = &h[colon + 1..];
while let Some(&f) = v.first() {
if f == b' ' || f == b'\t' { v = &v[1..]; } else { break; }
}
return v.eq_ignore_ascii_case(b"close");
}
false
}
/// 处理单个请求,返回 true 表示应关闭连接(对译 processRequest + routeRequest)
fn process_request(app: &App, stream: &mut std::net::TcpStream, req_bytes: &[u8]) -> std::io::Result<bool> {
let Some(r) = parse_request(req_bytes) else {
http::send_text(stream, 400, "bad request", true, &[])?;
return Ok(true);
};
let close = r.close;
let t0 = Instant::now();
// 日志中间件 + 总请求计数(对齐 logMiddleware:响应后打印 [Nms] METHOD path)
misc::stats_total_request();
let out_close = route_request(app, stream, req_bytes, &r, close)?;
// 日志中间件(对齐 logMiddleware:响应后打印 [Nms] METHOD path)
eprintln!("[{}ms] {} {}", t0.elapsed().as_millis(), r.method, r.path);
Ok(out_close)
}
/// 统一路由表(对齐 zig routeRequest:app.ts fontApiMiddleware + 静态资源中间件的兜底顺序)
fn route_request(
app: &App,
stream: &mut std::net::TcpStream,
req_bytes: &[u8],
r: &Request<'_>,
close: bool,
) -> std::io::Result<bool> {
// CORS 预检(corsMiddleware 的 OPTIONS 分支:204 + Content-Length: 0,无 CORS 头)
if r.method == "OPTIONS" {
http::send_options(stream, close)?;
return Ok(close);
}
let is_get = r.method == "GET";
let is_post = r.method == "POST";
if r.path == "/api/fonts" && is_get {
return misc::handle_list_fonts(stream, &app.font_dirs, close);
}
if r.path == "/api/config" && is_get {
return misc::handle_get_config(stream, close);
}
if r.path == "/api/stats" && is_get {
return misc::handle_stats(stream, close, app.subset_cache.len(), app.fonts.len());
}
if r.path == "/api/stats/event" && is_post {
return misc::handle_stats_event(stream, r.body, close);
}
if r.path == "/api/upload" && is_post {
return misc::handle_upload(stream, req_bytes, r.query, r.body, close);
}
if r.path == "/api/font-meta" && is_get {
return misc::handle_font_meta(stream, &app.font_dirs, r.query, close);
}
if r.path == "/api" && is_get {
return handle_subset(app, stream, r.query, close, r.if_none_match.as_deref());
}
if r.path == "/api/glyph-match" && is_post {
return misc::handle_glyph_match(stream, &app.font_dirs, &app.fonts, r.body, close);
}
// 未匹配路由 → 静态资源中间件(GET 走 dist/SSR/SPA fallback,非 GET 405)
misc::handle_static(stream, &app.font_dirs, r.method, r.path, close)
}
/// GET /api?font=..&text=..&outType=.. —— 字体裁剪(对译 handleSubset,语义对齐 backend/routes/subset.ts)
fn handle_subset(
app: &App,
stream: &mut std::net::TcpStream,
query: &str,
close: bool,
if_none_match: Option<&str>,
) -> std::io::Result<bool> {
let t0 = Instant::now();
let mut font_name: Vec<u8> = Vec::new();
let mut text: Vec<u8> = Vec::new();
let mut out_woff2 = false;
for kv in query.split('&') {
let Some(eq) = kv.find('=') else { continue };
let (k, v) = (&kv[..eq], &kv[eq + 1..]);
match k {
"font" => font_name = http::url_decode(v),
"text" => text = http::url_decode(v),
"outType" if v == "woff2" => out_woff2 = true,
_ => {}
}
}
let Ok(font_name) = std::str::from_utf8(&font_name) else {
http::send_text(stream, 400, "bad font", close, &[])?;
return Ok(close);
};
// TS 语义:text 为空时不响应(框架原样透传 res,连接挂起);Rust 收线等价安全
if text.is_empty() {
return Ok(true);
}
// 路径穿越防护(Zig 额外加固;TS 无此拦截)
if font_name.contains("..") {
http::send_text(stream, 400, "bad font", close, &[])?;
return Ok(close);
}
let Ok(text) = std::str::from_utf8(&text) else {
http::send_text(stream, 400, "bad text", close, &[])?;
return Ok(close);
};
let Some(font_path) = find_font_path(&app.font_dirs, font_name) else {
let msg = format!("Font not found: {font_name}");
http::send_text(stream, 404, &msg, close, &[])?;
return Ok(close);
};
let t1 = Instant::now();
// 记录字体被使用(临时字体保留机制)+ 子集化计数(对齐 subset.ts 顺序)
misc::mark_font_used(&font_path);
misc::stats_record_subset(misc::utf16_len_utf8(text));
// 查询裁剪结果缓存(key 对齐 TS:fontPath:outType:text)
let cache_key = format!(
"{font_path}:{}:{text}",
if out_woff2 { "woff2" } else { "ttf" }
);
if let Some(bytes) = app.subset_cache.get(&cache_key) {
misc::stats_record_cache_hit();
let t_hit = Instant::now();
let timing = [format!("X-Timing-Total: {}", ms(t0, t_hit))];
let timing: Vec<&str> = timing.iter().map(|s| s.as_str()).collect();
let etag = http::etag_of(&bytes);
http::send_font(
stream,
&bytes,
close,
if out_woff2 { "font/woff2" } else { "font/ttf" },
&["X-Cache: HIT"],
&timing,
&etag,
if_none_match,
)?;
return Ok(close);
}
// 内存闸门:完整字体读取和裁剪都在闸门内;缓存命中不占槽位(对齐 subset.ts)
let est = misc::estimate_subset_memory(&font_path);
if !misc::gate_acquire(misc::queue_timeout_ms(), est) {
http::send_text(stream, 503, "Server busy, please retry", close, &["Retry-After: 1"])?;
return Ok(close);
}
let font = match app.fonts.get_or_parse(&font_path) {
Ok(f) => f,
Err(cache::FontCacheError::NotFound) => {
misc::gate_release(est);
http::send_text(stream, 404, "font not found", close, &[])?;
return Ok(close);
}
Err(cache::FontCacheError::ParseFailed) => {
misc::gate_release(est);
http::send_text(stream, 422, "font parse failed", close, &[])?;
return Ok(close);
}
};
let t2 = Instant::now();
let result = match ttf::subset(&font, text) {
Ok(b) => b,
Err(ttf::SubsetError::NotTtf) => {
misc::gate_release(est);
http::send_text(stream, 422, "not a ttf font", close, &[])?;
return Ok(close);
}
Err(ttf::SubsetError::SubsetFailed) => {
misc::gate_release(est);
http::send_text(stream, 422, "subset failed", close, &[])?;
return Ok(close);
}
};
// woff2 编码(outType=woff2 时)。Zig 版 `try` 上抛后 defer 释放闸门再静默收线
let out_bytes: Vec<u8> = if out_woff2 {
match woff2::encode_ttf_to_woff2(&result) {
Ok(b) => b,
Err(_) => {
misc::gate_release(est);
return Ok(true);
}
}
} else {
result
};
let t3 = Instant::now();
misc::gate_release(est);
app.subset_cache.insert(&cache_key, out_bytes.to_vec());
let timing = [
format!("X-Timing-Find: {}", ms(t0, t1)),
format!("X-Timing-Read: {}", ms(t1, t2)),
format!("X-Timing-Subset: {}", ms(t2, t3)),
format!("X-Timing-Total: {}", ms(t0, t3)),
];
let timing: Vec<&str> = timing.iter().map(|s| s.as_str()).collect();
let etag = http::etag_of(&out_bytes);
http::send_font(
stream,
&out_bytes,
close,
if out_woff2 { "font/woff2" } else { "font/ttf" },
&["X-Cache: MISS"],
&timing,
&etag,
if_none_match,
)?;
Ok(close)
}
/// 毫秒耗时(X-Timing-* 头)
fn ms(from: Instant, to: Instant) -> u128 {
to.duration_since(from).as_millis()
}
/// 在所有字体目录中查找字体文件(对译 findFontPath:精确 > 前缀 > 包含,大小写不敏感,扩展名剥除比较)
pub(crate) fn find_font_path(dirs: &[String], name: &str) -> Option<String> {
// 精确匹配
for dir in dirs {
let p = format!("{dir}/{name}");
if let Ok(md) = std::fs::metadata(&p) {
if md.is_file() {
return Some(p);
}
}
}
// 扫描目录收集(basename 剥扩展名 + 小写)
let mut all: Vec<(String, String)> = Vec::new(); // (stem 小写, path)
for dir in dirs {
let Ok(entries) = std::fs::read_dir(dir) else { continue };
for entry in entries.flatten() {
// TS readdir 逐项 stat(跟随符号链接):必须用 std::fs::metadata(stat 语义),
// DirEntry::metadata 是 lstat——symlink 字体(如 /tmp/ssim-fonts 的基准链接)会被误跳过
let Ok(md) = std::fs::metadata(entry.path()) else { continue };
if !md.is_file() {
continue;
}
let fname = entry.file_name();
let Some(fname) = fname.to_str() else { continue };
if !has_font_ext(fname) {
continue;
}
all.push((strip_ext(fname).to_lowercase(), format!("{dir}/{fname}")));
}
}
let query = strip_ext(name).to_lowercase();
// 前缀匹配
for (stem, path) in &all {
if stem.starts_with(&query) {
return Some(path.clone());
}
}
// 包含匹配
for (stem, path) in &all {
if stem.contains(&query) {
return Some(path.clone());
}
}
None
}
/// 剥最后一个扩展名(语义对齐 JS name.replace(/\.[^.]+$/, ""))
fn strip_ext(name: &str) -> &str {
match name.rfind('.') {
Some(dot) => &name[..dot],
None => name,
}
}
/// 字体扩展名判定(大小写不敏感,语义对齐 /\.(ttf|otf|woff|woff2)$/i)
pub(crate) fn has_font_ext(name: &str) -> bool {
let lower = name.to_lowercase();
[".ttf", ".otf", ".woff", ".woff2"].iter().any(|e| lower.len() > e.len() && lower.ends_with(e))
}
/// 纯计算微基准:parse 一次,循环 subset 计 avg/min,累积 checksum 防编译器死代码消除
/// (对译 runBench;防 DCE 规则见 memory benchmark-dce-checksum-rule)
fn run_bench(args: &[String]) {
let (font_path, text, iters) = match args {
[f, t] => (f.as_str(), t.as_str(), 500usize),
[f, t, i] => (f.as_str(), t.as_str(), i.parse().unwrap_or(500)),
_ => {
eprintln!("用法: webfont-rs bench <font> <text> [iters]");
std::process::exit(2);
}
};
let data = std::fs::read(font_path).expect("read font");
let t0 = Instant::now();
let font = ttf::Font::parse(data).expect("parse");
eprintln!("Font.parse: {}ms", t0.elapsed().as_millis());
let mut checksum: u64 = 0;
let mut total_us: u128 = 0;
let mut min_us: u128 = u128::MAX;
// 预热 20 次(页缓存/JIT 无但 allocator/缓存热身),计入 checksum
for _ in 0..20 {
let r = ttf::subset(&font, text).expect("subset");
checksum = checksum.wrapping_add(r.len() as u64 + r[0] as u64 + r[r.len() - 1] as u64);
}
for _ in 0..iters {
let t0 = Instant::now();
let r = ttf::subset(&font, text).expect("subset");
let d = t0.elapsed();
total_us += d.as_micros();
min_us = min_us.min(d.as_micros());
checksum = checksum.wrapping_add(r.len() as u64 + r[0] as u64 + r[r.len() - 1] as u64);
}
eprintln!(
"iters={iters} checksum=0x{checksum:x} avg={:.3}ms min={:.3}ms",
total_us as f64 / iters as f64 / 1000.0,
min_us as f64 / 1000.0,
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extract_frame_partial_and_complete() {
let full = b"GET / HTTP/1.1\r\nHost: x\r\nContent-Length: 2\r\n\r\nhi";
assert_eq!(extract_frame(full), Some(full.len()));
assert_eq!(extract_frame(&full[..full.len() - 1]), None); // body 未齐
assert_eq!(extract_frame(b"GET / HTTP/1.1\r\n"), None); // 头未结束
}
#[test]
fn parse_request_fields() {
let req = b"GET /api?font=a.ttf&text=%E5%AD%97 HTTP/1.1\r\nConnection: close\r\n\r\nbody";
let r = parse_request(req).unwrap();
assert_eq!(r.method, "GET");
assert_eq!(r.path, "/api");
assert_eq!(r.query, "font=a.ttf&text=%E5%AD%97");
assert_eq!(r.body, b"body");
assert!(r.close);
}
#[test]
fn font_ext_case_insensitive() {
assert!(has_font_ext("A.TTF"));
assert!(has_font_ext("x.woff2"));
assert!(!has_font_ext("x.ttf.txt"));
assert!(!has_font_ext(".ttf")); // 与 Zig 一致:len > e.len 才算
}
}

2595
rust-spike/src/misc.rs Normal file

File diff suppressed because it is too large Load Diff

2173
rust-spike/src/ttf.rs Normal file

File diff suppressed because it is too large Load Diff

1025
rust-spike/src/woff2.rs Normal file

File diff suppressed because it is too large Load Diff

55
rust-spike/verify-parity.sh Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env bash
# rust↔zig 双实例字节对拍(Rust 迁移各轮复用的验收工具)
# 用法: ./verify-parity.sh <zig端口> <rust端口> [outType]
# 对每个字体 × 随机文本:两侧请求落盘 cmp,全部一致则 PASS
set -euo pipefail
cd "$(dirname "$0")/.."
ZIG_PORT=${1:-8090}
RS_PORT=${2:-8094}
OUT_TYPE=${3:-ttf}
FONTS=(
"鸿蒙黑体.ttf"
"令东齐伋复刻体.ttf"
"悠哉字体.ttf"
"方舟像素字体.ttf"
"思源黑体.ttf"
)
urlencode() { python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "$1"; }
rand_text() {
# 每次生成不同长度的随机文本(千字文常用字池),防 LRU 缓存命中
python3 - "$1" <<'EOF'
import random, sys
random.seed(sys.argv[1])
pool = ('字体工具测试页面永和九年岁在癸丑暮春之初会于会稽山阴之兰亭修禊事也群贤毕至'
'少长咸集此地有崇山峻岭茂林修竹又有清流激湍映带左右引以为流觞曲水列坐其次'
'虽无丝竹管弦之盛一觞一咏亦足以畅叙幽情是日也天朗气清惠风和畅')
n = random.choice([8, 16, 40, 128])
print(''.join(random.choices(pool, k=n)))
EOF
}
pass=0; fail=0
seed=0
for font in "${FONTS[@]}"; do
for rep in 1 2 3; do
seed=$((seed+1))
TXT=$(rand_text "$seed")
ENC=$(urlencode "$TXT"); FENC=$(urlencode "$font")
curl -s --noproxy '*' "http://127.0.0.1:${ZIG_PORT}/api?font=${FENC}&text=${ENC}&outType=${OUT_TYPE}" > /tmp/pz.bin
curl -s --noproxy '*' "http://127.0.0.1:${RS_PORT}/api?font=${FENC}&text=${ENC}&outType=${OUT_TYPE}" > /tmp/pr.bin
if cmp -s /tmp/pz.bin /tmp/pr.bin; then
echo "PASS ${font} seed=${seed} $(wc -c < /tmp/pz.bin)B"
pass=$((pass+1))
else
echo "FAIL ${font} seed=${seed} zig=$(wc -c < /tmp/pz.bin)B rust=$(wc -c < /tmp/pr.bin)B"
fail=$((fail+1))
fi
done
done
echo "----"
echo "结果: ${pass} PASS / ${fail} FAIL"
[ "$fail" -eq 0 ]

View File

@ -251,6 +251,7 @@ function onCardAppear(fontName: string) {
<LazyTrigger
v-for="font in sortedFonts"
:key="font.name"
root-margin="600px"
@appear="onCardAppear(font.name)"
>
<router-link