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 * https://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 ) ) ) {
47 '/^([0-9A-F]+)(?:..([0-9A-F]+))?\s*;\s*(NFC_QC)\s*;\s*([MN])/',
51 list( $junk, $first, $last, $prop, $value ) = $matches;
52 #print "$first $last $prop $value\n";
57 $lastInDecimal = hexdec( $last );
58 for ( $i = hexdec( $first ); $i <= $lastInDecimal; $i++
) {
59 $char = codepointToUtf8( $i );
60 $checkNFC[$char] = $value;
66 $in = fopen( "CompositionExclusions.txt", "rt" );
68 print "Can't open CompositionExclusions.txt for reading.\n";
69 print "If necessary, fetch this file from the internet:\n";
70 print "http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt\n";
74 while ( false !== ( $line = fgets( $in ) ) ) {
75 if ( preg_match( '/^([0-9A-F]+)/i', $line, $matches ) ) {
76 $codepoint = $matches[1];
77 $source = codepointToUtf8( hexdec( $codepoint ) );
78 $exclude[$source] = true;
83 $in = fopen( "UnicodeData.txt", "rt" );
85 print "Can't open UnicodeData.txt for reading.\n";
86 print "If necessary, fetch this file from the internet:\n";
87 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
91 $compatibilityDecomp = array();
92 $canonicalDecomp = array();
93 $canonicalComp = array();
94 $combiningClass = array();
99 print "Reading character definitions...\n";
100 while ( false !== ( $line = fgets( $in ) ) ) {
101 $columns = explode( ';', $line );
102 $codepoint = $columns[0];
104 $canonicalCombiningClass = $columns[3];
105 $decompositionMapping = $columns[5];
107 $source = codepointToUtf8( hexdec( $codepoint ) );
109 if ( $canonicalCombiningClass != 0 ) {
110 $combiningClass[$source] = intval( $canonicalCombiningClass );
113 if ( $decompositionMapping === '' ) continue;
114 if ( preg_match( '/^<(.+)> (.*)$/', $decompositionMapping, $matches ) ) {
115 # Compatibility decomposition
117 $decompositionMapping = $matches[2];
124 $dest = hexSequenceToUtf8( $decompositionMapping );
126 $compatibilityDecomp[$source] = $dest;
128 $canonicalDecomp[$source] = $dest;
129 if ( empty( $exclude[$source] ) ) {
130 $canonicalComp[$dest] = $source;
133 #print "$codepoint | $canonicalCombiningClasses | $decompositionMapping\n";
137 print "Recursively expanding canonical mappings...\n";
140 while ( $changed > 0 ) {
141 print "pass $pass\n";
143 foreach ( $canonicalDecomp as $source => $dest ) {
144 $newDest = preg_replace_callback(
145 '/([\xc0-\xff][\x80-\xbf]+)/',
148 if ( $newDest === $dest ) continue;
150 $canonicalDecomp[$source] = $newDest;
155 print "Recursively expanding compatibility mappings...\n";
158 while ( $changed > 0 ) {
159 print "pass $pass\n";
161 foreach ( $compatibilityDecomp as $source => $dest ) {
162 $newDest = preg_replace_callback(
163 '/([\xc0-\xff][\x80-\xbf]+)/',
166 if ( $newDest === $dest ) continue;
168 $compatibilityDecomp[$source] = $newDest;
173 print "$total decomposition mappings ($canon canonical, $compat compatibility)\n";
175 $out = fopen( "UtfNormalData.inc", "wt" );
177 $serCombining = escapeSingleString( serialize( $combiningClass ) );
178 $serComp = escapeSingleString( serialize( $canonicalComp ) );
179 $serCanon = escapeSingleString( serialize( $canonicalDecomp ) );
180 $serCheckNFC = escapeSingleString( serialize( $checkNFC ) );
181 $outdata = "<" . "?php
183 * This file was automatically generated -- do not edit!
184 * Run UtfNormalGenerate.php to create this file again (make clean && make)
188 // @codingStandardsIgnoreFile
190 UtfNormal::\$utfCombiningClass = unserialize( '$serCombining' );
191 UtfNormal::\$utfCanonicalComp = unserialize( '$serComp' );
192 UtfNormal::\$utfCanonicalDecomp = unserialize( '$serCanon' );
193 UtfNormal::\$utfCheckNFC = unserialize( '$serCheckNFC' );
195 fputs( $out, $outdata );
197 print "Wrote out UtfNormalData.inc\n";
199 print "Can't create file UtfNormalData.inc\n";
203 $out = fopen( "UtfNormalDataK.inc", "wt" );
205 $serCompat = escapeSingleString( serialize( $compatibilityDecomp ) );
206 $outdata = "<" . "?php
208 * This file was automatically generated -- do not edit!
209 * Run UtfNormalGenerate.php to create this file again (make clean && make)
213 // @codingStandardsIgnoreFile
215 UtfNormal::\$utfCompatibilityDecomp = unserialize( '$serCompat' );
217 fputs( $out, $outdata );
219 print "Wrote out UtfNormalDataK.inc\n";
222 print "Can't create file UtfNormalDataK.inc\n";
228 function callbackCanonical( $matches ) {
229 // @codingStandardsIgnoreStart MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
230 global $canonicalDecomp;
231 // @codingStandardsIgnoreEnd
233 if ( isset( $canonicalDecomp[$matches[1]] ) ) {
234 return $canonicalDecomp[$matches[1]];
240 function callbackCompat( $matches ) {
241 // @codingStandardsIgnoreStart MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
242 global $compatibilityDecomp;
243 // @codingStandardsIgnoreEnd
245 if ( isset( $compatibilityDecomp[$matches[1]] ) ) {
246 return $compatibilityDecomp[$matches[1]];