1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     font_CharStrmReader.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_CHAR_STRM_READER_H_
17 #define NN_FONT_FONT_CHAR_STRM_READER_H_
18 
19 #include <nn/types.h>
20 #include <nn/assert.h>
21 
22 namespace nn {
23 namespace font {
24 
25 
26 //---------------------------------------------------------------------------
27 //! @brief        文字ストリームを読み取るための方法を提供します。
28 //---------------------------------------------------------------------------
29 class CharStrmReader
30 {
31     friend class Font;
32 
33 public:
34     /* ------------------------------------------------------------------------
35             関数
36        ------------------------------------------------------------------------ */
37 
38     //! @name コンストラクタ / デストラクタ
39     //@{
40 
41     //! @brief      コンストラクタです。
42     //!
43     //! @param[in]  rhs     コピー元のオブジェクト。
44     //!
CharStrmReader(const CharStrmReader & rhs)45                             CharStrmReader(const CharStrmReader& rhs)
46     : mCharStrm(rhs.mCharStrm),
47       mReadFunc(rhs.mReadFunc)
48     {
49     }
50 
51     //! デストラクタです。
~CharStrmReader()52                             ~CharStrmReader()
53     {
54     }
55 
56     //@}
57 
58 
59     //! @name ストリーム読み取り
60     //@{
61 
62     //! @brief      CharStrmReader が解釈する文字ストリームを設定します。
63     //!
64     //! @param[in]  stream  新しく設定するストリームの開始点。
65     //!
Set(const char * stream)66     void                    Set(const char* stream)
67     {
68         NN_POINTER_ASSERT(stream);
69         NN_ASSERT(  mReadFunc == &CharStrmReader::ReadNextCharUTF8
70                    || mReadFunc == &CharStrmReader::ReadNextCharCP1252
71                    || mReadFunc == &CharStrmReader::ReadNextCharSJIS   );
72         mCharStrm = stream;
73     }
74 
75     //! @brief      CharStrmReader が解釈する文字ストリームを設定します。
76     //!
77     //! @param[in]  stream  新しく設定するストリームの開始点。
78     //!
Set(const wchar_t * stream)79     void                    Set(const wchar_t* stream)
80     {
81         NN_ALIGN_ASSERT(stream, 2);
82         NN_POINTER_ASSERT(stream);
83         NN_ASSERT(mReadFunc == &CharStrmReader::ReadNextCharUTF16);
84         mCharStrm = stream;
85     }
86 
87     //! @brief      ストリームの現在位置へのポインタを取得します。
88     //!
89     //! @return     ストリーム中の次に読み取られる文字へのポインタを返します。
90     //!
GetCurrentPos()91     const void*             GetCurrentPos() const   { return mCharStrm; }
92 
93     //! @brief      文字ストリームの次の文字の文字コードを取得すると共に、
94     //!             現在位置を一文字分進めます。
95     //!
96     //! @return     次の文字の文字コードを返します。
97     //!
Next()98     u16                     Next()                  { return (this->*mReadFunc)(); }
99 
100     //@}
101 
102 private:
103     /* ------------------------------------------------------------------------
104 105        ------------------------------------------------------------------------ */
106     typedef u16 (CharStrmReader::*ReadNextCharFunc)();
107 
108 
109     /* ------------------------------------------------------------------------
110             変数
111        ------------------------------------------------------------------------ */
112     const void*             mCharStrm;      // ストリーム現在位置へのポインタ
113     const ReadNextCharFunc  mReadFunc;      // ストリーム解釈関数へのポインタ
114 
115 
116     /* ------------------------------------------------------------------------
117             関数
118        ------------------------------------------------------------------------ */
119     //---- コンストラクタ
CharStrmReader(ReadNextCharFunc func)120     explicit                CharStrmReader(ReadNextCharFunc func)
121     : mCharStrm(NULL),
122       mReadFunc(func)
123     {
124     }
125 
126     //---- ストリーム操作
127     template <typename CharType>
128     CharType                GetChar(int offset=0) const
129     {
130         const CharType* charStrm = reinterpret_cast<const CharType*>(mCharStrm);
131         return *(charStrm + offset);
132     }
133 
134     template <typename CharType>
135     void                    StepStrm(int step=1)
136     {
137         const CharType*& charStrm = reinterpret_cast<const CharType*&>(mCharStrm);
138         charStrm += step;
139     }
140 
141     //! @brief      UTF8文字列ストリームの現在位置の文字の文字コードを読み取り、
142     //!             文字列ストリームの現在位置を1文字分進めます。
143     //!
144     //! @return     ストリームの現在位置の文字の文字コード。
145     //!
146     u16                     ReadNextCharUTF8();
147 
148     //! @brief      UTF16文字列ストリームの現在位置の文字の文字コードを読み取り、
149     //!             文字列ストリームの現在位置を1文字分進めます。
150     //!
151     //! @return     ストリームの現在位置の文字の文字コード。
152     //!
153     u16                     ReadNextCharUTF16();
154 
155     //! @brief      CP1252文字列ストリームの現在位置の文字の文字コードを読み取り、
156     //!             文字列ストリームの現在位置を1文字分進めます。
157     //!
158     //! @return     ストリームの現在位置の文字の文字コード。
159     //!
160     u16                     ReadNextCharCP1252();
161 
162     //! @brief      SJIS文字列ストリームの現在位置の文字の文字コードを読み取り、
163     //!             文字列ストリームの現在位置を1文字分進めます。
164     //!
165     //! @return     ストリームの現在位置の文字の文字コード。
166     //!
167     u16                     ReadNextCharSJIS();
168 };
169 
170 
171 
172 }   // namespace font
173 }   // namespace nn
174 
175 #endif  // NN_FONT_FONT_CHAR_STRM_READER_H_
176