Change font download URL.
[m17n/m17n-db.git] / gb180304.awk
1 # gb18030-4.awk -- awk script to make a charset map for 4-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 }
37
38 function decode_hex(str) {
39   n = 0;
40   len = length(str);
41   for (i = 1; i <= len; i++)
42     {
43       c = substr (str, i, 1);
44       if (c >= "0" && c <= "9")
45         n = n * 16 + (c - "0");
46       else
47         n = n * 16 + tohex[c];
48     }
49   return n;
50 }
51
52 function gb_to_index(gb) {
53   b0 = int(gb / 256);
54   b1 = gb % 256;
55   idx = (((b0 - 129)) * 191 + b1 - 64); 
56 #  if (b1 >= 127)
57 #    idx--;
58   return idx
59 }
60
61 function index_to_gb(idx) {
62   b3 = (idx % 10) + 48;
63   idx = int(idx / 10);
64   b2 = (idx % 126) + 129;
65   idx = int(idx / 126);
66   b1 = (idx % 10) + 48;
67   b0 = int(idx / 10) + 129;
68   return sprintf("%02X%02X%02X%02X", b0, b1, b2, b3);
69 }
70
71 /^\#/ {
72   print;
73   next;
74 }
75
76 /0x....-0x..../ {
77   gb_from = gb_to_index(decode_hex(substr($1, 3, 4)));
78   gb_to = gb_to_index(decode_hex(substr($1, 10, 4)));
79   unicode = decode_hex(substr($2, 3, 4));
80   while (gb_from <= gb_to)
81     {
82       table[unicode++] = 1;
83       gb_from++;
84     }
85   next;
86 }
87
88 {
89   gb = decode_hex(substr($1, 3, 4));
90   unicode = decode_hex(substr($2, 3, 4));
91   table[unicode] = 1;
92 }
93
94 END {
95   from_gb = -1;
96   to_gb = 0;
97   from_i = 0;
98   table[65536] = 1;
99   for (i = 128; i <= 65536; i++)
100     {
101       if (table[i] == 0)
102         {
103           if (i < 55296 || i >= 57344)
104             {
105               if (from_gb < 0)
106                 {
107                   from_gb = to_gb;
108                   from_i = i;
109                 }
110               to_gb++;
111             }
112         }
113       else if (from_gb >= 0)
114         {
115           if (from_gb + 1 == to_gb)
116             printf "0x%s\t\t0x%04X\n",
117               index_to_gb(from_gb), from_i;
118           else
119             printf "0x%s-0x%s\t0x%04X\n",
120               index_to_gb(from_gb), index_to_gb(to_gb - 1), from_i;
121           from_gb = -1;
122         }
123     }
124 }