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 [