1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     font_Font.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_FONT_H_
17 #define NN_FONT_FONT_FONT_H_
18 
19 #include <nn/types.h>
20 #include <nn/font/font_CharStrmReader.h>
21 #include <nn/font/font_ResourceFormat.h>
22 #include <climits>
23 
24 #define NN_FONT_MIN_ASSERT(exp, min)            NN_ASSERT((exp) >= (min))
25 
26 #define NN_FONT_MINMAX_ASSERT(exp, min, max)    NN_ASSERT((exp) >= (min) && (exp) <= (max))
27 
28 
29 namespace nn {
30 namespace font {
31 
32 class Font;
33 
34 //---- Texture format types
35 typedef u16     TexFmt;
36 
37 namespace internal {
38 
39 union TextureSize
40 {
41     struct
42     {
43         u16                 height;   //
44         u16                 width;    //
45     } HW;
46     u32                 size;   //
47 };
48 
49 class TextureObject
50 {
51 public:
52                             TextureObject();
53 
54     void                    Set(
55                                 u32         name,
56                                 const Font* pFont,
57                                 const void* pImage,
58                                 TexFmt      format,
59                                 u16         width,
60                                 u16         height);
61 
Reset()62     void                    Reset()
63     {
64         Set(0, NULL, NULL, FONT_SHEET_FORMAT_A8, 0, 0);
65     }
66 
GetName()67     u32                     GetName() const
68     {
69         return m_Name;
70     }
71 
GetFont()72     const Font*             GetFont() const
73     {
74         return m_pFont;
75     }
76 
SetName(u32 texName)77     void                    SetName(u32 texName)
78     {
79         m_Name = texName;
80     }
81 
GetImage()82     uptr                    GetImage() const
83     {
84         return m_pImage;
85     }
86 
GetFormat()87     u8                      GetFormat() const
88     {
89         return m_Format;
90     }
91 
GetSize()92     const TextureSize       GetSize() const
93     {
94         return m_Size;
95     }
96 
97     u32                     GetWrapFilter() const;
98 
99 private:
100     u32                     m_Name;     //
101     const Font*             m_pFont;    //
102     uptr                    m_pImage;   //
103     TextureSize             m_Size;     //
104     u8                      m_Format;   //
105     NN_PADDING3;
106 };
107 
108 }   // namespace internal
109 
110 
111 //---- Character code type
112 typedef u16     CharCode;
113 
114 //---------------------------------------------------------------------------
115 //
116 //---------------------------------------------------------------------------
117 struct Glyph
118 {
119     const void* pTexture;                   //
120     CharWidths  widths;                     //
121     u8          height;                     //
122     u16         texWidth;                   //
123     u16         texHeight;                  //
124     u16         cellX;                      //
125     u16         cellY;                      //
126     u8          isSheetUpdated;             //
127     NN_PADDING1;
128     TexFmt      texFormat;                  //
129     const internal::TextureObject*
130                 pTextureObject;             //
131 
132     //
133                 Glyph();
134 };
135 
136 //---------------------------------------------------------------------------
137 //
138 //---------------------------------------------------------------------------
139 class Font
140 {
141 public:
142     /* ------------------------------------------------------------------------
143             Types
144        ------------------------------------------------------------------------ */
145     enum Type
146     {
147         TYPE_NULL,          //
148         TYPE_ROM,           // RomFont
149         TYPE_RESOURCE,      // ResFont
150         TYPE_PAIR           // PairFont
151     };
152 
153 
154     /* ------------------------------------------------------------------------
155             Constants
156        ------------------------------------------------------------------------ */
157     // Invalid character code
158     static const CharCode   INVALID_CHARACTER_CODE = INVALID_CHAR_CODE;
159 
160 
161     /* ------------------------------------------------------------------------
162             Functions
163        ------------------------------------------------------------------------ */
164 
165     //
166     //
167 
168     //
Font()169                             Font()  {}
170 
171     //
172     virtual                 ~Font();
173 
174     //
175 
176 
177     //
178     //
179 
180     //
181     //
182     //
183     //
184     virtual int             GetWidth() const = 0;
185 
186     //
187     //
188     //
189     //
190     virtual int             GetHeight() const = 0;
191 
192     //
193     //
194     //
195     //
196     virtual int             GetAscent() const = 0;
197 
198     //
199     //
200     //
201     //
202     virtual int             GetDescent() const = 0;
203 
204     //
205     //
206     //
207     //
208     virtual int             GetMaxCharWidth() const = 0;
209 
210     //
211     //
212     //
213     //
214     virtual Type            GetType() const = 0;
215 
216     //
217     //
218     //
219     //
220     virtual TexFmt          GetTextureFormat() const = 0;
221 
222     //
223     //
224     //
225     //
226     virtual int             GetLineFeed() const = 0;
227 
228     //
229     //
230     //
231     //
232     virtual const CharWidths
233                             GetDefaultCharWidths() const = 0;
234 
235     //
236 
237 
238     //
239     //
240 
241     //
242     //
243     //
244     //
245     virtual void            SetLineFeed(int linefeed) = 0;
246 
247     //
248     //
249     //
250     //
251     virtual void            SetDefaultCharWidths(
252                                 const CharWidths& widths
253                             ) = 0;
254 
255     //
256     //
257     //
258     //
259     //
260     //
261     //
262     virtual bool            SetAlternateChar(CharCode c) = 0;
263 
264     //
265 
266 
267     //
268     //
269 
270     //
271     //
272     //
273     //
274     //
275     //
276     virtual int             GetCharWidth(CharCode c) const = 0;
277 
278     //
279     //
280     //
281     //
282     //
283     //
284     virtual const CharWidths
285                             GetCharWidths(CharCode c) const = 0;
286 
287     //
288     //
289     //
290     //
291     //
292     virtual void            GetGlyph(
293                                 Glyph*      pGlyph,
294                                 CharCode    c
295                             ) const = 0;
296 
297     //
298     //
299     //
300     //
301     //
302     //
303     virtual bool            HasGlyph(
304                                 CharCode    c
305                             ) const = 0;
306 
307     //
308 
309 
310     //
311     //
312 
313     //
314     //
315     //
316     //
317     virtual CharacterCode   GetCharacterCode() const = 0;
318 
319     //
320     //
321     //
322     //
323     //
324     //
325     //
326     //
327     //
328     const CharStrmReader    GetCharStrmReader(char dummy) const;
329 
330     //
331     //
332     //
333     //
334     //
335     //
336     //
337     //
338     //
339     const CharStrmReader    GetCharStrmReader(wchar_t dummy) const;
340 
341     //
342 
343 
344     //
345     //
346 
347     //
348     //
349     //
350     //
351     virtual int             GetBaselinePos() const = 0;
352 
353     //
354     //
355     //
356     //
357     virtual int             GetCellHeight() const = 0;
358 
359     //
360     //
361     //
362     //
363     virtual int             GetCellWidth() const = 0;
364 
365     //
366 
367     //
368     //
369 
370     //
371     //
372     //
373     //
374     //
375     //
376     //
377     virtual void            EnableLinearFilter(
378                                 bool    atSmall,
379                                 bool    atLarge
380                             ) = 0;
381 
382     //
383     //
384     //
385     //
386     //
387     virtual bool            IsLinearFilterEnableAtSmall() const = 0;
388 
389     //
390     //
391     //
392     //
393     //
394     virtual bool            IsLinearFilterEnableAtLarge() const = 0;
395 
396     //
397     //
398     //
399     //
400     virtual u32             GetTextureWrapFilterValue() const = 0;
401     //
402 };
403 
404 namespace internal {
405 
406 inline
407 u32
GetWrapFilter()408 TextureObject::GetWrapFilter() const
409 {
410     return m_pFont->GetTextureWrapFilterValue();
411 }
412 
413 void                    LoadTexture(
414                             u16         texWidth,
415                             u16         texHeight,
416                             TexFmt      texFormat,
417                             const void* pImage,
418                             bool        isSmallLinearFilter,
419                             bool        isLargeLinearFilter);
420 
421 }   // namespace internal
422 
423 }   // namespace font
424 }   // namespace nn
425 
426 #endif //  NN_FONT_FONT_FONT_H_
427