Browse Source

Text: moved FreeType and HarfBuzz fonts into plugins.

In next few commits AbstractFont will become plugin interface. Font
implementations are now in magnum-plugins repository. Removed all traces
of FreeType and HarfBuzz dependencies.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
cda51f3dd5
  1. 6
      CMakeLists.txt
  2. 7
      doc/building.dox
  3. 5
      doc/cmake.dox
  4. 57
      modules/FindHarfBuzz.cmake
  5. 22
      modules/FindMagnum.cmake
  6. 9
      src/Magnum.h
  7. 3
      src/Platform/magnum-info.cpp
  8. 18
      src/Text/CMakeLists.txt
  9. 180
      src/Text/FreeTypeFont.cpp
  10. 118
      src/Text/FreeTypeFont.h
  11. 124
      src/Text/HarfBuzzFont.cpp
  12. 81
      src/Text/HarfBuzzFont.h
  13. 6
      src/Text/Text.h
  14. 1
      src/magnumConfigure.h.cmake

6
CMakeLists.txt

@ -43,9 +43,6 @@ option(WITH_TEXT "Build Text library" ON)
cmake_dependent_option(WITH_TEXTURETOOLS "Build TextureTools library" ON "NOT WITH_TEXT" ON)
option(WITH_MAGNUMINFO "Build magnum-info utility" ON)
# Library features
cmake_dependent_option(USE_HARFBUZZ "Use HarfBuzz in Text library" ON "WITH_TEXT" OFF)
# Application libraries
if(${CMAKE_SYSTEM_NAME} STREQUAL NaCl)
option(WITH_NACLAPPLICATION "Build NaClApplication library" OFF)
@ -97,9 +94,6 @@ endif()
if(TARGET_DESKTOP_GLES)
set(MAGNUM_TARGET_DESKTOP_GLES 1)
endif()
if(USE_HARFBUZZ)
set(MAGNUM_USE_HARFBUZZ 1)
endif()
# Installation paths
include(CorradeLibSuffix)

7
doc/building.dox

@ -106,18 +106,11 @@ and which not:
- `WITH_SHADERS` - Shaders library. Enabled automatically if `WITH_DEBUGTOOLS`
is enabled.
- `WITH_TEXT` - Text library. Enables also building of TextureTools library.
Requires **FreeType** and possibly **HarfBuzz** library (see below).
- `WITH_TEXTURETOOLS` - TextureTools library. Enabled automatically if `WITH_TEXT`
is enabled.
- `WITH_MAGNUMINFO` - `magnum-info` executable, provides information about the
engine and OpenGL capabilities.
Some dependencies are optional, although disabling them might reduce some
functionality:
- `USE_HARFBUZZ` - Defaults to `ON`, disabling it will result in worse text
rendering (no kerning & ligatures) and possible issues with non-Latin text.
None of the @ref Platform "application libraries" is built by default (and you
need at least one). Choose the one which suits your requirements and your
platform best:

5
doc/cmake.dox

@ -57,8 +57,7 @@ The optional components are:
- `%Primitives` -- Primitives library
- `%SceneGraph` -- SceneGraph library
- `%Shaders` -- Shaders library
- `%Text` -- Text library (depends on `%TextureTools` component, FreeType
library and possibly HarfBuzz library, see below)
- `%Text` -- Text library (depends on `%TextureTools` component)
- `%TextureTools` -- TextureTools library
Platform namespace is split into more components:
@ -101,8 +100,6 @@ are also available as preprocessor variables if including Magnum.h:
- `MAGNUM_TARGET_DESKTOP_GLES` -- Defined if compiled with OpenGL ES emulation
on desktop OpenGL
- `MAGNUM_TARGET_NACL` -- Defined if compiled for Google Chrome Native Client
- `MAGNUM_USE_HARFBUZZ` -- Defined if HarfBuzz library is used for text
rendering
%Corrade library provides also its own set of CMake macros and variables, see
@ref corrade-cmake "its documentation" for more information.

57
modules/FindHarfBuzz.cmake

