From 5291bf36612ab43f9857bf6d951414fac8e6ec7f Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Wed, 7 May 2025 16:35:50 +0200 Subject: [PATCH] feat: just expand text link boxes by 0.25em in height --- crates/typst-layout/src/inline/shaping.rs | 2 +- crates/typst-layout/src/modifiers.rs | 29 +++++++++++++++-------- crates/typst-library/src/model/link.rs | 9 +------ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/crates/typst-layout/src/inline/shaping.rs b/crates/typst-layout/src/inline/shaping.rs index 8236d1e36..277237a27 100644 --- a/crates/typst-layout/src/inline/shaping.rs +++ b/crates/typst-layout/src/inline/shaping.rs @@ -327,7 +327,7 @@ impl<'a> ShapedText<'a> { offset += width; } - frame.modify(&FrameModifiers::get_in(self.styles)); + frame.modify(&FrameModifiers::get_in(self.styles).set_text(self.styles)); frame } diff --git a/crates/typst-layout/src/modifiers.rs b/crates/typst-layout/src/modifiers.rs index 49fa627c3..3502a9710 100644 --- a/crates/typst-layout/src/modifiers.rs +++ b/crates/typst-layout/src/modifiers.rs @@ -1,7 +1,5 @@ -use typst_library::foundations::StyleChain; -use typst_library::layout::{ - Abs, Fragment, Frame, FrameItem, HideElem, Point, Rel, Sides, -}; +use typst_library::foundations::{Resolve, StyleChain}; +use typst_library::layout::{Abs, Em, Fragment, Frame, FrameItem, HideElem, Point}; use typst_library::model::{Destination, LinkElem}; /// Frame-level modifications resulting from styles that do not impose any @@ -23,8 +21,7 @@ use typst_library::model::{Destination, LinkElem}; pub struct FrameModifiers { /// A destination to link to. dest: Option, - /// Outset of the link box to [`Self::dest`]. - link_box_outset: Sides>>, + expand_text_height: Option, /// Whether the contents of the frame should be hidden. hidden: bool, } @@ -34,10 +31,18 @@ impl FrameModifiers { pub fn get_in(styles: StyleChain) -> Self { Self { dest: LinkElem::current_in(styles), - link_box_outset: LinkElem::box_outset_in(styles), + expand_text_height: None, hidden: HideElem::hidden_in(styles), } } + + pub fn set_text(mut self, styles: StyleChain) -> Self { + if self.dest.is_some() { + let height = Em::one().resolve(styles); + self.expand_text_height = Some(height); + } + self + } } /// Applies [`FrameModifiers`]. @@ -58,10 +63,14 @@ pub trait FrameModify { impl FrameModify for Frame { fn modify(&mut self, modifiers: &FrameModifiers) { if let Some(dest) = &modifiers.dest { + let mut pos = Point::zero(); 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); + if let Some(height) = modifiers.expand_text_height { + let expand_top = 0.25 * height; + let expand_bottom = 0.25 * height; + pos.y -= expand_top; + size.y += expand_top + expand_bottom; + } self.push(pos, FrameItem::Link(dest.clone(), size)); } diff --git a/crates/typst-library/src/model/link.rs b/crates/typst-library/src/model/link.rs index c5c84222a..ea85aa945 100644 --- a/crates/typst-library/src/model/link.rs +++ b/crates/typst-library/src/model/link.rs @@ -10,7 +10,7 @@ use crate::foundations::{ }; use crate::html::{attr, tag, HtmlElem}; use crate::introspection::Location; -use crate::layout::{Em, Length, Position, Rel, Sides}; +use crate::layout::Position; use crate::text::TextElem; /// Links to a URL or a location in the document. @@ -93,13 +93,6 @@ pub struct LinkElem { #[internal] #[ghost] pub current: Option, - - /// 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>>, } impl LinkElem {