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
//! The glyphs of a text run the renderer keeps between frames.
use std::rc::Rc;
use cranpose_render_common::{glyph_mask::GlyphMaskKey, text_atlas::AtlasGlyph};
use cranpose_ui_graphics::Color;
use crate::rc_slice_pool::RcSlicePool;
// The most face lists of recent runs kept for the next runs to share.
const RECENT_FACES: usize = 8;
/// What the glyphs of one segment of a run share: face, size and synthesis
/// (an atlas key but for its glyph id) and color.
#[derive(Clone, Copy, PartialEq)]
struct RunFace {
key: GlyphMaskKey,
color: Color,
}
impl RunFace {
fn of(glyph: &AtlasGlyph) -> Self {
Self {
key: GlyphMaskKey {
glyph_id: 0,
..glyph.key
},
color: glyph.color,
}
}
}
/// A glyph of a run: where it sits in the run's raster, its size, its
/// glyph id and the index of its face.
#[derive(Clone, Copy)]
struct RunGlyph {
x: i32,
y: i32,
width: u16,
height: u16,
glyph_id: u16,
face: u16,
}
impl RunGlyph {
fn of(glyph: &AtlasGlyph, face: usize) -> Option<Self> {
Some(Self {
x: glyph.x,
y: glyph.y,
width: u16::try_from(glyph.width).ok()?,
height: u16::try_from(glyph.height).ok()?,
glyph_id: u16::try_from(glyph.key.glyph_id).ok()?,
face: u16::try_from(face).ok()?,
})
}
}
/// Where a glyph of a run draws from the run's raster origin, and in what
/// color: what a quad drawn of it needs, without the atlas key it is looked
/// up by.
#[derive(Clone, Copy)]
pub(crate) struct RunGlyphBox {
pub(crate) x: i32,
pub(crate) y: i32,
pub(crate) width: u16,
pub(crate) height: u16,
pub(crate) color: Color,
}
/// A run's drawing glyphs, 16 bytes each where a placement takes 72: what
/// a segment's glyphs share is held once, as their face.
#[derive(Clone)]
pub(crate) struct RunGlyphs {
faces: Rc<[RunFace]>,
glyphs: Rc<[RunGlyph]>,
}
// Buffers a renderer reuses while it builds runs, the faces of recent runs,
// which the next run shares when its own are the same, and the glyph slices
// of runs the cache let go of.
#[derive(Default)]
pub(crate) struct RunGlyphScratch {
faces: Vec<RunFace>,
glyphs: Vec<RunGlyph>,
recent_faces: Vec<Rc<[RunFace]>>,
spare_glyphs: RcSlicePool<RunGlyph>,
}
impl RunGlyphScratch {
// The recent face list equal to the faces just collected, or a new one
// that joins the recent lists.
fn shared_faces(&mut self) -> Rc<[RunFace]> {
if let Some(faces) = self
.recent_faces
.iter()
.find(|faces| ***faces == *self.faces)
{
return Rc::clone(faces);
}
let faces: Rc<[RunFace]> = Rc::from(self.faces.as_slice());
if self.recent_faces.len() == RECENT_FACES {
self.recent_faces.remove(0);
}
self.recent_faces.push(Rc::clone(&faces));
faces
}
// Drops the glyph slices kept for later runs, and their room.
pub(crate) fn clear_spares(&mut self) {
self.spare_glyphs.clear();
}
}
impl RunGlyphs {
/// The run of `placements`, sharing the last run's faces when they are
/// the same, as consecutive texts in one style's are. `None` when a
/// glyph has no compact form: a glyph id or a mask side past 65535, or
/// as many faces.
pub(crate) fn of(
placements: impl Iterator<Item = AtlasGlyph>,
scratch: &mut RunGlyphScratch,
) -> Option<Self> {
scratch.faces.clear();
scratch.glyphs.clear();
for placement in placements {
let face = RunFace::of(&placement);
if scratch.faces.last() != Some(&face) {
scratch.faces.push(face);
}
let glyph = RunGlyph::of(&placement, scratch.faces.len() - 1)?;
scratch.glyphs.push(glyph);
}
Some(Self {
faces: scratch.shared_faces(),
glyphs: scratch.spare_glyphs.filled(&scratch.glyphs),
})
}
// Gives this run's glyph slice to `scratch` for a later run of its
// length, when nothing else holds it.
pub(crate) fn give_back(self, scratch: &mut RunGlyphScratch) {
scratch.spare_glyphs.give(self.glyphs);
}
/// Each glyph's box and color.
pub(crate) fn boxes(&self) -> impl ExactSizeIterator<Item = RunGlyphBox> + '_ {
self.glyphs.iter().map(|glyph| RunGlyphBox {
x: glyph.x,
y: glyph.y,
width: glyph.width,
height: glyph.height,
color: self.faces[usize::from(glyph.face)].color,
})
}
/// Each glyph as the placement it was made from.
pub(crate) fn iter(&self) -> impl ExactSizeIterator<Item = AtlasGlyph> + '_ {
self.glyphs.iter().map(|glyph| {
let face = self.faces[usize::from(glyph.face)];
AtlasGlyph {
key: GlyphMaskKey {
glyph_id: u32::from(glyph.glyph_id),
..face.key
},
x: glyph.x,
y: glyph.y,
width: usize::from(glyph.width),
height: usize::from(glyph.height),
color: face.color,
}
})
}
}
#[cfg(test)]
#[path = "tests/glyph_run_tests.rs"]
mod tests;