140void drawTriangle(qint32 *bits,
int width,
int height,
const QPoint *center,
141 const QPoint *v1,
const QPoint *v2, qint32 value)
143 const int y1 = clip == Clip ? qBound(0, v1->y() >> 8, height) : v1->y() >> 8;
144 const int y2 = clip == Clip ? qBound(0, v2->y() >> 8, height) : v2->y() >> 8;
145 const int yC = clip == Clip ? qBound(0, center->y() >> 8, height) : center->y() >> 8;
147 const int v1Frac = clip == Clip ? (y1 << 8) + 0xff - v1->y() : ~v1->y() & 0xff;
148 const int v2Frac = clip == Clip ? (y2 << 8) + 0xff - v2->y() : ~v2->y() & 0xff;
149 const int centerFrac = clip == Clip ? (yC << 8) + 0xff - center->y() : ~center->y() & 0xff;
151 int dx1 = 0, x1 = 0, dx2 = 0, x2 = 0;
152 qint32 dd1, d1, dd2, d2;
153 if (v1->y() != center->y()) {
154 dx1 = ((v1->x() - center->x()) << 8) / (v1->y() - center->y());
155 x1 = center->x() + centerFrac * (v1->x() - center->x()) / (v1->y() - center->y());
157 if (v2->y() != center->y()) {
158 dx2 = ((v2->x() - center->x()) << 8) / (v2->y() - center->y());
159 x2 = center->x() + centerFrac * (v2->x() - center->x()) / (v2->y() - center->y());
162 const qint32 div = (v2->x() - center->x()) * (v1->y() - center->y())
163 - (v2->y() - center->y()) * (v1->x() - center->x());
164 const qint32 dd = div ? qint32((qint64(value * (v1->y() - v2->y())) << 8) / div) : 0;
172 d1 = centerFrac * value / (v1->y() - center->y());
173 dd1 = ((value << 8) / (v1->y() - center->y()));
174 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y1, yC, x1, dx1,
175 x2, dx2, d1, dd1, dd);
176 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
177 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
178 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, y1, x1, dx1,
179 x2, dx2, value, 0, dd);
183 d2 = centerFrac * value / (v2->y() - center->y());
184 dd2 = ((value << 8) / (v2->y() - center->y()));
185 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y2, yC, x1, dx1,
186 x2, dx2, d2, dd2, dd);
188 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
189 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
190 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, y2, x1, dx1,
191 x2, dx2, value, 0, dd);
197 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
199 xUp = xDn = v2->x() + (clip == Clip ? (yC << 8) + 0xff - v2->y()
200 : (center->y() | 0xff) - v2->y())
201 * (v1->x() - v2->x()) / (v1->y() - v2->y());
202 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, yC, xUp, dx,
203 x2, dx2, value, 0, dd);
205 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y1, xDn, dx,
206 x1, dx1, value, 0, dd);
212 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
214 xUp = xDn = v1->x() + (clip == Clip ? (yC << 8) + 0xff - v1->y()
215 : (center->y() | 0xff) - v1->y())
216 * (v1->x() - v2->x()) / (v1->y() - v2->y());
217 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, yC, x1, dx1,
218 xUp, dx, value, 0, dd);
220 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y2, x2, dx2,
221 xDn, dx, value, 0, dd);
228 d2 = centerFrac * value / (v2->y() - center->y());
229 dd2 = ((value << 8) / (v2->y() - center->y()));
230 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y2, x2, dx2,
231 x1, dx1, d2, dd2, dd);
233 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
234 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
235 fillLines<clip, TopDown, LeftToRight>(bits, width, height, y2, y1, x2, dx2,
236 x1, dx1, value, 0, dd);
241 d1 = centerFrac * value / (v1->y() - center->y());
242 dd1 = ((value << 8) / (v1->y() - center->y()));
243 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y1, x2, dx2,
244 x1, dx1, d1, dd1, dd);
247 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
248 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
249 fillLines<clip, TopDown, RightToLeft>(bits, width, height, y1, y2, x2, dx2,
250 x1, dx1, value, 0, dd);
259 const QPoint *int1,
const QPoint *center1,
const QPoint *ext1,
260 const QPoint *int2,
const QPoint *center2,
const QPoint *ext2,
263 if (center1->y() > center2->y()) {
264 qSwap(center1, center2);
267 extValue = -extValue;
270 Q_ASSERT(ext1->x() - center1->x() == center1->x() - int1->x());
271 Q_ASSERT(ext1->y() - center1->y() == center1->y() - int1->y());
272 Q_ASSERT(ext2->x() - center2->x() == center2->x() - int2->x());
273 Q_ASSERT(ext2->y() - center2->y() == center2->y() - int2->y());
275 const int yc1 = clip == Clip ? qBound(0, center1->y() >> 8, height) : center1->y() >> 8;
276 const int yc2 = clip == Clip ? qBound(0, center2->y() >> 8, height) : center2->y() >> 8;
277 const int yi1 = clip == Clip ? qBound(0, int1->y() >> 8, height) : int1->y() >> 8;
278 const int yi2 = clip == Clip ? qBound(0, int2->y() >> 8, height) : int2->y() >> 8;
279 const int ye1 = clip == Clip ? qBound(0, ext1->y() >> 8, height) : ext1->y() >> 8;
280 const int ye2 = clip == Clip ? qBound(0, ext2->y() >> 8, height) : ext2->y() >> 8;
282 const int center1Frac = clip == Clip ? (yc1 << 8) + 0xff - center1->y() : ~center1->y() & 0xff;
283 const int center2Frac = clip == Clip ? (yc2 << 8) + 0xff - center2->y() : ~center2->y() & 0xff;
284 const int int1Frac = clip == Clip ? (yi1 << 8) + 0xff - int1->y() : ~int1->y() & 0xff;
285 const int ext1Frac = clip == Clip ? (ye1 << 8) + 0xff - ext1->y() : ~ext1->y() & 0xff;
287 int dxC = 0, dxE = 0;
289 if (ext1->y() != int1->y()) {
290 dxC = ((ext1->x() - int1->x()) << 8) / (ext1->y() - int1->y());
291 ddC = (extValue << 9) / (ext1->y() - int1->y());
293 if (ext1->y() != ext2->y())
294 dxE = ((ext1->x() - ext2->x()) << 8) / (ext1->y() - ext2->y());
296 const qint32 div = (ext1->x() - int1->x()) * (ext2->y() - int1->y())
297 - (ext1->y() - int1->y()) * (ext2->x() - int1->x());
298 const qint32 dd = div ? qint32((qint64(extValue * (ext2->y() - ext1->y())) << 9) / div) : 0;
300 int xe1, xe2, xc1, xc2;
303 qint32 intValue = -extValue;
305 if (center2->x() < center1->x()) {
307 if (int1->y() < ext2->y()) {
309 Q_ASSERT(ext1->y() != ext2->y());
310 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
311 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
313 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
314 xc2 += (ye1 - yc1) * dxC;
315 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, yi1, xe1, dxE,
316 xc2, dxC, extValue, 0, dd);
319 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi1, ye2, xe1, dxE,
320 xe2, dxE, extValue, 0, dd);
322 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
323 xc1 += (ye2 - yc2) * dxC;
324 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye2, yi2, xc1, dxC,
325 xe2, dxE, intValue, 0, dd);
329 Q_ASSERT(ext1->y() != int1->y());
330 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
331 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
332 xc1 += (ye2 - yc2) * dxC;
333 xc2 += (ye1 - yc1) * dxC;
335 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
336 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
337 xc2, dxC, extValue, 0, dd);
340 d = (clip == Clip ? (ye2 << 8) + 0xff - center2->y()
341 : (ext2->y() | 0xff) - center2->y())
342 * 2 * extValue / (ext1->y() - int1->y());
343 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye2, yi1, xc1, dxC,
344 xc2, dxC, d, ddC, dd);
347 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
348 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
349 xe2, dxE, intValue, 0, dd);
354 if (ext1->y() < int2->y()) {
356 Q_ASSERT(ext1->y() != ext2->y());
357 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
358 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
360 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
361 xc1 += (yi1 - yc1) * dxC;
362 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, ye1, xc1, dxC,
363 xe2, dxE, intValue, 0, dd);
366 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye1, yi2, xe1, dxE,
367 xe2, dxE, intValue, 0, dd);
369 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
370 xc2 += (yi2 - yc2) * dxC;
371 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi2, ye2, xe1, dxE,
372 xc2, dxC, extValue, 0, dd);
376 Q_ASSERT(ext1->y() != int1->y());
377 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
378 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
379 xc1 += (yi1 - yc1) * dxC;
380 xc2 += (yi2 - yc2) * dxC;
382 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
383 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
384 xe2, dxE, intValue, 0, dd);
387 d = (clip == Clip ? (yi2 << 8) + 0xff - center2->y()
388 : (int2->y() | 0xff) - center2->y())
389 * 2 * extValue / (ext1->y() - int1->y());
390 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi2, ye1, xc1, dxC,
391 xc2, dxC, d, ddC, dd);
394 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
395 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
396 xc2, dxC, extValue, 0, dd);
402static void drawPolygons(qint32 *bits,
int width,
int height,
const QPoint *vertices,
403 const quint32 *indices,
int indexCount, qint32 value)
405 Q_ASSERT(indexCount != 0);
406 typedef QVarLengthArray<quint16, 16> ScanLine;
407 QVarLengthArray<ScanLine, 128> scans(height);
409 for (
int i = 1; i < indexCount; ++i) {
410 quint32 idx1 = indices[i - 1];
411 quint32 idx2 = indices[i];
412 Q_ASSERT(idx1 != quint32(-1));
413 if (idx2 == quint32(-1)) {
414 idx2 = indices[first];
415 Q_ASSERT(idx2 != quint32(-1));
418 const QPoint *v1 = &vertices[idx1];
419 const QPoint *v2 = &vertices[idx2];
420 if (v2->y() < v1->y())
422 int fromY = qMax(0, v1->y() >> 8);
423 int toY = qMin(height, v2->y() >> 8);
426 int dx = ((v2->x() - v1->x()) << 8) / (v2->y() - v1->y());
427 int x = v1->x() + ((fromY << 8) + 0xff - v1->y()) * (v2->x() - v1->x()) / (v2->y() - v1->y());
428 for (
int y = fromY; y < toY; ++y) {
429 quint32 c = quint32(x >> 8);
430 if (c < quint32(width))
431 scans[y].append(quint16(c));
435 for (
int i = 0; i < height; ++i) {
436 quint16 *scanline = scans[i].data();
437 int size = scans[i].size();
438 for (
int j = 1; j < size; ++j) {
440 quint16 value = scanline[k];
441 for (; k != 0 && value < scanline[k - 1]; --k)
442 scanline[k] = scanline[k - 1];
445 qint32 *line = bits + i * width;
447 for (; j + 1 < size; j += 2) {
448 for (quint16 x = scanline[j]; x < scanline[j + 1]; ++x)
452 for (
int x = scanline[j]; x < width; ++x)
458static void makeDistanceField(QDistanceFieldData *data,
const QPainterPath &path,
int dfScale,
int offs)
460 if (!data || !data->data)
463 if (path.isEmpty()) {
464 memset(data->data, 0, data->nbytes);
468 int imgWidth = data->width;
469 int imgHeight = data->height;
471 QTransform transform;
472 transform.translate(offs, offs);
473 transform.scale(qreal(1) / dfScale, qreal(1) / dfScale);
475 QDataBuffer<quint32> pathIndices(0);
476 QDataBuffer<QPoint> pathVertices(0);
477 qSimplifyPath(path, pathVertices, pathIndices, transform);
478 if (pathVertices.isEmpty()) {
479 qCWarning(lcDistanceField) <<
"Unexpected glyph path structure, bailing out";
480 memset(data->data, 0, data->nbytes);
484 const qint32 interiorColor = -0x7f80;
485 const qint32 exteriorColor = 0x7f80;
487 QScopedArrayPointer<qint32> bits(
new qint32[imgWidth * imgHeight]);
488 for (
int i = 0; i < imgWidth * imgHeight; ++i)
489 bits[i] = exteriorColor;
491 const qreal angleStep = qDegreesToRadians(qreal(15));
492 const QPoint rotation(qRound(qCos(angleStep) * 0x4000),
493 qRound(qSin(angleStep) * 0x4000));
495 const quint32 *indices = pathIndices.data();
496 QVarLengthArray<QPoint> normals;
497 QVarLengthArray<QPoint> vertices;
499 QVarLengthArray<
int> crossProducts;
500 QVarLengthArray<
bool> needsClipping;
502 drawPolygons(bits.data(), imgWidth, imgHeight, pathVertices.data(),
503 indices, pathIndices.size(), interiorColor);
507 while (index < pathIndices.size()) {
510 needsClipping.clear();
514 while (indices[end] != quint32(-1))
518 for (
int next = index, prev = end - 1; next < end; prev = next++) {
519 quint32 fromVertexIndex = indices[prev];
520 quint32 toVertexIndex = indices[next];
522 const QPoint &from = pathVertices.at(fromVertexIndex);
523 const QPoint &to = pathVertices.at(toVertexIndex);
525 QPoint n(to.y() - from.y(), from.x() - to.x());
526 if (n.x() == 0 && n.y() == 0)
528 int scale = qRound((offs << 16) / qSqrt(qreal(n.x()) * n.x() + qreal(n.y()) * n.y()));
529 Q_ASSERT(scale != 0);
531 n.rx() = n.x() * scale >> 8;
532 n.ry() = n.y() * scale >> 8;
534 QPoint v(to.x() + 0x7f, to.y() + 0x7f);
536 needsClipping.append((to.x() < offs << 8) || (to.x() >= (imgWidth - offs) << 8)
537 || (to.y() < offs << 8) || (to.y() >= (imgHeight - offs) << 8));
540 crossProducts.resize(normals.size());
541 for (
int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
542 crossProducts[prev] = normals.at(prev).x() * normals.at(next).y()
543 - normals.at(prev).y() * normals.at(next).x();
547 for (
int next = 0, prev = normals.size() - 1; next < normals.size(); prev = next++) {
548 QPoint n = normals.at(next);
549 QPoint intPrev = vertices.at(prev);
550 QPoint extPrev = vertices.at(prev);
551 QPoint intNext = vertices.at(next);
552 QPoint extNext = vertices.at(next);
554 extPrev.rx() -= n.x();
555 extPrev.ry() -= n.y();
556 intPrev.rx() += n.x();
557 intPrev.ry() += n.y();
558 extNext.rx() -= n.x();
559 extNext.ry() -= n.y();
560 intNext.rx() += n.x();
561 intNext.ry() += n.y();
563 if (needsClipping[prev] || needsClipping[next]) {
564 drawRectangle<Clip>(bits.data(), imgWidth, imgHeight,
565 &intPrev, &vertices.at(prev), &extPrev,
566 &intNext, &vertices.at(next), &extNext,
569 drawRectangle<NoClip>(bits.data(), imgWidth, imgHeight,
570 &intPrev, &vertices.at(prev), &extPrev,
571 &intNext, &vertices.at(next), &extNext,
575 const int crossProduct = crossProducts.at(prev);
576 if (crossProduct < 0) {
578 if (needsClipping[prev]) {
580 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
581 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
583 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
584 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
585 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
586 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
587 &extPrev, &p, exteriorColor);
591 p.rx() = vertices.at(prev).x() - n.x();
592 p.ry() = vertices.at(prev).y() - n.y();
593 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
594 &extPrev, &p, exteriorColor);
599 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
600 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
602 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
603 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
604 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
605 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
606 &extPrev, &p, exteriorColor);
610 p.rx() = vertices.at(prev).x() - n.x();
611 p.ry() = vertices.at(prev).y() - n.y();
612 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
613 &extPrev, &p, exteriorColor);
617 }
else if (crossProduct > 0) {
619 if (needsClipping[prev]) {
621 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
622 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
624 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
625 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
626 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
627 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
628 &p, &intPrev, interiorColor);
632 p.rx() = vertices.at(prev).x() + n.x();
633 p.ry() = vertices.at(prev).y() + n.y();
634 drawTriangle<Clip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
635 &p, &intPrev, interiorColor);
640 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
641 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
643 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
644 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
645 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
646 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
647 &p, &intPrev, interiorColor);
651 p.rx() = vertices.at(prev).x() + n.x();
652 p.ry() = vertices.at(prev).y() + n.y();
653 drawTriangle<NoClip>(bits.data(), imgWidth, imgHeight, &vertices.at(prev),
654 &p, &intPrev, interiorColor);
664 const qint32 *inLine = bits.data();
665 uchar *outLine = data->data;
666 for (
int y = 0; y < imgHeight; ++y) {
667 for (
int x = 0; x < imgWidth; ++x, ++inLine, ++outLine)
668 *outLine = uchar((0x7f80 - *inLine) >> 8);