-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraster.cpp
More file actions
282 lines (225 loc) · 6.38 KB
/
Copy pathraster.cpp
File metadata and controls
282 lines (225 loc) · 6.38 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <algorithm>
#include <assert.h>
#include <stack>
#include <cmath>
#include "raster.h"
//
Edge::Edge(
const fPoint& p0,
const fPoint& p1,
const BoundingBox& pBB,
const float upem
) : is_active(false), m(0), c(0), sclx(0)
{
// classify the points into min & max.
if (p0.y > p1.y) {
apex.x = p0.x;
apex.y = p0.y;
base.x = p1.x;
base.y = p1.y;
}
else {
apex.x = p1.x;
apex.y = p1.y;
base.x = p0.x;
base.y = p0.y;
}
is_vertical = (apex.x == base.x);
// Translate the outline into the 1st quadrant.
apex.x -= pBB.xMin;
base.x -= pBB.xMin;
apex.y -= pBB.yMin;
base.y -= pBB.yMin;
// Scale points into bitmap space.
apex.x = DesignToRaster(apex.x, upem);
apex.y = DesignToRaster(apex.y, upem);
base.x = DesignToRaster(base.x, upem);
base.y = DesignToRaster(base.y, upem);
// Calculate gradient & intercept (for non-vertical edges).
if (!is_vertical) {
m = (apex.y - base.y) / (apex.x - base.x);
c = base.y - m * base.x;
}
}
//
void
EdgeTable::AddEdge(const fPoint& p0, const fPoint& p1)
{
edges.emplace_back(p0, p1, m_glyphDesc.bb, m_upem);
}
void
EdgeTable::AddBezier(const fPoint& p0, const fPoint& ctrl, const fPoint& p1)
{
const float kTolerance = 1.0f; // @todo: what value makes sense for this?
std::stack<Bezier> stack;
stack.emplace(p0, ctrl, p1);
while (!stack.empty()) {
const Bezier curr = stack.top(); // copy off stack top.
stack.pop();
// See: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line.
const auto [x0, y0] = curr.ctrl;
const auto [x1, y1] = curr.p0;
const auto [x2, y2] = curr.p1;
const float k = (y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1;
const float m = std::powf(y2 - y1, 2) + std::powf(x2 - x1, 2);
assert(m != 0.0f);
const float dist = std::fabsf(k) / std::sqrtf(m);
//
if (dist <= kTolerance) {
AddEdge(curr.p0, curr.p1);
continue;
}
// @todo: The divide gets rounded here, do not in font units!
auto m0x = 0.5f * (curr.p0.x + curr.ctrl.x);
auto m0y = 0.5f * (curr.p0.y + curr.ctrl.y);
fPoint m0(m0x, m0y);
auto m2x = 0.5f * (curr.ctrl.x + curr.p1.x);
auto m2y = 0.5f * (curr.ctrl.y + curr.p1.y);
fPoint m2(m2x, m2y);
auto m1x = 0.5f * (m0.x + m2.x);
auto m1y = 0.5f * (m0.y + m2.y);
fPoint m1(m1x, m1y);
stack.emplace(curr.p0, m0, m1);
stack.emplace(m1, m2, curr.p1);
}
}
float
DesignToRaster(const float value, const float upem)
{
const float pts = 12.0f;
const float dpi = 96.0f;
return (value / upem) * pts * dpi;
}
void
EdgeTable::Generate(GlyphMesh& pMesh)
{
for (auto& c : pMesh.contours) {
auto& flags = c.flags;
auto& xs = c.xs;
auto& ys = c.ys;
assert(OnCurve(flags[0])); // Assume the 1st contour point is on-curve.
// Create any inferred points.
for (size_t i = 0; i < flags.size() - 1; ++i) {
if (!OnCurve(flags[i]) && !OnCurve(flags[i + 1])) {
const float x = (xs.at(i) + xs.at(i + 1)) / 2.0f;
const float y = (ys.at(i) + ys.at(i + 1)) / 2.0f;
flags.insert(flags.begin() + i + 1, 0xff);
xs.insert(xs.begin() + i + 1, x);
ys.insert(ys.begin() + i + 1, y);
}
}
const auto& pointCount = flags.size();
std::vector<size_t> buff;
//
for (size_t k = 0; k <= pointCount; ++k) {
const auto idx = k % pointCount; // point index into the data buffers.
buff.push_back(idx);
switch (buff.size()) {
case 2:
{
const auto& slt0 = buff[0];
const auto& slt1 = buff[1];
if (OnCurve(flags[slt0]) && OnCurve(flags[slt1])) {
const fPoint p0(xs.at(slt0), ys.at(slt0));
const fPoint p1(xs.at(slt1), ys.at(slt1));
if (p0.y != p1.y) { // filter out horizontal edges
AddEdge(p0, p1);
}
buff[0] = buff[1];
buff.pop_back();
}
}
break;
case 3:
{
const auto& slt0 = buff[0];
const auto& slt1 = buff[1];
const auto& slt2 = buff[2];
if (OnCurve(flags[slt0]) && !OnCurve(flags[slt1]) && OnCurve(flags[slt2])) {
const fPoint p0(xs.at(slt0), ys.at(slt0));
const fPoint p1(xs.at(slt1), ys.at(slt1));
const fPoint p2(xs.at(slt2), ys.at(slt2));
AddBezier(p0, p1, p2);
buff[0] = buff[2];
buff.pop_back();
buff.pop_back();
}
}
break;
}
}
}
}
//
const RasterTarget*
RenderOutline(const GlyphDescription& pGlyphDesc, const float upem)
{
EdgeTable et(pGlyphDesc, upem);
// Allocate bitmap memory.
const auto xExtent = DesignToRaster(pGlyphDesc.bb.xMax - pGlyphDesc.bb.xMin, upem);
const auto yExtent = DesignToRaster(pGlyphDesc.bb.yMax - pGlyphDesc.bb.yMin, upem);
const size_t img_width = std::ceil(xExtent);
const size_t img_height = std::ceil(yExtent);
RasterTarget* target = new RasterTarget(img_width, img_height);
// Rasterise outline.
const float kScanlineDelta = 1.0f;
for (float scanline = 0.5f; scanline < target->height; scanline += kScanlineDelta) {
std::vector<float> crossings; // @perf: set capacity to avoid allocs?
for (auto& e : et.edges) {
if (e.is_active) {
if (e.apex.y <= scanline) {
// edge shouldn't be active anymore.
e.is_active = false;
}
else {
// edge is still active so update its intersection point.
if (!e.is_vertical) { // intersection only changes for non-vertical edges.
e.sclx += kScanlineDelta / e.m;
}
crossings.push_back(e.sclx);
}
}
else {
// Note: the scanline can never be on ymax in this
// codepath as the edge would be activated from it
// passing ymin.
if (scanline >= e.base.y && scanline < e.apex.y) {
// create new active edge.
e.is_active = true;
e.sclx = e.is_vertical ? e.base.x : (scanline - e.c) / e.m;
crossings.push_back(e.sclx);
}
}
}
assert(crossings.size() % 2 == 0);
std::sort(crossings.begin(), crossings.end());
for (size_t k = 0; k < crossings.size(); k += 2) {
const float xs = crossings[k];
const float xe = crossings[k + 1];
for (int x = xs; x <= xe; x++) {
target->store(x, (int)scanline, 0xff);
}
}
}
return target;
}
//
RasterTarget::RasterTarget(const size_t width, const size_t height)
: width(width), height(height), memory_(nullptr)
{
const auto imgSize = width * height;
memory_ = std::malloc(imgSize);
assert(memory_);
std::memset(memory_, 0, imgSize);
}
void
RasterTarget::store(const size_t x, const size_t y, const uint8_t colour)
{
((char*)memory_)[x + y * width] = colour;
}
uint8_t
RasterTarget::fetch(const size_t x, const size_t y) const
{
return ((uint8_t*)memory_)[y * width + x];
}
//