1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     font_ResourceFormat.h
4 
5   Copyright (C)2009-2010 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: 28630 $
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         シグネチャ定義
31    ------------------------------------------------------------------------ */
32 
33 // 通常フォントリソースファイルシグネチャ
34 const detail::SigWord BINFILE_SIG_FONT              = NN_FONT_MAKE_SIGWORD('C','F','N','T');
35 
36 // オフセット解決済みバイナリファイルシグネチャ
37 const detail::SigWord BINFILE_SIG_FONT_RESOLEVED    = NN_FONT_MAKE_SIGWORD('C','F','N','U');
38 
39 // アーカイブフォントリソースファイルシグネチャ
40 const detail::SigWord BINFILE_SIG_FONTA             = NN_FONT_MAKE_SIGWORD('C','F','N','A');
41 
42 // バイナリブロックシグネチャ
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         リリースバージョン定義
58    ------------------------------------------------------------------------ */
59 
60 const u32 FONT_FILE_VERSION             = NN_FONT_MAKE_VERSION(3, 0, 0, 0);
61 
62 
63 
64 /* ------------------------------------------------------------------------
65         定数定義
66    ------------------------------------------------------------------------ */
67 const u16 INVALID_CHAR_CODE   = 0xFFFF;
68 const u16 INVALID_GLYPH_INDEX = 0xFFFF;
69 
70 
71 
72 
73 
74 /* ------------------------------------------------------------------------
75         enum定義
76    ------------------------------------------------------------------------ */
77 
78 /*---------------------------------------------------------------------------*
79   Name:         FontType
80 
81   Description:  フォントタイプ
82  *---------------------------------------------------------------------------*/
83 enum FontType
84 {
85     FONT_TYPE_GLYPH,            // BMP
86     FONT_TYPE_TEXTURE,          // Texture
87     NUM_OF_FONT_TYPE
88 };
89 
90 //---------------------------------------------------------------------------
91 //! @brief        文字コード
92 //---------------------------------------------------------------------------
93 enum CharacterCode
94 {
95     CHARACTER_CODE_UNICODE = 1,  //!< Unicode
96     CHARACTER_CODE_SJIS,         //!< ShiftJIS
97     CHARACTER_CODE_CP1252,       //!< CP1252
98 
99     CHARACTER_CODE_MAX
100 };
101 
102 /*---------------------------------------------------------------------------*
103   Name:         FontMapMethod
104 
105   Description:  文字コードマッピングタイプ
106  *---------------------------------------------------------------------------*/
107 enum FontMapMethod
108 {
109     FONT_MAPMETHOD_DIRECT,      // インデックス = 文字コード - オフセット
110     FONT_MAPMETHOD_TABLE,       // インデックス = mapInfo[文字コード - オフセット]
111     FONT_MAPMETHOD_SCAN,        // インデックス = search(mapInfo, 文字コード)
112     NUM_OF_FONT_MAPMETHOD
113 };
114 
115 
116 /*---------------------------------------------------------------------------*
117   Name:         FontSheetFormat
118 
119   Description:  シートの形式 (テクスチャフォーマット)
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    // 1 なら圧縮されている
140 };
141 
142 
143 
144 
145 /* ------------------------------------------------------------------------
146         構造体定義
147    ------------------------------------------------------------------------ */
148 
149 //---------------------------------------------------------------------------
150 //! @brief        文字の各種幅を保持する構造体です。
151 //---------------------------------------------------------------------------
152 struct CharWidths
153 {
154     s8  left;                   //!< 文字の左スペースの幅
155     u8  glyphWidth;             //!< 文字のグリフ幅
156     s8  charWidth;              //!< 文字の幅 = 左スペース幅 + グリフ幅 + 右スペース幅
157 };
158 
159 /*---------------------------------------------------------------------------*
160   Name:         CMapScanEntry
161 
162   Description:  文字コードとグリフインデックスペア
163  *---------------------------------------------------------------------------*/
164 struct CMapScanEntry
165 {
166     u16 ccode;                  // 文字コード
167     u16 index;                  // グリフインデックス
168 };
169 
170 /*---------------------------------------------------------------------------*
171   Name:         CMapInfoScan
172 
173   Description:  MAPMETHOD_SCAN の場合の FontCodeMap.mapInfo
174  *---------------------------------------------------------------------------*/
175 struct CMapInfoScan
176 {
177     u16 num;                    // entries の要素数
GetEntriesCMapInfoScan178     CMapScanEntry* GetEntries() const // 文字コードからグリフインデックスへのマッピングリスト
179     {
180         return reinterpret_cast<CMapScanEntry*>( reinterpret_cast<uptr>(this) + sizeof(*this) );
181     }
182 };
183 
184 
185 
186 
187 
188 /* ------------------------------------------------------------------------
189         ブロック本体定義
190    ------------------------------------------------------------------------ */
191 
192 /*---------------------------------------------------------------------------*
193   Name:         FontGlyphGroups
194 
195   Description:  フォントを部分ロードするための情報を格納します。
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     以下は可変長データが続きます
209 
210     (4 byte align)
211 
212     u32 sizeSheets[numSheet];       // 圧縮済みシートのサイズ
213     u32 sizeCWDH[numCWDH];          // CWDH ブロックのサイズ
214     u32 sizeCMAP[numCMAP];          // CMAP ブロックのサイズ
215     u32 useSheets[numSet][numSheet/32];
216     u32 useCWDH[numSet][numCWDH/32];
217     u32 useCMAP[numSet][numCMAP/32];
218 
219     char names[numSet][name length];
220 */
221 };
222 
223 
224 
225 /*---------------------------------------------------------------------------*
226   Name:         FontTextureGlyph
227 
228   Description:  フォントのグリフテクスチャを格納します。
229  *---------------------------------------------------------------------------*/
230 struct FontTextureGlyph
231 {
232     u8  cellWidth;              // セル幅=最大グリフ幅
233     u8  cellHeight;             // セル高さ
234     s8  baselinePos;            // ベースライン位置
235     u8  maxCharWidth;           // 最大文字幅
236     u32 sheetSize;              // テクスチャシートデータサイズ(byte単位)
237     u16 sheetNum;               // シート数
238     u16 sheetFormat;            // FontSheetFormat
239     u16 sheetRow;               // シート内の横方向セル数
240     u16 sheetLine;              // シート内の縦方向セル数
241     u16 sheetWidth;             // シート幅(ピクセル単位)
242     u16 sheetHeight;            // シート高さ(ピクセル単位)
243     u8* sheetImage;             // テクスチャデータへのポインタ
244 };
245 
246 
247 
248 /*---------------------------------------------------------------------------*
249   Name:         FontWidth
250 
251   Description:  各文字の文字幅情報を格納します。
252  *---------------------------------------------------------------------------*/
253 struct FontWidth
254 {
255     u16                 indexBegin;     // widthTable の最初のエントリが対応するグリフインデックス
256     u16                 indexEnd;       // widthTable の最後のエントリが対応するグリフインデックス
257     FontWidth*          pNext;          // 次の FontWidth へのポインタ
GetWidthTableFontWidth258     CharWidths*         GetWidthTable() const // 幅情報の配列
259     {
260         return reinterpret_cast<CharWidths*>( reinterpret_cast<uptr>(this) + sizeof(*this) );
261     }
262 };
263 
264 
265 
266 /*---------------------------------------------------------------------------*
267   Name:         FontCodeMap
268 
269   Description:  文字コードから、文字コードに対応するグリフの
270                 グリフイメージ配列中のインデックスへのマッピングを規定します。
271  *---------------------------------------------------------------------------*/
272 struct FontCodeMap
273 {
274     u16                 ccodeBegin;     // このブロックが担当する文字コード範囲の最初の文字コード
275     u16                 ccodeEnd;       // このブロックが担当する文字コード範囲の最後の文字コード
276     u16                 mappingMethod;  // マッピング方法 (FontMappingMethod型)
277     u16                 reserved;       // 予約
278     FontCodeMap*        pNext;          // 次の FontCodeMap へのポインタ
GetMapInfoFontCodeMap279     u16*                GetMapInfo() const // 文字コードマッピング情報 具体的な内容は mappingMethod に依る
280     {
281         return reinterpret_cast<u16*>( reinterpret_cast<uptr>(this) + sizeof(*this) );
282     }
283 };
284 
285 
286 
287 /*---------------------------------------------------------------------------*
288   Name:         FontInformation
289 
290   Description:  フォント全体に渡る情報を格納します。
291  *---------------------------------------------------------------------------*/
292 struct FontInformation
293 {
294     u8                  fontType;       // グリフデータタイプ (FontType型)
295     s8                  linefeed;       // (*)== leading
296     u16                 alterCharIndex; // (*)フォントに含まれない文字用のグリフのグリフインデックス
297     CharWidths          defaultWidth;   // (*)文字幅情報を持たないグリフ用の文字幅情報
298     u8                  characterCode;  // 対応する文字コード (CharacterCode型)
299     FontTextureGlyph*   pGlyph;         // 唯一の FontGlyph へのポインタ
300     FontWidth*          pWidth;         // 最初の FontWidth へのポインタ
301     FontCodeMap*        pMap;           // 最初の FontCodeMap へのポインタ
302     u8                  height;         // フォントの高さ
303     u8                  width;          // フォントの幅
304     u8                  ascent;         // アセント
305     u8                  padding_[1];
306 };
307 
308 
309 
310 /* ------------------------------------------------------------------------
311         ブロック定義
312    ------------------------------------------------------------------------ */
313 
314 /*---------------------------------------------------------------------------*
315   Name:         BinaryBlock
316 
317   Description:  NintendoWare 標準バイナリブロック
318  *---------------------------------------------------------------------------*/
319 template <typename BlockBodyType>
320 struct BinaryBlock
321 {
322     detail::BinaryBlockHeader       header;     // ブロックヘッダ
323     BlockBodyType                   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