libctru  v2.4.1
font.h
Go to the documentation of this file.
1 /**
2  * @file font.h
3  * @brief Shared font support.
4  */
5 #pragma once
6 #include <3ds/types.h>
7 
8 ///@name Data types
9 ///@{
10 
11 /// Character width information structure.
12 typedef struct
13 {
14  s8 left; ///< Horizontal offset to draw the glyph with.
15  u8 glyphWidth; ///< Width of the glyph.
16  u8 charWidth; ///< Width of the character, that is, horizontal distance to advance.
18 
19 /// Font texture sheet information.
20 typedef struct
21 {
22  u8 cellWidth; ///< Width of a glyph cell.
23  u8 cellHeight; ///< Height of a glyph cell.
24  u8 baselinePos; ///< Vertical position of the baseline.
25  u8 maxCharWidth; ///< Maximum character width.
26 
27  u32 sheetSize; ///< Size in bytes of a texture sheet.
28  u16 nSheets; ///< Number of texture sheets.
29  u16 sheetFmt; ///< GPU texture format (GPU_TEXCOLOR).
30 
31  u16 nRows; ///< Number of glyphs per row per sheet.
32  u16 nLines; ///< Number of glyph rows per sheet.
33 
34  u16 sheetWidth; ///< Texture sheet width.
35  u16 sheetHeight; ///< Texture sheet height.
36  u8* sheetData; ///< Pointer to texture sheet data.
37 } TGLP_s;
38 
39 /// Font character width information block type.
40 typedef struct tag_CWDH_s CWDH_s;
41 
42 /// Font character width information block structure.
43 struct tag_CWDH_s
44 {
45  u16 startIndex; ///< First Unicode codepoint the block applies to.
46  u16 endIndex; ///< Last Unicode codepoint the block applies to.
47  CWDH_s* next; ///< Pointer to the next block.
48 
49  charWidthInfo_s widths[0]; ///< Table of character width information structures.
50 };
51 
52 /// Font character map methods.
53 enum
54 {
55  CMAP_TYPE_DIRECT = 0, ///< Identity mapping.
56  CMAP_TYPE_TABLE = 1, ///< Mapping using a table.
57  CMAP_TYPE_SCAN = 2, ///< Mapping using a list of mapped characters.
58 };
59 
60 /// Font character map type.
61 typedef struct tag_CMAP_s CMAP_s;
62 
63 /// Font character map structure.
64 struct tag_CMAP_s
65 {
66  u16 codeBegin; ///< First Unicode codepoint the block applies to.
67  u16 codeEnd; ///< Last Unicode codepoint the block applies to.
68  u16 mappingMethod; ///< Mapping method.
69  u16 reserved;
70  CMAP_s* next; ///< Pointer to the next map.
71 
72  union
73  {
74  u16 indexOffset; ///< For CMAP_TYPE_DIRECT: index of the first glyph.
75  u16 indexTable[0]; ///< For CMAP_TYPE_TABLE: table of glyph indices.
76  /// For CMAP_TYPE_SCAN: Mapping data.
77  struct
78  {
79  u16 nScanEntries; ///< Number of pairs.
80  /// Mapping pairs.
81  struct
82  {
83  u16 code; ///< Unicode codepoint.
84  u16 glyphIndex; ///< Mapped glyph index.
86  };
87  };
88 };
89 
90 /// Font information structure.
91 typedef struct
92 {
93  u32 signature; ///< Signature (FINF).
94  u32 sectionSize; ///< Section size.
95 
96  u8 fontType; ///< Font type
97  u8 lineFeed; ///< Line feed vertical distance.
98  u16 alterCharIndex; ///< Glyph index of the replacement character.
99  charWidthInfo_s defaultWidth; ///< Default character width information.
100  u8 encoding; ///< Font encoding (?)
101 
102  TGLP_s* tglp; ///< Pointer to texture sheet information.
103  CWDH_s* cwdh; ///< Pointer to the first character width information block.
104  CMAP_s* cmap; ///< Pointer to the first character map.
105 
106  u8 height; ///< Font height.
107  u8 width; ///< Font width.
108  u8 ascent; ///< Font ascent.
109  u8 padding;
110 } FINF_s;
111 
112 /// Font structure.
113 typedef struct
114 {
115  u32 signature; ///< Signature (CFNU).
116  u16 endianness; ///< Endianness constant (0xFEFF).
117  u16 headerSize; ///< Header size.
118  u32 version; ///< Format version.
119  u32 fileSize; ///< File size.
120  u32 nBlocks; ///< Number of blocks.
121 
122  FINF_s finf; ///< Font information.
123 } CFNT_s;
124 
125 /// Font glyph position structure.
126 typedef struct
127 {
128  int sheetIndex; ///< Texture sheet index to use to render the glyph.
129  float xOffset; ///< Horizontal offset to draw the glyph width.
130  float xAdvance; ///< Horizontal distance to advance after drawing the glyph.
131  float width; ///< Glyph width.
132  /// Texture coordinates to use to render the glyph.
133  struct
134  {
135  float left, top, right, bottom;
136  } texcoord;
137  /// Vertex coordinates to use to render the glyph.
138  struct
139  {
140  float left, top, right, bottom;
141  } vtxcoord;
143 
144 /// Flags for use with fontCalcGlyphPos.
145 enum
146 {
147  GLYPH_POS_CALC_VTXCOORD = BIT(0), ///< Calculates vertex coordinates in addition to texture coordinates.
148  GLYPH_POS_AT_BASELINE = BIT(1), ///< Position the glyph at the baseline instead of at the top-left corner.
149  GLYPH_POS_Y_POINTS_UP = BIT(2), ///< Indicates that the Y axis points up instead of down.
150 };
151 
152 ///@}
153 
154 ///@name Initialization and basic operations
155 ///@{
156 
157 /// Ensures the shared system font is mapped.
159 
160 /**
161  * @brief Fixes the pointers internal to a just-loaded font
162  * @param font Font to fix
163  * @remark Should never be run on the system font, and only once on any other font.
164  */
166 
167 /// Gets the currently loaded system font
168 static inline CFNT_s* fontGetSystemFont(void)
169 {
170  extern CFNT_s* g_sharedFont;
171  if (!g_sharedFont)
173  return g_sharedFont;
174 }
175 
176 /**
177  * @brief Retrieves the font information structure of a font.
178  * @param font Pointer to font structure. If NULL, the shared system font is used.
179  */
180 static inline FINF_s* fontGetInfo(CFNT_s* font)
181 {
182  if (!font)
183  font = fontGetSystemFont();
184  return &font->finf;
185 }
186 
187 /**
188  * @brief Retrieves the texture sheet information of a font.
189  * @param font Pointer to font structure. If NULL, the shared system font is used.
190  */
191 static inline TGLP_s* fontGetGlyphInfo(CFNT_s* font)
192 {
193  if (!font)
194  font = fontGetSystemFont();
195  return fontGetInfo(font)->tglp;
196 }
197 
198 /**
199  * @brief Retrieves the pointer to texture data for the specified texture sheet.
200  * @param font Pointer to font structure. If NULL, the shared system font is used.
201  * @param sheetIndex Index of the texture sheet.
202  */
203 static inline void* fontGetGlyphSheetTex(CFNT_s* font, int sheetIndex)
204 {
205  if (!font)
206  font = fontGetSystemFont();
207  TGLP_s* tglp = fontGetGlyphInfo(font);
208  return &tglp->sheetData[sheetIndex*tglp->sheetSize];
209 }
210 
211 /**
212  * @brief Retrieves the glyph index of the specified Unicode codepoint.
213  * @param font Pointer to font structure. If NULL, the shared system font is used.
214  * @param codePoint Unicode codepoint.
215  */
216 int fontGlyphIndexFromCodePoint(CFNT_s* font, u32 codePoint);
217 
218 /**
219  * @brief Retrieves character width information of the specified glyph.
220  * @param font Pointer to font structure. If NULL, the shared system font is used.
221  * @param glyphIndex Index of the glyph.
222  */
224 
225 /**
226  * @brief Calculates position information for the specified glyph.
227  * @param out Output structure in which to write the information.
228  * @param font Pointer to font structure. If NULL, the shared system font is used.
229  * @param glyphIndex Index of the glyph.
230  * @param flags Calculation flags (see GLYPH_POS_* flags).
231  * @param scaleX Scale factor to apply horizontally.
232  * @param scaleY Scale factor to apply vertically.
233  */
234 void fontCalcGlyphPos(fontGlyphPos_s* out, CFNT_s* font, int glyphIndex, u32 flags, float scaleX, float scaleY);
235 
236 ///@}
@ CMAP_TYPE_SCAN
Mapping using a list of mapped characters.
Definition: font.h:57
@ CMAP_TYPE_TABLE
Mapping using a table.
Definition: font.h:56
@ CMAP_TYPE_DIRECT
Identity mapping.
Definition: font.h:55
void fontFixPointers(CFNT_s *font)
Fixes the pointers internal to a just-loaded font.
charWidthInfo_s * fontGetCharWidthInfo(CFNT_s *font, int glyphIndex)
Retrieves character width information of the specified glyph.
int fontGlyphIndexFromCodePoint(CFNT_s *font, u32 codePoint)
Retrieves the glyph index of the specified Unicode codepoint.
static CFNT_s * fontGetSystemFont(void)
Gets the currently loaded system font.
Definition: font.h:168
@ 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
Indicates that the Y axis points up instead of down.
Definition: font.h:149
@ GLYPH_POS_CALC_VTXCOORD
Calculates vertex coordinates in addition to texture coordinates.
Definition: font.h:147
static FINF_s * fontGetInfo(CFNT_s *font)
Retrieves the font information structure of a font.
Definition: font.h:180
Result fontEnsureMapped(void)
Ensures the shared system font is mapped.
void fontCalcGlyphPos(fontGlyphPos_s *out, CFNT_s *font, int glyphIndex, u32 flags, float scaleX, float scaleY)
Calculates position information for the specified glyph.
static void * fontGetGlyphSheetTex(CFNT_s *font, int sheetIndex)
Retrieves the pointer to texture data for the specified texture sheet.
Definition: font.h:203
static TGLP_s * fontGetGlyphInfo(CFNT_s *font)
Retrieves the texture sheet information of a font.
Definition: font.h:191
Font structure.
Definition: font.h:114
u32 signature
Signature (CFNU).
Definition: font.h:115
u16 endianness
Endianness constant (0xFEFF).
Definition: font.h:116
u16 headerSize
Header size.
Definition: font.h:117
u32 nBlocks
Number of blocks.
Definition: font.h:120
u32 fileSize
File size.
Definition: font.h:119
FINF_s finf
Font information.
Definition: font.h:122
u32 version
Format version.
Definition: font.h:118
Font information structure.
Definition: font.h:92
u32 sectionSize
Section size.
Definition: font.h:94
CWDH_s * cwdh
Pointer to the first character width information block.
Definition: font.h:103
u8 width
Font width.
Definition: font.h:107
CMAP_s * cmap
Pointer to the first character map.
Definition: font.h:104
charWidthInfo_s defaultWidth
Default character width information.
Definition: font.h:99
u8 fontType
Font type.
Definition: font.h:96
u8 lineFeed
Line feed vertical distance.
Definition: font.h:97
u8 ascent
Font ascent.
Definition: font.h:108
u8 height
Font height.
Definition: font.h:106
u32 signature
Signature (FINF).
Definition: font.h:93
u8 encoding
Font encoding (?)
Definition: font.h:100
TGLP_s * tglp
Pointer to texture sheet information.
Definition: font.h:102
u16 alterCharIndex
Glyph index of the replacement character.
Definition: font.h:98
Font texture sheet information.
Definition: font.h:21
u16 nSheets
Number of texture sheets.
Definition: font.h:28
u16 sheetFmt
GPU texture format (GPU_TEXCOLOR).
Definition: font.h:29
u16 nLines
Number of glyph rows per sheet.
Definition: font.h:32
u16 sheetHeight
Texture sheet height.
Definition: font.h:35
u16 sheetWidth
Texture sheet width.
Definition: font.h:34
u8 cellHeight
Height of a glyph cell.
Definition: font.h:23
u32 sheetSize
Size in bytes of a texture sheet.
Definition: font.h:27
u8 baselinePos
Vertical position of the baseline.
Definition: font.h:24
u8 cellWidth
Width of a glyph cell.
Definition: font.h:22
u16 nRows
Number of glyphs per row per sheet.
Definition: font.h:31
u8 * sheetData
Pointer to texture sheet data.
Definition: font.h:36
u8 maxCharWidth
Maximum character width.
Definition: font.h:25
Character width information structure.
Definition: font.h:13
u8 charWidth
Width of the character, that is, horizontal distance to advance.
Definition: font.h:16
s8 left
Horizontal offset to draw the glyph with.
Definition: font.h:14
u8 glyphWidth
Width of the glyph.
Definition: font.h:15
Font glyph position structure.
Definition: font.h:127
float xAdvance
Horizontal distance to advance after drawing the glyph.
Definition: font.h:130
float width
Glyph width.
Definition: font.h:131
int sheetIndex
Texture sheet index to use to render the glyph.
Definition: font.h:128
float xOffset
Horizontal offset to draw the glyph width.
Definition: font.h:129
Font character map structure.
Definition: font.h:65
u16 indexTable[0]
For CMAP_TYPE_TABLE: table of glyph indices.
Definition: font.h:75
u16 glyphIndex
Mapped glyph index.
Definition: font.h:84
u16 mappingMethod
Mapping method.
Definition: font.h:68
u16 codeBegin
First Unicode codepoint the block applies to.
Definition: font.h:66
struct tag_CMAP_s::@12::@14::@16 scanEntries[0]
Mapping pairs.
u16 indexOffset
For CMAP_TYPE_DIRECT: index of the first glyph.
Definition: font.h:74
u16 codeEnd
Last Unicode codepoint the block applies to.
Definition: font.h:67
u16 nScanEntries
Number of pairs.
Definition: font.h:79
CMAP_s * next
Pointer to the next map.
Definition: font.h:70
u16 code
Unicode codepoint.
Definition: font.h:83
Font character width information block structure.
Definition: font.h:44
u16 endIndex
Last Unicode codepoint the block applies to.
Definition: font.h:46
charWidthInfo_s widths[0]
Table of character width information structures.
Definition: font.h:49
u16 startIndex
First Unicode codepoint the block applies to.
Definition: font.h:45
CWDH_s * next
Pointer to the next block.
Definition: font.h:47
Various system types.
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
int8_t s8
8-bit signed integer
Definition: types.h:26
s32 Result
Function result.
Definition: types.h:42
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
uint32_t u32
32-bit unsigned integer
Definition: types.h:23