Skip to main content

usvg/text/
flatten.rs

1// Copyright 2022 the Resvg Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use std::mem;
5use std::sync::Arc;
6
7use tiny_skia_path::{NonZeroRect, Transform};
8
9use crate::text::cache::{FontCache, with_optical_sizing};
10use crate::*;
11
12fn resolve_rendering_mode(text: &Text) -> ShapeRendering {
13    match text.rendering_mode {
14        TextRendering::OptimizeSpeed => ShapeRendering::CrispEdges,
15        TextRendering::OptimizeLegibility => ShapeRendering::GeometricPrecision,
16        TextRendering::GeometricPrecision => ShapeRendering::GeometricPrecision,
17    }
18}
19
20fn push_outline_paths(
21    span: &layout::Span,
22    builder: &mut tiny_skia_path::PathBuilder,
23    new_children: &mut Vec<Node>,
24    rendering_mode: ShapeRendering,
25    abs_transform: Transform,
26) {
27    let builder = mem::replace(builder, tiny_skia_path::PathBuilder::new());
28
29    if let Some(data) = builder.finish() {
30        let path = Path::new(
31            Box::default(),
32            span.visible,
33            span.fill.clone(),
34            span.stroke.clone(),
35            span.paint_order,
36            rendering_mode,
37            Arc::new(data),
38            abs_transform,
39        );
40        new_children.push(Node::Path(Box::new(path)));
41    }
42}
43
44pub(crate) fn flatten(text: &mut Text, fonts: &mut FontCache) -> Option<(Group, NonZeroRect)> {
45    let mut new_children = vec![];
46
47    let abs_transform = text.abs_transform;
48    let rendering_mode = resolve_rendering_mode(text);
49
50    for span in &text.layouted {
51        if let Some(path) = span.overline.as_ref() {
52            let mut path = path.clone();
53            path.rendering_mode = rendering_mode;
54            new_children.push(Node::Path(path));
55        }
56
57        if let Some(path) = span.underline.as_ref() {
58            let mut path = path.clone();
59            path.rendering_mode = rendering_mode;
60            new_children.push(Node::Path(path));
61        }
62
63        // Instead of always processing each glyph separately, we always collect
64        // as many outline glyphs as possible by pushing them into the span_builder
65        // and only if we encounter a different glyph, or we reach the very end of the
66        // span to we push the actual outline paths into new_children. This way, we don't need
67        // to create a new path for every glyph if we have many consecutive glyphs
68        // with just outlines (which is the most common case).
69        let mut span_builder = tiny_skia_path::PathBuilder::new();
70
71        let variations = with_optical_sizing(
72            &span.variations,
73            span.font_optical_sizing,
74            span.font_size.get(),
75        );
76
77        for glyph in &span.positioned_glyphs {
78            // A (best-effort conversion of a) COLR glyph.
79            if let Some(tree) = fonts.colr(glyph.font, glyph.id, variations.clone()) {
80                let mut group = Group {
81                    transform: glyph.colr_transform(),
82                    ..Group::empty()
83                };
84                // TODO: Probably need to update abs_transform of children? Same
85                // for SVG and bitmap glyphs.
86                group.children.push(Node::Group(Box::new(tree.root)));
87
88                new_children.push(Node::Group(Box::new(group)));
89            }
90            // An SVG glyph. Will return the usvg node containing the glyph descriptions.
91            else if let Some(node) = fonts.svg(glyph.font, glyph.id) {
92                push_outline_paths(
93                    span,
94                    &mut span_builder,
95                    &mut new_children,
96                    rendering_mode,
97                    abs_transform,
98                );
99
100                let mut group = Group {
101                    transform: glyph.svg_transform(),
102                    ..Group::empty()
103                };
104                group.children.push(node);
105
106                new_children.push(Node::Group(Box::new(group)));
107            }
108            // A bitmap glyph.
109            else if let Some(img) = fonts.raster(glyph.font, glyph.id) {
110                push_outline_paths(
111                    span,
112                    &mut span_builder,
113                    &mut new_children,
114                    rendering_mode,
115                    abs_transform,
116                );
117
118                let transform = if img.is_sbix {
119                    glyph.sbix_transform(
120                        img.x as f32,
121                        img.y as f32,
122                        img.bearing_x,
123                        img.bearing_y,
124                        img.pixels_per_em as f32,
125                        img.image.size.height(),
126                    )
127                } else {
128                    glyph.cbdt_transform(img.x as f32, img.y as f32, img.pixels_per_em as f32)
129                };
130
131                let mut group = Group {
132                    transform,
133                    ..Group::empty()
134                };
135                group.children.push(Node::Image(Box::new(img.image)));
136
137                new_children.push(Node::Group(Box::new(group)));
138            } else {
139                let outline = fonts.outline(glyph.font, glyph.id, variations.clone());
140
141                if let Some(outline) = outline.and_then(|p| p.transform(glyph.outline_transform()))
142                {
143                    span_builder.push_path(&outline);
144                }
145            }
146        }
147
148        push_outline_paths(
149            span,
150            &mut span_builder,
151            &mut new_children,
152            rendering_mode,
153            abs_transform,
154        );
155
156        if let Some(path) = span.line_through.as_ref() {
157            let mut path = path.clone();
158            path.rendering_mode = rendering_mode;
159            new_children.push(Node::Path(path));
160        }
161    }
162
163    let mut group = Group {
164        id: text.id.clone(),
165        ..Group::empty()
166    };
167
168    for child in new_children {
169        group.children.push(child);
170    }
171
172    let stroke_bbox = group.stroke_bounding_box()?.to_non_zero_rect()?;
173    Some((group, stroke_bbox))
174}