Skip to main content

cranpose_render_common/
text_glyphs.rs

1//! The glyphs of a laid-out text placed on a renderer's pixels, each with
2//! the paint of the span it draws.
3
4use std::ops::Range;
5
6use cranpose_text::{FontSynthesis, GlyphId, TextLayout, Typeface};
7use cranpose_ui::{
8    Brush, Color,
9    text::{
10        FontStyle, FontWeight, RangeStyle, RenderString, Shadow, SpanBoundaries, SpanStyle,
11        TextDrawStyle, TextMotion, TextStyle,
12    },
13};
14use cranpose_ui_graphics::{Point, whole};
15use smallvec::SmallVec;
16
17use crate::{glyph_mask::GlyphRaster, layer_paint::LayerPaint, text_raster::TextRasterRequest};
18
19/// A text and the styles of its ranges.
20#[derive(Clone, Copy, Debug)]
21pub struct StyledTextRef<'a> {
22    /// The text.
23    pub text: &'a str,
24    /// Styles of byte ranges of `text`.
25    pub span_styles: &'a [RangeStyle<SpanStyle>],
26}
27
28impl StyledTextRef<'_> {
29    pub(crate) fn is_empty(&self) -> bool {
30        self.text.is_empty()
31    }
32
33    // The offsets where a span starts or ends, with the text's ends.
34    fn span_boundaries(&self) -> SpanBoundaries {
35        let mut boundaries: SpanBoundaries = smallvec::smallvec![0, self.text.len()];
36        for span in self.span_styles {
37            boundaries.push(span.range.start);
38            boundaries.push(span.range.end);
39        }
40        boundaries.sort_unstable();
41        boundaries.dedup();
42        boundaries.retain(|at| *at <= self.text.len() && self.text.is_char_boundary(*at));
43        boundaries
44    }
45}
46
47impl<'a> From<&'a str> for StyledTextRef<'a> {
48    fn from(text: &'a str) -> Self {
49        Self {
50            text,
51            span_styles: &[],
52        }
53    }
54}
55
56impl<'a> From<&'a cranpose_ui::text::AnnotatedString> for StyledTextRef<'a> {
57    fn from(text: &'a cranpose_ui::text::AnnotatedString) -> Self {
58        Self {
59            text: text.text(),
60            span_styles: text.span_styles(),
61        }
62    }
63}
64
65impl<'a> From<&'a RenderString> for StyledTextRef<'a> {
66    fn from(text: &'a RenderString) -> Self {
67        Self {
68            text: text.text(),
69            span_styles: text.span_styles(),
70        }
71    }
72}
73
74/// Calls `visit` with each stretch of `text` between its span boundaries
75/// and the span styles over it merged over `style`'s, once for an empty
76/// text.
77pub(crate) fn visit_span_styles(
78    text: StyledTextRef<'_>,
79    style: &TextStyle,
80    mut visit: impl FnMut(Range<usize>, &SpanStyle),
81) {
82    if text.span_styles.is_empty() || text.text.is_empty() {
83        visit(0..text.text.len(), &style.span_style);
84        return;
85    }
86    for edges in text.span_boundaries().windows(2) {
87        let range = edges[0]..edges[1];
88        let merged = text
89            .span_styles
90            .iter()
91            .filter(|span| span.range.start <= range.start && range.end <= span.range.end)
92            .fold(style.span_style.clone(), |merged, span| {
93                merged.merge(&span.item)
94            });
95        visit(range, &merged);
96    }
97}
98
99/// What a span of a text paints its glyphs with.
100#[derive(Clone, Debug, PartialEq)]
101pub(crate) struct SpanPaint {
102    pub(crate) brush: Brush,
103    // What the brush's alpha is multiplied by.
104    pub(crate) alpha: f32,
105    pub(crate) shadow: Option<Shadow>,
106    // The stroke width of stroked text, in layout pixels.
107    pub(crate) stroke: Option<f32>,
108    weight: FontWeight,
109    style: FontStyle,
110    synthesis: FontSynthesis,
111}
112
113impl SpanPaint {
114    // The paint of a span in `style`, glyphs whose style names no color in
115    // `fallback`.
116    pub(crate) fn of(style: &SpanStyle, fallback: Color) -> Self {
117        let (brush, alpha) = match &style.brush {
118            Some(brush) => (brush.clone(), style.alpha.unwrap_or(1.0).clamp(0.0, 1.0)),
119            None => (Brush::solid(style.resolve_foreground_color(fallback)), 1.0),
120        };
121        Self {
122            brush,
123            alpha,
124            shadow: style.shadow.filter(|shadow| shadow.color.3 > 0.0),
125            stroke: match style.draw_style {
126                Some(TextDrawStyle::Stroke { width }) if width.is_finite() && width > 0.0 => {
127                    Some(width)
128                }
129                _ => None,
130            },
131            weight: style.font_weight.unwrap_or_default(),
132            style: style.font_style.unwrap_or_default(),
133            synthesis: style.font_synthesis.unwrap_or(FontSynthesis::All),
134        }
135    }
136
137    // The paint with its brush and shadow under `layer_paint`.
138    fn painted(self, layer_paint: &LayerPaint) -> Self {
139        if !layer_paint.paints() {
140            return self;
141        }
142        Self {
143            brush: layer_paint.brush(self.brush),
144            shadow: self
145                .shadow
146                .map(|shadow| Shadow {
147                    color: layer_paint.color(shadow.color),
148                    ..shadow
149                })
150                .filter(|shadow| shadow.color.3 > 0.0),
151            ..self
152        }
153    }
154
155    // The color of a solid paint, `None` for a gradient.
156    pub(crate) fn solid_color(&self) -> Option<Color> {
157        match self.brush {
158            Brush::Solid(color) => Some(Color(color.0, color.1, color.2, color.3 * self.alpha)),
159            _ => None,
160        }
161    }
162
163    // How a glyph of `typeface` at `size` device pixels per em paints with
164    // this paint at `scale` device pixels a layout pixel: stroked, and with
165    // the bold and italic Compose synthesizes for a face that lacks them.
166    pub(crate) fn raster(&self, typeface: &Typeface, size: f32, scale: f32) -> GlyphRaster {
167        let synthesis = typeface.synthesis(self.weight, self.style, self.synthesis);
168        GlyphRaster {
169            stroke: self.stroke.map(|width| width * scale),
170            embolden: if synthesis.bold {
171                size * fake_bold_ratio(size)
172            } else {
173                0.0
174            },
175            skew: if synthesis.italic {
176                FAKE_ITALIC_SKEW
177            } else {
178                0.0
179            },
180        }
181    }
182}
183
184// Skia's fake italic: Android's `setTextSkewX(-0.25)`.
185pub(crate) const FAKE_ITALIC_SKEW: f32 = 0.25;
186
187// The widest Skia's fake bold strokes a glyph, as a share of its size: its
188// share at 9 pixels and below.
189pub(crate) const FAKE_BOLD_MAX_RATIO: f32 = 1.0 / 24.0;
190
191// How wide Skia's fake bold strokes a glyph, as a share of its size: 1/24
192// at 9 pixels to 1/32 at 36, constant past either end.
193fn fake_bold_ratio(size: f32) -> f32 {
194    let along = ((size - 9.0) / 27.0).clamp(0.0, 1.0);
195    FAKE_BOLD_MAX_RATIO + along * (1.0 / 32.0 - FAKE_BOLD_MAX_RATIO)
196}
197
198/// The paints of a text's spans, by the text they cover.
199#[derive(Default)]
200pub(crate) struct SpanPaints {
201    paints: SmallVec<[(Range<usize>, SpanPaint); 2]>,
202}
203
204impl SpanPaints {
205    // Holds the paints of `request`'s text in its style, a span's style over
206    // the text's, under the request's layer paint. The caller's list fills in
207    // place: one built here would move out whole, as the visit borrows it.
208    pub(crate) fn fill(&mut self, request: TextRasterRequest<'_>) {
209        let paints = &mut self.paints;
210        paints.clear();
211        visit_span_styles(request.text, request.style, |range, merged| {
212            let paint = SpanPaint::of(merged, request.fallback_color).painted(request.layer_paint);
213            match paints.last_mut() {
214                Some((last, last_paint)) if *last_paint == paint => last.end = range.end,
215                _ => paints.push((range, paint)),
216            }
217        });
218    }
219
220    // The span of the character at `offset` and its paint.
221    pub(crate) fn span_at(&self, offset: usize) -> (&Range<usize>, &SpanPaint) {
222        let at = self
223            .paints
224            .partition_point(|(range, _)| range.end <= offset)
225            .min(self.paints.len() - 1);
226        let (range, paint) = &self.paints[at];
227        (range, paint)
228    }
229
230    // Whether every span paints with `test` true.
231    pub(crate) fn all(&self, test: impl Fn(&SpanPaint) -> bool) -> bool {
232        self.paints.iter().all(|(_, paint)| test(paint))
233    }
234}
235
236/// A glyph of a text placed on the device's pixels.
237pub(crate) struct PlacedGlyph<'a> {
238    pub(crate) typeface: &'a Typeface,
239    pub(crate) glyph: GlyphId,
240    // Device pixels per em.
241    pub(crate) size: f32,
242    pub(crate) raster: GlyphRaster,
243    // The glyph's origin on the device's pixels: whole pixels for a static
244    // text, whole pixels from `origin` for one in motion.
245    pub(crate) x: f32,
246    pub(crate) y: f32,
247    pub(crate) paint: &'a SpanPaint,
248}
249
250/// Calls `visit` with each glyph `layout` draws, its top left corner at
251/// `origin` in device pixels, `scale` device pixels a layout pixel, in the
252/// paint `paints` give it.
253pub(crate) fn visit_placed_glyphs<'a>(
254    layout: &'a TextLayout,
255    paints: &'a SpanPaints,
256    (origin, scale): (Point, f32),
257    motion: TextMotion,
258    mut visit: impl FnMut(PlacedGlyph<'a>),
259) {
260    let place = |value: f32, from: f32| match motion {
261        TextMotion::Static => whole::round(from + value * scale),
262        TextMotion::Animated => from + whole::round(value * scale),
263    };
264    for line in 0..layout.line_count() {
265        for run in layout.glyph_runs(line) {
266            let size = run.size * scale;
267            // The span of the glyph before, its paint and its raster: a run's
268            // glyphs mostly share them.
269            let mut span: Option<(&Range<usize>, &SpanPaint, GlyphRaster)> = None;
270            for glyph in run.positions() {
271                let (_, paint, raster) = match span {
272                    Some(found) if found.0.contains(&glyph.offset) => found,
273                    _ => {
274                        let (range, paint) = paints.span_at(glyph.offset);
275                        *span.insert((range, paint, paint.raster(run.typeface, size, scale)))
276                    }
277                };
278                visit(PlacedGlyph {
279                    typeface: run.typeface,
280                    glyph: glyph.id,
281                    size,
282                    raster,
283                    x: place(glyph.x, origin.x),
284                    y: place(glyph.y, origin.y),
285                    paint,
286                });
287            }
288        }
289    }
290}