mirror of https://github.com/mosra/magnum.git
13 changed files with 541 additions and 208 deletions
@ -0,0 +1,66 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "DistanceFieldGlyphCache.h" |
||||
|
||||
#include "Extensions.h" |
||||
#include "Image.h" |
||||
#include "TextureTools/DistanceField.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
namespace { |
||||
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3) |
||||
const AbstractTexture::InternalFormat internalFormat = AbstractTexture::InternalFormat::R8; |
||||
#else |
||||
const AbstractTexture::InternalFormat internalFormat = AbstractTexture::InternalFormat::Red; |
||||
#endif |
||||
} |
||||
|
||||
DistanceFieldGlyphCache::DistanceFieldGlyphCache(const Vector2i& originalSize, const Vector2i& distanceFieldSize, UnsignedInt radius): GlyphCache(originalSize, Vector2i(radius)), scale(Vector2(distanceFieldSize)/originalSize), radius(radius) { |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); |
||||
#else |
||||
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); |
||||
#endif |
||||
|
||||
initialize(internalFormat, distanceFieldSize); |
||||
} |
||||
|
||||
void DistanceFieldGlyphCache::setImage(const Vector2i& offset, Image2D* const image) { |
||||
Texture2D input; |
||||
input.setWrapping(Texture2D::Wrapping::ClampToEdge) |
||||
->setMinificationFilter(Texture2D::Filter::Linear) |
||||
->setMagnificationFilter(Texture2D::Filter::Linear) |
||||
->setImage(0, internalFormat, image); |
||||
|
||||
/* Create distance field from input texture */ |
||||
TextureTools::distanceField(&input, &_texture, Rectanglei::fromSize(offset*scale, image->size()*scale), radius); |
||||
} |
||||
|
||||
void DistanceFieldGlyphCache::setDistanceFieldImage(const Vector2i& offset, Image2D* const image) { |
||||
_texture.setSubImage(0, offset, image); |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,92 @@
|
||||
#ifndef Magnum_Text_DistanceFieldGlyphCache_h |
||||
#define Magnum_Text_DistanceFieldGlyphCache_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class Magnum::Text::DistanceFieldGlyphCache |
||||
*/ |
||||
|
||||
#include "Text/GlyphCache.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
/**
|
||||
@brief Glyph cache with distance field rendering |
||||
|
||||
Unlike original GlyphCache converts each binary image to distance field. It is |
||||
not possible to use non-binary colors with this cache, internal texture format |
||||
is red channel only. |
||||
|
||||
@section GlyphCache-usage Usage |
||||
|
||||
Usage is similar to GlyphCache, additionaly you need to specify size of |
||||
resulting distance field texture. |
||||
@code |
||||
Text::AbstractFont* font; |
||||
Text::GlyphCache* cache = new Text::DistanceFieldGlyphCache(Vector2i(2048), Vector2i(384)); |
||||
font->createGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" |
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
"0123456789 "); |
||||
@endcode |
||||
|
||||
@see TextureTools::distanceField() |
||||
*/ |
||||
class MAGNUM_TEXT_EXPORT DistanceFieldGlyphCache: public GlyphCache { |
||||
public: |
||||
/**
|
||||
* @brief Constructor |
||||
* @param originalSize Original cache texture size |
||||
* @param distanceFieldSize Size of computed distance field texture |
||||
* @param radius Distance field computation radius |
||||
* |
||||
* See TextureTools::distanceField() for more information about the |
||||
* parameters. |
||||
*/ |
||||
explicit DistanceFieldGlyphCache(const Vector2i& originalSize, const Vector2i& distanceFieldSize, UnsignedInt radius); |
||||
|
||||
/**
|
||||
* @brief Set cache image |
||||
* |
||||
* Uploads image for one or more glyphs to given offset in original |
||||
* cache texture. The texture is then converted to distance field. |
||||
*/ |
||||
void setImage(const Vector2i& offset, Image2D* const image) override; |
||||
|
||||
/**
|
||||
* @brief Set distance field cache image |
||||
* |
||||
* Uploads already computed distance field image to given offset in |
||||
* distance field texture. |
||||
*/ |
||||
void setDistanceFieldImage(const Vector2i& offset, Image2D* const image); |
||||
|
||||
private: |
||||
const Vector2 scale; |
||||
const UnsignedInt radius; |
||||
}; |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,91 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "GlyphCache.h" |
||||
|
||||
#include "Extensions.h" |
||||
#include "Image.h" |
||||
#include "TextureTools/Atlas.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
namespace { |
||||
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3) |
||||
const AbstractTexture::InternalFormat internalFormat = AbstractTexture::InternalFormat::R8; |
||||
#else |
||||
const AbstractTexture::InternalFormat internalFormat = AbstractTexture::InternalFormat::Red; |
||||
#endif |
||||
} |
||||
|
||||
GlyphCache::GlyphCache(const Vector2i& size): _size(size) { |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); |
||||
#else |
||||
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); |
||||
#endif |
||||
|
||||
initialize(internalFormat, size); |
||||
} |
||||
|
||||
GlyphCache::GlyphCache(const Vector2i& size, const AbstractTexture::InternalFormat internalFormat): _size(size) { |
||||
initialize(internalFormat, size); |
||||
} |
||||
|
||||
GlyphCache::GlyphCache(const Vector2i& size, const Vector2i& padding): _size(size), _padding(padding) {} |
||||
|
||||
GlyphCache::~GlyphCache() = default; |
||||
|
||||
/** @todo Delegating constructor when support for GCC 4.6 is dropped */ |
||||
void GlyphCache::initialize(const AbstractTexture::InternalFormat internalFormat, const Vector2i& size) { |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_storage); |
||||
#else |
||||
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_storage); |
||||
#endif |
||||
|
||||
_texture.setWrapping(Texture2D::Wrapping::ClampToEdge) |
||||
->setMinificationFilter(Texture2D::Filter::Linear) |
||||
->setMagnificationFilter(Texture2D::Filter::Linear) |
||||
->setStorage(1, internalFormat, size); |
||||
} |
||||
|
||||
std::vector<Rectanglei> GlyphCache::reserve(const std::vector<Vector2i>& sizes) { |
||||
CORRADE_ASSERT(glyphs.empty(), "Text::GlyphCache::reserve(): reserving space in non-empty cache is not yet implemented", {}); |
||||
glyphs.reserve(glyphs.size() + sizes.size()); |
||||
return TextureTools::atlas(_size, sizes, _padding); |
||||
} |
||||
|
||||
void GlyphCache::insert(const UnsignedInt glyph, Vector2i position, Rectanglei rectangle) { |
||||
position -= _padding; |
||||
rectangle.bottomLeft() -= _padding; |
||||
rectangle.topRight() += _padding; |
||||
|
||||
glyphs.insert({glyph, {position, rectangle}}); |
||||
} |
||||
|
||||
void GlyphCache::setImage(const Vector2i& offset, Image2D* const image) { |
||||
_texture.setSubImage(0, offset, image); |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,157 @@
|
||||
#ifndef Magnum_Text_GlyphCache_h |
||||
#define Magnum_Text_GlyphCache_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class Magnum::Text::GlyphCache |
||||
*/ |
||||
|
||||
#include <unordered_map> |
||||
|
||||
#include "Math/Geometry/Rectangle.h" |
||||
#include "Texture.h" |
||||
#include "Text/magnumTextVisibility.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
/**
|
||||
@brief Glyph cache |
||||
|
||||
Contains font glyphs prerendered into texture atlas. |
||||
|
||||
@section GlyphCache-usage Usage |
||||
|
||||
Create %GlyphCache object with sufficient size and then call |
||||
AbstractFont::createGlyphCache() to fill it with glyphs. |
||||
@code |
||||
Text::AbstractFont* font; |
||||
Text::GlyphCache* cache = new GlyphCache(Vector2i(512)); |
||||
font->createGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" |
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
"0123456789 "); |
||||
@endcode |
||||
|
||||
See TextRenderer for information about text rendering. |
||||
*/ |
||||
class MAGNUM_TEXT_EXPORT GlyphCache { |
||||
public: |
||||
/**
|
||||
* @brief Constructor |
||||
* @param size Glyph cache texture size |
||||
* @param internalFormat Internal texture format |
||||
*/ |
||||
explicit GlyphCache(const Vector2i& size, const Texture2D::InternalFormat internalFormat); |
||||
|
||||
/**
|
||||
* @brief Constructor |
||||
* @param size Glyph cache texture size |
||||
* |
||||
* Sets internal texture format to red channel only. Requires |
||||
* @extension{ARB,texture_rg} (also part of OpenGL ES 3.0 or available |
||||
* as @es_extension{EXT,texture_rg} in ES 2.0). |
||||
*/ |
||||
explicit GlyphCache(const Vector2i& size); |
||||
|
||||
virtual ~GlyphCache(); |
||||
|
||||
/**
|
||||
* @brief Cache size |
||||
* |
||||
* Size of unscaled glyph cache texture. |
||||
*/ |
||||
inline Vector2i textureSize() const { return _size; } |
||||
|
||||
/** @brief Count of glyphs in the cache */ |
||||
inline std::size_t glyphCount() const { return glyphs.size(); } |
||||
|
||||
/** @brief Cache texture */ |
||||
inline Texture2D* texture() { return &_texture; } |
||||
|
||||
/**
|
||||
* @brief Parameters of given glyph |
||||
* @param glyph Glyph ID |
||||
* |
||||
* First tuple element is glyph position relative to point on baseline, |
||||
* second element is glyph region in texture atlas. If no glyph is |
||||
* found, glyph on zero index is returned. |
||||
*/ |
||||
inline std::pair<Vector2i, Rectanglei> operator[](const UnsignedInt glyph) const { |
||||
auto it = glyphs.find(glyph); |
||||
return it == glyphs.end() ? glyphs.at(0) : it->second; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Layout glyphs with given sizes to the cache |
||||
* |
||||
* Returns non-overlapping regions in cache texture to store glyphs. |
||||
* The reserved space is reused on next call to reserve() if no glyph |
||||
* was stored there, use insert() to store actual glyph on given |
||||
* position and setImage() to upload glyph image. |
||||
* |
||||
* @attention Cache size must be large enough to contain all rendered |
||||
* glyphs. |
||||
*/ |
||||
std::vector<Rectanglei> reserve(const std::vector<Vector2i>& sizes); |
||||
|
||||
/**
|
||||
* @brief Insert glyph to cache |
||||
* @param glyph Glyph ID |
||||
* @param position Position relative to point on baseline |
||||
* @param rectangle Region in texture atlas |
||||
* |
||||
* You can obtain unused non-overlapping regions with reserve(). See |
||||
* also setImage() to upload glyph image. |
||||
*/ |
||||
void insert(const UnsignedInt glyph, Vector2i position, Rectanglei rectangle); |
||||
|
||||
/**
|
||||
* @brief Set cache image |
||||
* |
||||
* Uploads image for one or more glyphs to given offset in cache |
||||
* texture. |
||||
*/ |
||||
virtual void setImage(const Vector2i& offset, Image2D* const image); |
||||
|
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
private: |
||||
#else |
||||
protected: |
||||
#endif |
||||
/* Used from DistanceFieldGlyphCache */ |
||||
explicit MAGNUM_LOCAL GlyphCache(const Vector2i& size, const Vector2i& padding); |
||||
|
||||
void MAGNUM_LOCAL initialize(const Texture2D::InternalFormat internalFormat, const Vector2i& size); |
||||
|
||||
const Vector2i _size; |
||||
Texture2D _texture; |
||||
|
||||
private: |
||||
const Vector2i _padding; |
||||
std::unordered_map<UnsignedInt, std::pair<Vector2i, Rectanglei>> glyphs; |
||||
}; |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue