1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QApplication>
30 
31 #include "qpf2.h"
32 #include "mainwindow.h"
33 
34 #include <private/qfontengine_p.h>
35 
36 QT_BEGIN_NAMESPACE
37 
help()38 static void help()
39 {
40     printf("usage:\n");
41     printf("makeqpf fontname pixelsize [italic] [bold] [--exclude-cmap] [-v]\n");
42     printf("makeqpf -dump [-v] file.qpf2\n");
43     exit(0);
44 }
45 
gui(const QString & customFont=QString ())46 static int gui(const QString &customFont = QString())
47 {
48     MainWindow mw(customFont);
49     mw.show();
50     return qApp->exec();
51 }
52 
53 QT_END_NAMESPACE
54 
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57     QT_USE_NAMESPACE
58 
59     QApplication app(argc, argv);
60     app.setOrganizationName(QLatin1String("QtProject"));
61     app.setApplicationName(QLatin1String("MakeQPF"));
62 
63     const QStringList arguments = app.arguments();
64 
65     if (arguments.count() <= 1) {
66         return gui();
67     } else if (arguments.count() == 2
68                && QFile::exists(arguments.at(1))) {
69         return gui(arguments.at(1));
70     }
71 
72     const QString &firstArg = arguments.at(1);
73     if (firstArg == QLatin1String("-h") || firstArg == QLatin1String("--help"))
74         help();
75     if (firstArg == QLatin1String("-dump")) {
76         QString file;
77         for (int i = 2; i < arguments.count(); ++i) {
78             if (arguments.at(i).startsWith(QLatin1String("-v")))
79                 QPF::debugVerbosity += arguments.at(i).length() - 1;
80             else if (file.isEmpty())
81                 file = arguments.at(i);
82             else
83                 help();
84         }
85 
86         if (file.isEmpty())
87             help();
88 
89         QFile f(file);
90         if (!f.open(QIODevice::ReadOnly)) {
91             printf("cannot open %s\n", qPrintable(file));
92             exit(1);
93         }
94 
95         QByteArray qpf = f.readAll();
96         f.close();
97 
98         QPF::dump(qpf);
99         return 0;
100     }
101 
102     if (arguments.count() < 3) help();
103 
104     QFont font;
105 
106     QString fontName = firstArg;
107     if (QFile::exists(fontName)) {
108         int id = QFontDatabase::addApplicationFont(fontName);
109         if (id == -1) {
110             printf("cannot open font %s", qPrintable(fontName));
111             help();
112         }
113         QStringList families = QFontDatabase::applicationFontFamilies(id);
114         if (families.isEmpty()) {
115             printf("cannot find any font families in %s", qPrintable(fontName));
116             help();
117         }
118         fontName = families.first();
119     }
120     font.setFamily(fontName);
121 
122     bool ok = false;
123     int pixelSize = arguments.at(2).toInt(&ok);
124     if (!ok) help();
125     font.setPixelSize(pixelSize);
126 
127     int generationOptions = QPF::IncludeCMap | QPF::RenderGlyphs;
128 
129     for (int i = 3; i < arguments.count(); ++i) {
130         const QString &arg = arguments.at(i);
131         if (arg == QLatin1String("italic")) {
132             font.setItalic(true);
133         } else if (arg == QLatin1String("bold")) {
134             font.setBold(true);
135         } else if (arg == QLatin1String("--exclude-cmap")) {
136             generationOptions &= ~QPF::IncludeCMap;
137         } else if (arg == QLatin1String("--exclude-glyphs")) {
138             generationOptions &= ~QPF::RenderGlyphs;
139         } else if (arg == QLatin1String("-v")) {
140             ++QPF::debugVerbosity;
141         } else {
142             printf("unknown option %s\n", qPrintable(arg));
143             help();
144         }
145     }
146 
147     font.setStyleStrategy(QFont::NoFontMerging);
148 
149     QList<QPF::CharacterRange> ranges;
150     ranges.append(QPF::CharacterRange()); // default range from 0 to 0xffff
151 
152     QString origFont;
153     QByteArray qpf = QPF::generate(font, generationOptions, ranges, &origFont);
154 
155     QString fileName = QPF::fileNameForFont(font);
156     QFile f(fileName);
157     f.open(QIODevice::WriteOnly | QIODevice::Truncate);
158     f.write(qpf);
159     f.close();
160 
161     if (generationOptions & QPF::IncludeCMap) {
162         printf("Created %s from %s\n", qPrintable(fileName), qPrintable(origFont));
163     } else {
164         printf("Created %s from %s excluding the character-map\n", qPrintable(fileName), qPrintable(origFont));
165         printf("The TrueType font file is therefore required for the font to work\n");
166     }
167 
168     return 0;
169 }
170 
171