Debian Codegraph @

src/LomiriMetrics/tools/bitmaptextbuilder/bitmaptextbuilder.cpp

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright © 2016 Canonical Ltd.
2 // Author: Loïc Molinari <[email protected]>
3 //
4 // This file is part of Lomiri UI Toolkit.
5 //
6 // Lomiri UI Toolkit is free software: you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published by the
8 // Free Software Foundation; version 3.
9 //
10 // Lomiri UI Toolkit is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 // for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>.
17
18 // Generates the texture atlas containing printing ASCII codes of a monospaced
19 // font in different sizes. The output is a header file containing a structure
20 // to be accessed by the bitmap text implementation.
21
22 #include <QtCore/QFile>
23 #include <QtCore/QTextStream>
24 #include <QtGui/QGuiApplication>
25 #include <QtGui/QPainter>
26 #include <QtGui/QImage>
27
28 // Input data.
29 // FIXME(loicm) Make that command line arguments?
30 const char* fileName = "bitmaptextfont_p.h";
•31 const char* fontFamily = "Noto Mono"; // Must be monospace.
32 const int fontPixelSizeMin = 12; // Must be an even number.
33 const int fontPixelSizeMax = 20; // Must be an even number, higher than fontPixelSizeMin.
34 const bool outline = false;
35 const bool strongOutline = false;
36 const bool saveImage = false;
37 const quint32 backgroundColor = 0xcc000000; // AABBGGRR (premultiplied).
38 const quint32 fontColor = 0xffffffff; // AARRGGBB.
39 const quint32 outlineColor = 0xff000000; // AARRGGBB.
40
41 // Several drivers prefer power-of-two or (at least) multiples of 32 for
42 // performance reasons.
43 // FIXME(loicm) Compute texture size programmatically (based on selected font).
44 const int textureWidth = 736;
45 const int textureHeight = 224;
46 const int textureSize = textureWidth * textureHeight;
47
48 // From 32 to 126 (without the non-printing control characters).
49 const QString asciiCodes[2] = {
50 " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO",
51 "PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
52 };
53
54 int main(int argc, char* argv[])
55 {
56 // Prevents slow texture layout, as well as making sure the code writing
57 // texture data in the header stores everything correctly (loop stores
58 // pixels 4 by 4).
59 Q_STATIC_ASSERT(((textureSize) % 4) == 0);
60 // Prevents incorrect font sizes.
61 Q_STATIC_ASSERT((fontPixelSizeMin & 1) != 1);
62 Q_STATIC_ASSERT((fontPixelSizeMax & 1) != 1);
63 Q_STATIC_ASSERT(fontPixelSizeMax > fontPixelSizeMin);
64
65 QGuiApplication application(argc, argv);
66
67 // Create and start filling header file.
68 QFile file(fileName);
69 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
70 qWarning("Can't create file \'%s\'", fileName);
71 return 1;
72 }
73 QTextStream fileOut(&file);
74 fileOut << "// Copyright 2016 Canonical Ltd.\n"
75 << "// This file has been automatically generated by bitmaptextbuilder."
76 << "\n\n"
77 << "static const struct {\n"
78 << " int fontCount; // Number of fonts in the texture.\n"
79 << " struct {\n"
80 << " short int size; // Size.\n"
81 << " short int y; // Position y of the first font line.\n"
82 << " short int width; // Width of each character.\n"
83 << " short int height; // Height of each character.\n"
84 << " } font[" << (fontPixelSizeMax - fontPixelSizeMin + 2) / 2 << "];\n"
85 << " short int textureWidth; // Width of the texture.\n"
86 << " short int textureHeight; // Height of the texture.\n"
87 << " const unsigned char textureData["
88 << textureSize * 4 + 1 << "];" // Don't forget string terminator.
89 << " // Data (premultiplied 32-bit RGBA).\n"
90 << "} g_bitmapTextFont = {\n"
91 << " " << (fontPixelSizeMax - fontPixelSizeMin + 2) / 2 << ",\n"
92 << " {\n";
93
94 // Create texture data.
95 quint32* data = new quint32 [textureSize];
96 for (int i = 0; i < textureSize; ++i) {
97 data[i] = backgroundColor;
98 }
99
100 // Setup painter to render the fonts in the texture data.
101 QPainter painter;
102 QImage image(reinterpret_cast<uchar*>(data), textureWidth, textureHeight,
103 QImage::Format_RGBA8888_Premultiplied);
104 painter.begin(&image);
105 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
106 QFont font(fontFamily);
107 font.setHintingPreference(QFont::PreferVerticalHinting);
108 font.setStyleStrategy(QFont::ForceIntegerMetrics);
109 font.setBold(true);
110 if (outline) {
111 font.setLetterSpacing(QFont::AbsoluteSpacing, 2);
112 }
113
114 for (int i = fontPixelSizeMin, y = 0; i <= fontPixelSizeMax; i += 2) {
115 font.setPixelSize(i);
116 painter.setFont(font);
117 const QFontMetrics metrics(font);
118 const int initialY = y;
119 for (int j = 0; j < 2; j++) {
120 if (outline) {
121 // Not using QPainterPath for quality reasons.
122 y += metrics.ascent() + 2;
123 painter.setPen(QColor(outlineColor));
124 painter.drawText(1, y - 2, asciiCodes[j]);
125 painter.drawText(0, y - 1, asciiCodes[j]);
126 painter.drawText(2, y - 1, asciiCodes[j]);
127 painter.drawText(1, y, asciiCodes[j]);
128 if (strongOutline) {
129 painter.drawText(0, y - 2, asciiCodes[j]);
130 painter.drawText(2, y - 2, asciiCodes[j]);
131 painter.drawText(0, y, asciiCodes[j]);
132 painter.drawText(2, y, asciiCodes[j]);
133 }
134 painter.setPen(QColor(fontColor));
135 painter.drawText(1, y - 1, asciiCodes[j]);
136 } else {
137 y += metrics.ascent();
138 painter.setPen(QColor(fontColor));
139 painter.drawText(0, y, asciiCodes[j]);
140 }
141 y += metrics.descent() + 1; // Add 1 for the base line (see QFontMetrics docs).
142 }
143
144 // Write font info.
145 const int w = metrics.maxWidth() + (outline ? 2 : 0);
146 const int h = metrics.ascent() + metrics.descent() + (outline ? 3 : 1);
147 fileOut << " { " << i << ", " << initialY << ", " << w << ", " << h
148 << ((i != fontPixelSizeMax) ? " },\n" : " }\n");
149 }
150 painter.end();
151
152 // Write texture data.
153 fileOut << " },\n"
154 << " " << textureWidth << ", " << textureHeight << ",\n";
155 fileOut.setIntegerBase(16);
156 fileOut.setFieldWidth(2);
157 fileOut.setPadChar('0');
158 for (int i = 0; i < textureSize; i += 4) {
159 const quint32 pixel[4] = { data[i], data[i+1], data[i+2], data[i+3] };
160 fileOut << " \""
161 << "\\x" << (pixel[0] & 0xff)
162 << "\\x" << ((pixel[0] >> 8) & 0xff)
163 << "\\x" << ((pixel[0] >> 16) & 0xff)
164 << "\\x" << ((pixel[0] >> 24) & 0xff)
165 << "\\x" << (pixel[1] & 0xff)
166 << "\\x" << ((pixel[1] >> 8) & 0xff)
167 << "\\x" << ((pixel[1] >> 16) & 0xff)
168 << "\\x" << ((pixel[1] >> 24) & 0xff)
169 << "\\x" << (pixel[2] & 0xff)
170 << "\\x" << ((pixel[2] >> 8) & 0xff)
171 << "\\x" << ((pixel[2] >> 16) & 0xff)
172 << "\\x" << ((pixel[2] >> 24) & 0xff)
173 << "\\x" << (pixel[3] & 0xff)
174 << "\\x" << ((pixel[3] >> 8) & 0xff)
175 << "\\x" << ((pixel[3] >> 16) & 0xff)
176 << "\\x" << ((pixel[3] >> 24) & 0xff);
177 fileOut.setFieldWidth(1);
178 fileOut << "\"\n";
179 fileOut.setFieldWidth(2);
180 }
181 fileOut << "};\n";
182
183 if (saveImage) {
184 image.save("bitmaptextfont.png");
185 }
186
187 delete [] data;
188 return 0;
189 }