mirror of
https://github.com/typst/typst
synced 2025-08-16 07:58:32 +08:00
Compare commits
No commits in common. "efdb75558f20543af39f75fb88b3bae59b20e2e8" and "838a46dbb7124125947bfdafe8ddf97810c5de47" have entirely different histories.
efdb75558f
...
838a46dbb7
@ -3,7 +3,7 @@ use std::num::NonZeroUsize;
|
|||||||
use typst::layout::{Frame, FrameItem, PagedDocument, Point, Position, Size};
|
use typst::layout::{Frame, FrameItem, PagedDocument, Point, Position, Size};
|
||||||
use typst::model::{Destination, Url};
|
use typst::model::{Destination, Url};
|
||||||
use typst::syntax::{FileId, LinkedNode, Side, Source, Span, SyntaxKind};
|
use typst::syntax::{FileId, LinkedNode, Side, Source, Span, SyntaxKind};
|
||||||
use typst::visualize::{Curve, CurveItem, FillRule, Geometry};
|
use typst::visualize::Geometry;
|
||||||
use typst::WorldExt;
|
use typst::WorldExt;
|
||||||
|
|
||||||
use crate::IdeWorld;
|
use crate::IdeWorld;
|
||||||
@ -53,20 +53,10 @@ pub fn jump_from_click(
|
|||||||
for (mut pos, item) in frame.items().rev() {
|
for (mut pos, item) in frame.items().rev() {
|
||||||
match item {
|
match item {
|
||||||
FrameItem::Group(group) => {
|
FrameItem::Group(group) => {
|
||||||
let pos = click - pos;
|
// TODO: Handle transformation.
|
||||||
if let Some(clip) = &group.clip {
|
if let Some(span) =
|
||||||
if !clip.contains(FillRule::NonZero, pos) {
|
jump_from_click(world, document, &group.frame, click - 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);
|
return Some(span);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,32 +94,9 @@ pub fn jump_from_click(
|
|||||||
}
|
}
|
||||||
|
|
||||||
FrameItem::Shape(shape, span) => {
|
FrameItem::Shape(shape, span) => {
|
||||||
if shape.fill.is_some() {
|
let Geometry::Rect(size) = shape.geometry else { continue };
|
||||||
let within = match &shape.geometry {
|
if is_in_rect(pos, size, click) {
|
||||||
Geometry::Line(..) => false,
|
return Jump::from_span(world, *span);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,8 +146,9 @@ pub fn jump_from_cursor(
|
|||||||
fn find_in_frame(frame: &Frame, span: Span) -> Option<Point> {
|
fn find_in_frame(frame: &Frame, span: Span) -> Option<Point> {
|
||||||
for (mut pos, item) in frame.items() {
|
for (mut pos, item) in frame.items() {
|
||||||
if let FrameItem::Group(group) = item {
|
if let FrameItem::Group(group) = item {
|
||||||
|
// TODO: Handle transformation.
|
||||||
if let Some(point) = find_in_frame(&group.frame, span) {
|
if let Some(point) = find_in_frame(&group.frame, span) {
|
||||||
return Some(pos + point.transform(group.transform));
|
return Some(point + pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,97 +269,6 @@ mod tests {
|
|||||||
test_click("$a + b$", point(28.0, 14.0), cursor(5));
|
test_click("$a + b$", point(28.0, 14.0), cursor(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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]
|
#[test]
|
||||||
fn test_jump_from_cursor() {
|
fn test_jump_from_cursor() {
|
||||||
let s = "*Hello* #box[ABC] World";
|
let s = "*Hello* #box[ABC] World";
|
||||||
@ -404,15 +281,6 @@ mod tests {
|
|||||||
test_cursor("$a + b$", -3, pos(1, 27.51, 16.83));
|
test_cursor("$a + b$", -3, pos(1, 27.51, 16.83));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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]
|
#[test]
|
||||||
fn test_backlink() {
|
fn test_backlink() {
|
||||||
let s = "#footnote[Hi]";
|
let s = "#footnote[Hi]";
|
||||||
|
@ -148,11 +148,11 @@ static TO_SRGB: LazyLock<qcms::Transform> = LazyLock::new(|| {
|
|||||||
/// | `magma` | A black to purple to yellow color map. |
|
/// | `magma` | A black to purple to yellow color map. |
|
||||||
/// | `plasma` | A purple to pink to yellow color map. |
|
/// | `plasma` | A purple to pink to yellow color map. |
|
||||||
/// | `rocket` | A black to red to white color map. |
|
/// | `rocket` | A black to red to white color map. |
|
||||||
/// | `mako` | A black to teal to white color map. |
|
/// | `mako` | A black to teal to yellow color map. |
|
||||||
/// | `vlag` | A light blue to white to red color map. |
|
/// | `vlag` | A light blue to white to red color map. |
|
||||||
/// | `icefire` | A light teal to black to orange color map. |
|
/// | `icefire` | A light teal to black to yellow color map. |
|
||||||
/// | `flare` | A orange to purple color map that is perceptually uniform. |
|
/// | `flare` | A orange to purple color map that is perceptually uniform. |
|
||||||
/// | `crest` | A light green to blue color map. |
|
/// | `crest` | A blue to white to red color map. |
|
||||||
///
|
///
|
||||||
/// Some popular presets are not included because they are not available under a
|
/// Some popular presets are not included because they are not available under a
|
||||||
/// free licence. Others, like
|
/// free licence. Others, like
|
||||||
|
@ -10,8 +10,6 @@ use crate::foundations::{
|
|||||||
use crate::layout::{Abs, Axes, BlockElem, Length, Point, Rel, Size};
|
use crate::layout::{Abs, Axes, BlockElem, Length, Point, Rel, Size};
|
||||||
use crate::visualize::{FillRule, Paint, Stroke};
|
use crate::visualize::{FillRule, Paint, Stroke};
|
||||||
|
|
||||||
use super::FixedStroke;
|
|
||||||
|
|
||||||
/// A curve consisting of movements, lines, and Bézier segments.
|
/// A curve consisting of movements, lines, and Bézier segments.
|
||||||
///
|
///
|
||||||
/// At any point in time, there is a conceptual pen or cursor.
|
/// At any point in time, there is a conceptual pen or cursor.
|
||||||
@ -532,65 +530,3 @@ impl Curve {
|
|||||||
Size::new(max_x - min_x, max_y - min_y)
|
Size::new(max_x - min_x, max_y - min_y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Curve {
|
|
||||||
fn to_kurbo(&self) -> impl Iterator<Item = kurbo::PathEl> + '_ {
|
|
||||||
use kurbo::PathEl;
|
|
||||||
|
|
||||||
self.0.iter().map(|item| match *item {
|
|
||||||
CurveItem::Move(point) => PathEl::MoveTo(point_to_kurbo(point)),
|
|
||||||
CurveItem::Line(point) => PathEl::LineTo(point_to_kurbo(point)),
|
|
||||||
CurveItem::Cubic(point, point1, point2) => PathEl::CurveTo(
|
|
||||||
point_to_kurbo(point),
|
|
||||||
point_to_kurbo(point1),
|
|
||||||
point_to_kurbo(point2),
|
|
||||||
),
|
|
||||||
CurveItem::Close => PathEl::ClosePath,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// When this curve is interpreted as a clip mask, would it contain `point`?
|
|
||||||
pub fn contains(&self, fill_rule: FillRule, needle: Point) -> bool {
|
|
||||||
let kurbo = kurbo::BezPath::from_vec(self.to_kurbo().collect());
|
|
||||||
let windings = kurbo::Shape::winding(&kurbo, point_to_kurbo(needle));
|
|
||||||
match fill_rule {
|
|
||||||
FillRule::NonZero => windings != 0,
|
|
||||||
FillRule::EvenOdd => windings % 2 != 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// When this curve is stroked with `stroke`, would the stroke contain
|
|
||||||
/// `point`?
|
|
||||||
pub fn stroke_contains(&self, stroke: &FixedStroke, needle: Point) -> bool {
|
|
||||||
let width = stroke.thickness.to_raw();
|
|
||||||
let cap = match stroke.cap {
|
|
||||||
super::LineCap::Butt => kurbo::Cap::Butt,
|
|
||||||
super::LineCap::Round => kurbo::Cap::Round,
|
|
||||||
super::LineCap::Square => kurbo::Cap::Square,
|
|
||||||
};
|
|
||||||
let join = match stroke.join {
|
|
||||||
super::LineJoin::Miter => kurbo::Join::Miter,
|
|
||||||
super::LineJoin::Round => kurbo::Join::Round,
|
|
||||||
super::LineJoin::Bevel => kurbo::Join::Bevel,
|
|
||||||
};
|
|
||||||
let miter_limit = stroke.miter_limit.get();
|
|
||||||
let mut style = kurbo::Stroke::new(width)
|
|
||||||
.with_caps(cap)
|
|
||||||
.with_join(join)
|
|
||||||
.with_miter_limit(miter_limit);
|
|
||||||
if let Some(dash) = &stroke.dash {
|
|
||||||
style = style.with_dashes(
|
|
||||||
dash.phase.to_raw(),
|
|
||||||
dash.array.iter().copied().map(Abs::to_raw),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
let opts = kurbo::StrokeOpts::default();
|
|
||||||
let tolerance = 0.01;
|
|
||||||
let expanded = kurbo::stroke(self.to_kurbo(), &style, &opts, tolerance);
|
|
||||||
kurbo::Shape::contains(&expanded, point_to_kurbo(needle))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn point_to_kurbo(point: Point) -> kurbo::Point {
|
|
||||||
kurbo::Point::new(point.x.to_raw(), point.y.to_raw())
|
|
||||||
}
|
|
||||||
|
@ -106,7 +106,7 @@ pub struct RectElem {
|
|||||||
pub radius: Corners<Option<Rel<Length>>>,
|
pub radius: Corners<Option<Rel<Length>>>,
|
||||||
|
|
||||||
/// How much to pad the rectangle's content.
|
/// How much to pad the rectangle's content.
|
||||||
/// See the [box's documentation]($box.inset) for more details.
|
/// See the [box's documentation]($box.outset) for more details.
|
||||||
#[resolve]
|
#[resolve]
|
||||||
#[fold]
|
#[fold]
|
||||||
#[default(Sides::splat(Some(Abs::pt(5.0).into())))]
|
#[default(Sides::splat(Some(Abs::pt(5.0).into())))]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user