summaryrefslogtreecommitdiff
path: root/includes/normal/UtfNormalGenerate.php
blob: a16e76a88fa3d574bbe97f097a531d31b20ab996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
# http://www.mediawiki.org/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# http://www.gnu.org/copyleft/gpl.html

/**
 * This script generates UniNormalData.inc from the Unicode Character Database
 * and supplementary files.
 *
 * @ingroup UtfNormal
 * @access private
 */

/** */

if( php_sapi_name() != 'cli' ) {
	die( "Run me from the command line please.\n" );
}

require_once 'UtfNormalUtil.php';

$in = fopen("DerivedNormalizationProps.txt", "rt" );
if( !$in ) {
	print "Can't open DerivedNormalizationProps.txt for reading.\n";
	print "If necessary, fetch this file from the internet:\n";
	print "http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt\n";
	exit(-1);
}
print "Initializing normalization quick check tables...\n";
$checkNFC = array();
while( false !== ($line = fgets( $in ) ) ) {
	$matches = array();
	if( preg_match( '/^([0-9A-F]+)(?:..([0-9A-F]+))?\s*;\s*(NFC_QC)\s*;\s*([MN])/', $line, $matches ) ) {
		list( $junk, $first, $last, $prop, $value ) = $matches;
		#print "$first $last $prop $value\n";
		if( !$last ) $last = $first;
		for( $i = hexdec( $first ); $i <= hexdec( $last ); $i++) {
			$char = codepointToUtf8( $i );
			$checkNFC[$char] = $value;
		}
	}
}
fclose( $in );

$in = fopen("CompositionExclusions.txt", "rt" );
if( !$in ) {
	print "Can't open CompositionExclusions.txt for reading.\n";
	print "If necessary, fetch this file from the internet:\n";
	print "http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt\n";
	exit(-1);
}
$exclude = array();
while( false !== ($line = fgets( $in ) ) ) {
	if( preg_match( '/^([0-9A-F]+)/i', $line, $matches ) ) {
		$codepoint = $matches[1];
		$source = codepointToUtf8( hexdec( $codepoint ) );
		$exclude[$source] = true;
	}
}
fclose($in);

$in = fopen("UnicodeData.txt", "rt" );
if( !$in ) {
	print "Can't open UnicodeData.txt for reading.\n";
	print "If necessary, fetch this file from the internet:\n";
	print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
	exit(-1);
}

$compatibilityDecomp = array();
$canonicalDecomp = array();
$canonicalComp = array();
$combiningClass = array();
$total = 0;
$compat = 0;
$canon = 0;

print "Reading character definitions...\n";
while( false !== ($line = fgets( $in ) ) ) {
	$columns = split(';', $line);
	$codepoint = $columns[0];
	$name = $columns[1];
	$canonicalCombiningClass = $columns[3];
	$decompositionMapping = $columns[5];

	$source = codepointToUtf8( hexdec( $codepoint ) );

	if( $canonicalCombiningClass != 0 ) {
		$combiningClass[$source] = intval( $canonicalCombiningClass );
	}

	if( $decompositionMapping === '' ) continue;
	if( preg_match( '/^<(.+)> (.*)$/', $decompositionMapping, $matches ) ) {
		# Compatibility decomposition
		$canonical = false;
		$decompositionMapping = $matches[2];
		$compat++;
	} else {
		$canonical = true;
		$canon++;
	}
	$total++;
	$dest = hexSequenceToUtf8( $decompositionMapping );

	$compatibilityDecomp[$source] = $dest;
	if( $canonical ) {
		$canonicalDecomp[$source] = $dest;
		if( empty( $exclude[$source] ) ) {
			$canonicalComp[$dest] = $source;
		}
	}
	#print "$codepoint | $canonicalCombiningClasses | $decompositionMapping\n";
}
fclose( $in );

print "Recursively expanding canonical mappings...\n";
$changed = 42;
$pass = 1;
while( $changed > 0 ) {
	print "pass $pass\n";
	$changed = 0;
	foreach( $canonicalDecomp as $source => $dest ) {
		$newDest = preg_replace_callback(
			'/([\xc0-\xff][\x80-\xbf]+)/',
			'callbackCanonical',
			$dest);
		if( $newDest === $dest ) continue;
		$changed++;
		$canonicalDecomp[$source] = $newDest;
	}
	$pass++;
}

print "Recursively expanding compatibility mappings...\n";
$changed = 42;
$pass = 1;
while( $changed > 0 ) {
	print "pass $pass\n";
	$changed = 0;
	foreach( $compatibilityDecomp as $source => $dest ) {
		$newDest = preg_replace_callback(
			'/([\xc0-\xff][\x80-\xbf]+)/',
			'callbackCompat',
			$dest);
		if( $newDest === $dest ) continue;
		$changed++;
		$compatibilityDecomp[$source] = $newDest;
	}
	$pass++;
}

print "$total decomposition mappings ($canon canonical, $compat compatibility)\n";

$out = fopen("UtfNormalData.inc", "wt");
if( $out ) {
	$serCombining = escapeSingleString( serialize( $combiningClass ) );
	$serComp = escapeSingleString( serialize( $canonicalComp ) );
	$serCanon = escapeSingleString( serialize( $canonicalDecomp ) );
	$serCheckNFC = escapeSingleString( serialize( $checkNFC ) );
	$outdata = "<" . "?php
/**
 * This file was automatically generated -- do not edit!
 * Run UtfNormalGenerate.php to create this file again (make clean && make)
 */
/** */
global \$utfCombiningClass, \$utfCanonicalComp, \$utfCanonicalDecomp, \$utfCheckNFC;
\$utfCombiningClass = unserialize( '$serCombining' );
\$utfCanonicalComp = unserialize( '$serComp' );
\$utfCanonicalDecomp = unserialize( '$serCanon' );
\$utfCheckNFC = unserialize( '$serCheckNFC' );
?" . ">\n";
	fputs( $out, $outdata );
	fclose( $out );
	print "Wrote out UtfNormalData.inc\n";
} else {
	print "Can't create file UtfNormalData.inc\n";
	exit(-1);
}


$out = fopen("UtfNormalDataK.inc", "wt");
if( $out ) {
	$serCompat = escapeSingleString( serialize( $compatibilityDecomp ) );
	$outdata = "<" . "?php
/**
 * This file was automatically generated -- do not edit!
 * Run UtfNormalGenerate.php to create this file again (make clean && make)
 */
/** */
global \$utfCompatibilityDecomp;
\$utfCompatibilityDecomp = unserialize( '$serCompat' );
?" . ">\n";
	fputs( $out, $outdata );
	fclose( $out );
	print "Wrote out UtfNormalDataK.inc\n";
	exit(0);
} else {
	print "Can't create file UtfNormalDataK.inc\n";
	exit(-1);
}

# ---------------

function callbackCanonical( $matches ) {
	global $canonicalDecomp;
	if( isset( $canonicalDecomp[$matches[1]] ) ) {
		return $canonicalDecomp[$matches[1]];
	}
	return $matches[1];
}

function callbackCompat( $matches ) {
	global $compatibilityDecomp;
	if( isset( $compatibilityDecomp[$matches[1]] ) ) {
		return $compatibilityDecomp[$matches[1]];
	}
	return $matches[1];
}