Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- MCP 的 RSI 完整验证报告区分首次导出就与检查单匹配和后来才匹配,并显示导出文件总数和多次发布的任务数。

- 正文和公式中的 Ω、μ 改用所选字体笔宽与倾斜手绘,不再借用手写字体里的印刷体希腊字母。本次更新前保存的精调稿保留原字形和字位,不会因此漂移。
- 正文、公式和电路标签优先使用主字体的希腊字形。常用单位、变量及 θ/ϑ、φ/ϕ、ε/ϵ、ρ/ϱ 等变体缺字时,按主字体笔迹补绘;随包李国夫手写体中的这组选定字形也使用笔绘。已有精调键的旧稿继续沿用保存时的字形和位置。

- MCP 的 TeX 包目录可按中英文学科词(可多个词)、ID 或命令搜索用途和写法,AI 客户端可在建稿前查看默认包和用户选用的自定义命令。

Expand Down
7 changes: 6 additions & 1 deletion crates/handwriting-core/examples/math_strokes_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let output = PathBuf::from(env::args_os().nth(1).ok_or("output directory required")?);
fs::create_dir_all(&output)?;
let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/font_assets");
let text = "离散数学:推理与证明\n1. 拒取式:(A→B)∧¬B ⇒ ¬A\n2. 析取三段论:(A∨B)∧¬A ⇒ B\n3. 等价关系:A↔B,A⇔B;反向推导:B←A,B⇐A。\n4. 序关系:a<b,b>a,x≤y,y≥x,x≠y,x≈y。\n数值分析:误差估计与根式\n5. |Rₙ|≤(M)/(n+1)! × |x-a|ⁿ⁺¹\n6. 标准差:σ=√((Σᵢ₌₁ⁿ(xᵢ-μ)²)/(n))\n7. 嵌套分式:R=(1)/(1+(x+1)/(x+2))\n8. 根式:√(x²+y²),∛(1+x),√((a+b)/(c+d))\n结论:符号笔画随所选字体,数学含义与连接关系保持。";
let text = "离散数学:推理与证明\n1. 拒取式:(A→B)∧¬B ⇒ ¬A\n2. 析取三段论:(A∨B)∧¬A ⇒ B\n3. 等价关系:A↔B,A⇔B;反向推导:B←A,B⇐A。\n4. 序关系:a<b,b>a,x≤y,y≥x,x≠y,x≈y。\n数值分析:误差估计与根式\n5. |Rₙ|≤(M)/(n+1)! × |x-a|ⁿ⁺¹\n6. 标准差:σ=√((Σᵢ₌₁ⁿ(xᵢ-μ)²)/(n))\n7. 嵌套分式:R=(1)/(1+(x+1)/(x+2))\n8. 根式:√(x²+y²),∛(1+x),√((a+b)/(c+d))\n电阻 R=10 kΩ,C=100 μF;热量 ΔH,参数 α β π。\n相位 θ ϑ,磁通 φ ϕ;波长 λ,介电常数 ε ϵ,电阻率 ρ ϱ。\n信号 ω τ ζ,波函数 ψ,磁通 Φ。\n结论:符号笔画随所选字体,数学含义与连接关系保持。";
for (name, file) in [
("yunyan", "云烟体"),
("huayang", "华阳手写体"),
("liguofu", "李国夫手写体"),
("wenkai", "霞鹜文楷"),
("zhimang", "志莽行书"),
("qingye", "青叶手写体"),
("youzai", "悠哉手写体"),
("shenyun", "神韵英子楷书"),
("manhei", "霞鹜漫黑"),
] {
let primary = FontBook::load(&fs::read(dir.join(format!("{file}.ttf")))?)?;
let fonts = FontBook::with_text_fallbacks(primary, text, |name| {
Expand Down
18 changes: 9 additions & 9 deletions crates/handwriting-core/src/fine_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,45 +153,45 @@ impl Adjustments {
}

// Crop extents set the pivot and gradient domain; keep them when reopening saved edits.
// Fine4 switched small glyphs to coverage masks; Fine5 hand-draws Ω and μ.
// Fine4 introduced coverage masks; Fine5 drew all Ω/μ. Fine6 selects Greek forms per primary font.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum LayoutVersion {
Fine2,
Fine3,
Fine4,
Fine5,
Fine6,
}

impl LayoutVersion {
pub(crate) const CURRENT: Self = Self::Fine6;

fn prefix(self) -> &'static [u8] {
match self {
Self::Fine2 => b"panache-fine-2\0",
Self::Fine3 => b"panache-fine-3\0",
Self::Fine4 => b"panache-fine-4\0",
Self::Fine5 => b"panache-fine-5\0",
Self::Fine6 => b"panache-fine-6\0",
}
}

pub(crate) fn for_document(text: &str, p: &Parameters) -> Self {
if p.fine_edit
&& let Some(edits) = &p.fine_edits
{
for version in [Self::Fine4, Self::Fine3, Self::Fine2] {
for version in [Self::Fine5, Self::Fine4, Self::Fine3, Self::Fine2] {
if edits.key == layout_key(text, p, version.prefix()) {
return version;
}
}
}
Self::Fine5
Self::CURRENT
}

pub(crate) fn legacy_mask(self) -> bool {
self < Self::Fine4
}

pub(crate) fn hand_drawn_greek(self) -> bool {
self >= Self::Fine5
}
}

pub(crate) fn document_key(text: &str, p: &Parameters) -> String {
Expand Down Expand Up @@ -259,15 +259,15 @@ mod layout_version_tests {
params.fine_edits.as_mut().unwrap().key = fine4.clone();
let saved = LayoutVersion::for_document(text, &params);
assert!(saved == LayoutVersion::Fine4);
assert!(!saved.legacy_mask() && !saved.hand_drawn_greek());
assert!(!saved.legacy_mask());
assert_eq!(
draft_keys(text, &params),
(
fine4,
Some(layout_key(text, &params, LayoutVersion::Fine3.prefix()))
)
);
assert!(LayoutVersion::for_document("新内容", &params).hand_drawn_greek());
assert!(LayoutVersion::for_document("新内容", &params) == LayoutVersion::Fine6);
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/handwriting-core/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ fn render_planes(
let mut composer = composition::UnitComposer::new(p, &r.text, source.as_deref(), (w, h));
let mut glyphs = Glyphs::with_parameters(&r.fonts, p)?;
let fine_layout = fine_edit::LayoutVersion::for_document(&r.text, &r.params);
glyphs.use_legacy_masks(fine_layout.legacy_mask());
glyphs.use_hand_drawn_greek(fine_layout.hand_drawn_greek());
glyphs.set_layout_version(fine_layout);
let mut pages = Pages {
canvas: if transparent {
std::mem::take(&mut background)
Expand Down Expand Up @@ -763,7 +762,7 @@ mod ruled_tests {
x: 10.,
y: 10. + spacing,
previous_bottom: 10.,
fine_layout: fine_edit::LayoutVersion::Fine5,
fine_layout: fine_edit::LayoutVersion::CURRENT,
table_edge: Vec::new(),
written_lines: 0,
line: vec![],
Expand Down
51 changes: 30 additions & 21 deletions crates/handwriting-core/src/render/glyph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub(super) struct Glyphs<'a> {
cache: HashMap<(char, u32), Glyph>,
cache_bytes: usize,
pen: pen::MathPen,
layout_version: crate::fine_edit::LayoutVersion,
drawing_stream: u64,
}
type Path = (Vec<(i32, i32)>, i32);
Expand All @@ -122,17 +123,16 @@ impl<'a> Glyphs<'a> {
cache: HashMap::new(),
cache_bytes: 0,
pen: pen::MathPen::sample(book.primary()),
layout_version: crate::fine_edit::LayoutVersion::CURRENT,
drawing_stream: 0,
}
}
pub(super) fn legacy_mask(&self) -> bool {
self.pen.legacy_mask
}
pub(super) fn use_legacy_masks(&mut self, legacy: bool) {
self.pen.legacy_mask = legacy;
}
pub(super) fn use_hand_drawn_greek(&mut self, enabled: bool) {
self.pen.hand_drawn_greek = enabled;
pub(super) fn set_layout_version(&mut self, version: crate::fine_edit::LayoutVersion) {
self.layout_version = version;
self.pen.legacy_mask = version.legacy_mask();
}
pub fn with_parameters(book: &'a FontBook, p: &Parameters) -> Result<Self> {
let mut glyphs = Self::new(book);
Expand Down Expand Up @@ -163,13 +163,35 @@ impl<'a> Glyphs<'a> {
if let Some(g) = self.cache.get(&key) {
return Ok(g.clone());
}
use pen::greek::GlyphSource;
let glyph = match pen::greek::source(self.book.primary(), c, self.layout_version) {
GlyphSource::Pen(letter) => self.pen.draw_greek(letter, size)?,
GlyphSource::Font(c) => {
if let Some(glyph) = self.circled(c, size)? {
glyph
} else if let Some(glyph) = self.pen.symbol(c, size)? {
glyph
} else {
self.font_glyph(c, size, uses_latin)?
}
}
};
if self.cache.len() >= 1024 || self.cache_bytes + glyph.mask.len() > 8 * 1024 * 1024 {
self.cache.clear();
self.cache_bytes = 0;
}
self.cache_bytes += glyph.mask.len();
self.cache.insert(key, glyph.clone());
Ok(glyph)
}
fn font_glyph(&self, c: char, size: f32, uses_latin: bool) -> Result<Glyph> {
let selected = match self.latin_font {
Some(font) if uses_latin && crate::fonts::visible_glyph(font, c) => font,
_ => self.book.for_char(c),
};
let (m, data) = selected.rasterize(c, size);
let advance = m.advance_width.max((m.xmin + m.width as i32) as f32);
let mut glyph = if m.width == 0 || m.height == 0 {
let glyph = if m.width == 0 || m.height == 0 {
Glyph::empty(advance)
} else {
Self::check_size(m.width as u32, m.height as u32)?;
Expand All @@ -182,22 +204,9 @@ impl<'a> Glyphs<'a> {
}
};
// 2026-10-15:裁去栅格化器的空白外缘,避免间距由透明 padding 决定。
if glyph.ink {
glyph = Self::trim(glyph);
}
if let Some(symbol) = self.circled(c, size)? {
glyph = symbol;
} else if let Some(symbol) = self.pen.symbol(c, size)? {
glyph = symbol;
}
if self.cache.len() >= 1024 || self.cache_bytes + glyph.mask.len() > 8 * 1024 * 1024 {
self.cache.clear();
self.cache_bytes = 0;
}
self.cache_bytes += glyph.mask.len();
self.cache.insert(key, glyph.clone());
Ok(glyph)
Ok(if glyph.ink { Self::trim(glyph) } else { glyph })
}

fn trim(mut g: Glyph) -> Glyph {
let mut bounds = (g.mask.width(), g.mask.height(), 0, 0);
for (x, y, p) in g.mask.enumerate_pixels() {
Expand Down
68 changes: 7 additions & 61 deletions crates/handwriting-core/src/render/glyph/pen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::Result;
use crate::text::ArrowKind;
use image::GrayImage;

pub(super) mod greek;
mod sample;

pub(super) struct MathPen {
Expand All @@ -18,7 +19,6 @@ pub(super) struct MathPen {
pub naturalness: f32,
pub seed: u64,
pub(super) legacy_mask: bool,
pub(super) hand_drawn_greek: bool,
}

impl MathPen {
Expand Down Expand Up @@ -72,59 +72,7 @@ impl MathPen {
)?))
}

// 2026-12-02:多数中文手写字体的希腊字母借自印刷体,Ω 与 μ 改用字体采样的笔迹手绘;
// Fine5 之前保存的精调稿保留字体字形,避免已保存字位漂移。
fn greek(&self, c: char, size: f32) -> Result<Option<Glyph>> {
if !self.hand_drawn_greek {
return Ok(None);
}
let mut paths: Vec<Vec<(f32, f32)>> = Vec::new();
let width;
match c {
'\u{3a9}' | '\u{2126}' => {
let cap = self.cap * size;
width = cap * 0.92;
let (cx, cy) = (width * 0.5, -cap * 0.56);
let (rx, ry) = (width * 0.4, cap * 0.44);
let mut stroke = vec![(0., -cap * 0.015), (width * 0.27, 0.)];
for i in 0..=24 {
let t = (235. - 290. * i as f32 / 24.).to_radians();
// 右半弧略收窄,避免镜面对称的印刷轮廓。
let r = if t.cos() > 0. { rx * 0.94 } else { rx };
stroke.push((cx + r * t.cos(), cy - ry * t.sin()));
}
stroke.extend([(width * 0.71, -cap * 0.01), (width * 0.95, -cap * 0.04)]);
paths.push(stroke);
}
'\u{3bc}' | '\u{b5}' => {
let x = self.lower * size;
width = x * 0.95;
paths.push(vec![(width * 0.07, -x), (0., x * 0.5)]);
paths.push(vec![
(width * 0.04, -x * 0.45),
(width * 0.16, -x * 0.08),
(width * 0.4, 0.),
(width * 0.63, -x * 0.14),
(width * 0.8, -x * 0.58),
(width * 0.84, -x),
]);
paths.push(vec![
(width * 0.84, -x),
(width * 0.83, -x * 0.14),
(width * 0.9, 0.),
(width, -x * 0.07),
]);
}
_ => return Ok(None),
}
self.slant_paths(self.axis * size, &mut paths);
Ok(Some(self.draw(size, width + size * 0.08, &paths)?))
}

pub fn symbol(&self, c: char, size: f32) -> Result<Option<Glyph>> {
if let Some(glyph) = self.greek(c, size)? {
return Ok(Some(glyph));
}
if !"¬→←↔⇒⇐⇔≤≥≠".contains(c) {
return Ok(None);
}
Expand Down Expand Up @@ -440,7 +388,7 @@ mod tests {
);
}
#[test]
fn greek_units_are_single_pen_strokes_except_in_legacy_layouts() {
fn greek_units_have_connected_ink_and_distinct_vertical_metrics() {
for name in ["云烟体", "李国夫手写体", "霞鹜文楷"] {
let font = FontBook::load(
&std::fs::read(
Expand All @@ -453,19 +401,17 @@ mod tests {
let mut pen = MathPen::sample(&font);
pen.naturalness = 1.;
for size in [18., 36., 72.] {
for c in ['\u{3a9}', '\u{2126}', '\u{3bc}', '\u{b5}'] {
let g = pen.symbol(c, size).unwrap().unwrap();
assert_eq!(components(&g), 1, "{name} {c} {size}");
for c in [greek::Letter::Omega, greek::Letter::Mu] {
let g = pen.draw_greek(c, size).unwrap();
assert_eq!(components(&g), 1, "{name} {c:?} {size}");
assert!(g.advance >= g.right() as f32);
assert!((g.mask.height() as f32) < size);
}
let omega = pen.symbol('\u{3a9}', size).unwrap().unwrap();
let mu = pen.symbol('\u{3bc}', size).unwrap().unwrap();
let omega = pen.draw_greek(greek::Letter::Omega, size).unwrap();
let mu = pen.draw_greek(greek::Letter::Mu, size).unwrap();
assert!(omega.top < mu.top, "{name}: Ω reaches cap height");
assert!(mu.bottom() > omega.bottom(), "{name}: μ descends");
}
pen.hand_drawn_greek = false;
assert!(pen.symbol('\u{3a9}', 36.).unwrap().is_none());
}
}
#[test]
Expand Down
Loading
Loading