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