(Vcharacter_attribute_table): New variable.
[chise/xemacs-chise.git-] / src / text-coding.c
index 6b49083..e3fe8ff 100644 (file)
@@ -163,10 +163,11 @@ static void decode_coding_sjis (Lstream *decoding,
                                CONST unsigned char *src,
                                unsigned_char_dynarr *dst,
                                unsigned int n);
-static void encode_coding_sjis (Lstream *encoding,
-                               CONST unsigned char *src,
-                               unsigned_char_dynarr *dst,
-                               unsigned int n);
+void char_encode_shift_jis (struct encoding_stream *str, Emchar c,
+                           unsigned_char_dynarr *dst, unsigned int *flags);
+void char_finish_shift_jis (struct encoding_stream *str,
+                           unsigned_char_dynarr *dst, unsigned int *flags);
+
 static int detect_coding_big5 (struct detection_state *st,
                               CONST unsigned char *src,
                               unsigned int n);
@@ -868,10 +869,6 @@ if TYPE is 'ccl:
     CHECK_STRING (doc_string);
   CODING_SYSTEM_DOC_STRING (codesys) = doc_string;
 
-#ifdef UTF2000
-  if (ty == CODESYS_NO_CONVERSION)
-    codesys->fixed.size = 1;
-#endif
   EXTERNAL_PROPERTY_LIST_LOOP (rest, key, value, props)
     {
       if (EQ (key, Qmnemonic))
@@ -2561,6 +2558,15 @@ reset_encoding_stream (struct encoding_stream *str)
     case CODESYS_UTF8:
       str->encode_char = &char_encode_utf8;
       str->finish = &char_finish_utf8;
+      break;
+    case CODESYS_UCS4:
+      str->encode_char = &char_encode_ucs4;
+      str->finish = &char_finish_ucs4;
+      break;
+    case CODESYS_SHIFT_JIS:
+      str->encode_char = &char_encode_shift_jis;
+      str->finish = &char_finish_shift_jis;
+      break;
     default:
       break;
     }
