summaryrefslogtreecommitdiff
path: root/src/library/text
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-10-28 16:43:38 +0200
committerLaurenz <laurmaedje@gmail.com>2022-10-28 16:43:38 +0200
commit95e9134a3c7d7a14d8c8928413fdffc665671895 (patch)
tree822b5f6c2d23aba33cfe4d199405e493e37c3d70 /src/library/text
parent66030ae5d73d85a0463562230b87f8ec7554c746 (diff)
Refactor `geom` module
Diffstat (limited to 'src/library/text')
-rw-r--r--src/library/text/deco.rs24
-rw-r--r--src/library/text/link.rs6
-rw-r--r--src/library/text/mod.rs24
-rw-r--r--src/library/text/par.rs66
-rw-r--r--src/library/text/shaping.rs18
-rw-r--r--src/library/text/shift.rs2
6 files changed, 70 insertions, 70 deletions
diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs
index 1242488b..6a4905c8 100644
--- a/src/library/text/deco.rs
+++ b/src/library/text/deco.rs
@@ -26,10 +26,10 @@ impl<const L: DecoLine> DecoNode<L> {
/// Position of the line relative to the baseline, read from the font tables
/// if `auto`.
#[property(resolve)]
- pub const OFFSET: Smart<RawLength> = Smart::Auto;
+ pub const OFFSET: Smart<Length> = Smart::Auto;
/// Amount that the line will be longer or shorter than its associated text.
#[property(resolve)]
- pub const EXTENT: RawLength = RawLength::zero();
+ pub const EXTENT: Length = Length::zero();
/// Whether the line skips sections in which it would collide
/// with the glyphs. Does not apply to strikethrough.
pub const EVADE: bool = true;
@@ -69,9 +69,9 @@ impl<const L: DecoLine> Show for DecoNode<L> {
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Decoration {
pub line: DecoLine,
- pub stroke: RawStroke<Length>,
- pub offset: Smart<Length>,
- pub extent: Length,
+ pub stroke: RawStroke<Abs>,
+ pub offset: Smart<Abs>,
+ pub extent: Abs,
pub evade: bool,
}
@@ -92,9 +92,9 @@ pub fn decorate(
frame: &mut Frame,
deco: &Decoration,
text: &Text,
- shift: Length,
+ shift: Abs,
pos: Point,
- width: Length,
+ width: Abs,
) {
let font_metrics = text.font.metrics();
let metrics = match deco.line {
@@ -116,9 +116,9 @@ pub fn decorate(
let mut start = pos.x - deco.extent;
let end = pos.x + (width + 2.0 * deco.extent);
- let mut push_segment = |from: Length, to: Length| {
+ let mut push_segment = |from: Abs, to: Abs| {
let origin = Point::new(from, pos.y + offset);
- let target = Point::new(to - from, Length::zero());
+ let target = Point::new(to - from, Abs::zero());
if target.x >= min_width || !evade {
let shape = Geometry::Line(target).stroked(stroke);
@@ -161,7 +161,7 @@ pub fn decorate(
intersections.extend(
path.segments()
.flat_map(|seg| seg.intersect_line(line))
- .map(|is| Length::raw(line.eval(is.line_t).x)),
+ .map(|is| Abs::raw(line.eval(is.line_t).x)),
);
}
}
@@ -196,12 +196,12 @@ pub fn decorate(
struct BezPathBuilder {
path: BezPath,
units_per_em: f64,
- font_size: Length,
+ font_size: Abs,
x_offset: f64,
}
impl BezPathBuilder {
- fn new(units_per_em: f64, font_size: Length, x_offset: f64) -> Self {
+ fn new(units_per_em: f64, font_size: Abs, x_offset: f64) -> Self {
Self {
path: BezPath::new(),
units_per_em,
diff --git a/src/library/text/link.rs b/src/library/text/link.rs
index 78ae4d23..7c4a95ed 100644
--- a/src/library/text/link.rs
+++ b/src/library/text/link.rs
@@ -43,9 +43,9 @@ castable! {
Value::Str(string) => Self::Url(string.into()),
Value::Dict(dict) => {
let page = dict.get("page")?.clone().cast()?;
- let x: RawLength = dict.get("x")?.clone().cast()?;
- let y: RawLength = dict.get("y")?.clone().cast()?;
- Self::Internal(Location { page, pos: Point::new(x.length, y.length) })
+ let x: Length = dict.get("x")?.clone().cast()?;
+ let y: Length = dict.get("y")?.clone().cast()?;
+ Self::Internal(Location { page, pos: Point::new(x.abs, y.abs) })
},
}
diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs
index 299357de..2d302b9a 100644
--- a/src/library/text/mod.rs
+++ b/src/library/text/mod.rs
@@ -48,19 +48,19 @@ impl TextNode {
/// The size of the glyphs.
#[property(shorthand, fold)]
- pub const SIZE: TextSize = Length::pt(11.0);
+ pub const SIZE: TextSize = Abs::pt(11.0);
/// The glyph fill color.
#[property(shorthand)]
pub const FILL: Paint = Color::BLACK.into();
/// The amount of space that should be added between characters.
#[property(resolve)]
- pub const TRACKING: RawLength = RawLength::zero();
+ pub const TRACKING: Length = Length::zero();
/// The width of spaces relative to the font's space width.
#[property(resolve)]
- pub const SPACING: Relative<RawLength> = Relative::one();
+ pub const SPACING: Rel<Length> = Rel::one();
/// The offset of the baseline.
#[property(resolve)]
- pub const BASELINE: RawLength = RawLength::zero();
+ pub const BASELINE: Length = Length::zero();
/// Whether certain glyphs can hang over into the margin.
pub const OVERHANG: bool = true;
/// The top end of the text bounding box.
@@ -243,17 +243,17 @@ castable! {
/// The size of text.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub struct TextSize(pub RawLength);
+pub struct TextSize(pub Length);
impl Fold for TextSize {
- type Output = Length;
+ type Output = Abs;
fn fold(self, outer: Self::Output) -> Self::Output {
- self.0.em.at(outer) + self.0.length
+ self.0.em.at(outer) + self.0.abs
}
}
-castable!(TextSize: RawLength);
+castable!(TextSize: Length);
/// Specifies the bottom or top edge of text.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
@@ -261,12 +261,12 @@ pub enum TextEdge {
/// An edge specified using one of the well-known font metrics.
Metric(VerticalFontMetric),
/// An edge specified as a length.
- Length(RawLength),
+ Length(Length),
}
impl TextEdge {
/// Resolve the value of the text edge given a font's metrics.
- pub fn resolve(self, styles: StyleChain, metrics: &FontMetrics) -> Length {
+ pub fn resolve(self, styles: StyleChain, metrics: &FontMetrics) -> Abs {
match self {
Self::Metric(metric) => metrics.vertical(metric).resolve(styles),
Self::Length(length) => length.resolve(styles),
@@ -310,8 +310,8 @@ castable! {
HorizontalDir,
Expected: "direction",
@dir: Dir => match dir.axis() {
- SpecAxis::Horizontal => Self(*dir),
- SpecAxis::Vertical => Err("must be horizontal")?,
+ Axis::X => Self(*dir),
+ Axis::Y => Err("must be horizontal")?,
},
}
diff --git a/src/library/text/par.rs b/src/library/text/par.rs
index 859a7c87..477bf97f 100644
--- a/src/library/text/par.rs
+++ b/src/library/text/par.rs
@@ -30,13 +30,13 @@ pub enum ParChild {
impl ParNode {
/// The spacing between lines.
#[property(resolve)]
- pub const LEADING: RawLength = Em::new(0.65).into();
+ pub const LEADING: Length = Em::new(0.65).into();
/// The extra spacing between paragraphs.
#[property(resolve)]
- pub const SPACING: RawLength = Em::new(1.2).into();
+ pub const SPACING: Length = Em::new(1.2).into();
/// The indent the first line of a consecutive paragraph should have.
#[property(resolve)]
- pub const INDENT: RawLength = RawLength::zero();
+ pub const INDENT: Length = Length::zero();
/// Whether to allow paragraph spacing when there is paragraph indent.
pub const SPACING_AND_INDENT: bool = false;
@@ -119,8 +119,8 @@ castable! {
HorizontalAlign,
Expected: "alignment",
@align: RawAlign => match align.axis() {
- SpecAxis::Horizontal => Self(*align),
- SpecAxis::Vertical => Err("must be horizontal")?,
+ Axis::X => Self(*align),
+ Axis::Y => Err("must be horizontal")?,
},
}
@@ -212,7 +212,7 @@ struct Preparation<'a> {
/// The text language if it's the same for all children.
lang: Option<Lang>,
/// The resolved leading between lines.
- leading: Length,
+ leading: Abs,
/// The paragraph's resolved alignment.
align: Align,
/// Whether to justify the paragraph.
@@ -292,9 +292,9 @@ enum Item<'a> {
/// A shaped text run with consistent style and direction.
Text(ShapedText<'a>),
/// Absolute spacing between other items.
- Absolute(Length),
+ Absolute(Abs),
/// Fractional spacing between other items.
- Fractional(Fraction),
+ Fractional(Fr),
/// A layouted child node.
Frame(Frame),
/// A repeating node that fills the remaining space.
@@ -320,12 +320,12 @@ impl<'a> Item<'a> {
}
/// The natural layouted width of the item.
- fn width(&self) -> Length {
+ fn width(&self) -> Abs {
match self {
Self::Text(shaped) => shaped.width,
Self::Absolute(v) => *v,
Self::Frame(frame) => frame.width(),
- Self::Fractional(_) | Self::Repeat(_, _) => Length::zero(),
+ Self::Fractional(_) | Self::Repeat(_, _) => Abs::zero(),
}
}
}
@@ -354,7 +354,7 @@ struct Line<'a> {
/// there is only one text item, this takes precedence over `first`.
last: Option<Item<'a>>,
/// The width of the line.
- width: Length,
+ width: Abs,
/// Whether the line should be justified.
justify: bool,
/// Whether the line ends with a hyphen or dash, either naturally or through
@@ -402,8 +402,8 @@ impl<'a> Line<'a> {
}
/// How much of the line is stretchable spaces.
- fn stretch(&self) -> Length {
- let mut stretch = Length::zero();
+ fn stretch(&self) -> Abs {
+ let mut stretch = Abs::zero();
for shaped in self.items().filter_map(Item::text) {
stretch += shaped.stretch();
}
@@ -411,11 +411,11 @@ impl<'a> Line<'a> {
}
/// The sum of fractions in the line.
- fn fr(&self) -> Fraction {
+ fn fr(&self) -> Fr {
self.items()
.filter_map(|item| match item {
Item::Fractional(fr) => Some(*fr),
- Item::Repeat(_, _) => Some(Fraction::one()),
+ Item::Repeat(_, _) => Some(Fr::one()),
_ => None,
})
.sum()
@@ -533,7 +533,7 @@ fn prepare<'a>(
items.push(Item::Repeat(repeat, styles));
} else {
let size = Size::new(regions.first.x, regions.base.y);
- let pod = Regions::one(size, regions.base, Spec::splat(false));
+ let pod = Regions::one(size, regions.base, Axes::splat(false));
let mut frame = node.layout(world, &pod, styles)?.remove(0);
frame.translate(Point::with_y(styles.get(TextNode::BASELINE)));
frame.apply_role(Role::GenericInline);
@@ -628,7 +628,7 @@ fn shared_get<'a, K: Key<'a>>(
fn linebreak<'a>(
p: &'a Preparation<'a>,
world: Tracked<dyn World>,
- width: Length,
+ width: Abs,
) -> Vec<Line<'a>> {
match p.styles.get(ParNode::LINEBREAKS) {
Linebreaks::Simple => linebreak_simple(p, world, width),
@@ -642,7 +642,7 @@ fn linebreak<'a>(
fn linebreak_simple<'a>(
p: &'a Preparation<'a>,
world: Tracked<dyn World>,
- width: Length,
+ width: Abs,
) -> Vec<Line<'a>> {
let mut lines = vec![];
let mut start = 0;
@@ -702,7 +702,7 @@ fn linebreak_simple<'a>(
fn linebreak_optimized<'a>(
p: &'a Preparation<'a>,
world: Tracked<dyn World>,
- width: Length,
+ width: Abs,
) -> Vec<Line<'a>> {
/// The cost of a line or paragraph layout.
type Cost = f64;
@@ -930,7 +930,7 @@ fn line<'a>(
first: None,
inner: &[],
last: None,
- width: Length::zero(),
+ width: Abs::zero(),
justify,
dash: false,
};
@@ -938,7 +938,7 @@ fn line<'a>(
// Slice out the relevant items.
let (expanded, mut inner) = p.slice(range.clone());
- let mut width = Length::zero();
+ let mut width = Abs::zero();
// Reshape the last item if it's split in half or hyphenated.
let mut last = None;
@@ -1075,10 +1075,10 @@ fn commit(
world: Tracked<dyn World>,
line: &Line,
regions: &Regions,
- width: Length,
+ width: Abs,
) -> SourceResult<Frame> {
let mut remaining = width - line.width;
- let mut offset = Length::zero();
+ let mut offset = Abs::zero();
// Reorder the line from logical to visual order.
let reordered = reorder(line);
@@ -1112,22 +1112,22 @@ fn commit(
// Determine how much to justify each space.
let fr = line.fr();
- let mut justification = Length::zero();
- if remaining < Length::zero() || (line.justify && fr.is_zero()) {
+ let mut justification = Abs::zero();
+ if remaining < Abs::zero() || (line.justify && fr.is_zero()) {
let justifiables = line.justifiables();
if justifiables > 0 {
justification = remaining / justifiables as f64;
- remaining = Length::zero();
+ remaining = Abs::zero();
}
}
- let mut top = Length::zero();
- let mut bottom = Length::zero();
+ let mut top = Abs::zero();
+ let mut bottom = Abs::zero();
// Build the frames and determine the height and baseline.
let mut frames = vec![];
for item in reordered {
- let mut push = |offset: &mut Length, frame: Frame| {
+ let mut push = |offset: &mut Abs, frame: Frame| {
let width = frame.width();
top.set_max(frame.baseline());
bottom.set_max(frame.size().y - frame.baseline());
@@ -1151,9 +1151,9 @@ fn commit(
}
Item::Repeat(node, styles) => {
let before = offset;
- let fill = Fraction::one().share(fr, remaining);
+ let fill = Fr::one().share(fr, remaining);
let size = Size::new(fill, regions.base.y);
- let pod = Regions::one(size, regions.base, Spec::new(false, false));
+ let pod = Regions::one(size, regions.base, Axes::new(false, false));
let frame = node.layout(world, &pod, *styles)?.remove(0);
let width = frame.width();
let count = (fill / width).floor();
@@ -1162,7 +1162,7 @@ fn commit(
if count == 1.0 {
offset += p.align.position(remaining);
}
- if width > Length::zero() {
+ if width > Abs::zero() {
for _ in 0 .. (count as usize).min(1000) {
push(&mut offset, frame.clone());
offset += apart;
@@ -1175,7 +1175,7 @@ fn commit(
// Remaining space is distributed now.
if !fr.is_zero() {
- remaining = Length::zero();
+ remaining = Abs::zero();
}
let size = Size::new(width, top + bottom);
diff --git a/src/library/text/shaping.rs b/src/library/text/shaping.rs
index 487b9090..9ac3db1c 100644
--- a/src/library/text/shaping.rs
+++ b/src/library/text/shaping.rs
@@ -23,9 +23,9 @@ pub struct ShapedText<'a> {
/// The font variant.
pub variant: FontVariant,
/// The font size.
- pub size: Length,
+ pub size: Abs,
/// The width of the text's bounding box.
- pub width: Length,
+ pub width: Abs,
/// The shaped glyphs.
pub glyphs: Cow<'a, [ShapedGlyph]>,
}
@@ -80,11 +80,11 @@ impl<'a> ShapedText<'a> {
///
/// The `justification` defines how much extra advance width each
/// [justifiable glyph](ShapedGlyph::is_justifiable) will get.
- pub fn build(&self, world: Tracked<dyn World>, justification: Length) -> Frame {
+ pub fn build(&self, world: Tracked<dyn World>, justification: Abs) -> Frame {
let (top, bottom) = self.measure(world);
let size = Size::new(self.width, top + bottom);
- let mut offset = Length::zero();
+ let mut offset = Abs::zero();
let mut frame = Frame::new(size);
frame.set_baseline(top);
@@ -144,9 +144,9 @@ impl<'a> ShapedText<'a> {
}
/// Measure the top and bottom extent of this text.
- fn measure(&self, world: Tracked<dyn World>) -> (Length, Length) {
- let mut top = Length::zero();
- let mut bottom = Length::zero();
+ fn measure(&self, world: Tracked<dyn World>) -> (Abs, Abs) {
+ let mut top = Abs::zero();
+ let mut bottom = Abs::zero();
let top_edge = self.styles.get(TextNode::TOP_EDGE);
let bottom_edge = self.styles.get(TextNode::BOTTOM_EDGE);
@@ -186,7 +186,7 @@ impl<'a> ShapedText<'a> {
}
/// The width of the spaces in the text.
- pub fn stretch(&self) -> Length {
+ pub fn stretch(&self) -> Abs {
self.glyphs
.iter()
.filter(|g| g.is_justifiable())
@@ -310,7 +310,7 @@ struct ShapingContext<'a> {
glyphs: Vec<ShapedGlyph>,
used: Vec<Font>,
styles: StyleChain<'a>,
- size: Length,
+ size: Abs,
variant: FontVariant,
tags: Vec<rustybuzz::Feature>,
fallback: bool,
diff --git a/src/library/text/shift.rs b/src/library/text/shift.rs
index 6a5415e8..adad5dc0 100644
--- a/src/library/text/shift.rs
+++ b/src/library/text/shift.rs
@@ -23,7 +23,7 @@ impl<const S: ScriptKind> ShiftNode<S> {
/// font.
pub const TYPOGRAPHIC: bool = true;
/// The baseline shift for synthetic sub- and superscripts.
- pub const BASELINE: RawLength =
+ pub const BASELINE: Length =
Em::new(if S == SUPERSCRIPT { -0.5 } else { 0.2 }).into();
/// The font size for synthetic sub- and superscripts.
pub const SIZE: TextSize = TextSize(Em::new(0.6).into());