|
|
|
|
#ifndef Magnum_Text_MagnumFont_h
|
|
|
|
|
#define Magnum_Text_MagnumFont_h
|
|
|
|
|
/*
|
|
|
|
|
This file is part of Magnum.
|
|
|
|
|
|
|
|
|
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
|
|
|
|
2020, 2021, 2022, 2023, 2024
|
|
|
|
|
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::MagnumFont
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Magnum/configure.h"
|
|
|
|
|
|
|
|
|
|
#ifdef MAGNUM_TARGET_GL
|
|
|
|
|
#include <Corrade/Containers/Pointer.h>
|
|
|
|
|
|
|
|
|
|
#include "Magnum/Text/AbstractFont.h"
|
|
|
|
|
#include "MagnumPlugins/MagnumFont/configure.h"
|
|
|
|
|
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
#ifndef MAGNUM_MAGNUMFONT_BUILD_STATIC
|
plugins: new testing workflow.
The current testing workflow had quite a few major flaws and it was no
longer possible after the move of Any* plugins to core. Among the flaws
is:
* Every plugin was basically built twice, once as the real plugin and
once as a static testing library. Most of the build shared common
object files, but nevertheless it inflated build times and made the
buildsystem extremely complex.
* Because the actual plugin binary was never actually loaded during the
test, it couldn't spot problems like:
- undefined references
- errors in metadata files
- mismatched plugin interface/version, missing entry points
- broken static plugin import files
* Tests that made use of independent plugins (such as TgaImageConverter
test using TgaImporter to verify the output) had a hardcoded
dependency on such plugins, making a minimal setup very hard.
* Dynamic loading of plugins from the Any* proxies was always directed
to the install location on the filesystem with no possibility to
load these directly from the build tree. That caused random ABI
mismatch crashes, or, on the other hand, if no plugins were
installed, particular portions of the codebase weren't tested at all.
Now the workflow is the following:
* Every plugin is built exactly once, either as dynamic or as static.
* The test always loads it via the plugin manager. If it's dynamic,
it's loaded straight from the build directory; if it's static, it
gets linked to the test executable directly.
* Plugins used indirectly are always served from the build directory
(if enabled) to ensure reproducibility and independence on what's
installed on the filesystem. Missing presence of these plugins causes
particular tests to be simply skipped.
* Plugins that have extensive tests for internal functionality that's
not exposed through the plugin interface are still built in two
parts, but the internal tests are simply consuming the OBJECT files
directly instead of linking to a static library.
8 years ago
|
|
|
#ifdef MagnumFont_EXPORTS
|
|
|
|
|
#define MAGNUM_MAGNUMFONT_EXPORT CORRADE_VISIBILITY_EXPORT
|
|
|
|
|
#else
|
|
|
|
|
#define MAGNUM_MAGNUMFONT_EXPORT CORRADE_VISIBILITY_IMPORT
|
|
|
|
|
#endif
|
|
|
|
|
#else
|
|
|
|
|
#define MAGNUM_MAGNUMFONT_EXPORT CORRADE_VISIBILITY_STATIC
|
|
|
|
|
#endif
|
|
|
|
|
#define MAGNUM_MAGNUMFONT_LOCAL CORRADE_VISIBILITY_LOCAL
|
|
|
|
|
#else
|
|
|
|
|
#define MAGNUM_MAGNUMFONT_EXPORT
|
|
|
|
|
#define MAGNUM_MAGNUMFONT_LOCAL
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Text {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Simple bitmap font plugin
|
|
|
|
|
|
|
|
|
|
The font consists of two files, one text file containing character and glyph
|
|
|
|
|
info and one TGA file containing the glyphs in distance field format. The font
|
|
|
|
|
can be conveniently created from any other format using
|
|
|
|
|
@ref MagnumFontConverter. The file syntax is as in following:
|
|
|
|
|
|
|
|
|
|
@code{.ini}
|
|
|
|
|
# Font image filename
|
|
|
|
|
image=font.tga
|
|
|
|
|
|
|
|
|
|
# Size of unscaled font image
|
|
|
|
|
originalImageSize=1536 1536
|
|
|
|
|
|
|
|
|
|
# Glyph padding
|
|
|
|
|
padding=9
|
|
|
|
|
|
|
|
|
|
# Font size
|
|
|
|
|
fontSize=128
|
|
|
|
|
|
|
|
|
|
# Line height
|
|
|
|
|
lineHeight=270
|
|
|
|
|
|
|
|
|
|
# Character
|
|
|
|
|
[char]
|
|
|
|
|
|
|
|
|
|
# UTF-32 codepoint ('a')
|
|
|
|
|
unicode=0061
|
|
|
|
|
|
|
|
|
|
# Glyph ID
|
|
|
|
|
glyph=1
|
|
|
|
|
|
|
|
|
|
# Advance to next character in pixels (i.e. on unscaled font image)
|
|
|
|
|
advance=45 0
|
|
|
|
|
|
|
|
|
|
# Another character
|
|
|
|
|
[char]
|
|
|
|
|
unicode=0062
|
|
|
|
|
glyph=2
|
|
|
|
|
advance=42 0
|
|
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
|
|
|
|
|
# Glyph 0, a.k.a. "Not Found" glyph
|
|
|
|
|
[glyph]
|
|
|
|
|
|
|
|
|
|
# Glyph texture position relative to baseline, in pixels
|
|
|
|
|
position=5 -1
|
|
|
|
|
|
|
|
|
|
# Glyph rectangle in font image, in pixels (left, bottom, right, top)
|
|
|
|
|
rectangle=0 0 42 25
|
|
|
|
|
|
|
|
|
|
# Glyph 1
|
|
|
|
|
[glyph]
|
|
|
|
|
position=0 0
|
|
|
|
|
rectangle=45 0 44 25
|
|
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
|
|
@section Text-MagnumFont-usage Usage
|
|
|
|
|
|
|
|
|
|
@m_class{m-note m-success}
|
|
|
|
|
|
|
|
|
|
@par
|
|
|
|
|
This class is a plugin that's meant to be dynamically loaded and used
|
|
|
|
|
through the base @ref AbstractFont interface. See its documentation for
|
|
|
|
|
introduction and usage examples.
|
|
|
|
|
|
|
|
|
|
This plugin depends on the @ref Text library and the
|
|
|
|
|
@ref Trade::TgaImporter "TgaImporter" plugin. It is built if
|
|
|
|
|
`MAGNUM_WITH_MAGNUMFONT` is enabled when building Magnum. To use as a dynamic
|
|
|
|
|
plugin, load @cpp "MagnumFont" @ce via @ref Corrade::PluginManager::Manager.
|
|
|
|
|
|
|
|
|
|
Additionally, if you're using Magnum as a CMake subproject, do the following:
|
|
|
|
|
|
|
|
|
|
@code{.cmake}
|
|
|
|
|
set(MAGNUM_WITH_MAGNUMFONT ON CACHE BOOL "" FORCE)
|
|
|
|
|
add_subdirectory(magnum EXCLUDE_FROM_ALL)
|
|
|
|
|
|
|
|
|
|
# So the dynamically loaded plugin gets built implicitly
|
|
|
|
|
add_dependencies(your-app Magnum::MagnumFont)
|
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
|
|
To use as a static plugin or as a dependency of another plugin with CMake, you
|
|
|
|
|
need to request the `MagnumFont` component of the `Magnum` package and link to
|
|
|
|
|
the `Magnum::MagnumFont` target:
|
|
|
|
|
|
|
|
|
|
@code{.cmake}
|
|
|
|
|
find_package(Magnum REQUIRED MagnumFont)
|
|
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
target_link_libraries(your-app PRIVATE Magnum::MagnumFont)
|
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
|
|
Because the plugin needs access to @ref Trade::AbstractImporter plugins, you
|
|
|
|
|
need to instantiate a manager for them and register it with
|
|
|
|
|
@ref Corrade::PluginManager::Manager::registerExternalManager():
|
|
|
|
|
|
|
|
|
|
@snippet plugins.cpp MagnumFont-importer-register
|
|
|
|
|
|
|
|
|
|
See @ref building, @ref cmake, @ref plugins and @ref file-formats for more
|
|
|
|
|
information.
|
|
|
|
|
*/
|
|
|
|
|
class MAGNUM_MAGNUMFONT_EXPORT MagnumFont: public AbstractFont {
|
|
|
|
|
public:
|
|
|
|
|
/** @brief Default constructor */
|
|
|
|
|
explicit MagnumFont();
|
|
|
|
|
|
|
|
|
|
/** @brief Plugin manager constructor */
|
|
|
|
|
explicit MagnumFont(PluginManager::AbstractManager& manager, const Containers::StringView& plugin);
|
|
|
|
|
|
|
|
|
|
~MagnumFont();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL FontFeatures doFeatures() const override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL bool doIsOpened() const override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL Properties doOpenData(Containers::ArrayView<const char> data, Float) override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL Properties doOpenFile(Containers::StringView filename, Float) override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL void doClose() override;
|
|
|
|
|
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL void doGlyphIdsInto(const Containers::StridedArrayView1D<const char32_t>& characters, const Containers::StridedArrayView1D<UnsignedInt>& glyphs) override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL Vector2 doGlyphSize(UnsignedInt glyph) override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL Vector2 doGlyphAdvance(UnsignedInt glyph) override;
|
|
|
|
|
MAGNUM_MAGNUMFONT_LOCAL Containers::Pointer<AbstractGlyphCache> doCreateGlyphCache() override;
|
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
|
|
|
MAGNUM_MAGNUMFONT_LOCAL Containers::Pointer<AbstractShaper> doCreateShaper() override;
|
|
|
|
|
|
|
|
|
|
struct Data;
|
|
|
|
|
Containers::Pointer<Data> _opened;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
#else
|
|
|
|
|
#error this header is available only in the OpenGL build
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|