You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
18 KiB

/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022, 2023 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 "AbstractFont.h"
#include <string> /** @todo remove once file callbacks are <string>-free */
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/EnumSet.hpp>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringStl.h> /** @todo remove once file callbacks are <string>-free */
#include <Corrade/PluginManager/Manager.hpp>
#include <Corrade/Utility/Path.h>
#include <Corrade/Utility/Unicode.h>
#include "Magnum/FileCallback.h"
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
#include "Magnum/Math/Vector2.h"
#include "Magnum/Text/AbstractGlyphCache.h"
#include "Magnum/Text/AbstractShaper.h"
#ifdef MAGNUM_BUILD_DEPRECATED
#include <Corrade/Containers/Pair.h>
#include <Corrade/Containers/Triple.h>
#include <Corrade/Containers/StridedArrayView.h>
#include "Magnum/Math/Functions.h"
Text: rework AbstractGlyphCache for better flexibility and efficiency. The class now supports incremental filling, multiple fonts, texture arrays, removes all reliance on STL containers and is finally properly documented. To avoid complete breakage of every use, as much as possible was kept as deprecated APIs -- in particular the reserve() with the nasty std::vectors, the insert() that assumes a 2D cache and a single font and textureSize() that returns a 2D vector. Those behave the same as before, but will assert if the cache is an array or contains more than one font. On the other hand, begin() / end() access with std::unordered_map iterators (ew!) was removed as the internals simply aren't a hashmap anymore. The image() that returned an Image2D is now used to fill the glyph cache instead of querying its potentially processed contents, and returns a MutableImageView3D. I considered keeping it and adding sourceImage() instead, but such naming turned out to be too inconsistent. For querying processed image data (such as with the distance field cache) there's a new processedImage() query, guarded by new GlyphCacheFeature bits -- if both ImageProcessing and ProcessedImageDownload is set, it can be used to retrieve the processed image (so, similar as ImageDownload was before), and if neither is set, the cache contents are queryable directly through image(), without needing any special support from the GPU API. Existing code is updated only in the minimal way possible to ensure that no serious breakage was introduced by reimplementing the deprecated APIs on top of the new backend. Porting away from deprecated APIs will be done in next commits. The GlyphCache and DistanceFieldGlyphCache have their public API kept intact for now, as a similar rework will be needed for them as well. Additionally, the MagnumFont and MagnumFontConverter plugins aren't compiling yet as they require substantial changes to deal with the new glyph cache features. That is not the case with other plugins in the magnum-plugins repository tho, for those the backwards compatibility "just works". On the other hand, since layout of the AbstractGlyphChange changed, I'm bumping the AbstractFont plugin interface version to force-trigger a rebuild of dependent projects. Because I ran a stale magnum-player binary, it worked without crashing or GL errors but just didn't show ANY text whatsoever due to ABI differences, and I wasted some precious minutes before realizing that a simple rebuild would fix it.
3 years ago
#include "Magnum/Math/Range.h"
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
#endif
#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
#include "Magnum/Text/configure.h"
#endif
namespace Corrade { namespace PluginManager {
template class MAGNUM_TEXT_EXPORT Manager<Magnum::Text::AbstractFont>;
}}
namespace Magnum { namespace Text {
using namespace Containers::Literals;
Containers::StringView AbstractFont::pluginInterface() {
return MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE ""_s;
}
#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
Containers::Array<Containers::String> AbstractFont::pluginSearchPaths() {
const Containers::Optional<Containers::String> libraryLocation = Utility::Path::libraryLocation(&pluginInterface);
return PluginManager::implicitPluginSearchPaths(
#ifndef MAGNUM_BUILD_STATIC
libraryLocation ? *libraryLocation : Containers::String{},
#else
{},
#endif
#ifdef CORRADE_IS_DEBUG_BUILD
MAGNUM_PLUGINS_FONT_DEBUG_DIR,
#else
MAGNUM_PLUGINS_FONT_DIR,
#endif
#ifdef CORRADE_IS_DEBUG_BUILD
"magnum-d/"
#else
"magnum/"
#endif
"fonts"_s);
}
#endif
AbstractFont::AbstractFont() = default;
AbstractFont::AbstractFont(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): AbstractPlugin{manager, plugin} {}
void AbstractFont::setFileCallback(Containers::Optional<Containers::ArrayView<const char>>(*callback)(const std::string&, InputFileCallbackPolicy, void*), void* const userData) {
CORRADE_ASSERT(!isOpened(), "Text::AbstractFont::setFileCallback(): can't be set while a font is opened", );
CORRADE_ASSERT(features() & (FontFeature::FileCallback|FontFeature::OpenData), "Text::AbstractFont::setFileCallback(): font plugin supports neither loading from data nor via callbacks, callbacks can't be used", );
_fileCallback = callback;
_fileCallbackUserData = userData;
doSetFileCallback(callback, userData);
}
void AbstractFont::doSetFileCallback(Containers::Optional<Containers::ArrayView<const char>>(*)(const std::string&, InputFileCallbackPolicy, void*), void*) {}
bool AbstractFont::openData(Containers::ArrayView<const void> data, const Float size) {
CORRADE_ASSERT(features() & FontFeature::OpenData,
"Text::AbstractFont::openData(): feature not supported", false);
/* We accept empty data here (instead of checking for them and failing so
the check doesn't be done on the plugin side) because for some file
formats it could be valid (MagnumFont in particular). */
close();
const Properties properties = doOpenData(Containers::arrayCast<const char>(data), size);
/* If opening succeeded, save the returned values. If not, the values were
set to their default values by close() already. */
if(isOpened()) {
_size = properties.size;
_ascent = properties.ascent;
_descent = properties.descent;
_lineHeight = properties.lineHeight;
_glyphCount = properties.glyphCount;
return true;
}
return false;
}
auto AbstractFont::doOpenData(Containers::ArrayView<const char>, Float) -> Properties {
CORRADE_ASSERT_UNREACHABLE("Text::AbstractFont::openData(): feature advertised but not implemented", {});
}
bool AbstractFont::openFile(const Containers::StringView filename, const Float size) {
close();
Properties properties;
/* If file loading callbacks are not set or the font implementation
supports handling them directly, call into the implementation */
if(!_fileCallback || (doFeatures() & FontFeature::FileCallback)) {
properties = doOpenFile(filename, size);
/* Otherwise, if loading from data is supported, use the callback and pass
the data through to openData(). Mark the file as ready to be closed once
opening is finished. */
} else if(doFeatures() & FontFeature::OpenData) {
/* This needs to be duplicated here and in the doOpenFile()
implementation in order to support both following cases:
- plugins that don't support FileCallback but have their own
doOpenFile() implementation (callback needs to be used here,
because the base doOpenFile() implementation might never get
called)
- plugins that support FileCallback but want to delegate the actual
file loading to the default implementation (callback used in the
base doOpenFile() implementation, because this branch is never
taken in that case) */
const Containers::Optional<Containers::ArrayView<const char>> data = _fileCallback(filename, InputFileCallbackPolicy::LoadTemporary, _fileCallbackUserData);
if(!data) {
Error() << "Text::AbstractFont::openFile(): cannot open file" << filename;
return isOpened();
}
properties = doOpenData(*data, size);
_fileCallback(filename, InputFileCallbackPolicy::Close, _fileCallbackUserData);
/* Shouldn't get here, the assert is fired already in setFileCallback() */
} else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */
/* If opening succeeded, save the returned values. If not, the values were
set to their default values by close() already. */
if(isOpened()) {
_size = properties.size;
_ascent = properties.ascent;
_descent = properties.descent;
_lineHeight = properties.lineHeight;
_glyphCount = properties.glyphCount;
return true;
}
return false;
}
auto AbstractFont::doOpenFile(const Containers::StringView filename, const Float size) -> Properties {
CORRADE_ASSERT(features() & FontFeature::OpenData, "Text::AbstractFont::openFile(): not implemented", {});
Properties properties;
/* If callbacks are set, use them. This is the same implementation as in
openFile(), see the comment there for details. */
if(_fileCallback) {
const Containers::Optional<Containers::ArrayView<const char>> data = _fileCallback(filename, InputFileCallbackPolicy::LoadTemporary, _fileCallbackUserData);
if(!data) {
Error() << "Text::AbstractFont::openFile(): cannot open file" << filename;
return {};
}
properties = doOpenData(*data, size);
_fileCallback(filename, InputFileCallbackPolicy::Close, _fileCallbackUserData);
/* Otherwise open the file directly */
} else {
const Containers::Optional<Containers::Array<char>> data = Utility::Path::read(filename);
if(!data) {
Error() << "Text::AbstractFont::openFile(): cannot open file" << filename;
return {};
}
properties = doOpenData(*data, size);
}
return properties;
}
void AbstractFont::close() {
if(!isOpened()) return;
doClose();
CORRADE_INTERNAL_ASSERT(!isOpened());
/* Clear the saved values to avoid accidental use of stale (state even
though their public access is guarded with isOpened()) */
_size = {};
_lineHeight = {};
_descent = {};
_lineHeight = {};
}
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::glyphCount() const {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::glyphCount(): no font opened", 0);
return _glyphCount;
}
UnsignedInt AbstractFont::glyphId(const char32_t character) {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::glyphId(): no font opened", 0);
return doGlyphId(character);
}
Vector2 AbstractFont::glyphSize(const UnsignedInt glyph) {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::glyphSize(): no font opened", {});
CORRADE_ASSERT(glyph < _glyphCount, "Text::AbstractFont::glyphSize(): index" << glyph << "out of range for" << _glyphCount << "glyphs", {});
return doGlyphSize(glyph);
}
Vector2 AbstractFont::glyphAdvance(const UnsignedInt glyph) {
CORRADE_ASSERT(isOpened(), "Text::AbstractFont::glyphAdvance(): no font opened", {});
CORRADE_ASSERT(glyph < _glyphCount, "Text::AbstractFont::glyphAdvance(): index" << glyph << "out of range for" << _glyphCount << "glyphs", {});
return doGlyphAdvance(glyph);
}
void AbstractFont::fillGlyphCache(AbstractGlyphCache& cache, const Containers::StringView characters) {
CORRADE_ASSERT(isOpened(),
"Text::AbstractFont::fillGlyphCache(): no font opened", );
CORRADE_ASSERT(!(features() & FontFeature::PreparedGlyphCache),
"Text::AbstractFont::fillGlyphCache(): feature not supported", );
const Containers::Optional<Containers::Array<char32_t>> utf32 = Utility::Unicode::utf32(characters);
CORRADE_ASSERT(utf32,
"Text::AbstractFont::fillGlyphCache(): not a valid UTF-8 string:" << characters, );
doFillGlyphCache(cache, *utf32);
}
void AbstractFont::doFillGlyphCache(AbstractGlyphCache&, Containers::ArrayView<const char32_t>) {
CORRADE_ASSERT_UNREACHABLE("Text::AbstractFont::fillGlyphCache(): feature advertised but not implemented", );
}
Containers::Pointer<AbstractGlyphCache> AbstractFont::createGlyphCache() {
CORRADE_ASSERT(isOpened(),
"Text::AbstractFont::createGlyphCache(): no font opened", nullptr);
CORRADE_ASSERT(features() & FontFeature::PreparedGlyphCache,
"Text::AbstractFont::createGlyphCache(): feature not supported", nullptr);
return doCreateGlyphCache();
}
Containers::Pointer<AbstractGlyphCache> AbstractFont::doCreateGlyphCache() {
CORRADE_ASSERT_UNREACHABLE("Text::AbstractFont::createGlyphCache(): feature advertised but not implemented", nullptr);
}
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
Containers::Pointer<AbstractShaper> AbstractFont::createShaper() {
CORRADE_ASSERT(isOpened(),
"Text::AbstractFont::createShaper(): no font opened", {});
Containers::Pointer<AbstractShaper> out = doCreateShaper();
CORRADE_ASSERT(out,
"Text::AbstractFont::createShaper(): implementation returned nullptr", {});
return out;
}
#ifdef MAGNUM_BUILD_DEPRECATED
CORRADE_IGNORE_DEPRECATED_PUSH
Containers::Pointer<AbstractLayouter> AbstractFont::layout(const AbstractGlyphCache& cache, const Float size, const Containers::StringView text) {
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
CORRADE_ASSERT(isOpened(),
"Text::AbstractFont::layout(): no font opened", {});
/* This was originally added as a runtime error into plugin
implementations during the transition period for the new
AbstractGlyphCache API, now it's an assert. Shouldn't get triggered by
existing code in practice. */
CORRADE_ASSERT(cache.size().z() == 1,
"Text::AbstractFont::layout(): array glyph caches are not supported", {});
/* Find this font in the cache. This is kept as a runtime error however. */
Containers::Optional<UnsignedInt> fontId = cache.findFont(this);
if(!fontId) {
Error{} << "Text::AbstractFont::layout(): font not found among" << cache.fontCount() << "fonts in passed glyph cache";
return {};
}
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
/* Ignoring the failures in this case, as the old API was never failing
also -- it'll simply return an empty AbstractLayouter */
Containers::Pointer<AbstractShaper> shaper = createShaper();
shaper->shape(text);
/* Scaling factor */
const Float scale = size/this->size();
/* Get the glyph data. Yes, this is one extra temporary allocation which
could be aliased with the output array, but for the deprecated API implementation should be as unsurprising and unclever as possible. */
struct Glyph {
UnsignedInt id;
Vector2 offset;
Vector2 advance;
};
Containers::Array<Glyph> glyphs{NoInit, shaper->glyphCount()};
shaper->glyphIdsInto(
stridedArrayView(glyphs).slice(&Glyph::id));
shaper->glyphOffsetsAdvancesInto(
stridedArrayView(glyphs).slice(&Glyph::offset),
stridedArrayView(glyphs).slice(&Glyph::advance));
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
/* Create the data to return from AbstractLayouter::renderGlyph(). Most of
this used to be copypasted in various *Layouter::doRenderGlyph()
implementations, ugh. */
Containers::Array<Containers::Triple<Range2D, Range2D, Vector2>> out{NoInit, glyphs.size()};
for(std::size_t i = 0; i != glyphs.size(); ++i) {
/* Offset of the glyph rectangle relative to the cursor, layer, texture
coordinates. We checked that the glyph cache is 2D above so the
layer can be ignored. */
const Containers::Triple<Vector2i, Int, Range2Di> cacheGlyph = cache.glyph(*fontId, glyphs[i].id);
CORRADE_INTERNAL_ASSERT(cacheGlyph.second() == 0);
out[i] = {
/* Quad rectangle, created from cache and shaper offset and the
texture rectangle, scaled to requested text size */
Range2D::fromSize(Vector2{cacheGlyph.first()} + glyphs[i].offset,
Vector2{cacheGlyph.third().size()})
.scaled(Vector2{scale}),
/* Normalized texture coordinates */
Range2D{cacheGlyph.third()}
.scaled(1.0f/Vector2{cache.size().xy()}),
/* Advance from the font, again scaled */
glyphs[i].advance*scale
};
}
return Containers::pointer<AbstractLayouter>(Utility::move(out));
}
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
CORRADE_IGNORE_DEPRECATED_POP
#endif
Debug& operator<<(Debug& debug, const FontFeature value) {
const bool packed = debug.immediateFlags() >= Debug::Flag::Packed;
if(!packed)
debug << "Text::FontFeature" << Debug::nospace;
switch(value) {
/* LCOV_EXCL_START */
#define _c(v) case FontFeature::v: return debug << (packed ? "" : "::") << Debug::nospace << #v;
_c(OpenData)
_c(FileCallback)
_c(PreparedGlyphCache)
#undef _c
/* LCOV_EXCL_STOP */
}
return debug << (packed ? "" : "(") << Debug::nospace << reinterpret_cast<void*>(UnsignedByte(value)) << Debug::nospace << (packed ? "" : ")");
}
Debug& operator<<(Debug& debug, const FontFeatures value) {
return Containers::enumSetDebugOutput(debug, value, debug.immediateFlags() >= Debug::Flag::Packed ? "{}" : "Text::FontFeatures{}", {
FontFeature::OpenData,
FontFeature::FileCallback,
FontFeature::PreparedGlyphCache});
}
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
#ifdef MAGNUM_BUILD_DEPRECATED
AbstractLayouter::AbstractLayouter(Containers::Array<Containers::Triple<Range2D, Range2D, Vector2>>&& glyphs): _glyphs{Utility::move(glyphs)} {}
9 years ago
AbstractLayouter::~AbstractLayouter() = default;
Containers::Pair<Range2D, Range2D> AbstractLayouter::renderGlyph(const UnsignedInt i, Vector2& cursorPosition, Range2D& rectangle) {
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
CORRADE_ASSERT(i < _glyphs.size(),
"Text::AbstractLayouter::renderGlyph(): index" << i << "out of range for" << _glyphs.size() << "glyphs", {});
/* Move the quad to cursor */
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
const Range2D quadPosition = _glyphs[i].first().translated(cursorPosition);
/* Extend the rectangle with current quad bounds. If the original is zero
size, it gets replaced. */
rectangle = Math::join(rectangle, quadPosition);
/* Advance cursor position to next character */
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
cursorPosition += _glyphs[i].third();
/* Return moved quad and unchanged texture coordinates */
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
return {quadPosition, _glyphs[i].second()};
}
Text: new AbstractShaper interface for shaping. Replaces the previous, grossly inefficient AbstractLayouter which was performing one virtual call per glyph (!). It's now also reusable, meaning it doesn't need to be allocated anew for every new shaped text, and it no longer requires each and every font plugin to implement the same redundant glyph data fetching from the glyph cache, scaling etc. -- all that is meant to be done by the users of AbstractShaper, i.e. Renderer. The independency on a glyph cache theorerically also means it can be used for a completely different, non-texture-based way to render text (such as direct path drawing directly on the GPU), although I won't be exploring that path now. It also exposes an interface for specifying script, language, direction and typographic features. Such interface will be currently only implemented in HarfBuzz, but that's the intent -- to provide a flexible enough interface to support all possible use cases that a font or a font plugin may support, instead of exposing a least common denominator and then having no easy way to shape a text in a non-Latin script or use a fancy OpenType feature the chosen font has. The old public interface is preserved for backwards compatibility, marked as deprecated, however the virtual APIs are not, as supporting that would be too nasty. I don't think any user code ever implemented a font plugin so this should be okay. To ensure smooth transition with no regressions, the Renderer class and MagnumFont tests still use the old API in this commit, and their test pass the same way as they did before (except for two removed MagnumFont test cases which tested errors that are now an assertion in the deprecated layout() API and thus cannot be tested from the plugin anymore). Porting them away from the deprecated API will be done in separate commits.
3 years ago
#endif
}}