Debian Codegraph @

src/game/fontfactory.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2014 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "fontfactory.hpp"
17 #include "game.hpp"
18
19 #include <MCAssetManager>
20 #include <MCSurface>
21 #include <MCSurfaceManager>
22 #include <MCSurfaceMetaData>
23 #include <MCTextureGlyph>
24
25 #include "simple_logger.hpp"
26
27 #include <QFont>
28 #include <QPainter>
29 #include <QPixmap>
30
31 #include <cassert>
32
33 // Note!!: MSVC requires that this file is saved in "UTF-8 with BOM" format
34 // in order to make all the wide chars work correctly. On Linux/Unix it doesn't
35 // matter whether there's a BOM or not.
36
37 static const std::vector<wchar_t> russianGlyphs({
38 // Russian letters in UTF-8
39 L'А', L'Б', L'В', L'Г', L'Д', L'Е', L'Ё', L'Ж',
40 L'З', L'И', L'Й', L'К', L'Л', L'М', L'Н', L'О',
41 L'П', L'Р', L'С', L'Т', L'У', L'Ф', L'Х', L'Ц',
42 L'Ч', L'Ш', L'Щ', L'Ъ', L'Ы', L'Ь', L'Э', L'Ю',
43 L'Я',
44
45 L'а', L'б', L'в', L'г', L'д', L'е', L'ё', L'ж',
46 L'з', L'и', L'й', L'к', L'л', L'м', L'н', L'о',
47 L'п', L'р', L'с', L'т', L'у', L'ф', L'х', L'ц',
48 L'ч', L'ш', L'щ', L'ъ', L'ы', L'ь', L'э', L'ю',
49 L'я', L'№'
50 // End of russian letters in UTF-8
51 });
52
53 static void addFallbacks(MCTextureFontData & fontData)
54 {
55 fontData.fallback[L'Á'] = L'A';
56 fontData.fallback[L'á'] = L'a';
57 fontData.fallback[L'Č'] = L'C';
58 fontData.fallback[L'č'] = L'c';
59 fontData.fallback[L'Ď'] = L'D';
60 fontData.fallback[L'ď'] = L'd';
61 fontData.fallback[L'Ě'] = L'E';
62 fontData.fallback[L'ě'] = L'e';
63 fontData.fallback[L'Í'] = L'I';
64 fontData.fallback[L'ì'] = L'i';
65 fontData.fallback[L'í'] = L'i';
66 fontData.fallback[L'Ň'] = L'N';
67 fontData.fallback[L'ň'] = L'n';
68 fontData.fallback[L'Ó'] = L'O';
69 fontData.fallback[L'ò'] = L'o';
70 fontData.fallback[L'ó'] = L'o';
71 fontData.fallback[L'Ř'] = L'R';
72 fontData.fallback[L'ř'] = L'r';
73 fontData.fallback[L'Š'] = L'S';
74 fontData.fallback[L'š'] = L's';
75 fontData.fallback[L'Ť'] = L'T';
76 fontData.fallback[L'ť'] = L't';
77 fontData.fallback[L'Ú'] = L'U';
78 fontData.fallback[L'ú'] = L'u';
79 fontData.fallback[L'Ů'] = L'U';
80 fontData.fallback[L'ů'] = L'u';
81 fontData.fallback[L'ù'] = L'u';
82 fontData.fallback[L'Ý'] = L'Y';
83 fontData.fallback[L'ý'] = L'y';
84 fontData.fallback[L'Ž'] = L'Z';
85 fontData.fallback[L'ž'] = L'z';
86 }
87
88 MCTextureFontData FontFactory::generateFontData(QString family)
89 {
90 auto glyphs = russianGlyphs;
91 for (wchar_t ascii = 0; ascii < 256; ascii++)
92 {
93 glyphs.push_back(ascii);
94 }
95
96 QFont font;
97 font.setFamily(family);
98 font.setStyleHint(QFont::Monospace);
99 const int glyphHeight = 64;
100 font.setPixelSize(glyphHeight);
101 font.setBold(true);
102
103 // Calculate the widest glyph and tune the slot size accordingly
104 int slotWidth = glyphHeight;
105 int slotHeight = glyphHeight;
106 const int cols = 8;
107 const int rows = static_cast<int>(glyphs.size()) / cols;
108 QFontMetrics fm(font);
109 for (int j = 0; j < rows; j++)
110 {
111 for (int i = 0; i < cols; i++)
112 {
113 const int glyphIndex = j * cols + i;
114 const QString text { static_cast<QChar>(glyphs.at(static_cast<size_t>(glyphIndex))) };
115 if (fm.horizontalAdvance(text) > slotWidth)
116 {
117 slotWidth = fm.horizontalAdvance(text);
118 }
119 }
120 }
121 if (fm.height() > slotHeight)
122 {
123 slotHeight = fm.height();
124 }
125
126 // Add a little margin
127 slotHeight += 4;
128
129 juzzlin::L().debug() << "Font slot size: " << slotWidth << "x" << slotHeight;
130
131 const int textureW = cols * slotWidth;
132 const int textureH = rows * slotHeight;
133
134 juzzlin::L().debug() << "Font texture size (initial): " << textureW << "x" << textureH;
135
136 QPixmap fontPixmap(textureW, textureH);
137 fontPixmap.fill(Qt::transparent);
138
139 MCTextureFontData fontData;
140
141 for (int j = 0; j < rows; j++)
142 {
143 for (int i = 0; i < cols; i++)
144 {
145 const int glyphIndex = j * cols + i;
146 const QString text { static_cast<QChar>(glyphs.at(static_cast<size_t>(glyphIndex))) };
147
148 if (text.length())
149 {
150 QPainter painter;
151 painter.begin(&fontPixmap);
152
153 painter.setFont(font);
154 painter.setPen(QColor(255, 255, 255));
155
156 painter.drawText(
157 i * slotWidth,
158 j * slotHeight,
159 slotWidth,
160 slotHeight,
161 Qt::AlignCenter,
162 text);
163
164 painter.end();
165
166 MCTextureFontData::Glyph glyph;
167 glyph.name = glyphs.at(static_cast<size_t>(glyphIndex));
168 glyph.x0 = i * textureW / cols;
169 glyph.y0 = (rows - j) * textureH / rows;
170 glyph.x1 = (i + 1) * textureW / cols;
171 glyph.y1 = (rows - j - 1) * textureH / rows;
172
173 fontData.glyphs.push_back(glyph);
174 }
175 }
176 }
177
178 // Note, that the size of the pixmap doesn't affect the size of the actual
179 // surface / texture rendering that pixmap.
180 MCSurfaceMetaData surfaceData;
181 surfaceData.height = std::pair<int, bool>(textureH, true);
182 surfaceData.width = std::pair<int, bool>(textureW, true);
183 surfaceData.minFilter = std::pair<GLint, bool>(GL_LINEAR, true);
184 surfaceData.magFilter = std::pair<GLint, bool>(GL_LINEAR, true);
185 surfaceData.handle = family.toStdString();
186
187 MCAssetManager::surfaceManager().createSurfaceFromImage(surfaceData, fontPixmap.toImage());
188 fontData.name = Game::instance().fontName();
189 fontData.surface = surfaceData.handle;
190
191 // Compensate the "too big" texture rectangle per glyph
192 fontData.xDensity = 0.75f;
193 fontData.yDensity = 1.0f;
194
195 addFallbacks(fontData);
196
197 return fontData;
198 }