summaryrefslogtreecommitdiff
path: root/crates/typst-ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-ide')
-rw-r--r--crates/typst-ide/src/jump.rs152
1 files changed, 142 insertions, 10 deletions
diff --git a/crates/typst-ide/src/jump.rs b/crates/typst-ide/src/jump.rs
index 42833542..b29bc4a4 100644
--- a/crates/typst-ide/src/jump.rs
+++ b/crates/typst-ide/src/jump.rs
@@ -3,7 +3,7 @@ use std::num::NonZeroUsize;
use typst::layout::{Frame, FrameItem, PagedDocument, Point, Position, Size};
use typst::model::{Destination, Url};
use typst::syntax::{FileId, LinkedNode, Side, Source, Span, SyntaxKind};
-use typst::visualize::Geometry;
+use typst::visualize::{Curve, CurveItem, FillRule, Geometry};
use typst::WorldExt;
use crate::IdeWorld;
@@ -53,10 +53,20 @@ pub fn jump_from_click(
for (mut pos, item) in frame.items().rev() {
match item {
FrameItem::Group(group) => {
- // TODO: Handle transformation.
- if let Some(span) =
- jump_from_click(world, document, &group.frame, click - pos)
- {
+ let pos = click - pos;
+ if let Some(clip) = &group.clip {
+ if !clip.contains(FillRule::NonZero, pos) {
+ continue;
+ }
+ }
+ // Realistic transforms should always be invertible.
+ // An example of one that isn't is a scale of 0, which would
+ // not be clickable anyway.
+ let Some(inv_transform) = group.transform.invert() else {
+ continue;
+ };
+ let pos = pos.transform_inf(inv_transform);
+ if let Some(span) = jump_from_click(world, document, &group.frame, pos) {
return Some(span);
}
}
@@ -94,9 +104,32 @@ pub fn jump_from_click(
}
FrameItem::Shape(shape, span) => {
- let Geometry::Rect(size) = shape.geometry else { continue };
- if is_in_rect(pos, size, click) {
- return Jump::from_span(world, *span);
+ if shape.fill.is_some() {
+ let within = match &shape.geometry {
+ Geometry::Line(..) => false,
+ Geometry::Rect(size) => is_in_rect(pos, *size, click),
+ Geometry::Curve(curve) => {
+ curve.contains(shape.fill_rule, click - pos)
+ }
+ };
+ if within {
+ return Jump::from_span(world, *span);
+ }
+ }
+
+ if let Some(stroke) = &shape.stroke {
+ let within = !stroke.thickness.approx_empty() && {
+ // This curve is rooted at (0, 0), not `pos`.
+ let base_curve = match &shape.geometry {
+ Geometry::Line(to) => &Curve(vec![CurveItem::Line(*to)]),
+ Geometry::Rect(size) => &Curve::rect(*size),
+ Geometry::Curve(curve) => curve,
+ };
+ base_curve.stroke_contains(stroke, click - pos)
+ };
+ if within {
+ return Jump::from_span(world, *span);
+ }
}
}
@@ -146,9 +179,8 @@ pub fn jump_from_cursor(
fn find_in_frame(frame: &Frame, span: Span) -> Option<Point> {
for (mut pos, item) in frame.items() {
if let FrameItem::Group(group) = item {
- // TODO: Handle transformation.
if let Some(point) = find_in_frame(&group.frame, span) {
- return Some(point + pos);
+ return Some(pos + point.transform(group.transform));
}
}
@@ -270,6 +302,97 @@ mod tests {
}
#[test]
+ fn test_jump_from_click_transform_clip() {
+ let margin = point(10.0, 10.0);
+ test_click(
+ "#rect(width: 20pt, height: 20pt, fill: black)",
+ point(10.0, 10.0) + margin,
+ cursor(1),
+ );
+ test_click(
+ "#rect(width: 60pt, height: 10pt, fill: black)",
+ point(5.0, 30.0) + margin,
+ None,
+ );
+ test_click(
+ "#rotate(90deg, origin: bottom + left, rect(width: 60pt, height: 10pt, fill: black))",
+ point(5.0, 30.0) + margin,
+ cursor(38),
+ );
+ test_click(
+ "#scale(x: 300%, y: 300%, origin: top + left, rect(width: 10pt, height: 10pt, fill: black))",
+ point(20.0, 20.0) + margin,
+ cursor(45),
+ );
+ test_click(
+ "#box(width: 10pt, height: 10pt, clip: true, scale(x: 300%, y: 300%, \
+ origin: top + left, rect(width: 10pt, height: 10pt, fill: black)))",
+ point(20.0, 20.0) + margin,
+ None,
+ );
+ test_click(
+ "#box(width: 10pt, height: 10pt, clip: false, rect(width: 30pt, height: 30pt, fill: black))",
+ point(20.0, 20.0) + margin,
+ cursor(45),
+ );
+ test_click(
+ "#box(width: 10pt, height: 10pt, clip: true, rect(width: 30pt, height: 30pt, fill: black))",
+ point(20.0, 20.0) + margin,
+ None,
+ );
+ test_click(
+ "#rotate(90deg, origin: bottom + left)[hello world]",
+ point(5.0, 15.0) + margin,
+ cursor(40),
+ );
+ }
+
+ #[test]
+ fn test_jump_from_click_shapes() {
+ let margin = point(10.0, 10.0);
+
+ test_click(
+ "#rect(width: 30pt, height: 30pt, fill: black)",
+ point(15.0, 15.0) + margin,
+ cursor(1),
+ );
+
+ let circle = "#circle(width: 30pt, height: 30pt, fill: black)";
+ test_click(circle, point(15.0, 15.0) + margin, cursor(1));
+ test_click(circle, point(1.0, 1.0) + margin, None);
+
+ let bowtie =
+ "#polygon(fill: black, (0pt, 0pt), (20pt, 20pt), (20pt, 0pt), (0pt, 20pt))";
+ test_click(bowtie, point(1.0, 2.0) + margin, cursor(1));
+ test_click(bowtie, point(2.0, 1.0) + margin, None);
+ test_click(bowtie, point(19.0, 10.0) + margin, cursor(1));
+
+ let evenodd = r#"#polygon(fill: black, fill-rule: "even-odd",
+ (0pt, 10pt), (30pt, 10pt), (30pt, 20pt), (20pt, 20pt),
+ (20pt, 0pt), (10pt, 0pt), (10pt, 30pt), (20pt, 30pt),
+ (20pt, 20pt), (0pt, 20pt))"#;
+ test_click(evenodd, point(15.0, 15.0) + margin, None);
+ test_click(evenodd, point(5.0, 15.0) + margin, cursor(1));
+ test_click(evenodd, point(15.0, 5.0) + margin, cursor(1));
+ }
+
+ #[test]
+ fn test_jump_from_click_shapes_stroke() {
+ let margin = point(10.0, 10.0);
+
+ let rect =
+ "#place(dx: 10pt, dy: 10pt, rect(width: 10pt, height: 10pt, stroke: 5pt))";
+ test_click(rect, point(15.0, 15.0) + margin, None);
+ test_click(rect, point(10.0, 15.0) + margin, cursor(27));
+
+ test_click(
+ "#line(angle: 45deg, length: 10pt, stroke: 2pt)",
+ point(2.0, 2.0) + margin,
+ cursor(1),
+ );
+ }
+
+ #[test]
fn test_jump_from_cursor() {
let s = "*Hello* #box[ABC] World";
test_cursor(s, 12, None);
@@ -282,6 +405,15 @@ mod tests {
}
#[test]
+ fn test_jump_from_cursor_transform() {
+ test_cursor(
+ r#"#rotate(90deg, origin: bottom + left, [hello world])"#,
+ -5,
+ pos(1, 10.0, 16.58),
+ );
+ }
+
+ #[test]
fn test_backlink() {
let s = "#footnote[Hi]";
test_click(s, point(10.0, 10.0), pos(1, 18.5, 37.1).map(Jump::Position));