Skip to content

Commit 227d60c

Browse files
Default font saving and adding to schemes
1 parent 6f3b409 commit 227d60c

3 files changed

Lines changed: 84 additions & 29 deletions

File tree

src/cegui/CEGUIManager.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/util/Utils.h"
1313
#include "src/Application.h"
1414
#include <CEGUI/CEGUI.h>
15+
#include <CEGUI/FreeTypeFont.h>
1516
#include <CEGUI/RendererModules/OpenGL/GLRenderer.h>
1617
#include <CEGUI/RendererModules/OpenGL/GL3Renderer.h>
1718
#include <CEGUI/RendererModules/OpenGL/ViewportTarget.h>
@@ -25,6 +26,8 @@
2526
#include "qopenglframebufferobject.h"
2627
#include "qopenglfunctions.h"
2728
#include <qopenglfunctions_3_2_core.h>
29+
#include <qdom.h>
30+
#include <qtextstream.h>
2831

2932
// Allows us to register subscribers that want CEGUI log info
3033
// This prevents writing CEGUI.log into CWD and allow log display inside the app
@@ -648,6 +651,82 @@ QStringList CEGUIManager::getAvailableFonts() const
648651
return fonts;
649652
}
650653

654+
bool CEGUIManager::saveFont(CEGUI::Font& font, bool addToSchemes) const
655+
{
656+
const QString fontName = CEGUIUtils::stringToQString(font.getName());
657+
const QString fontDescFileName = fontName + ".font";
658+
659+
// Save an XML font description to the project
660+
// TODO: rewrite with FontManager::writeFontToStream!
661+
662+
QDomDocument doc;
663+
auto xmlHeader = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
664+
doc.appendChild(xmlHeader);
665+
auto xmlRoot = doc.createElement("Font");
666+
xmlRoot.setAttribute("version", "3");
667+
xmlRoot.setAttribute("name", fontName);
668+
xmlRoot.setAttribute("filename", CEGUIUtils::stringToQString(font.getFileName()));
669+
xmlRoot.setAttribute("type", CEGUIUtils::stringToQString(font.getTypeName()));
670+
if (auto freeTypeFont = dynamic_cast<CEGUI::FreeTypeFont*>(&font))
671+
xmlRoot.setAttribute("size", QString::number(static_cast<int>(freeTypeFont->getSize())));
672+
xmlRoot.setAttribute("nativeHorzRes", QString::number(static_cast<int>(font.getNativeResolution().d_width)));
673+
xmlRoot.setAttribute("nativeVertRes", QString::number(static_cast<int>(font.getNativeResolution().d_height)));
674+
xmlRoot.setAttribute("autoScaled", CEGUIUtils::stringToQString(CEGUI::PropertyHelper<CEGUI::AutoScaledMode>::toString(font.getAutoScaled())));
675+
doc.appendChild(xmlRoot);
676+
677+
{
678+
const auto dstFontDescPath = currentProject->getResourceFilePath(fontDescFileName, "fonts");
679+
QFile file(dstFontDescPath);
680+
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false;
681+
682+
QTextStream stream(&file);
683+
doc.save(stream, 4);
684+
file.close();
685+
}
686+
687+
// Add a new font to all schemes
688+
689+
if (!addToSchemes) return true;
690+
691+
const auto absoluteSchemesPath = currentProject->getAbsolutePathOf(currentProject->schemesPath);
692+
if (!QDir(absoluteSchemesPath).exists()) return true;
693+
694+
QDirIterator schemesIt(absoluteSchemesPath);
695+
while (schemesIt.hasNext())
696+
{
697+
schemesIt.next();
698+
QFileInfo info = schemesIt.fileInfo();
699+
if (info.isDir() || info.suffix() != "scheme") continue;
700+
701+
// Open, read & close file. We will work with a DOM document.
702+
{
703+
QFile file(info.absoluteFilePath());
704+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
705+
return false;
706+
707+
if (!doc.setContent(&file))
708+
return false;
709+
}
710+
711+
auto xmlGUIScheme = doc.documentElement();
712+
QDomElement xmlFontRec = doc.createElement("Font");
713+
xmlFontRec.setAttribute("filename", fontDescFileName);
714+
xmlGUIScheme.appendChild(xmlFontRec);
715+
716+
// Write XML back with changes
717+
{
718+
QFile file(info.absoluteFilePath());
719+
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false;
720+
721+
QTextStream stream(&file);
722+
doc.save(stream, 4);
723+
file.close();
724+
}
725+
}
726+
727+
return true;
728+
}
729+
651730
// Retrieves names of images that are available from the set of schemes that were loaded.
652731
// see syncProjectToCEGUIInstance
653732
QStringList CEGUIManager::getAvailableImages() const

src/cegui/CEGUIManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class CEGUIManager
4545

4646
QStringList getAvailableSkins() const;
4747
QStringList getAvailableFonts() const;
48+
bool saveFont(CEGUI::Font& font, bool addToSchemes) const;
4849
QStringList getAvailableImages() const;
4950
void getAvailableWidgetsBySkin(std::map<QString, QStringList>& out) const;
5051
const QImage& getWidgetPreviewImage(const QString& widgetType, int previewWidth = 0, int previewHeight = 0);

src/ui/CEGUIGraphicsScene.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include <qopenglframebufferobject.h>
1616
#include <qmessagebox.h>
1717
#include <qdir.h>
18-
#include <qdom.h>
19-
#include <qtextstream.h>
2018

2119
static void validateResolution(float& width, float& height)
2220
{
@@ -152,8 +150,7 @@ bool CEGUIGraphicsScene::ensureDefaultFontExists()
152150
{
153151
auto ret = QMessageBox::question(qobject_cast<Application*>(qApp)->getMainWindow(),
154152
"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.",
153+
"Do you want CEED to create a default font? Note that some widgets may render wrong without a font.\n",
157154
QMessageBox::Yes | QMessageBox::No,
158155
QMessageBox::Yes);
159156
if (ret != QMessageBox::Yes) return false;
@@ -179,31 +176,9 @@ bool CEGUIGraphicsScene::ensureDefaultFontExists()
179176
return false;
180177
}
181178

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
179+
// Save font XML description and add it to all schemes
180+
// FIXME: createFont() with family, size etc instead of this, add to particular schemes optionally!
181+
CEGUIManager::Instance().saveFont(*defaultFont, true);
207182
}
208183

209184
if (ceguiContext) ceguiContext->setDefaultFont(defaultFont);

0 commit comments

Comments
 (0)