@ -1,57 +0,0 @@
# - Find HarfBuzz
#
# This module tries to find HarfBuzz library and then defines:
# HARFBUZZ_FOUND - True if HarfBuzz library is found
# HARFBUZZ_INCLUDE_DIRS - Include dirs
# HARFBUZZ_LIBRARIES - HarfBuzz libraries
#
# Additionally these variables are defined for internal usage:
# HARFBUZZ_INCLUDE_DIR - Include dir (w/o dependencies)
# HARFBUZZ_LIBRARY - HarfBuzz library (w/o dependencies)
#
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# Library
find_library(HARFBUZZ_LIBRARY NAMES harfbuzz)
# Include dir
find_path(HARFBUZZ_INCLUDE_DIR
NAMES hb.h
PATH_SUFFIXES harfbuzz
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args("HarfBuzz" DEFAULT_MSG
HARFBUZZ_LIBRARY
HARFBUZZ_INCLUDE_DIR
)
set(HARFBUZZ_INCLUDE_DIRS ${HARFBUZZ_INCLUDE_DIR})
set(HARFBUZZ_LIBRARIES ${HARFBUZZ_LIBRARY})
mark_as_advanced(FORCE
HARFBUZZ_LIBRARY
HARFBUZZ_INCLUDE_DIR)

22
modules/FindMagnum.cmake

@ -19,9 +19,7 @@
# Primitives - Primitives library
# SceneGraph - SceneGraph library
# Shaders - Shaders library
# Text - Text library (depends on TextureTools component,
# FreeType library and possibly HarfBuzz library,
# see below)
# Text - Text library (depends on TextureTools component)
# TextureTools - TextureTools library
# GlutApplication - GLUT application (depends on GLUT library)
# GlxApplication - GLX application (depends on GLX and X11 libraries)
@ -52,8 +50,6 @@
# emulation on desktop OpenGL
# MAGNUM_TARGET_NACL - Defined if compiled for Google Chrome Native
# Client
# MAGNUM_USE_HARFBUZZ - Defined if HarfBuzz library is used for text
# rendering
#
# Additionally these variables are defined for internal usage:
# MAGNUM_INCLUDE_DIR - Root include dir (w/o
@ -132,10 +128,6 @@ string(FIND "${_magnumConfigure}" "#define MAGNUM_TARGET_DESKTOP_GLES" _TARGET_D
if(NOT _TARGET_DESKTOP_GLES EQUAL -1)
set(MAGNUM_TARGET_DESKTOP_GLES 1)
endif()
string(FIND "${_magnumConfigure}" "#define MAGNUM_USE_HARFBUZZ" _USE_HARFBUZZ)
if(NOT _USE_HARFBUZZ EQUAL -1)
set(MAGNUM_USE_HARFBUZZ 1)
endif()
if(NOT MAGNUM_TARGET_GLES OR MAGNUM_TARGET_DESKTOP_GLES)
find_package(OpenGL REQUIRED)
@ -255,18 +247,6 @@ foreach(component ${Magnum_FIND_COMPONENTS})
# Text library
if(${component} STREQUAL Text)
set(_MAGNUM_${_COMPONENT}_INCLUDE_PATH_NAMES Font.h)
# Dependencies
find_package(Freetype)
if(NOT FREETYPE_FOUND)
unset(MAGNUM_${_COMPONENT}_LIBRARY)
endif()
if(MAGNUM_USE_HARFBUZZ)
find_package(HarfBuzz)
if(NOT HARFBUZZ_FOUND)
unset(MAGNUM_${_COMPONENT}_LIBRARY)
endif()
endif()
endif()
# TextureTools library

9
src/Magnum.h

@ -124,15 +124,6 @@ and @ref MAGNUM_TARGET_GLES2_ "MAGNUM_TARGET_GLES".
*/
#define MAGNUM_TARGET_NACL_
/**
@brief HarfBuzz library usage
`MAGNUM_USE_HARFBUZZ` is defined if HarfBuzz library is used for text
rendering.
@see Text::HarfBuzzFont
*/
#define MAGNUM_USE_HARFBUZZ_
#endif
/** @{ @name Basic type definitions

3
src/Platform/magnum-info.cpp

@ -65,9 +65,6 @@ MagnumInfo::MagnumInfo(int& argc, char** argv): WindowlessGlxApplication(argc, a
#ifdef MAGNUM_TARGET_NACL
d << "MAGNUM_TARGET_NACL";
#endif
#ifdef MAGNUM_USE_HARFBUZZ
d << "MAGNUM_USE_HARFBUZZ";
#endif
}
Debug() << "";

18
src/Text/CMakeLists.txt

@ -22,38 +22,22 @@
# DEALINGS IN THE SOFTWARE.
#
find_package(Freetype REQUIRED)
include_directories(${FREETYPE_INCLUDE_DIRS})
set(MagnumText_SRCS
AbstractFont.cpp
DistanceFieldGlyphCache.cpp
GlyphCache.cpp
FreeTypeFont.cpp
TextRenderer.cpp)
set(MagnumText_HEADERS
AbstractFont.h
DistanceFieldGlyphCache.h
GlyphCache.h
FreeTypeFont.h
Text.h
TextRenderer.h
magnumTextVisibility.h)
if(USE_HARFBUZZ)
find_package(HarfBuzz REQUIRED)
include_directories(${HARFBUZZ_INCLUDE_DIRS})
set(MagnumText_SRCS ${MagnumText_SRCS} HarfBuzzFont.cpp)
set(MagnumText_HEADERS ${MagnumText_HEADERS} HarfBuzzFont.h)
endif()
add_library(MagnumText SHARED ${MagnumText_SRCS})
target_link_libraries(MagnumText Magnum MagnumTextureTools ${FREETYPE_LIBRARIES})
if(USE_HARFBUZZ)
target_link_libraries(MagnumText ${HARFBUZZ_LIBRARIES})
endif()
target_link_libraries(MagnumText Magnum MagnumTextureTools)
install(TARGETS MagnumText DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR})
install(FILES ${MagnumText_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Text)

180
src/Text/FreeTypeFont.cpp

@ -1,180 +0,0 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "FreeTypeFont.h"
#include "GlyphCache.h"
#include <algorithm>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <Utility/Unicode.h>
#include "Extensions.h"
#include "Image.h"
#include "TextureTools/Atlas.h"
#include "TextureTools/DistanceField.h"
namespace Magnum { namespace Text {
namespace {
class FreeTypeLayouter: public AbstractLayouter {
public:
explicit FreeTypeLayouter(FT_Face ftFont, const GlyphCache* const cache, const Float fontSize, const Float textSize, const std::string& text);
std::tuple<Rectangle, Rectangle, Vector2> renderGlyph(const Vector2& cursorPosition, const UnsignedInt i) override;
private:
FT_Face font;
const GlyphCache* const cache;
const Float fontSize, textSize;
std::vector<FT_UInt> glyphs;
};
}
FreeTypeFontRenderer::FreeTypeFontRenderer() {
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Init_FreeType(&_library) == 0);
}
FreeTypeFontRenderer::~FreeTypeFontRenderer() {
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Done_FreeType(_library) == 0);
}
FreeTypeFont::FreeTypeFont(FreeTypeFontRenderer& renderer, const std::string& fontFile, Float size): AbstractFont(size) {
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_New_Face(renderer.library(), fontFile.c_str(), 0, &ftFont) == 0);
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Set_Char_Size(ftFont, 0, size*64, 100, 100) == 0);
}
FreeTypeFont::FreeTypeFont(FreeTypeFontRenderer& renderer, const unsigned char* data, std::size_t dataSize, Float size): AbstractFont(size) {
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_New_Memory_Face(renderer.library(), data, dataSize, 0, &ftFont) == 0);
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Set_Char_Size(ftFont, 0, size*64, 100, 100) == 0);
}
void FreeTypeFont::createGlyphCache(GlyphCache* const cache, const std::string& characters) {
/** @bug Crash when atlas is too small */
/* Get glyph codes from characters */
std::vector<FT_UInt> charIndices;
charIndices.reserve(characters.size()+1);
charIndices.push_back(0);
for(std::size_t i = 0; i != characters.size(); ) {
UnsignedInt codepoint;
std::tie(codepoint, i) = Corrade::Utility::Unicode::nextChar(characters, i);
charIndices.push_back(FT_Get_Char_Index(ftFont, codepoint));
}
/* Remove duplicates (e.g. uppercase and lowercase mapped to same glyph) */
std::sort(charIndices.begin(), charIndices.end());
charIndices.erase(std::unique(charIndices.begin(), charIndices.end()), charIndices.end());
/* Sizes of all characters */
std::vector<Vector2i> charSizes;
charSizes.reserve(charIndices.size());
for(FT_UInt c: charIndices) {
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(ftFont, c, FT_LOAD_DEFAULT) == 0);
charSizes.push_back(Vector2i(ftFont->glyph->metrics.width, ftFont->glyph->metrics.height)/64);
}
/* Create texture atlas */
const std::vector<Rectanglei> charPositions = cache->reserve(charSizes);
/* Render all characters to the atlas and create character map */
unsigned char* pixmap = new unsigned char[cache->textureSize().product()]();
Image2D image(cache->textureSize(), Image2D::Format::Red, Image2D::Type::UnsignedByte, pixmap);
for(std::size_t i = 0; i != charPositions.size(); ++i) {
/* Load and render glyph */
/** @todo B&W only if radius != 0 */
FT_GlyphSlot glyph = ftFont->glyph;
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(ftFont, charIndices[i], FT_LOAD_DEFAULT) == 0);
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Render_Glyph(glyph, FT_RENDER_MODE_NORMAL) == 0);
/* Copy rendered bitmap to texture image */
const FT_Bitmap& bitmap = glyph->bitmap;
CORRADE_INTERNAL_ASSERT(std::abs(bitmap.width-charPositions[i].width()) <= 2);
CORRADE_INTERNAL_ASSERT(std::abs(bitmap.rows-charPositions[i].height()) <= 2);
for(Int yin = 0, yout = charPositions[i].bottom(), ymax = bitmap.rows; yin != ymax; ++yin, ++yout)
for(Int xin = 0, xout = charPositions[i].left(), xmax = bitmap.width; xin != xmax; ++xin, ++xout)
pixmap[yout*cache->textureSize().x() + xout] = bitmap.buffer[(bitmap.rows-yin-1)*bitmap.width + xin];
/* Insert glyph parameters into cache */
cache->insert(charIndices[i],
Vector2i(glyph->bitmap_left, glyph->bitmap_top-charPositions[i].height()),
charPositions[i]);
}
/* Set cache image */
cache->setImage({}, &image);
}
FreeTypeFont::~FreeTypeFont() {
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Done_Face(ftFont) == 0);
}
AbstractLayouter* FreeTypeFont::layout(const GlyphCache* const cache, const Float size, const std::string& text) {
return new FreeTypeLayouter(ftFont, cache, this->size(), size, text);
}
namespace {
FreeTypeLayouter::FreeTypeLayouter(FT_Face font, const GlyphCache* const cache, const Float fontSize, const Float textSize, const std::string& text): font(font), cache(cache), fontSize(fontSize), textSize(textSize) {
/* Get glyph codes from characters */
glyphs.reserve(text.size());
for(std::size_t i = 0; i != text.size(); ) {
UnsignedInt codepoint;
std::tie(codepoint, i) = Corrade::Utility::Unicode::nextChar(text, i);
glyphs.push_back(FT_Get_Char_Index(font, codepoint));
}
_glyphCount = glyphs.size();
}
std::tuple<Rectangle, Rectangle, Vector2> FreeTypeLayouter::renderGlyph(const Vector2& cursorPosition, const UnsignedInt i) {
/* Position of the texture in the resulting glyph, texture coordinates */
Vector2i position;
Rectanglei rectangle;
std::tie(position, rectangle) = (*cache)[glyphs[i]];
Rectangle texturePosition = Rectangle::fromSize(Vector2(position)/fontSize,
Vector2(rectangle.size())/fontSize);
Rectangle textureCoordinates(Vector2(rectangle.bottomLeft())/cache->textureSize(),
Vector2(rectangle.topRight())/cache->textureSize());
/* Load glyph */
CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(font, glyphs[i], FT_LOAD_DEFAULT) == 0);
const FT_GlyphSlot slot = font->glyph;
Vector2 offset = Vector2(0, 0); /** @todo really? */
Vector2 advance = Vector2(slot->advance.x, slot->advance.y)/(64*fontSize);
/* Absolute quad position, composed from cursor position, glyph offset
and texture position, denormalized to requested text size */
Rectangle quadPosition = Rectangle::fromSize(
(cursorPosition + offset + Vector2(texturePosition.left(), texturePosition.bottom()))*textSize,
texturePosition.size()*textSize);
return std::make_tuple(quadPosition, textureCoordinates, advance);
}
}
}}

