feat: make LinkElem::box_outset configurable

This commit is contained in:
Tobias Schmitz 2025-05-07 16:19:01 +02:00
parent 9b09146a6b
commit d02f8c33a8
No known key found for this signature in database
2 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,7 @@
use typst_library::foundations::StyleChain;
use typst_library::layout::{Fragment, Frame, FrameItem, HideElem, Point};
use typst_library::layout::{
Abs, Fragment, Frame, FrameItem, HideElem, Point, Rel, Sides,
};
use typst_library::model::{Destination, LinkElem};
/// Frame-level modifications resulting from styles that do not impose any
@ -21,6 +23,8 @@ use typst_library::model::{Destination, LinkElem};
pub struct FrameModifiers {
/// A destination to link to.
dest: Option<Destination>,
/// Outset of the link box to [`Self::dest`].
link_box_outset: Sides<Option<Rel<Abs>>>,
/// Whether the contents of the frame should be hidden.
hidden: bool,
}
@ -30,6 +34,7 @@ impl FrameModifiers {
pub fn get_in(styles: StyleChain) -> Self {
Self {
dest: LinkElem::current_in(styles),
link_box_outset: LinkElem::box_outset_in(styles),
hidden: HideElem::hidden_in(styles),
}
}
@ -53,8 +58,11 @@ pub trait FrameModify {
impl FrameModify for Frame {
fn modify(&mut self, modifiers: &FrameModifiers) {
if let Some(dest) = &modifiers.dest {
let size = self.size();
self.push(Point::zero(), FrameItem::Link(dest.clone(), size));
let mut size = self.size();
let outset = modifiers.link_box_outset.unwrap_or_default().relative_to(size);
size += outset.sum_by_axis();
let pos = Point::new(-outset.left, -outset.top);
self.push(pos, FrameItem::Link(dest.clone(), size));
}
if modifiers.hidden {

View File

@ -10,7 +10,7 @@ use crate::foundations::{
};
use crate::html::{attr, tag, HtmlElem};
use crate::introspection::Location;
use crate::layout::Position;
use crate::layout::{Em, Length, Position, Rel, Sides};
use crate::text::TextElem;
/// Links to a URL or a location in the document.
@ -93,6 +93,13 @@ pub struct LinkElem {
#[internal]
#[ghost]
pub current: Option<Destination>,
/// How much to expand the link box size without affecting the layout. See
/// the [box's documentation]($box.outset) for more details.
#[resolve]
#[fold]
#[default(Sides::splat(Some(Em::new(0.25).into())))]
pub box_outset: Sides<Option<Rel<Length>>>,
}
impl LinkElem {