Skip to content

Commit 620d63f

Browse files
committed
Overlay: Font - add merge_fonts property
1 parent f0b87ee commit 620d63f

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

Components/Overlay/include/OgreFont.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ THE SOFTWARE
2828
#define _Font_H__
2929

3030
#include "OgreOverlayPrerequisites.h"
31+
#include "OgrePrerequisites.h"
3132
#include "OgreResource.h"
3233
#include "OgreCommon.h"
3334
#include "OgreSharedPtr.h"
@@ -44,6 +45,8 @@ namespace Ogre
4445
/** \addtogroup Overlays
4546
* @{
4647
*/
48+
class Font;
49+
typedef SharedPtr<Font> FontPtr;
4750

4851
/// decode UTF8 encoded bytestream to uint32 codepoints
4952
_OgreOverlayExport std::vector<uint32> utftoc32(String str);
@@ -124,12 +127,17 @@ namespace Ogre
124127
/// Range of code points to generate glyphs for (truetype only)
125128
CodePointRangeList mCodePointRangeList;
126129

130+
std::vector<FontPtr> mMergeFonts;
131+
127132
/// Internal method for loading from ttf
128133
void createTextureFromFont(void);
129134

130135
void loadImpl() override;
131136
void unloadImpl() override;
132137
size_t calculateSize(void) const override { return 0; } // permanent resource is in the texture
138+
139+
friend class ImGuiOverlay;
140+
DataStreamPtr _getTTFData();
133141
public:
134142

135143
/** Constructor.
@@ -287,6 +295,16 @@ namespace Ogre
287295
{
288296
return mCodePointRangeList;
289297
}
298+
299+
/** Add a font to merge with this one.
300+
301+
This is useful when you want to use a font for most of the characters, but
302+
fall back to another font for characters not present in the current font. e.g. icons.
303+
*/
304+
void addMergeFont(const FontPtr& font) { mMergeFonts.push_back(font); }
305+
306+
const std::vector<FontPtr>& getMergeFontList() const { return mMergeFonts; }
307+
290308
/** Gets the material generated for this font, as a weak reference.
291309
292310
This will only be valid after the Font has been loaded.
@@ -343,8 +361,6 @@ namespace Ogre
343361
*/
344362
void _setMaterial(const MaterialPtr& mat);
345363
};
346-
347-
typedef SharedPtr<Font> FontPtr;
348364
/** @} */
349365
/** @} */
350366
}

Components/Overlay/src/OgreFont.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,15 @@ namespace Ogre
328328
mTexture->load();
329329
}
330330
//---------------------------------------------------------------------
331+
DataStreamPtr Font::_getTTFData()
332+
{
333+
// Locate ttf file, load it pre-buffered into memory by wrapping
334+
// the original DataStream in a MemoryDataStream
335+
return ResourceGroupManager::getSingleton().openResource(mSource, mGroup, this);
336+
}
331337
void Font::loadResource(Resource* res)
332338
{
333-
// Locate ttf file, load it pre-buffered into memory by wrapping the
334-
// original DataStream in a MemoryDataStream
335-
DataStreamPtr dataStreamPtr =
336-
ResourceGroupManager::getSingleton().openResource(
337-
mSource, mGroup, this);
338-
MemoryDataStream ttfchunk(dataStreamPtr);
339+
MemoryDataStream ttfchunk = _getTTFData();
339340

340341
// If codepoints not supplied, assume ASCII
341342
if (mCodePointRangeList.empty())

Components/Overlay/src/OgreImGuiOverlay.cpp

+28-5
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ ImFont* ImGuiOverlay::addFont(const String& name, const String& group)
123123
StringUtil::format("Font '%s' not found in group '%s'", name.c_str(), group.c_str()));
124124

125125
OgreAssert(font->getType() == FT_TRUETYPE, "font must be of FT_TRUETYPE");
126-
DataStreamPtr dataStreamPtr =
127-
ResourceGroupManager::getSingleton().openResource(font->getSource(), font->getGroup());
128-
MemoryDataStream ttfchunk(dataStreamPtr, false); // transfer ownership to imgui
126+
MemoryDataStream ttfchunk(font->_getTTFData(), false); // transfer ownership to imgui
129127

