Browse Source

Text: expect opened font in all AbstractFont accessors.

Consistency is a king. This might be a perf penalty, but it prevents
accidents (and the user code can always cache the value), so it's
warranted.
audio-import
Vladimír Vondruš 7 years ago
parent
commit
25aa1b8c22
  1. 4
      doc/changelog.dox
  2. 20
      src/Magnum/Text/AbstractFont.cpp
  3. 29
      src/Magnum/Text/AbstractFont.h
  4. 59
      src/Magnum/Text/Test/AbstractFontTest.cpp

4
doc/changelog.dox

@ -147,6 +147,10 @@ See also:
- For consistency with @ref Trade::AbstractImporter, @ref Text::AbstractFont
now doesn't fail when opening empty files --- instead this is handled in
the particular plugin implementations
- @ref Text::AbstractFont::size(), @ref Text::AbstractFont::ascent(),
@ref Text::AbstractFont::descent() and @ref Text::AbstractFont::lineHeight()
now expect that a font is opened, consistently with other accessor
functions
@subsubsection changelog-latest-changes-trade Trade library

20
src/Magnum/Text/AbstractFont.cpp

@ -193,6 +193,26 @@ void AbstractFont::close() {
}
}
Float AbstractFont::size() const {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::size(): no font opened", {});
return _size;
}
Float AbstractFont::ascent() const {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::ascent(): no font opened", {});
return _ascent;
}
Float AbstractFont::descent() const {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::descent(): no font opened", {});
return _descent;
}
Float AbstractFont::lineHeight() const {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::lineHeight(): no font opened", {});
return _lineHeight;
}
UnsignedInt AbstractFont::glyphId(const char32_t character) {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::glyphId(): no font opened", 0);

29
src/Magnum/Text/AbstractFont.h

@ -324,38 +324,42 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
* @brief Font size
*
* Returns scale in which @ref lineHeight(), @ref ascent(),
* @ref descent() and @ref glyphAdvance() is returned.
* @ref descent() and @ref glyphAdvance() is returned. Expects that a
* font is opened.
*/
Float size() const { return _size; }
Float size() const;
/**
* @brief Font ascent
*
* Distance from baseline to top, scaled to font size. Positive value.
* Expects that a font is opened.
* @see @ref size(), @ref descent(), @ref lineHeight()
*/
Float ascent() const { return _ascent; }
Float ascent() const;
/**
* @brief Font descent
*
* Distance from baseline to bottom, scalled to font size. Negative
* value.
* value. Expects that a font is opened.
* @see @ref size(), @ref ascent(), @ref lineHeight()
*/
Float descent() const { return _descent; }
Float descent() const;
/**
* @brief Line height
*
* Returns line height scaled to font size.
* Returns line height scaled to font size. Expects that a font is
* opened.
* @see @ref size(), @ref ascent(), @ref descent()
*/
Float lineHeight() const { return _lineHeight; }
Float lineHeight() const;
/**
* @brief Glyph ID for given character
*
* Expects that a font is opened.
* @note This function is meant to be used only for font observations
* and conversions. In performance-critical code the @ref layout()
* function should be used instead.
@ -366,7 +370,8 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
* @brief Glyph advance
* @param glyph Glyph ID
*
* Returns glyph advance scaled to font size.
* Returns glyph advance scaled to font size. Expects that a font is
* opened.
* @note This function is meant to be used only for font observations
* and conversions. In performance-critical code the @ref layout()
* function should be used instead.
@ -381,7 +386,8 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
*
* Fills the cache with given characters. Fonts having
* @ref Feature::PreparedGlyphCache do not support partial glyph cache
* filling, use @ref createGlyphCache() instead.
* filling, use @ref createGlyphCache() instead. Expects that a font is
* opened.
*/
void fillGlyphCache(AbstractGlyphCache& cache, const std::string& characters);
@ -391,7 +397,7 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
* Configures and fills glyph cache with the contents of whole font.
* Available only if @ref Feature::PreparedGlyphCache is supported.
* Other fonts support only partial glyph cache filling, see
* @ref fillGlyphCache().
* @ref fillGlyphCache(). Expects that a font is opened.
*/
Containers::Pointer<AbstractGlyphCache> createGlyphCache();
@ -402,7 +408,8 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
* @param text Text to layout
*
* Note that the layouters support rendering of single-line text only.
* See @ref Renderer class for more advanced text layouting.
* See @ref Renderer class for more advanced text layouting. Expects
* that a font is opened.
* @see @ref fillGlyphCache(), @ref createGlyphCache()
*/
Containers::Pointer<AbstractLayouter> layout(const AbstractGlyphCache& cache, Float size, const std::string& text);

59
src/Magnum/Text/Test/AbstractFontTest.cpp

@ -69,6 +69,9 @@ struct AbstractFontTest: TestSuite::Tester {
void setFileCallbackOpenFileAsData();
void setFileCallbackOpenFileAsDataFailed();
void properties();
void propertiesNoFont();
void glyphId();
void glyphIdNoFont();
@ -121,6 +124,9 @@ AbstractFontTest::AbstractFontTest() {
&AbstractFontTest::setFileCallbackOpenFileAsData,
&AbstractFontTest::setFileCallbackOpenFileAsDataFailed,
&AbstractFontTest::properties,
&AbstractFontTest::propertiesNoFont,
&AbstractFontTest::glyphId,
&AbstractFontTest::glyphIdNoFont,
@ -776,6 +782,59 @@ void AbstractFontTest::setFileCallbackOpenFileAsDataFailed() {
CORRADE_COMPARE(out.str(), "Text::AbstractFont::openFile(): cannot open file file.dat\n");
}
void AbstractFontTest::properties() {
struct: AbstractFont {
Features doFeatures() const override { return Feature::OpenData; }
bool doIsOpened() const override { return _opened; }
void doClose() override {}
Metrics doOpenData(const Containers::ArrayView<const char>, Float size) override {
_opened = true;
return {size, 1.0f, 2.0f, 3.0f};
}
UnsignedInt doGlyphId(char32_t) override { return {}; }
Vector2 doGlyphAdvance(UnsignedInt) override { return {}; }
Containers::Pointer<AbstractLayouter> doLayout(const AbstractGlyphCache&, Float, const std::string&) override {
return nullptr;
}
bool _opened = false;
} font;
CORRADE_VERIFY(font.openData(nullptr, 13.0f));
CORRADE_COMPARE(font.size(), 13.0f);
CORRADE_COMPARE(font.ascent(), 1.0f);
CORRADE_COMPARE(font.descent(), 2.0f);
CORRADE_COMPARE(font.lineHeight(), 3.0f);
}
void AbstractFontTest::propertiesNoFont() {
struct MyFont: AbstractFont {
Features doFeatures() const override { return {}; }
bool doIsOpened() const override { return false; }
void doClose() override {}
UnsignedInt doGlyphId(char32_t) override { return {}; }
Vector2 doGlyphAdvance(UnsignedInt) override { return {}; }
Containers::Pointer<AbstractLayouter> doLayout(const AbstractGlyphCache&, Float, const std::string&) override {
return nullptr;
}
} font;
std::ostringstream out;
Error redirectError{&out};
font.size();
font.ascent();
font.descent();
font.lineHeight();
CORRADE_COMPARE(out.str(),
"Text::AbstractFont::size(): no font opened\n"
"Text::AbstractFont::ascent(): no font opened\n"
"Text::AbstractFont::descent(): no font opened\n"
"Text::AbstractFont::lineHeight(): no font opened\n");
}
void AbstractFontTest::glyphId() {
struct MyFont: AbstractFont {
Features doFeatures() const override { return {}; }

Loading…
Cancel
Save