118
src/Text/FreeTypeFont.h

@ -1,118 +0,0 @@
#ifndef Magnum_Text_FreeTypeFont_h
#define Magnum_Text_FreeTypeFont_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/** @file
* @brief Class Magnum::Text::FreeTypeFontRenderer, Magnum::Text::FreeTypeFont
*/
#include <unordered_map>
#include "Math/Geometry/Rectangle.h"
#include "Texture.h"
#include "Text/AbstractFont.h"
#include "Text/magnumTextVisibility.h"
#ifndef DOXYGEN_GENERATING_OUTPUT
struct FT_LibraryRec_;
typedef FT_LibraryRec_* FT_Library;
struct FT_FaceRec_;
typedef FT_FaceRec_* FT_Face;
#endif
namespace Magnum { namespace Text {
/**
@brief FreeType font renderer
Contains global instance of font renderer. See FreeTypeFont class documentation
for more information.
*/
class MAGNUM_TEXT_EXPORT FreeTypeFontRenderer {
public:
explicit FreeTypeFontRenderer();
~FreeTypeFontRenderer();
/** @brief FreeType library handle */
inline FT_Library library() { return _library; }
private:
FT_Library _library;
};
/**
@brief FreeType font
@section FreeTypeFont-usage Usage
You need to maintain instance of FreeTypeFontRenderer during the lifetime of
all FreeTypeFont instances. The font can be created either from file or from
memory location of format supported by [FreeType](http://www.freetype.org/)
library.
@code
Text::FreeTypeFontRenderer fontRenderer;
Text::FreeTypeFont font(fontRenderer, "MyFreeTypeFont.ttf", 48.0f);
@endcode
Next step is to prerender all the glyphs which will be used in text rendering
later, see GlyphCache for more information. See TextRenderer for information
about text rendering.
*/
class MAGNUM_TEXT_EXPORT FreeTypeFont: public AbstractFont {
public:
/**
* @brief Create font from file
* @param renderer Font renderer
* @param fontFile Font file
* @param size Font size
*/
explicit FreeTypeFont(FreeTypeFontRenderer& renderer, const std::string& fontFile, Float size);
/**
* @brief Create font from memory
* @param renderer Font renderer
* @param data Font data
* @param dataSize Font data size
* @param size Font size
*/
explicit FreeTypeFont(FreeTypeFontRenderer& renderer, const unsigned char* data, std::size_t dataSize, Float size);
~FreeTypeFont();
void createGlyphCache(GlyphCache* const cache, const std::string& characters) override;
AbstractLayouter* layout(const GlyphCache* const cache, const Float size, const std::string& text) override;
#ifdef DOXYGEN_GENERATING_OUTPUT
private:
#else
protected:
#endif
FT_Face ftFont;
};
}}
#endif

