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
41 changes: 26 additions & 15 deletions crates/handwriting-core/src/text/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,15 @@ impl<'a> Parser<'a> {
}
Some(p)
}
fn node(&mut self) -> Option<()> {
fn node(&mut self, path_midpoint: Option<Point>) -> 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",
Expand All @@ -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())
{
Expand All @@ -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()
Expand Down Expand Up @@ -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(";") {
Expand All @@ -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("--") {
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions crates/handwriting-core/src/text/circuit/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Parser {
"cdot" => "·",
"times" => "×",
"pm" => "±",
"le" | "leq" => "≤",
"ge" | "geq" => "≥",
"infty" => "∞",
"angle" => "∠",
"circ" => "°",
Expand Down
31 changes: 31 additions & 0 deletions crates/handwriting-core/tests/circuitikz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down