From 69a6abd0e8451b45f531bdc38cb81780fedc1ffb Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Thu, 26 Nov 2026 22:50:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(crates/handwriting-core):=20=E8=AE=A9?= =?UTF-8?q?=E5=B8=A6=E4=B8=AD=E7=82=B9=E6=9D=A1=E4=BB=B6=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=9A=84=E6=B5=81=E7=A8=8B=E5=9B=BE=E6=AD=A3=E5=B8=B8=E5=87=BA?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景:循环流程图的中点标签和不等式命令曾导致整个绘图环境逐字输出。 解决手段:解析受限的线段中点标签和不等式命令,验证标签位置与箭头端点。 收益:这类作业的流程图能随正文一起绘制。 --- crates/handwriting-core/src/text/circuit.rs | 41 ++++++++++++------- .../src/text/circuit/labels.rs | 2 + crates/handwriting-core/tests/circuitikz.rs | 31 ++++++++++++++ 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/crates/handwriting-core/src/text/circuit.rs b/crates/handwriting-core/src/text/circuit.rs index 8f39b13e..8fe990d3 100644 --- a/crates/handwriting-core/src/text/circuit.rs +++ b/crates/handwriting-core/src/text/circuit.rs @@ -148,19 +148,15 @@ impl<'a> Parser<'a> { } Some(p) } - fn node(&mut self) -> Option<()> { + fn node(&mut self, path_midpoint: Option) -> Option<()> { let options = if self.source[self.at..].trim_start().starts_with('[') { self.group('[', ']')? } else { String::new() }; let text = self.group('{', '}')?; - let kind = options.trim(); - if ![ - "ground", - "rground", - "circ", - "ocirc", + let options = split_options(&options)?; + const ANCHORS: &[&str] = &[ "above", "below", "left", @@ -169,12 +165,20 @@ impl<'a> Parser<'a> { "above right", "below left", "below right", - "", - ] - .contains(&kind) - { - return None; - } + ]; + let (point, kind) = match options.as_slice() { + ["midway"] => (path_midpoint?, ""), + ["midway", anchor] | [anchor, "midway"] if ANCHORS.contains(anchor) => { + (path_midpoint?, *anchor) + } + [kind] + if ANCHORS.contains(kind) + || matches!(*kind, "" | "ground" | "rground" | "circ" | "ocirc") => + { + (self.current, *kind) + } + _ => return None, + }; if text.chars().count() > 256 || (matches!(kind, "ground" | "rground" | "circ" | "ocirc") && !text.is_empty()) { @@ -184,7 +188,7 @@ impl<'a> Parser<'a> { return None; } self.circuit.decorations.push(Decoration { - point: self.current, + point, kind: kind.to_owned(), text: if text.is_empty() { String::new() @@ -224,6 +228,7 @@ impl<'a> Parser<'a> { } self.current = self.point()?; let first = self.current; + let mut path_midpoint = None; loop { self.space(); if self.eat(";") { @@ -237,7 +242,7 @@ impl<'a> Parser<'a> { return Some(()); } if self.eat("node") { - self.node()?; + self.node(path_midpoint)?; continue; } let (component, route) = if self.eat("--") { @@ -262,6 +267,12 @@ impl<'a> Parser<'a> { Route::VerticalFirst => Some(Point(self.current.0, end.1)), Route::HorizontalFirst => Some(Point(end.0, self.current.1)), }; + path_midpoint = (component.kind == "short").then(|| { + middle.unwrap_or(Point( + (self.current.0 + end.0) / 2., + (self.current.1 + end.1) / 2., + )) + }); if let Some(point) = middle && point != self.current { diff --git a/crates/handwriting-core/src/text/circuit/labels.rs b/crates/handwriting-core/src/text/circuit/labels.rs index 3ddbf747..a6495024 100644 --- a/crates/handwriting-core/src/text/circuit/labels.rs +++ b/crates/handwriting-core/src/text/circuit/labels.rs @@ -80,6 +80,8 @@ impl Parser { "cdot" => "·", "times" => "×", "pm" => "±", + "le" | "leq" => "≤", + "ge" | "geq" => "≥", "infty" => "∞", "angle" => "∠", "circ" => "°", diff --git a/crates/handwriting-core/tests/circuitikz.rs b/crates/handwriting-core/tests/circuitikz.rs index c30b1c9c..3d498026 100644 --- a/crates/handwriting-core/tests/circuitikz.rs +++ b/crates/handwriting-core/tests/circuitikz.rs @@ -69,6 +69,37 @@ fn relative_coordinates_reference_and_named_coordinates_remain_exact() { ] ); } +#[test] +fn flowchart_conditions_follow_their_wire_without_moving_its_endpoint() { + let source = env(r"\draw[->] (0,0) -- (4,2) node[midway,above] {$i\le10$} + |- (8,6) node[right,midway] {$i\geq1$} -- (10,6) node {end};"); + let c = circuit::parse(&source).unwrap(); + let labels: Vec<_> = c + .decorations + .iter() + .map(|d| (d.point, d.kind.as_str(), d.text.as_str())) + .collect(); + assert_eq!( + labels, + [ + (circuit::Point(2., 1.), "above", "i≤10"), + (circuit::Point(4., 6.), "right", "i≥1"), + (circuit::Point(10., 6.), "", "end") + ] + ); + assert!(c.segments.last().unwrap().end_arrow); + assert!(matches!( + text::parse(&source).as_slice(), + [Node::Circuit(_)] + )); + for extra in [ + r"\draw (3,3) node[midway] {x};", + r"\draw (1,1) -- (2,2) node[midway,rotate] {x};", + ] { + assert!(circuit::parse(&env(&format!(r"\draw (0,0) -- (1,0);{extra}"))).is_none()); + } +} + #[test] fn unknown_topology_and_budgets_preserve_the_whole_environment() { for body in [