~ubuntu-branches/ubuntu/gutsy/lv/gutsy

« back to all changes in this revision

Viewing changes to src/shiftjis.c

  • Committer: Bazaar Package Importer
  • Author(s): GOTO Masanori
  • Date: 2003-11-16 01:21:59 UTC
  • Revision ID: james.westby@ubuntu.com-20031116012159-wpu27qhoxzskmjy0
Tags: upstream-4.50
ImportĀ upstreamĀ versionĀ 4.50

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * shiftjis.c
 
3
 *
 
4
 * All rights reserved. Copyright (C) 1996 by NARITA Tomio.
 
5
 * $Id: shiftjis.c,v 1.6 2003/11/13 03:08:19 nrt Exp $
 
6
 */
 
7
/*
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 */
 
22
 
 
23
#include <import.h>
 
24
#include <decode.h>
 
25
#include <escape.h>
 
26
#include <encode.h>
 
27
#include <unimap.h>
 
28
#include <big5.h>
 
29
#include <begin.h>
 
30
#include <shiftjis.h>
 
31
 
 
32
private void msk2jis( byte *c )
 
33
{
 
34
  int c1, c2;
 
35
 
 
36
  c1 = (int)c[ 0 ];
 
37
  c2 = (int)c[ 1 ];
 
38
 
 
39
  if( 0x00e0 <= c1 )
 
40
    c1 = ( c1 << 1 ) - 0x0160;                  /* 63-94 ku */
 
41
  else
 
42
    c1 = ( c1 << 1 ) - 0x00e0;                  /* 01-62 ku */
 
43
 
 
44
  if( 0x009f <= c2 )
 
45
    c2 -= 0x007e;                               /* even ku */
 
46
  else {
 
47
    c1--;                                       /* odd ku */
 
48
    if( c2 >= (int)DEL )
 
49
      c2 -= 0x0020;
 
50
    else
 
51
      c2 -= 0x001f;
 
52
  }
 
53
 
 
54
  c[ 0 ] = (byte)c1;
 
55
  c[ 1 ] = (byte)c2;
 
56
}
 
57
 
 
58
public void DecodeShiftJis( state_t *state, byte codingSystem )
 
59
{
 
60
  byte charset, ch;
 
61
  byte c[ ICHAR_WIDTH ];
 
62
 
 
63
  for( ; ; ){
 
64
    GetChar( ch );
 
65
    if( ch < SP ){
 
66
      if( ESC == ch ){
 
67
        if( FALSE == DecodeEscape( state ) )
 
68
          break;
 
69
      } else if( HT == ch )
 
70
        DecodeAddTab( state->attr );
 
71
      else if( SO == ch )       /* LS1 for 8bit */
 
72
        state->gset[ GL ] = G1;
 
73
      else if( SI == ch )       /* LS0 for 8bit */
 
74
        state->gset[ GL ] = G0;
 
75
      else if( BS == ch )
 
76
        DecodeAddBs();
 
77
      else
 
78
        DecodeAddControl( ch );
 
79
    } else {
 
80
      if( NULL != state->sset ){
 
81
        if( FALSE == DecodeAddShifted( state, ch ) )
 
82
          break;
 
83
        else
 
84
          continue;
 
85
      } else if( 0x80 & ch ){
 
86
        if( IsShiftJisByte1( ch ) ){
 
87
          charset = X0208;
 
88
          c[ 0 ] = ch;
 
89
          GetChar( ch );
 
90
          c[ 1 ] = ch;
 
91
          msk2jis( c );
 
92
          if( !IsGraphicChar94( c[ 0 ] ) || !IsGraphicChar94( c[ 1 ] ) ){
 
93
            DecodeAddControl( ch );
 
94
            continue;
 
95
          }
 
96
        } else if( IsKatakana( 0x7f & ch ) ){
 
97
          charset = X0201KANA;
 
98
          c[ 0 ] = 0x7f & ch;
 
99
        } else {
 
100
          DecodeAddControl( ch );
 
101
          continue;
 
102
        }
 
103
      } else {
 
104
        /*
 
105
         * iso-2022
 
106
         */
 
107
        charset = CSET( G0 );
 
108
        if( !IsGraphicChar( charset, ch ) ){
 
109
          if( SP == ch ){
 
110
            DecodeAddSpace( state->attr );
 
111
          } else {
 
112
            DecodeAddControl( ch );
 
113
          }
 
114
          continue;
 
115
        } else if( X0201KANA == charset && !IsKatakana( ch ) ){
 
116
          DecodeAddControl( ch );
 
117
          continue;
 
118
        }
 
119
        c[ 0 ] = ch;
 
120
        if( TRUE == iTable[ (int)charset ].multi ){
 
121
          GetChar( ch );
 
122
          ch &= 0x7f;
 
123
          if( !IsGraphicChar( charset, ch ) )
 
124
            continue;
 
125
          c[ 1 ] = ch;
 
126
        }
 
127
      }
 
128
      DecodeAddChar( charset, c, state->attr );
 
129
    }
 
130
  }
 
131
}
 
