1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     font_ResFontBase.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: 25674 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_FONT_FONT_RES_FONT_BASE_H_
17 #define NN_FONT_FONT_RES_FONT_BASE_H_
18 
19 #include <nn/types.h>
20 #include <nn/font/font_Font.h>
21 #include <nn/font/font_ResourceFormat.h>
22 
23 
24 namespace nn {
25 namespace font {
26 
27 //---------------------------------------------------------------------------
28 //! @brief        リソースを扱うフォントクラスの基底クラスです。
29 //---------------------------------------------------------------------------
30 class ResFontBase : public Font
31 {
32 public:
33     /* ------------------------------------------------------------------------
34             関数
35        ------------------------------------------------------------------------ */
36 
37     //! @name コンストラクタ/デストラクタ
38     //@{
39 
40     //! コンストラクタです。
41                             ResFontBase();
42 
43     //! デストラクタです。
44     virtual                 ~ResFontBase();
45 
46     //@}
47 
48     //! @name フォント情報の取得
49     //@{
50 
51     virtual int             GetWidth() const;
52 
53     virtual int             GetHeight() const;
54 
55     virtual int             GetAscent() const;
56 
57     virtual int             GetDescent() const;
58 
59     virtual int             GetMaxCharWidth() const;
60 
61     virtual Type            GetType() const;
62 
63     virtual TexFmt          GetTextureFormat() const;
64 
65     virtual int             GetLineFeed() const;
66 
67     virtual const CharWidths
68                             GetDefaultCharWidths() const;
69 
70     //! @name フォント情報の設定
71     //@{
72 
73     virtual void            SetLineFeed(int linefeed);
74 
75     virtual void            SetDefaultCharWidths(const CharWidths& widths);
76 
77     virtual bool            SetAlternateChar(CharCode c);
78 
79     //@}
80 
81     //! @name 文字情報の取得
82     //@{
83 
84     virtual int             GetCharWidth(CharCode c) const;
85 
86     virtual const CharWidths
87                             GetCharWidths(CharCode c) const;
88 
89     virtual void            GetGlyph(
90                                 Glyph*      pGlyph,
91                                 CharCode    c
92                             ) const;
93 
94     virtual bool            HasGlyph(CharCode c) const;
95 
96     //@}
97 
98     //! @name 文字列エンコーディング
99     //@{
100 
101     virtual CharacterCode   GetCharacterCode() const;
102 
103     //@}
104 
105     //! @name シート情報の取得
106     //@{
107 
108     virtual int             GetBaselinePos() const;
109     virtual int             GetCellHeight() const;
110     virtual int             GetCellWidth() const;
111 
112     //@}
113 
114 
115     //! @name テクスチャ補間
116     //@{
117 
118     virtual void            EnableLinearFilter(
119                                 bool    atSmall,
120                                 bool    atLarge);
121 
122     virtual bool            IsLinearFilterEnableAtSmall() const;
123 
124     virtual bool            IsLinearFilterEnableAtLarge() const;
125 
126     virtual u32             GetTextureWrapFilterValue() const;
127 
128     //@}
129 
130 protected:
131     /* ------------------------------------------------------------------------
132 133        ------------------------------------------------------------------------ */
134     typedef u16             GlyphIndex;
135 
136     /* ------------------------------------------------------------------------
137             定数
138        ------------------------------------------------------------------------ */
139     static const GlyphIndex GLYPH_INDEX_NOT_FOUND = INVALID_GLYPH_INDEX;
140 
141 
142     /* ------------------------------------------------------------------------
143             関数
144        ------------------------------------------------------------------------ */
145     //---- メンバアクセス
146 
GetFINF()147     FontInformation*        GetFINF()       { return m_pFontInfo; }
148 
GetFINF()149     const FontInformation*  GetFINF() const { return m_pFontInfo; }
150 
151     //! @brief      関連付けられているポインタかどうか判断します。
152     //!
153     //! @param[in]  ptr 比較対象のリソースフォントへのポインタ。
154     //!
155     //! @return     この ResFont が ptr の指すフォントリソースと関連付けられている
156     //!             ならば true を、そうでなければ false を返します。
157     //!
IsManaging(const void * ptr)158     bool                    IsManaging(const void* ptr) const { return m_pResource == ptr; }
159 
160     //! @brief      割り当てられたバッファとそこに配置されている FINF ブロックへの
161     //!             ポインタを設定します。
162     //!
163     //! @param[in]  pUserBuffer  割り当てられたバッファへのポインタ。
164     //! @param[in]  pFontInfo    FINF ブロックへのポインタ。
165     //!
166     void                    SetResourceBuffer(
167                                 void*               pUserBuffer,
168                                 FontInformation*    pFontInfo);
169 
170     //! @brief      SetResourceBuffer で設定されたパラメータをリセットし、
171     //!             割り当てられていたバッファへのポインタを返します。
172     //!
173     //! @return     設定されていたバッファへのポインタ。
174     //!
175     void*                   RemoveResourceBuffer();
176 
177 
178     //---- グリフインデックス
179 
180 
181     //! @brief      文字のグリフインデックスを取得します。
182     //!
183     //! @param[in]  c  グリフインデックスを取得する文字の文字コード。
184     //!
185     //! @return     文字のグリフインデックス。
186     //!             フォントに対象の文字が含まれていない場合は代替文字の
187     //!             グリフインデックスを返します。
188     //!
189     GlyphIndex              GetGlyphIndex(CharCode c) const;
190 
191     //! @brief      文字のグリフインデックスを取得します。
192     //!
193     //! @param[in]  c  グリフインデックスを取得する文字の文字コード。
194     //!
195     //! @return     文字のグリフインデックス。
196     //!             フォントに対象の文字が含まれていない場合は
197     //!             ResFontBase::GLYPH_INDEX_NOT_FOUND を返します。
198     //!
199     GlyphIndex              FindGlyphIndex(CharCode c) const;
200 
201     //! @brief      文字のグリフインデックスをCMAPブロックから探索します。
202     //!
203     //! @param[in]  pMap  探索対象のCMAPブロック本体へのポインタ。
204     //! @param[in]  c     グリフインデックスを取得する文字の文字コード。
205     //!
206     //! @return     文字のグリフインデックス。
207     //!             フォントに対象の文字が含まれていない場合は
208     //!             ResFontBase::GLYPH_INDEX_NOT_FOUND を返します。
209     //!
210     GlyphIndex              FindGlyphIndex(
211                                 const FontCodeMap*  pMap,
212                                 CharCode            c
213                             ) const;
214 
215     //! @brief      グリフインデックスに対応する文字の文字幅を取得します。
216     //!
217     //! @param[in]  index  文字幅情報を取得する文字のグリフインデックス
218     //!
219     //! @return     文字幅情報へのリファレンス。
220     //!
221     const CharWidths&       GetCharWidthsFromIndex(GlyphIndex index) const;
222 
223     //! @brief      CWDHブロック本体からグリフインデックスに対応する文字の
224     //!             文字幅情報を取得します。
225     //!
226     //! @param[in]  pWidth  探索対象のCWDHブロック本体へのポインタ。
227     //! @param[in]  index   文字幅情報を取得する文字のグリフインデックス
228     //!
229     //! return      文字幅情報へのリファレンス。
230     //!
231     const CharWidths&       GetCharWidthsFromIndex(
232                                 const FontWidth*    pWidth,
233                                 GlyphIndex          index
234                             ) const;
235 
236     //! @brief      グリフインデックスに対応するグリフデータを取得します。
237     //!
238     //! @param[out] glyph  グリフデータを格納するバッファへのポインタ。
239     //! @param[in]  index  取得するグリフデータのグリフインデックス。
240     //!
241     void                    GetGlyphFromIndex(
242                                 Glyph*      glyph,
243                                 GlyphIndex  index
244                             ) const;
245 
246 
247     //! @brief      FontTextureGlyph構造体の内容をもとにGlyphのメンバを設定します。
248     //!
249     //! @param[out] glyph  グリフデータを格納するバッファへのポインタ。
250     //! @param[in]  index  取得するグリフデータのグリフインデックス。
251     //! @param[in]  tg     FontTextureGlyph構造体への参照。
252     //!
253     static void             SetGlyphMember(
254                                 Glyph*                  glyph,
255                                 GlyphIndex              index,
256                                 const FontTextureGlyph& tg);
257 
258 
259     //! @name シート情報の取得
260     //@{
261 
262     //! @brief      アクティブなシートの数を取得します。
263     //!
264     //! @return     アクティブなシートの数を返します。
265     //!
266     virtual int             GetActiveSheetNum() const;
267 
268     //! @brief      テクスチャオブジェクトバッファへのポインタを取得します。
269     //!
270     //! @return     テクスチャオブジェクトバッファへのポインタを返します。
271     //!
272     internal::TextureObject*
GetTextureObjectsBufferPtr()273                             GetTextureObjectsBufferPtr()
274     {
275         return m_pTexObjs;
276     }
277 
278     //! @brief      テクスチャオブジェクトバッファへのポインタを取得します。
279     //!
280     //! @return     テクスチャオブジェクトバッファへのポインタを返します。
281     //!
282     const internal::TextureObject*
GetTextureObjectsBufferPtr()283                             GetTextureObjectsBufferPtr() const
284     {
285         return m_pTexObjs;
286     }
287 
288     //! @brief      テクスチャオブジェクトバッファへのポインタを設定します。
289     //!
290     //! @param[in]  buffer  テクスチャオブジェクトバッファへのポインタ
291     //!
SetTextureObjectsBufferPtr(void * buffer)292     void                    SetTextureObjectsBufferPtr(void* buffer)
293     {
294         m_pTexObjs = static_cast<internal::TextureObject*>(buffer);
295     }
296 
297     //! @brief      テクスチャオブジェクトを取得します。
298     //!
299     //! @param[in]  index  テクスチャオブジェクトへのインデックス
300     //!
301     //! @return     テクスチャオブジェクトへのポインタを返します。
302     //!
303     const internal::TextureObject*
GetTextureObject(int index)304                             GetTextureObject(int index) const
305     {
306         NN_POINTER_ASSERT(GetTextureObjectsBufferPtr());
307         return &GetTextureObjectsBufferPtr()[index];
308     }
309 
310     //! テクスチャ名を生成します。
311     void                    GenTextureNames();
312 
313     // テクスチャ名を破棄します。
314     void                    DeleteTextureNames();
315 
316     //@}
317 
318 private:
319     /* ------------------------------------------------------------------------
320             変数
321        ------------------------------------------------------------------------ */
322 
323     void*                   m_pResource;    //!< リソースへのポインタ
324 
325     //! リソース中のFINFブロック本体へのポインタ
326     FontInformation*        m_pFontInfo;
327 
328     internal::TextureObject*
329                             m_pTexObjs;     //!< テクスチャオブジェクト配列
330     u32                     m_WrapFilter;   //!< テクスチャラップ・フィルタ値
331 
332     //! 最後に検索した文字のキャッシュ
333     mutable CharCode        m_LastCharCode;
334 
335     //! 最後に検索した文字に対応するグリフインデックスのキャッシュ
336     mutable GlyphIndex      m_LastGlyphIndex;
337 };
338 
339 namespace internal {
340 
341 //---------------------------------------------------------------------------
342 //! @brief        1シートあたりのセル数を計算します。
343 //!
344 //! @param[in]    tg  FontTextureGlyph構造体への参照。
345 //!
346 //! @return       1シートあたりのセル数を返します。
347 //---------------------------------------------------------------------------
348 inline
349 u32
GetCellsInASheet(const FontTextureGlyph & tg)350 GetCellsInASheet(const FontTextureGlyph& tg)
351 {
352     return static_cast<u32>(tg.sheetRow * tg.sheetLine);
353 }
354 
355 
356 }   // namespace internal
357 
358 }   // namespace font
359 }   // namespace nn
360 
361 #endif //  NN_FONT_FONT_RES_FONT_BASE_H_
362