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
// Copyright 2021 the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Context for layout.
use alloc::{vec, vec::Vec};
use super::FontContext;
use super::builder::{RangedBuilder, StyleRunBuilder};
use super::resolve::tree::TreeStyleBuilder;
use super::resolve::{RangedStyleBuilder, ResolveContext, ResolvedStyle, StyleRun};
use super::style::{Brush, TextStyle};
use crate::analysis::{AnalysisDataSources, CharInfo};
use crate::bidi::BidiResolver;
use crate::builder::TreeBuilder;
use crate::inline_box::InlineBox;
use crate::shape::ShapeContext;
/// Shared scratch space used when constructing text layouts.
///
/// This type is designed to be a global resource with only one per-application (or per-thread).
pub struct LayoutContext<B: Brush = [u8; 4]> {
pub(crate) rcx: ResolveContext,
pub(crate) style_table: Vec<ResolvedStyle<B>>,
pub(crate) style_runs: Vec<StyleRun>,
pub(crate) inline_boxes: Vec<InlineBox>,
pub(crate) bidi: BidiResolver,
// Reusable style builders (to amortise allocations)
pub(crate) ranged_style_builder: RangedStyleBuilder<B>,
pub(crate) tree_style_builder: TreeStyleBuilder<B>,
// u16: style index for character
pub(crate) info: Vec<(CharInfo, u16)>,
pub(crate) scx: ShapeContext,
// Unicode analysis data sources (provided by icu)
pub(crate) analysis_data_sources: AnalysisDataSources,
}
impl<B: Brush> LayoutContext<B> {
pub fn new() -> Self {
Self {
rcx: ResolveContext::default(),
style_table: vec![],
style_runs: vec![],
inline_boxes: vec![],
bidi: BidiResolver::new(),
ranged_style_builder: RangedStyleBuilder::default(),
tree_style_builder: TreeStyleBuilder::default(),
info: vec![],
analysis_data_sources: AnalysisDataSources::new(),
scx: ShapeContext::default(),
}
}
fn resolve_style_set(
&mut self,
font_ctx: &mut FontContext,
scale: f32,
raw_style: &TextStyle<'_, '_, B>,
) -> ResolvedStyle<B> {
self.rcx
.resolve_entire_style_set(font_ctx, raw_style, scale)
}
/// Create a ranged style layout builder.
///
/// Set `quantize` as `true` to have the layout coordinates aligned to pixel boundaries.
/// That is the easiest way to avoid blurry text and to receive ready-to-paint layout metrics.
///
/// For advanced rendering use cases you can set `quantize` as `false` and receive
/// fractional coordinates. This ensures the most accurate results if you want to perform
/// some post-processing on the coordinates before painting. To avoid blurry text you will
/// still need to quantize the coordinates just before painting.
///
/// Your should round at least the following:
/// * Glyph run baseline
/// * Inline box baseline
/// - `box.y = (box.y + box.height).round() - box.height`
/// * Selection geometry's `y0` & `y1`
/// * Cursor geometry's `y0` & `y1`
///
/// Keep in mind that for the simple `f32::round` to be effective,
/// you need to first ensure the coordinates are in physical pixel space.
pub fn ranged_builder<'a>(
&'a mut self,
fcx: &'a mut FontContext,
text: &'a str,
scale: f32,
quantize: bool,
) -> RangedBuilder<'a, B> {
self.begin();
let resolved_root_style = self.resolve_style_set(fcx, scale, &TextStyle::default());
self.ranged_style_builder
.begin(resolved_root_style, text.len());
fcx.source_cache.prune(128, false);
RangedBuilder {
scale,
quantize,
lcx: self,
fcx,
}
}
/// Create a builder for constructing a layout from indexed style runs.
///
/// Unlike [`Self::ranged_builder`], this builder expects callers to provide:
/// - a style table of fully specified [`TextStyle`] values (via [`StyleRunBuilder::push_style`])
/// - a complete sequence of **contiguous**, **non-overlapping** spans that cover
/// `0..text.len()` and reference style indices (via [`StyleRunBuilder::push_style_run`])
///
/// Parley then skips its internal range-splitting logic.
pub fn style_run_builder<'a>(
&'a mut self,
fcx: &'a mut FontContext,
text: &'a str,
scale: f32,
quantize: bool,
) -> StyleRunBuilder<'a, B> {
self.begin();
fcx.source_cache.prune(128, false);
StyleRunBuilder {
scale,
quantize,
len: text.len(),
lcx: self,
fcx,
cursor: 0,
}
}
/// Create a tree style layout builder.
///
/// Set `quantize` as `true` to have the layout coordinates aligned to pixel boundaries.
/// That is the easiest way to avoid blurry text and to receive ready-to-paint layout metrics.
///
/// For advanced rendering use cases you can set `quantize` as `false` and receive
/// fractional coordinates. This ensures the most accurate results if you want to perform
/// some post-processing on the coordinates before painting. To avoid blurry text you will
/// still need to quantize the coordinates just before painting.
///
/// Your should round at least the following:
/// * Glyph run baseline
/// * Inline box baseline
/// - `box.y = (box.y + box.height).round() - box.height`
/// * Selection geometry's `y0` & `y1`
/// * Cursor geometry's `y0` & `y1`
///
/// Keep in mind that for the simple `f32::round` to be effective,
/// you need to first ensure the coordinates are in physical pixel space.
pub fn tree_builder<'a>(
&'a mut self,
fcx: &'a mut FontContext,
scale: f32,
quantize: bool,
root_style: &TextStyle<'_, '_, B>,
) -> TreeBuilder<'a, B> {
self.begin();
let resolved_root_style = self.resolve_style_set(fcx, scale, root_style);
self.tree_style_builder.begin(resolved_root_style);
fcx.source_cache.prune(128, false);
TreeBuilder {
scale,
quantize,
lcx: self,
fcx,
}
}
fn begin(&mut self) {
self.rcx.clear();
self.style_table.clear();
self.style_runs.clear();
self.inline_boxes.clear();
self.info.clear();
self.bidi.clear();
}
}
impl<B: Brush> Default for LayoutContext<B> {
fn default() -> Self {
Self::new()
}
}
impl<B: Brush> Clone for LayoutContext<B> {
fn clone(&self) -> Self {
// None of the internal state is visible so just return a new instance.
Self::new()
}
}