isoir165.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 1999-2001, 2005 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  18. * Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21. * ISO-IR-165
  22. */
  23. /*
  24. * ISO-IR-165 is an extension of GB 2312, consisting of:
  25. * 1. GB 6345.1-86 corrections:
  26. * Two corrections to GB 2312, at 0x2367 and 0x6F71.
  27. * 2. GB 6345.1-86 additions:
  28. * - 6 new full-width pinyin characters in row 0x28.
  29. * - ISO646-CN in row 0x2A.
  30. * - 32 half-width pinyin characters in row 0x2B.
  31. * 3. GB 8565.2-88 additions:
  32. * - 50 characters in row 0x2D.
  33. * - 92 characters in row 0x2E.
  34. * - 93 characters in row 0x2F.
  35. * - 470 characters in rows 0x7A-0x7E.
  36. * 4. ISO-IR-165 additions:
  37. * - 22 characters in row 0x26.
  38. * - 94 characters in row 0x2C.
  39. * - 44 new characters in row 0x2D.
  40. * - 1 new character in row 0x2F.
  41. *
  42. * The conversion table was created from the following sources:
  43. * Ad 1. The 0x2367 correction is already integrated in the unicode.org
  44. * GB2312.TXT table. The 0x6F71 mapping is the same in the unicode.org
  45. * GB2312.TXT and UNIHAN.TXT table and in Koichi Yasuoka's Uni2GB table,
  46. * so we assume it's correct.
  47. * The unicode.org UNIHAN.TXT table about GB 8565 is not usable: it has
  48. * extraneous code points at rows 0x28, 0x2C, 0x2D. Note also that it does
  49. * not list the 69 non-hanzi in row 0x2F. Moreover, it has the characters
  50. * 0x2F7A-0x2F7D shifted down by one to 0x2F79-0x2F7C.
  51. * Therefore we take the GB8565 and ISO-IR-165 data from Koichi Yasuoka's
  52. * Uni2GB table.
  53. * Ad 1. Yasuoka maps 0x2367 to U+0261 (small script g) and 0x2840 to U+FF47
  54. * (full-width small normal g). While coherent with ISO-IR's 165.pdf,
  55. * this disagrees with Ken Lunde's book: He says that ISO-IR-165
  56. * includes the GB6345 correction, i.e. maps 0x2367 to U+FF47 or U+0067
  57. * and _not_ to U+0261 (small script g).
  58. * To overcome the confusion, we just map both 0x2367 and 0x2840 to
  59. * U+FF47.
  60. * Ad 2. Row 0x28: Add a mapping from 0x283F to U+01F9.
  61. * Row 0x2A: Mapping is well-known, also present in Koichi Yasuoka's
  62. * table.
  63. * Row 0x2B: Typed in by hand from appendix E in Ken Lunde's book.
  64. * When converting from Unicode to ISO-IR-165, prefer the half-width
  65. * range 0x2B{21..40} to the full-width range 0x28{21..40}.
  66. * Ad 3. Rows 0x2D, 0x2E: Both Koichi Yasuoka's Uni2GB table and the UNIHAN.TXT
  67. * data for GB 8565 agree here.
  68. * Row 0x2F: Taken from Koichi Yasuoka's Uni2GB table.
  69. * Rows 0x7A-0x7E: Koichi Yasuoka's Uni2GB table and the UNIHAN.TXT
  70. * data for GB 8565 agree here mostly. Differences:
  71. * 0x7C38 -> U+6F26 or U+527A ? We choose U+6F26.
  72. * 0x7C5A -> U+7A40 or U+6996 ? We choose U+6996.
  73. * Ad 4. Row 0x26: Mapping unknown.
  74. * Rows 0x2C, 0x2D: Both Koichi Yasuoka's Uni2GB table and the UNIHAN.TXT
  75. * data for GB 8565 (!) agree here.
  76. * Row 0x2F: Taken from Koichi Yasuoka's Uni2GB table.
  77. */
  78. #include "isoir165ext.h"
  79. static int
  80. isoir165_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  81. {
  82. int ret;
  83. /* Map full-width pinyin (row 0x28) like half-width pinyin (row 0x2B). */
  84. if (s[0] == 0x28) {
  85. if (n >= 2) {
  86. unsigned char c2 = s[1];
  87. if (c2 >= 0x21 && c2 <= 0x40) {
  88. unsigned char buf[2];
  89. buf[0] = 0x2b;
  90. buf[1] = c2;
  91. ret = isoir165ext_mbtowc(conv,pwc,buf,2);
  92. if (ret != RET_ILSEQ)
  93. return ret;
  94. }
  95. }
  96. }
  97. /* Try the GB2312 -> Unicode table. */
  98. ret = gb2312_mbtowc(conv,pwc,s,n);
  99. if (ret != RET_ILSEQ)
  100. return ret;
  101. /* Row 0x2A is GB_1988-80. */
  102. if (s[0] == 0x2a) {
  103. if (n >= 2) {
  104. unsigned char c2 = s[1];
  105. if (c2 >= 0x21 && c2 < 0x7f) {
  106. int ret = iso646_cn_mbtowc(conv,pwc,s+1,1);
  107. if (ret != 1) abort();
  108. return 2;
  109. }
  110. return RET_ILSEQ;
  111. }
  112. return RET_TOOFEW(0);
  113. }
  114. /* Try the ISO-IR-165 extensions -> Unicode table. */
  115. ret = isoir165ext_mbtowc(conv,pwc,s,n);
  116. return ret;
  117. }
  118. static int
  119. isoir165_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  120. {
  121. unsigned char buf[2];
  122. int ret;
  123. /* Try the Unicode -> GB2312 table. */
  124. ret = gb2312_wctomb(conv,buf,wc,2);
  125. if (ret != RET_ILUNI) {
  126. if (ret != 2) abort();
  127. if (!(buf[0] == 0x28 && buf[1] >= 0x21 && buf[1] <= 0x40)) {
  128. if (n >= 2) {
  129. r[0] = buf[0];
  130. r[1] = buf[1];
  131. return 2;
  132. }
  133. return RET_TOOSMALL;
  134. }
  135. }
  136. /* Row 0x2A is GB_1988-80. */
  137. ret = iso646_cn_wctomb(conv,buf,wc,1);
  138. if (ret != RET_ILUNI) {
  139. if (ret != 1) abort();
  140. if (buf[0] >= 0x21 && buf[0] < 0x7f) {
  141. if (n >= 2) {
  142. r[0] = 0x2a;
  143. r[1] = buf[0];
  144. return 2;
  145. }
  146. return RET_TOOSMALL;
  147. }
  148. }
  149. /* Try the Unicode -> ISO-IR-165 extensions table. */
  150. ret = isoir165ext_wctomb(conv,r,wc,n);
  151. return ret;
  152. }