aboutsummaryrefslogtreecommitdiffstats
path: root/tools/distfieldgen/main.cpp
blob: 3c944b3d4cc01e8c6365746dbc88afe7e0462487 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt scene graph research project.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore>
#include <QtGui>

#include <private/qsgdistancefieldglyphcache_p.h>

static void usage()
{
    qWarning("Usage: distfieldgen [options] <font_filename>");
    qWarning(" ");
    qWarning("Distfieldgen generates distance-field renderings of the provided font file,");
    qWarning("one for each font family/style it contains.");
    qWarning("Unless the QT_QML_DISTFIELDDIR environment variable is set, the renderings are");
    qWarning("saved in the fonts/distancefields directory where the Qt libraries are located.");
    qWarning("You can also override the output directory with the -d option.");
    qWarning(" ");
    qWarning(" options:");
    qWarning("  -d <directory>................................ output directory");
    qWarning("  --no-multithread.............................. don't use multiple threads to render distance-fields");
    qWarning("  --force-all-styles............................ force rendering of styles Normal, Bold, Italic and Bold Italic");
    qWarning("  -styles \"style1,style2,..\".................... force rendering of specified styles");

    qWarning(" ");
    exit(1);
}

void printProgress(int p)
{
    printf("\r  [");
    for (int i = 0; i < 50; ++i)
        printf(i < p / 2 ? "=" : " ");
    printf("]");
    printf(" %d%%", p);
    fflush(stdout);
}

class DistFieldGenTask : public QRunnable
{
public:
    DistFieldGenTask(QSGDistanceFieldGlyphCache *atlas, int c, int nbGlyph, QMap<int, QImage> *outList)
        : QRunnable()
        , m_atlas(atlas)
        , m_char(c)
        , m_nbGlyph(nbGlyph)
        , m_outList(outList)
    { }

    void run()
    {
        QImage df = m_atlas->renderDistanceFieldGlyph(m_char);
        QMutexLocker lock(&m_mutex);
        m_outList->insert(m_char, df);
        printProgress(float(m_outList->count()) / m_nbGlyph * 100);
    }

    static QMutex m_mutex;
    QSGDistanceFieldGlyphCache *m_atlas;
    int m_char;
    int m_nbGlyph;
    QMap<int, QImage> *m_outList;
};

QMutex DistFieldGenTask::m_mutex;

static void generateDistanceFieldForFont(const QFont &font, const QString &destinationDir, bool multithread)
{
    QSGDistanceFieldGlyphCache *atlas = QSGDistanceFieldGlyphCache::get(QGLContext::currentContext(), font);
    QFontDatabase db;
    QString fontString = font.family() + QLatin1String(" ") + db.styleString(font);
    qWarning("> Generating distance-field for font '%s' (%d glyphs)", fontString.toLatin1().constData(), atlas->glyphCount());

    QMap<int, QImage> distfields;
    for (int i = 0; i < atlas->glyphCount(); ++i) {
        if (multithread) {
            DistFieldGenTask *task = new DistFieldGenTask(atlas, i, atlas->glyphCount(), &distfields);
            QThreadPool::globalInstance()->start(task);
        } else {
            QImage df = atlas->renderDistanceFieldGlyph(i);
            distfields.insert(i, df);
            printProgress(float(distfields.count()) / atlas->glyphCount() * 100);
        }
    }

    if (multithread)
        QThreadPool::globalInstance()->waitForDone();

    // Combine dist fields in one image
    int size = qCeil(qSqrt(qreal(atlas->glyphCount()))) * 64;
    QImage output(size, size, QImage::Format_ARGB32_Premultiplied);
    output.fill(Qt::transparent);
    QPainter p(&output);
    int x, y = 0;
    for (QMap<int, QImage>::const_iterator i = distfields.constBegin(); i != distfields.constEnd(); ++i) {
        p.drawImage(x, y, i.value());
        x += 64;
        if (x >= size) {
            x = 0;
            y += 64;
        }
    }
    p.end();
    printProgress(100);
    printf("\n");

    // Save output
    QFileInfo dfi(destinationDir);
    if (!dfi.isDir()) {
        qWarning("Error: '%s' is not a directory.", destinationDir.toLatin1().constData());
        qWarning(" ");
        exit(1);
    }

    QString filename = font.family();
    filename.remove(QLatin1String(" "));
    QString italic = font.italic() ? QLatin1String("i") : QLatin1String("");
    QString bold = font.weight() > QFont::Normal ? QLatin1String("b") : QLatin1String("");
    filename = filename + bold + italic;
    QString out = QString(QLatin1String("%1/%2.png")).arg(destinationDir).arg(filename);
    output.save(out);
    qWarning("  Distance-field saved to '%s'\n", out.toLatin1().constData());
}

class MyWidget : public QGLWidget
{
    Q_OBJECT
public:
    MyWidget()
        : QGLWidget()
    { }

    ~MyWidget() { }

    void showEvent(QShowEvent *e)
    {
        QStringList args = QApplication::arguments();

        bool noMultithread = args.contains(QLatin1String("--no-multithread"));
        bool forceAllStyles = args.contains(QLatin1String("--force-all-styles"));

        QString fontFile;
        QString destDir;
        for (int i = 0; i < args.count(); ++i) {
            QString a = args.at(i);
            if (!a.startsWith('-') && QFileInfo(a).exists())
                fontFile = a;
            if (a == QLatin1String("-d"))
                destDir = args.at(++i);
        }
        if (destDir.isEmpty()) {
            destDir = QFileInfo(fontFile).canonicalPath();
        }

        QStringList customStyles;
        if (args.contains(QLatin1String("-styles"))) {
            int index = args.indexOf(QLatin1String("-styles"));
            QString styles = args.at(index + 1);
            customStyles = styles.split(QLatin1String(","));
        }

        // Load the font
        int fontID = QFontDatabase::addApplicationFont(fontFile);
        if (fontID == -1) {
            qWarning("Error: Invalid font file.");
            qWarning(" ");
            exit(1);
        }

        QStringList allStyles = QStringList() << QLatin1String("Normal")
                                              << QLatin1String("Bold")
                                              << QLatin1String("Italic")
                                              << QLatin1String("Bold Italic");

        // Generate distance-fields for all families and all styles provided by the font file
        QFontDatabase fontDatabase;
        QStringList families = QFontDatabase::applicationFontFamilies(fontID);
        int famCount = families.count();
        for (int i = 0; i < famCount; ++i) {
            QStringList styles;
            if (forceAllStyles)
                styles = allStyles;
            else if (customStyles.count() > 0)
                styles = customStyles;
            else
                styles = fontDatabase.styles(families.at(i));

            int styleCount = styles.count();
            for (int j = 0; j < styleCount; ++j) {
                QFont font;
                if (forceAllStyles || customStyles.count() > 0) {
                    int weight = styles.at(j).contains(QLatin1String("Bold")) ? QFont::Bold : QFont::Normal;
                    font = QFont(families.at(i), 10, weight, styles.at(j).contains(QLatin1String("Italic")));
                } else {
                    font = fontDatabase.font(families.at(i), styles.at(j), 10); // point size is ignored
                }
                generateDistanceFieldForFont(font, destDir, !noMultithread);
            }
        }

        exit(0);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QStringList args = QApplication::arguments();

    if (argc < 2
            || args.contains(QLatin1String("--help"))
            || args.contains(QLatin1String("-help"))
            || args.contains(QLatin1String("--h"))
            || args.contains(QLatin1String("-h")))
        usage();


   MyWidget w;
   w.show();

   return app.exec();
}

#include "main.moc"