3 * This script generates UniNormalData.inc from the Unicode Character Database
4 * and supplementary files.
6 * Copyright (C) 2004 Brion Vibber <brion@pobox.com>
7 * http://www.mediawiki.org/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 if( PHP_SAPI
!= 'cli' ) {
29 die( "Run me from the command line please.\n" );
32 require_once 'UtfNormalDefines.php';
33 require_once 'UtfNormalUtil.php';
35 $in = fopen("DerivedNormalizationProps.txt", "rt" );
37 print "Can't open DerivedNormalizationProps.txt for reading.\n";
38 print "If necessary, fetch this file from the internet:\n";
39 print "http://www.unicode.org/Public/UNIDATA/DerivedNormalizationProps.txt\n";
42 print "Initializing normalization quick check tables...\n";
44 while( false !== ($line = fgets( $in ) ) ) {
46 if( preg_match( '/^([0-9A-F]+)(?:..([0-9A-F]+))?\s*;\s*(NFC_QC)\s*;\s*([MN])/', $line, $matches ) ) {
47 list( $junk, $first, $last, $prop, $value ) = $matches;
48 #print "$first $last $prop $value\n";
49 if( !$last ) $last = $first;
50 for( $i = hexdec( $first ); $i <= hexdec( $last ); $i++
) {
51 $char = codepointToUtf8( $i );
52 $checkNFC[$char] = $value;
58 $in = fopen("CompositionExclusions.txt", "rt" );
60 print "Can't open CompositionExclusions.txt for reading.\n";
61 print "If necessary, fetch this file from the internet:\n";
62 print "http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt\n";
66 while( false !== ($line = fgets( $in ) ) ) {
67 if( preg_match( '/^([0-9A-F]+)/i', $line, $matches ) ) {
68 $codepoint = $matches[1];
69 $source = codepointToUtf8( hexdec( $codepoint ) );
70 $exclude[$source] = true;
75 $in = fopen("UnicodeData.txt", "rt" );
77 print "Can't open UnicodeData.txt for reading.\n";
78 print "If necessary, fetch this file from the internet:\n";
79 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
83 $compatibilityDecomp = array();
84 $canonicalDecomp = array();
85 $canonicalComp = array();
86 $combiningClass = array();
91 print "Reading character definitions...\n";
92 while( false !== ($line = fgets( $in ) ) ) {
93 $columns = explode(';', $line);
94 $codepoint = $columns[0];
96 $canonicalCombiningClass = $columns[3];
97 $decompositionMapping = $columns[5];
99 $source = codepointToUtf8( hexdec( $codepoint ) );
101 if( $canonicalCombiningClass != 0 ) {
102 $combiningClass[$source] = intval( $canonicalCombiningClass );
105 if( $decompositionMapping === '' ) continue;
106 if( preg_match( '/^<(.+)> (.*)$/', $decompositionMapping, $matches ) ) {
107 # Compatibility decomposition
109 $decompositionMapping = $matches[2];
116 $dest = hexSequenceToUtf8( $decompositionMapping );
118 $compatibilityDecomp[$source] = $dest;
120 $canonicalDecomp[$source] = $dest;
121 if( empty( $exclude[$source] ) ) {
122 $canonicalComp[$dest] = $source;
125 #print "$codepoint | $canonicalCombiningClasses | $decompositionMapping\n";
129 print "Recursively expanding canonical mappings...\n";
132 while( $changed > 0 ) {
133 print "pass $pass\n";
135 foreach( $canonicalDecomp as $source => $dest ) {
136 $newDest = preg_replace_callback(
137 '/([\xc0-\xff][\x80-\xbf]+)/',
140 if( $newDest === $dest ) continue;
142 $canonicalDecomp[$source] = $newDest;
147 print "Recursively expanding compatibility mappings...\n";
150 while( $changed > 0 ) {
151 print "pass $pass\n";
153 foreach( $compatibilityDecomp as $source => $dest ) {
154 $newDest = preg_replace_callback(
155 '/([\xc0-\xff][\x80-\xbf]+)/',
158 if( $newDest === $dest ) continue;
160 $compatibilityDecomp[$source] = $newDest;
165 print "$total decomposition mappings ($canon canonical, $compat compatibility)\n";
167 $out = fopen("UtfNormalData.inc", "wt");
169 $serCombining = escapeSingleString( serialize( $combiningClass ) );
170 $serComp = escapeSingleString( serialize( $canonicalComp ) );
171 $serCanon = escapeSingleString( serialize( $canonicalDecomp ) );
172 $serCheckNFC = escapeSingleString( serialize( $checkNFC ) );
173 $outdata = "<" . "?php
175 * This file was automatically generated -- do not edit!
176 * Run UtfNormalGenerate.php to create this file again (make clean && make)
181 UtfNormal::\$utfCombiningClass = unserialize( '$serCombining' );
182 UtfNormal::\$utfCanonicalComp = unserialize( '$serComp' );
183 UtfNormal::\$utfCanonicalDecomp = unserialize( '$serCanon' );
184 UtfNormal::\$utfCheckNFC = unserialize( '$serCheckNFC' );
186 fputs( $out, $outdata );
188 print "Wrote out UtfNormalData.inc\n";
190 print "Can't create file UtfNormalData.inc\n";
195 $out = fopen("UtfNormalDataK.inc", "wt");
197 $serCompat = escapeSingleString( serialize( $compatibilityDecomp ) );
198 $outdata = "<" . "?php
200 * This file was automatically generated -- do not edit!
201 * Run UtfNormalGenerate.php to create this file again (make clean && make)
206 UtfNormal::\$utfCompatibilityDecomp = unserialize( '$serCompat' );
208 fputs( $out, $outdata );
210 print "Wrote out UtfNormalDataK.inc\n";
213 print "Can't create file UtfNormalDataK.inc\n";
219 function callbackCanonical( $matches ) {
220 global $canonicalDecomp;
221 if( isset( $canonicalDecomp[$matches[1]] ) ) {
222 return $canonicalDecomp[$matches[1]];
227 function callbackCompat( $matches ) {
228 global $compatibilityDecomp;
229 if( isset( $compatibilityDecomp[$matches[1]] ) ) {
230 return $compatibilityDecomp[$matches[1]];