bgfx/examples/common/font/font_manager.h

223 lines
7.6 KiB
C
Raw Normal View History

2013-05-15 17:07:04 +04:00
/*
* Copyright 2013 Jeremie Roy. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-05-15 17:07:04 +04:00
*/
#ifndef FONT_MANAGER_H_HEADER_GUARD
#define FONT_MANAGER_H_HEADER_GUARD
2013-05-15 17:01:38 +04:00
2013-04-23 00:42:11 +04:00
#include <bx/handlealloc.h>
2019-03-08 18:53:15 +03:00
#include <bx/string.h>
#include <bgfx/bgfx.h>
2013-04-23 00:42:11 +04:00
class Atlas;
2013-05-17 09:03:57 +04:00
2013-09-21 09:13:58 +04:00
#define MAX_OPENED_FILES 64
#define MAX_OPENED_FONT 64
2013-05-17 09:03:57 +04:00
#define FONT_TYPE_ALPHA UINT32_C(0x00000100) // L8
// #define FONT_TYPE_LCD UINT32_C(0x00000200) // BGRA8
// #define FONT_TYPE_RGBA UINT32_C(0x00000300) // BGRA8
#define FONT_TYPE_DISTANCE UINT32_C(0x00000400) // L8
#define FONT_TYPE_DISTANCE_SUBPIXEL UINT32_C(0x00000500) // L8
#define FONT_TYPE_DISTANCE_OUTLINE UINT32_C(0x00000600) // L8
#define FONT_TYPE_DISTANCE_OUTLINE_IMAGE UINT32_C(0x00001600) // L8 + BGRA8
#define FONT_TYPE_DISTANCE_DROP_SHADOW UINT32_C(0x00002700) // L8
#define FONT_TYPE_DISTANCE_DROP_SHADOW_IMAGE UINT32_C(0x00003800) // L8 + BGRA8
#define FONT_TYPE_DISTANCE_OUTLINE_DROP_SHADOW_IMAGE UINT32_C(0x00003900) // L8 + BGRA8
#define FONT_TYPE_MASK_DISTANCE_IMAGE UINT32_C(0x00001000)
#define FONT_TYPE_MASK_DISTANCE_DROP_SHADOW UINT32_C(0x00002000)
2013-04-23 00:42:11 +04:00
struct FontInfo
{
2013-05-17 07:54:25 +04:00
/// The font height in pixel.
uint16_t pixelSize;
2013-05-17 07:54:25 +04:00
/// Rendering type used for the font.
int16_t fontType;
2013-04-23 00:42:11 +04:00
2013-05-17 07:54:25 +04:00
/// The pixel extents above the baseline in pixels (typically positive).
float ascender;
2013-05-17 07:54:25 +04:00
/// The extents below the baseline in pixels (typically negative).
float descender;
2013-05-17 07:54:25 +04:00
/// The spacing in pixels between one row's descent and the next row's ascent.
float lineGap;
2016-08-23 00:17:08 +03:00
/// This field gives the maximum horizontal cursor advance for all glyphs in the font.
2013-05-22 19:13:17 +04:00
float maxAdvanceWidth;
2013-05-17 07:54:25 +04:00
/// The thickness of the under/hover/strike-trough line in pixels.
float underlineThickness;
/// The position of the underline relatively to the baseline.
float underlinePosition;
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Scale to apply to glyph data.
float scale;
2013-04-23 00:42:11 +04:00
};
// Glyph metrics:
// --------------
//
// xmin xmax
// | |
// |<-------- width -------->|
2013-05-15 17:21:23 +04:00
// | |
2013-04-23 00:42:11 +04:00
// | +-------------------------+----------------- ymax
// | | ggggggggg ggggg | ^ ^
2013-05-15 17:21:23 +04:00
// | | g:::::::::ggg::::g | | |
// | | g:::::::::::::::::g | | |
// | | g::::::ggggg::::::gg | | |
// | | g:::::g g:::::g | | |
// offset_x -|-------->| g:::::g g:::::g | offset_y |
// | | g:::::g g:::::g | | |
// | | g::::::g g:::::g | | |
// | | g:::::::ggggg:::::g | | |
2013-04-23 00:42:11 +04:00
// | | g::::::::::::::::g | | height
2013-05-15 17:21:23 +04:00
// | | gg::::::::::::::g | | |
2013-04-23 00:42:11 +04:00
// baseline ---*---------|---- gggggggg::::::g-----*-------- |
2013-05-15 17:21:23 +04:00
// / | | g:::::g | |
// origin | | gggggg g:::::g | |
// | | g:::::gg gg:::::g | |
// | | g::::::ggg:::::::g | |
// | | gg:::::::::::::g | |
// | | ggg::::::ggg | |
2013-04-23 00:42:11 +04:00
// | | gggggg | v
// | +-------------------------+----------------- ymin
// | |
// |------------- advance_x ---------->|
/// Unicode value of a character
2013-05-17 09:03:57 +04:00
typedef int32_t CodePoint;
2013-04-23 00:42:11 +04:00
2013-05-15 17:21:23 +04:00
/// A structure that describe a glyph.
2013-04-23 00:42:11 +04:00
struct GlyphInfo
{
2013-05-17 07:54:25 +04:00
/// Index for faster retrieval.
int32_t glyphIndex;
2013-05-15 17:21:23 +04:00
2013-04-23 00:42:11 +04:00
/// Glyph's width in pixels.
float width;
2013-04-23 00:42:11 +04:00
/// Glyph's height in pixels.
float height;
2013-05-15 17:21:23 +04:00
2013-04-23 00:42:11 +04:00
/// Glyph's left offset in pixels
float offset_x;
2013-04-23 00:42:11 +04:00
2013-05-17 07:54:25 +04:00
/// Glyph's top offset in pixels.
///
/// @remark This is the distance from the baseline to the top-most glyph
/// scan line, upwards y coordinates being positive.
float offset_y;
2013-04-23 00:42:11 +04:00
2013-05-17 07:54:25 +04:00
/// For horizontal text layouts, this is the unscaled horizontal
2016-08-23 00:17:08 +03:00
/// distance in pixels used to increment the pen position when the
2013-05-17 07:54:25 +04:00
/// glyph is drawn as part of a string of text.
float advance_x;
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// For vertical text layouts, this is the unscaled vertical distance
2016-08-23 00:17:08 +03:00
/// in pixels used to increment the pen position when the glyph is
2013-05-17 07:54:25 +04:00
/// drawn as part of a string of text.
float advance_y;
2013-05-15 17:21:23 +04:00
/// Amount to scale a bitmap image glyph.
float bitmapScale;
2013-05-17 07:54:25 +04:00
/// Region index in the atlas storing textures.
uint16_t regionIndex;
2013-04-23 00:42:11 +04:00
};
BGFX_HANDLE(TrueTypeHandle)
BGFX_HANDLE(FontHandle)
2013-04-23 00:42:11 +04:00
class FontManager
{
public:
2013-05-17 07:54:25 +04:00
/// Create the font manager using an external cube atlas (doesn't take
/// ownership of the atlas).
2013-05-16 06:42:39 +04:00
FontManager(Atlas* _atlas);
2013-05-17 07:54:25 +04:00
/// Create the font manager and create the texture cube as BGRA8 with
/// linear filtering.
2016-08-23 00:17:08 +03:00
FontManager(uint16_t _textureSideWidth = 512);
2013-05-15 17:21:23 +04:00
2013-05-16 06:42:39 +04:00
~FontManager();
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Retrieve the atlas used by the font manager (e.g. to add stuff to it)
2013-05-30 09:47:19 +04:00
const Atlas* getAtlas() const
2013-05-16 06:42:39 +04:00
{
return m_atlas;
}
2013-05-15 17:21:23 +04:00
2016-08-23 00:17:08 +03:00
/// Load a TrueType font from a given buffer. The buffer is copied and
2013-05-17 07:54:25 +04:00
/// thus can be freed or reused after this call.
///
2013-05-16 06:42:39 +04:00
/// @return invalid handle if the loading fail
2013-05-30 08:53:19 +04:00
TrueTypeHandle createTtf(const uint8_t* _buffer, uint32_t _size);
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Unload a TrueType font (free font memory) but keep loaded glyphs.
2013-05-30 08:53:19 +04:00
void destroyTtf(TrueTypeHandle _handle);
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Return a font whose height is a fixed pixel size.
FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, uint32_t _fontType = FONT_TYPE_ALPHA,
uint16_t _glyphWidthPadding = 6, uint16_t glyphHeightPadding = 6);
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Return a scaled child font whose height is a fixed pixel size.
2013-05-16 06:42:39 +04:00
FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
2013-05-15 17:21:23 +04:00
2013-05-16 06:42:39 +04:00
/// destroy a font (truetype or baked)
void destroyFont(FontHandle _handle);
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Preload a set of glyphs from a TrueType file.
///
2016-08-23 00:17:08 +03:00
/// @return True if every glyph could be preloaded, false otherwise if
2013-05-17 07:54:25 +04:00
/// the Font is a baked font, this only do validation on the characters.
2013-05-16 06:42:39 +04:00
bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
2013-05-15 17:21:23 +04:00
2013-05-17 07:54:25 +04:00
/// Preload a single glyph, return true on success.
2013-05-17 09:03:57 +04:00
bool preloadGlyph(FontHandle _handle, CodePoint _character);
2013-05-15 17:21:23 +04:00
bool addGlyphBitmap(FontHandle _handle, CodePoint _character, uint16_t _width, uint16_t height, uint16_t _pitch, float extraScale, const uint8_t* _bitmapBuffer, float glyphOffsetX, float glyphOffsetY);
2013-05-17 07:54:25 +04:00
/// Return the font descriptor of a font.
///
2013-05-16 06:42:39 +04:00
/// @remark the handle is required to be valid
2013-05-22 19:13:17 +04:00
const FontInfo& getFontInfo(FontHandle _handle) const;
2013-05-15 17:21:23 +04:00
2016-08-23 00:17:08 +03:00
/// Return the rendering informations about the glyph region. Load the
2013-05-17 07:54:25 +04:00
/// glyph from a TrueType font if possible
///
2013-05-30 08:53:19 +04:00
const GlyphInfo* getGlyphInfo(FontHandle _handle, CodePoint _codePoint);
2013-05-15 17:21:23 +04:00
float getKerning(FontHandle _handle, CodePoint _prevCodePoint, CodePoint _codePoint);
2013-05-30 08:53:19 +04:00
const GlyphInfo& getBlackGlyph() const
2013-05-16 06:42:39 +04:00
{
return m_blackGlyph;
}
2013-05-15 17:21:23 +04:00
2013-04-23 00:42:11 +04:00
private:
2013-05-16 06:42:39 +04:00
struct CachedFont;
struct CachedFile
{
uint8_t* buffer;
uint32_t bufferSize;
};
2013-05-15 17:21:23 +04:00
2013-05-16 06:42:39 +04:00
void init();
bool addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data);
2013-05-15 17:21:23 +04:00
2013-05-16 06:42:39 +04:00
bool m_ownAtlas;
Atlas* m_atlas;
2013-05-15 17:21:23 +04:00
2013-09-21 09:13:58 +04:00
bx::HandleAllocT<MAX_OPENED_FONT> m_fontHandles;
2013-05-16 06:42:39 +04:00
CachedFont* m_cachedFonts;
2013-05-15 17:21:23 +04:00
2013-09-21 09:13:58 +04:00
bx::HandleAllocT<MAX_OPENED_FILES> m_filesHandles;
2013-05-16 06:42:39 +04:00
CachedFile* m_cachedFiles;
2013-05-15 17:21:23 +04:00
2013-05-16 06:42:39 +04:00
GlyphInfo m_blackGlyph;
2013-05-15 17:21:23 +04:00
2013-05-16 06:42:39 +04:00
//temporary buffer to raster glyph
uint8_t* m_buffer;
2013-04-23 00:42:11 +04:00
};
2013-05-15 17:01:38 +04:00
#endif // FONT_MANAGER_H_HEADER_GUARD