Skip to content

Commit 747771a

Browse files
committed
Merge branch 'fontexport' into 'master'
Restore --export-fonts option functionality See merge request OpenMW/openmw!4561
2 parents 5f413e7 + dc3264a commit 747771a

File tree

8 files changed

+44
-7
lines changed

8 files changed

+44
-7
lines changed

apps/openmw/engine.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ OMW::Engine::Engine(Files::ConfigurationManager& configurationManager)
373373
, mScriptConsoleMode(false)
374374
, mActivationDistanceOverride(-1)
375375
, mGrab(true)
376+
, mExportFonts(false)
376377
, mRandomSeed(0)
377378
, mNewGame(false)
378379
, mCfgMgr(configurationManager)
@@ -807,7 +808,7 @@ void OMW::Engine::prepareEngine()
807808
rootNode->addChild(guiRoot);
808809

809810
mWindowManager = std::make_unique<MWGui::WindowManager>(mWindow, mViewer, guiRoot, mResourceSystem.get(),
810-
mWorkQueue.get(), mCfgMgr.getLogPath(), mScriptConsoleMode, mTranslationDataStorage, mEncoding,
811+
mWorkQueue.get(), mCfgMgr.getLogPath(), mScriptConsoleMode, mTranslationDataStorage, mEncoding, mExportFonts,
811812
Version::getOpenmwVersionDescription(), shadersSupported, mCfgMgr);
812813
mEnvironment.setWindowManager(*mWindowManager);
813814

@@ -1109,6 +1110,11 @@ void OMW::Engine::setWarningsMode(int mode)
11091110
mWarningsMode = mode;
11101111
}
11111112

