141void drawTriangle(qint32 *bits,
int width,
int height,
const QPoint *center,
142 const QPoint *v1,
const QPoint *v2, qint32 value)
144 const int y1 = clip == Clip ? qBound(0, v1->y() >> 8, height) : v1->y() >> 8;
145 const int y2 = clip == Clip ? qBound(0, v2->y() >> 8, height) : v2->y() >> 8;
146 const int yC = clip == Clip ? qBound(0, center->y() >> 8, height) : center->y() >> 8;
148 const int v1Frac = clip == Clip ? (y1 << 8) + 0xff - v1->y() : ~v1->y() & 0xff;
149 const int v2Frac = clip == Clip ? (y2 << 8) + 0xff - v2->y() : ~v2->y() & 0xff;
150 const int centerFrac = clip == Clip ? (yC << 8) + 0xff - center->y() : ~center->y() & 0xff;
152 int dx1 = 0, x1 = 0, dx2 = 0, x2 = 0;
153 qint32 dd1, d1, dd2, d2;
154 if (v1->y() != center->y()) {
155 dx1 = ((v1->x() - center->x()) << 8) / (v1->y() - center->y());
156 x1 = center->x() + centerFrac * (v1->x() - center->x()) / (v1->y() - center->y());
158 if (v2->y() != center->y()) {
159 dx2 = ((v2->x() - center->x()) << 8) / (v2->y() - center->y());
160 x2 = center->x() + centerFrac * (v2->x() - center->x()) / (v2->y() - center->y());
163 const qint32 div = (v2->x() - center->x()) * (v1->y() - center->y())
164 - (v2->y() - center->y()) * (v1->x() - center->x());
165 const qint32 dd = div ? qint32((qint64(value * (v1->y() - v2->y())) << 8) / div) : 0;
173 d1 = centerFrac * value / (v1->y() - center->y());
174 dd1 = ((value << 8) / (v1->y() - center->y()));
175 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y1, yC, x1, dx1,
176 x2, dx2, d1, dd1, dd);
177 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
178 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
179 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, y1, x1, dx1,
180 x2, dx2, value, 0, dd);
184 d2 = centerFrac * value / (v2->y() - center->y());
185 dd2 = ((value << 8) / (v2->y() - center->y()));
186 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y2, yC, x1, dx1,
187 x2, dx2, d2, dd2, dd);
189 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
190 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
191 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, y2, x1, dx1,
192 x2, dx2, value, 0, dd);
198 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
200 xUp = xDn = v2->x() + (clip == Clip ? (yC << 8) + 0xff - v2->y()
201 : (center->y() | 0xff) - v2->y())
202 * (v1->x() - v2->x()) / (v1->y() - v2->y());
203 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, yC, xUp, dx,
204 x2, dx2, value, 0, dd);
206 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y1, xDn, dx,
207 x1, dx1, value, 0, dd);
213 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
215 xUp = xDn = v1->x() + (clip == Clip ? (yC << 8) + 0xff - v1->y()
216 : (center->y() | 0xff) - v1->y())
217 * (v1->x() - v2->x()) / (v1->y() - v2->y());
218 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, yC, x1, dx1,
219 xUp, dx, value, 0, dd);
221 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y2, x2, dx2,
222 xDn, dx, value, 0, dd);
229 d2 = centerFrac * value / (v2->y() - center->y());
230 dd2 = ((value << 8) / (v2->y() - center->y()));
231 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y2, x2, dx2,
232 x1, dx1, d2, dd2, dd);
234 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
235 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
236 fillLines<clip, TopDown, LeftToRight>(bits, width, height, y2, y1, x2, dx2,
237 x1, dx1, value, 0, dd);
242 d1 = centerFrac * value / (v1->y() - center->y());
243 dd1 = ((value << 8) / (v1->y() - center->y()));
244 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y1, x2, dx2,
245 x1, dx1, d1, dd1, dd);
248 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
249 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
250 fillLines<clip, TopDown, RightToLeft>(bits, width, height, y1, y2, x2, dx2,
251 x1, dx1, value, 0, dd);
260 const QPoint *int1,
const QPoint *center1,
const QPoint *ext1,
261 const QPoint *int2,
const QPoint *center2,
const QPoint *ext2,
264 if (center1->y() > center2->y()) {
265 qSwap(center1, center2);
268 extValue = -extValue;
271 Q_ASSERT(ext1->x() - center1->x() == center1->x() - int1->x());
272 Q_ASSERT(ext1->y() - center1->y() == center1->y() - int1->y());
273 Q_ASSERT(ext2->x() - center2->x() == center2->x() - int2->x());
274 Q_ASSERT(ext2->y() - center2->y() == center2->y() - int2->y());
276 const int yc1 = clip == Clip ? qBound(0, center1->y() >> 8, height) : center1->y() >> 8;
277 const int yc2 = clip == Clip ? qBound(0, center2->y() >> 8, height) : center2->y() >> 8;
278 const int yi1 = clip == Clip ? qBound(0, int1->y() >> 8, height) : int1->y() >> 8;
279 const int yi2 = clip == Clip ? qBound(0, int2->y() >> 8, height) : int2->y() >> 8;
280 const int ye1 = clip == Clip ? qBound(0, ext1->y() >> 8, height) : ext1->y() >> 8;
281 const int ye2 = clip == Clip ? qBound(0, ext2->y() >> 8, height) : ext2->y() >> 8;
283 const int center1Frac = clip == Clip ? (yc1 << 8) + 0xff - center1->y() : ~center1->y() & 0xff;
284 const int center2Frac = clip == Clip ? (yc2 << 8) + 0xff - center2->y() : ~center2->y() & 0xff;
285 const int int1Frac = clip == Clip ? (yi1 << 8) + 0xff - int1->y() : ~int1->y() & 0xff;
286 const int ext1Frac = clip == Clip ? (ye1 << 8) + 0xff - ext1->y() : ~ext1->y() & 0xff;
288 int dxC = 0, dxE = 0;
290 if (ext1->y() != int1->y()) {
291 dxC = ((ext1->x() - int1->x()) << 8) / (ext1->y() - int1->y());
292 ddC = (extValue << 9) / (ext1->y() - int1->y());
294 if (ext1->y() != ext2->y())
295 dxE = ((ext1->x() - ext2->x()) << 8) / (ext1->y() - ext2->y());
297 const qint32 div = (ext1->x() - int1->x()) * (ext2->y() - int1->y())
298 - (ext1->y() - int1->y()) * (ext2->x() - int1->x());
299 const qint32 dd = div ? qint32((qint64(extValue * (ext2->y() - ext1->y())) << 9) / div) : 0;
301 int xe1, xe2, xc1, xc2;
304 qint32 intValue = -extValue;
306 if (center2->x() < center1->x()) {
308 if (int1->y() < ext2->y()) {
310 Q_ASSERT(ext1->y() != ext2->y());
311 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
312 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
314 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
315 xc2 += (ye1 - yc1) * dxC;
316 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, yi1, xe1, dxE,
317 xc2, dxC, extValue, 0, dd);
320 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi1, ye2, xe1, dxE,
321 xe2, dxE, extValue, 0, dd);
323 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
324 xc1 += (ye2 - yc2) * dxC;
325 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye2, yi2, xc1, dxC,
326 xe2, dxE, intValue, 0, dd);
330 Q_ASSERT(ext1->y() != int1->y());
331 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
332 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
333 xc1 += (ye2 - yc2) * dxC;
334 xc2 += (ye1 - yc1) * dxC;
336 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
337 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
338 xc2, dxC, extValue, 0, dd);
341 d = (clip == Clip ? (ye2 << 8) + 0xff - center2->y()
342 : (ext2->y() | 0xff) - center2->y())
343 * 2 * extValue / (ext1->y() - int1->y());
344 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye2, yi1, xc1, dxC,
345 xc2, dxC, d, ddC, dd);
348 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
349 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
350 xe2, dxE, intValue, 0, dd);
355 if (ext1->y() < int2->y()) {
357 Q_ASSERT(ext1->y() != ext2->y());
358 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
359 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
361 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
362 xc1 += (yi1 - yc1) * dxC;
363 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, ye1, xc1, dxC,
364 xe2, dxE, intValue, 0, dd);
367 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye1, yi2, xe1, dxE,
368 xe2, dxE, intValue, 0, dd);
370 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
371 xc2 += (yi2 - yc2) * dxC;
372 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi2, ye2, xe1, dxE,
373 xc2, dxC, extValue, 0, dd);
377 Q_ASSERT(ext1->y() != int1->y());
378 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
379 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
380 xc1 += (yi1 - yc1) * dxC;
381 xc2 += (yi2 - yc2) * dxC;
383 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
384 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
385 xe2, dxE, intValue, 0, dd);
388 d = (clip == Clip ? (yi2 << 8) + 0xff - center2->y()
389 : (int2->y() | 0xff) - center2->y())
390 * 2 * extValue / (ext1->y() - int1->y());
391 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi2, ye1, xc1, dxC,
392 xc2, dxC, d, ddC, dd);
395 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
396 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
397 xc2, dxC, extValue, 0, dd);
403static void drawPolygons(qint32 *bits,
int width,
int height,
const QPoint *vertices,
404 const quint32 *indices,
int indexCount, qint32 value)
406 Q_ASSERT(indexCount != 0);
407 typedef QVarLengthArray<quint16, 16> ScanLine;
408 QVarLengthArray<ScanLine, 128> scans(height);
410 for (
int i = 1; i < indexCount; ++i) {
411 quint32 idx1 = indices[i - 1];
412 quint32 idx2 = indices[i];
413 Q_ASSERT(idx1 != quint32(-1));
414 if (idx2 == quint32(-1)) {
415 idx2 = indices[first];
416 Q_ASSERT(idx2 != quint32(-1));
419 const QPoint *v1 = &vertices[idx1];
420 const QPoint *v2 = &vertices[idx2];
421 if (v2->y() < v1->y())
423 int fromY = qMax(0, v1->y() >> 8);
424 int toY = qMin(height, v2->y() >> 8);
427 int dx = ((v2->x() - v1->x()) << 8) / (v2->y() - v1->y());
428 int x = v1->x() + ((fromY << 8) + 0xff - v1->y()) * (v2->x() - v1->x()) / (v2->y() - v1->y());
429 for (
int y = fromY; y < toY; ++y) {
430 quint32 c = quint32(x >> 8);
431 if (c < quint32(width))
432 scans[y].append(quint16(c));
436 for (
int i = 0; i < height; ++i) {
437 quint16 *scanline = scans[i].data();
438 int size = scans[i].size();
439 for (
int j = 1; j < size; ++j) {
441 quint16 value = scanline[k];
442 for (; k != 0 && value < scanline[k - 1]; --k)
443 scanline[k] = scanline[k - 1];
446 qint32 *line = bits + i * width;
448 for (; j + 1 < size; j += 2) {
449 for (quint16 x = scanline[j]; x < scanline[j + 1]; ++x)
453 for (
int x = scanline[j]; x < width; ++x)
459static void makeDistanceField(QDistanceFieldData *data,
const QPainterPath &path,
int dfScale,
int offs)
461 if (!data || !data->data)
464 if (path.isEmpty()) {
465 memset(data->data, 0, data->nbytes);
469 int imgWidth = data->width;
470 int imgHeight = data->height;
472 QTransform transform;
473 transform.translate(offs, offs);
474 transform.scale(qreal(1) / dfScale, qreal(1) / dfScale);
476 QDataBuffer<quint32> pathIndices(0);
477 QDataBuffer<QPoint> pathVertices(0);
478 qSimplifyPath(path, pathVertices, pathIndices, transform);
479 if (pathVertices.isEmpty()) {
480 qCWarning(lcDistanceField) <<
"Unexpected glyph path structure, bailing out";
481 memset(data->data, 0, data->nbytes);
485 const qint32 interiorColor = -0x7f80;
486 const qint32 exteriorColor = 0x7f80;
488 QScopedArrayPointer<qint32> bits(
new qint32[imgWidth * imgHeight]);
489 for (
int i = 0; i < imgWidth * imgHeight; ++i)
490 bits[i] = exteriorColor;
492 const qreal angleStep = qDegreesToRadians(qreal(15));
493 const QPoint rotation(qRound(qCos(angleStep) * 0x4000),
494 qRound(qSin(angleStep) * 0x4000));
496 const quint32 *indices = pathIndices.data();
497 QVarLengthArray<QPoint> normals;
498 QVarLengthArray<QPoint> vertices;
500 QVarLengthArray<
int> crossProducts;
501 QVarLengthArray<
bool> needsClipping;
503 drawPolygons(bits.data(), imgWidth, imgHeight, pathVertices.data(),
504 indices, pathIndices.size(), interiorColor);
508 while (index < pathIndices.size()) {
511 needsClipping.clear();
515 while (indices[end] != quint32(-1))
519 for (
int next = index, prev = end - 1; next < end; prev = next++) {
520 quint32 fromVertexIndex = indices[prev];
521 quint32 toVertexIndex = indices[next];
523 const QPoint &from = pathVertices.at(fromVertexIndex);
524 const QPoint &to = pathVertices.at(toVertexIndex);
526 QPoint n(to.y() - from.y(), from.x() - to.x());
527 if (n.x() == 0 && n.y() == 0)
529 int scale = qRound((offs << 16) / qSqrt(qreal(n.x()) * n.x() + qreal(n.y()) * n.y()));
530 Q_ASSERT(scale != 0);
532 n.rx() = n.x() * scale >> 8;
533 n.ry() = n.y() * scale >> 8;
535 QPoint v(to.x() + 0x7f, to.y() + 0x7f);
537 needsClipping.append((to.x() < offs << 8) || (to.x() >= (imgWidth - offs) << 8)
538 || (to.y() < offs << 8) || (to.y() >= (imgHeight - offs) << 8));
541 crossProducts.resize(normals.size());
542 for (
int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
543 crossProducts[prev] = normals.at(prev).x() * normals.at(next).y()
544 - normals.at(prev).y() * normals.at(next).x();
548 for (
int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
549 QPoint n = normals.at(next);
550 QPoint intPrev = vertices.at(prev);
551 QPoint extPrev = vertices.at(prev);
552 QPoint intNext = vertices.at(next);
553 QPoint extNext = vertices.at(next);
555 extPrev.rx() -= n.x();
556 extPrev.ry() -= n.y();
557 intPrev.rx() += n.x();
558 intPrev.ry() += n.y();
559 extNext.rx() -= n.x();
560 extNext.ry() -= n.y();
561 intNext.rx() += n.x();
562 intNext.ry() += n.y();
564 if (needsClipping[prev] || needsClipping[next]) {
565 drawRectangle<Clip>(bits.data(), imgWidth, imgHeight,
566 &intPrev, &vertices.at(prev), &extPrev,
567 &intNext, &vertices.at(next), &extNext,
570 drawRectangle<NoClip>(bits.data(), imgWidth, imgHeight,
571 &intPrev, &vertices.at(prev), &extPrev,
572 &intNext, &vertices.at(next), &extNext,
576 const int crossProduct = crossProducts.at(prev);
577 if (crossProduct < 0) {
579 if (needsClipping[prev]) {
581 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
582 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
584 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
585 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
586 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
587 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
588 &extPrev, &p, exteriorColor);
592 p.rx() = vertices.at(prev).x() - n.x();
593 p.ry() = vertices.at(prev).y() - n.y();
594 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
595 &extPrev, &p, exteriorColor);
600 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
601 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
603 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
604 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
605 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
606 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
607 &extPrev, &p, exteriorColor);
611 p.rx() = vertices.at(prev).x() - n.x();
612 p.ry() = vertices.at(prev).y() - n.y();
613 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
614 &extPrev, &p, exteriorColor);
618 }
else if (crossProduct > 0) {
620 if (needsClipping[prev]) {
622 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
623 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
625 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
626 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
627 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
628 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
629 &p, &intPrev, interiorColor);
633 p.rx() = vertices.at(prev).x() + n.x();
634 p.ry() = vertices.at(prev).y() + n.y();
635 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
636 &p, &intPrev, interiorColor);
641 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
642 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
644 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
645 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
646 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
647 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
648 &p, &intPrev, interiorColor);
652 p.rx() = vertices.at(prev).x() + n.x();
653 p.ry() = vertices.at(prev).y() + n.y();
654 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
655 &p, &intPrev, interiorColor);
665 const qint32 *inLine = bits.data();
666 uchar *outLine = data->data;
667 for (
int y = 0; y < imgHeight; ++y) {
668 for (
int x = 0; x < imgWidth; ++x, ++inLine, ++outLine)
669 *outLine = uchar((0x7f80 - *inLine) >> 8);