| 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 | |
| 4 | #include "qsgsoftwareglyphnode_p.h" |
| 5 | #include <QtGui/private/qrawfont_p.h> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | QSGSoftwareGlyphNode::QSGSoftwareGlyphNode() |
| 10 | : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0) |
| 11 | , m_style(QQuickText::Normal) |
| 12 | { |
| 13 | setMaterial((QSGMaterial*)1); |
| 14 | setGeometry(&m_geometry); |
| 15 | } |
| 16 | |
| 17 | namespace { |
| 18 | QRectF calculateBoundingRect(const QPointF &position, const QGlyphRun &glyphs) |
| 19 | { |
| 20 | QFixed minX; |
| 21 | QFixed minY; |
| 22 | QFixed maxX; |
| 23 | QFixed maxY; |
| 24 | |
| 25 | QRawFontPrivate *rawFontD = QRawFontPrivate::get(font: glyphs.rawFont()); |
| 26 | QFontEngine *fontEngine = rawFontD->fontEngine; |
| 27 | |
| 28 | QFontEngine::GlyphFormat glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None ? fontEngine->glyphFormat : QFontEngine::Format_A32; |
| 29 | |
| 30 | int margin = fontEngine->glyphMargin(format: glyphFormat); |
| 31 | |
| 32 | const QVector<uint> glyphIndexes = glyphs.glyphIndexes(); |
| 33 | const QVector<QPointF> glyphPositions = glyphs.positions(); |
| 34 | for (int i = 0, n = qMin(a: glyphIndexes.size(), b: glyphPositions.size()); i < n; ++i) { |
| 35 | glyph_metrics_t gm = fontEngine->alphaMapBoundingBox(glyph: glyphIndexes.at(i), QFixedPoint(), matrix: QTransform(), glyphFormat); |
| 36 | |
| 37 | gm.x += QFixed::fromReal(r: glyphPositions.at(i).x()) - margin; |
| 38 | gm.y += QFixed::fromReal(r: glyphPositions.at(i).y()) - margin; |
| 39 | |
| 40 | if (i == 0) { |
| 41 | minX = gm.x; |
| 42 | minY = gm.y; |
| 43 | maxX = gm.x + gm.width; |
| 44 | maxY = gm.y + gm.height; |
| 45 | } else { |
| 46 | minX = qMin(a: gm.x, b: minX); |
| 47 | minY = qMin(a: gm.y, b: minY); |
| 48 | maxX = qMax(a: gm.x + gm.width, b: maxX); |
| 49 | maxY = qMax(a: gm.y + gm.height, b: maxY); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | QRectF boundingRect(QPointF(minX.toReal(), minY.toReal()), QPointF(maxX.toReal(), maxY.toReal())); |
| 54 | return boundingRect.translated(p: position - QPointF(0.0, glyphs.rawFont().ascent())); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void QSGSoftwareGlyphNode::setGlyphs(const QPointF &position, const QGlyphRun &glyphs) |
| 59 | { |
| 60 | m_position = position; |
| 61 | m_glyphRun = glyphs; |
| 62 | // Decorations handled by text node |
| 63 | m_glyphRun.setOverline(false); |
| 64 | m_glyphRun.setStrikeOut(false); |
| 65 | m_glyphRun.setUnderline(false); |
| 66 | m_bounding_rect = calculateBoundingRect(position, glyphs); |
| 67 | } |
| 68 | |
| 69 | void QSGSoftwareGlyphNode::setColor(const QColor &color) |
| 70 | { |
| 71 | m_color = color; |
| 72 | } |
| 73 | |
| 74 | void QSGSoftwareGlyphNode::setStyle(QQuickText::TextStyle style) |
| 75 | { |
| 76 | m_style = style; |
| 77 | } |
| 78 | |
| 79 | void QSGSoftwareGlyphNode::setStyleColor(const QColor &color) |
| 80 | { |
| 81 | m_styleColor = color; |
| 82 | } |
| 83 | |
| 84 | QPointF QSGSoftwareGlyphNode::baseLine() const |
| 85 | { |
| 86 | return QPointF(); |
| 87 | } |
| 88 | |
| 89 | void QSGSoftwareGlyphNode::setPreferredAntialiasingMode(QSGGlyphNode::AntialiasingMode) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | void QSGSoftwareGlyphNode::update() |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | void QSGSoftwareGlyphNode::paint(QPainter *painter) |
| 98 | { |
| 99 | painter->setBrush(QBrush()); |
| 100 | QPointF pos = m_position - QPointF(0, m_glyphRun.rawFont().ascent()); |
| 101 | |
| 102 | qreal offset = 1.0; |
| 103 | if (painter->device()->devicePixelRatio() > 0.0) |
| 104 | offset = 1.0 / painter->device()->devicePixelRatio(); |
| 105 | |
| 106 | switch (m_style) { |
| 107 | case QQuickText::Normal: break; |
| 108 | case QQuickText::Outline: |
| 109 | painter->setPen(m_styleColor); |
| 110 | painter->drawGlyphRun(position: pos + QPointF(0, offset), glyphRun: m_glyphRun); |
| 111 | painter->drawGlyphRun(position: pos + QPointF(0, -offset), glyphRun: m_glyphRun); |
| 112 | painter->drawGlyphRun(position: pos + QPointF(offset, 0), glyphRun: m_glyphRun); |
| 113 | painter->drawGlyphRun(position: pos + QPointF(-offset, 0), glyphRun: m_glyphRun); |
| 114 | break; |
| 115 | case QQuickText::Raised: |
| 116 | painter->setPen(m_styleColor); |
| 117 | painter->drawGlyphRun(position: pos + QPointF(0, offset), glyphRun: m_glyphRun); |
| 118 | break; |
| 119 | case QQuickText::Sunken: |
| 120 | painter->setPen(m_styleColor); |
| 121 | painter->drawGlyphRun(position: pos + QPointF(0, -offset), glyphRun: m_glyphRun); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | painter->setPen(m_color); |
| 126 | painter->drawGlyphRun(position: pos, glyphRun: m_glyphRun); |
| 127 | } |
| 128 | |
| 129 | QT_END_NAMESPACE |
| 130 | |