summaryrefslogtreecommitdiffstats
path: root/src/render/Qt3DSRenderPathFontText.cpp
blob: aeaad13cd0c17ca7d8f4671371f76338194d3d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/****************************************************************************
**
** Copyright (C) 2015 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "render/Qt3DSRenderPathFontText.h"
#include "render/Qt3DSRenderPathFontSpecification.h"
#include "render/Qt3DSRenderBaseTypes.h"
#include "render/Qt3DSRenderContext.h"
#include "render/Qt3DSRenderPathRender.h"

namespace qt3ds {
namespace render {

    // see NVprSDK for explanation
    // Math from page 54-56 of "Digital Image Warping" by George Wolberg,
    // though credited to Paul Heckert's "Fundamentals of Texture
    // Mapping and Image Warping" 1989 Master's thesis.
    static QT3DSMat33 mapSquareToQuad(QT3DSVec2 inSquare[4])
    {
        QT3DSMat33 ret;

        QT3DSVec2 d1(inSquare[1] - inSquare[2]);
        QT3DSVec2 d2(inSquare[3] - inSquare[2]);
        QT3DSVec2 d3(inSquare[0] - inSquare[1] + inSquare[2] - inSquare[3]);

        QT3DSF32 denom = d1.x * d2.y - d2.x * d1.y;
        if (denom == 0.0) {
            return QT3DSMat33::createIdentity();
        }

        ret[2][0] = (d3.x * d2.y - d2.x * d3.y) / denom;
        ret[2][1] = (d1.x * d3.y - d3.x * d1.y) / denom;
        ret[2][2] = 1.0;
        ret[0][0] = inSquare[1].x - inSquare[0].x + ret[2][0] * inSquare[1].x;
        ret[1][0] = inSquare[1].y - inSquare[0].y + ret[2][0] * inSquare[1].y;
        ret[0][1] = inSquare[3].x - inSquare[0].x + ret[2][1] * inSquare[3].x;
        ret[1][1] = inSquare[3].y - inSquare[0].y + ret[2][1] * inSquare[3].y;
        ret[0][2] = inSquare[0].x;
        ret[1][2] = inSquare[0].y;

        return ret;
    }

    static QT3DSMat33 mapQuadToSquare(QT3DSVec2 inSquare[4])
    {
        return mapSquareToQuad(inSquare).getInverse();
    }

    static QT3DSMat33 mapQuadToQuad(QT3DSVec2 fromSquare[4], QT3DSVec2 toSquare[4])
    {
        return (mapSquareToQuad(toSquare) * mapQuadToSquare(fromSquare));
    }

    static QT3DSMat44 mapBoxToQuad(QT3DSVec4 inBox, QT3DSVec2 inSquare[4])
    {
        QT3DSVec2 fromSquare[4] = { QT3DSVec2(inBox.x, inBox.y), QT3DSVec2(inBox.z, inBox.y),
                                 QT3DSVec2(inBox.z, inBox.w), QT3DSVec2(inBox.x, inBox.w) };

        QT3DSMat33 ret = mapQuadToQuad(fromSquare, inSquare);

        return QT3DSMat44(ret.column0, ret.column1, ret.column2, QT3DSVec3(0.0, 0.0, 0.0));
    }

    NVRenderPathFontItem::NVRenderPathFontItem(NVFoundationBase &fnd)
        : m_Foundation(fnd)
        , mRefCount(0)
        , m_NumGlyphs(0)
        , m_GlyphIDs(NULL)
        , m_TranslateXY(NULL)
    {
    }

    NVRenderPathFontItem::~NVRenderPathFontItem()
    {
        if (m_TranslateXY)
            QT3DS_FREE(m_Foundation.getAllocator(), m_TranslateXY);
        if (m_GlyphIDs)
            QT3DS_FREE(m_Foundation.getAllocator(), m_GlyphIDs);
    }

    void NVRenderPathFontItem::InitTextItem(size_t glyphCount, const QT3DSU32 *glyphIDs,
                                            NVRenderPathFormatType::Enum type, QT3DSF32 *posArray,
                                            QT3DSVec2 pixelBound, QT3DSVec2 logicalBound, QT3DSF32 emScale)
    {
        m_NumGlyphs = glyphCount;

        // allocate glyphs array
        if (m_GlyphIDs)
            QT3DS_FREE(m_Foundation.getAllocator(), m_GlyphIDs);

        // allocate position array
        if (m_TranslateXY)
            QT3DS_FREE(m_Foundation.getAllocator(), m_TranslateXY);

        m_GlyphIDs = (QT3DSU32 *)QT3DS_ALLOC(m_Foundation.getAllocator(),
                                       glyphCount * getSizeofType(type), "NVRenderPathFontItem");
        m_TranslateXY =
            (QT3DSF32 *)QT3DS_ALLOC(m_Foundation.getAllocator(), 2 * (glyphCount + 1) * sizeof(QT3DSF32),
                              "NVRenderPathFontItem");

        if (!m_GlyphIDs || !m_TranslateXY)
            return;

        QT3DSU32 *pTheGlyphIDs = (QT3DSU32 *)m_GlyphIDs;
        QT3DSU32 *pInGlyphs = (QT3DSU32 *)glyphIDs;

        /// copy glyphs array
        for (size_t i = 0; i < glyphCount; i++) {
            pTheGlyphIDs[i] = pInGlyphs[i];
        }

        // copy position array
        // we copy what we got from our layout system
        if (posArray != NULL) {
            for (size_t i = 0, k = 0; i < glyphCount * 2; i += 2, k++) {
                m_TranslateXY[i] = posArray[i] * emScale;
                m_TranslateXY[i + 1] = posArray[i + 1] * emScale;
            }
        }

        // setup transform
        QT3DSVec2 square[4] = { QT3DSVec2(0.0, 0.0), QT3DSVec2(pixelBound.x, 0.0),
                             QT3DSVec2(pixelBound.x, pixelBound.y), QT3DSVec2(0.0, pixelBound.y) };
        QT3DSVec4 box(0.0, 0.0, logicalBound.x * emScale, logicalBound.y * emScale);

        m_ModelMatrix = mapBoxToQuad(box, square);
    }

    const QT3DSMat44 NVRenderPathFontItem::GetTransform()
    {
        return QT3DSMat44(QT3DSVec4(m_ModelMatrix[0][0], m_ModelMatrix[1][0], 0.0, m_ModelMatrix[2][0]),
                       QT3DSVec4(m_ModelMatrix[0][1], m_ModelMatrix[1][1], 0.0, m_ModelMatrix[2][1]),
                       QT3DSVec4(0.0, 0.0, 1.0, 0.0),
                       QT3DSVec4(m_ModelMatrix[0][2], m_ModelMatrix[1][2], 0.0, m_ModelMatrix[2][2]));
    }

    QT3DSU32
    NVRenderPathFontItem::getSizeofType(NVRenderPathFormatType::Enum type)
    {
        switch (type) {
        case NVRenderPathFormatType::Byte:
            return sizeof(QT3DSI8);
        case NVRenderPathFormatType::UByte:
            return sizeof(QT3DSU8);
        case NVRenderPathFormatType::Bytes2:
            return sizeof(QT3DSU16);
        case NVRenderPathFormatType::Uint:
            return sizeof(QT3DSU32);
        case NVRenderPathFormatType::Utf8:
            return sizeof(QT3DSU32);
        default:
            QT3DS_ASSERT(false);
            return 1;
        }
    }

    NVRenderPathFontItem *NVRenderPathFontItem::CreatePathFontItem(NVRenderContextImpl &context)
    {
        QT3DS_ASSERT(context.IsPathRenderingSupported());

        return QT3DS_NEW(context.GetFoundation().getAllocator(),
                      NVRenderPathFontItem)(context.GetFoundation());
    }
}
}