3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 # The following files must be available in a ./input subdir:
22 # <http://www.unicode.org/Public/UNIDATA/Unihan.txt>:
23 # "Unicode version: 3.1.1 Table version: 1.1 Date: 28 June 2001"
24 # contains descriptions for:
25 # U+3400..4DFF CJK Unified Ideographs Extension A
26 # U+4E00..9FFF CJK Unified Ideographs
27 # U+F900..FAFF CJK Compatibility Ideographs
28 # U+20000..2F7FF CJK Unified Ideographs Extension B
29 # U+2F800..2FFFF CJK Compatibility Ideographs Supplement
31 # <http://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/CNS11643.TXT>:
32 # "Unicode version: 1.1 Table version: 0.0d1 Date: 21 October 1994"
33 # contains mappings for CNS 11643-1986
35 # <http://kanji.zinbun.kyoto-u.ac.jp/~yasuoka/ftp/CJKtable/Uni2CNS.Z>:
36 # "Unicode version: 1.1 Table version: 0.49 Date: 26 March 1998"
37 # contains mappings for CNS 11643-1992 that are incompatible with
45 return $utf32 >= 0 && $utf32 <= 0x10FFFF
46 && !($utf32 >= 0xD800 && $utf32 <= 0xDFFF)
47 && !($utf32 >= 0xFDD0 && $utf32 <= 0xFDEF)
48 && ($utf32 & 0xFFFF) < 0xFFFE;
54 return sprintf("U+%04X", $utf32);
57 sub isValidCns116431992
62 return $plane >= 1 && $plane <= 16
63 && $row >= 1 && $row <= 94
64 && $column >= 1 && $column <= 94;
72 return sprintf("%d-%02d/%02d", $plane, $row, $column);
79 return sprintf("%d/%d bytes (%.1f%%)",
82 $used * 100 / $space);
87 my $column_width = $_[0];
88 my $columns_per_line = $_[1];
91 for ($i = int($end / $columns_per_line) * $columns_per_line;
95 for ($j = 0; $j < $column_width; ++$j)
97 $output = $output . " ";
103 $count_Unihan_txt = 0;
104 $count_CNS11643_TXT = 0;
109 $filename = "Unihan.txt";
110 open IN
, ("input/" . $filename) or die "Cannot read " . $filename;
113 if (/^U\+([0-9A-F]+)\tkCNS1992\t([0-9A-F])-([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])$/)
115 $utf32 = oct("0x" . $1);
116 $cns_plane = oct("0x" . $2);
117 $cns_row = oct("0x" . $3) - 0x20;
118 $cns_column = oct("0x" . $4) - 0x20;
120 or die "Bad UTF32 char U+" . printUtf32
($utf32);
121 isValidCns116431992
($cns_plane, $cns_row, $cns_column)
122 or die "Bad CNS11643-1992 char "
123 . printCns116431992
($cns_plane,
126 if (!defined($cns_map[$cns_plane][$cns_row][$cns_column]))
128 $cns_map[$cns_plane][$cns_row][$cns_column] = $utf32;
129 $cns_plane_used[$cns_plane] = 1;
134 ($cns_map[$cns_plane][$cns_row][$cns_column] == $utf32)
136 . printCns116431992
($cns_plane,
140 . printUtf32
($cns_map[$cns_plane]
144 . printUtf32
($utf32);
147 elsif (/^U\+([0-9A-F]+)\tkIRG_TSource\t([0-9A-F])-([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])$/)
149 $utf32 = oct("0x" . $1);
150 $cns_plane = oct("0x" . $2);
151 $cns_row = oct("0x" . $3) - 0x20;
152 $cns_column = oct("0x" . $4) - 0x20;
154 or die "Bad UTF32 char U+" . printUtf32
($utf32);
155 isValidCns116431992
($cns_plane, $cns_row, $cns_column)
156 or die "Bad CNS11643-1992 char "
157 . printCns116431992
($cns_plane,
160 if (!defined($cns_map[$cns_plane][$cns_row][$cns_column]))
162 $cns_map[$cns_plane][$cns_row][$cns_column] = $utf32;
163 $cns_plane_used[$cns_plane] = 1;
168 ($cns_map[$cns_plane][$cns_row][$cns_column] == $utf32)
169 or print "WARNING! Mapping ",
170 printCns116431992
($cns_plane,
174 printUtf32
($cns_map[$cns_plane]
182 elsif (/^U\+([0-9A-F]+)\tkCNS1992\t.*$/)
192 $filename = "CNS11643.TXT";
193 open IN
, ("input/" . $filename) or die "Cannot read " . $filename;
196 if (/0x([0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])\t0x([0-9A-F]+)\t\#.*$/)
198 $utf32 = oct("0x" . $4);
199 $cns_plane = oct("0x" . $1);
200 $cns_row = oct("0x" . $2) - 0x20;
201 $cns_column = oct("0x" . $3) - 0x20;
203 or die "Bad UTF32 char U+" . printUtf32
($utf32);
204 isValidCns116431992
($cns_plane, $cns_row, $cns_column)
205 or die "Bad CNS11643-1992 char "
206 . printCns116431992
($cns_plane,
211 if (!defined($cns_map[$cns_plane][$cns_row][$cns_column]))
213 $cns_map[$cns_plane][$cns_row][$cns_column] = $utf32;
214 $cns_plane_used[$cns_plane] = 1;
215 ++$count_CNS11643_TXT;
219 ($cns_map[$cns_plane][$cns_row][$cns_column] == $utf32)
221 . printCns116431992
($cns_plane,
225 . printUtf32
($cns_map[$cns_plane]
229 . printUtf32
($utf32);
239 $filename = "Uni2CNS";
240 open IN
, ("input/" . $filename) or die "Cannot read " . $filename;
243 if (/([0-9A-F]+)\t([0-9A-F])-([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])\t.*$/)
245 $utf32 = oct("0x" . $1);
246 $cns_plane = oct("0x" . $2);
247 $cns_row = oct("0x" . $3) - 0x20;
248 $cns_column = oct("0x" . $4) - 0x20;
250 or die "Bad UTF32 char U+" . printUtf32
($utf32);
251 isValidCns116431992
($cns_plane, $cns_row, $cns_column)
252 or die "Bad CNS11643-1992 char "
253 . printCns116431992
($cns_plane,
256 if (!defined($cns_map[$cns_plane][$cns_row][$cns_column]))
258 $cns_map[$cns_plane][$cns_row][$cns_column] = $utf32;
259 $cns_plane_used[$cns_plane] = 1;
264 # ($cns_map[$cns_plane][$cns_row][$cns_column] == $utf32)
266 # . printCns116431992($cns_plane,
270 # . printUtf32($cns_map[$cns_plane]
274 # . printUtf32($utf32);
278 print printCns116431992
($cns_plane, $cns_row, $cns_column),
286 for ($cns_plane = 1; $cns_plane <= 16; ++$cns_plane)
288 if (defined($cns_plane_used[$cns_plane]))
290 for ($cns_row = 1; $cns_row <= 94; ++$cns_row)
292 for ($cns_column = 1; $cns_column <= 94; ++$cns_column)
294 if (defined($cns_map[$cns_plane][$cns_row][$cns_column]))
296 $utf32 = $cns_map[$cns_plane][$cns_row][$cns_column];
297 $uni_plane = $utf32 >> 16;
298 $uni_page = ($utf32 >> 8) & 0xFF;
299 $uni_index = $utf32 & 0xFF;
300 if (!defined($uni_plane_used[$uni_plane])
301 || !defined($uni_page_used[$uni_plane][$uni_page])
302 || !defined($uni_map[$uni_plane]
306 $uni_map[$uni_plane][$uni_page][$uni_index]
310 $uni_plane_used[$uni_plane] = 1;
311 $uni_page_used[$uni_plane][$uni_page] = 1;
315 $cns1 = $uni_map[$uni_plane][$uni_page][$uni_index];
316 $cns1_plane = $cns1 >> 16;
317 $cns1_row = ($cns1 >> 8) & 0xFF;
318 $cns1_column = $cns1 & 0xFF;
320 # Do not map from Unicode to Fictitious Character Set
321 # Extensions (Lunde, p. 131), if possible:
323 && ($cns_row == 66 && $cns_column > 38
329 printCns116431992
($cns_plane,
332 " ignored, favouring ",
333 printCns116431992
($cns1_plane,
338 elsif ($cns1_plane == 3
339 && ($cns1_row == 66 && $cns1_column > 38
342 $uni_map[$uni_plane][$uni_page][$uni_index]
349 printCns116431992
($cns1_plane,
352 " ignored, favouring ",
353 printCns116431992
($cns_plane,
360 print "WARNING! Mapping ",
363 printCns116431992
($cns1_plane,
367 printCns116431992
($cns_plane,
378 if (defined($uni_plane_used[0]) && defined($uni_page_used[0][0]))
380 for ($utf32 = 0; $utf32 <= 0x7F; ++$utf32)
382 if (defined($uni_map[0][0][$uni_index]))
384 $cns = $uni_map[0][0][$utf32];
388 . printCns116431992
($cns >> 16,
395 $filename = lc($id) . ".tab";
396 open OUT
, ("> " . $filename) or die "Cannot write " . $filename;
399 $filename = lc($id). ".pl";
400 open IN
, $filename or die "Cannot read ". $filename;
411 print OUT
"/", $1, "\n";
416 print OUT
" ", substr($1, 0, length($1) - 1), "/\n";
421 print OUT
" *", $1, "\n";
425 print OUT
" *", $1, "\n";
436 "#include \"sal/types.h\"\n",
439 print OUT
"static sal_uInt16 const aImpl", $id, "ToUnicodeData[] = {\n";
441 for ($cns_plane = 1; $cns_plane <= 16; ++$cns_plane)
443 if (defined($cns_plane_used[$cns_plane]))
447 for ($cns_row = 1; $cns_row <= 94; ++$cns_row)
450 for ($cns_column = 1; $cns_column <= 94; ++$cns_column)
452 if (defined($cns_map[$cns_plane][$cns_row][$cns_column]))
454 if ($cns_row_first == -1)
456 $cns_row_first = $cns_column;
458 $cns_row_last = $cns_column;
461 if ($cns_row_first != -1)
463 $cns_data_offsets[$cns_plane][$cns_row] = $cns_data_index;
465 print OUT
" /* plane ", $cns_plane, ", row ", $cns_row,
468 $cns_row_surrogates_first = -1;
470 $cns_row_surrogates = 0;
472 print OUT
" ", $cns_row_first, " | (", $cns_row_last,
473 " << 8), /* first, last */\n";
476 print OUT
" ", printSpaces
(7, 10, $cns_row_first);
478 for ($cns_column = $cns_row_first;
479 $cns_column <= $cns_row_last;
487 if (defined($cns_map[$cns_plane][$cns_row][$cns_column]))
489 $utf32 = $cns_map[$cns_plane][$cns_row][$cns_column];
491 if ($utf32 <= 0xFFFF)
493 printf OUT
"0x%04X,", $utf32;
497 ++$cns_row_surrogates;
498 printf OUT
"0x%04X,",
499 (0xD800 | (($utf32 - 0x10000) >> 10));
500 if ($cns_row_surrogates_first == -1)
502 $cns_row_surrogates_first = $cns_column;
504 $cns_row_surrogates_last = $cns_column;
509 printf OUT
"0xffff,";
512 if ($cns_column % 10 == 9)
523 if ($cns_row_surrogates_first != -1)
525 print OUT
" ", $cns_row_surrogates_first,
526 ", /* first low-surrogate */\n";
530 printSpaces
(7, 10, $cns_row_surrogates_first);
532 for ($cns_column = $cns_row_surrogates_first;
533 $cns_column <= $cns_row_surrogates_last;
542 if (defined($cns_map[$cns_plane]
547 = $cns_map[$cns_plane][$cns_row][$cns_column];
549 if ($utf32 <= 0xFFFF)
555 printf OUT
"0x%04X,",
557 | (($utf32 - 0x10000) & 0x3FF));
560 if ($cns_column % 10 == 9)
572 $cns_chars += $cns_row_chars;
573 $cns_data_space[$cns_plane][$cns_row]
575 - $cns_data_offsets[$cns_plane][$cns_row]) * 2;
576 $cns_data_used[$cns_plane][$cns_row]
577 = (1 + $cns_row_chars
578 + ($cns_row_surrogates == 0 ?
579 0 : 1 + $cns_row_surrogates)) * 2;
583 print OUT
" /* plane ", $cns_plane, ", row ", $cns_row,
585 $cns_data_offsets[$cns_plane][$cns_row] = -1;
599 print OUT
"static sal_Int32 const aImpl", $id, "ToUnicodeRowOffsets[] = {\n";
600 for ($cns_plane = 1; $cns_plane <= 16; ++$cns_plane)
602 if (defined ($cns_plane_used[$cns_plane]))
604 $cns_rowoffsets_used[$cns_plane] = 0;
605 for ($cns_row = 1; $cns_row <= 94; ++$cns_row)
607 if ($cns_data_offsets[$cns_plane][$cns_row] == -1)
609 print OUT
" -1, /* plane ",
618 $cns_data_offsets[$cns_plane][$cns_row],
624 printStats
($cns_data_used[$cns_plane][$cns_row],
625 $cns_data_space[$cns_plane][$cns_row]),
627 $cns_rowoffsets_used[$cns_plane] += 4;
633 print OUT
" /* plane ", $cns_plane, ": --- */\n";
638 print OUT
"static sal_Int32 const aImpl",
640 "ToUnicodePlaneOffsets[] = {\n";
642 for ($cns_plane = 1; $cns_plane <= 16; ++$cns_plane)
644 if (defined ($cns_plane_used[$cns_plane]))
651 printStats
($cns_rowoffsets_used[$cns_plane], 94 * 4),
656 print OUT
" -1, /* plane ", $cns_plane, " */\n";
661 print OUT
"static sal_uInt8 const aImplUnicodeTo", $id, "Data[] = {\n";
663 for ($uni_plane = 0; $uni_plane <= 16; ++$uni_plane)
665 if (defined($uni_plane_used[$uni_plane]))
667 for ($uni_page = 0; $uni_page <= 255; ++$uni_page)
669 if (defined($uni_page_used[$uni_plane][$uni_page]))
671 $uni_data_offsets[$uni_plane][$uni_page] = $uni_data_index;
672 print OUT
" /* plane ", $uni_plane, ", page ", $uni_page,
675 $uni_page_first = -1;
676 for ($uni_index = 0; $uni_index <= 255; ++$uni_index)
678 if (defined($uni_map[$uni_plane][$uni_page][$uni_index]))
680 if ($uni_page_first == -1)
682 $uni_page_first = $uni_index;
684 $uni_page_last = $uni_index;
688 $uni_data_used[$uni_plane][$uni_page] = 0;
690 print OUT
" ", $uni_page_first, ", ", $uni_page_last,
691 ", /* first, last */\n";
692 $uni_data_index += 2;
693 $uni_data_used[$uni_plane][$uni_page] += 2;
695 print OUT
" ", printSpaces
(9, 8, $uni_page_first);
697 for ($uni_index = $uni_page_first;
698 $uni_index <= $uni_page_last;
706 if (defined($uni_map[$uni_plane][$uni_page][$uni_index]))
708 $cns = $uni_map[$uni_plane][$uni_page][$uni_index];
709 printf OUT
"%2d,%2d,%2d,",
713 $uni_data_used[$uni_plane][$uni_page] += 3;
717 print OUT
" 0, 0, 0,";
719 $uni_data_index += 3;
720 if ($uni_index % 8 == 7)
731 $uni_data_space[$uni_plane][$uni_page]
733 - $uni_data_offsets[$uni_plane][$uni_page];
737 $uni_data_offsets[$uni_plane][$uni_page] = -1;
738 print OUT
" /* plane ", $uni_plane, ", page ", $uni_page,
745 print OUT
" /* plane ", $uni_plane, ": --- */\n";
750 print OUT
"static sal_Int32 const aImplUnicodeTo", $id, "PageOffsets[] = {\n";
751 for ($uni_plane = 0; $uni_plane <= 16; ++$uni_plane)
753 if (defined($uni_plane_used[$uni_plane]))
755 $uni_pageoffsets_used[$uni_plane] = 0;
756 $uni_data_used_sum[$uni_plane] = 0;
757 $uni_data_space_sum[$uni_plane] = 0;
758 for ($uni_page = 0; $uni_page <= 255; ++$uni_page)
760 $offset = $uni_data_offsets[$uni_plane][$uni_page];
763 print OUT
" -1, /* plane ",
778 printStats
($uni_data_used[$uni_plane][$uni_page],
779 $uni_data_space[$uni_plane][$uni_page]),
781 $uni_pageoffsets_used[$uni_plane] += 4;
782 $uni_data_used_sum[$uni_plane]
783 += $uni_data_used[$uni_plane][$uni_page];
784 $uni_data_space_sum[$uni_plane]
785 += $uni_data_space[$uni_plane][$uni_page];
791 print OUT
" /* plane ", $uni_plane, ": --- */\n";
796 print OUT
"static sal_Int32 const aImplUnicodeTo",
798 "PlaneOffsets[] = {\n";
799 $uni_page_offset = 0;
800 $uni_planeoffsets_used = 0;
801 $uni_pageoffsets_used_sum = 0;
802 $uni_pageoffsets_space_sum = 0;
803 $uni_data_used_sum2 = 0;
804 $uni_data_space_sum2 = 0;
805 for ($uni_plane = 0; $uni_plane <= 16; ++$uni_plane)
807 if (defined ($uni_plane_used[$uni_plane]))
814 printStats
($uni_pageoffsets_used[$uni_plane], 256 * 4),
816 printStats
($uni_data_used_sum[$uni_plane],
817 $uni_data_space_sum[$uni_plane]),
819 $uni_planeoffsets_used += 4;
820 $uni_pageoffsets_used_sum += $uni_pageoffsets_used[$uni_plane];
821 $uni_pageoffsets_space_sum += 256 * 4;
822 $uni_data_used_sum2 += $uni_data_used_sum[$uni_plane];
823 $uni_data_space_sum2 += $uni_data_space_sum[$uni_plane];
827 print OUT
" -1, /* plane ", $uni_plane, " */\n";
831 printStats
($uni_planeoffsets_used, 17 * 4),
833 printStats
($uni_pageoffsets_used_sum, $uni_pageoffsets_space_sum),
835 printStats
($uni_data_used_sum2, $uni_data_space_sum2),
840 print "Unihan.txt = ", $count_Unihan_txt,
841 ", CNS11643.TXT = ", $count_CNS11643_TXT,
842 ", Uni2CNS = ", $count_Uni2CNS,
844 ($count_Unihan_txt + $count_CNS11643_TXT + $count_Uni2CNS),