mirror of
https://github.com/typst/typst
synced 2025-07-12 07:02:53 +08:00
refactor: use enum to better convey intent
This commit is contained in:
parent
43468ec6ee
commit
0b3df7548e
@ -327,7 +327,7 @@ impl<'a> ShapedText<'a> {
|
|||||||
offset += width;
|
offset += width;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.modify(&FrameModifiers::get_in(self.styles).set_text(self.styles));
|
frame.modify(&FrameModifiers::for_text_in(self.styles));
|
||||||
frame
|
frame
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,8 @@ use typst_library::model::{Destination, LinkElem};
|
|||||||
pub struct FrameModifiers {
|
pub struct FrameModifiers {
|
||||||
/// A destination to link to.
|
/// A destination to link to.
|
||||||
dest: Option<Destination>,
|
dest: Option<Destination>,
|
||||||
expand_text_height: Option<Abs>,
|
/// Used to determine if the link box should be expanded.
|
||||||
|
link_kind: LinkKind,
|
||||||
/// Whether the contents of the frame should be hidden.
|
/// Whether the contents of the frame should be hidden.
|
||||||
hidden: bool,
|
hidden: bool,
|
||||||
}
|
}
|
||||||
@ -31,20 +32,29 @@ impl FrameModifiers {
|
|||||||
pub fn get_in(styles: StyleChain) -> Self {
|
pub fn get_in(styles: StyleChain) -> Self {
|
||||||
Self {
|
Self {
|
||||||
dest: LinkElem::current_in(styles),
|
dest: LinkElem::current_in(styles),
|
||||||
expand_text_height: None,
|
link_kind: LinkKind::default(),
|
||||||
hidden: HideElem::hidden_in(styles),
|
hidden: HideElem::hidden_in(styles),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_text(mut self, styles: StyleChain) -> Self {
|
pub fn for_text_in(styles: StyleChain) -> Self {
|
||||||
if self.dest.is_some() {
|
let mut modifiers = Self::get_in(styles);
|
||||||
let height = Em::one().resolve(styles);
|
if modifiers.dest.is_some() {
|
||||||
self.expand_text_height = Some(height);
|
let em = Em::one().resolve(styles);
|
||||||
|
modifiers.link_kind = LinkKind::Text(em);
|
||||||
}
|
}
|
||||||
self
|
modifiers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
enum LinkKind {
|
||||||
|
/// Contains the font size.
|
||||||
|
Text(Abs),
|
||||||
|
#[default]
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
/// Applies [`FrameModifiers`].
|
/// Applies [`FrameModifiers`].
|
||||||
pub trait FrameModify {
|
pub trait FrameModify {
|
||||||
/// Apply the modifiers in-place.
|
/// Apply the modifiers in-place.
|
||||||
@ -65,9 +75,9 @@ impl FrameModify for Frame {
|
|||||||
if let Some(dest) = &modifiers.dest {
|
if let Some(dest) = &modifiers.dest {
|
||||||
let mut pos = Point::zero();
|
let mut pos = Point::zero();
|
||||||
let mut size = self.size();
|
let mut size = self.size();
|
||||||
if let Some(height) = modifiers.expand_text_height {
|
if let LinkKind::Text(em) = modifiers.link_kind {
|
||||||
let expand_top = 0.25 * height;
|
let expand_top = 0.25 * em;
|
||||||
let expand_bottom = 0.25 * height;
|
let expand_bottom = 0.25 * em;
|
||||||
pos.y -= expand_top;
|
pos.y -= expand_top;
|
||||||
size.y += expand_top + expand_bottom;
|
size.y += expand_top + expand_bottom;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user