1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     font_CharStrmReader.cpp
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: 13145 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nn/assert.h>
19 #include <nw/font/font_CharStrmReader.h>
20 
21 namespace nw {
22 namespace font {
23 namespace internal {
24 
25 #if defined(NW_FONT_UNREFERENCE)
26 const u32 SJIS_LOW_WIDTH    = 0xBC;
27 const u32 SJIS_LOW_BASE     = 0x40;
28 #endif
29 const u32 SJIS_HIGH0_ST     = 0x81;
30 #if defined(NW_FONT_UNREFERENCE)
31 const u32 SJIS_HIGH0_ED     = 0x85;
32 const u32 SJIS_HIGH1_ST     = 0x87;
33 const u32 SJIS_HIGH1_ED     = 0x88;
34 const u32 SJIS_HIGH2_ST     = 0x88;
35 #endif
36 const u32 SJIS_HIGH2_ED     = 0xA0;
37 const u32 SJIS_HIGH3_ST     = 0xE0;
38 #if defined(NW_FONT_UNREFERENCE)
39 const u32 SJIS_HIGH3_ED     = 0x100;
40 const u32 ASCII_ST          = 0x20;
41 const u32 ASCII_ED          = 0x80;
42 const u32 SJIS_HANKANA_ST   = 0xA0;
43 const u32 SJIS_HANKANA_ED   = 0xE0;
44 #endif
45 
46 /*!--------------------------------------------------------------------------*
47   @brief        対象のバイトがSJIS文字のリードバイトがどうか判定します。
48 
49   @param[in]    c   判定対象のバイトデータ。
50 
51   @return       c がリードバイトなら true そうでなければ false。
52  *---------------------------------------------------------------------------*/
53 inline
54 bool
IsSJISLeadByte(u8 c)55 IsSJISLeadByte(u8 c)
56 {
57     return ((SJIS_HIGH0_ST <= c) && (c < SJIS_HIGH2_ED))
58             || (SJIS_HIGH3_ST <= c);
59 }
60 
61 }   // namespace internal
62 
63 
64 u16
ReadNextCharUTF8()65 CharStrmReader::ReadNextCharUTF8()
66 {
67     NN_POINTER_ASSERT( this );
68     NN_POINTER_ASSERT( mCharStrm );
69     //---- UTF-8 の1バイト目の条件
70     NN_ASSERT( (GetChar<u8>() & 0xC0) != 0x80 );
71     u16 code;
72 
73     if( (GetChar<u8>() & 0x80) == 0x00 )
74     {
75         //---- 1バイト文字
76         code = GetChar<u8>();
77         StepStrm<u8>();
78     }
79     else if( (GetChar<u8>() & 0xE0) == 0xC0 )
80     {
81         //---- 2バイト文字
82         code = static_cast<u16>(
83             ((GetChar<u8>(0) & 0x1F) << 6)
84              | ((GetChar<u8>(1) & 0x3F) << 0)
85         );
86         StepStrm<u8>(2);
87     }
88     else
89     {
90         //---- 4バイト以上には対応しない
91         NN_ASSERT( (GetChar<u8>() & 0xF0) == 0xE0 );
92 
93         //---- 3バイト文字
94         code = static_cast<u16>(
95             ((GetChar<u8>(0) & 0x1F) << 12)
96              | ((GetChar<u8>(1) & 0x3F) << 6)
97              | ((GetChar<u8>(2) & 0x3F) << 0)
98         );
99         StepStrm<u8>(3);
100     }
101 
102     return code;
103 }
104 
105 u16
ReadNextCharUTF16()106 CharStrmReader::ReadNextCharUTF16()
107 {
108     NN_POINTER_ASSERT( this );
109     NN_POINTER_ASSERT( mCharStrm );
110     NN_ALIGN_ASSERT (mCharStrm, 2);
111     u16 code;
112 
113     code = GetChar<u16>();
114     StepStrm<u16>();
115 
116     return code;
117 }
118 
119 u16
ReadNextCharCP1252()120 CharStrmReader::ReadNextCharCP1252()
121 {
122     NN_POINTER_ASSERT( this );
123     NN_POINTER_ASSERT( mCharStrm );
124     u16 code;
125 
126     code = GetChar<u8>();
127     StepStrm<u8>();
128 
129     return code;
130 }
131 
132 u16
ReadNextCharSJIS()133 CharStrmReader::ReadNextCharSJIS()
134 {
135     NN_POINTER_ASSERT( this );
136     NN_POINTER_ASSERT( mCharStrm );
137     u16 code;
138 
139     if (internal::IsSJISLeadByte(GetChar<u8>()))
140     {
141         //---- 2バイト文字
142         code = static_cast<u16>((GetChar<u8>(0) << 8) | GetChar<u8>(1));
143         StepStrm<u8>(2);
144     }
145     else
146     {
147         //---- 1バイト文字
148         code = GetChar<u8>();
149         StepStrm<u8>();
150     }
151 
152     return code;
153 }
154 
155 }   // namespace font
156 }   // namespace nw
157