summaryrefslogtreecommitdiff
path: root/src/library/text/deco.rs
blob: 52f8ea80c819830bcca1042c25a3cbe60fbe3c67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use kurbo::{BezPath, Line, ParamCurve};
use ttf_parser::{GlyphId, OutlineBuilder};

use super::TextNode;
use crate::font::FontStore;
use crate::library::prelude::*;

/// Typeset underline, stricken-through or overlined text.
#[derive(Debug, Hash)]
pub struct DecoNode<const L: DecoLine>(pub Content);

/// Typeset underlined text.
pub type UnderlineNode = DecoNode<UNDERLINE>;

/// Typeset stricken-through text.
pub type StrikethroughNode = DecoNode<STRIKETHROUGH>;

/// Typeset overlined text.
pub type OverlineNode = DecoNode<OVERLINE>;

#[node(showable)]
impl<const L: DecoLine> DecoNode<L> {
    /// How to stroke the line. The text color and thickness read from the font
    /// tables if `auto`.
    #[property(shorthand, resolve, fold)]
    pub const STROKE: Smart<RawStroke> = Smart::Auto;

    /// Position of the line relative to the baseline, read from the font tables
    /// if `auto`.
    #[property(resolve)]
    pub const OFFSET: Smart<RawLength> = Smart::Auto;
    /// Amount that the line will be longer or shorter than its associated text.
    #[property(resolve)]
    pub const EXTENT: RawLength = RawLength::zero();

    /// Whether the line skips sections in which it would collide
    /// with the glyphs. Does not apply to strikethrough.
    pub const EVADE: bool = true;

    fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> {
        Ok(Content::show(Self(args.expect::<Content>("body")?)))
    }
}

impl<const L: DecoLine> Show for DecoNode<L> {
    fn encode(&self) -> Dict {
        dict! { "body" => Value::Content(self.0.clone()) }
    }

    fn realize(&self, _: &mut Context, styles: StyleChain) -> TypResult<Content> {
        Ok(self.0.clone().styled(TextNode::DECO, Decoration {
            line: L,
            stroke: styles.get(Self::STROKE).unwrap_or_default(),
            offset: styles.get(Self::OFFSET),
            extent: styles.get(Self::EXTENT),
            evade: styles.get(Self::EVADE),
        }))
    }
}

/// Defines a line that is positioned over, under or on top of text.
///
/// For more details, see [`DecoNode`].
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Decoration {
    pub line: DecoLine,
    pub stroke: RawStroke<Length>,
    pub offset: Smart<Length>,
    pub extent: Length,
    pub evade: bool,
}

/// A kind of decorative line.
pub type DecoLine = usize;

/// A line under text.
pub const UNDERLINE: DecoLine = 0;

/// A line through text.
pub const STRIKETHROUGH: DecoLine = 1;

/// A line over text.
pub const OVERLINE: DecoLine = 2;

/// Add line decorations to a single run of shaped text.
pub fn decorate(
    frame: &mut Frame,
    deco: &Decoration,
    fonts: &FontStore,
    text: &Text,
    pos: Point,
    width: Length,
) {
    let face = fonts.get(text.face_id);
    let face_metrics = face.metrics();
    let metrics = match deco.line {
        STRIKETHROUGH => face_metrics.strikethrough,
        OVERLINE => face_metrics.overline,
        UNDERLINE | _ => face_metrics.underline,
    };

    let evade = deco.evade && deco.line != STRIKETHROUGH;
    let offset = deco.offset.unwrap_or(-metrics.position.at(text.size));
    let stroke = deco.stroke.unwrap_or(Stroke {
        paint: text.fill,
        thickness: metrics.thickness.at(text.size),
    });

    let gap_padding = 0.08 * text.size;
    let min_width = 0.162 * text.size;

    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 origin = Point::new(from, pos.y + offset);
        let target = Point::new(to - from, Length::zero());

        if target.x >= min_width || !evade {
            let shape = Geometry::Line(target).stroked(stroke);
            frame.push(origin, Element::Shape(shape));
        }
    };

    if !evade {
        push_segment(start, end);
        return;
    }

    let line = Line::new(
        kurbo::Point::new(pos.x.to_raw(), offset.to_raw()),
        kurbo::Point::new((pos.x + width).to_raw(), offset.to_raw()),
    );

    let mut x = pos.x;
    let mut intersections = vec![];

    for glyph in text.glyphs.iter() {
        let dx = glyph.x_offset.at(text.size) + x;
        let mut builder =
            BezPathBuilder::new(face_metrics.units_per_em, text.size, dx.to_raw());

        let bbox = face.ttf().outline_glyph(GlyphId(glyph.id), &mut builder);
        let path = builder.finish();

        x += glyph.x_advance.at(text.size);

        // Only do the costly segments intersection test if the line
        // intersects the bounding box.
        if bbox.map_or(false, |bbox| {
            let y_min = -face.to_em(bbox.y_max).at(text.size);
            let y_max = -face.to_em(bbox.y_min).at(text.size);

            offset >= y_min && offset <= y_max
        }) {
            // Find all intersections of segments with the line.
            intersections.extend(
                path.segments()
                    .flat_map(|seg| seg.intersect_line(line))
                    .map(|is| Length::raw(line.eval(is.line_t).x)),
            );
        }
    }

    // When emitting the decorative line segments, we move from left to
    // right. The intersections are not necessarily in this order, yet.
    intersections.sort();

    for gap in intersections.chunks_exact(2) {
        let l = gap[0] - gap_padding;
        let r = gap[1] + gap_padding;

        if start >= end {
            break;
        }

        if start >= l {
            start = r;
            continue;
        }

        push_segment(start, l);
        start = r;
    }

    if start < end {
        push_segment(start, end);
    }
}

/// Builds a kurbo [`BezPath`] for a glyph.
struct BezPathBuilder {
    path: BezPath,
    units_per_em: f64,
    font_size: Length,
    x_offset: f64,
}

impl BezPathBuilder {
    fn new(units_per_em: f64, font_size: Length, x_offset: f64) -> Self {
        Self {
            path: BezPath::new(),
            units_per_em,
            font_size,
            x_offset,
        }
    }

    fn finish(self) -> BezPath {
        self.path
    }

    fn p(&self, x: f32, y: f32) -> kurbo::Point {
        kurbo::Point::new(self.s(x) + self.x_offset, -self.s(y))
    }

    fn s(&self, v: f32) -> f64 {
        Em::from_units(v, self.units_per_em).at(self.font_size).to_raw()
    }
}

impl OutlineBuilder for BezPathBuilder {
    fn move_to(&mut self, x: f32, y: f32) {
        self.path.move_to(self.p(x, y));
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.path.line_to(self.p(x, y));
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.path.quad_to(self.p(x1, y1), self.p(x, y));
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.path.curve_to(self.p(x1, y1), self.p(x2, y2), self.p(x, y));
    }

    fn close(&mut self) {
        self.path.close_path();
    }
}