natOutput_SJIS.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 1999 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. #include <config.h>
  7. #include <gcj/cni.h>
  8. #include <gnu/gcj/convert/Output_SJIS.h>
  9. extern unsigned short Unicode_to_JIS[];
  10. extern int trie_lookup (unsigned short *trie, unsigned short key);
  11. static jint
  12. convert_TO_SJIS (gnu::gcj::convert::Output_SJIS *encoder,
  13. jchar *ptr, jint inlength)
  14. {
  15. int orig_inlength = inlength;
  16. jint outbuf_length = encoder->buf->length;
  17. for (;;)
  18. {
  19. if (encoder->count >= outbuf_length)
  20. break;
  21. if (encoder->pending >= 0)
  22. {
  23. elements(encoder->buf)[encoder->count++] = encoder->pending;
  24. encoder->pending = -1;
  25. continue;
  26. }
  27. if (inlength == 0)
  28. break;
  29. jchar ch = *ptr++;
  30. inlength--;
  31. unsigned short val = trie_lookup(Unicode_to_JIS, ch);
  32. if (val < 0xFF)
  33. {
  34. if (val == 0xffff)
  35. val = '?';
  36. }
  37. else
  38. {
  39. int b1 = val >> 8;
  40. int b2 = val & 0xff;
  41. // From Lunde: "CJKV Informatio Processing", O'Reilly, 1999:
  42. int rowOffset = b1 < 95 ? 112 : 176;
  43. int cellOffset = (b1 & 1) != 0 ? (b2 > 95 ? 32 : 31) : 126;
  44. b1 = ((b1 + 1) >> 1) + rowOffset;
  45. b2 += cellOffset;
  46. val = b1;
  47. encoder->pending = b2;
  48. }
  49. elements(encoder->buf)[encoder->count++] = val;
  50. }
  51. return orig_inlength - inlength;
  52. }
  53. jint
  54. gnu::gcj::convert::Output_SJIS::write (jcharArray inbuffer,
  55. jint inpos, jint inlength)
  56. {
  57. return convert_TO_SJIS(this, &elements(inbuffer)[inpos], inlength);
  58. }
  59. jint
  60. gnu::gcj::convert::Output_SJIS::write (jstring str, jint inpos,
  61. jint inlength, jcharArray)
  62. {
  63. return convert_TO_SJIS(this, _Jv_GetStringChars(str)+inpos, inlength);
  64. }