Skip to content

Commit 6f3b409

Browse files
Create default font if there are no fonts in a project
1 parent b1291c1 commit 6f3b409

5 files changed

Lines changed: 90 additions & 5 deletions

File tree

src/cegui/CEGUIProject.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "src/cegui/CEGUIProject.h"
22
#include "src/cegui/CEGUIProjectItem.h"
33
#include "src/Application.h"
4-
#include "qfile.h"
5-
#include "qdir.h"
6-
#include "qdom.h"
7-
#include "qtextstream.h"
4+
#include <qdir.h>
5+
#include <qdom.h>
6+
#include <qtextstream.h>
87
#include <qmessagebox.h>
98

109
const QString CEGUIProject::EditorEmbeddedCEGUIVersion("1.0");

src/editors/layout/LayoutUndoCommands.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ void LayoutCreateCommand::undo()
307307

308308
void LayoutCreateCommand::redo()
309309
{
310+
// Most of (but not all) widgets require a font to be rendered properly
311+
_visualMode.getScene()->ensureDefaultFontExists();
312+
310313
CEGUI::Window* widget = CEGUI::WindowManager::getSingleton().createWindow(
311314
CEGUIUtils::qStringToString(_type), CEGUIUtils::qStringToString(_name));
312315

src/editors/layout/LayoutVisualMode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "src/util/SettingsEntry.h"
1414
#include "src/util/Utils.h"
1515
#include "src/Application.h"
16-
#include <CEGUI/GUIContext.h>
1716
#include <CEGUI/WindowManager.h>
1817
#include "qboxlayout.h"
1918
#include "qtoolbar.h"
@@ -62,6 +61,8 @@ LayoutVisualMode::LayoutVisualMode(LayoutEditor& editor)
6261
auto&& settings = qobject_cast<Application*>(qApp)->getSettings();
6362
const bool continuousRendering = settings->getEntryValue("layout/visual/continuous_rendering").toBool();
6463

64+
scene->ensureDefaultFontExists();
65+
6566
ceguiWidget = new CEGUIWidget(this);
6667
layout->addWidget(ceguiWidget);
6768
ceguiWidget->setScene(scene);

src/ui/CEGUIGraphicsScene.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
#include "src/ui/CEGUIGraphicsScene.h"
22
#include "src/cegui/CEGUIManager.h"
33
#include "src/cegui/CEGUIProject.h"
4+
#include "src/Application.h"
45
#include <CEGUI/RendererModules/OpenGL/RendererBase.h>
56
#include <CEGUI/RendererModules/OpenGL/ViewportTarget.h>
67
#include <CEGUI/System.h>
78
#include <CEGUI/GUIContext.h>
9+
#include <CEGUI/FontManager.h>
810
#include <qdatetime.h>
911
#include <qgraphicsitem.h>
1012
#include <qgraphicssceneevent.h>
1113
#include <qopenglcontext.h>
1214
#include <qopenglfunctions.h>
1315
#include <qopenglframebufferobject.h>
16+
#include <qmessagebox.h>
17+
#include <qdir.h>
18+
#include <qdom.h>
19+
#include <qtextstream.h>
1420

1521
static void validateResolution(float& width, float& height)
1622
{
@@ -130,6 +136,80 @@ QList<QGraphicsItem*> CEGUIGraphicsScene::topLevelItems() const
130136
return ret;
131137
}
132138

139+
bool CEGUIGraphicsScene::ensureDefaultFontExists()
140+
{
141+
if (ceguiContext && ceguiContext->getDefaultFont()) return true;
142+
143+
const auto* currentProject = CEGUIManager::Instance().getCurrentProject();
144+
if (!currentProject) return false;
145+
146+
auto& fontManager = CEGUI::FontManager::getSingleton();
147+
const auto& fontRegistry = fontManager.getRegisteredFonts();
148+
CEGUI::Font* defaultFont = fontRegistry.empty() ? nullptr : fontRegistry.begin()->second;
149+
150+
// If project has no fonts, offer the user to autocreate a default font
151+
if (!defaultFont)
152+
{
153+
auto ret = QMessageBox::question(qobject_cast<Application*>(qApp)->getMainWindow(),
154+
"Project has no fonts",
155+
"Do you want CEED to create a default font? Note that some widgets may render wrong without a font.\n"
156+
"NOTE: You must add a new font into your scheme file by hand to get it loaded in the future. Will be fixed later.",
157+
QMessageBox::Yes | QMessageBox::No,
158+
QMessageBox::Yes);
159+
if (ret != QMessageBox::Yes) return false;
160+
161+
// Copy font source file if not exist
162+
const auto dstFontPath = currentProject->getResourceFilePath("DejaVuSans.ttf", "fonts");
163+
if (!QFileInfo(dstFontPath).exists())
164+
{
165+
const auto srcFontPath = QDir::current().filePath("data/fonts/DejaVuSans.ttf");
166+
if (!QFile(srcFontPath).copy(dstFontPath)) return false;
167+
}
168+
169+
// Create CEGUI font object
170+
const QSizeF& resolution = currentProject->getDefaultResolution();
171+
try
172+
{
173+
defaultFont = &fontManager.createFreeTypeFont("Default", 14.f, CEGUI::FontSizeUnit::Pixels,
174+
true, "DejaVuSans.ttf", "fonts", CEGUI::AutoScaledMode::Disabled,
175+
CEGUI::Sizef(static_cast<float>(resolution.width()), static_cast<float>(resolution.height())));
176+
}
177+
catch (...)
178+
{
179+
return false;
180+
}
181+
182+
// Save an XML font description to the project
183+
QDomDocument doc;
184+
auto xmlHeader = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
185+
doc.appendChild(xmlHeader);
186+
auto xmlRoot = doc.createElement("Font");
187+
xmlRoot.setAttribute("version", "3");
188+
xmlRoot.setAttribute("name", "Default");
189+
xmlRoot.setAttribute("filename", "DejaVuSans.ttf");
190+
xmlRoot.setAttribute("type", "FreeType");
191+
xmlRoot.setAttribute("size", "14");
192+
xmlRoot.setAttribute("nativeHorzRes", QString::number(static_cast<int>(resolution.width())));
193+
xmlRoot.setAttribute("nativeVertRes", QString::number(static_cast<int>(resolution.height())));
194+
xmlRoot.setAttribute("autoScaled", "disabled"); // CEGUI::PropertyHelper<CEGUI::AutoScaledMode>::toString(CEGUI::AutoScaledMode::Disabled));
195+
doc.appendChild(xmlRoot);
196+
197+
const auto dstFontDescPath = currentProject->getResourceFilePath("Default.font", "fonts");
198+
QFile file(dstFontDescPath);
199+
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
200+
{
201+
QTextStream stream(&file);
202+
doc.save(stream, 4);
203+
file.close();
204+
}
205+
206+
// TODO: add to scheme
207+
}
208+
209+
if (ceguiContext) ceguiContext->setDefaultFont(defaultFont);
210+
return true;
211+
}
212+
133213
void CEGUIGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
134214
{
135215
// Ignore item interaction for rubber band selection in a CEGUIGraphicsView

src/ui/CEGUIGraphicsScene.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class CEGUIGraphicsScene : public QGraphicsScene
3232

3333
QOpenGLFramebufferObject* getOffscreenBuffer() const { return _fbo; }
3434

35+
bool ensureDefaultFontExists();
36+
3537
protected:
3638

3739
virtual void mousePressEvent(QGraphicsSceneMouseEvent* event) override;

0 commit comments

Comments
 (0)