| 1 |
<?php |
| 2 |
/** |
| 3 |
* This script generates UniNormalData.inc from the Unicode Character Database |
| 4 |
* and supplementary files. |
| 5 |
* |
| 6 |
* Copyright (C) 2004 Brion Vibber <[email protected]> |
| 7 |
* https://www.mediawiki.org/ |
| 8 |
* |
| 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. |
| 13 |
* |
| 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. |
| 18 |
* |
| 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 |
| 23 |
* |
| 24 |
* @file |
| 25 |
* @ingroup UtfNormal |
| 26 |
*/ |
| 27 |
|
| 28 |
use UtfNormal\Utils; |
| 29 |
|
| 30 |
if ( PHP_SAPI != 'cli' ) { |
| 31 |
die( "Run me from the command line please.\n" ); |
| 32 |
} |
| 33 |
|
| 34 |
require_once dirname( __DIR__ ) . '/vendor/autoload.php'; |
| 35 |
|
| 36 |
/** |
| 37 |
* @param string $file |
| 38 |
* @param string $url |
| 39 |
*/ |
| 40 |
function download( $file, $url ) { |
| 41 |
print "Downloading data from $url...\n"; |
| 42 |
$fp = fopen( $file, 'w+' ); |
| 43 |
$ch = curl_init( $url ); |
| 44 |
curl_setopt( $ch, CURLOPT_FILE, $fp ); |
| 45 |
curl_exec( $ch ); |
| 46 |
curl_close( $ch ); |
| 47 |
fclose( $fp ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param string $file |
| 52 |
* @param string $url |
| 53 |
* @return resource |
| 54 |
*/ |
| 55 |
function getFilePointer( $file, $url ) { |
| 56 |
if ( in_array( '--fetch', $_SERVER['argv'] ) ) { |
| 57 |
download( $file, $url ); |
| 58 |
} elseif ( !file_exists( $file ) ) { |
| 59 |
print "Can't open $file for reading.\n"; |
| 60 |
print "If necessary, fetch this file from the internet:\n"; |
| 61 |
print "$url\n"; |
| 62 |
print "Or re-run this script with --fetch\n"; |
| 63 |
exit( -1 ); |
| 64 |
} |
| 65 |
|
| 66 |
$fp = fopen( $file, "rt" ); |
| 67 |
if ( !$fp ) { |
| 68 |
// Eh? |
| 69 |
print "Can't open $file for reading.\n"; |
| 70 |
exit( -1 ); |
| 71 |
} |
| 72 |
|
| 73 |
return $fp; |
| 74 |
} |
| 75 |
|
| 76 |
$in = getFilePointer( |
| 77 |
__DIR__ . "/data/DerivedNormalizationProps.txt", |
| 78 |
'http://www.unicode.org/Public/UNIDATA/DerivedNormalizationProps.txt' |
| 79 |
); |
| 80 |
print "Initializing normalization quick check tables...\n"; |
| 81 |
$checkNFC = []; |
| 82 |
while ( ( $line = fgets( $in ) ) !== false ) { |
| 83 |
$matches = []; |
| 84 |
if ( preg_match( |
| 85 |
'/^([0-9A-F]+)(?:..([0-9A-F]+))?\s*;\s*(NFC_QC)\s*;\s*([MN])/', |
| 86 |
$line, |
| 87 |
$matches ) |
| 88 |
) { |
| 89 |
list( $junk, $first, $last, $prop, $value ) = $matches; |
| 90 |
# print "$first $last $prop $value\n"; |
| 91 |
if ( !$last ) { |
| 92 |
$last = $first; |
| 93 |
} |
| 94 |
|
| 95 |
$lastInDecimal = hexdec( $last ); |
| 96 |
for ( $i = hexdec( $first ); $i <= $lastInDecimal; $i++ ) { |
| 97 |
$char = Utils::codepointToUtf8( $i ); |
| 98 |
$checkNFC[$char] = $value; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
fclose( $in ); |
| 103 |
|
| 104 |
$in = getFilePointer( |
| 105 |
__DIR__ . "/data/CompositionExclusions.txt", |
| 106 |
'http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt' |
| 107 |
); |
| 108 |
$exclude = []; |
| 109 |
while ( ( $line = fgets( $in ) ) !== false ) { |
| 110 |
if ( preg_match( '/^([0-9A-F]+)/i', $line, $matches ) ) { |
| 111 |
$codepoint = $matches[1]; |
| 112 |
$source = Utils::codepointToUtf8( hexdec( $codepoint ) ); |
| 113 |
$exclude[$source] = true; |
| 114 |
} |
| 115 |
} |
| 116 |
fclose( $in ); |
| 117 |
|
| 118 |
$in = getFilePointer( |
| 119 |
__DIR__ . "/data/UnicodeData.txt", |
| 120 |
'http://www.unicode.org/Public/UNIDATA/UnicodeData.txt' |
| 121 |
); |
| 122 |
$compatibilityDecomp = []; |
| 123 |
$canonicalDecomp = []; |
| 124 |
$canonicalComp = []; |
| 125 |
$combiningClass = []; |
| 126 |
$total = 0; |
| 127 |
$compat = 0; |
| 128 |
$canon = 0; |
| 129 |
|
| 130 |
print "Reading character definitions...\n"; |
| 131 |
while ( ( $line = fgets( $in ) ) !== false ) { |
| 132 |
$columns = explode( ';', $line ); |
| 133 |
$codepoint = $columns[0]; |
| 134 |
$name = $columns[1]; |
| 135 |
$canonicalCombiningClass = $columns[3]; |
| 136 |
$decompositionMapping = $columns[5]; |
| 137 |
|
| 138 |
$source = Utils::codepointToUtf8( hexdec( $codepoint ) ); |
| 139 |
|
| 140 |
if ( $canonicalCombiningClass != 0 ) { |
| 141 |
$combiningClass[$source] = intval( $canonicalCombiningClass ); |
| 142 |
} |
| 143 |
|
| 144 |
if ( $decompositionMapping === '' ) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
if ( preg_match( '/^<(.+)> (.*)$/', $decompositionMapping, $matches ) ) { |
| 148 |
# Compatibility decomposition |
| 149 |
$canonical = false; |
| 150 |
$decompositionMapping = $matches[2]; |
| 151 |
$compat++; |
| 152 |
} else { |
| 153 |
$canonical = true; |
| 154 |
$canon++; |
| 155 |
} |
| 156 |
$total++; |
| 157 |
$dest = Utils::hexSequenceToUtf8( $decompositionMapping ); |
| 158 |
|
| 159 |
$compatibilityDecomp[$source] = $dest; |
| 160 |
if ( $canonical ) { |
| 161 |
$canonicalDecomp[$source] = $dest; |
| 162 |
if ( empty( $exclude[$source] ) ) { |
| 163 |
$canonicalComp[$dest] = $source; |
| 164 |
} |
| 165 |
} |
| 166 |
# print "$codepoint | $canonicalCombiningClasses | $decompositionMapping\n"; |
| 167 |
} |
| 168 |
fclose( $in ); |
| 169 |
|
| 170 |
print "Recursively expanding canonical mappings...\n"; |
| 171 |
$changed = 42; |
| 172 |
$pass = 1; |
| 173 |
while ( $changed > 0 ) { |
| 174 |
print "pass $pass\n"; |
| 175 |
$changed = 0; |
| 176 |
foreach ( $canonicalDecomp as $source => $dest ) { |
| 177 |
$newDest = preg_replace_callback( |
| 178 |
'/([\xc0-\xff][\x80-\xbf]+)/', |
| 179 |
'callbackCanonical', |
| 180 |
$dest ); |
| 181 |
if ( $newDest === $dest ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
$changed++; |
| 185 |
$canonicalDecomp[$source] = $newDest; |
| 186 |
} |
| 187 |
$pass++; |
| 188 |
} |
| 189 |
|
| 190 |
print "Recursively expanding compatibility mappings...\n"; |
| 191 |
$changed = 42; |
| 192 |
$pass = 1; |
| 193 |
while ( $changed > 0 ) { |
| 194 |
print "pass $pass\n"; |
| 195 |
$changed = 0; |
| 196 |
foreach ( $compatibilityDecomp as $source => $dest ) { |
| 197 |
$newDest = preg_replace_callback( |
| 198 |
'/([\xc0-\xff][\x80-\xbf]+)/', |
| 199 |
'callbackCompat', |
| 200 |
$dest ); |
| 201 |
if ( $newDest === $dest ) { |
| 202 |
continue; |
| 203 |
} |
| 204 |
$changed++; |
| 205 |
$compatibilityDecomp[$source] = $newDest; |
| 206 |
} |
| 207 |
$pass++; |
| 208 |
} |
| 209 |
|
| 210 |
print "$total decomposition mappings ($canon canonical, $compat compatibility)\n"; |
| 211 |
|
| 212 |
$out = fopen( dirname( __DIR__ ) . "/src/UtfNormalData.inc", "wt" ); |
| 213 |
if ( $out ) { |
| 214 |
$serCombining = Utils::escapeSingleString( serialize( $combiningClass ) ); |
| 215 |
$serComp = Utils::escapeSingleString( serialize( $canonicalComp ) ); |
| 216 |
$serCanon = Utils::escapeSingleString( serialize( $canonicalDecomp ) ); |
| 217 |
$serCheckNFC = Utils::escapeSingleString( serialize( $checkNFC ) ); |
| 218 |
$outdata = "<" . "?php |
| 219 |
/** |
| 220 |
* This file was automatically generated -- do not edit! |
| 221 |
* Run UtfNormalGenerate.php to create this file again (make clean && make) |
| 222 |
* |
| 223 |
* @file |
| 224 |
*/ |
| 225 |
|
| 226 |
UtfNormal\Validator::\$utfCombiningClass = unserialize( '$serCombining' ); |
| 227 |
UtfNormal\Validator::\$utfCanonicalComp = unserialize( '$serComp' ); |
| 228 |
UtfNormal\Validator::\$utfCanonicalDecomp = unserialize( '$serCanon' ); |
| 229 |
UtfNormal\Validator::\$utfCheckNFC = unserialize( '$serCheckNFC' ); |
| 230 |
"; |
| 231 |
fputs( $out, $outdata ); |
| 232 |
fclose( $out ); |
| 233 |
print "Wrote out UtfNormalData.inc\n"; |
| 234 |
} else { |
| 235 |
print "Can't create file UtfNormalData.inc\n"; |
| 236 |
exit( -1 ); |
| 237 |
} |
| 238 |
|
| 239 |
$out = fopen( dirname( __DIR__ ) . "/src/UtfNormalDataK.inc", "wt" ); |
| 240 |
if ( $out ) { |
| 241 |
$serCompat = Utils::escapeSingleString( serialize( $compatibilityDecomp ) ); |
| 242 |
$outdata = "<" . "?php |
| 243 |
/** |
| 244 |
* This file was automatically generated -- do not edit! |
| 245 |
* Run UtfNormalGenerate.php to create this file again (make clean && make) |
| 246 |
* |
| 247 |
* @file |
| 248 |
*/ |
| 249 |
|
| 250 |
UtfNormal\Validator::\$utfCompatibilityDecomp = unserialize( '$serCompat' ); |
| 251 |
"; |
| 252 |
fputs( $out, $outdata ); |
| 253 |
fclose( $out ); |
| 254 |
print "Wrote out UtfNormalDataK.inc\n"; |
| 255 |
exit( 0 ); |
| 256 |
} else { |
| 257 |
print "Can't create file UtfNormalDataK.inc\n"; |
| 258 |
exit( -1 ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @param array $matches |
| 263 |
* @return string |
| 264 |
*/ |
| 265 |
function callbackCanonical( $matches ) { |
| 266 |
global $canonicalDecomp; |
| 267 |
|
| 268 |
if ( isset( $canonicalDecomp[$matches[1]] ) ) { |
| 269 |
return $canonicalDecomp[$matches[1]]; |
| 270 |
} |
| 271 |
|
| 272 |
return $matches[1]; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* @param array $matches |
| 277 |
* @return string |
| 278 |
*/ |
| 279 |
function callbackCompat( $matches ) { |
| 280 |
global $compatibilityDecomp; |
| 281 |
|
| 282 |
if ( isset( $compatibilityDecomp[$matches[1]] ) ) { |
| 283 |
return $compatibilityDecomp[$matches[1]]; |
| 284 |
} |
| 285 |
|
| 286 |
return $matches[1]; |
| 287 |
} |