130128
// convert codepoint ranges for imgui
131129
CodePointRange cprange;
@@ -149,8 +147,33 @@ ImFont* ImGuiOverlay::addFont(const String& name, const String& group)
149147

150148
ImFontConfig cfg;
151149
strncpy(cfg.Name, name.c_str(), IM_ARRAYSIZE(cfg.Name) - 1);
152-
return io.Fonts->AddFontFromMemoryTTF(ttfchunk.getPtr(), ttfchunk.size(), font->getTrueTypeSize() * vpScale, &cfg,
153-
cprangePtr);
150+
auto* ret = io.Fonts->AddFontFromMemoryTTF(ttfchunk.getPtr(), ttfchunk.size(), font->getTrueTypeSize() * vpScale,
151+
&cfg, cprangePtr);
152+
153+
cfg.MergeMode = true;
154+
155+
for(const auto& mergeFont : font->getMergeFontList())
156+
{
157+
MemoryDataStream mergeTtfchunk(mergeFont->_getTTFData(), false); // transfer ownership to imgui
158+
159+
CodePointRange mergeCprange;
160+
for (const auto& r : mergeFont->getCodePointRangeList())
161+
{
162+
mergeCprange.push_back(r.first);
163+
mergeCprange.push_back(r.second);
164+
}
165+
166+
OgreAssert(!mergeCprange.empty(), "merge font must have codepoint ranges");
167+
mergeCprange.push_back(0); // terminate
168+
mCodePointRanges.push_back(mergeCprange);
169+
cprangePtr = mCodePointRanges.back().data();
170+
171+
cfg.MergeMode = true;
172+
ret = io.Fonts->AddFontFromMemoryTTF(mergeTtfchunk.getPtr(), mergeTtfchunk.size(),
173+
mergeFont->getTrueTypeSize() * vpScale, &cfg, cprangePtr);
174+
}
175+
176+
return ret;
154177
}
155178

156179
void ImGuiOverlay::ImGUIRenderable::createFontTexture()

Components/Overlay/src/OgreOverlayTranslator.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,31 @@ void FontTranslator::parseAttribute(ScriptCompiler* compiler, FontPtr& pFont,
106106
pFont->setAntialiasColour(flag);
107107
compiler->addError(ScriptCompiler::CE_DEPRECATEDSYMBOL, prop->file, prop->line, attrib);
108108
}
109+
else if(attrib == "merge_fonts")
110+
{
111+
if (prop->values.empty())
112+
{
113+
compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line);
114+
return;
115+
}
116+
117+
for (auto& v : prop->values)
118+
{
119+
if (!getString(v, &val))
120+
{
121+
compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line);
122+
return;
123+
}
124+
125+
auto mergeFont = FontManager::getSingleton().getByName(val, pFont->getGroup());
126+
if (!mergeFont)
127+
{
128+
compiler->addError(ScriptCompiler::CE_REFERENCETOANONEXISTINGOBJECT, prop->file, prop->line, val);
129+
return;
130+
}
131+
pFont->addMergeFont(mergeFont);
132+
}
133+
}
109134
else if (attrib == "code_points")
110135
{
111136
if (prop->values.empty())

Docs/src/scripts.md

+2
Original file line numberDiff line numberDiff line change
@@ -1299,4 +1299,6 @@ Here are the attributes you need to supply:
12991299

13001300
@param code\_points <b>nn-nn \[nn-nn\] ..</b> This directive allows you to specify which unicode code points should be generated as glyphs into the font texture. If you don’t specify this, code points 32-126 will be generated by default which covers the ASCII glyphs. If you use this flag, you should specify a space-separated list of inclusive code point ranges of the form ’start-end’. Numbers must be decimal.
13011301

1302+
@param merge_font **name [name2]** @copydetails Ogre::Font::addMergeFont
1303+
13021304
You can also create new fonts at runtime by using the Ogre::FontManager if you wish.

0 commit comments

Comments
 (0)