124
src/Text/HarfBuzzFont.cpp

@ -1,124 +0,0 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "HarfBuzzFont.h"
#include <hb-ft.h>
#include "GlyphCache.h"
namespace Magnum { namespace Text {
namespace {
class HarfBuzzLayouter: public AbstractLayouter {
public:
explicit HarfBuzzLayouter(hb_font_t* const font, const GlyphCache* const cache, const Float fontSize, const Float textSize, const std::string& text);
~HarfBuzzLayouter();
std::tuple<Rectangle, Rectangle, Vector2> renderGlyph(const Vector2& cursorPosition, const UnsignedInt i) override;
private:
const hb_font_t* const font;
const GlyphCache* const cache;
const Float fontSize, textSize;
hb_buffer_t* buffer;
hb_glyph_info_t* glyphInfo;
hb_glyph_position_t* glyphPositions;
};
}
HarfBuzzFont::HarfBuzzFont(FreeTypeFontRenderer& renderer, const std::string& fontFile, Float size): FreeTypeFont(renderer, fontFile, size) {
finishConstruction();
}
HarfBuzzFont::HarfBuzzFont(FreeTypeFontRenderer& renderer, const unsigned char* data, std::size_t dataSize, Float size): FreeTypeFont(renderer, data, dataSize, size) {
finishConstruction();
}
void HarfBuzzFont::finishConstruction() {
/* Create Harfbuzz font */
hbFont = hb_ft_font_create(ftFont, nullptr);
}
HarfBuzzFont::~HarfBuzzFont() {
hb_font_destroy(hbFont);
}
AbstractLayouter* HarfBuzzFont::layout(const GlyphCache* const cache, const Float size, const std::string& text) {
return new HarfBuzzLayouter(hbFont, cache, this->size(), size, text);
}
namespace {
HarfBuzzLayouter::HarfBuzzLayouter(hb_font_t* const font, const GlyphCache* const cache, const Float fontSize, const Float textSize, const std::string& text): font(font), cache(cache), fontSize(fontSize), textSize(textSize) {
/* Prepare HarfBuzz buffer */
buffer = hb_buffer_create();
hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
hb_buffer_set_script(buffer, HB_SCRIPT_LATIN);
hb_buffer_set_language(buffer, hb_language_from_string("en", 2));
/* Layout the text */
hb_buffer_add_utf8(buffer, text.c_str(), -1, 0, -1);
hb_shape(font, buffer, nullptr, 0);
glyphInfo = hb_buffer_get_glyph_infos(buffer, &_glyphCount);
glyphPositions = hb_buffer_get_glyph_positions(buffer, &_glyphCount);
}
HarfBuzzLayouter::~HarfBuzzLayouter() {
/* Destroy HarfBuzz buffer */
hb_buffer_destroy(buffer);
}
std::tuple<Rectangle, Rectangle, Vector2> HarfBuzzLayouter::renderGlyph(const Vector2& cursorPosition, const UnsignedInt i) {
/* Position of the texture in the resulting glyph, texture coordinates */
Vector2i position;
Rectanglei rectangle;
std::tie(position, rectangle) = (*cache)[glyphInfo[i].codepoint];
Rectangle texturePosition = Rectangle::fromSize(Vector2(position)/fontSize,
Vector2(rectangle.size())/fontSize);
Rectangle textureCoordinates(Vector2(rectangle.bottomLeft())/cache->textureSize(),
Vector2(rectangle.topRight())/cache->textureSize());
/* Glyph offset and advance to next glyph in normalized coordinates */
Vector2 offset = Vector2(glyphPositions[i].x_offset,
glyphPositions[i].y_offset)/(64*fontSize);
Vector2 advance = Vector2(glyphPositions[i].x_advance,
glyphPositions[i].y_advance)/(64*fontSize);
/* Absolute quad position, composed from cursor position, glyph offset
and texture position, denormalized to requested text size */
Rectangle quadPosition = Rectangle::fromSize(
(cursorPosition + offset + Vector2(texturePosition.left(), texturePosition.bottom()))*textSize,
texturePosition.size()*textSize);
return std::make_tuple(quadPosition, textureCoordinates, advance);
}
}
}}

