Browse Source

Text: allow font to fully create its glyph cache.

Some fonts are able to add individual characters to cache, some need to
fully configure whole cache.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
731aac7d74
  1. 28
      src/Text/AbstractFont.cpp
  2. 36
      src/Text/AbstractFont.h
  3. 2
      src/Text/Test/AbstractFontTest.cpp

28
src/Text/AbstractFont.cpp

@ -115,12 +115,30 @@ Vector2 AbstractFont::glyphAdvance(const UnsignedInt glyph) {
return doGlyphAdvance(glyph); return doGlyphAdvance(glyph);
} }
void AbstractFont::createGlyphCache(GlyphCache* const cache, const std::string& characters) { void AbstractFont::fillGlyphCache(GlyphCache* const cache, const std::string& characters) {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::createGlyphCache(): no font opened", ); CORRADE_ASSERT(isOpened(),
CORRADE_ASSERT(!characters.empty() || features() & Feature::Enumerable, "Text::AbstractFont::createGlyphCache(): no font opened", );
"Text::AbstractFont::createGlyphCache(): the font is not enumerable, can't create cache from all characters", ); CORRADE_ASSERT(!(features() & Feature::PreparedGlyphCache),
"Text::AbstractFont::fillGlyphCache(): feature not supported", );
doCreateGlyphCache(cache, Utility::Unicode::utf32(characters)); doFillGlyphCache(cache, Utility::Unicode::utf32(characters));
}
void AbstractFont::doFillGlyphCache(GlyphCache*, const std::u32string&) {
CORRADE_ASSERT(false, "Text::AbstractFont::fillGlyphCache(): feature advertised but not implemented", );
}
GlyphCache* AbstractFont::createGlyphCache() {
CORRADE_ASSERT(isOpened(),
"Text::AbstractFont::createGlyphCache(): no font opened", nullptr);
CORRADE_ASSERT(features() & Feature::PreparedGlyphCache,
"Text::AbstractFont::createGlyphCache(): feature not supported", nullptr);
return doCreateGlyphCache();
}
GlyphCache* AbstractFont::doCreateGlyphCache() {
CORRADE_ASSERT(false, "Text::AbstractFont::createGlyphCache(): feature advertised but not implemented", nullptr);
} }
AbstractLayouter* AbstractFont::layout(const GlyphCache* const cache, const Float size, const std::string& text) { AbstractLayouter* AbstractFont::layout(const GlyphCache* const cache, const Float size, const std::string& text) {

36
src/Text/AbstractFont.h

@ -84,10 +84,11 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
MultiFile = 1 << 1, MultiFile = 1 << 1,
/** /**
* The font is enumerable, i.e. it is possible to loop over all * The font contains prepared glyph cache.
* characters in the font. *
* @see fillGlyphCache(), createGlyphCache()
*/ */
Enumerable = 1 << 2 PreparedGlyphCache = 1 << 2
}; };
/** @brief Set of features supported by this importer */ /** @brief Set of features supported by this importer */
@ -166,15 +167,25 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
Vector2 glyphAdvance(UnsignedInt glyph); Vector2 glyphAdvance(UnsignedInt glyph);
/** /**
* @brief Create glyph cache for given character set * @brief Fill glyph cache with given character set
* @param cache Glyph cache instance * @param cache Glyph cache instance
* @param characters UTF-8 characters to render * @param characters UTF-8 characters to render
* *
* Fills the cache with given characters. If @ref Feature "Feature::Enumerable" * Fills the cache with given characters. Fonts having
* is supported, @p characters can be empty and all glyphs from given * @ref Feature "Feature::PreparedGlyphCache" do not support partial
* font will be added to the cache. * glyph cache filling, use createGlyphCache() instead.
*/ */
void createGlyphCache(GlyphCache* cache, const std::string& characters); void fillGlyphCache(GlyphCache* cache, const std::string& characters);
/**
* @brief Create glyph cache
*
* Configures and fills glyph cache with the contents of whole font.
* Available only if @ref Feature "Feature::PreparedGlyphCache" is
* supported. Other fonts support only partial glyph cache filling,
* see fillGlyphCache().
*/
GlyphCache* createGlyphCache();
/** /**
* @brief Layout the text using font's own layouter * @brief Layout the text using font's own layouter
@ -182,7 +193,7 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
* @param size Font size * @param size Font size
* @param text %Text to layout * @param text %Text to layout
* *
* @see createGlyphCache() * @see fillGlyphCache(), createGlyphCache()
*/ */
AbstractLayouter* layout(const GlyphCache* cache, Float size, const std::string& text); AbstractLayouter* layout(const GlyphCache* cache, Float size, const std::string& text);
@ -240,7 +251,12 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
* The string is converted from UTF-8 to UTF-32, unique characters are * The string is converted from UTF-8 to UTF-32, unique characters are
* *not* removed. * *not* removed.
*/ */
virtual void doCreateGlyphCache(GlyphCache* cache, const std::u32string& characters) = 0; virtual void doFillGlyphCache(GlyphCache* cache, const std::u32string& characters);
/**
* @brief Implementation for createGlyphCache()
*/
virtual GlyphCache* doCreateGlyphCache();
/** @brief Implementation for layout() */ /** @brief Implementation for layout() */
virtual AbstractLayouter* doLayout(const GlyphCache* cache, Float size, const std::string& text) = 0; virtual AbstractLayouter* doLayout(const GlyphCache* cache, Float size, const std::string& text) = 0;

2
src/Text/Test/AbstractFontTest.cpp

@ -57,8 +57,6 @@ class SingleDataFont: public Text::AbstractFont {
opened = (data.size() == 1 && data[0] == 0xa5); opened = (data.size() == 1 && data[0] == 0xa5);
} }
void doCreateGlyphCache(GlyphCache*, const std::u32string&) override {}
UnsignedInt doGlyphId(char32_t) override { return 0; } UnsignedInt doGlyphId(char32_t) override { return 0; }
Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; }

Loading…
Cancel
Save