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
#[cfg(all(feature = "libm", not(feature = "std")))]
use crate::nostd_float::FloatExt;
use crate::{point, Glyph, Point, PxScaleFactor};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[derive(Clone, Debug)]
pub struct Outline {
pub bounds: Rect,
pub curves: Vec<OutlineCurve>,
}
impl Outline {
pub fn px_bounds(&self, scale_factor: PxScaleFactor, position: Point) -> Rect {
let Rect { min, max } = self.bounds;
let (x_trunc, x_fract) = (position.x.trunc(), position.x.fract());
let (y_trunc, y_fract) = (position.y.trunc(), position.y.fract());
Rect {
min: point(
(min.x * scale_factor.horizontal + x_fract).floor() + x_trunc,
(min.y * -scale_factor.vertical + y_fract).floor() + y_trunc,
),
max: point(
(max.x * scale_factor.horizontal + x_fract).ceil() + x_trunc,
(max.y * -scale_factor.vertical + y_fract).ceil() + y_trunc,
),
}
}
}
#[derive(Clone, Debug)]
pub struct OutlinedGlyph {
glyph: Glyph,
px_bounds: Rect,
scale_factor: PxScaleFactor,
outline: Outline,
}
impl OutlinedGlyph {
#[inline]
pub fn new(glyph: Glyph, outline: Outline, scale_factor: PxScaleFactor) -> Self {
let px_bounds = outline.px_bounds(scale_factor, glyph.position);
Self {
glyph,
px_bounds,
scale_factor,
outline,
}
}
#[inline]
pub fn glyph(&self) -> &Glyph {
&self.glyph
}
#[deprecated = "Renamed to `px_bounds`"]
#[doc(hidden)]
pub fn bounds(&self) -> Rect {
self.px_bounds()
}
#[inline]
pub fn px_bounds(&self) -> Rect {
self.px_bounds
}
pub fn draw<O: FnMut(u32, u32, f32)>(&self, o: O) {
use ab_glyph_rasterizer::Rasterizer;
let h_factor = self.scale_factor.horizontal;
let v_factor = -self.scale_factor.vertical;
let offset = self.glyph.position - self.px_bounds.min;
let (w, h) = (
self.px_bounds.width() as usize,
self.px_bounds.height() as usize,
);
let scale_up = |&Point { x, y }| point(x * h_factor, y * v_factor);
self.outline
.curves
.iter()
.fold(Rasterizer::new(w, h), |mut rasterizer, curve| match curve {
OutlineCurve::Line(p0, p1) => {
rasterizer.draw_line(scale_up(p0) + offset, scale_up(p1) + offset);
rasterizer
}
OutlineCurve::Quad(p0, p1, p2) => {
rasterizer.draw_quad(
scale_up(p0) + offset,
scale_up(p1) + offset,
scale_up(p2) + offset,
);
rasterizer
}
OutlineCurve::Cubic(p0, p1, p2, p3) => {
rasterizer.draw_cubic(
scale_up(p0) + offset,
scale_up(p1) + offset,
scale_up(p2) + offset,
scale_up(p3) + offset,
);
rasterizer
}
})
.for_each_pixel_2d(o);
}
}
impl AsRef<Glyph> for OutlinedGlyph {
#[inline]
fn as_ref(&self) -> &Glyph {
self.glyph()
}
}
#[derive(Clone, Debug)]
pub enum OutlineCurve {
Line(Point, Point),
Quad(Point, Point, Point),
Cubic(Point, Point, Point, Point),
}
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct Rect {
pub min: Point,
pub max: Point,
}
impl Rect {
#[inline]
pub fn width(&self) -> f32 {
self.max.x - self.min.x
}
#[inline]
pub fn height(&self) -> f32 {
self.max.y - self.min.y
}
}