Restore --export-fonts option functionality

This commit is contained in:
Alexei Kotov 2025-03-02 19:10:03 +03:00
parent a6676fd6f3
commit dc3264a3a5
8 changed files with 44 additions and 7 deletions

View file

@ -227,9 +227,10 @@ namespace
namespace Gui
{
FontLoader::FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor)
FontLoader::FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor, bool exportFonts)
: mVFS(vfs)
, mScalingFactor(scalingFactor)
, mExportFonts(exportFonts)
{
if (encoding == ToUTF8::WINDOWS_1252)
mEncoding = ToUTF8::CP437;
@ -407,7 +408,8 @@ namespace Gui
file.reset();
// Create the font texture
std::string bitmapFilename = "fonts/" + std::string(name_) + ".tex";
const std::string name(name_);
const std::string bitmapFilename = "fonts/" + name + ".tex";
Files::IStreamPtr bitmapFile = mVFS->get(bitmapFilename);
@ -428,6 +430,19 @@ namespace Gui
fail(*bitmapFile, bitmapFilename, "File too small to be a valid bitmap");
bitmapFile.reset();
if (mExportFonts)
{
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
assert(image->isDataContiguous());
memcpy(image->data(), textureData.data(), textureData.size());
// Convert to OpenGL origin for sensible output
image->flipVertical();
Log(Debug::Info) << "Writing " << name + ".png";
osgDB::writeImageFile(*image, name + ".png");
}
MyGUI::ITexture* tex = MyGUI::RenderManager::getInstance().createTexture(bitmapFilename);
tex->createManual(width, height, MyGUI::TextureUsage::Write, MyGUI::PixelFormat::R8G8B8A8);
unsigned char* texData = reinterpret_cast<unsigned char*>(tex->lock(MyGUI::TextureUsage::Write));
@ -647,6 +662,13 @@ namespace Gui
cursorCode->addAttribute("size", "0 0");
}
if (mExportFonts)
{
Log(Debug::Info) << "Writing " << name + ".xml";
xmlDocument.createDeclaration();
xmlDocument.save(name + ".xml");
}
// Register the font with MyGUI
MyGUI::ResourceManualFont* font = static_cast<MyGUI::ResourceManualFont*>(
MyGUI::FactoryManager::getInstance().createObject("Resource", "ResourceManualFont"));

View file

@ -25,7 +25,8 @@ namespace Gui
class FontLoader
{
public:
FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor);
/// @param exportFonts export the converted fonts (Images and XML with glyph metrics) to files?
FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, float scalingFactor, bool exportFonts);
void overrideLineHeight(MyGUI::xml::ElementPtr _node, std::string_view _file, MyGUI::Version _version);
@ -35,6 +36,7 @@ namespace Gui
ToUTF8::FromType mEncoding;
const VFS::Manager* mVFS;
float mScalingFactor;
bool mExportFonts;
void loadFonts();
void loadFont(const std::string& fontName, const std::string& fontId);