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