mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-13 12:40:04 +00:00
Move font loading to the FontLoader
This commit is contained in:
parent
f36288569c
commit
396afe79f1
@ -181,7 +181,6 @@ namespace MWGui
|
|||||||
, mRestAllowed(true)
|
, mRestAllowed(true)
|
||||||
, mShowOwned(0)
|
, mShowOwned(0)
|
||||||
, mEncoding(encoding)
|
, mEncoding(encoding)
|
||||||
, mFontHeight(16)
|
|
||||||
, mVersionDescription(versionDescription)
|
, mVersionDescription(versionDescription)
|
||||||
, mWindowVisible(true)
|
, mWindowVisible(true)
|
||||||
{
|
{
|
||||||
@ -219,13 +218,6 @@ namespace MWGui
|
|||||||
SpellView::registerComponents();
|
SpellView::registerComponents();
|
||||||
Gui::registerAllWidgets();
|
Gui::registerAllWidgets();
|
||||||
|
|
||||||
int fontSize = Settings::Manager::getInt("font size", "GUI");
|
|
||||||
fontSize = std::min(std::max(12, fontSize), 20);
|
|
||||||
mFontHeight = fontSize;
|
|
||||||
|
|
||||||
MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate("Resource");
|
|
||||||
MyGUI::ResourceManager::getInstance().registerLoadXmlDelegate("Resource") = newDelegate(this, &WindowManager::loadFontDelegate);
|
|
||||||
|
|
||||||
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerFollowMouse>("Controller");
|
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerFollowMouse>("Controller");
|
||||||
|
|
||||||
MyGUI::FactoryManager::getInstance().registerFactory<ResourceImageSetPointerFix>("Resource", "ResourceImageSetPointer");
|
MyGUI::FactoryManager::getInstance().registerFactory<ResourceImageSetPointerFix>("Resource", "ResourceImageSetPointer");
|
||||||
@ -282,94 +274,6 @@ namespace MWGui
|
|||||||
Settings::Manager::getFloat("contrast", "Video"));
|
Settings::Manager::getFloat("contrast", "Video"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::loadFontDelegate(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version)
|
|
||||||
{
|
|
||||||
MyGUI::xml::ElementEnumerator resourceNode = _node->getElementEnumerator();
|
|
||||||
bool createCopy = false;
|
|
||||||
while (resourceNode.next("Resource"))
|
|
||||||
{
|
|
||||||
std::string type, name;
|
|
||||||
resourceNode->findAttribute("type", type);
|
|
||||||
resourceNode->findAttribute("name", name);
|
|
||||||
|
|
||||||
if (name.empty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (Misc::StringUtils::ciEqual(type, "ResourceTrueTypeFont"))
|
|
||||||
{
|
|
||||||
createCopy = true;
|
|
||||||
|
|
||||||
// For TrueType fonts we should override Size and Resolution properties
|
|
||||||
// to allow to configure font size via config file, without need to edit XML files.
|
|
||||||
// Also we should take UI scaling factor in account.
|
|
||||||
int resolution = Settings::Manager::getInt("ttf resolution", "GUI");
|
|
||||||
resolution = std::min(960, std::max(48, resolution));
|
|
||||||
|
|
||||||
float uiScale = Settings::Manager::getFloat("scaling factor", "GUI");
|
|
||||||
resolution *= uiScale;
|
|
||||||
|
|
||||||
MyGUI::xml::ElementPtr resolutionNode = resourceNode->createChild("Property");
|
|
||||||
resolutionNode->addAttribute("key", "Resolution");
|
|
||||||
resolutionNode->addAttribute("value", std::to_string(resolution));
|
|
||||||
|
|
||||||
MyGUI::xml::ElementPtr sizeNode = resourceNode->createChild("Property");
|
|
||||||
sizeNode->addAttribute("key", "Size");
|
|
||||||
sizeNode->addAttribute("value", std::to_string(mFontHeight));
|
|
||||||
}
|
|
||||||
else if (Misc::StringUtils::ciEqual(type, "ResourceSkin") ||
|
|
||||||
Misc::StringUtils::ciEqual(type, "AutoSizedResourceSkin"))
|
|
||||||
{
|
|
||||||
// We should adjust line height for MyGUI widgets depending on font size
|
|
||||||
MyGUI::xml::ElementPtr heightNode = resourceNode->createChild("Property");
|
|
||||||
heightNode->addAttribute("key", "HeightLine");
|
|
||||||
heightNode->addAttribute("value", std::to_string(mFontHeight+2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MyGUI::ResourceManager::getInstance().loadFromXmlNode(_node, _file, _version);
|
|
||||||
|
|
||||||
if (createCopy)
|
|
||||||
{
|
|
||||||
MyGUI::xml::ElementPtr copy = _node->createCopy();
|
|
||||||
|
|
||||||
MyGUI::xml::ElementEnumerator copyFont = copy->getElementEnumerator();
|
|
||||||
while (copyFont.next("Resource"))
|
|
||||||
{
|
|
||||||
std::string type, name;
|
|
||||||
copyFont->findAttribute("type", type);
|
|
||||||
copyFont->findAttribute("name", name);
|
|
||||||
|
|
||||||
if (name.empty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (Misc::StringUtils::ciEqual(type, "ResourceTrueTypeFont"))
|
|
||||||
{
|
|
||||||
// Since the journal and books use the custom scaling factor depending on resolution,
|
|
||||||
// setup separate fonts with different Resolution to fit these windows.
|
|
||||||
// These fonts have an internal prefix.
|
|
||||||
int resolution = Settings::Manager::getInt("ttf resolution", "GUI");
|
|
||||||
resolution = std::min(960, std::max(48, resolution));
|
|
||||||
|
|
||||||
float currentX = Settings::Manager::getInt("resolution x", "Video");
|
|
||||||
float currentY = Settings::Manager::getInt("resolution y", "Video");
|
|
||||||
// TODO: read size from openmw_layout.xml
|
|
||||||
float heightScale = (currentY / 520);
|
|
||||||
float widthScale = (currentX / 600);
|
|
||||||
float uiScale = std::min(widthScale, heightScale);
|
|
||||||
resolution *= uiScale;
|
|
||||||
|
|
||||||
MyGUI::xml::ElementPtr resolutionNode = copyFont->createChild("Property");
|
|
||||||
resolutionNode->addAttribute("key", "Resolution");
|
|
||||||
resolutionNode->addAttribute("value", std::to_string(resolution));
|
|
||||||
|
|
||||||
copyFont->setAttribute("name", "Journalbook " + name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MyGUI::ResourceManager::getInstance().loadFromXmlNode(copy, _file, _version);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowManager::loadUserFonts()
|
void WindowManager::loadUserFonts()
|
||||||
{
|
{
|
||||||
mFontLoader->loadTrueTypeFonts();
|
mFontLoader->loadTrueTypeFonts();
|
||||||
@ -566,7 +470,7 @@ namespace MWGui
|
|||||||
|
|
||||||
int WindowManager::getFontHeight() const
|
int WindowManager::getFontHeight() const
|
||||||
{
|
{
|
||||||
return mFontHeight;
|
return mFontLoader->getFontHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::setNewGame(bool newgame)
|
void WindowManager::setNewGame(bool newgame)
|
||||||
@ -587,7 +491,6 @@ namespace MWGui
|
|||||||
{
|
{
|
||||||
mKeyboardNavigation.reset();
|
mKeyboardNavigation.reset();
|
||||||
|
|
||||||
MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate("Resource");
|
|
||||||
MyGUI::LanguageManager::getInstance().eventRequestTag.clear();
|
MyGUI::LanguageManager::getInstance().eventRequestTag.clear();
|
||||||
MyGUI::PointerManager::getInstance().eventChangeMousePointer.clear();
|
MyGUI::PointerManager::getInstance().eventChangeMousePointer.clear();
|
||||||
MyGUI::InputManager::getInstance().eventChangeKeyFocus.clear();
|
MyGUI::InputManager::getInstance().eventChangeKeyFocus.clear();
|
||||||
|
@ -411,8 +411,6 @@ namespace MWGui
|
|||||||
MWWorld::Ptr mSelectedEnchantItem;
|
MWWorld::Ptr mSelectedEnchantItem;
|
||||||
MWWorld::Ptr mSelectedWeapon;
|
MWWorld::Ptr mSelectedWeapon;
|
||||||
|
|
||||||
void loadFontDelegate(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version);
|
|
||||||
|
|
||||||
std::vector<WindowModal*> mCurrentModals;
|
std::vector<WindowModal*> mCurrentModals;
|
||||||
|
|
||||||
// Markers placed manually by the player. Must be shared between both map views (the HUD map and the map window).
|
// Markers placed manually by the player. Must be shared between both map views (the HUD map and the map window).
|
||||||
@ -517,8 +515,6 @@ namespace MWGui
|
|||||||
|
|
||||||
ToUTF8::FromType mEncoding;
|
ToUTF8::FromType mEncoding;
|
||||||
|
|
||||||
int mFontHeight;
|
|
||||||
|
|
||||||
std::string mVersionDescription;
|
std::string mVersionDescription;
|
||||||
|
|
||||||
bool mWindowVisible;
|
bool mWindowVisible;
|
||||||
|
@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
#include <components/myguiplatform/myguitexture.hpp>
|
#include <components/myguiplatform/myguitexture.hpp>
|
||||||
|
|
||||||
|
#include <components/settings/settings.hpp>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
unsigned long utf8ToUnicode(const std::string& utf8)
|
unsigned long utf8ToUnicode(const std::string& utf8)
|
||||||
@ -147,15 +149,24 @@ namespace Gui
|
|||||||
FontLoader::FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, const std::string& userDataPath)
|
FontLoader::FontLoader(ToUTF8::FromType encoding, const VFS::Manager* vfs, const std::string& userDataPath)
|
||||||
: mVFS(vfs)
|
: mVFS(vfs)
|
||||||
, mUserDataPath(userDataPath)
|
, mUserDataPath(userDataPath)
|
||||||
|
, mFontHeight(16)
|
||||||
{
|
{
|
||||||
if (encoding == ToUTF8::WINDOWS_1252)
|
if (encoding == ToUTF8::WINDOWS_1252)
|
||||||
mEncoding = ToUTF8::CP437;
|
mEncoding = ToUTF8::CP437;
|
||||||
else
|
else
|
||||||
mEncoding = encoding;
|
mEncoding = encoding;
|
||||||
|
|
||||||
|
int fontSize = Settings::Manager::getInt("font size", "GUI");
|
||||||
|
mFontHeight = std::min(std::max(12, fontSize), 20);
|
||||||
|
|
||||||
|
MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate("Resource");
|
||||||
|
MyGUI::ResourceManager::getInstance().registerLoadXmlDelegate("Resource") = MyGUI::newDelegate(this, &FontLoader::loadFontFromXml);
|
||||||
}
|
}
|
||||||
|
|
||||||
FontLoader::~FontLoader()
|
FontLoader::~FontLoader()
|
||||||
{
|
{
|
||||||
|
MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate("Resource");
|
||||||
|
|
||||||
for (std::vector<MyGUI::ITexture*>::iterator it = mTextures.begin(); it != mTextures.end(); ++it)
|
for (std::vector<MyGUI::ITexture*>::iterator it = mTextures.begin(); it != mTextures.end(); ++it)
|
||||||
delete *it;
|
delete *it;
|
||||||
mTextures.clear();
|
mTextures.clear();
|
||||||
@ -190,7 +201,7 @@ namespace Gui
|
|||||||
{
|
{
|
||||||
size_t pos = name.find_last_of('.');
|
size_t pos = name.find_last_of('.');
|
||||||
if (pos != std::string::npos && name.compare(pos, name.size()-pos, ".fnt") == 0)
|
if (pos != std::string::npos && name.compare(pos, name.size()-pos, ".fnt") == 0)
|
||||||
loadFont(name, exportToFile);
|
loadBitmapFont(name, exportToFile);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
@ -238,7 +249,7 @@ namespace Gui
|
|||||||
float ascent;
|
float ascent;
|
||||||
} GlyphInfo;
|
} GlyphInfo;
|
||||||
|
|
||||||
void FontLoader::loadFont(const std::string &fileName, bool exportToFile)
|
void FontLoader::loadBitmapFont(const std::string &fileName, bool exportToFile)
|
||||||
{
|
{
|
||||||
Files::IStreamPtr file = mVFS->get(fileName);
|
Files::IStreamPtr file = mVFS->get(fileName);
|
||||||
|
|
||||||
@ -527,4 +538,96 @@ namespace Gui
|
|||||||
MyGUI::ResourceManager::getInstance().addResource(bookFont);
|
MyGUI::ResourceManager::getInstance().addResource(bookFont);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FontLoader::loadFontFromXml(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version)
|
||||||
|
{
|
||||||
|
MyGUI::xml::ElementEnumerator resourceNode = _node->getElementEnumerator();
|
||||||
|
bool createCopy = false;
|
||||||
|
while (resourceNode.next("Resource"))
|
||||||
|
{
|
||||||
|
std::string type, name;
|
||||||
|
resourceNode->findAttribute("type", type);
|
||||||
|
resourceNode->findAttribute("name", name);
|
||||||
|
|
||||||
|
if (name.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (Misc::StringUtils::ciEqual(type, "ResourceTrueTypeFont"))
|
||||||
|
{
|
||||||
|
createCopy = true;
|
||||||
|
|
||||||
|
// For TrueType fonts we should override Size and Resolution properties
|
||||||
|
// to allow to configure font size via config file, without need to edit XML files.
|
||||||
|
// Also we should take UI scaling factor in account.
|
||||||
|
int resolution = Settings::Manager::getInt("ttf resolution", "GUI");
|
||||||
|
resolution = std::min(960, std::max(48, resolution));
|
||||||
|
|
||||||
|
float uiScale = Settings::Manager::getFloat("scaling factor", "GUI");
|
||||||
|
resolution *= uiScale;
|
||||||
|
|
||||||
|
MyGUI::xml::ElementPtr resolutionNode = resourceNode->createChild("Property");
|
||||||
|
resolutionNode->addAttribute("key", "Resolution");
|
||||||
|
resolutionNode->addAttribute("value", std::to_string(resolution));
|
||||||
|
|
||||||
|
MyGUI::xml::ElementPtr sizeNode = resourceNode->createChild("Property");
|
||||||
|
sizeNode->addAttribute("key", "Size");
|
||||||
|
sizeNode->addAttribute("value", std::to_string(mFontHeight));
|
||||||
|
}
|
||||||
|
else if (Misc::StringUtils::ciEqual(type, "ResourceSkin") ||
|
||||||
|
Misc::StringUtils::ciEqual(type, "AutoSizedResourceSkin"))
|
||||||
|
{
|
||||||
|
// We should adjust line height for MyGUI widgets depending on font size
|
||||||
|
MyGUI::xml::ElementPtr heightNode = resourceNode->createChild("Property");
|
||||||
|
heightNode->addAttribute("key", "HeightLine");
|
||||||
|
heightNode->addAttribute("value", std::to_string(mFontHeight+2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MyGUI::ResourceManager::getInstance().loadFromXmlNode(_node, _file, _version);
|
||||||
|
|
||||||
|
if (createCopy)
|
||||||
|
{
|
||||||
|
MyGUI::xml::ElementPtr copy = _node->createCopy();
|
||||||
|
|
||||||
|
MyGUI::xml::ElementEnumerator copyFont = copy->getElementEnumerator();
|
||||||
|
while (copyFont.next("Resource"))
|
||||||
|
{
|
||||||
|
std::string type, name;
|
||||||
|
copyFont->findAttribute("type", type);
|
||||||
|
copyFont->findAttribute("name", name);
|
||||||
|
|
||||||
|
if (name.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (Misc::StringUtils::ciEqual(type, "ResourceTrueTypeFont"))
|
||||||
|
{
|
||||||
|
// Since the journal and books use the custom scaling factor depending on resolution,
|
||||||
|
// setup separate fonts with different Resolution to fit these windows.
|
||||||
|
// These fonts have an internal prefix.
|
||||||
|
int resolution = Settings::Manager::getInt("ttf resolution", "GUI");
|
||||||
|
resolution = std::min(960, std::max(48, resolution));
|
||||||
|
|
||||||
|
float currentX = Settings::Manager::getInt("resolution x", "Video");
|
||||||
|
float currentY = Settings::Manager::getInt("resolution y", "Video");
|
||||||
|
// TODO: read size from openmw_layout.xml somehow
|
||||||
|
float heightScale = (currentY / 520);
|
||||||
|
float widthScale = (currentX / 600);
|
||||||
|
float uiScale = std::min(widthScale, heightScale);
|
||||||
|
resolution *= uiScale;
|
||||||
|
|
||||||
|
MyGUI::xml::ElementPtr resolutionNode = copyFont->createChild("Property");
|
||||||
|
resolutionNode->addAttribute("key", "Resolution");
|
||||||
|
resolutionNode->addAttribute("value", std::to_string(resolution));
|
||||||
|
|
||||||
|
copyFont->setAttribute("name", "Journalbook " + name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MyGUI::ResourceManager::getInstance().loadFromXmlNode(copy, _file, _version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int FontLoader::getFontHeight()
|
||||||
|
{
|
||||||
|
return mFontHeight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
#include "boost/filesystem/operations.hpp"
|
#include "boost/filesystem/operations.hpp"
|
||||||
|
|
||||||
|
#include <MyGUI_XmlDocument.h>
|
||||||
|
#include <MyGUI_Version.h>
|
||||||
|
|
||||||
#include <components/myguiplatform/myguidatamanager.hpp>
|
#include <components/myguiplatform/myguidatamanager.hpp>
|
||||||
#include <components/to_utf8/to_utf8.hpp>
|
#include <components/to_utf8/to_utf8.hpp>
|
||||||
|
|
||||||
@ -19,8 +22,6 @@ namespace MyGUI
|
|||||||
|
|
||||||
namespace Gui
|
namespace Gui
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/// @brief loads Morrowind's .fnt/.tex fonts for use with MyGUI and OSG
|
/// @brief loads Morrowind's .fnt/.tex fonts for use with MyGUI and OSG
|
||||||
/// @note The FontLoader needs to remain in scope as long as you want to use the loaded fonts.
|
/// @note The FontLoader needs to remain in scope as long as you want to use the loaded fonts.
|
||||||
class FontLoader
|
class FontLoader
|
||||||
@ -33,16 +34,21 @@ namespace Gui
|
|||||||
void loadBitmapFonts (bool exportToFile);
|
void loadBitmapFonts (bool exportToFile);
|
||||||
void loadTrueTypeFonts ();
|
void loadTrueTypeFonts ();
|
||||||
|
|
||||||
|
void loadFontFromXml(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version);
|
||||||
|
|
||||||
|
int getFontHeight();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ToUTF8::FromType mEncoding;
|
ToUTF8::FromType mEncoding;
|
||||||
const VFS::Manager* mVFS;
|
const VFS::Manager* mVFS;
|
||||||
std::string mUserDataPath;
|
std::string mUserDataPath;
|
||||||
|
int mFontHeight;
|
||||||
|
|
||||||
std::vector<MyGUI::ITexture*> mTextures;
|
std::vector<MyGUI::ITexture*> mTextures;
|
||||||
std::vector<MyGUI::ResourceManualFont*> mFonts;
|
std::vector<MyGUI::ResourceManualFont*> mFonts;
|
||||||
|
|
||||||
/// @param exportToFile export the converted font (Image and XML with glyph metrics) to files?
|
/// @param exportToFile export the converted font (Image and XML with glyph metrics) to files?
|
||||||
void loadFont (const std::string& fileName, bool exportToFile);
|
void loadBitmapFont (const std::string& fileName, bool exportToFile);
|
||||||
|
|
||||||
FontLoader(const FontLoader&);
|
FontLoader(const FontLoader&);
|
||||||
void operator=(const FontLoader&);
|
void operator=(const FontLoader&);
|
||||||
|
@ -38,7 +38,7 @@ namespace Gui
|
|||||||
|
|
||||||
std::string getFontSize()
|
std::string getFontSize()
|
||||||
{
|
{
|
||||||
// Note: we can not use the WindowManager here, so there is a code duplication a bit.
|
// Note: we can not use the FontLoader here, so there is a code duplication a bit.
|
||||||
static const std::string fontSize = std::to_string(clamp(Settings::Manager::getInt("font size", "GUI"), 12, 20));
|
static const std::string fontSize = std::to_string(clamp(Settings::Manager::getInt("font size", "GUI"), 12, 20));
|
||||||
return fontSize;
|
return fontSize;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user