Browse Source

Merge branch 'master' into compatibility

Conflicts:
	src/MagnumFont/MagnumFont.cpp
Vladimír Vondruš 13 years ago
parent
commit
1920e6383b
  1. 22
      src/Plugins/MagnumFont/MagnumFont.cpp
  2. 8
      src/Plugins/MagnumFont/Test/MagnumFontTest.cpp
  3. 8
      src/Plugins/MagnumFont/Test/font.conf
  4. 7
      src/Plugins/MagnumFontConverter/MagnumFontConverter.cpp
  5. 10
      src/Plugins/TgaImageConverter/Test/TgaImageConverterTest.cpp
  6. 38
      src/Plugins/TgaImageConverter/TgaImageConverter.cpp
  7. 2
      src/Plugins/TgaImageConverter/TgaImageConverter.h
  8. 2
      src/Plugins/TgaImageConverter/pluginRegistrationTgaImageConverter.cpp

22
src/Plugins/MagnumFont/MagnumFont.cpp

@ -84,6 +84,13 @@ void MagnumFont::doOpenData(const std::vector<std::pair<std::string, Containers:
return;
}
/* Check version */
if(conf.value<UnsignedInt>("version") != 1) {
Error() << "Magnum::Text::MagnumFont::openData(): unsupported file version, expected 1 but got"
<< conf.value<UnsignedInt>("version");
return;
}
/* Check that we have also the image file */
if(conf.value("image") != data[1].first) {
Error() << "Magnum::Text::MagnumFont::openData(): expected file"
@ -115,6 +122,13 @@ void MagnumFont::doOpenFile(const std::string& filename, Float) {
return;
}
/* Check version */
if(conf.value<UnsignedInt>("version") != 1) {
Error() << "Magnum::Text::MagnumFont::openFile(): unsupported file version, expected 1 but got"
<< conf.value<UnsignedInt>("version");
return;
}
/* Open and load image file */
const std::string imageFilename = Utility::Directory::join(Utility::Directory::path(filename), conf.value("image"));
Trade::TgaImporter importer;
@ -137,8 +151,11 @@ void MagnumFont::openInternal(Utility::Configuration&& conf, Trade::ImageData2D&
_opened = new Data{std::move(conf), std::move(image), {}, {}};
_size = _opened->conf.value<Float>("fontSize");
/* Set glyph advance array to proper size */
_opened->glyphAdvance.resize(_opened->conf.groupCount("glyph"));
/* Glyph advances */
const std::vector<Utility::ConfigurationGroup*> glyphs = _opened->conf.groups("glyph");
_opened->glyphAdvance.reserve(glyphs.size());
for(const Utility::ConfigurationGroup* const g: glyphs)
_opened->glyphAdvance.push_back(g->value<Vector2>("advance"));
/* Fill character->glyph map */
const std::vector<Utility::ConfigurationGroup*> chars = _opened->conf.groups("char");
@ -151,7 +168,6 @@ void MagnumFont::openInternal(Utility::Configuration&& conf, Trade::ImageData2D&
#else
_opened->glyphId.insert({c->value<char32_t>("unicode"), glyphId});
#endif
_opened->glyphAdvance[glyphId] = c->value<Vector2>("advance");
}
}

8
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);

8
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

7
src/Plugins/MagnumFontConverter/MagnumFontConverter.cpp

