1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: font_ResourceFormat.h 4 5 Copyright (C)2009-2012 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Revision: 46347 $ 14 *---------------------------------------------------------------------------*/ 15 16 #ifndef NN_FONT_FONT_RESOURCE_FORMAT_H_ 17 #define NN_FONT_FONT_RESOURCE_FORMAT_H_ 18 19 #include <nn/types.h> 20 #include <nn/font/detail/font_BinaryFileFormat.h> 21 22 namespace nn { 23 namespace font { 24 25 26 const int GlyphDataAlignment = 128; 27 28 29 /* ------------------------------------------------------------------------ 30 Signature Definitions 31 ------------------------------------------------------------------------ */ 32 33 // Standard font resource file signature 34 const detail::SigWord BINFILE_SIG_FONT = NN_FONT_MAKE_SIGWORD('C','F','N','T'); 35 36 // Binary file signature with already resolved offset 37 const detail::SigWord BINFILE_SIG_FONT_RESOLEVED = NN_FONT_MAKE_SIGWORD('C','F','N','U'); 38 39 // Archive font resource file signature 40 const detail::SigWord BINFILE_SIG_FONTA = NN_FONT_MAKE_SIGWORD('C','F','N','A'); 41 42 // Binary block signature 43 const detail::SigWord BINBLOCK_SIG_FINF = NN_FONT_MAKE_SIGWORD('F','I','N','F'); 44 const detail::SigWord BINBLOCK_SIG_CGLP = NN_FONT_MAKE_SIGWORD('C','G','L','P'); 45 const detail::SigWord BINBLOCK_SIG_TGLP = NN_FONT_MAKE_SIGWORD('T','G','L','P'); 46 const detail::SigWord BINBLOCK_SIG_CWDH = NN_FONT_MAKE_SIGWORD('C','W','D','H'); 47 const detail::SigWord BINBLOCK_SIG_CMAP = NN_FONT_MAKE_SIGWORD('C','M','A','P'); 48 49 const detail::SigWord BINBLOCK_SIG_GLGR = NN_FONT_MAKE_SIGWORD('G','L','G','R'); 50 const detail::SigWord BINBLOCK_SIG_HTGL = NN_FONT_MAKE_SIGWORD('H','T','G','L'); 51 52 53 54 55 56 /* ------------------------------------------------------------------------ 57 Resource version definitions 58 ------------------------------------------------------------------------ */ 59 60 const u32 FONT_FILE_VERSION = NN_FONT_MAKE_VERSION(3, 0, 0, 0); 61 62 63 64 /* ------------------------------------------------------------------------ 65 Constant definitions 66 ------------------------------------------------------------------------ */ 67 const u16 INVALID_CHAR_CODE = 0xFFFF; 68 const u16 INVALID_GLYPH_INDEX = 0xFFFF; 69 70 71 72 73 74 /* ------------------------------------------------------------------------ 75 enum Definitions 76 ------------------------------------------------------------------------ */ 77 78 /*---------------------------------------------------------------------------* 79 Name: FontType 80 Description: Font type 81 *--------------------------------------------------------------------------- 82 */ 83 enum FontType 84 { 85 FONT_TYPE_GLYPH, // BMP 86 FONT_TYPE_TEXTURE, // Texture 87 NUM_OF_FONT_TYPE 88 }; 89 90 //--------------------------------------------------------------------------- 91 // 92 //--------------------------------------------------------------------------- 93 enum CharacterCode 94 { 95 CHARACTER_CODE_UNICODE = 1, // 96 CHARACTER_CODE_SJIS, // 97 CHARACTER_CODE_CP1252, // 98 99 CHARACTER_CODE_MAX 100 }; 101 102 /*---------------------------------------------------------------------------* 103 Name: FontMapMethod 104 Description: character code mapping type 105 *--------------------------------------------------------------------------- 106 */ 107 enum FontMapMethod 108 { 109 FONT_MAPMETHOD_DIRECT, // index = character code - offset 110 FONT_MAPMETHOD_TABLE, // index = mapInfo[character code - offset] 111 FONT_MAPMETHOD_SCAN, // index = search(mapInfo, character code) 112 NUM_OF_FONT_MAPMETHOD 113 }; 114 115 116 /*---------------------------------------------------------------------------* 117 Name: FontSheetFormat 118 Description: Sheet format (texture format) 119 *--------------------------------------------------------------------------- 120 */ 121 enum FontSheetFormat 122 { 123 FONT_SHEET_FORMAT_RGBA8, 124 FONT_SHEET_FORMAT_RGB8, 125 FONT_SHEET_FORMAT_RGB5A1, 126 FONT_SHEET_FORMAT_RGB565, 127 FONT_SHEET_FORMAT_RGBA4, 128 FONT_SHEET_FORMAT_LA8, 129 130 131 FONT_SHEET_FORMAT_A8 = 8, 132 FONT_SHEET_FORMAT_LA4, 133 134 FONT_SHEET_FORMAT_A4 = 11, 135 136 137 138 FONT_SHEET_FORMAT_MASK = 0x7FFF, 139 FONT_SHEET_FORMAT_COMPRESSED_FLAG = 0x8000 // Compressed if 1. 140 }; 141 142 143 144 145 /* ------------------------------------------------------------------------ 146 Structure definitions 147 ------------------------------------------------------------------------ */ 148 149 //--------------------------------------------------------------------------- 150 // 151 //--------------------------------------------------------------------------- 152 struct CharWidths 153 { 154 s8 left; // 155 u8 glyphWidth; // 156 s8 charWidth; // 157 }; 158 159 /*---------------------------------------------------------------------------* 160 Name: CMapScanEntry 161 Description: Character code and glyph index pair 162 *--------------------------------------------------------------------------- 163 */ 164 struct CMapScanEntry 165 { 166 u16 ccode; // Character code 167 u16 index; // Glyph index 168 }; 169 170 /*---------------------------------------------------------------------------* 171 Name: CMapInfoScan 172 Description: FontCodeMap.mapInfo when using MAPMETHOD_SCAN 173 *--------------------------------------------------------------------------- 174 */ 175 struct CMapInfoScan 176 { 177 u16 num; // number of elements in "entries" GetEntriesCMapInfoScan178 CMapScanEntry* GetEntries() const // mapping list to the glyph index from the character code 179 { 180 return reinterpret_cast<CMapScanEntry*>( reinterpret_cast<uptr>(this) + sizeof(*this) ); 181 } 182 }; 183 184 185 186 187 188 /* ------------------------------------------------------------------------ 189 Block definition 190 ------------------------------------------------------------------------ */ 191 192 /*---------------------------------------------------------------------------* 193 Name: FontGlyphGroups 194 Description: Stores the information for partially loading font. 195 *--------------------------------------------------------------------------- 196 */ 197 struct FontGlyphGroups 198 { 199 u32 sheetSize; 200 u16 glyphsPerSheet; 201 u16 numSet; 202 u16 numSheet; 203 u16 numCWDH; 204 u16 numCMAP; 205 206 u16 nameOffsets[1]; // numSet 207 /* 208 A variable length data follows from here 209 (4 byte align) 210 u32 sizeSheets[numSheet]; // size of the compressed sheet 211 u32 sizeCWDH[numCWDH]; // size of the CWDH block 212 u32 sizeCMAP[numCMAP]; // size of the CMAP block 213 u32 useSheets[numSet][numSheet/32]; 214 u32 useCWDH[numSet][numCWDH/32]; 215 u32 useCMAP[numSet][numCMAP/32]; 216 char names[numSet][name length]; 217 218 219 220 */ 221 }; 222 223 224 225 /*---------------------------------------------------------------------------* 226 Name: FontTextureGlyph 227 Description: Stores the glyph texture of the font. 228 *--------------------------------------------------------------------------- 229 */ 230 struct FontTextureGlyph 231 { 232 u8 cellWidth; // Cell width = maximum glyph width 233 u8 cellHeight; // Cell height 234 s8 baselinePos; // Baseline position 235 u8 maxCharWidth; // Maximum character width 236 u32 sheetSize; // Texture sheet data size (in bytes) 237 u16 sheetNum; // Number of sheets 238 u16 sheetFormat; // FontSheetFormat 239 u16 sheetRow; // Number of horizontal cells on a sheet 240 u16 sheetLine; // Number of vertical cells on a sheet 241 u16 sheetWidth; // Sheet width (in pixels) 242 u16 sheetHeight; // Sheet height (in pixels) 243 u8* sheetImage; // The pointer to the texture data 244 }; 245 246 247 248 /*---------------------------------------------------------------------------* 249 Name: FontWidth 250 Description: Stores the character width information for each character. 251 *--------------------------------------------------------------------------- 252 */ 253 struct FontWidth 254 { 255 u16 indexBegin; // glyph index corresponding to the first entry in widthTable 256 u16 indexEnd; // glyph index corresponding to the last entry in widthTable 257 FontWidth* pNext; // Pointer to the next FontWidth GetWidthTableFontWidth258 CharWidths* GetWidthTable() const // width information array 259 { 260 return reinterpret_cast<CharWidths*>( reinterpret_cast<uptr>(this) + sizeof(*this) ); 261 } 262 }; 263 264 265 266 /*---------------------------------------------------------------------------* 267 Name: FontCodeMap 268 Description: Given a character code, specifies the mapping to the index of the glyph in the glyph image arrangment that matches the character code. 269 270 *--------------------------------------------------------------------------- 271 */ 272 struct FontCodeMap 273 { 274 u16 ccodeBegin; // First character code in the character code range that this block manages 275 u16 ccodeEnd; // Last character code in the character code range that this block manages 276 u16 mappingMethod; // Mapping method (FontMappingMethod type) 277 u16 reserved; // Reserved 278 FontCodeMap* pNext; // Pointer to the next FontCodeMap GetMapInfoFontCodeMap279 u16* GetMapInfo() const // The specific details of the character code mapping information depend on the mappingMethod 280 { 281 return reinterpret_cast<u16*>( reinterpret_cast<uptr>(this) + sizeof(*this) ); 282 } 283 }; 284 285 286 287 /*---------------------------------------------------------------------------* 288 Name: FontInformation 289 Description: Stores the information related to the overall font. 290 *--------------------------------------------------------------------------- 291 */ 292 struct FontInformation 293 { 294 u8 fontType; // glyph data type (FontType) 295 s8 linefeed; // (*)== leading 296 u16 alterCharIndex; // (*) glyph index of the glyph for characters not contained in the font 297 CharWidths defaultWidth; // (*) character width information for glyphs that have no character width information 298 u8 characterCode; // Corresponding character code (CharacterCode type) 299 FontTextureGlyph* pGlyph; // Pointer to a unique FontGlyph 300 FontWidth* pWidth; // Pointer to the first FontWidth 301 FontCodeMap* pMap; // Pointer to the first FontCodeMap 302 u8 height; // Font height 303 u8 width; // Font width 304 u8 ascent; // Ascent 305 u8 padding_[1]; 306 }; 307 308 309 310 /* ------------------------------------------------------------------------ 311 Block definition 312 ------------------------------------------------------------------------ */ 313 314 /*---------------------------------------------------------------------------* 315 Name: BinaryBlock 316 Description: NintendoWare standard binary block 317 *--------------------------------------------------------------------------- 318 */ 319 template <typename BlockBodyType> 320 struct BinaryBlock 321 { 322 detail::BinaryBlockHeader header; // Block header 323 BlockBodyType body; // Block body 324 }; 325 326 typedef BinaryBlock<FontGlyphGroups> FontGlyphGroupsBlock; 327 typedef BinaryBlock<FontInformation> FontInformationBlock; 328 typedef BinaryBlock<FontTextureGlyph> FontTextureGlyphBlock; 329 typedef BinaryBlock<FontWidth> FontWidthBlock; 330 typedef BinaryBlock<FontCodeMap> FontCodeMapBlock; 331 332 333 334 } // namespace font 335 } // namespace nn 336 337 338 #endif // NN_FONT_FONT_RESOURCE_FORMAT_H_ 339