Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdistancefield.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <qmath.h>
6#include <QtGui/qimagereader.h>
7#include <private/qdatabuffer_p.h>
8#include <private/qpathsimplifier_p.h>
9
11
12using namespace Qt::StringLiterals;
13
14Q_STATIC_LOGGING_CATEGORY(lcDistanceField, "qt.distanceField");
15
16namespace
17{
18 enum FillHDir
19 {
20 LeftToRight,
21 RightToLeft
22 };
23
24 enum FillVDir
25 {
26 TopDown,
27 BottomUp
28 };
29
30 enum FillClip
31 {
32 NoClip,
33 Clip
34 };
35}
36
37template <FillClip clip, FillHDir dir>
38inline void fillLine(qint32 *, int, int, int, qint32, qint32)
39{
40}
41
42template <>
43inline void fillLine<Clip, LeftToRight>(qint32 *line, int width, int lx, int rx, qint32 d, qint32 dd)
44{
45 int fromX = qMax(0, lx >> 8);
46 int toX = qMin(width, rx >> 8);
47 int x = toX - fromX;
48 if (x <= 0)
49 return;
50 qint32 val = d + (((fromX << 8) + 0xff - lx) * dd >> 8);
51 line += fromX;
52 do {
53 *line = abs(val) < abs(*line) ? val : *line;
54 val += dd;
55 ++line;
56 } while (--x);
57}
58
59template <>
60inline void fillLine<Clip, RightToLeft>(qint32 *line, int width, int lx, int rx, qint32 d, qint32 dd)
61{
62 int fromX = qMax(0, lx >> 8);
63 int toX = qMin(width, rx >> 8);
64 int x = toX - fromX;
65 if (x <= 0)
66 return;
67 qint32 val = d + (((toX << 8) + 0xff - rx) * dd >> 8);
68 line += toX;
69 do {
70 val -= dd;
71 --line;
72 *line = abs(val) < abs(*line) ? val : *line;
73 } while (--x);
74}
75
76template <>
77inline void fillLine<NoClip, LeftToRight>(qint32 *line, int, int lx, int rx, qint32 d, qint32 dd)
78{
79 int fromX = lx >> 8;
80 int toX = rx >> 8;
81 int x = toX - fromX;
82 if (x <= 0)
83 return;
84 qint32 val = d + ((~lx & 0xff) * dd >> 8);
85 line += fromX;
86 do {
87 *line = abs(val) < abs(*line) ? val : *line;
88 val += dd;
89 ++line;
90 } while (--x);
91}
92
93template <>
94inline void fillLine<NoClip, RightToLeft>(qint32 *line, int, int lx, int rx, qint32 d, qint32 dd)
95{
96 int fromX = lx >> 8;
97 int toX = rx >> 8;
98 int x = toX - fromX;
99 if (x <= 0)
100 return;
101 qint32 val = d + ((~rx & 0xff) * dd >> 8);
102 line += toX;
103 do {
104 val -= dd;
105 --line;
106 *line = abs(val) < abs(*line) ? val : *line;
107 } while (--x);
108}
109
110template <FillClip clip, FillVDir vDir, FillHDir hDir>
111inline void fillLines(qint32 *bits, int width, int height, int upperY, int lowerY,
112 int &lx, int ldx, int &rx, int rdx, qint32 &d, qint32 ddy, qint32 ddx)
113{
114 Q_UNUSED(height);
115 Q_ASSERT(upperY < lowerY);
116 int y = lowerY - upperY;
117 if (vDir == TopDown) {
118 qint32 *line = bits + upperY * width;
119 do {
120 fillLine<clip, hDir>(line, width, lx, rx, d, ddx);
121 lx += ldx;
122 d += ddy;
123 rx += rdx;
124 line += width;
125 } while (--y);
126 } else {
127 qint32 *line = bits + lowerY * width;
128 do {
129 lx -= ldx;
130 d -= ddy;
131 rx -= rdx;
132 line -= width;
133 fillLine<clip, hDir>(line, width, lx, rx, d, ddx);
134 } while (--y);
135 }
136}
137
138template <FillClip clip>
139void drawTriangle(qint32 *bits, int width, int height, const QPoint *center,
140 const QPoint *v1, const QPoint *v2, qint32 value)
141{
142 const int y1 = clip == Clip ? qBound(0, v1->y() >> 8, height) : v1->y() >> 8;
143 const int y2 = clip == Clip ? qBound(0, v2->y() >> 8, height) : v2->y() >> 8;
144 const int yC = clip == Clip ? qBound(0, center->y() >> 8, height) : center->y() >> 8;
145
146 const int v1Frac = clip == Clip ? (y1 << 8) + 0xff - v1->y() : ~v1->y() & 0xff;
147 const int v2Frac = clip == Clip ? (y2 << 8) + 0xff - v2->y() : ~v2->y() & 0xff;
148 const int centerFrac = clip == Clip ? (yC << 8) + 0xff - center->y() : ~center->y() & 0xff;
149
150 int dx1 = 0, x1 = 0, dx2 = 0, x2 = 0;
151 qint32 dd1, d1, dd2, d2;
152 if (v1->y() != center->y()) {
153 dx1 = ((v1->x() - center->x()) << 8) / (v1->y() - center->y());
154 x1 = center->x() + centerFrac * (v1->x() - center->x()) / (v1->y() - center->y());
155 }
156 if (v2->y() != center->y()) {
157 dx2 = ((v2->x() - center->x()) << 8) / (v2->y() - center->y());
158 x2 = center->x() + centerFrac * (v2->x() - center->x()) / (v2->y() - center->y());
159 }
160
161 const qint32 div = (v2->x() - center->x()) * (v1->y() - center->y())
162 - (v2->y() - center->y()) * (v1->x() - center->x());
163 const qint32 dd = div ? qint32((qint64(value * (v1->y() - v2->y())) << 8) / div) : 0;
164
165 if (y2 < yC) {
166 if (y1 < yC) {
167 // Center at the bottom.
168 if (y2 < y1) {
169 // y2 < y1 < yC
170 // Long right edge.
171 d1 = centerFrac * value / (v1->y() - center->y());
172 dd1 = ((value << 8) / (v1->y() - center->y()));
173 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y1, yC, x1, dx1,
174 x2, dx2, d1, dd1, dd);
175 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
176 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
177 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, y1, x1, dx1,
178 x2, dx2, value, 0, dd);
179 } else {
180 // y1 <= y2 < yC
181 // Long left edge.
182 d2 = centerFrac * value / (v2->y() - center->y());
183 dd2 = ((value << 8) / (v2->y() - center->y()));
184 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y2, yC, x1, dx1,
185 x2, dx2, d2, dd2, dd);
186 if (y1 != y2) {
187 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
188 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
189 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, y2, x1, dx1,
190 x2, dx2, value, 0, dd);
191 }
192 }
193 } else {
194 // y2 < yC <= y1
195 // Center to the right.
196 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
197 int xUp, xDn;
198 xUp = xDn = v2->x() + (clip == Clip ? (yC << 8) + 0xff - v2->y()
199 : (center->y() | 0xff) - v2->y())
200 * (v1->x() - v2->x()) / (v1->y() - v2->y());
201 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, yC, xUp, dx,
202 x2, dx2, value, 0, dd);
203 if (yC != y1)
204 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y1, xDn, dx,
205 x1, dx1, value, 0, dd);
206 }
207 } else {
208 if (y1 < yC) {
209 // y1 < yC <= y2
210 // Center to the left.
211 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
212 int xUp, xDn;
213 xUp = xDn = v1->x() + (clip == Clip ? (yC << 8) + 0xff - v1->y()
214 : (center->y() | 0xff) - v1->y())
215 * (v1->x() - v2->x()) / (v1->y() - v2->y());
216 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, yC, x1, dx1,
217 xUp, dx, value, 0, dd);
218 if (yC != y2)
219 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y2, x2, dx2,
220 xDn, dx, value, 0, dd);
221 } else {
222 // Center at the top.
223 if (y2 < y1) {
224 // yC <= y2 < y1
225 // Long right edge.
226 if (yC != y2) {
227 d2 = centerFrac * value / (v2->y() - center->y());
228 dd2 = ((value << 8) / (v2->y() - center->y()));
229 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y2, x2, dx2,
230 x1, dx1, d2, dd2, dd);
231 }
232 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
233 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
234 fillLines<clip, TopDown, LeftToRight>(bits, width, height, y2, y1, x2, dx2,
235 x1, dx1, value, 0, dd);
236 } else {
237 // Long left edge.
238 // yC <= y1 <= y2
239 if (yC != y1) {
240 d1 = centerFrac * value / (v1->y() - center->y());
241 dd1 = ((value << 8) / (v1->y() - center->y()));
242 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y1, x2, dx2,
243 x1, dx1, d1, dd1, dd);
244 }
245 if (y1 != y2) {
246 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
247 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
248 fillLines<clip, TopDown, RightToLeft>(bits, width, height, y1, y2, x2, dx2,
249 x1, dx1, value, 0, dd);
250 }
251 }
252 }
253 }
254}
255
256template <FillClip clip>
257void drawRectangle(qint32 *bits, int width, int height,
258 const QPoint *int1, const QPoint *center1, const QPoint *ext1,
259 const QPoint *int2, const QPoint *center2, const QPoint *ext2,
260 qint32 extValue)
261{
262 if (center1->y() > center2->y()) {
263 qSwap(center1, center2);
264 qSwap(int1, ext2);
265 qSwap(ext1, int2);
266 extValue = -extValue;
267 }
268
269 Q_ASSERT(ext1->x() - center1->x() == center1->x() - int1->x());
270 Q_ASSERT(ext1->y() - center1->y() == center1->y() - int1->y());
271 Q_ASSERT(ext2->x() - center2->x() == center2->x() - int2->x());
272 Q_ASSERT(ext2->y() - center2->y() == center2->y() - int2->y());
273
274 const int yc1 = clip == Clip ? qBound(0, center1->y() >> 8, height) : center1->y() >> 8;
275 const int yc2 = clip == Clip ? qBound(0, center2->y() >> 8, height) : center2->y() >> 8;
276 const int yi1 = clip == Clip ? qBound(0, int1->y() >> 8, height) : int1->y() >> 8;
277 const int yi2 = clip == Clip ? qBound(0, int2->y() >> 8, height) : int2->y() >> 8;
278 const int ye1 = clip == Clip ? qBound(0, ext1->y() >> 8, height) : ext1->y() >> 8;
279 const int ye2 = clip == Clip ? qBound(0, ext2->y() >> 8, height) : ext2->y() >> 8;
280
281 const int center1Frac = clip == Clip ? (yc1 << 8) + 0xff - center1->y() : ~center1->y() & 0xff;
282 const int center2Frac = clip == Clip ? (yc2 << 8) + 0xff - center2->y() : ~center2->y() & 0xff;
283 const int int1Frac = clip == Clip ? (yi1 << 8) + 0xff - int1->y() : ~int1->y() & 0xff;
284 const int ext1Frac = clip == Clip ? (ye1 << 8) + 0xff - ext1->y() : ~ext1->y() & 0xff;
285
286 int dxC = 0, dxE = 0; // cap slope, edge slope
287 qint32 ddC = 0;
288 if (ext1->y() != int1->y()) {
289 dxC = ((ext1->x() - int1->x()) << 8) / (ext1->y() - int1->y());
290 ddC = (extValue << 9) / (ext1->y() - int1->y());
291 }
292 if (ext1->y() != ext2->y())
293 dxE = ((ext1->x() - ext2->x()) << 8) / (ext1->y() - ext2->y());
294
295 const qint32 div = (ext1->x() - int1->x()) * (ext2->y() - int1->y())
296 - (ext1->y() - int1->y()) * (ext2->x() - int1->x());
297 const qint32 dd = div ? qint32((qint64(extValue * (ext2->y() - ext1->y())) << 9) / div) : 0;
298
299 int xe1, xe2, xc1, xc2;
300 qint32 d;
301
302 qint32 intValue = -extValue;
303
304 if (center2->x() < center1->x()) {
305 // Leaning to the right. '/'
306 if (int1->y() < ext2->y()) {
307 // Mostly vertical.
308 Q_ASSERT(ext1->y() != ext2->y());
309 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
310 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
311 if (ye1 != yi1) {
312 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
313 xc2 += (ye1 - yc1) * dxC;
314 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, yi1, xe1, dxE,
315 xc2, dxC, extValue, 0, dd);
316 }
317 if (yi1 != ye2)
318 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi1, ye2, xe1, dxE,
319 xe2, dxE, extValue, 0, dd);
320 if (ye2 != yi2) {
321 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
322 xc1 += (ye2 - yc2) * dxC;
323 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye2, yi2, xc1, dxC,
324 xe2, dxE, intValue, 0, dd);
325 }
326 } else {
327 // Mostly horizontal.
328 Q_ASSERT(ext1->y() != int1->y());
329 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
330 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
331 xc1 += (ye2 - yc2) * dxC;
332 xc2 += (ye1 - yc1) * dxC;
333 if (ye1 != ye2) {
334 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
335 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
336 xc2, dxC, extValue, 0, dd);
337 }
338 if (ye2 != yi1) {
339 d = (clip == Clip ? (ye2 << 8) + 0xff - center2->y()
340 : (ext2->y() | 0xff) - center2->y())
341 * 2 * extValue / (ext1->y() - int1->y());
342 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye2, yi1, xc1, dxC,
343 xc2, dxC, d, ddC, dd);
344 }
345 if (yi1 != yi2) {
346 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
347 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
348 xe2, dxE, intValue, 0, dd);
349 }
350 }
351 } else {
352 // Leaning to the left. '\'
353 if (ext1->y() < int2->y()) {
354 // Mostly vertical.
355 Q_ASSERT(ext1->y() != ext2->y());
356 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
357 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
358 if (yi1 != ye1) {
359 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
360 xc1 += (yi1 - yc1) * dxC;
361 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, ye1, xc1, dxC,
362 xe2, dxE, intValue, 0, dd);
363 }
364 if (ye1 != yi2)
365 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye1, yi2, xe1, dxE,
366 xe2, dxE, intValue, 0, dd);
367 if (yi2 != ye2) {
368 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
369 xc2 += (yi2 - yc2) * dxC;
370 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi2, ye2, xe1, dxE,
371 xc2, dxC, extValue, 0, dd);
372 }
373 } else {
374 // Mostly horizontal.
375 Q_ASSERT(ext1->y() != int1->y());
376 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
377 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
378 xc1 += (yi1 - yc1) * dxC;
379 xc2 += (yi2 - yc2) * dxC;
380 if (yi1 != yi2) {
381 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
382 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
383 xe2, dxE, intValue, 0, dd);
384 }
385 if (yi2 != ye1) {
386 d = (clip == Clip ? (yi2 << 8) + 0xff - center2->y()
387 : (int2->y() | 0xff) - center2->y())
388 * 2 * extValue / (ext1->y() - int1->y());
389 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi2, ye1, xc1, dxC,
390 xc2, dxC, d, ddC, dd);
391 }
392 if (ye1 != ye2) {
393 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
394 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
395 xc2, dxC, extValue, 0, dd);
396 }
397 }
398 }
399}
400
401static void drawPolygons(qint32 *bits, int width, int height, const QPoint *vertices,
402 const quint32 *indices, int indexCount, qint32 value)
403{
404 Q_ASSERT(indexCount != 0);
405 typedef QVarLengthArray<quint16, 16> ScanLine;
406 QVarLengthArray<ScanLine, 128> scans(height);
407 int first = 0;
408 for (int i = 1; i < indexCount; ++i) {
409 quint32 idx1 = indices[i - 1];
410 quint32 idx2 = indices[i];
411 Q_ASSERT(idx1 != quint32(-1));
412 if (idx2 == quint32(-1)) {
413 idx2 = indices[first];
414 Q_ASSERT(idx2 != quint32(-1));
415 first = ++i;
416 }
417 const QPoint *v1 = &vertices[idx1];
418 const QPoint *v2 = &vertices[idx2];
419 if (v2->y() < v1->y())
420 qSwap(v1, v2);
421 int fromY = qMax(0, v1->y() >> 8);
422 int toY = qMin(height, v2->y() >> 8);
423 if (fromY >= toY)
424 continue;
425 int dx = ((v2->x() - v1->x()) << 8) / (v2->y() - v1->y());
426 int x = v1->x() + ((fromY << 8) + 0xff - v1->y()) * (v2->x() - v1->x()) / (v2->y() - v1->y());
427 for (int y = fromY; y < toY; ++y) {
428 quint32 c = quint32(x >> 8);
429 if (c < quint32(width))
430 scans[y].append(quint16(c));
431 x += dx;
432 }
433 }
434 for (int i = 0; i < height; ++i) {
435 quint16 *scanline = scans[i].data();
436 int size = scans[i].size();
437 for (int j = 1; j < size; ++j) {
438 int k = j;
439 quint16 value = scanline[k];
440 for (; k != 0 && value < scanline[k - 1]; --k)
441 scanline[k] = scanline[k - 1];
442 scanline[k] = value;
443 }
444 qint32 *line = bits + i * width;
445 int j = 0;
446 for (; j + 1 < size; j += 2) {
447 for (quint16 x = scanline[j]; x < scanline[j + 1]; ++x)
448 line[x] = value;
449 }
450 if (j < size) {
451 for (int x = scanline[j]; x < width; ++x)
452 line[x] = value;
453 }
454 }
455}
456
457static void makeDistanceField(QA8Image *image, const QPainterPath &path, int dfScale, int offs)
458{
459 if (!image || image->isNull())
460 return;
461
462 if (path.isEmpty()) {
463 image->fill(0);
464 return;
465 }
466
467 int imgWidth = image->width();
468 int imgHeight = image->height();
469
470 QTransform transform;
471 transform.translate(offs, offs);
472 transform.scale(qreal(1) / dfScale, qreal(1) / dfScale);
473
474 QDataBuffer<quint32> pathIndices(0);
475 QDataBuffer<QPoint> pathVertices(0);
476 qSimplifyPath(path, pathVertices, pathIndices, transform);
477 if (pathVertices.isEmpty()) {
478 qCWarning(lcDistanceField) << "Unexpected glyph path structure, bailing out";
479 image->fill(0);
480 return;
481 }
482
483 const qint32 interiorColor = -0x7f80; // 8:8 signed format, -127.5
484 const qint32 exteriorColor = 0x7f80; // 8:8 signed format, 127.5
485
486 QScopedArrayPointer<qint32> bits(new qint32[imgWidth * imgHeight]);
487 for (int i = 0; i < imgWidth * imgHeight; ++i)
488 bits[i] = exteriorColor;
489
490 const qreal angleStep = qDegreesToRadians(qreal(15));
491 const QPoint rotation(qRound(qCos(angleStep) * 0x4000),
492 qRound(qSin(angleStep) * 0x4000)); // 2:14 signed
493
494 const quint32 *indices = pathIndices.data();
495 QVarLengthArray<QPoint> normals;
496 QVarLengthArray<QPoint> vertices;
497
498 QVarLengthArray<int> crossProducts;
499 QVarLengthArray<bool> needsClipping;
500
501 drawPolygons(bits.data(), imgWidth, imgHeight, pathVertices.data(),
502 indices, pathIndices.size(), interiorColor);
503
504 int index = 0;
505
506 while (index < pathIndices.size()) {
507 normals.clear();
508 vertices.clear();
509 needsClipping.clear();
510
511 // Find end of polygon.
512 int end = index;
513 while (indices[end] != quint32(-1))
514 ++end;
515
516 // Calculate vertex normals.
517 for (int next = index, prev = end - 1; next < end; prev = next++) {
518 quint32 fromVertexIndex = indices[prev];
519 quint32 toVertexIndex = indices[next];
520
521 const QPoint &from = pathVertices.at(fromVertexIndex);
522 const QPoint &to = pathVertices.at(toVertexIndex);
523
524 QPoint n(to.y() - from.y(), from.x() - to.x());
525 if (n.x() == 0 && n.y() == 0)
526 continue;
527 int scale = qRound((offs << 16) / qSqrt(qreal(n.x()) * n.x() + qreal(n.y()) * n.y())); // 8:16
528 Q_ASSERT(scale != 0);
529
530 n.rx() = n.x() * scale >> 8;
531 n.ry() = n.y() * scale >> 8;
532 normals.append(n);
533 QPoint v(to.x() + 0x7f, to.y() + 0x7f);
534 vertices.append(v);
535 needsClipping.append((to.x() < offs << 8) || (to.x() >= (imgWidth - offs) << 8)
536 || (to.y() < offs << 8) || (to.y() >= (imgHeight - offs) << 8));
537 }
538
539 crossProducts.resize(normals.size());
540 for (int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
541 crossProducts[prev] = normals.at(prev).x() * normals.at(next).y()
542 - normals.at(prev).y() * normals.at(next).x();
543 }
544
545 // Draw quads.
546 for (int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
547 QPoint n = normals.at(next);
548 QPoint intPrev = vertices.at(prev);
549 QPoint extPrev = vertices.at(prev);
550 QPoint intNext = vertices.at(next);
551 QPoint extNext = vertices.at(next);
552
553 extPrev.rx() -= n.x();
554 extPrev.ry() -= n.y();
555 intPrev.rx() += n.x();
556 intPrev.ry() += n.y();
557 extNext.rx() -= n.x();
558 extNext.ry() -= n.y();
559 intNext.rx() += n.x();
560 intNext.ry() += n.y();
561
562 if (needsClipping[prev] || needsClipping[next]) {
563 drawRectangle<Clip>(bits.data(), imgWidth, imgHeight,
564 &intPrev, &vertices.at(prev), &extPrev,
565 &intNext, &vertices.at(next), &extNext,
566 exteriorColor);
567 } else {
568 drawRectangle<NoClip>(bits.data(), imgWidth, imgHeight,
569 &intPrev, &vertices.at(prev), &extPrev,
570 &intNext, &vertices.at(next), &extNext,
571 exteriorColor);
572 }
573
574 const int crossProduct = crossProducts.at(prev);
575 if (crossProduct < 0) {
576 QPoint p = extPrev;
577 if (needsClipping[prev]) {
578 for (;;) {
579 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
580 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
581 n = rn;
582 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
583 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
584 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
585 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
586 &extPrev, &p, exteriorColor);
587 break;
588 }
589
590 p.rx() = vertices.at(prev).x() - n.x();
591 p.ry() = vertices.at(prev).y() - n.y();
592 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
593 &extPrev, &p, exteriorColor);
594 extPrev = p;
595 }
596 } else {
597 for (;;) {
598 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
599 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
600 n = rn;
601 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
602 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
603 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
604 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
605 &extPrev, &p, exteriorColor);
606 break;
607 }
608
609 p.rx() = vertices.at(prev).x() - n.x();
610 p.ry() = vertices.at(prev).y() - n.y();
611 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
612 &extPrev, &p, exteriorColor);
613 extPrev = p;
614 }
615 }
616 } else if (crossProduct > 0) {
617 QPoint p = intPrev;
618 if (needsClipping[prev]) {
619 for (;;) {
620 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
621 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
622 n = rn;
623 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
624 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
625 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
626 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
627 &p, &intPrev, interiorColor);
628 break;
629 }
630
631 p.rx() = vertices.at(prev).x() + n.x();
632 p.ry() = vertices.at(prev).y() + n.y();
633 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
634 &p, &intPrev, interiorColor);
635 intPrev = p;
636 }
637 } else {
638 for (;;) {
639 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
640 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
641 n = rn;
642 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
643 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
644 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
645 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
646 &p, &intPrev, interiorColor);
647 break;
648 }
649
650 p.rx() = vertices.at(prev).x() + n.x();
651 p.ry() = vertices.at(prev).y() + n.y();
652 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
653 &p, &intPrev, interiorColor);
654 intPrev = p;
655 }
656 }
657 }
658 }
659
660 index = end + 1;
661 }
662
663 const qint32 *inLine = bits.data();
664 uchar *outLine = image->bits();
665 for (int y = 0; y < imgHeight; ++y) {
666 for (int x = 0; x < imgWidth; ++x, ++inLine, ++outLine)
667 *outLine = uchar((0x7f80 - *inLine) >> 8);
668 }
669}
670
671static bool imageHasNarrowOutlines(const QImage &im)
672{
673 if (im.isNull() || im.width() < 1 || im.height() < 1)
674 return false;
675 else if (im.width() == 1 || im.height() == 1)
676 return true;
677
678 int minHThick = 999;
679 int minVThick = 999;
680
681 int thick = 0;
682 bool in = false;
683 int y = (im.height() + 1) / 2;
684 for (int x = 0; x < im.width(); ++x) {
685 int a = qAlpha(im.pixel(x, y));
686 if (a > 127) {
687 in = true;
688 ++thick;
689 } else if (in) {
690 in = false;
691 minHThick = qMin(minHThick, thick);
692 thick = 0;
693 }
694 }
695
696 thick = 0;
697 in = false;
698 int x = (im.width() + 1) / 2;
699 for (int y = 0; y < im.height(); ++y) {
700 int a = qAlpha(im.pixel(x, y));
701 if (a > 127) {
702 in = true;
703 ++thick;
704 } else if (in) {
705 in = false;
706 minVThick = qMin(minVThick, thick);
707 thick = 0;
708 }
709 }
710
711 return minHThick == 1 || minVThick == 1;
712}
713
718
720{
721 static bool initialized = false;
722 if (initialized)
723 return;
724 initialized = true;
725
726 if (qEnvironmentVariableIsSet("QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE")) {
727 QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE = qEnvironmentVariableIntValue("QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE");
728 qCDebug(lcDistanceField) << "set the QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE:" << QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE;
729 }
730
731 if (qEnvironmentVariableIsSet("QT_DISTANCEFIELD_DEFAULT_SCALE")) {
732 QT_DISTANCEFIELD_DEFAULT_SCALE = qEnvironmentVariableIntValue("QT_DISTANCEFIELD_DEFAULT_SCALE");
733 qCDebug(lcDistanceField) << "set the QT_DISTANCEFIELD_DEFAULT_SCALE:" << QT_DISTANCEFIELD_DEFAULT_SCALE;
734 }
735 if (qEnvironmentVariableIsSet("QT_DISTANCEFIELD_DEFAULT_RADIUS")) {
736 QT_DISTANCEFIELD_DEFAULT_RADIUS = qEnvironmentVariableIntValue("QT_DISTANCEFIELD_DEFAULT_RADIUS");
737 qCDebug(lcDistanceField) << "set the QT_DISTANCEFIELD_DEFAULT_RADIUS:" << QT_DISTANCEFIELD_DEFAULT_RADIUS;
738 }
739 if (qEnvironmentVariableIsSet("QT_DISTANCEFIELD_DEFAULT_HIGHGLYPHCOUNT")) {
740 QT_DISTANCEFIELD_DEFAULT_HIGHGLYPHCOUNT = qEnvironmentVariableIntValue("QT_DISTANCEFIELD_DEFAULT_HIGHGLYPHCOUNT");
741 qCDebug(lcDistanceField) << "set the QT_DISTANCEFIELD_DEFAULT_HIGHGLYPHCOUNT:" << QT_DISTANCEFIELD_DEFAULT_HIGHGLYPHCOUNT;
742 }
743}
744
745bool qt_fontHasNarrowOutlines(QFontEngine *fontEngine)
746{
748 QFontEngine *fe = fontEngine->cloneWithSize(QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE);
749 if (!fe)
750 return false;
751
752 QImage im;
753
754 const glyph_t glyph = fe->glyphIndex('O');
755 if (glyph != 0)
756 im = fe->alphaMapForGlyph(glyph, QFixedPoint(), QTransform());
757
758 Q_ASSERT(fe->ref.loadRelaxed() == 0);
759 delete fe;
760
761 return imageHasNarrowOutlines(im);
762}
763
764bool qt_fontHasNarrowOutlines(const QRawFont &f)
765{
766 QRawFont font = f;
768 font.setPixelSize(QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE);
769 if (!font.isValid())
770 return false;
771
772 QList<quint32> glyphIndices = font.glyphIndexesForString("O"_L1);
773 if (glyphIndices.isEmpty() || glyphIndices[0] == 0)
774 return false;
775
776 return imageHasNarrowOutlines(font.alphaMapForGlyph(glyphIndices.at(0),
777 QRawFont::PixelAntialiasing));
778}
779
780int QT_DISTANCEFIELD_BASEFONTSIZE(bool narrowOutlineFont)
781{
783
784 if (Q_UNLIKELY(narrowOutlineFont))
786 else
788}
789
790int QT_DISTANCEFIELD_SCALE(bool narrowOutlineFont)
791{
793
794 if (Q_UNLIKELY(narrowOutlineFont))
796 else
798}
799
800int QT_DISTANCEFIELD_RADIUS(bool narrowOutlineFont)
801{
803
804 if (Q_UNLIKELY(narrowOutlineFont))
806 else
808}
809
815
816static bool isWithinAllocationLimit(QSize size)
817{
818 if (size.isEmpty())
819 return false;
820
821 const quint64 nbytes = quint64(size.width()) * quint64(size.height());
822 if (const int mbLimit = QImageReader::allocationLimit()) {
823 if (nbytes > quint64(mbLimit) * 1024 * 1024) {
824 qCWarning(lcDistanceField, "QDistanceField: Rejecting path as size exceeds the "
825 "current allocation limit of %i megabytes", mbLimit);
826 return false;
827 }
828 }
829
830 return true;
831}
832
833static QSize distanceFieldSize(const QPainterPath &path, bool doubleResolution)
834{
835 const int dfScale = QT_DISTANCEFIELD_SCALE(doubleResolution);
836 const int dfMargin = QT_DISTANCEFIELD_RADIUS(doubleResolution) / dfScale;
837 const QRectF boundingRect = path.boundingRect();
838 return QSize(qCeil(boundingRect.width() / dfScale) + dfMargin * 2,
839 qCeil(boundingRect.height() / dfScale) + dfMargin * 2);
840}
841
842static QPainterPath glyphPath(const QRawFont &font, glyph_t glyph, bool doubleResolution)
843{
844 QRawFont renderFont = font;
845 renderFont.setPixelSize(QT_DISTANCEFIELD_BASEFONTSIZE(doubleResolution) * QT_DISTANCEFIELD_SCALE(doubleResolution));
846 return renderFont.pathForGlyph(glyph);
847}
848
849static QPainterPath glyphPath(QFontEngine *fontEngine, glyph_t glyph)
850{
851 QFixedPoint position;
852 QPainterPath path;
853 fontEngine->addGlyphsToPath(&glyph, &position, 1, &path, { });
854 return path;
855}
856
857QDistanceField::QDistanceField() = default;
858
859QDistanceField::QDistanceField(int width, int height)
860 : QA8Image(width, height)
861{
862}
863
864QDistanceField::QDistanceField(const QRawFont &font, glyph_t glyph, bool doubleResolution)
865 : QDistanceField(glyphPath(font, glyph, doubleResolution), glyph, doubleResolution)
866{
867}
868
869QDistanceField::QDistanceField(QFontEngine *fontEngine, glyph_t glyph, bool doubleResolution)
870 : QDistanceField(glyphPath(fontEngine, glyph), glyph, doubleResolution)
871{
872}
873
874QDistanceField::QDistanceField(QSize size, const QPainterPath &path, glyph_t glyph, bool doubleResolution)
875 : QA8Image(isWithinAllocationLimit(size) ? size : QSize())
876 , m_glyph(glyph)
877{
878 QPainterPath dfPath = path;
879 dfPath.translate(-dfPath.boundingRect().topLeft());
880 dfPath.setFillRule(Qt::WindingFill);
881
882 const int dfScale = QT_DISTANCEFIELD_SCALE(doubleResolution);
883 makeDistanceField(this,
884 dfPath,
885 dfScale,
886 QT_DISTANCEFIELD_RADIUS(doubleResolution) / dfScale);
887}
888
889QDistanceField::QDistanceField(const QPainterPath &path, glyph_t glyph, bool doubleResolution)
890 : QDistanceField(distanceFieldSize(path, doubleResolution), path, glyph, doubleResolution)
891{
892}
893
894QDistanceField::QDistanceField(const QA8Image &image, glyph_t glyph)
895 : QA8Image(image)
896 , m_glyph(glyph)
897{
898}
899
900glyph_t QDistanceField::glyph() const
901{
902 return m_glyph;
903}
904
905void QDistanceField::setGlyph(const QRawFont &font, glyph_t glyph, bool doubleResolution)
906{
907 *this = QDistanceField(font, glyph, doubleResolution);
908}
909
910void QDistanceField::setGlyph(QFontEngine *fontEngine, glyph_t glyph, bool doubleResolution)
911{
912 *this = QDistanceField(fontEngine, glyph, doubleResolution);
913}
914
915QDistanceField QDistanceField::copy(const QRect &r) const
916{
917 QA8Image image = QA8Image::copy(r);
918 return QDistanceField(image, image.isNull() ? glyph_t(0) : m_glyph);
919}
920
921QT_END_NAMESPACE
The QRawFont class provides access to a single physical instance of a font.
Definition qrawfont.h:24
Combined button and popup list for selecting options.
static QPainterPath glyphPath(QFontEngine *fontEngine, glyph_t glyph)
static void makeDistanceField(QA8Image *image, const QPainterPath &path, int dfScale, int offs)
static QPainterPath glyphPath(const QRawFont &font, glyph_t glyph, bool doubleResolution)
int QT_DISTANCEFIELD_HIGHGLYPHCOUNT()
bool qt_fontHasNarrowOutlines(const QRawFont &f)
void fillLines(qint32 *bits, int width, int height, int upperY, int lowerY, int &lx, int ldx, int &rx, int rdx, qint32 &d, qint32 ddy, qint32 ddx)
static QSize distanceFieldSize(const QPainterPath &path, bool doubleResolution)
static int QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE
static void drawPolygons(qint32 *bits, int width, int height, const QPoint *vertices, const quint32 *indices, int indexCount, qint32 value)
static int QT_DISTANCEFIELD_DEFAULT_HIGHGLYPHCOUNT
void drawRectangle(qint32 *bits, int width, int height, const QPoint *int1, const QPoint *center1, const QPoint *ext1, const QPoint *int2, const QPoint *center2, const QPoint *ext2, qint32 extValue)
void fillLine(qint32 *, int, int, int, qint32, qint32)
static int QT_DISTANCEFIELD_DEFAULT_RADIUS
bool qt_fontHasNarrowOutlines(QFontEngine *fontEngine)
int QT_DISTANCEFIELD_RADIUS(bool narrowOutlineFont)
static bool isWithinAllocationLimit(QSize size)
int QT_DISTANCEFIELD_SCALE(bool narrowOutlineFont)
static int QT_DISTANCEFIELD_DEFAULT_SCALE
void fillLine< Clip, LeftToRight >(qint32 *line, int width, int lx, int rx, qint32 d, qint32 dd)
static void initialDistanceFieldFactor()
void drawTriangle(qint32 *bits, int width, int height, const QPoint *center, const QPoint *v1, const QPoint *v2, qint32 value)
int QT_DISTANCEFIELD_BASEFONTSIZE(bool narrowOutlineFont)
static bool imageHasNarrowOutlines(const QImage &im)
void fillLine< Clip, RightToLeft >(qint32 *line, int width, int lx, int rx, qint32 d, qint32 dd)
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)