@ -46,6 +46,7 @@ auto MagnumFontConverter::doFeatures() const -> Features {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> 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<std::pair<std::string, Containers::Array<unsigned char>>> MagnumFont
for(const std::pair<UnsignedInt, UnsignedInt>& 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<std::pair<std::string, Containers::Array<unsigned char>>> MagnumFont
for(UnsignedInt oldGlyphId: inverseGlyphIdMap) {
std::pair<Vector2i, Rectanglei> 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()));
@ -98,7 +99,7 @@ std::vector<std::pair<std::string, Containers::Array<unsigned char>>> MagnumFont
/* Save cache image */
Image2D image(ImageFormat::Red, ImageType::UnsignedByte);
cache->texture()->image(0, image);
auto tgaData = Trade::TgaImageConverter().exportToData(&image);
auto tgaData = Trade::TgaImageConverter().exportToData(image);
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> out;
out.emplace_back(filename + ".conf", std::move(confData));

10
src/Plugins/TgaImageConverter/Test/TgaImageConverterTest.cpp

@ -68,29 +68,29 @@ TgaImageConverterTest::TgaImageConverterTest() {
}
void TgaImageConverterTest::wrongFormat() {
Image2D image(ImageFormat::RG, ImageType::UnsignedByte, {}, nullptr);
ImageReference2D image(ImageFormat::RG, ImageType::UnsignedByte, {}, nullptr);
std::ostringstream out;
Error::setOutput(&out);
const auto data = TgaImageConverter().exportToData(&image);
const auto data = TgaImageConverter().exportToData(image);
CORRADE_VERIFY(!data);
CORRADE_COMPARE(out.str(), "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image format ImageFormat::RG\n");
}
void TgaImageConverterTest::wrongType() {
Image2D image(ImageFormat::Red, ImageType::Float, {}, nullptr);
ImageReference2D image(ImageFormat::Red, ImageType::Float, {}, nullptr);
std::ostringstream out;
Error::setOutput(&out);
const auto data = TgaImageConverter().exportToData(&image);
const auto data = TgaImageConverter().exportToData(image);
CORRADE_VERIFY(!data);
CORRADE_COMPARE(out.str(), "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image type ImageType::Float\n");
}
void TgaImageConverterTest::data() {
const auto data = TgaImageConverter().exportToData(&original);
const auto data = TgaImageConverter().exportToData(original);
TgaImporter importer;
CORRADE_VERIFY(importer.openData(data));

38
src/Plugins/TgaImageConverter/TgaImageConverter.cpp

@ -46,48 +46,48 @@ TgaImageConverter::TgaImageConverter(PluginManager::AbstractManager* manager, st
auto TgaImageConverter::doFeatures() const -> Features { return Feature::ConvertData; }
Containers::Array<unsigned char> TgaImageConverter::doExportToData(const Image2D* const image) const {
Containers::Array<unsigned char> TgaImageConverter::doExportToData(const ImageReference2D& image) const {
#ifndef MAGNUM_TARGET_GLES
if(image->format() != ImageFormat::BGR &&
image->format() != ImageFormat::BGRA &&
image->format() != ImageFormat::Red)
if(image.format() != ImageFormat::BGR &&
image.format() != ImageFormat::BGRA &&
image.format() != ImageFormat::Red)
#else
if(image->format() != ImageFormat::RGB &&
image->format() != ImageFormat::RGBA &&
image->format() != ImageFormat::Red)
if(image.format() != ImageFormat::RGB &&
image.format() != ImageFormat::RGBA &&
image.format() != ImageFormat::Red)
#endif
{
Error() << "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image format" << image->format();
Error() << "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image format" << image.format();
return nullptr;
}
if(image->type() != ImageType::UnsignedByte) {
Error() << "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image type" << image->type();
if(image.type() != ImageType::UnsignedByte) {
Error() << "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image type" << image.type();
return nullptr;
}
/* Initialize data buffer */
const UnsignedByte pixelSize = image->pixelSize();
auto data = Containers::Array<unsigned char>::zeroInitialized(sizeof(TgaHeader) + pixelSize*image->size().product());
const UnsignedByte pixelSize = image.pixelSize();
auto data = Containers::Array<unsigned char>::zeroInitialized(sizeof(TgaHeader) + pixelSize*image.size().product());
/* Fill header */
auto header = reinterpret_cast<TgaHeader*>(data.begin());
header->imageType = image->format() == ImageFormat::Red ? 3 : 2;
header->imageType = image.format() == ImageFormat::Red ? 3 : 2;
header->bpp = pixelSize*8;
header->width = Utility::Endianness::littleEndian(image->size().x());
header->height = Utility::Endianness::littleEndian(image->size().y());
header->width = Utility::Endianness::littleEndian(image.size().x());
header->height = Utility::Endianness::littleEndian(image.size().y());
/* Fill data */
std::copy(image->data(), image->data()+pixelSize*image->size().product(), data.begin()+sizeof(TgaHeader));
std::copy(image.data(), image.data()+pixelSize*image.size().product(), data.begin()+sizeof(TgaHeader));
#ifdef MAGNUM_TARGET_GLES
if(image->format() == ImageFormat::RGB) {
auto pixels = reinterpret_cast<Math::Vector3<UnsignedByte>*>(data.begin()+sizeof(TgaHeader));
std::transform(pixels, pixels + image->size().product(), pixels,
std::transform(pixels, pixels + image.size().product(), pixels,
[](Math::Vector3<UnsignedByte> pixel) { return swizzle<'b', 'g', 'r'>(pixel); });
} else if(image->format() == ImageFormat::RGBA) {
} else if(image.format() == ImageFormat::RGBA) {
auto pixels = reinterpret_cast<Math::Vector4<UnsignedByte>*>(data.begin()+sizeof(TgaHeader));
std::transform(pixels, pixels + image->size().product(), pixels,
std::transform(pixels, pixels + image.size().product(), pixels,
[](Math::Vector4<UnsignedByte> pixel) { return swizzle<'b', 'g', 'r', 'a'>(pixel); });
}
#endif

2
src/Plugins/TgaImageConverter/TgaImageConverter.h

@ -58,7 +58,7 @@ class MAGNUM_TRADE_TGAIMAGECONVERTER_EXPORT TgaImageConverter: public AbstractIm
private:
Features MAGNUM_TRADE_TGAIMAGECONVERTER_LOCAL doFeatures() const override;
Containers::Array<unsigned char> MAGNUM_TRADE_TGAIMAGECONVERTER_LOCAL doExportToData(const Image2D* const image) const override;
Containers::Array<unsigned char> MAGNUM_TRADE_TGAIMAGECONVERTER_LOCAL doExportToData(const ImageReference2D& image) const override;
};
}}

2
src/Plugins/TgaImageConverter/pluginRegistrationTgaImageConverter.cpp

@ -25,4 +25,4 @@
#include "TgaImageConverter/TgaImageConverter.h"
CORRADE_PLUGIN_REGISTER(TgaImageConverter, Magnum::Trade::TgaImageConverter,
"cz.mosra.magnum.Trade.AbstractImageConverter/0.2")
"cz.mosra.magnum.Trade.AbstractImageConverter/0.2.1")

Loading…
Cancel
Save