1113+
void OMW::Engine::enableFontExport(bool exportFonts)
1114+
{
1115+
mExportFonts = exportFonts;
1116+
}
1117+
11121118
void OMW::Engine::setSaveGameFile(const std::filesystem::path& savegame)
11131119
{
11141120
mSaveGameFile = savegame;

apps/openmw/engine.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ namespace OMW
172172
// Grab mouse?
173173
bool mGrab;
174174

175+
bool mExportFonts;
175176
unsigned int mRandomSeed;
176177
Debug::Level mMaxRecastLogLevel = Debug::Error;
177178

@@ -256,6 +257,8 @@ namespace OMW
256257

257258
void setWarningsMode(int mode);
258259

260+
void enableFontExport(bool exportFonts);
261+
259262
/// Set the save game file to load after initialising the engine.
260263
void setSaveGameFile(const std::filesystem::path& savegame);
261264

apps/openmw/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ bool parseOptions(int argc, char** argv, OMW::Engine& engine, Files::Configurati
155155
Fallback::Map::init(variables["fallback"].as<FallbackMap>().mMap);
156156
engine.setSoundUsage(!variables["no-sound"].as<bool>());
157157
engine.setActivationDistanceOverride(variables["activate-dist"].as<int>());
158+
engine.enableFontExport(variables["export-fonts"].as<bool>());
158159
engine.setRandomSeed(variables["random-seed"].as<unsigned int>());
159160

160161
return true;

apps/openmw/mwgui/windowmanagerimp.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ namespace MWGui
146146
WindowManager::WindowManager(SDL_Window* window, osgViewer::Viewer* viewer, osg::Group* guiRoot,
147147
Resource::ResourceSystem* resourceSystem, SceneUtil::WorkQueue* workQueue, const std::filesystem::path& logpath,
148148
bool consoleOnlyScripts, Translation::Storage& translationDataStorage, ToUTF8::FromType encoding,
149-
const std::string& versionDescription, bool useShaders, Files::ConfigurationManager& cfgMgr)
149+
bool exportFonts, const std::string& versionDescription, bool useShaders, Files::ConfigurationManager& cfgMgr)
150150
: mOldUpdateMask(0)
151151
, mOldCullMask(0)
152152
, mStore(nullptr)
@@ -215,7 +215,8 @@ namespace MWGui
215215
MyGUI::LanguageManager::getInstance().eventRequestTag = MyGUI::newDelegate(this, &WindowManager::onRetrieveTag);
216216

217217
// Load fonts
218-
mFontLoader = std::make_unique<Gui::FontLoader>(encoding, resourceSystem->getVFS(), mScalingFactor);
218+
mFontLoader
219+
= std::make_unique<Gui::FontLoader>(encoding, resourceSystem->getVFS(), mScalingFactor, exportFonts);
219220

220221
// Register own widgets with MyGUI
221222
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWSkill>("Widget");

apps/openmw/mwgui/windowmanagerimp.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ namespace MWGui
128128
WindowManager(SDL_Window* window, osgViewer::Viewer* viewer, osg::Group* guiRoot,
129129
Resource::ResourceSystem* resourceSystem, SceneUtil::WorkQueue* workQueue,
130130
const std::filesystem::path& logpath, bool consoleOnlyScripts, Translation::Storage& translationDataStorage,
131-
ToUTF8::FromType encoding, const std::string& versionDescription, bool useShaders,
131+
ToUTF8::FromType encoding, bool exportFonts, const std::string& versionDescription, bool useShaders,
132132
Files::ConfigurationManager& cfgMgr);
133133
virtual ~WindowManager();
134134

components/fontloader/fontloader.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ namespace
227227
namespace Gui
228228
{
229229

230-
FontLoader::FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor)
230+
FontLoader::FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor, bool exportFonts)
231231
: mVFS(vfs)
232232
, mScalingFactor(scalingFactor)
233+
, mExportFonts(exportFonts)
233234
{
234235
if (encoding == ToUTF8::WINDOWS_1252)
235236
mEncoding = ToUTF8::CP437;
@@ -407,7 +408,8 @@ namespace Gui
407408
file.reset();
408409

409410
// Create the font texture
410-
std::string bitmapFilename = "fonts/" + std::string(name_) + ".tex";
411+
const std::string name(name_);
412+
const std::string bitmapFilename = "fonts/" + name + ".tex";
411413

412414
Files::IStreamPtr bitmapFile = mVFS->get(bitmapFilename);
413415

@@ -429,6 +431,19 @@ namespace Gui
429431
<< bitmapFile->gcount() << "/" << (width * height * 4) << " bytes)";
430432
bitmapFile.reset();
431433

434+
if (mExportFonts)
435+
{
436+
osg::ref_ptr<osg::Image> image = new osg::Image;
437+
image->allocateImage(width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
438+
assert(image->isDataContiguous());
439+
memcpy(image->data(), textureData.data(), textureData.size());
440+
// Convert to OpenGL origin for sensible output
441+
image->flipVertical();
442+
443+
Log(Debug::Info) << "Writing " << name + ".png";
444+
osgDB::writeImageFile(*image, name + ".png");
445+
}
446+
432447
MyGUI::ITexture* tex = MyGUI::RenderManager::getInstance().createTexture(bitmapFilename);
433448
tex->createManual(width, height, MyGUI::TextureUsage::Write, MyGUI::PixelFormat::R8G8B8A8);
434449
unsigned char* texData = reinterpret_cast<unsigned char*>(tex->lock(MyGUI::TextureUsage::Write));
@@ -626,6 +641,13 @@ namespace Gui
626641
code->addAttribute("size", "0 0");
627642
}
628643

644+
if (mExportFonts)
645+
{
646+
Log(Debug::Info) << "Writing " << name + ".xml";
647+
xmlDocument.createDeclaration();
648+
xmlDocument.save(name + ".xml");
649+
}
650+
629651
// Register the font with MyGUI
630652
MyGUI::ResourceManualFont* font = static_cast<MyGUI::ResourceManualFont*>(
631653
MyGUI::FactoryManager::getInstance().createObject("Resource", "ResourceManualFont"));

components/fontloader/fontloader.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace Gui
2525
class FontLoader
2626
{
2727
public:
28-
FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor);
28+
/// @param exportFonts export the converted fonts (Images and XML with glyph metrics) to files?
29+
FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor, bool exportFonts);
2930

3031
void overrideLineHeight(MyGUI::xml::ElementPtr _node, std::string_view _file, MyGUI::Version _version);
3132

@@ -35,6 +36,7 @@ namespace Gui
3536
ToUTF8::FromType mEncoding;
3637
const VFS::Manager* mVFS;
3738
float mScalingFactor;
39+
bool mExportFonts;
3840

3941
void loadFonts();
4042
void loadFont(const std::string& fontName, const std::string& fontId);

docs/source/reference/modding/font.rst

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Morrowind .fnt fonts
1515
Morrowind uses a custom ``.fnt`` file format. It is not compatible with the Windows Font File ``.fnt`` format.
1616
To our knowledge, the format is undocumented. OpenMW can load this format and convert it on the fly into something usable
1717
(see font loader `source code <https://gitlab.com/OpenMW/openmw/blob/master/components/fontloader/fontloader.cpp>`_).
18+
You can use --export-fonts command line option to write the converted font
19+
(a PNG image and an XML file describing the position of each glyph in the image) to the current directory.
1820

1921
They can be used instead of TrueType fonts if needed by specifying their ``.fnt`` files names in the ``openmw.cfg``. For example:
2022

0 commit comments

Comments
 (0)