139void drawTriangle(qint32 *bits,
int width,
int height,
const QPoint *center,
140 const QPoint *v1,
const QPoint *v2, qint32 value)
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;
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;
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());
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());
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;
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);
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);
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);
196 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
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);
204 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y1, xDn, dx,
205 x1, dx1, value, 0, dd);
211 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
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);
219 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y2, x2, dx2,
220 xDn, dx, value, 0, dd);
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);
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);
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);
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);
258 const QPoint *int1,
const QPoint *center1,
const QPoint *ext1,
259 const QPoint *int2,
const QPoint *center2,
const QPoint *ext2,
262 if (center1->y() > center2->y()) {
263 qSwap(center1, center2);
266 extValue = -extValue;
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());
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;
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;
286 int dxC = 0, dxE = 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());
292 if (ext1->y() != ext2->y())
293 dxE = ((ext1->x() - ext2->x()) << 8) / (ext1->y() - ext2->y());
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;
299 int xe1, xe2, xc1, xc2;
302 qint32 intValue = -extValue;
304 if (center2->x() < center1->x()) {
306 if (int1->y() < ext2->y()) {
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());
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);
318 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi1, ye2, xe1, dxE,
319 xe2, dxE, extValue, 0, dd);
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);
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;
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);
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);
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);
353 if (ext1->y() < int2->y()) {
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());
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);
365 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye1, yi2, xe1, dxE,
366 xe2, dxE, intValue, 0, dd);
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);
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;
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);
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);
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);
401static void drawPolygons(qint32 *bits,
int width,
int height,
const QPoint *vertices,
402 const quint32 *indices,
int indexCount, qint32 value)
404 Q_ASSERT(indexCount != 0);
405 typedef QVarLengthArray<quint16, 16> ScanLine;
406 QVarLengthArray<ScanLine, 128> scans(height);
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));
417 const QPoint *v1 = &vertices[idx1];
418 const QPoint *v2 = &vertices[idx2];
419 if (v2->y() < v1->y())
421 int fromY = qMax(0, v1->y() >> 8);
422 int toY = qMin(height, v2->y() >> 8);
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));
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) {
439 quint16 value = scanline[k];
440 for (; k != 0 && value < scanline[k - 1]; --k)
441 scanline[k] = scanline[k - 1];
444 qint32 *line = bits + i * width;
446 for (; j + 1 < size; j += 2) {
447 for (quint16 x = scanline[j]; x < scanline[j + 1]; ++x)
451 for (
int x = scanline[j]; x < width; ++x)
457static void makeDistanceField(QDistanceFieldData *data,
const QPainterPath &path,
int dfScale,
int offs)
459 if (!data || !data->data)
462 if (path.isEmpty()) {
463 memset(data->data, 0, data->nbytes);
467 int imgWidth = data->width;
468 int imgHeight = data->height;
470 QTransform transform;
471 transform.translate(offs, offs);
472 transform.scale(qreal(1) / dfScale, qreal(1) / dfScale);
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 memset(data->data, 0, data->nbytes);
483 const qint32 interiorColor = -0x7f80;
484 const qint32 exteriorColor = 0x7f80;
486 QScopedArrayPointer<qint32> bits(
new qint32[imgWidth * imgHeight]);
487 for (
int i = 0; i < imgWidth * imgHeight; ++i)
488 bits[i] = exteriorColor;
490 const qreal angleStep = qDegreesToRadians(qreal(15));
491 const QPoint rotation(qRound(qCos(angleStep) * 0x4000),
492 qRound(qSin(angleStep) * 0x4000));
494 const quint32 *indices = pathIndices.data();
495 QVarLengthArray<QPoint> normals;
496 QVarLengthArray<QPoint> vertices;
497 QVarLengthArray<
bool> isConvex;
498 QVarLengthArray<
bool> needsClipping;
500 drawPolygons(bits.data(), imgWidth, imgHeight, pathVertices.data(),
501 indices, pathIndices.size(), interiorColor);
505 while (index < pathIndices.size()) {
508 needsClipping.clear();
512 while (indices[end] != quint32(-1))
516 for (
int next = index, prev = end - 1; next < end; prev = next++) {
517 quint32 fromVertexIndex = indices[prev];
518 quint32 toVertexIndex = indices[next];
520 const QPoint &from = pathVertices.at(fromVertexIndex);
521 const QPoint &to = pathVertices.at(toVertexIndex);
523 QPoint n(to.y() - from.y(), from.x() - to.x());
524 if (n.x() == 0 && n.y() == 0)
526 int scale = qRound((offs << 16) / qSqrt(qreal(n.x()) * n.x() + qreal(n.y()) * n.y()));
527 Q_ASSERT(scale != 0);
529 n.rx() = n.x() * scale >> 8;
530 n.ry() = n.y() * scale >> 8;
532 QPoint v(to.x() + 0x7f, to.y() + 0x7f);
534 needsClipping.append((to.x() < offs << 8) || (to.x() >= (imgWidth - offs) << 8)
535 || (to.y() < offs << 8) || (to.y() >= (imgHeight - offs) << 8));
538 isConvex.resize(normals.size());
539 for (
int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
540 isConvex[prev] = normals.at(prev).x() * normals.at(next).y()
541 - normals.at(prev).y() * normals.at(next).x() < 0;
545 for (
int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
546 QPoint n = normals.at(next);
547 QPoint intPrev = vertices.at(prev);
548 QPoint extPrev = vertices.at(prev);
549 QPoint intNext = vertices.at(next);
550 QPoint extNext = vertices.at(next);
552 extPrev.rx() -= n.x();
553 extPrev.ry() -= n.y();
554 intPrev.rx() += n.x();
555 intPrev.ry() += n.y();
556 extNext.rx() -= n.x();
557 extNext.ry() -= n.y();
558 intNext.rx() += n.x();
559 intNext.ry() += n.y();
561 if (needsClipping[prev] || needsClipping[next]) {
562 drawRectangle<Clip>(bits.data(), imgWidth, imgHeight,
563 &intPrev, &vertices.at(prev), &extPrev,
564 &intNext, &vertices.at(next), &extNext,
567 drawRectangle<NoClip>(bits.data(), imgWidth, imgHeight,
568 &intPrev, &vertices.at(prev), &extPrev,
569 &intNext, &vertices.at(next), &extNext,
573 if (isConvex.at(prev)) {
575 if (needsClipping[prev]) {
577 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
578 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
580 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
581 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
582 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
583 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
584 &extPrev, &p, exteriorColor);
588 p.rx() = vertices.at(prev).x() - n.x();
589 p.ry() = vertices.at(prev).y() - n.y();
590 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
591 &extPrev, &p, exteriorColor);
596 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
597 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
599 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
600 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
601 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
602 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
603 &extPrev, &p, exteriorColor);
607 p.rx() = vertices.at(prev).x() - n.x();
608 p.ry() = vertices.at(prev).y() - n.y();
609 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
610 &extPrev, &p, exteriorColor);
616 if (needsClipping[prev]) {
618 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
619 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
621 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
622 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
623 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
624 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
625 &p, &intPrev, interiorColor);
629 p.rx() = vertices.at(prev).x() + n.x();
630 p.ry() = vertices.at(prev).y() + n.y();
631 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
632 &p, &intPrev, interiorColor);
637 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
638 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
640 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
641 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
642 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
643 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
644 &p, &intPrev, interiorColor);
648 p.rx() = vertices.at(prev).x() + n.x();
649 p.ry() = vertices.at(prev).y() + n.y();
650 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
651 &p, &intPrev, interiorColor);
661 const qint32 *inLine = bits.data();
662 uchar *outLine = data->data;
663 for (
int y = 0; y < imgHeight; ++y) {
664 for (
int x = 0; x < imgWidth; ++x, ++inLine, ++outLine)
665 *outLine = uchar((0x7f80 - *inLine) >> 8);