Fix for the case of failed LAM-ALEF ligature. Handle positioning of
[m17n/m17n-db.git] / gb180302.awk
1 # gb18030-2.awk -- awk script to make a charset map for 2-byte part of GB18030
2 # Copyright (C) 2003
3 #   National Institute of Advanced Industrial Science and Technology (AIST)
4 #   Registration Number H15PRO112
5 #
6 # This file is part of the m17n database, a sub-part of the m17n library.
7 #
8 # The m17n library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; either version 2, or (at
11 # your option) any later version.
12 #
13 # The m17n library is distributed in the hope that it will be
14 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 # of 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 the m17n database; see the file COPYING.  If not, write
20 # to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 # Boston, MA 02110-1301, USA.
22
23 BEGIN {
24   tohex["A"] = 10;
25   tohex["B"] = 11;
26   tohex["C"] = 12;
27   tohex["D"] = 13;
28   tohex["E"] = 14;
29   tohex["F"] = 15;
30   tohex["a"] = 10;
31   tohex["b"] = 11;
32   tohex["c"] = 12;
33   tohex["d"] = 13;
34   tohex["e"] = 14;
35   tohex["f"] = 15;
36   from_gb = 0;
37   to_gb = -1;
38   to_unicode = 0;
39   from_unicode = 0;
40 }
41
42 function decode_hex(str) {
43   n = 0;
44   len = length(str);
45   for (i = 1; i <= len; i++)
46     {
47       c = substr (str, i, 1);
48       if (c >= "0" && c <= "9")
49         n = n * 16 + (c - "0");
50       else
51         n = n * 16 + tohex[c];
52     }
53   return n;
54 }
55
56 function gb_to_index(gb) {
57   b0 = int(gb / 256);
58   b1 = gb % 256;
59   idx = (((b0 - 129)) * 191 + b1 - 64); 
60 #  if (b1 >= 128)
61 #    idx--;
62   return idx
63 }
64
65 function index_to_gb(idx) {
66   b0 = int(idx / 191) + 129;
67   b1 = (idx % 191) + 64;
68 #  if (b1 >= 127)
69 #    b1++;
70   return (b0 * 256 + b1);
71 }
72
73 /^\#/ {
74   print;
75   next;
76 }
77
78 {
79   gb = gb_to_index(decode_hex(substr($1, 3, 4)));
80   unicode = decode_hex(substr($2, 3, 4));
81   if ((gb == to_gb + 1) && (unicode == to_unicode + 1))
82     {
83       to_gb++;
84       to_unicode++;
85     }
86   else
87     {
88       if (from_gb == to_gb)
89         printf "0x%04X 0x%04X\n", index_to_gb(from_gb), from_unicode;
90       else if (from_gb < to_gb)
91         printf "0x%04X-0x%04X 0x%04X\n",
92           index_to_gb(from_gb), index_to_gb(to_gb), from_unicode;
93       from_gb = to_gb = gb;
94       from_unicode = to_unicode = unicode;
95     }
96 }
97
98 END {
99   if (from_gb <= to_gb)
100     printf "0x%04X-0x%04X 0x%04X\n",
101       index_to_gb(from_gb), index_to_gb(to_gb), from_unicode;
102 }