libgui/include/gui/FreeTypeGX.h

164 lines
5.8 KiB
C
Raw Normal View History

2017-10-29 10:28:14 +01:00
/*
* FreeTypeGX is a wrapper class for libFreeType which renders a compiled
* FreeType parsable font into a GX texture for Wii homebrew development.
* Copyright (C) 2008 Armin Tamzarian
* Modified by Dimok, 2015 for WiiU GX2
*
* This file is part of FreeTypeGX.
*
* FreeTypeGX is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTypeGX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FreeTypeGX. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
2017-10-29 10:28:14 +01:00
#include <ft2build.h>
2022-02-05 14:28:08 +01:00
#include <string>
2017-10-29 10:28:14 +01:00
#include FT_FREETYPE_H
#include FT_BITMAP_H
#include <malloc.h>
#include <map>
2020-08-13 14:20:50 +02:00
#include <mutex>
2022-02-05 14:28:08 +01:00
#include <string.h>
#include <wchar.h>
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
#include <gui/gx2_ext.h>
#include <gx2/sampler.h>
#include <gx2/texture.h>
2017-10-29 10:28:14 +01:00
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
/*! \struct ftgxCharData_
*
* Font face character glyph relevant data structure.
*/
2018-06-21 20:44:58 +02:00
typedef struct ftgxCharData_ {
2022-02-05 14:28:08 +01:00
int16_t renderOffsetX; /**< Texture X axis bearing offset. */
2017-10-29 10:28:14 +01:00
uint16_t glyphAdvanceX; /**< Character glyph X coordinate advance in pixels. */
uint16_t glyphAdvanceY; /**< Character glyph Y coordinate advance in pixels. */
2022-02-05 14:28:08 +01:00
uint32_t glyphIndex; /**< Charachter glyph index in the font face. */
2017-10-29 10:28:14 +01:00
2022-02-05 14:28:08 +01:00
int16_t renderOffsetY; /**< Texture Y axis bearing offset. */
2017-10-29 10:28:14 +01:00
int16_t renderOffsetMax; /**< Texture Y axis bearing maximum value. */
int16_t renderOffsetMin; /**< Texture Y axis bearing minimum value. */
2020-08-13 12:38:07 +02:00
GX2Texture *texture;
2017-10-29 10:28:14 +01:00
} ftgxCharData;
/*! \struct ftgxDataOffset_
*
* Offset structure which hold both a maximum and minimum value.
*/
2018-06-21 20:44:58 +02:00
typedef struct ftgxDataOffset_ {
2022-02-05 14:28:08 +01:00
int16_t ascender; /**< Maximum data offset. */
2017-10-29 10:28:14 +01:00
int16_t descender; /**< Minimum data offset. */
2022-02-05 14:28:08 +01:00
int16_t max; /**< Maximum data offset. */
int16_t min; /**< Minimum data offset. */
2017-10-29 10:28:14 +01:00
} ftgxDataOffset;
typedef struct ftgxCharData_ ftgxCharData;
typedef struct ftgxDataOffset_ ftgxDataOffset;
2022-02-05 14:28:08 +01:00
#define _TEXT(t) L##t /**< Unicode helper macro. */
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
#define FTGX_NULL 0x0000
#define FTGX_JUSTIFY_LEFT 0x0001
#define FTGX_JUSTIFY_CENTER 0x0002
#define FTGX_JUSTIFY_RIGHT 0x0004
#define FTGX_JUSTIFY_MASK 0x000f
#define FTGX_ALIGN_TOP 0x0010
#define FTGX_ALIGN_MIDDLE 0x0020
#define FTGX_ALIGN_BOTTOM 0x0040
#define FTGX_ALIGN_BASELINE 0x0080
#define FTGX_ALIGN_GLYPH_TOP 0x0100
2017-10-29 10:28:14 +01:00
#define FTGX_ALIGN_GLYPH_MIDDLE 0x0200
#define FTGX_ALIGN_GLYPH_BOTTOM 0x0400
2020-08-13 12:38:07 +02:00
#define FTGX_ALIGN_MASK 0x0ff0
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
#define FTGX_STYLE_UNDERLINE 0x1000
#define FTGX_STYLE_STRIKE 0x2000
#define FTGX_STYLE_MASK 0xf000
2017-10-29 10:28:14 +01:00
/**< Constant color value used only to sanitize Doxygen documentation. */
2022-02-05 14:28:08 +01:00
static const GX2ColorF32 ftgxWhite = (GX2ColorF32){
1.0f, 1.0f, 1.0f, 1.0f};
2017-10-29 10:28:14 +01:00
//! forward declaration
class CVideo;
/*! \class FreeTypeGX
* \brief Wrapper class for the libFreeType library with GX rendering.
* \author Armin Tamzarian
* \version 0.2.4
*
* FreeTypeGX acts as a wrapper class for the libFreeType library. It supports precaching of transformed glyph data into
* a specified texture format. Rendering of the data to the EFB is accomplished through the application of high performance
* GX texture functions resulting in high throughput of string rendering.
*/
2018-06-21 20:44:58 +02:00
class FreeTypeGX {
private:
2022-02-05 14:28:08 +01:00
FT_Library ftLibrary; /**< FreeType FT_Library instance. */
FT_Face ftFace; /**< FreeType reusable FT_Face typographic object. */
2018-06-21 20:44:58 +02:00
bool ftKerningEnabled; /**< Flag indicating the availability of font kerning data. */
2022-02-05 14:28:08 +01:00
uint8_t vertexIndex; /**< Vertex format descriptor index. */
2018-06-21 20:44:58 +02:00
GX2Sampler ftSampler;
2020-08-13 14:20:50 +02:00
std::recursive_mutex faceMutex;
std::recursive_mutex fontDataMutex;
2018-06-21 20:44:58 +02:00
typedef struct _ftGX2Data {
ftgxDataOffset ftgxAlign;
std::map<wchar_t, ftgxCharData> ftgxCharMap;
} ftGX2Data;
std::map<int16_t, ftGX2Data> fontData; /**< Map which holds the glyph data structures for the corresponding characters in one size. */
int16_t getStyleOffsetWidth(uint16_t width, uint16_t format);
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
int16_t getStyleOffsetHeight(int16_t format, uint16_t pixelSize);
void unloadFont();
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
ftgxCharData *cacheGlyphData(wchar_t charCode, int16_t pixelSize);
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
uint16_t cacheGlyphDataComplete(int16_t pixelSize);
2020-08-13 12:38:07 +02:00
2020-08-16 14:14:00 +02:00
bool loadGlyphData(FT_Bitmap *bmp, ftgxCharData *charData);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
void copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *tex, int16_t screenX, int16_t screenY, int16_t screenZ, const glm::vec4 &color, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 &blurColor,
2020-08-13 13:26:45 +02:00
const float &superSamplingScale);
2018-06-21 20:44:58 +02:00
public:
2020-08-13 12:38:07 +02:00
FreeTypeGX(const uint8_t *fontBuffer, FT_Long bufferSize, bool lastFace = false);
2018-06-21 20:44:58 +02:00
~FreeTypeGX();
2020-08-13 12:38:07 +02:00
uint16_t drawText(CVideo *pVideo, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 &color,
2020-08-13 13:26:45 +02:00
uint16_t textStyling, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 &blurColor, const float &superSamplingScale);
2018-06-21 20:44:58 +02:00
uint16_t getWidth(const wchar_t *text, int16_t pixelSize);
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
uint16_t getCharWidth(const wchar_t wChar, int16_t pixelSize, const wchar_t prevChar = 0x0000);
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
uint16_t getHeight(const wchar_t *text, int16_t pixelSize);
2020-08-13 12:38:07 +02:00
static wchar_t *charToWideChar(const char *p);
static char *wideCharToUTF8(const wchar_t *strChar);
2017-10-29 10:28:14 +01:00
};