81
src/Text/HarfBuzzFont.h

@ -1,81 +0,0 @@
#ifndef Magnum_Text_HarfBuzzFont_h
#define Magnum_Text_HarfBuzzFont_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/** @file
* @brief Class Magnum::Text::HarfBuzzFont
*/
#include "Text/FreeTypeFont.h"
#ifndef DOXYGEN_GENERATING_OUTPUT
struct hb_font_t;
#endif
#ifndef MAGNUM_USE_HARFBUZZ
#error Magnum is not compiled with HarfBuzz support
#endif
namespace Magnum { namespace Text {
/**
@brief HarfBuzz font
Improves FreeTypeFont with [HarfBuzz](http://www.freedesktop.org/wiki/Software/HarfBuzz)
text layouting capabilities, such as kerning, ligatures etc. See FreeTypeFont
class documentation for more information about usage.
*/
class MAGNUM_TEXT_EXPORT HarfBuzzFont: public FreeTypeFont {
public:
/**
* @brief Create font from file
* @param renderer Font renderer
* @param fontFile Font file
* @param size Font size
*/
explicit HarfBuzzFont(FreeTypeFontRenderer& renderer, const std::string& fontFile, Float size);
/**
* @brief Create font from memory
* @param renderer Font renderer
* @param data Font data
* @param dataSize Font data size
* @param size Font size
*/
explicit HarfBuzzFont(FreeTypeFontRenderer& renderer, const unsigned char* data, std::size_t dataSize, Float size);
~HarfBuzzFont();
AbstractLayouter* layout(const GlyphCache* const cache, const Float size, const std::string& text) override;
private:
void MAGNUM_TEXT_LOCAL finishConstruction();
hb_font_t* hbFont;
};
}}
#endif

6
src/Text/Text.h

@ -39,12 +39,6 @@ class AbstractLayouter;
class DistanceFieldGlyphCache;
class GlyphCache;
class FreeTypeFontRenderer;
class FreeTypeFont;
#ifdef MAGNUM_USE_HARFBUZZ
class HarfBuzzFont;
#endif
class AbstractTextRenderer;
template<UnsignedInt> class TextRenderer;
typedef TextRenderer<2> TextRenderer2D;

1
src/magnumConfigure.h.cmake

@ -27,4 +27,3 @@
#cmakedefine MAGNUM_TARGET_GLES3
#cmakedefine MAGNUM_TARGET_DESKTOP_GLES
#cmakedefine MAGNUM_TARGET_NACL
#cmakedefine MAGNUM_USE_HARFBUZZ

Loading…
Cancel
Save