Load fonts

This commit is contained in:
Andrei Kortunov 2022-07-07 19:34:18 +04:00
parent 7c442926f8
commit dd04bfccfb
23 changed files with 430 additions and 30 deletions

View file

@ -214,17 +214,17 @@ namespace Gui
return;
}
const std::string cfg = dataManager->getDataPath("");
const std::string fontFile = mUserDataPath + "/" + "Fonts" + "/" + "openmw_font.xml";
if (!std::filesystem::exists(fontFile))
return;
dataManager->setUseVfs(true);
dataManager->setResourcePath(mUserDataPath + "/" + "Fonts");
MyGUI::ResourceManager::getInstance().load("openmw_font.xml");
dataManager->setResourcePath(cfg);
for (const auto& name : mVFS->getRecursiveDirectoryIterator("Fonts/"))
{
if (Misc::getFileExtension(name) == "omwfont")
MyGUI::ResourceManager::getInstance().load(name);
}
dataManager->setUseVfs(false);
}
typedef struct
{
float x;

View file

@ -18,8 +18,25 @@ void DataManager::setResourcePath(const std::string &path)
mResourcePath = path;
}
void DataManager::setUseVfs(bool useVfs)
{
mUseVfs = useVfs;
}
MyGUI::IDataStream *DataManager::getData(const std::string &name) const
{
if (mUseVfs)
{
// Note: MyGUI is supposed to read/free input steam itself,
// so copy data from VFS stream to the string stream and pass it to MyGUI.
Files::IStreamPtr streamPtr = mVfs->get(name);
std::istream* fileStream = streamPtr.get();
std::unique_ptr<std::stringstream> dataStream;
dataStream.reset(new std::stringstream);
*dataStream << fileStream->rdbuf();
return new MyGUI::DataStream(dataStream.release());
}
std::string fullpath = getDataPath(name);
auto stream = std::make_unique<std::ifstream>();
stream->open(fullpath, std::ios::binary);
@ -38,10 +55,17 @@ void DataManager::freeData(MyGUI::IDataStream *data)
bool DataManager::isDataExist(const std::string &name) const
{
if (mUseVfs) return mVfs->exists(name);
std::string fullpath = mResourcePath + "/" + name;
return std::filesystem::exists(fullpath);
}
void DataManager::setVfs(const VFS::Manager* vfs)
{
mVfs = vfs;
}
const MyGUI::VectorString &DataManager::getDataListNames(const std::string &pattern) const
{
// TODO: pattern matching (unused?)
@ -53,6 +77,9 @@ const MyGUI::VectorString &DataManager::getDataListNames(const std::string &patt
const std::string &DataManager::getDataPath(const std::string &name) const
{
// FIXME: in theory, we should use the VFS here too, but it does not provide the real path to data files.
// In some cases there is no real path at all (when the requested MyGUI file is in BSA archive, for example).
// Currently it should not matter since we use this virtual function only to setup fonts for profilers.
static std::string result;
result.clear();
if (!isDataExist(name))

View file

@ -5,6 +5,8 @@
#include <string>
#include <components/vfs/manager.hpp>
namespace osgMyGUI
{
@ -16,6 +18,10 @@ public:
void setResourcePath(const std::string& path);
void setUseVfs(bool useVfs);
void setVfs(const VFS::Manager* vfs);
/** Get data stream from specified resource name.
@param _name Resource name (usually file name).
*/
@ -44,6 +50,10 @@ public:
private:
std::string mResourcePath;
const VFS::Manager* mVfs;
bool mUseVfs{false};
};
}

View file

@ -30,7 +30,7 @@ Platform::~Platform()
mLogFacility = nullptr;
}
void Platform::initialise(const std::string &resourcePath, const std::string &_logName)
void Platform::initialise(const VFS::Manager* vfs, const std::string &resourcePath, const std::string &_logName)
{
if (!_logName.empty() && !mLogFacility)
{
@ -39,6 +39,7 @@ void Platform::initialise(const std::string &resourcePath, const std::string &_l
}
mDataManager->setResourcePath(resourcePath);
mDataManager->setVfs(vfs);
mRenderManager->initialise();
mDataManager->initialise();

View file

@ -3,6 +3,8 @@
#include <string>
#include <components/vfs/manager.hpp>
namespace osgViewer
{
class Viewer;
@ -34,7 +36,7 @@ namespace osgMyGUI
~Platform();
void initialise(const std::string& resourcePath, const std::string& _logName = "MyGUI.log");
void initialise(const VFS::Manager* vfs, const std::string& resourcePath, const std::string& _logName = "MyGUI.log");
void shutdown();

View file

@ -15,6 +15,8 @@
#include <components/myguiplatform/myguidatamanager.hpp>
#include <components/vfs/manager.hpp>
namespace Resource
{
@ -28,6 +30,8 @@ static bool collectStatFrameRate = false;
static bool collectStatUpdate = false;
static bool collectStatEngine = false;
inline static std::string sFontName = "Fonts\\DejaVuLGCSansMono.ttf";
static void setupStatCollection()
{
const char* envList = getenv("OPENMW_OSG_STATS_LIST");
@ -79,7 +83,7 @@ static void setupStatCollection()
}
}
StatsHandler::StatsHandler(bool offlineCollect):
StatsHandler::StatsHandler(bool offlineCollect, VFS::Manager* vfs):
_key(osgGA::GUIEventAdapter::KEY_F4),
_initialized(false),
_statsType(false),
@ -96,15 +100,19 @@ StatsHandler::StatsHandler(bool offlineCollect):
_resourceStatsChildNum = 0;
if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf"))
_font = osgMyGUI::DataManager::getInstance().getDataPath("DejaVuLGCSansMono.ttf");
if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf") && vfs->exists(sFontName))
{
_font = vfs->getAbsoluteFileName(sFontName);
}
}
Profiler::Profiler(bool offlineCollect):
Profiler::Profiler(bool offlineCollect, VFS::Manager* vfs):
_offlineCollect(offlineCollect)
{
if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf"))
_font = osgMyGUI::DataManager::getInstance().getDataPath("DejaVuLGCSansMono.ttf");
if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf") && vfs->exists(sFontName))
{
_font = vfs->getAbsoluteFileName(sFontName);
}
else
_font.clear();

View file

@ -13,12 +13,17 @@ namespace osg
class Switch;
}
namespace VFS
{
class Manager;
}
namespace Resource
{
class Profiler : public osgViewer::StatsHandler
{
public:
Profiler(bool offlineCollect);
Profiler(bool offlineCollect, VFS::Manager* vfs);
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override;
private:
@ -28,7 +33,7 @@ namespace Resource
class StatsHandler : public osgGA::GUIEventHandler
{
public:
StatsHandler(bool offlineCollect);
StatsHandler(bool offlineCollect, VFS::Manager* vfs);
void setKey(int key) { _key = key; }
int getKey() const { return _key; }