mirror of https://github.com/mosra/magnum.git
Browse Source
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.pull/168/head
20 changed files with 2115 additions and 459 deletions
@ -0,0 +1,145 @@
|
||||
/*
|
||||
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 "AbstractShaper.h" |
||||
|
||||
#include <Corrade/Containers/StridedArrayView.h> |
||||
#include <Corrade/Containers/StringView.h> |
||||
|
||||
#include "Magnum/Text/Script.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
Debug& operator<<(Debug& debug, const Direction value) { |
||||
debug << "Text::Direction" << Debug::nospace; |
||||
|
||||
switch(value) { |
||||
/* LCOV_EXCL_START */ |
||||
#define _c(v) case Direction::v: return debug << "::" #v; |
||||
_c(Unspecified) |
||||
_c(LeftToRight) |
||||
_c(RightToLeft) |
||||
_c(TopToBottom) |
||||
_c(BottomToTop) |
||||
#undef _c |
||||
/* LCOV_EXCL_STOP */ |
||||
} |
||||
|
||||
return debug << "(" << Debug::nospace << reinterpret_cast<void*>(UnsignedByte(value)) << Debug::nospace << ")"; |
||||
} |
||||
|
||||
AbstractShaper::AbstractShaper(AbstractFont& font): _font(font), _glyphCount{0} {} |
||||
|
||||
AbstractShaper::AbstractShaper(AbstractShaper&&) noexcept = default; |
||||
|
||||
AbstractShaper::~AbstractShaper() = default; |
||||
|
||||
AbstractShaper& AbstractShaper::operator=(AbstractShaper&&) noexcept = default; |
||||
|
||||
bool AbstractShaper::setScript(const Script script) { |
||||
return doSetScript(script); |
||||
} |
||||
|
||||
bool AbstractShaper::doSetScript(Script) { return false; } |
||||
|
||||
bool AbstractShaper::setLanguage(const Containers::StringView language) { |
||||
return doSetLanguage(language); |
||||
} |
||||
|
||||
bool AbstractShaper::doSetLanguage(Containers::StringView) { return false; } |
||||
|
||||
bool AbstractShaper::setDirection(const Direction direction) { |
||||
return doSetDirection(direction); |
||||
} |
||||
|
||||
bool AbstractShaper::doSetDirection(Direction) { return false; } |
||||
|
||||
UnsignedInt AbstractShaper::shape(const Containers::StringView text, const UnsignedInt begin, const UnsignedInt end, const Containers::ArrayView<const FeatureRange> features) { |
||||
CORRADE_ASSERT((end == ~UnsignedInt{} && begin <= text.size()) || |
||||
(begin <= end && end <= text.size()), |
||||
"Text::AbstractShaper::shape(): begin" << begin << "and end" << end << "out of range for a text of" << text.size() << "bytes", {}); |
||||
CORRADE_ASSERT(begin < text.size() && begin != end, |
||||
"Text::AbstractShaper::shape(): shaped text at begin" << begin << "is empty", {}); |
||||
#ifndef CORRADE_NO_ASSERT |
||||
for(std::size_t i = 0; i != features.size(); ++i) { |
||||
const FeatureRange& feature = features[i]; |
||||
CORRADE_ASSERT( |
||||
(feature._end == ~UnsignedInt{} && feature._begin <= text.size()) || |
||||
(feature._begin <= feature._end && feature._end <= text.size()), |
||||
"Text::AbstractShaper::shape(): feature" << i << "begin" << feature._begin << "and end" << feature._end << "out of range for a text of" << text.size() << "bytes", {}); |
||||
/** @todo catch empty feature ranges too? or not important */ |
||||
} |
||||
#endif |
||||
return _glyphCount = doShape(text, begin, end, features); |
||||
} |
||||
|
||||
UnsignedInt AbstractShaper::shape(const Containers::StringView text, const UnsignedInt begin, const UnsignedInt end, const std::initializer_list<FeatureRange> features) { |
||||
return shape(text, begin, end, Containers::arrayView(features)); |
||||
} |
||||
|
||||
UnsignedInt AbstractShaper::shape(const Containers::StringView text, const UnsignedInt begin, const UnsignedInt end) { |
||||
return shape(text, begin, end, nullptr); |
||||
} |
||||
|
||||
UnsignedInt AbstractShaper::shape(const Containers::StringView text, const Containers::ArrayView<const FeatureRange> features) { |
||||
return shape(text, 0, ~UnsignedInt{}, features); |
||||
} |
||||
|
||||
UnsignedInt AbstractShaper::shape(const Containers::StringView text, const std::initializer_list<FeatureRange> features) { |
||||
return shape(text, Containers::arrayView(features)); |
||||
} |
||||
|
||||
UnsignedInt AbstractShaper::shape(const Containers::StringView text) { |
||||
return shape(text, nullptr); |
||||
} |
||||
|
||||
Script AbstractShaper::script() const { |
||||
return _glyphCount ? doScript() : Script::Unspecified; |
||||
} |
||||
|
||||
Script AbstractShaper::doScript() const { return Script::Unspecified; } |
||||
|
||||
Containers::StringView AbstractShaper::language() const { |
||||
return _glyphCount ? doLanguage() : Containers::StringView{}; |
||||
} |
||||
|
||||
Containers::StringView AbstractShaper::doLanguage() const { return {}; } |
||||
|
||||
Direction AbstractShaper::direction() const { |
||||
return _glyphCount ? doDirection() : Direction::Unspecified; |
||||
} |
||||
|
||||
Direction AbstractShaper::doDirection() const { return Direction::Unspecified; } |
||||
|
||||
void AbstractShaper::glyphsInto(const Containers::StridedArrayView1D<UnsignedInt>& ids, const Containers::StridedArrayView1D<Vector2>& offsets, const Containers::StridedArrayView1D<Vector2>& advances) const { |
||||
CORRADE_ASSERT(ids.size() == _glyphCount && offsets.size() == _glyphCount && advances.size() == _glyphCount, |
||||
"Text::AbstractShaper::glyphsInto(): expected the ids, offsets and advanced views to have a size of" << _glyphCount << "but got" << ids.size() << Debug::nospace << "," << offsets.size() << "and" << advances.size(), ); |
||||
/* Call into the implementation only if there's actually anything shaped,
|
||||
otherwise it might not yet have everything properly set up */ |
||||
if(_glyphCount) |
||||
doGlyphsInto(ids, offsets, advances); |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,543 @@
|
||||
#ifndef Magnum_Text_AbstractShaper_h |
||||
#define Magnum_Text_AbstractShaper_h |
||||
/*
|
||||
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. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class @ref Magnum::Text::AbstractShaper, @ref Magnum::Text::FeatureRange, enum @ref Magnum::Text::Direction |
||||
* @m_since_latest |
||||
*/ |
||||
|
||||
#include <initializer_list> |
||||
#include <Corrade/Containers/Reference.h> |
||||
|
||||
#include "Magnum/Magnum.h" |
||||
#include "Magnum/Text/Text.h" |
||||
#include "Magnum/Text/visibility.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
/**
|
||||
@brief Direction a text is shaped in |
||||
@m_since_latest |
||||
|
||||
@see @ref AbstractShaper::setDirection(), @ref AbstractShaper::direction() |
||||
*/ |
||||
enum class Direction: UnsignedByte { |
||||
/**
|
||||
* Unspecified. When set in @ref AbstractShaper::setDirection(), makes the |
||||
* shaping rely on direction autodetection implemented in a particular |
||||
* @ref AbstractFont plugin (if any). When returned from |
||||
* @ref AbstractShaper::direction() after a successful |
||||
* @ref AbstractShaper::shape() call, it means a particular |
||||
* @ref AbstractFont plugin doesn't implement any script-specific behavior. |
||||
*/ |
||||
Unspecified = 0, |
||||
|
||||
/**
|
||||
* Left to right. When returned from @ref AbstractShaper::direction(), |
||||
* the @p advances filled by @ref AbstractShaper::glyphsInto() are |
||||
* guaranteed to have their Y components @cpp 0.0f @ce. |
||||
*/ |
||||
LeftToRight = 1, |
||||
|
||||
/**
|
||||
* Right to left. When returned from @ref AbstractShaper::direction(), |
||||
* the @p advances filled by @ref AbstractShaper::glyphsInto() are |
||||
* guaranteed to have their Y components @cpp 0.0f @ce. |
||||
*/ |
||||
RightToLeft, |
||||
|
||||
/**
|
||||
* Top to bottom. When returned from @ref AbstractShaper::direction(), |
||||
* the @p advances filled by @ref AbstractShaper::glyphsInto() are |
||||
* guaranteed to have their X components @cpp 0.0f @ce. |
||||
*/ |
||||
TopToBottom, |
||||
|
||||
/**
|
||||
* Bottom to top. When returned from @ref AbstractShaper::direction(), |
||||
* the @p advances filled by @ref AbstractShaper::glyphsInto() are |
||||
* guaranteed to have their X components @cpp 0.0f @ce. |
||||
*/ |
||||
BottomToTop |
||||
}; |
||||
|
||||
/** @debugoperatorenum{Direction} */ |
||||
MAGNUM_TEXT_EXPORT Debug& operator<<(Debug& debug, Direction value); |
||||
|
||||
/**
|
||||
@brief OpenType feature for a text range |
||||
@m_since_latest |
||||
|
||||
@see @ref AbstractShaper::shape() |
||||
*/ |
||||
class FeatureRange { |
||||
public: |
||||
/**
|
||||
* @brief Constructor |
||||
* @param feature Feature to control |
||||
* @param begin Beginning byte in the input text |
||||
* @param end (One byte after) the end byte in the input text |
||||
* @param value Feature value to set |
||||
*/ |
||||
constexpr /*implicit*/ FeatureRange(Feature feature, UnsignedInt begin, UnsignedInt end, UnsignedInt value = true): _feature{feature}, _value{value}, _begin{begin}, _end{end} {} |
||||
|
||||
/**
|
||||
* @brief Construct for the whole text |
||||
* |
||||
* Equivalent to calling @ref FeatureRange(Feature, UnsignedInt, UnsignedInt, UnsignedInt) |
||||
* with @p begin set to @cpp 0 @ce and @p end to @cpp 0xffffffffu @ce. |
||||
*/ |
||||
constexpr /*implicit*/ FeatureRange(Feature feature, UnsignedInt value = true): _feature{feature}, _value{value}, _begin{0}, _end{~UnsignedInt{}} {} |
||||
|
||||
/** @brief Feature to control */ |
||||
constexpr Feature feature() const { return _feature; } |
||||
|
||||
/**
|
||||
* @brief Whether to enable the feature |
||||
* |
||||
* Returns @cpp false @ce if @ref value() is @cpp 0 @ce, @cpp true @ce |
||||
* otherwise. |
||||
*/ |
||||
constexpr bool isEnabled() const { return _value; } |
||||
|
||||
/** @brief Feature value to set */ |
||||
constexpr UnsignedInt value() const { return _value; } |
||||
|
||||
/**
|
||||
* @brief Beginning byte in the input text |
||||
* |
||||
* If the feature is set for the whole text, this is @cpp 0 @ce. |
||||
*/ |
||||
constexpr UnsignedInt begin() const { return _begin; } |
||||
|
||||
/**
|
||||
* @brief (One byte after) the end byte in the input text |
||||
* |
||||
* If the feature is set for the whole text, this is |
||||
* @cpp 0xffffffffu @ce. |
||||
*/ |
||||
constexpr UnsignedInt end() const { return _end; } |
||||
|
||||
private: |
||||
friend AbstractShaper; |
||||
|
||||
Feature _feature; |
||||
UnsignedInt _value; |
||||
UnsignedInt _begin; |
||||
UnsignedInt _end; |
||||
}; |
||||
|
||||
/**
|
||||
@brief Base for text shapers |
||||
@m_since_latest |
||||
|
||||
Returned from @ref AbstractFont::createShaper(), provides an interface for |
||||
* *shaping* text with the @ref AbstractFont it originated from. Meant to be |
||||
(privately) subclassed by @ref AbstractFont plugin implementations. |
||||
|
||||
* *Shaping* is a process of converting a sequence of Unicode codepoints to a |
||||
visual form, i.e. a list of glyphs of a particular font, their offsets and horizontal or vertical advances. Shaping is often not a 1:1 mapping from |
||||
codepoints to glyphs, but involves merging, subdividing and reordering as well. |
||||
|
||||
@section Text-AbstractShaper-usage Usage |
||||
|
||||
Call @ref AbstractFont::createShaper() to get a shaper instance. The plugin |
||||
will always return a valid instance so it's not needed to check for the pointer |
||||
beging @cpp nullptr @ce, however note that the originating @ref AbstractFont |
||||
instance has to stay in scope for at least as long as the @ref AbstractShaper |
||||
is alive. |
||||
|
||||
A text is shaped by calling @ref shape(), retrieving the shaped glyph count |
||||
with @ref glyphCount() and then getting the glyph data with @ref glyphsInto(). |
||||
Glyph IDs can be then queried in (or inserted into) an @ref AbstractGlyphCache, |
||||
and the rendered glyphs positioned at @p offsets with the cursor moving by |
||||
@p advances is what makes up the final shaped text. |
||||
|
||||
@snippet MagnumText.cpp AbstractShaper-shape |
||||
|
||||
For best results, it's recommended to call (a subset of) @ref setScript(), |
||||
@ref setLanguage() and @ref setDirection() if at least some properties of the |
||||
input text are known, as shown above. Without these, the font plugin may |
||||
attempt to autodetect the properties, which might not always give a correct |
||||
result. If a particular font plugin doesn't implement given script, language or |
||||
direction or if it doesn't have any special handling for it, given function |
||||
will return @cpp false @ce. The @ref script() const, @ref language() const and |
||||
@ref direction() const can be used to inspect results of autodetection after |
||||
@ref shape() has been called. The set of supported scripts, languages and |
||||
directions and exact behavior for unsupported values is plugin-specific --- it |
||||
may for example choose a fallback instead, or it may ignore the setting |
||||
altogeter. See documentation of particular @ref AbstractFont subclasses for |
||||
more information. |
||||
|
||||
@subsection Text-AbstractShaper-usage-features Enabling and disabling typographic features |
||||
|
||||
In the above snippet, the whole text is shaped using typographic features that |
||||
are default in the font. For example, assuming the font would support small |
||||
capitals (and the particular @ref AbstractFont plugin would recognize and use |
||||
the feature), we could render the "world" part with small caps, resulting in |
||||
"Hello, ᴡᴏʀʟᴅ!". |
||||
|
||||
@snippet MagnumText.cpp AbstractShaper-shape-features |
||||
|
||||
Similarly, features can be enabled for the whole text by omitting the begin and |
||||
end parameters, or for example a feature that a particular @ref AbstractFont |
||||
plugin uses by default can be disabled by passing an explicit @cpp false @ce |
||||
argument. The range, if present, is always given in *bytes* of the UTF-8 input. |
||||
Capabilities of typographic features are rather broad, see the @ref Feature |
||||
enum and documentation linked from it for exhaustive information. |
||||
|
||||
@subsection Text-AbstractShaper-usage-multiple Combining different shapers |
||||
|
||||
Sometimes it's desirable to render different parts of the text with different |
||||
fonts, not just different features of the same font. A variation of the above |
||||
example could be rendering the "world" part with a bold font: |
||||
|
||||
@snippet MagnumText.cpp AbstractShaper-shape-multiple |
||||
|
||||
The resulting `glyphs` array is usable the same way as in the above case, with |
||||
a difference that the glyph IDs have to be looked up in an |
||||
@ref AbstractGlyphCache with a font ID corresponding to the range they're in. |
||||
Also note that the whole text is passed every time and a begin & end is |
||||
specified for it instead of passing just the slice alone. While possibly not |
||||
having any visible effect in this particular case, in general it allows the |
||||
shaper to make additional decisions based on surrounding context, for example |
||||
picking glyphs that are better connected to their neighbors in handwriting |
||||
fonts. |
||||
|
||||
@subsection Text-AbstractShaper-usage-instances Managing multiple instances |
||||
|
||||
As shown above, a particular @ref AbstractShaper instance is reusable, i.e. |
||||
it's possible to call @ref shape() (and potentially also @ref setScript(), |
||||
@ref setLanguage() and @ref setDirection()) several times to shape multiple |
||||
pieces of text with it. Doing so allows the @ref AbstractFont plugin |
||||
implementation to reuse allocated buffers and other state compared to a fresh |
||||
instance from @ref AbstractFont::createShaper() having to be initialized every |
||||
time. |
||||
|
||||
The application may choose several strategies, for example have a single |
||||
@ref AbstractShaper instance and shape all texts with it, resetting its state |
||||
every time. Or for example have a few persistent @ref AbstractShaper instances |
||||
for dynamic text that changes every frame, or have dedicated preconfigured |
||||
per-font, per-script or per-language instances. |
||||
|
||||
@section Text-AbstractShaper-subclassing Subclassing |
||||
|
||||
The @ref AbstractFont plugin is meant to create a local @ref AbstractShaper |
||||
subclass. It implements at least @ref doShape() and @ref doGlyphsInto(), and |
||||
potentially also (a subset of) @ref doSetScript(), @ref doScript(), |
||||
@ref doSetLanguage(),@ref doLanguage(), @ref doSetDirection() and |
||||
@ref doDirection(). The public API does most sanity checks on its own, see |
||||
documentation of particular `do*()` functions for more information about the |
||||
guarantees. |
||||
*/ |
||||
class MAGNUM_TEXT_EXPORT AbstractShaper { |
||||
public: |
||||
/**
|
||||
* @brief Constructor |
||||
* @param font Font the shaper is originating from |
||||
*/ |
||||
explicit AbstractShaper(AbstractFont& font); |
||||
|
||||
/** @brief Copying is not allowed */ |
||||
AbstractShaper(AbstractShaper&) = delete; |
||||
|
||||
/** @brief Move constructor */ |
||||
AbstractShaper(AbstractShaper&&) noexcept; |
||||
|
||||
/** @brief Copying is not allowed */ |
||||
AbstractShaper& operator=(AbstractShaper&) = delete; |
||||
|
||||
/** @brief Move assignment */ |
||||
AbstractShaper& operator=(AbstractShaper&&) noexcept; |
||||
|
||||
virtual ~AbstractShaper(); |
||||
|
||||
/** @brief Font the shaper is originating from */ |
||||
AbstractFont& font() { return _font; } |
||||
const AbstractFont& font() const { return _font; } /**< @overload */ |
||||
|
||||
/**
|
||||
* @brief Set text script |
||||
* |
||||
* The script is used for all following @ref shape() calls. If not |
||||
* called at all or if explicitly set to @ref Script::Unspecified, the |
||||
* @ref AbstractFont plugin may attempt to guess the script from the |
||||
* input text. The actual script used for shaping (if any) is queryable |
||||
* with @ref script() const after @ref shape() has been called. |
||||
* |
||||
* Returns @cpp true @ce if the plugin supports setting a |
||||
* script and the script is supported, @cpp false @ce otherwise, in |
||||
* which case the shaping falls back to a generic behavior. See |
||||
* documentation of a particular plugin for more information. |
||||
* @see @ref setLanguage(), @ref setDirection() |
||||
*/ |
||||
bool setScript(Script script); |
||||
|
||||
/**
|
||||
* @brief Set text language |
||||
* |
||||
* The language is expected to be a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag),
|
||||
* either just the base tag such as @cpp "en" @ce or @cpp "cs" @ce |
||||
* alone, or further differentiating with a region subtag like for |
||||
* example @cpp "en-US" @ce vs @cpp "en-GB" @ce. |
||||
* |
||||
* The language is used for all following @ref shape() calls. If not |
||||
* called at all or if explicitly set to an empty string, the |
||||
* @ref AbstractFont plugin may attempt to guess the language from the |
||||
* input text or the execution environment, such as current locale. The |
||||
* actual language used for shaping (if any) is queryable with |
||||
* @ref language() const after @ref shape() has been called. |
||||
* |
||||
* Returns @cpp true @ce if the plugin supports setting a language and |
||||
* the language is supported, @cpp false @ce otherwise, in which case |
||||
* the shaping falls back to a generic behavior. See documentation of a |
||||
* particular plugin for more information. |
||||
* @see @ref setScript(), @ref setDirection() |
||||
*/ |
||||
bool setLanguage(Containers::StringView language); |
||||
|
||||
/**
|
||||
* @brief Set text direction |
||||
* |
||||
* The direction is used for all following @ref shape() calls. If not |
||||
* called at all or if explicitly set to @ref Direction::Unspecified, |
||||
* the @ref AbstractFont plugin may attempt to guess the direction from |
||||
* the input text. The actual direction used for shaping (if any) is |
||||
* queryable with @ref direction() const after @ref shape() has been |
||||
* called. |
||||
* |
||||
* Returns @cpp true @ce if the plugin supports setting a language and |
||||
* the language is supported, @cpp false @ce otherwise, in which case |
||||
* the shaping falls back to a generic behavior. See documentation of a |
||||
* particular font plugin for more information. |
||||
* @see @ref setScript(), @ref setLanguage() |
||||
*/ |
||||
bool setDirection(Direction direction); |
||||
|
||||
/**
|
||||
* @brief Shape a text |
||||
* @param text Text in UTF-8 |
||||
* @param features OpenType features to apply for the whole text or |
||||
* its subranges |
||||
* |
||||
* Expects that @p text is non-empty, that both @p begin and all |
||||
* @ref FeatureRange::begin() are contained within @p text, and that |
||||
* @p end and all @ref FeatureRange::end() are either contained within |
||||
* @p text or have a value of @cpp 0xffffffffu @ce. On success returns |
||||
* the number of shaped glyphs (which is also subsequently available |
||||
* through @ref glyphCount() const) and updates the @ref script() const, |
||||
* @ref language() const and @ref direction() const values. |
||||
* |
||||
* On failure, such as when the input is not valid UTF-8 or when |
||||
* specified combination or script, language and direction is |
||||
* unsupported, prints a message to @relativeref{Magnum,Error} and |
||||
* returns @cpp 0 @ce. |
||||
* |
||||
* Whether @p features are used depends on a particular |
||||
* @ref AbstractFont plugin implementation and the font file itself as |
||||
* well --- for example, a plugin may enable @ref Feature::Kerning |
||||
* by default but the font may not even have appropriate tables for it |
||||
* included, in which case no kerning is performed. See documentation |
||||
* of a particular font plugin for more information. |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
UnsignedInt shape(Containers::StringView text, Containers::ArrayView<const FeatureRange> features = {}); |
||||
#else |
||||
/* To not have to include ArrayView */ |
||||
UnsignedInt shape(Containers::StringView text, Containers::ArrayView<const FeatureRange> features); |
||||
UnsignedInt shape(Containers::StringView text); |
||||
#endif |
||||
|
||||
/** @overload */ |
||||
UnsignedInt shape(Containers::StringView text, std::initializer_list<FeatureRange> features); |
||||
|
||||
/**
|
||||
* @brief Shape a slice of text |
||||
* @param text Text in UTF-8 |
||||
* @param begin Beginning byte in the input text |
||||
* @param end (One byte after) the end byte in the input text |
||||
* @param features OpenType features to apply for the whole text or |
||||
* its subranges |
||||
* |
||||
* A variant of @ref shape(Containers::StringView, Containers::ArrayView<const FeatureRange>) |
||||
* to be used when passing pieces of larger text with different |
||||
* shapers, for example when the script, language or direction changes |
||||
* in each piece or when the pieces are using a different font |
||||
* entirely. Compared to passing just the actually shaped slice of |
||||
* @p text this allows the implementation to perform shaping aware of |
||||
* surrounding context, such as picking correct glyphs for beginning, |
||||
* middle or end of a word or a paragraph. |
||||
* @see @ref Text-AbstractShaper-usage-multiple |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
UnsignedInt shape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features = {}); |
||||
#else |
||||
/* To not have to include ArrayView */ |
||||
UnsignedInt shape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features); |
||||
UnsignedInt shape(Containers::StringView text, UnsignedInt begin, UnsignedInt end); |
||||
#endif |
||||
|
||||
/** @overload */ |
||||
UnsignedInt shape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, std::initializer_list<FeatureRange> features); |
||||
|
||||
/**
|
||||
* @brief Count of glyphs produced by the last @ref shape() call |
||||
* |
||||
* If the last @ref shape() call failed or it hasn't been called yet, |
||||
* returns @cpp 0 @ce. |
||||
*/ |
||||
UnsignedInt glyphCount() const { return _glyphCount; } |
||||
|
||||
/**
|
||||
* @brief Script used for the last @ref shape() call |
||||
* |
||||
* If the last @ref shape() call failed, it hasn't been called yet or |
||||
* if the @ref AbstractFont doesn't implement any script-specific |
||||
* behavior, returns @ref Script::Unspecified. |
||||
* @see @ref setScript(), @ref language(), @ref direction() |
||||
*/ |
||||
Script script() const; |
||||
|
||||
/**
|
||||
* @brief Language used for the last @ref shape() call |
||||
* |
||||
* If the last @ref shape() call failed, it hasn't been called yet or |
||||
* if the @ref AbstractFont doesn't implement any language-specific |
||||
* behavior, returns an empty string. |
||||
* |
||||
* The returned view is generally neither |
||||
* @relativeref{Corrade,Containers::StringViewFlag::Global} nor |
||||
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated} and |
||||
* is only guaranteed to stay valid until the next @ref setLanguage() |
||||
* or @ref shape() call. Particular @ref AbstractFont implementations |
||||
* may give better guarantees, see their documentation for more |
||||
* information. |
||||
* @see @ref setLanguage(), @ref script(), @ref direction() |
||||
*/ |
||||
Containers::StringView language() const; |
||||
|
||||
/**
|
||||
* @brief Language used for the last @ref shape() call |
||||
* |
||||
* If the last @ref shape() call failed, it hasn't been called yet or |
||||
* if the @ref AbstractFont doesn't implement any direction-specific |
||||
* behavior, returns @ref Direction::Unspecified. |
||||
* @see @ref setDirection(), @ref script(), @ref language() |
||||
*/ |
||||
Direction direction() const; |
||||
|
||||
/**
|
||||
* @brief Retrieve glyph information |
||||
* @param[out] ids Where to put glyph IDs |
||||
* @param[out] offsets Where to put glyph offsets |
||||
* @param[out] advances Where to put glyph advances |
||||
* |
||||
* The @p ids, @p offsets and @p advances views are all expected to |
||||
* have a size of @ref glyphCount(). After calling this function, the |
||||
* @p ids are commonly looked up in or inserted into an |
||||
* @ref AbstractGlyphCache, @p offsets specify where to put the glyph |
||||
* relative to current cursor (which is then further offset for the |
||||
* particular glyph rectangle returned from the glyph cache) and |
||||
* @p advances specify in which direction to move the cursor for the |
||||
* next glyph. For @ref direction() being @ref Direction::LeftToRight |
||||
* or @relativeref{Direction,RightToLeft} Y components of @p advances |
||||
* are @cpp 0.0f @ce, for @relativeref{Direction,TopToBottom} or |
||||
* @relativeref{Direction,BottomToTop} X components of @p advances are |
||||
* @cpp 0.0f @ce. |
||||
* @see @ref direction() |
||||
*/ |
||||
void glyphsInto(const Containers::StridedArrayView1D<UnsignedInt>& ids, const Containers::StridedArrayView1D<Vector2>& offsets, const Containers::StridedArrayView1D<Vector2>& advances) const; |
||||
|
||||
private: |
||||
/**
|
||||
* @brief Implemenation for @ref setScript() |
||||
* |
||||
* Default implementation does nothing and returns @cpp false @ce. |
||||
*/ |
||||
virtual bool doSetScript(Script script); |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref setLanguage() |
||||
* |
||||
* Default implementation does nothing and returns @cpp false @ce. |
||||
*/ |
||||
virtual bool doSetLanguage(Containers::StringView language); |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref setDirection() |
||||
* |
||||
* Default implementation does nothing and returns @cpp false @ce. |
||||
*/ |
||||
virtual bool doSetDirection(Direction direction); |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref shape() |
||||
* |
||||
* The @p begin as well as all @ref FeatureRange::begin() values |
||||
* are guaranteed to be within @p text, @p end as well as all |
||||
* @ref FeatureRange::end() values are guaranteed to be either within |
||||
* @p text or have a value of @cpp 0xffffffffu @ce. |
||||
*/ |
||||
virtual UnsignedInt doShape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features) = 0; |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref script() |
||||
* |
||||
* Default implementation returns @ref Script::Unspecified. |
||||
*/ |
||||
virtual Script doScript() const; |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref language() |
||||
* |
||||
* Default implementation returns an empty string. |
||||
*/ |
||||
virtual Containers::StringView doLanguage() const; |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref direction() |
||||
* |
||||
* Default implementation returns @ref Direction::Unspecified. |
||||
*/ |
||||
virtual Direction doDirection() const; |
||||
|
||||
/**
|
||||
* @brief Implemenation for @ref glyphsInto() |
||||
* |
||||
* The @p ids, @p offsets and @p advances are guaranteed to have a size |
||||
* of @ref glyphCount(). Called only if @ref glyphCount() is not |
||||
* @cpp 0 @ce. |
||||
*/ |
||||
virtual void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>& ids, const Containers::StridedArrayView1D<Vector2>& offsets, const Containers::StridedArrayView1D<Vector2>& advances) const = 0; |
||||
|
||||
Containers::Reference<AbstractFont> _font; |
||||
UnsignedInt _glyphCount; |
||||
}; |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -1,103 +0,0 @@
|
||||
/*
|
||||
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 <sstream> |
||||
#include <Corrade/Containers/Triple.h> |
||||
#include <Corrade/TestSuite/Tester.h> |
||||
#include <Corrade/Utility/DebugStl.h> /** @todo remove once Debug is stream-free */ |
||||
|
||||
#include "Magnum/Math/Range.h" |
||||
#include "Magnum/Text/AbstractFont.h" |
||||
|
||||
namespace Magnum { namespace Text { namespace Test { namespace { |
||||
|
||||
struct AbstractLayouterTest: TestSuite::Tester { |
||||
explicit AbstractLayouterTest(); |
||||
|
||||
void renderGlyph(); |
||||
void renderGlyphOutOfRange(); |
||||
}; |
||||
|
||||
AbstractLayouterTest::AbstractLayouterTest() { |
||||
addTests({&AbstractLayouterTest::renderGlyph, |
||||
&AbstractLayouterTest::renderGlyphOutOfRange}); |
||||
} |
||||
|
||||
void AbstractLayouterTest::renderGlyph() { |
||||
struct Layouter: AbstractLayouter { |
||||
explicit Layouter(): AbstractLayouter{3} {} |
||||
|
||||
Containers::Triple<Range2D, Range2D, Vector2> doRenderGlyph(UnsignedInt) override { |
||||
return {{{1.0f, 0.5f}, {1.1f, 1.0f}}, |
||||
{{0.3f, 1.1f}, {-0.5f, 0.7f}}, |
||||
{2.0f, -1.0f}}; |
||||
} |
||||
}; |
||||
|
||||
/* Rectangle of zero size shouldn't be merged, but replaced */ |
||||
Range2D rectangle({-1.0f, -1.0f}, {-1.0f, -1.0f}); |
||||
Vector2 cursorPosition(1.0f, 2.0f); |
||||
|
||||
Layouter l; |
||||
CORRADE_COMPARE(l.renderGlyph(0, cursorPosition, rectangle), |
||||
Containers::pair(Range2D{{2.0f, 2.5f}, {2.1f, 3.0f}}, |
||||
Range2D{{0.3f, 1.1f}, {-0.5f, 0.7f}})); |
||||
CORRADE_COMPARE(cursorPosition, Vector2(3.0f, 1.0f)); |
||||
CORRADE_COMPARE(rectangle, Range2D({2.0f, 2.5f}, {2.1f, 3.0f})); |
||||
|
||||
CORRADE_COMPARE(l.renderGlyph(1, cursorPosition, rectangle), |
||||
Containers::pair(Range2D{{4.0f, 1.5f}, {4.1f, 2.0f}}, |
||||
Range2D{{0.3f, 1.1f}, {-0.5f, 0.7f}})); |
||||
CORRADE_COMPARE(cursorPosition, Vector2(5.0f, 0.0f)); |
||||
CORRADE_COMPARE(rectangle, Range2D({2.0f, 1.5f}, {4.1f, 3.0f})); |
||||
|
||||
CORRADE_COMPARE(l.renderGlyph(2, cursorPosition, rectangle), |
||||
Containers::pair(Range2D{{6.0f, 0.5f}, {6.1f, 1.0f}}, |
||||
Range2D{{0.3f, 1.1f}, {-0.5f, 0.7f}})); |
||||
CORRADE_COMPARE(cursorPosition, Vector2(7.0f, -1.0f)); |
||||
CORRADE_COMPARE(rectangle, Range2D({2.0f, 0.5f}, {6.1f, 3.0f})); |
||||
} |
||||
|
||||
void AbstractLayouterTest::renderGlyphOutOfRange() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
struct Layouter: AbstractLayouter { |
||||
explicit Layouter(): AbstractLayouter{3} {} |
||||
|
||||
Containers::Triple<Range2D, Range2D, Vector2> doRenderGlyph(UnsignedInt) override { return {}; } |
||||
} layouter; |
||||
|
||||
Range2D rectangle; |
||||
Vector2 cursorPosition; |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
layouter.renderGlyph(3, cursorPosition, rectangle); |
||||
CORRADE_COMPARE(out.str(), "Text::AbstractLayouter::renderGlyph(): index 3 out of range for 3 glyphs\n"); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Text::Test::AbstractLayouterTest) |
||||
@ -0,0 +1,675 @@
|
||||
/*
|
||||
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 <sstream> |
||||
#include <Corrade/Containers/StringView.h> |
||||
#include <Corrade/Containers/StringStl.h> /** @todo remove once Debug is stream-free */ |
||||
#include <Corrade/Containers/StridedArrayView.h> |
||||
#include <Corrade/TestSuite/Tester.h> |
||||
#include <Corrade/TestSuite/Compare/String.h> |
||||
#include <Corrade/Utility/DebugStl.h> /** @todo remove once Debug is stream-free */ |
||||
|
||||
#include "Magnum/Math/Vector2.h" |
||||
#include "Magnum/Text/AbstractShaper.h" |
||||
#include "Magnum/Text/Feature.h" |
||||
#include "Magnum/Text/Script.h" |
||||
|
||||
namespace Magnum { namespace Text { namespace Test { namespace { |
||||
|
||||
struct AbstractShaperTest: TestSuite::Tester { |
||||
explicit AbstractShaperTest(); |
||||
|
||||
void debugDirection(); |
||||
|
||||
void featureRangeConstruct(); |
||||
void featureRangeConstructBeginEnd(); |
||||
|
||||
void construct(); |
||||
void constructCopy(); |
||||
void constructMove(); |
||||
|
||||
void setScript(); |
||||
void setScriptNotImplemented(); |
||||
|
||||
void setLanguage(); |
||||
void setLanguageNotImplemented(); |
||||
|
||||
void setDirection(); |
||||
void setDirectionNotImplemented(); |
||||
|
||||
void shape(); |
||||
void shapeNoFeatures(); |
||||
void shapeNoBeginEnd(); |
||||
void shapeNoBeginEndFeatures(); |
||||
void shapeScriptLanguageDirectionNotImplemented(); |
||||
void shapeFailed(); |
||||
void shapeBeginEndOutOfRange(); |
||||
void shapeEmptyText(); |
||||
|
||||
/* glyphsInto() tested in shape() already */ |
||||
void glyphsIntoEmpty(); |
||||
void glyphsIntoInvalidViewSizes(); |
||||
}; |
||||
|
||||
AbstractShaperTest::AbstractShaperTest() { |
||||
addTests({&AbstractShaperTest::debugDirection, |
||||
|
||||
&AbstractShaperTest::featureRangeConstruct, |
||||
&AbstractShaperTest::featureRangeConstructBeginEnd, |
||||
|
||||
&AbstractShaperTest::construct, |
||||
&AbstractShaperTest::constructCopy, |
||||
&AbstractShaperTest::constructMove, |
||||
|
||||
&AbstractShaperTest::setScript, |
||||
&AbstractShaperTest::setScriptNotImplemented, |
||||
|
||||
&AbstractShaperTest::setLanguage, |
||||
&AbstractShaperTest::setLanguageNotImplemented, |
||||
|
||||
&AbstractShaperTest::setDirection, |
||||
&AbstractShaperTest::setDirectionNotImplemented, |
||||
|
||||
&AbstractShaperTest::shape, |
||||
&AbstractShaperTest::shapeNoFeatures, |
||||
&AbstractShaperTest::shapeNoBeginEnd, |
||||
&AbstractShaperTest::shapeNoBeginEndFeatures, |
||||
&AbstractShaperTest::shapeScriptLanguageDirectionNotImplemented, |
||||
&AbstractShaperTest::shapeFailed, |
||||
&AbstractShaperTest::shapeBeginEndOutOfRange, |
||||
&AbstractShaperTest::shapeEmptyText, |
||||
|
||||
&AbstractShaperTest::glyphsIntoEmpty, |
||||
&AbstractShaperTest::glyphsIntoInvalidViewSizes}); |
||||
} |
||||
|
||||
void AbstractShaperTest::debugDirection() { |
||||
std::ostringstream out; |
||||
Debug{&out} << Direction::RightToLeft << Direction(0xab); |
||||
CORRADE_COMPARE(out.str(), "Text::Direction::RightToLeft Text::Direction(0xab)\n"); |
||||
} |
||||
|
||||
void AbstractShaperTest::featureRangeConstruct() { |
||||
FeatureRange a{Feature::Kerning}; |
||||
FeatureRange b{Feature::StandardLigatures, false}; |
||||
FeatureRange c{Feature::AccessAllAlternates, 13}; |
||||
CORRADE_COMPARE(a.feature(), Feature::Kerning); |
||||
CORRADE_COMPARE(b.feature(), Feature::StandardLigatures); |
||||
CORRADE_COMPARE(c.feature(), Feature::AccessAllAlternates); |
||||
CORRADE_VERIFY(a.isEnabled()); |
||||
CORRADE_VERIFY(!b.isEnabled()); |
||||
CORRADE_COMPARE(a.value(), 1); |
||||
CORRADE_COMPARE(b.value(), 0); |
||||
CORRADE_COMPARE(c.value(), 13); |
||||
CORRADE_COMPARE(a.begin(), 0); |
||||
CORRADE_COMPARE(b.begin(), 0); |
||||
CORRADE_COMPARE(c.begin(), 0); |
||||
CORRADE_COMPARE(a.end(), ~UnsignedInt{}); |
||||
CORRADE_COMPARE(b.end(), ~UnsignedInt{}); |
||||
CORRADE_COMPARE(c.end(), ~UnsignedInt{}); |
||||
|
||||
constexpr FeatureRange ca{Feature::Kerning}; |
||||
constexpr FeatureRange cb{Feature::StandardLigatures, false}; |
||||
constexpr FeatureRange cc{Feature::AccessAllAlternates, 13}; |
||||
CORRADE_COMPARE(ca.feature(), Feature::Kerning); |
||||
CORRADE_COMPARE(cb.feature(), Feature::StandardLigatures); |
||||
CORRADE_COMPARE(cc.feature(), Feature::AccessAllAlternates); |
||||
CORRADE_VERIFY(ca.isEnabled()); |
||||
CORRADE_VERIFY(!cb.isEnabled()); |
||||
CORRADE_COMPARE(ca.value(), 1); |
||||
CORRADE_COMPARE(cb.value(), 0); |
||||
CORRADE_COMPARE(cc.value(), 13); |
||||
CORRADE_COMPARE(ca.begin(), 0); |
||||
CORRADE_COMPARE(cb.begin(), 0); |
||||
CORRADE_COMPARE(cc.begin(), 0); |
||||
CORRADE_COMPARE(ca.end(), ~UnsignedInt{}); |
||||
CORRADE_COMPARE(cb.end(), ~UnsignedInt{}); |
||||
CORRADE_COMPARE(cc.end(), ~UnsignedInt{}); |
||||
} |
||||
|
||||
void AbstractShaperTest::featureRangeConstructBeginEnd() { |
||||
FeatureRange a{Feature::Kerning, 7, 26}; |
||||
FeatureRange b{Feature::StandardLigatures, 7, 26, false}; |
||||
FeatureRange c{Feature::AccessAllAlternates, 7, 26, 13}; |
||||
CORRADE_COMPARE(a.feature(), Feature::Kerning); |
||||
CORRADE_COMPARE(b.feature(), Feature::StandardLigatures); |
||||
CORRADE_COMPARE(c.feature(), Feature::AccessAllAlternates); |
||||
CORRADE_VERIFY(a.isEnabled()); |
||||
CORRADE_VERIFY(!b.isEnabled()); |
||||
CORRADE_COMPARE(a.value(), 1); |
||||
CORRADE_COMPARE(b.value(), 0); |
||||
CORRADE_COMPARE(c.value(), 13); |
||||
CORRADE_COMPARE(a.begin(), 7); |
||||
CORRADE_COMPARE(b.begin(), 7); |
||||
CORRADE_COMPARE(c.begin(), 7); |
||||
CORRADE_COMPARE(a.end(), 26); |
||||
CORRADE_COMPARE(b.end(), 26); |
||||
CORRADE_COMPARE(c.end(), 26); |
||||
|
||||
constexpr FeatureRange ca{Feature::Kerning, 7, 26}; |
||||
constexpr FeatureRange cb{Feature::StandardLigatures, 7, 26, false}; |
||||
constexpr FeatureRange cc{Feature::AccessAllAlternates, 7, 26, 13}; |
||||
CORRADE_COMPARE(ca.feature(), Feature::Kerning); |
||||
CORRADE_COMPARE(cb.feature(), Feature::StandardLigatures); |
||||
CORRADE_COMPARE(cc.feature(), Feature::AccessAllAlternates); |
||||
CORRADE_VERIFY(ca.isEnabled()); |
||||
CORRADE_VERIFY(!cb.isEnabled()); |
||||
CORRADE_COMPARE(ca.value(), 1); |
||||
CORRADE_COMPARE(cb.value(), 0); |
||||
CORRADE_COMPARE(cc.value(), 13); |
||||
CORRADE_COMPARE(ca.begin(), 7); |
||||
CORRADE_COMPARE(cb.begin(), 7); |
||||
CORRADE_COMPARE(cc.begin(), 7); |
||||
CORRADE_COMPARE(ca.end(), 26); |
||||
CORRADE_COMPARE(cb.end(), 26); |
||||
CORRADE_COMPARE(cc.end(), 26); |
||||
} |
||||
|
||||
AbstractFont& FakeFont = *reinterpret_cast<AbstractFont*>(0xdeadbeef); |
||||
|
||||
struct DummyShaper: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { return {}; } |
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
}; |
||||
|
||||
void AbstractShaperTest::construct() { |
||||
DummyShaper shaper{FakeFont}; |
||||
CORRADE_COMPARE(&shaper.font(), &FakeFont); |
||||
CORRADE_COMPARE(shaper.glyphCount(), 0); |
||||
|
||||
/* Initial state of script() etc getters verified in the shape() test */ |
||||
|
||||
/* Const overloads */ |
||||
const DummyShaper& cshaper = shaper; |
||||
CORRADE_COMPARE(&cshaper.font(), &FakeFont); |
||||
} |
||||
|
||||
void AbstractShaperTest::constructCopy() { |
||||
CORRADE_VERIFY(!std::is_copy_constructible<DummyShaper>{}); |
||||
CORRADE_VERIFY(!std::is_copy_assignable<DummyShaper>{}); |
||||
} |
||||
|
||||
void AbstractShaperTest::constructMove() { |
||||
DummyShaper a{FakeFont}; |
||||
|
||||
DummyShaper b = Utility::move(a); |
||||
CORRADE_COMPARE(&b.font(), &FakeFont); |
||||
CORRADE_COMPARE(b.glyphCount(), 0); |
||||
|
||||
DummyShaper c{*reinterpret_cast<AbstractFont*>(0xcafebabe)}; |
||||
c = Utility::move(b); |
||||
CORRADE_COMPARE(&b.font(), &FakeFont); |
||||
CORRADE_COMPARE(b.glyphCount(), 0); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_move_constructible<DummyShaper>::value); |
||||
CORRADE_VERIFY(std::is_nothrow_move_assignable<DummyShaper>::value); |
||||
} |
||||
|
||||
void AbstractShaperTest::setScript() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
bool doSetScript(Script script) override { |
||||
CORRADE_COMPARE(script, Script::Math); |
||||
called = true; |
||||
return true; |
||||
} |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { return {}; } |
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
|
||||
bool called = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
CORRADE_VERIFY(shaper.setScript(Script::Math)); |
||||
CORRADE_VERIFY(shaper.called); |
||||
} |
||||
|
||||
void AbstractShaperTest::setScriptNotImplemented() { |
||||
DummyShaper shaper{FakeFont}; |
||||
CORRADE_VERIFY(!shaper.setScript(Script::Math)); |
||||
} |
||||
|
||||
void AbstractShaperTest::setLanguage() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
bool doSetLanguage(Containers::StringView language) override { |
||||
CORRADE_COMPARE(language, "cs"); |
||||
called = true; |
||||
return true; |
||||
} |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { return {}; } |
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
|
||||
bool called = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
CORRADE_VERIFY(shaper.setLanguage("cs")); |
||||
CORRADE_VERIFY(shaper.called); |
||||
} |
||||
|
||||
void AbstractShaperTest::setLanguageNotImplemented() { |
||||
DummyShaper shaper{FakeFont}; |
||||
CORRADE_VERIFY(!shaper.setLanguage("cs")); |
||||
} |
||||
|
||||
void AbstractShaperTest::setDirection() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
bool doSetDirection(Direction direction) override { |
||||
CORRADE_COMPARE(direction, Direction::BottomToTop); |
||||
called = true; |
||||
return true; |
||||
} |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { return {}; } |
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
|
||||
bool called = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
CORRADE_VERIFY(shaper.setDirection(Direction::BottomToTop)); |
||||
CORRADE_VERIFY(shaper.called); |
||||
} |
||||
|
||||
void AbstractShaperTest::setDirectionNotImplemented() { |
||||
DummyShaper shaper{FakeFont}; |
||||
CORRADE_VERIFY(!shaper.setDirection(Direction::BottomToTop)); |
||||
} |
||||
|
||||
void AbstractShaperTest::shape() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features) override { |
||||
CORRADE_COMPARE(text, "some text"); |
||||
CORRADE_COMPARE(begin, 3); |
||||
CORRADE_COMPARE(end, 8); |
||||
CORRADE_COMPARE(features.size(), 2); |
||||
CORRADE_COMPARE(features[0].feature(), Feature::ContextualLigatures); |
||||
CORRADE_VERIFY(features[0].isEnabled()); |
||||
CORRADE_COMPARE(features[0].begin(), 0); |
||||
CORRADE_COMPARE(features[0].end(), ~UnsignedInt{}); |
||||
CORRADE_COMPARE(features[1].feature(), Feature::Kerning); |
||||
CORRADE_VERIFY(!features[1].isEnabled()); |
||||
CORRADE_COMPARE(features[1].begin(), 2); |
||||
CORRADE_COMPARE(features[1].end(), 5); |
||||
shapeCalled = true; |
||||
return 24; |
||||
} |
||||
|
||||
Script doScript() const override { |
||||
return Script::LinearA; |
||||
} |
||||
|
||||
Containers::StringView doLanguage() const override { |
||||
return "eh-UH"; |
||||
} |
||||
|
||||
Direction doDirection() const override { |
||||
return Direction::BottomToTop; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>& ids, const Containers::StridedArrayView1D<Vector2>& offsets, const Containers::StridedArrayView1D<Vector2>& advances) const override { |
||||
CORRADE_COMPARE(ids.size(), 24); |
||||
CORRADE_COMPARE(ids[0], 1337); |
||||
CORRADE_COMPARE(offsets.size(), 24); |
||||
CORRADE_COMPARE(offsets[0], (Vector2{13.0f, 37.0f})); |
||||
CORRADE_COMPARE(advances.size(), 24); |
||||
CORRADE_COMPARE(advances[0], (Vector2{42.0f, 69.0f})); |
||||
ids[1] = 666; |
||||
offsets[1] = {-4.0f, -5.0f}; |
||||
advances[1] = {12.0f, 23.0f}; |
||||
} |
||||
|
||||
bool shapeCalled = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Initially it shouldn't call into any of the implementations */ |
||||
CORRADE_COMPARE(shaper.glyphCount(), 0); |
||||
CORRADE_COMPARE(shaper.script(), Script::Unspecified); |
||||
CORRADE_COMPARE(shaper.language(), ""); |
||||
CORRADE_COMPARE(shaper.direction(), Direction::Unspecified); |
||||
|
||||
/* Shaping fills glyph count and allows calling into the implementations */ |
||||
CORRADE_COMPARE(shaper.shape("some text", 3, 8, { |
||||
Feature::ContextualLigatures, |
||||
{Feature::Kerning, 2, 5, false} |
||||
}), 24); |
||||
CORRADE_VERIFY(shaper.shapeCalled); |
||||
CORRADE_COMPARE(shaper.glyphCount(), 24); |
||||
CORRADE_COMPARE(shaper.script(), Script::LinearA); |
||||
CORRADE_COMPARE(shaper.language(), "eh-UH"); |
||||
CORRADE_COMPARE(shaper.direction(), Direction::BottomToTop); |
||||
|
||||
UnsignedInt ids[24]; |
||||
Vector2 offsets[24]; |
||||
Vector2 advances[24]; |
||||
ids[0] = 1337; |
||||
offsets[0] = {13.0f, 37.0f}; |
||||
advances[0] = {42.0f, 69.0f}; |
||||
shaper.glyphsInto(ids, offsets, advances); |
||||
CORRADE_COMPARE(ids[1], 666); |
||||
CORRADE_COMPARE(offsets[1], (Vector2{-4.0f, -5.0f})); |
||||
CORRADE_COMPARE(advances[1], (Vector2{12.0f, 23.0f})); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeNoFeatures() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features) override { |
||||
CORRADE_COMPARE(text, "some text"); |
||||
CORRADE_COMPARE(begin, 3); |
||||
CORRADE_COMPARE(end, 8); |
||||
CORRADE_COMPARE(features.size(), 0); |
||||
shapeCalled = true; |
||||
return 24; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
|
||||
bool shapeCalled = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Capture correct function name */ |
||||
CORRADE_VERIFY(true); |
||||
|
||||
CORRADE_COMPARE(shaper.shape("some text", 3, 8), 24); |
||||
CORRADE_VERIFY(shaper.shapeCalled); |
||||
CORRADE_COMPARE(shaper.glyphCount(), 24); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeNoBeginEnd() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features) override { |
||||
CORRADE_COMPARE(text, "some text"); |
||||
CORRADE_COMPARE(begin, 0); |
||||
CORRADE_COMPARE(end, ~UnsignedInt{}); |
||||
CORRADE_COMPARE(features.size(), 2); |
||||
CORRADE_COMPARE(features[0].feature(), Feature::ContextualLigatures); |
||||
CORRADE_VERIFY(features[0].isEnabled()); |
||||
CORRADE_COMPARE(features[0].begin(), 0); |
||||
CORRADE_COMPARE(features[0].end(), ~UnsignedInt{}); |
||||
CORRADE_COMPARE(features[1].feature(), Feature::Kerning); |
||||
CORRADE_VERIFY(!features[1].isEnabled()); |
||||
CORRADE_COMPARE(features[1].begin(), 2); |
||||
CORRADE_COMPARE(features[1].end(), 5); |
||||
shapeCalled = true; |
||||
return 24; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
|
||||
bool shapeCalled = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Capture correct function name */ |
||||
CORRADE_VERIFY(true); |
||||
|
||||
/* Shaping fills glyph count and allows calling into the implementations */ |
||||
CORRADE_COMPARE(shaper.shape("some text", { |
||||
Feature::ContextualLigatures, |
||||
{Feature::Kerning, 2, 5, false} |
||||
}), 24); |
||||
CORRADE_VERIFY(shaper.shapeCalled); |
||||
CORRADE_COMPARE(shaper.glyphCount(), 24); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeNoBeginEndFeatures() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView text, UnsignedInt begin, UnsignedInt end, Containers::ArrayView<const FeatureRange> features) override { |
||||
CORRADE_COMPARE(text, "some text"); |
||||
CORRADE_COMPARE(begin, 0); |
||||
CORRADE_COMPARE(end, ~UnsignedInt{}); |
||||
CORRADE_COMPARE(features.size(), 0); |
||||
shapeCalled = true; |
||||
return 24; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
|
||||
bool shapeCalled = false; |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Capture correct function name */ |
||||
CORRADE_VERIFY(true); |
||||
|
||||
CORRADE_COMPARE(shaper.shape("some text"), 24); |
||||
CORRADE_VERIFY(shaper.shapeCalled); |
||||
CORRADE_COMPARE(shaper.glyphCount(), 24); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeScriptLanguageDirectionNotImplemented() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { |
||||
return 24; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Initially it won't call into any of the implementations */ |
||||
CORRADE_COMPARE(shaper.script(), Script::Unspecified); |
||||
CORRADE_COMPARE(shaper.language(), ""); |
||||
CORRADE_COMPARE(shaper.direction(), Direction::Unspecified); |
||||
|
||||
CORRADE_COMPARE(shaper.shape("some text"), 24); |
||||
|
||||
/* It should delegate to the default implementations, which return the same
|
||||
values as if shape() wouldn't be called at all */ |
||||
CORRADE_COMPARE(shaper.script(), Script::Unspecified); |
||||
CORRADE_COMPARE(shaper.language(), ""); |
||||
CORRADE_COMPARE(shaper.direction(), Direction::Unspecified); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeFailed() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { |
||||
return 0; |
||||
} |
||||
|
||||
Script doScript() const override { |
||||
return Script::LinearA; |
||||
} |
||||
|
||||
Containers::StringView doLanguage() const override { |
||||
return "eh-UH"; |
||||
} |
||||
|
||||
Direction doDirection() const override { |
||||
return Direction::BottomToTop; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
} shaper{FakeFont}; |
||||
|
||||
/* The implementation is responsible for printing a message, the base class
|
||||
doesn't */ |
||||
CORRADE_COMPARE(shaper.shape("some text", 3, 8), 0); |
||||
|
||||
/* After a failure it shouldn't call into any of the implementations
|
||||
either */ |
||||
CORRADE_COMPARE(shaper.glyphCount(), 0); |
||||
CORRADE_COMPARE(shaper.script(), Script::Unspecified); |
||||
CORRADE_COMPARE(shaper.language(), ""); |
||||
CORRADE_COMPARE(shaper.direction(), Direction::Unspecified); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeBeginEndOutOfRange() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { |
||||
CORRADE_FAIL("This shouldn't be called"); |
||||
return 5; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Capture correct function name */ |
||||
CORRADE_VERIFY(true); |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
/* Begin out of range, end unbounded */ |
||||
shaper.shape("hello", 6, ~UnsignedInt{}); |
||||
shaper.shape("hello", { |
||||
Feature::AccessAllAlternates, |
||||
{Feature::Kerning, 6, ~UnsignedInt{}}, |
||||
}); |
||||
/* Begin and end out of range */ |
||||
shaper.shape("hello", 6, 7); |
||||
shaper.shape("hello", { |
||||
Feature::AccessAllAlternates, |
||||
{Feature::Kerning, 6, 7}, |
||||
}); |
||||
/* End out of range */ |
||||
shaper.shape("hello", 4, 6); |
||||
shaper.shape("hello", { |
||||
Feature::AccessAllAlternates, |
||||
{Feature::Kerning, 4, 6}, |
||||
}); |
||||
/* Begin larger than end */ |
||||
shaper.shape("hello", 4, 3); |
||||
shaper.shape("hello", { |
||||
Feature::AccessAllAlternates, |
||||
{Feature::Kerning, 4, 3}, |
||||
}); |
||||
CORRADE_COMPARE_AS(out.str(), |
||||
"Text::AbstractShaper::shape(): begin 6 and end 4294967295 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): feature 1 begin 6 and end 4294967295 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): begin 6 and end 7 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): feature 1 begin 6 and end 7 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): begin 4 and end 6 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): feature 1 begin 4 and end 6 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): begin 4 and end 3 out of range for a text of 5 bytes\n" |
||||
"Text::AbstractShaper::shape(): feature 1 begin 4 and end 3 out of range for a text of 5 bytes\n", |
||||
TestSuite::Compare::String); |
||||
} |
||||
|
||||
void AbstractShaperTest::shapeEmptyText() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { |
||||
CORRADE_FAIL("This shouldn't be called"); |
||||
return 5; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override {} |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Capture correct function name */ |
||||
CORRADE_VERIFY(true); |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
shaper.shape(""); |
||||
shaper.shape("hello", 3, 3); |
||||
shaper.shape("hello", 5, ~UnsignedInt{}); |
||||
CORRADE_COMPARE_AS(out.str(), |
||||
"Text::AbstractShaper::shape(): shaped text at begin 0 is empty\n" |
||||
"Text::AbstractShaper::shape(): shaped text at begin 3 is empty\n" |
||||
"Text::AbstractShaper::shape(): shaped text at begin 5 is empty\n", |
||||
TestSuite::Compare::String); |
||||
} |
||||
|
||||
void AbstractShaperTest::glyphsIntoEmpty() { |
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { |
||||
return 0; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override { |
||||
CORRADE_FAIL("This shouldn't be called"); |
||||
} |
||||
} shaper{FakeFont}; |
||||
|
||||
/* Capture correct function name */ |
||||
CORRADE_VERIFY(true); |
||||
|
||||
/* This should not assert but also not call anywhere */ |
||||
shaper.glyphsInto(nullptr, nullptr, nullptr); |
||||
} |
||||
|
||||
void AbstractShaperTest::glyphsIntoInvalidViewSizes() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
struct: AbstractShaper { |
||||
using AbstractShaper::AbstractShaper; |
||||
|
||||
UnsignedInt doShape(Containers::StringView, UnsignedInt, UnsignedInt, Containers::ArrayView<const FeatureRange>) override { |
||||
return 5; |
||||
} |
||||
|
||||
void doGlyphsInto(const Containers::StridedArrayView1D<UnsignedInt>&, const Containers::StridedArrayView1D<Vector2>&, const Containers::StridedArrayView1D<Vector2>&) const override { |
||||
CORRADE_FAIL("This shouldn't be called"); |
||||
} |
||||
} shaper{FakeFont}; |
||||
|
||||
CORRADE_COMPARE(shaper.shape("yey"), 5); |
||||
|
||||
UnsignedInt ids[5]; |
||||
UnsignedInt idsWrong[6]; |
||||
Vector2 offsets[5]; |
||||
Vector2 offsetsWrong[6]; |
||||
Vector2 advances[5]; |
||||
Vector2 advancesWrong[6]; |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
shaper.glyphsInto(idsWrong, offsets, advances); |
||||
shaper.glyphsInto(ids, offsetsWrong, advances); |
||||
shaper.glyphsInto(ids, offsets, advancesWrong); |
||||
CORRADE_COMPARE(out.str(), |
||||
"Text::AbstractShaper::glyphsInto(): expected the ids, offsets and advanced views to have a size of 5 but got 6, 5 and 5\n" |
||||
"Text::AbstractShaper::glyphsInto(): expected the ids, offsets and advanced views to have a size of 5 but got 5, 6 and 5\n" |
||||
"Text::AbstractShaper::glyphsInto(): expected the ids, offsets and advanced views to have a size of 5 but got 5, 5 and 6\n"); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Text::Test::AbstractShaperTest) |
||||
Loading…
Reference in new issue