font.h
Go to the documentation of this file.
/**
* @file font.h
* @brief Shared font support.
*/
#pragma once
#include <3ds/types.h>
///@name Data types
///@{
/// Character width information structure.
typedef struct
{
s8 left; ///< Horizontal offset to draw the glyph with.
u8 glyphWidth; ///< Width of the glyph.
u8 charWidth; ///< Width of the character, that is, horizontal distance to advance.
} charWidthInfo_s;
/// Font texture sheet information.
typedef struct
{
u8 cellWidth; ///< Width of a glyph cell.
u8 cellHeight; ///< Height of a glyph cell.
u8 baselinePos; ///< Vertical position of the baseline.
u8 maxCharWidth; ///< Maximum character width.
u32 sheetSize; ///< Size in bytes of a texture sheet.
u16 nSheets; ///< Number of texture sheets.
u16 sheetFmt; ///< GPU texture format (GPU_TEXCOLOR).
u16 nRows; ///< Number of glyphs per row per sheet.
u16 nLines; ///< Number of glyph rows per sheet.
u16 sheetWidth; ///< Texture sheet width.
u16 sheetHeight; ///< Texture sheet height.
u8* sheetData; ///< Pointer to texture sheet data.
} TGLP_s;
/// Font character width information block type.
typedef struct tag_CWDH_s CWDH_s;
/// Font character width information block structure.
struct tag_CWDH_s
{
u16 startIndex; ///< First Unicode codepoint the block applies to.
u16 endIndex; ///< Last Unicode codepoint the block applies to.
CWDH_s* next; ///< Pointer to the next block.
charWidthInfo_s widths[0]; ///< Table of character width information structures.
};
/// Font character map methods.
enum
{
CMAP_TYPE_DIRECT = 0, ///< Identity mapping.
CMAP_TYPE_TABLE = 1, ///< Mapping using a table.
CMAP_TYPE_SCAN = 2, ///< Mapping using a list of mapped characters.
};
/// Font character map type.
typedef struct tag_CMAP_s CMAP_s;
/// Font character map structure.
struct tag_CMAP_s
{
u16 codeBegin; ///< First Unicode codepoint the block applies to.
u16 codeEnd; ///< Last Unicode codepoint the block applies to.
u16 mappingMethod; ///< Mapping method.
u16 reserved;
CMAP_s* next; ///< Pointer to the next map.
union
{
u16 indexOffset; ///< For CMAP_TYPE_DIRECT: index of the first glyph.
u16 indexTable[0]; ///< For CMAP_TYPE_TABLE: table of glyph indices.
/// For CMAP_TYPE_SCAN: Mapping data.
struct
{
u16 nScanEntries; ///< Number of pairs.
/// Mapping pairs.
struct
{
u16 code; ///< Unicode codepoint.
u16 glyphIndex; ///< Mapped glyph index.
} scanEntries[0];
};
};
};
/// Font information structure.
typedef struct
{
u32 signature; ///< Signature (FINF).
u32 sectionSize; ///< Section size.
u8 fontType; ///< Font type
u8 lineFeed; ///< Line feed vertical distance.
u16 alterCharIndex; ///< Glyph index of the replacement character.
charWidthInfo_s defaultWidth; ///< Default character width information.
u8 encoding; ///< Font encoding (?)
TGLP_s* tglp; ///< Pointer to texture sheet information.
CWDH_s* cwdh; ///< Pointer to the first character width information block.
CMAP_s* cmap; ///< Pointer to the first character map.
u8 height; ///< Font height.
u8 width; ///< Font width.
u8 ascent; ///< Font ascent.
u8 padding;
} FINF_s;
/// Font structure.
typedef struct
{
u32 signature; ///< Signature (CFNU).
u16 endianness; ///< Endianness constant (0xFEFF).
u16 headerSize; ///< Header size.
u32 version; ///< Format version.
u32 fileSize; ///< File size.
u32 nBlocks; ///< Number of blocks.
FINF_s finf; ///< Font information.
} CFNT_s;
/// Font glyph position structure.
typedef struct
{
int sheetIndex; ///< Texture sheet index to use to render the glyph.
float xOffset; ///< Horizontal offset to draw the glyph width.
float xAdvance; ///< Horizontal distance to advance after drawing the glyph.
float width; ///< Glyph width.
/// Texture coordinates to use to render the glyph.
struct
{
float left, top, right, bottom;
} texcoord;
/// Vertex coordinates to use to render the glyph.
struct
{
float left, top, right, bottom;
} vtxcoord;
} fontGlyphPos_s;
/// Flags for use with fontCalcGlyphPos.
enum
{
GLYPH_POS_CALC_VTXCOORD = BIT(0), ///< Calculates vertex coordinates in addition to texture coordinates.
GLYPH_POS_AT_BASELINE = BIT(1), ///< Position the glyph at the baseline instead of at the top-left corner.
GLYPH_POS_Y_POINTS_UP = BIT(2), ///< Indicates that the Y axis points up instead of down.
};
///@}
///@name Initialization and basic operations
///@{
/// Ensures the shared system font is mapped.
Result fontEnsureMapped(void);
/**
* @brief Fixes the pointers internal to a just-loaded font
* @param font Font to fix
* @remark Should never be run on the system font, and only once on any other font.
*/
void fontFixPointers(CFNT_s* font);
/// Gets the currently loaded system font
static inline CFNT_s* fontGetSystemFont(void)
{
extern CFNT_s* g_sharedFont;
if (!g_sharedFont)
fontEnsureMapped();
return g_sharedFont;
}
/**
* @brief Retrieves the font information structure of a font.
* @param font Pointer to font structure. If NULL, the shared system font is used.
*/
static inline FINF_s* fontGetInfo(CFNT_s* font)
{
if (!font)
font = fontGetSystemFont();
return &font->finf;
}
/**
* @brief Retrieves the texture sheet information of a font.
* @param font Pointer to font structure. If NULL, the shared system font is used.
*/
static inline TGLP_s* fontGetGlyphInfo(CFNT_s* font)
{
if (!font)
font = fontGetSystemFont();
return fontGetInfo(font)->tglp;
}
/**
* @brief Retrieves the pointer to texture data for the specified texture sheet.
* @param font Pointer to font structure. If NULL, the shared system font is used.
* @param sheetIndex Index of the texture sheet.
*/
static inline void* fontGetGlyphSheetTex(CFNT_s* font, int sheetIndex)
{
if (!font)
font = fontGetSystemFont();
TGLP_s* tglp = fontGetGlyphInfo(font);
return &tglp->sheetData[sheetIndex*tglp->sheetSize];
}
/**
* @brief Retrieves the glyph index of the specified Unicode codepoint.
* @param font Pointer to font structure. If NULL, the shared system font is used.
* @param codePoint Unicode codepoint.
*/
int fontGlyphIndexFromCodePoint(CFNT_s* font, u32 codePoint);
/**
* @brief Retrieves character width information of the specified glyph.
* @param font Pointer to font structure. If NULL, the shared system font is used.
* @param glyphIndex Index of the glyph.
*/
charWidthInfo_s* fontGetCharWidthInfo(CFNT_s* font, int glyphIndex);
/**
* @brief Calculates position information for the specified glyph.
* @param out Output structure in which to write the information.
* @param font Pointer to font structure. If NULL, the shared system font is used.
* @param glyphIndex Index of the glyph.
* @param flags Calculation flags (see GLYPH_POS_* flags).
* @param scaleX Scale factor to apply horizontally.
* @param scaleY Scale factor to apply vertically.
*/
void fontCalcGlyphPos(fontGlyphPos_s* out, CFNT_s* font, int glyphIndex, u32 flags, float scaleX, float scaleY);
///@}
CMAP_TYPE_SCAN
@ CMAP_TYPE_SCAN
Mapping using a list of mapped characters.
Definition: font.h:57
CMAP_TYPE_TABLE
@ CMAP_TYPE_TABLE
Mapping using a table.
Definition: font.h:56
CMAP_TYPE_DIRECT
@ CMAP_TYPE_DIRECT
Identity mapping.
Definition: font.h:55
fontFixPointers
void fontFixPointers(CFNT_s *font)
Fixes the pointers internal to a just-loaded font.
fontGetCharWidthInfo
charWidthInfo_s * fontGetCharWidthInfo(CFNT_s *font, int glyphIndex)
Retrieves character width information of the specified glyph.
fontGlyphIndexFromCodePoint
int fontGlyphIndexFromCodePoint(CFNT_s *font, u32 codePoint)
Retrieves the glyph index of the specified Unicode codepoint.
fontGetSystemFont
static CFNT_s * fontGetSystemFont(void)
Gets the currently loaded system font.
Definition: font.h:168
GLYPH_POS_AT_BASELINE
@ GLYPH_POS_AT_BASELINE
Position the glyph at the baseline instead of at the top-left corner.
Definition: font.h:148
GLYPH_POS_Y_POINTS_UP
@ GLYPH_POS_Y_POINTS_UP
Indicates that the Y axis points up instead of down.
Definition: font.h:149
GLYPH_POS_CALC_VTXCOORD
@ GLYPH_POS_CALC_VTXCOORD
Calculates vertex coordinates in addition to texture coordinates.
Definition: font.h:147
fontGetInfo
static FINF_s * fontGetInfo(CFNT_s *font)
Retrieves the font information structure of a font.
Definition: font.h:180
fontEnsureMapped
Result fontEnsureMapped(void)
Ensures the shared system font is mapped.
fontCalcGlyphPos
void fontCalcGlyphPos(fontGlyphPos_s *out, CFNT_s *font, int glyphIndex, u32 flags, float scaleX, float scaleY)
Calculates position information for the specified glyph.
fontGetGlyphSheetTex
static void * fontGetGlyphSheetTex(CFNT_s *font, int sheetIndex)
Retrieves the pointer to texture data for the specified texture sheet.
Definition: font.h:203
fontGetGlyphInfo
static TGLP_s * fontGetGlyphInfo(CFNT_s *font)
Retrieves the texture sheet information of a font.
Definition: font.h:191
CFNT_s
Font structure.
Definition: font.h:114
CFNT_s::signature
u32 signature
Signature (CFNU).
Definition: font.h:115
CFNT_s::endianness
u16 endianness
Endianness constant (0xFEFF).
Definition: font.h:116
CFNT_s::headerSize
u16 headerSize
Header size.
Definition: font.h:117
CFNT_s::nBlocks
u32 nBlocks
Number of blocks.
Definition: font.h:120
CFNT_s::fileSize
u32 fileSize
File size.
Definition: font.h:119
CFNT_s::finf
FINF_s finf
Font information.
Definition: font.h:122
CFNT_s::version
u32 version
Format version.
Definition: font.h:118
FINF_s
Font information structure.
Definition: font.h:92
FINF_s::sectionSize
u32 sectionSize
Section size.
Definition: font.h:94
FINF_s::cwdh
CWDH_s * cwdh
Pointer to the first character width information block.
Definition: font.h:103
FINF_s::width
u8 width
Font width.
Definition: font.h:107
FINF_s::cmap
CMAP_s * cmap
Pointer to the first character map.
Definition: font.h:104
FINF_s::defaultWidth
charWidthInfo_s defaultWidth
Default character width information.
Definition: font.h:99
FINF_s::fontType
u8 fontType
Font type.
Definition: font.h:96
FINF_s::lineFeed
u8 lineFeed
Line feed vertical distance.
Definition: font.h:97
FINF_s::ascent
u8 ascent
Font ascent.
Definition: font.h:108
FINF_s::height
u8 height
Font height.
Definition: font.h:106
FINF_s::signature
u32 signature
Signature (FINF).
Definition: font.h:93
FINF_s::encoding
u8 encoding
Font encoding (?)
Definition: font.h:100
FINF_s::tglp
TGLP_s * tglp
Pointer to texture sheet information.
Definition: font.h:102
FINF_s::alterCharIndex
u16 alterCharIndex
Glyph index of the replacement character.
Definition: font.h:98
TGLP_s
Font texture sheet information.
Definition: font.h:21
TGLP_s::nSheets
u16 nSheets
Number of texture sheets.
Definition: font.h:28
TGLP_s::sheetFmt
u16 sheetFmt
GPU texture format (GPU_TEXCOLOR).
Definition: font.h:29
TGLP_s::nLines
u16 nLines
Number of glyph rows per sheet.
Definition: font.h:32
TGLP_s::sheetHeight
u16 sheetHeight
Texture sheet height.
Definition: font.h:35
TGLP_s::sheetWidth
u16 sheetWidth
Texture sheet width.
Definition: font.h:34
TGLP_s::cellHeight
u8 cellHeight
Height of a glyph cell.
Definition: font.h:23
TGLP_s::sheetSize
u32 sheetSize
Size in bytes of a texture sheet.
Definition: font.h:27
TGLP_s::baselinePos
u8 baselinePos
Vertical position of the baseline.
Definition: font.h:24
TGLP_s::cellWidth
u8 cellWidth
Width of a glyph cell.
Definition: font.h:22
TGLP_s::nRows
u16 nRows
Number of glyphs per row per sheet.
Definition: font.h:31
TGLP_s::sheetData
u8 * sheetData
Pointer to texture sheet data.
Definition: font.h:36
TGLP_s::maxCharWidth
u8 maxCharWidth
Maximum character width.
Definition: font.h:25
charWidthInfo_s
Character width information structure.
Definition: font.h:13
charWidthInfo_s::charWidth
u8 charWidth
Width of the character, that is, horizontal distance to advance.
Definition: font.h:16
charWidthInfo_s::left
s8 left
Horizontal offset to draw the glyph with.
Definition: font.h:14
charWidthInfo_s::glyphWidth
u8 glyphWidth
Width of the glyph.
Definition: font.h:15
fontGlyphPos_s
Font glyph position structure.
Definition: font.h:127
fontGlyphPos_s::xAdvance
float xAdvance
Horizontal distance to advance after drawing the glyph.
Definition: font.h:130
fontGlyphPos_s::width
float width
Glyph width.
Definition: font.h:131
fontGlyphPos_s::sheetIndex
int sheetIndex
Texture sheet index to use to render the glyph.
Definition: font.h:128
fontGlyphPos_s::xOffset
float xOffset
Horizontal offset to draw the glyph width.
Definition: font.h:129
tag_CMAP_s
Font character map structure.
Definition: font.h:65
tag_CMAP_s::indexTable
u16 indexTable[0]
For CMAP_TYPE_TABLE: table of glyph indices.
Definition: font.h:75
tag_CMAP_s::glyphIndex
u16 glyphIndex
Mapped glyph index.
Definition: font.h:84
tag_CMAP_s::mappingMethod
u16 mappingMethod
Mapping method.
Definition: font.h:68
tag_CMAP_s::codeBegin
u16 codeBegin
First Unicode codepoint the block applies to.
Definition: font.h:66
tag_CMAP_s::scanEntries
struct tag_CMAP_s::@12::@14::@16 scanEntries[0]
Mapping pairs.
tag_CMAP_s::indexOffset
u16 indexOffset
For CMAP_TYPE_DIRECT: index of the first glyph.
Definition: font.h:74
tag_CMAP_s::codeEnd
u16 codeEnd
Last Unicode codepoint the block applies to.
Definition: font.h:67
tag_CMAP_s::nScanEntries
u16 nScanEntries
Number of pairs.
Definition: font.h:79
tag_CMAP_s::next
CMAP_s * next
Pointer to the next map.
Definition: font.h:70
tag_CMAP_s::code
u16 code
Unicode codepoint.
Definition: font.h:83
tag_CWDH_s
Font character width information block structure.
Definition: font.h:44
tag_CWDH_s::endIndex
u16 endIndex
Last Unicode codepoint the block applies to.
Definition: font.h:46
tag_CWDH_s::widths
charWidthInfo_s widths[0]
Table of character width information structures.
Definition: font.h:49
tag_CWDH_s::startIndex
u16 startIndex
First Unicode codepoint the block applies to.
Definition: font.h:45
tag_CWDH_s::next
CWDH_s * next
Pointer to the next block.
Definition: font.h:47
types.h
Various system types.
BIT
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
u8
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
s8
int8_t s8
8-bit signed integer
Definition: types.h:26
Result
s32 Result
Function result.
Definition: types.h:42
u16
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23