@@ -2676,9 +2682,6 @@ mule_encode (Lstream *encoding, CONST unsigned char *src,
       encode_coding_no_conversion (encoding, src, dst, n);
       break;
 #ifdef MULE
-    case CODESYS_SHIFT_JIS:
-      encode_coding_sjis (encoding, src, dst, n);
-      break;
     case CODESYS_BIG5:
       encode_coding_big5 (encoding, src, dst, n);
       break;
@@ -2775,53 +2778,50 @@ text_encode_generic (Lstream *encoding, CONST unsigned char *src,
     {
       c = *src++;
 
-      switch (char_boundary)
+      if (char_boundary == 0)
        {
-       case 0:
-         if ( c >= 0xfc )
+         if (c >= 0xfc)
            {
              ch = c & 0x01;
              char_boundary = 5;
            }
-         else if ( c >= 0xf8 )
+         else if (c >= 0xf8)
            {
              ch = c & 0x03;
              char_boundary = 4;
            }
-         else if ( c >= 0xf0 )
+         else if (c >= 0xf0)
            {
              ch = c & 0x07;
              char_boundary = 3;
            }
-         else if ( c >= 0xe0 )
+         else if (c >= 0xe0)
            {
              ch = c & 0x0f;
              char_boundary = 2;
            }
-         else if ( c >= 0xc0 )
+         else if (c >= 0xc0)
            {
              ch = c & 0x1f;
              char_boundary = 1;
            }
          else
-           {
-             (*str->encode_char) (str, c, dst, &flags);
-             ch = 0;
-             char_boundary = 0;
-           }
-         break;
-       case 1:
+           (*str->encode_char) (str, c, dst, &flags);
+       }
+      else if (char_boundary == 1)
+       {
          (*str->encode_char) (str, (ch << 6) | (c & 0x3f), dst, &flags);
          ch =0;
          char_boundary = 0;
-         break;
-       default:
-         ch = ( ch << 6 ) | ( c & 0x3f );
+       }
+      else
+       {
+         ch = (ch << 6) | (c & 0x3f);
          char_boundary--;
        }
     }
 
-  if ( (char_boundary == 0) && flags & CODING_STATE_END)
+  if ((char_boundary == 0) && (flags & CODING_STATE_END))
     {
       (*str->finish) (str, dst, &flags);
     }
@@ -2829,8 +2829,6 @@ text_encode_generic (Lstream *encoding, CONST unsigned char *src,
   str->flags = flags;
   str->ch    = ch;
   str->iso2022.current_char_boundary = char_boundary;
-
-  /* Verbum caro factum est! */
 }
 
 \f
@@ -2948,6 +2946,11 @@ decode_coding_sjis (Lstream *decoding, CONST unsigned char *src,
              Dynarr_add (dst, c);
 #endif
            }
+#ifdef UTF2000
+         else if (c > 32)
+           DECODE_ADD_UCS_CHAR(MAKE_CHAR(Vcharset_latin_jisx0201,
+                                         c, 0), dst);
+#endif
          else
            DECODE_ADD_BINARY_CHAR (c, dst);
        }
@@ -2960,137 +2963,62 @@ decode_coding_sjis (Lstream *decoding, CONST unsigned char *src,
   str->ch    = ch;
 }
 
-/* Convert internally-formatted data to Shift-JIS. */
+/* Convert internal character representation to Shift_JIS. */
 
-static void
-encode_coding_sjis (Lstream *encoding, CONST unsigned char *src,
-                   unsigned_char_dynarr *dst, unsigned int n)
+void
+char_encode_shift_jis (struct encoding_stream *str, Emchar ch,
+                      unsigned_char_dynarr *dst, unsigned int *flags)
 {
-  unsigned char c;
-  struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
-  unsigned int flags  = str->flags;
-  unsigned int ch     = str->ch;
   eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
-#ifdef UTF2000
-  unsigned char char_boundary = str->iso2022.current_char_boundary;
-#endif
 
-  while (n--)
+  if (ch == '\n')
     {
-      c = *src++;
+      if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
+       Dynarr_add (dst, '\r');
+      if (eol_type != EOL_CR)
+       Dynarr_add (dst, ch);
+    }
+  else
+    {
+      Lisp_Object charset, value;
+      unsigned int c1, c2, s1, s2;
+      
 #ifdef UTF2000
-      switch (char_boundary)
+      if (INTP (value =
+               get_char_code_table
+               (ch, XCHARSET_ENCODING_TABLE (Vcharset_latin_jisx0201))))
        {
-       case 0:
-         if ( c >= 0xfc )
-           {
-             ch = c & 0x01;
-             char_boundary = 5;
-           }
-         else if ( c >= 0xf8 )
-           {
-             ch = c & 0x03;
-             char_boundary = 4;
-           }
-         else if ( c >= 0xf0 )
-           {
-             ch = c & 0x07;
-             char_boundary = 3;
-           }
-         else if ( c >= 0xe0 )
-           {
-             ch = c & 0x0f;
-             char_boundary = 2;
-           }
-         else if ( c >= 0xc0 )
-           {
-             ch = c & 0x1f;
-             char_boundary = 1;
-           }
-         else
-           {
-             ch = 0;
-             if (c == '\n')
-               {
-                 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
-                   Dynarr_add (dst, '\r');
-                 if (eol_type != EOL_CR)
-                   Dynarr_add (dst, c);
-               }
-             else
-               Dynarr_add (dst, c);
-             char_boundary = 0;
-           }
-         break;
-       case 1:
-         ch = ( ch << 6 ) | ( c & 0x3f );
-         {
-           Lisp_Object charset;
-           unsigned int c1, c2, s1, s2;
-           
-           BREAKUP_CHAR (ch, charset, c1, c2);
-           if (EQ(charset, Vcharset_katakana_jisx0201))
-             {
-               Dynarr_add (dst, c1 | 0x80);
-             }
-           else if (EQ(charset, Vcharset_japanese_jisx0208))
-             {
-               ENCODE_SJIS (c1 | 0x80, c2 | 0x80, s1, s2);
-               Dynarr_add (dst, s1);
-               Dynarr_add (dst, s2);
-             }
-         }
-         char_boundary = 0;
-         break;
-       default:
-         ch = ( ch << 6 ) | ( c & 0x3f );
-         char_boundary--;
+         charset = Vcharset_latin_jisx0201;
+         c1 = XINT (value);
+         c2 = 0;
        }
-#else
-      if (c == '\n')
+      else
+#endif
+       BREAKUP_CHAR (ch, charset, c1, c2);
+         
+      if (EQ(charset, Vcharset_katakana_jisx0201))
        {
-         if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
-           Dynarr_add (dst, '\r');
-         if (eol_type != EOL_CR)
-           Dynarr_add (dst, '\n');
-         ch = 0;
+         Dynarr_add (dst, c1 | 0x80);
        }
-      else if (BYTE_ASCII_P (c))
+      else if (c2 == 0)
        {
-         Dynarr_add (dst, c);
-         ch = 0;
+         Dynarr_add (dst, c1);
        }
-      else if (BUFBYTE_LEADING_BYTE_P (c))
-       ch = (c == LEADING_BYTE_KATAKANA_JISX0201 ||
-             c == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
-             c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0;
-      else if (ch)
+      else if (EQ(charset, Vcharset_japanese_jisx0208))
        {
-         if (ch == LEADING_BYTE_KATAKANA_JISX0201)
-           {
-             Dynarr_add (dst, c);
-             ch = 0;
-           }
-         else if (ch == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
-                  ch == LEADING_BYTE_JAPANESE_JISX0208)
-           ch = c;
-         else
-           {
-             unsigned char j1, j2;
-             ENCODE_SJIS (ch, c, j1, j2);
-             Dynarr_add (dst, j1);
-             Dynarr_add (dst, j2);
-             ch = 0;
-           }
+         ENCODE_SJIS (c1 | 0x80, c2 | 0x80, s1, s2);
+         Dynarr_add (dst, s1);
+         Dynarr_add (dst, s2);
        }
-#endif
+      else
+       Dynarr_add (dst, '?');
     }
+}
 
-  str->flags = flags;
-  str->ch    = ch;
-#ifdef UTF2000
-  str->iso2022.current_char_boundary = char_boundary;
-#endif
+void
+char_finish_shift_jis (struct encoding_stream *str, unsigned_char_dynarr *dst,
+                      unsigned int *flags)
+{
 }
 
 DEFUN ("decode-shift-jis-char", Fdecode_shift_jis_char, 1, 1, 0, /*
@@ -3500,10 +3428,10 @@ void
 char_encode_ucs4 (struct encoding_stream *str, Emchar ch,
                  unsigned_char_dynarr *dst, unsigned int *flags)
 {
-  Dynarr_add (dst,  ch >> 24);
-  Dynarr_add (dst, (ch >> 16) & 255);
-  Dynarr_add (dst, (ch >>  8) & 255);
-  Dynarr_add (dst,  ch        & 255);
+  Dynarr_add (dst, ch >> 24);
+  Dynarr_add (dst, ch >> 16);
+  Dynarr_add (dst, ch >>  8);
+  Dynarr_add (dst, ch      );
 }
 
 void
@@ -3621,47 +3549,56 @@ decode_coding_utf8 (Lstream *decoding, CONST unsigned char *src,
 }
 
 void
-char_encode_utf8 (struct encoding_stream *str, Emchar code,
+char_encode_utf8 (struct encoding_stream *str, Emchar ch,
                  unsigned_char_dynarr *dst, unsigned int *flags)
 {
-  if ( code <= 0x7f )
+  eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
+
+  if (ch == '\n')
     {
-      Dynarr_add (dst, code);
+      if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
+       Dynarr_add (dst, '\r');
+      if (eol_type != EOL_CR)
+       Dynarr_add (dst, ch);
     }
-  else if ( code <= 0x7ff )
+  else if (ch <= 0x7f)
     {
-      Dynarr_add (dst, (code >> 6) | 0xc0);
-      Dynarr_add (dst, (code & 0x3f) | 0x80);
+      Dynarr_add (dst, ch);
+    }
+  else if (ch <= 0x7ff)
+    {
+      Dynarr_add (dst, (ch >> 6) | 0xc0);
+      Dynarr_add (dst, (ch & 0x3f) | 0x80);
     }
-  else if ( code <= 0xffff )
+  else if (ch <= 0xffff)
     {
-      Dynarr_add (dst,  (code >> 12) | 0xe0);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 12) | 0xe0);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
-  else if ( code <= 0x1fffff )
+  else if (ch <= 0x1fffff)
     {
-      Dynarr_add (dst,  (code >> 18) | 0xf0);
-      Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 18) | 0xf0);
+      Dynarr_add (dst, ((ch >> 12) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
-  else if ( code <= 0x3ffffff )
+  else if (ch <= 0x3ffffff)
     {
-      Dynarr_add (dst,  (code >> 24) | 0xf8);
-      Dynarr_add (dst, ((code >> 18) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 24) | 0xf8);
+      Dynarr_add (dst, ((ch >> 18) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >> 12) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
   else
     {
-      Dynarr_add (dst,  (code >> 30) | 0xfc);
-      Dynarr_add (dst, ((code >> 24) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >> 18) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 30) | 0xfc);
+      Dynarr_add (dst, ((ch >> 24) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >> 18) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >> 12) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
 }
 
@@ -4851,23 +4788,44 @@ char_encode_iso2022 (struct encoding_stream *str, Emchar ch,
     {
       int reg;
 
-      BREAKUP_CHAR (ch, charset, byte1, byte2);
-      ensure_correct_direction (XCHARSET_DIRECTION (charset),
-                               codesys, dst, flags, 0);
-
       /* Now determine which register to use. */
       reg = -1;
       for (i = 0; i < 4; i++)
        {
-         if (EQ (charset, str->iso2022.charset[i]) ||
-             EQ (charset,
-                 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i)))
+         if ((CHARSETP (charset = str->iso2022.charset[i])
+              && (byte1 = charset_get_byte1 (charset, ch))) ||
+             (CHARSETP
+              (charset
+               = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i))
+              && (byte1 = charset_get_byte1 (charset, ch))))
            {
              reg = i;
+             byte2 = charset_get_byte2 (charset, ch);
              break;
            }
        }
-             
+      if (reg == -1)
+       {
+         Lisp_Object original_default_coded_charset_priority_list
+           = Vdefault_coded_charset_priority_list;
+
+         while (!EQ (Vdefault_coded_charset_priority_list, Qnil))
+           {
+             BREAKUP_CHAR (ch, charset, byte1, byte2);
+             if (XCHARSET_FINAL (charset))
+               goto found;
+             Vdefault_coded_charset_priority_list
+               = Fcdr (Fmemq (XCHARSET_NAME (charset),
+                              Vdefault_coded_charset_priority_list));
+           }
+         BREAKUP_CHAR (ch, charset, byte1, byte2);
+       found:
+         Vdefault_coded_charset_priority_list
+           = original_default_coded_charset_priority_list;
+       }
+      ensure_correct_direction (XCHARSET_DIRECTION (charset),
+                               codesys, dst, flags, 0);
+      
       if (reg == -1)
        {
          if (XCHARSET_GRAPHIC (charset) != 0)
@@ -5021,80 +4979,54 @@ encode_coding_no_conversion (Lstream *encoding, CONST unsigned char *src,
     {
       c = *src++;        
 #ifdef UTF2000
-      switch (char_boundary)
+      if (char_boundary == 0)
+       if ( c >= 0xfc )
+         {
+           ch = c & 0x01;
+           char_boundary = 5;
+         }
+       else if ( c >= 0xf8 )
+         {
+           ch = c & 0x03;
+           char_boundary = 4;
+         }
+       else if ( c >= 0xf0 )
+         {
+           ch = c & 0x07;
+           char_boundary = 3;
+         }
+       else if ( c >= 0xe0 )
+         {
+           ch = c & 0x0f;
+           char_boundary = 2;
+         }
+       else if ( c >= 0xc0 )
+         {
+           ch = c & 0x1f;
+           char_boundary = 1;
+         }
+       else
+         {
+           ch = 0;
+           if (c == '\n')
+             {
+               if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
+                 Dynarr_add (dst, '\r');
+               if (eol_type != EOL_CR)
+                 Dynarr_add (dst, c);
+             }
+           else
+             Dynarr_add (dst, c);
+           char_boundary = 0;
+         }
+      else if (char_boundary == 1)
        {
-       case 0:
-         if ( c >= 0xfc )
-           {
-             ch = c & 0x01;
-             char_boundary = 5;
-           }
-         else if ( c >= 0xf8 )
-           {
-             ch = c & 0x03;
-             char_boundary = 4;
-           }
-         else if ( c >= 0xf0 )
-           {
-             ch = c & 0x07;
-             char_boundary = 3;
-           }
-         else if ( c >= 0xe0 )
-           {
-             ch = c & 0x0f;
-             char_boundary = 2;
-           }
-         else if ( c >= 0xc0 )
-           {
-             ch = c & 0x1f;
-             char_boundary = 1;
-           }
-         else
-           {
-             ch = 0;
-
-             if (c == '\n')
-               {
-                 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
-                   Dynarr_add (dst, '\r');
-                 if (eol_type != EOL_CR)
-                   Dynarr_add (dst, c);
-               }
-             else
-               Dynarr_add (dst, c);
-             char_boundary = 0;
-           }
-         break;
-       case 1:
          ch = ( ch << 6 ) | ( c & 0x3f );
-         switch ( str->codesys->fixed.size )
-           {
-           case 1:
-             Dynarr_add (dst, ch & 0xff);
-             break;
-           case 2:
-             Dynarr_add (dst, (ch >> 8) & 0xff);
-             Dynarr_add (dst,  ch       & 0xff);
-             break;
-           case 3:
-             Dynarr_add (dst, (ch >> 16) & 0xff);
-             Dynarr_add (dst, (ch >>  8) & 0xff);
-             Dynarr_add (dst,  ch        & 0xff);
-             break;
-           case 4:
-             Dynarr_add (dst, (ch >> 24) & 0xff);
-             Dynarr_add (dst, (ch >> 16) & 0xff);
-             Dynarr_add (dst, (ch >>  8) & 0xff);
-             Dynarr_add (dst,  ch        & 0xff);
-             break;
-           default:
-             fprintf(stderr, "It seems %d bytes stream.\n",
-                     str->codesys->fixed.size);
-             abort ();
-           }
+         Dynarr_add (dst, ch & 0xff);
          char_boundary = 0;
-         break;
-       default:
+       }
+      else
+       {
          ch = ( ch << 6 ) | ( c & 0x3f );
          char_boundary--;
        }