From 4725aa6687308e41ed1ffd1e26fb097109bd1cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 7 Jul 2013 21:30:12 +0200 Subject: [PATCH] MagnumFont: store glyph advance with glyphs, not characters. Allows to store glyph advance also for notfound glyph, which is exactly what we want for unknown characters. Also added version information to allow further extensions. --- src/Plugins/MagnumFont/MagnumFont.cpp | 22 ++++++++++++++++--- .../MagnumFont/Test/MagnumFontTest.cpp | 8 +++---- src/Plugins/MagnumFont/Test/font.conf | 8 +++---- .../MagnumFontConverter.cpp | 5 +++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/Plugins/MagnumFont/MagnumFont.cpp b/src/Plugins/MagnumFont/MagnumFont.cpp index 1880f1332..4c1448f57 100644 --- a/src/Plugins/MagnumFont/MagnumFont.cpp +++ b/src/Plugins/MagnumFont/MagnumFont.cpp @@ -80,6 +80,13 @@ void MagnumFont::doOpenData(const std::vector("version") != 1) { + Error() << "Magnum::Text::MagnumFont::openData(): unsupported file version, expected 1 but got" + << conf.value("version"); + return; + } + /* Check that we have also the image file */ if(conf.value("image") != data[1].first) { Error() << "Magnum::Text::MagnumFont::openData(): expected file" @@ -111,6 +118,13 @@ void MagnumFont::doOpenFile(const std::string& filename, Float) { return; } + /* Check version */ + if(conf.value("version") != 1) { + Error() << "Magnum::Text::MagnumFont::openFile(): unsupported file version, expected 1 but got" + << conf.value("version"); + return; + } + /* Open and load image file */ const std::string imageFilename = Utility::Directory::join(Utility::Directory::path(filename), conf.value("image")); Trade::TgaImporter importer; @@ -133,8 +147,11 @@ void MagnumFont::openInternal(Utility::Configuration&& conf, Trade::ImageData2D& _opened = new Data{std::move(conf), std::move(image), {}, {}}; _size = _opened->conf.value("fontSize"); - /* Set glyph advance array to proper size */ - _opened->glyphAdvance.resize(_opened->conf.groupCount("glyph")); + /* Glyph advances */ + const std::vector glyphs = _opened->conf.groups("glyph"); + _opened->glyphAdvance.reserve(glyphs.size()); + for(const Utility::ConfigurationGroup* const g: glyphs) + _opened->glyphAdvance.push_back(g->value("advance")); /* Fill character->glyph map */ const std::vector chars = _opened->conf.groups("char"); @@ -142,7 +159,6 @@ void MagnumFont::openInternal(Utility::Configuration&& conf, Trade::ImageData2D& const UnsignedInt glyphId = c->value("glyph"); CORRADE_INTERNAL_ASSERT(glyphId < _opened->glyphAdvance.size()); _opened->glyphId.emplace(c->value("unicode"), glyphId); - _opened->glyphAdvance[glyphId] = c->value("advance"); } } diff --git a/src/Plugins/MagnumFont/Test/MagnumFontTest.cpp b/src/Plugins/MagnumFont/Test/MagnumFontTest.cpp index 638abc890..d31dbd4b7 100644 --- a/src/Plugins/MagnumFont/Test/MagnumFontTest.cpp +++ b/src/Plugins/MagnumFont/Test/MagnumFontTest.cpp @@ -77,17 +77,17 @@ void MagnumFontTest::layout() { CORRADE_COMPARE(textureCoordinates, Rectangle({0, 0.03125f}, {0.0625f, 0.5f})); CORRADE_COMPARE(advance, Vector2(0.71875f, 0.0f)); - /* 'a' (not in cache) */ + /* 'a' (not found) */ std::tie(position, textureCoordinates, advance) = layouter->renderGlyph(1); CORRADE_COMPARE(position, Rectangle()); CORRADE_COMPARE(textureCoordinates, Rectangle()); - CORRADE_COMPARE(advance, Vector2(0.34375f, 0.0f)); + CORRADE_COMPARE(advance, Vector2(0.25f, 0.0f)); - /* 'v' (not in cache) */ + /* 'v' (not found) */ std::tie(position, textureCoordinates, advance) = layouter->renderGlyph(2); CORRADE_COMPARE(position, Rectangle()); CORRADE_COMPARE(textureCoordinates, Rectangle()); - CORRADE_COMPARE(advance, Vector2(0.34375f, 0.0f)); + CORRADE_COMPARE(advance, Vector2(0.25f, 0.0f)); /* 'e' */ std::tie(position, textureCoordinates, advance) = layouter->renderGlyph(3); diff --git a/src/Plugins/MagnumFont/Test/font.conf b/src/Plugins/MagnumFont/Test/font.conf index 1b5553a04..4c2a3baea 100644 --- a/src/Plugins/MagnumFont/Test/font.conf +++ b/src/Plugins/MagnumFont/Test/font.conf @@ -1,29 +1,29 @@ +version=1 image=font.tga originalImageSize=1536 1536 padding=24 24 fontSize=16 [char] unicode=57 -advance=23 0 glyph=2 [char] unicode=61 -advance=11 0 glyph=0 [char] unicode=65 -advance=12 0 glyph=1 [char] unicode=76 -advance=11 0 glyph=0 [glyph] +advance=8 0 position=24 24 rectangle=24 24 -24 -24 [glyph] +advance=12 0 position=25 12 rectangle=16 4 64 32 [glyph] +advance=23 0 position=25 34 rectangle=0 8 16 128 diff --git a/src/Plugins/MagnumFontConverter/MagnumFontConverter.cpp b/src/Plugins/MagnumFontConverter/MagnumFontConverter.cpp index 99dfb0b96..7b5282bd4 100644 --- a/src/Plugins/MagnumFontConverter/MagnumFontConverter.cpp +++ b/src/Plugins/MagnumFontConverter/MagnumFontConverter.cpp @@ -46,6 +46,7 @@ auto MagnumFontConverter::doFeatures() const -> Features { std::vector>> MagnumFontConverter::doExportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::u32string& characters) const { Utility::Configuration configuration; + configuration.setValue("version", 1); configuration.setValue("image", Utility::Directory::filename(filename) + ".tga"); configuration.setValue("originalImageSize", cache->textureSize()); configuration.setValue("padding", cache->padding()); @@ -66,12 +67,11 @@ std::vector>> MagnumFont for(const std::pair& map: glyphIdMap) inverseGlyphIdMap[map.second] = map.first; - /* Save character properties, map glyph IDs to new ones */ + /* Character->glyph map, map glyph IDs to new ones */ for(const char32_t c: characters) { Utility::ConfigurationGroup* group = configuration.addGroup("char"); const UnsignedInt glyphId = font->glyphId(c); group->setValue("unicode", c); - group->setValue("advance", font->glyphAdvance(glyphId)); /* Map old glyph ID to new, if not found, map to glyph 0 */ auto found = glyphIdMap.find(glyphId); @@ -84,6 +84,7 @@ std::vector>> MagnumFont for(UnsignedInt oldGlyphId: inverseGlyphIdMap) { std::pair glyph = (*cache)[oldGlyphId]; Utility::ConfigurationGroup* group = configuration.addGroup("glyph"); + group->setValue("advance", font->glyphAdvance(oldGlyphId)); group->setValue("position", glyph.first+cache->padding()); group->setValue("rectangle", Rectanglei(glyph.second.bottomLeft()+cache->padding(), glyph.second.topRight()-cache->padding()));