132
 
 
133
private void jis2msk( byte *c )
 
134
{
 
135
  int c1, c2;
 
136
 
 
137
  c1 = (int)c[ 0 ];
 
138
  c2 = (int)c[ 1 ];
 
139
 
 
140
  if( 0 == ( c1 & 0x0001 ) )
 
141
    c2 += 0x007e;                               /* even ku */
 
142
  else {
 
143
    if( c2 >= 0x0060 )                          /* odd ku */
 
144
      c2 += 0x0020;
 
145
    else
 
146
      c2 += 0x001f;
 
147
  }
 
148
 
 
149
  if( 0x005f <= c1 )
 
150
    c1 = ( ( c1 - 0x005f ) >> 1 ) + 0x00e0;     /* 63-94 ku */
 
151
  else
 
152
    c1 = ( ( c1 - 0x0021 ) >> 1 ) + 0x0081;     /* 01-62 ku */
 
153
 
 
154
  c[ 0 ] = (byte)c1;
 
155
  c[ 1 ] = (byte)c2;
 
156
}
 
157
 
 
158
public void EncodeShiftJis( i_str_t *istr, int head, int tail,
 
159
                           byte codingSystem, boolean_t binary )
 
160
{
 
161
  int idx, attr;
 
162
  ic_t ic;
 
163
  byte cset, sj[ 2 ];
 
164
 
 
165
  for( idx = head ; idx < tail ; idx++ ){
 
166
    cset = istr[ idx ].charset;
 
167
    ic = istr[ idx ].c;
 
168
    attr = (int)istr[ idx ].attr << 8;
 
169
#ifndef MSDOS /* IF NOT DEFINED */
 
170
    if( UNICODE == cset )
 
171
      ic = UNItoJIS( ic, &cset );
 
172
#endif /* MSDOS */
 
173
    if( cset < PSEUDO ){
 
174
      if( ASCII == cset ){
 
175
        EncodeAddChar( attr, ic );
 
176
      } else if( X0208 == cset || C6226 == cset ){
 
177
        sj[ 0 ] = MakeByte1( ic );
 
178
        sj[ 1 ] = MakeByte2( ic );
 
179
        jis2msk( sj );
 
180
        EncodeAddChar( attr, sj[ 0 ] );
 
181
        EncodeAddChar( attr, sj[ 1 ] );
 
182
      } else if( X0201KANA == cset ){
 
183
        EncodeAddChar( attr, 0x80 | ic );
 
184
      } else {
 
185
        if( FALSE == EncodeAddInvalid( attr, ic, cset ) )
 
186
          break;
 
187
      }
 
188
    } else if( FALSE == EncodeAddPseudo( attr, ic, cset, binary ) ){
 
189
      break;
 
190
    }
 
191
  }
 
192
}