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