genUnicodePropertyData.pl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. #!/usr/bin/env perl
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. # This tool is used to prepare lookup tables of Unicode character properties
  6. # needed by gfx code to support text shaping operations. The properties are
  7. # read from the Unicode Character Database and compiled into multi-level arrays
  8. # for efficient lookup.
  9. #
  10. # To regenerate the tables in nsUnicodePropertyData.cpp:
  11. #
  12. # (1) Download the current Unicode data files from
  13. #
  14. # http://www.unicode.org/Public/UNIDATA/
  15. #
  16. # NB: not all the files are actually needed; currently, we require
  17. # - UnicodeData.txt
  18. # - Scripts.txt
  19. # - BidiMirroring.txt
  20. # - BidiBrackets.txt
  21. # - HangulSyllableType.txt
  22. # - LineBreak.txt
  23. # - EastAsianWidth.txt
  24. # - ReadMe.txt (to record version/date of the UCD)
  25. # - Unihan_Variants.txt (from Unihan.zip)
  26. # though this may change if we find a need for additional properties.
  27. #
  28. # The Unicode data files listed above should be together in one directory.
  29. #
  30. # We also require the file
  31. # http://www.unicode.org/Public/security/latest/xidmodifications.txt
  32. # This file should be in a sub-directory "security" immediately below the
  33. # directory containing the other Unicode data files.
  34. #
  35. # We also require the latest data file for UTR50, currently revision-13:
  36. # http://www.unicode.org/Public/vertical/revision-13/VerticalOrientation-13.txt
  37. # This file should be in a sub-directory "vertical" immediately below the
  38. # directory containing the other Unicode data files.
  39. #
  40. #
  41. # (2) Run this tool using a command line of the form
  42. #
  43. # perl genUnicodePropertyData.pl \
  44. # /path/to/harfbuzz/src \
  45. # /path/to/icu/common/unicode \
  46. # /path/to/UCD-directory
  47. #
  48. # This will generate (or overwrite!) the files
  49. #
  50. # nsUnicodePropertyData.cpp
  51. # nsUnicodeScriptCodes.h
  52. #
  53. # in the current directory.
  54. use strict;
  55. use List::Util qw(first);
  56. if ($#ARGV != 2) {
  57. print <<__EOT;
  58. # Run this tool using a command line of the form
  59. #
  60. # perl genUnicodePropertyData.pl \\
  61. # /path/to/harfbuzz/src \\
  62. # /path/to/icu/common/unicode \\
  63. # /path/to/UCD-directory
  64. #
  65. # where harfbuzz/src is the directory containing harfbuzz .cc and .hh files,
  66. # icu/common/unicode is the directory containing ICU 'common' public headers,
  67. # and UCD-directory is a directory containing the current Unicode Character
  68. # Database files (UnicodeData.txt, etc), available from
  69. # http://www.unicode.org/Public/UNIDATA/, with additional resources as
  70. # detailed in the source comments.
  71. #
  72. # This will generate (or overwrite!) the files
  73. #
  74. # nsUnicodePropertyData.cpp
  75. # nsUnicodeScriptCodes.h
  76. #
  77. # in the current directory.
  78. __EOT
  79. exit 0;
  80. }
  81. my $HARFBUZZ = $ARGV[0];
  82. my $ICU = $ARGV[1];
  83. my $UNICODE = $ARGV[2];
  84. # load HB_Category constants
  85. my $cc = -1;
  86. my %catCode;
  87. sub readHarfBuzzHeader
  88. {
  89. my $file = shift;
  90. open FH, "< $HARFBUZZ/$file" or die "can't open harfbuzz header $HARFBUZZ/$file\n";
  91. while (<FH>) {
  92. if (m/HB_UNICODE_GENERAL_CATEGORY_([A-Z_]+)/) {
  93. $cc++;
  94. $catCode{$1} = $cc;
  95. }
  96. }
  97. close FH;
  98. }
  99. &readHarfBuzzHeader("hb-unicode.h");
  100. die "didn't find HarfBuzz category codes\n" if $cc == -1;
  101. my %scriptCode;
  102. my @scriptCodeToTag;
  103. my @scriptCodeToName;
  104. my $sc = -1;
  105. sub readIcuHeader
  106. {
  107. my $file = shift;
  108. open FH, "< $ICU/$file" or die "can't open ICU header $ICU/$file\n";
  109. while (<FH>) {
  110. # adjust for ICU vs UCD naming discrepancies
  111. s/LANNA/TAI_THAM/;
  112. s/MEITEI_MAYEK/MEETEI_MAYEK/;
  113. s/ORKHON/OLD_TURKIC/;
  114. s/MENDE/MENDE_KIKAKUI/;
  115. s/SIGN_WRITING/SIGNWRITING/;
  116. if (m|USCRIPT_([A-Z_]+)\s*=\s*([0-9]+),\s*/\*\s*([A-Z][a-z]{3})\s*\*/|) {
  117. $sc = $2;
  118. $scriptCode{$1} = $sc;
  119. $scriptCodeToTag[$sc] = $3;
  120. $scriptCodeToName[$sc] = $1;
  121. }
  122. }
  123. close FH;
  124. }
  125. &readIcuHeader("uscript.h");
  126. die "didn't find ICU script codes\n" if $sc == -1;
  127. my %xidmodCode = (
  128. 'Recommended' => 0,
  129. 'Inclusion' => 1,
  130. 'Uncommon_Use' => 2,
  131. 'Technical' => 3,
  132. 'Obsolete' => 4,
  133. 'Aspirational' => 5,
  134. 'Limited_Use' => 6,
  135. 'Exclusion' => 7,
  136. 'Not_XID' => 8,
  137. 'Not_NFKC' => 9,
  138. 'Default_Ignorable' => 10,
  139. 'Deprecated' => 11,
  140. 'not-chars' => 12
  141. );
  142. my %bidicategoryCode = (
  143. "L" => 0, # Left-to-Right
  144. "R" => 1, # Right-to-Left
  145. "EN" => 2, # European Number
  146. "ES" => 3, # European Number Separator
  147. "ET" => 4, # European Number Terminator
  148. "AN" => 5, # Arabic Number
  149. "CS" => 6, # Common Number Separator
  150. "B" => 7, # Paragraph Separator
  151. "S" => 8, # Segment Separator
  152. "WS" => 9, # Whitespace
  153. "ON" => 10, # Other Neutrals
  154. "LRE" => 11, # Left-to-Right Embedding
  155. "LRO" => 12, # Left-to-Right Override
  156. "AL" => 13, # Right-to-Left Arabic
  157. "RLE" => 14, # Right-to-Left Embedding
  158. "RLO" => 15, # Right-to-Left Override
  159. "PDF" => 16, # Pop Directional Format
  160. "NSM" => 17, # Non-Spacing Mark
  161. "BN" => 18, # Boundary Neutral
  162. "FSI" => 19, # First Strong Isolate
  163. "LRI" => 20, # Left-to-Right Isolate
  164. "RLI" => 21, # Right-to-left Isolate
  165. "PDI" => 22 # Pop Direcitonal Isolate
  166. );
  167. my %verticalOrientationCode = (
  168. 'U' => 0, # U - Upright, the same orientation as in the code charts
  169. 'R' => 1, # R - Rotated 90 degrees clockwise compared to the code charts
  170. 'Tu' => 2, # Tu - Transformed typographically, with fallback to Upright
  171. 'Tr' => 3 # Tr - Transformed typographically, with fallback to Rotated
  172. );
  173. my %lineBreakCode = ( # ordering matches ICU's ULineBreak enum
  174. "XX" => 0,
  175. "AI" => 1,
  176. "AL" => 2,
  177. "B2" => 3,
  178. "BA" => 4,
  179. "BB" => 5,
  180. "BK" => 6,
  181. "CB" => 7,
  182. "CL" => 8,
  183. "CM" => 9,
  184. "CR" => 10,
  185. "EX" => 11,
  186. "GL" => 12,
  187. "HY" => 13,
  188. "ID" => 14,
  189. "IN" => 15,
  190. "IS" => 16,
  191. "LF" => 17,
  192. "NS" => 18,
  193. "NU" => 19,
  194. "OP" => 20,
  195. "PO" => 21,
  196. "PR" => 22,
  197. "QU" => 23,
  198. "SA" => 24,
  199. "SG" => 25,
  200. "SP" => 26,
  201. "SY" => 27,
  202. "ZW" => 28,
  203. "NL" => 29,
  204. "WJ" => 30,
  205. "H2" => 31,
  206. "H3" => 32,
  207. "JL" => 33,
  208. "JT" => 34,
  209. "JV" => 35,
  210. "CP" => 36,
  211. "CJ" => 37,
  212. "HL" => 38,
  213. "RI" => 39
  214. );
  215. my %eastAsianWidthCode = (
  216. "N" => 0,
  217. "A" => 1,
  218. "H" => 2,
  219. "W" => 3,
  220. "F" => 4,
  221. "Na" => 5
  222. );
  223. # initialize default properties
  224. my @script;
  225. my @category;
  226. my @combining;
  227. my @mirror;
  228. my @pairedBracketType;
  229. my @hangul;
  230. my @casemap;
  231. my @xidmod;
  232. my @numericvalue;
  233. my @hanVariant;
  234. my @bidicategory;
  235. my @fullWidth;
  236. my @fullWidthInverse;
  237. my @verticalOrientation;
  238. my @lineBreak;
  239. my @eastAsianWidthFWH;
  240. for (my $i = 0; $i < 0x110000; ++$i) {
  241. $script[$i] = $scriptCode{"UNKNOWN"};
  242. $category[$i] = $catCode{"UNASSIGNED"};
  243. $combining[$i] = 0;
  244. $pairedBracketType[$i] = 0;
  245. $casemap[$i] = 0;
  246. $xidmod[$i] = $xidmodCode{"not-chars"};
  247. $numericvalue[$i] = -1;
  248. $hanVariant[$i] = 0;
  249. $bidicategory[$i] = $bidicategoryCode{"L"};
  250. $fullWidth[$i] = 0;
  251. $fullWidthInverse[$i] = 0;
  252. $verticalOrientation[$i] = 1; # default for unlisted codepoints is 'R'
  253. $lineBreak[$i] = $lineBreakCode{"XX"};
  254. $eastAsianWidthFWH[$i] = 0;
  255. }
  256. # blocks where the default for bidi category is not L
  257. for my $i (0x0600..0x07BF, 0x08A0..0x08FF, 0xFB50..0xFDCF, 0xFDF0..0xFDFF, 0xFE70..0xFEFF, 0x1EE00..0x0001EEFF) {
  258. $bidicategory[$i] = $bidicategoryCode{"AL"};
  259. }
  260. for my $i (0x0590..0x05FF, 0x07C0..0x089F, 0xFB1D..0xFB4F, 0x00010800..0x00010FFF, 0x0001E800..0x0001EDFF, 0x0001EF00..0x0001EFFF) {
  261. $bidicategory[$i] = $bidicategoryCode{"R"};
  262. }
  263. for my $i (0x20A0..0x20CF) {
  264. $bidicategory[$i] = $bidicategoryCode{"ET"};
  265. }
  266. my %ucd2hb = (
  267. 'Cc' => 'CONTROL',
  268. 'Cf' => 'FORMAT',
  269. 'Cn' => 'UNASSIGNED',
  270. 'Co' => 'PRIVATE_USE',
  271. 'Cs' => 'SURROGATE',
  272. 'Ll' => 'LOWERCASE_LETTER',
  273. 'Lm' => 'MODIFIER_LETTER',
  274. 'Lo' => 'OTHER_LETTER',
  275. 'Lt' => 'TITLECASE_LETTER',
  276. 'Lu' => 'UPPERCASE_LETTER',
  277. 'Mc' => 'SPACING_MARK',
  278. 'Me' => 'ENCLOSING_MARK',
  279. 'Mn' => 'NON_SPACING_MARK',
  280. 'Nd' => 'DECIMAL_NUMBER',
  281. 'Nl' => 'LETTER_NUMBER',
  282. 'No' => 'OTHER_NUMBER',
  283. 'Pc' => 'CONNECT_PUNCTUATION',
  284. 'Pd' => 'DASH_PUNCTUATION',
  285. 'Pe' => 'CLOSE_PUNCTUATION',
  286. 'Pf' => 'FINAL_PUNCTUATION',
  287. 'Pi' => 'INITIAL_PUNCTUATION',
  288. 'Po' => 'OTHER_PUNCTUATION',
  289. 'Ps' => 'OPEN_PUNCTUATION',
  290. 'Sc' => 'CURRENCY_SYMBOL',
  291. 'Sk' => 'MODIFIER_SYMBOL',
  292. 'Sm' => 'MATH_SYMBOL',
  293. 'So' => 'OTHER_SYMBOL',
  294. 'Zl' => 'LINE_SEPARATOR',
  295. 'Zp' => 'PARAGRAPH_SEPARATOR',
  296. 'Zs' => 'SPACE_SEPARATOR'
  297. );
  298. # read ReadMe.txt
  299. my @versionInfo;
  300. open FH, "< $UNICODE/ReadMe.txt" or die "can't open Unicode ReadMe.txt file\n";
  301. while (<FH>) {
  302. chomp;
  303. push @versionInfo, $_;
  304. }
  305. close FH;
  306. my $kTitleToUpper = 0x80000000;
  307. my $kUpperToLower = 0x40000000;
  308. my $kLowerToTitle = 0x20000000;
  309. my $kLowerToUpper = 0x10000000;
  310. my $kCaseMapCharMask = 0x001fffff;
  311. # read UnicodeData.txt
  312. open FH, "< $UNICODE/UnicodeData.txt" or die "can't open UCD file UnicodeData.txt\n";
  313. while (<FH>) {
  314. chomp;
  315. my @fields = split /;/;
  316. if ($fields[1] =~ /First/) {
  317. my $first = hex "0x$fields[0]";
  318. $_ = <FH>;
  319. @fields = split /;/;
  320. if ($fields[1] =~ /Last/) {
  321. my $last = hex "0x$fields[0]";
  322. do {
  323. $category[$first] = $catCode{$ucd2hb{$fields[2]}};
  324. $combining[$first] = $fields[3];
  325. $bidicategory[$first] = $bidicategoryCode{$fields[4]};
  326. unless (length($fields[7]) == 0) {
  327. $numericvalue[$first] = $fields[7];
  328. }
  329. if ($fields[1] =~ /CJK/) {
  330. @hanVariant[$first] = 3;
  331. }
  332. $first++;
  333. } while ($first <= $last);
  334. } else {
  335. die "didn't find Last code for range!\n";
  336. }
  337. } else {
  338. my $usv = hex "0x$fields[0]";
  339. $category[$usv] = $catCode{$ucd2hb{$fields[2]}};
  340. $combining[$usv] = $fields[3];
  341. my $upper = hex $fields[12];
  342. my $lower = hex $fields[13];
  343. my $title = hex $fields[14];
  344. # we only store one mapping for each character,
  345. # but also record what kind of mapping it is
  346. if ($upper && $lower) {
  347. $casemap[$usv] |= $kTitleToUpper;
  348. $casemap[$usv] |= ($usv ^ $upper);
  349. }
  350. elsif ($lower) {
  351. $casemap[$usv] |= $kUpperToLower;
  352. $casemap[$usv] |= ($usv ^ $lower);
  353. }
  354. elsif ($title && ($title != $upper)) {
  355. $casemap[$usv] |= $kLowerToTitle;
  356. $casemap[$usv] |= ($usv ^ $title);
  357. }
  358. elsif ($upper) {
  359. $casemap[$usv] |= $kLowerToUpper;
  360. $casemap[$usv] |= ($usv ^ $upper);
  361. }
  362. $bidicategory[$usv] = $bidicategoryCode{$fields[4]};
  363. unless (length($fields[7]) == 0) {
  364. $numericvalue[$usv] = $fields[7];
  365. }
  366. if ($fields[1] =~ /CJK/) {
  367. @hanVariant[$usv] = 3;
  368. }
  369. if ($fields[5] =~ /^<narrow>/) {
  370. my $wideChar = hex(substr($fields[5], 9));
  371. die "didn't expect supplementary-plane values here" if $usv > 0xffff || $wideChar > 0xffff;
  372. $fullWidth[$usv] = $wideChar;
  373. $fullWidthInverse[$wideChar] = $usv;
  374. }
  375. elsif ($fields[5] =~ /^<wide>/) {
  376. my $narrowChar = hex(substr($fields[5], 7));
  377. die "didn't expect supplementary-plane values here" if $usv > 0xffff || $narrowChar > 0xffff;
  378. $fullWidth[$narrowChar] = $usv;
  379. $fullWidthInverse[$usv] = $narrowChar;
  380. }
  381. }
  382. }
  383. close FH;
  384. # read Scripts.txt
  385. open FH, "< $UNICODE/Scripts.txt" or die "can't open UCD file Scripts.txt\n";
  386. push @versionInfo, "";
  387. while (<FH>) {
  388. chomp;
  389. push @versionInfo, $_;
  390. last if /Date:/;
  391. }
  392. while (<FH>) {
  393. if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))*\s+;\s+([^ ]+)/) {
  394. my $script = uc($3);
  395. warn "unknown ICU script $script" unless exists $scriptCode{$script};
  396. my $script = $scriptCode{$script};
  397. my $start = hex "0x$1";
  398. my $end = (defined $2) ? hex "0x$2" : $start;
  399. for (my $i = $start; $i <= $end; ++$i) {
  400. $script[$i] = $script;
  401. }
  402. }
  403. }
  404. close FH;
  405. # read BidiMirroring.txt
  406. my @offsets = ();
  407. push @offsets, 0;
  408. open FH, "< $UNICODE/BidiMirroring.txt" or die "can't open UCD file BidiMirroring.txt\n";
  409. push @versionInfo, "";
  410. while (<FH>) {
  411. chomp;
  412. push @versionInfo, $_;
  413. last if /Date:/;
  414. }
  415. while (<FH>) {
  416. s/#.*//;
  417. if (m/([0-9A-F]{4,6});\s*([0-9A-F]{4,6})/) {
  418. my $mirrorOffset = hex("0x$2") - hex("0x$1");
  419. my $offsetIndex = first { $offsets[$_] eq $mirrorOffset } 0..$#offsets;
  420. if ($offsetIndex == undef) {
  421. die "too many offset codes\n" if scalar @offsets == 31;
  422. push @offsets, $mirrorOffset;
  423. $offsetIndex = $#offsets;
  424. }
  425. $mirror[hex "0x$1"] = $offsetIndex;
  426. }
  427. }
  428. close FH;
  429. # read BidiBrackets.txt
  430. my %pairedBracketTypeCode = (
  431. 'N' => 0,
  432. 'O' => 1,
  433. 'C' => 2
  434. );
  435. open FH, "< $UNICODE/BidiBrackets.txt" or die "can't open UCD file BidiBrackets.txt\n";
  436. push @versionInfo, "";
  437. while (<FH>) {
  438. chomp;
  439. push @versionInfo, $_;
  440. last if /Date:/;
  441. }
  442. while (<FH>) {
  443. s/#.*//;
  444. if (m/([0-9A-F]{4,6});\s*([0-9A-F]{4,6});\s*(.)/) {
  445. my $mirroredChar = $offsets[$mirror[hex "0x$1"]] + hex "0x$1";
  446. die "bidi bracket does not match mirrored char\n" unless $mirroredChar == hex "0x$2";
  447. my $pbt = uc($3);
  448. warn "unknown Bidi Bracket type" unless exists $pairedBracketTypeCode{$pbt};
  449. $pairedBracketType[hex "0x$1"] = $pairedBracketTypeCode{$pbt};
  450. }
  451. }
  452. close FH;
  453. # read HangulSyllableType.txt
  454. my %hangulType = (
  455. 'L' => 0x01,
  456. 'V' => 0x02,
  457. 'T' => 0x04,
  458. 'LV' => 0x03,
  459. 'LVT' => 0x07
  460. );
  461. open FH, "< $UNICODE/HangulSyllableType.txt" or die "can't open UCD file HangulSyllableType.txt\n";
  462. push @versionInfo, "";
  463. while (<FH>) {
  464. chomp;
  465. push @versionInfo, $_;
  466. last if /Date:/;
  467. }
  468. while (<FH>) {
  469. s/#.*//;
  470. if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))*\s*;\s*([^ ]+)/) {
  471. my $hangul = uc($3);
  472. warn "unknown Hangul syllable type" unless exists $hangulType{$hangul};
  473. $hangul = $hangulType{$hangul};
  474. my $start = hex "0x$1";
  475. my $end = (defined $2) ? hex "0x$2" : $start;
  476. for (my $i = $start; $i <= $end; ++$i) {
  477. $hangul[$i] = $hangul;
  478. }
  479. }
  480. }
  481. close FH;
  482. # read LineBreak.txt
  483. open FH, "< $UNICODE/LineBreak.txt" or die "can't open UCD file LineBreak.txt\n";
  484. push @versionInfo, "";
  485. while (<FH>) {
  486. chomp;
  487. push @versionInfo, $_;
  488. last if /Date:/;
  489. }
  490. while (<FH>) {
  491. s/#.*//;
  492. if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))*\s*;\s*([^ ]+)/) {
  493. my $lb = uc($3);
  494. warn "unknown LineBreak class" unless exists $lineBreakCode{$lb};
  495. $lb = $lineBreakCode{$lb};
  496. my $start = hex "0x$1";
  497. my $end = (defined $2) ? hex "0x$2" : $start;
  498. for (my $i = $start; $i <= $end; ++$i) {
  499. $lineBreak[$i] = $lb;
  500. }
  501. }
  502. }
  503. close FH;
  504. # read EastAsianWidth.txt
  505. open FH, "< $UNICODE/EastAsianWidth.txt" or die "can't open UCD file EastAsianWidth.txt\n";
  506. push @versionInfo, "";
  507. while (<FH>) {
  508. chomp;
  509. push @versionInfo, $_;
  510. last if /Date:/;
  511. }
  512. while (<FH>) {
  513. s/#.*//;
  514. if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))*\s*;\s*([^ ]+)/) {
  515. my $start = hex "0x$1";
  516. my $end = (defined $2) ? hex "0x$2" : $start;
  517. my $eaw = $3;
  518. warn "unknown EastAsianWidth class" unless exists $eastAsianWidthCode{$eaw};
  519. my $isFWH = ($eaw =~ m/^[FWH]$/) ? 1 : 0;
  520. for (my $i = $start; $i <= $end; ++$i) {
  521. $eastAsianWidthFWH[$i] = $isFWH;
  522. }
  523. }
  524. }
  525. close FH;
  526. # read xidmodifications.txt
  527. open FH, "< $UNICODE/security/xidmodifications.txt" or die "can't open UCD file xidmodifications.txt\n";
  528. push @versionInfo, "";
  529. while (<FH>) {
  530. chomp;
  531. unless (/\xef\xbb\xbf/) {
  532. push @versionInfo, $_;
  533. }
  534. last if /Generated:/;
  535. }
  536. while (<FH>) {
  537. if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))*\s+;\s+[^ ]+\s+;\s+([^ ]+)/) {
  538. my $xidmod = $3;
  539. warn "unknown Identifier Modification $xidmod" unless exists $xidmodCode{$xidmod};
  540. $xidmod = $xidmodCode{$xidmod};
  541. my $start = hex "0x$1";
  542. my $end = (defined $2) ? hex "0x$2" : $start;
  543. for (my $i = $start; $i <= $end; ++$i) {
  544. $xidmod[$i] = $xidmod;
  545. }
  546. }
  547. }
  548. close FH;
  549. open FH, "< $UNICODE/Unihan_Variants.txt" or die "can't open UCD file Unihan_Variants.txt (from Unihan.zip)\n";
  550. push @versionInfo, "";
  551. while (<FH>) {
  552. chomp;
  553. push @versionInfo, $_;
  554. last if /Date:/;
  555. }
  556. my $savedusv = 0;
  557. my $hasTC = 0;
  558. my $hasSC = 0;
  559. while (<FH>) {
  560. chomp;
  561. if (m/U\+([0-9A-F]{4,6})\s+k([^ ]+)Variant/) {
  562. my $usv = hex "0x$1";
  563. if ($usv != $savedusv) {
  564. unless ($savedusv == 0) {
  565. if ($hasTC && !$hasSC) {
  566. $hanVariant[$savedusv] = 1;
  567. } elsif (!$hasTC && $hasSC) {
  568. $hanVariant[$savedusv] = 2;
  569. }
  570. }
  571. $savedusv = $usv;
  572. $hasTC = 0;
  573. $hasSC = 0;
  574. }
  575. if ($2 eq "Traditional") {
  576. $hasTC = 1;
  577. }
  578. if ($2 eq "Simplified") {
  579. $hasSC = 1;
  580. }
  581. }
  582. }
  583. close FH;
  584. # read VerticalOrientation-13.txt
  585. open FH, "< $UNICODE/vertical/VerticalOrientation-13.txt" or die "can't open UTR50 data file VerticalOrientation-13.txt\n";
  586. push @versionInfo, "";
  587. while (<FH>) {
  588. chomp;
  589. push @versionInfo, $_;
  590. last if /Date:/;
  591. }
  592. while (<FH>) {
  593. chomp;
  594. s/#.*//;
  595. if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))*\s*;\s*([^ ]+)/) {
  596. my $vo = $3;
  597. warn "unknown Vertical_Orientation code $vo"
  598. unless exists $verticalOrientationCode{$vo};
  599. $vo = $verticalOrientationCode{$vo};
  600. my $start = hex "0x$1";
  601. my $end = (defined $2) ? hex "0x$2" : $start;
  602. for (my $i = $start; $i <= $end; ++$i) {
  603. $verticalOrientation[$i] = $vo;
  604. }
  605. }
  606. }
  607. close FH;
  608. my $timestamp = gmtime();
  609. open DATA_TABLES, "> nsUnicodePropertyData.cpp" or die "unable to open nsUnicodePropertyData.cpp for output";
  610. my $licenseBlock = q[
  611. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  612. /* This Source Code Form is subject to the terms of the Mozilla Public
  613. * License, v. 2.0. If a copy of the MPL was not distributed with this
  614. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  615. /*
  616. * Derived from the Unicode Character Database by genUnicodePropertyData.pl
  617. *
  618. * For Unicode terms of use, see http://www.unicode.org/terms_of_use.html
  619. */
  620. ];
  621. my $versionInfo = join("\n", @versionInfo);
  622. print DATA_TABLES <<__END;
  623. $licenseBlock
  624. /*
  625. * Created on $timestamp from UCD data files with version info:
  626. *
  627. $versionInfo
  628. *
  629. * * * * * This file contains MACHINE-GENERATED DATA, do not edit! * * * * *
  630. */
  631. #include <stdint.h>
  632. #include "harfbuzz/hb.h"
  633. __END
  634. open HEADER, "> nsUnicodeScriptCodes.h" or die "unable to open nsUnicodeScriptCodes.h for output";
  635. print HEADER <<__END;
  636. $licenseBlock
  637. /*
  638. * Created on $timestamp from UCD data files with version info:
  639. *
  640. $versionInfo
  641. *
  642. * * * * * This file contains MACHINE-GENERATED DATA, do not edit! * * * * *
  643. */
  644. #ifndef NS_UNICODE_SCRIPT_CODES
  645. #define NS_UNICODE_SCRIPT_CODES
  646. __END
  647. print DATA_TABLES "#if !ENABLE_INTL_API\n";
  648. print DATA_TABLES "static const uint32_t sScriptCodeToTag[] = {\n";
  649. for (my $i = 0; $i < scalar @scriptCodeToTag; ++$i) {
  650. printf DATA_TABLES " HB_TAG('%c','%c','%c','%c')", unpack('cccc', $scriptCodeToTag[$i]);
  651. print DATA_TABLES $i < $#scriptCodeToTag ? ",\n" : "\n";
  652. }
  653. print DATA_TABLES "};\n";
  654. print DATA_TABLES "#endif\n\n";
  655. our $totalData = 0;
  656. print DATA_TABLES "#if !ENABLE_INTL_API\n";
  657. print DATA_TABLES "static const int16_t sMirrorOffsets[] = {\n";
  658. for (my $i = 0; $i < scalar @offsets; ++$i) {
  659. printf DATA_TABLES " $offsets[$i]";
  660. print DATA_TABLES $i < $#offsets ? ",\n" : "\n";
  661. }
  662. print DATA_TABLES "};\n";
  663. print DATA_TABLES "#endif\n\n";
  664. print HEADER "#pragma pack(1)\n\n";
  665. sub sprintCharProps1
  666. {
  667. my $usv = shift;
  668. return sprintf("{%d,%d,%d}, ", $mirror[$usv], $hangul[$usv], $combining[$usv]);
  669. }
  670. my $type = q/
  671. struct nsCharProps1 {
  672. unsigned char mMirrorOffsetIndex:5;
  673. unsigned char mHangulType:3;
  674. unsigned char mCombiningClass:8;
  675. };
  676. /;
  677. &genTables("#if !ENABLE_INTL_API", "#endif",
  678. "CharProp1", $type, "nsCharProps1", 11, 5, \&sprintCharProps1, 1, 2, 1);
  679. sub sprintCharProps2_short
  680. {
  681. my $usv = shift;
  682. return sprintf("{%d,%d},",
  683. $verticalOrientation[$usv], $xidmod[$usv]);
  684. }
  685. $type = q|
  686. struct nsCharProps2 {
  687. // Currently only 6 bits are defined here, so 2 more could be added without
  688. // affecting the storage requirements for this struct.
  689. unsigned char mVertOrient:2;
  690. unsigned char mXidmod:4;
  691. };
  692. |;
  693. &genTables("#if ENABLE_INTL_API", "#endif",
  694. "CharProp2", $type, "nsCharProps2", 9, 7, \&sprintCharProps2_short, 16, 1, 1);
  695. sub sprintCharProps2_full
  696. {
  697. my $usv = shift;
  698. return sprintf("{%d,%d,%d,%d,%d,%d,%d,%d,%d},",
  699. $script[$usv], $pairedBracketType[$usv],
  700. $eastAsianWidthFWH[$usv], $category[$usv],
  701. $bidicategory[$usv], $xidmod[$usv], $numericvalue[$usv],
  702. $verticalOrientation[$usv], $lineBreak[$usv]);
  703. }
  704. $type = q|
  705. struct nsCharProps2 {
  706. unsigned char mScriptCode:8;
  707. unsigned char mPairedBracketType:2;
  708. unsigned char mEastAsianWidthFWH:1;
  709. unsigned char mCategory:5;
  710. unsigned char mBidiCategory:5;
  711. unsigned char mXidmod:4;
  712. signed char mNumericValue:5;
  713. unsigned char mVertOrient:2;
  714. unsigned char mLineBreak; // only 6 bits actually needed
  715. };
  716. |;
  717. &genTables("#if !ENABLE_INTL_API", "#endif",
  718. "CharProp2", $type, "nsCharProps2", 12, 4, \&sprintCharProps2_full, 16, 5, 1);
  719. print HEADER "#pragma pack()\n\n";
  720. sub sprintHanVariants
  721. {
  722. my $baseUsv = shift;
  723. my $varShift = 0;
  724. my $val = 0;
  725. while ($varShift < 8) {
  726. $val |= $hanVariant[$baseUsv++] << $varShift;
  727. $varShift += 2;
  728. }
  729. return sprintf("0x%02x,", $val);
  730. }
  731. ## Han Variant data currently unused but may be needed in future, see bug 857481
  732. ## &genTables("", "", "HanVariant", "", "uint8_t", 9, 7, \&sprintHanVariants, 2, 1, 4);
  733. sub sprintFullWidth
  734. {
  735. my $usv = shift;
  736. return sprintf("0x%04x,", $fullWidth[$usv]);
  737. }
  738. &genTables("", "", "FullWidth", "", "uint16_t", 10, 6, \&sprintFullWidth, 0, 2, 1);
  739. sub sprintFullWidthInverse
  740. {
  741. my $usv = shift;
  742. return sprintf("0x%04x,", $fullWidthInverse[$usv]);
  743. }
  744. &genTables("", "", "FullWidthInverse", "", "uint16_t", 10, 6, \&sprintFullWidthInverse, 0, 2, 1);
  745. sub sprintCasemap
  746. {
  747. my $usv = shift;
  748. return sprintf("0x%08x,", $casemap[$usv]);
  749. }
  750. &genTables("#if !ENABLE_INTL_API", "#endif",
  751. "CaseMap", "", "uint32_t", 11, 5, \&sprintCasemap, 1, 4, 1);
  752. print STDERR "Total data = $totalData\n";
  753. printf DATA_TABLES "const uint32_t kTitleToUpper = 0x%08x;\n", $kTitleToUpper;
  754. printf DATA_TABLES "const uint32_t kUpperToLower = 0x%08x;\n", $kUpperToLower;
  755. printf DATA_TABLES "const uint32_t kLowerToTitle = 0x%08x;\n", $kLowerToTitle;
  756. printf DATA_TABLES "const uint32_t kLowerToUpper = 0x%08x;\n", $kLowerToUpper;
  757. printf DATA_TABLES "const uint32_t kCaseMapCharMask = 0x%08x;\n\n", $kCaseMapCharMask;
  758. sub genTables
  759. {
  760. my ($guardBegin, $guardEnd,
  761. $prefix, $typedef, $type, $indexBits, $charBits, $func, $maxPlane, $bytesPerEntry, $charsPerEntry) = @_;
  762. if ($typedef ne '') {
  763. print HEADER "$guardBegin\n";
  764. print HEADER "$typedef\n";
  765. print HEADER "$guardEnd\n\n";
  766. }
  767. print DATA_TABLES "\n$guardBegin\n";
  768. print DATA_TABLES "#define k${prefix}MaxPlane $maxPlane\n";
  769. print DATA_TABLES "#define k${prefix}IndexBits $indexBits\n";
  770. print DATA_TABLES "#define k${prefix}CharBits $charBits\n";
  771. my $indexLen = 1 << $indexBits;
  772. my $charsPerPage = 1 << $charBits;
  773. my %charIndex = ();
  774. my %pageMapIndex = ();
  775. my @pageMap = ();
  776. my @char = ();
  777. my $planeMap = "\x00" x $maxPlane;
  778. foreach my $plane (0 .. $maxPlane) {
  779. my $pageMap = "\x00" x $indexLen * 2;
  780. foreach my $page (0 .. $indexLen - 1) {
  781. my $charValues = "";
  782. for (my $ch = 0; $ch < $charsPerPage; $ch += $charsPerEntry) {
  783. my $usv = $plane * 0x10000 + $page * $charsPerPage + $ch;
  784. $charValues .= &$func($usv);
  785. }
  786. chop $charValues;
  787. unless (exists $charIndex{$charValues}) {
  788. $charIndex{$charValues} = scalar keys %charIndex;
  789. $char[$charIndex{$charValues}] = $charValues;
  790. }
  791. substr($pageMap, $page * 2, 2) = pack('S', $charIndex{$charValues});
  792. }
  793. unless (exists $pageMapIndex{$pageMap}) {
  794. $pageMapIndex{$pageMap} = scalar keys %pageMapIndex;
  795. $pageMap[$pageMapIndex{$pageMap}] = $pageMap;
  796. }
  797. if ($plane > 0) {
  798. substr($planeMap, $plane - 1, 1) = pack('C', $pageMapIndex{$pageMap});
  799. }
  800. }
  801. if ($maxPlane) {
  802. print DATA_TABLES "static const uint8_t s${prefix}Planes[$maxPlane] = {";
  803. print DATA_TABLES join(',', map { sprintf("%d", $_) } unpack('C*', $planeMap));
  804. print DATA_TABLES "};\n\n";
  805. }
  806. my $chCount = scalar @char;
  807. my $pmBits = $chCount > 255 ? 16 : 8;
  808. my $pmCount = scalar @pageMap;
  809. if ($maxPlane == 0) {
  810. die "there should only be one pageMap entry!" if $pmCount > 1;
  811. print DATA_TABLES "static const uint${pmBits}_t s${prefix}Pages[$indexLen] = {\n";
  812. } else {
  813. print DATA_TABLES "static const uint${pmBits}_t s${prefix}Pages[$pmCount][$indexLen] = {\n";
  814. }
  815. for (my $i = 0; $i < scalar @pageMap; ++$i) {
  816. print DATA_TABLES $maxPlane > 0 ? " {" : " ";
  817. print DATA_TABLES join(',', map { sprintf("%d", $_) } unpack('S*', $pageMap[$i]));
  818. print DATA_TABLES $maxPlane > 0 ? ($i < $#pageMap ? "},\n" : "}\n") : "\n";
  819. }
  820. print DATA_TABLES "};\n\n";
  821. my $pageLen = $charsPerPage / $charsPerEntry;
  822. print DATA_TABLES "static const $type s${prefix}Values[$chCount][$pageLen] = {\n";
  823. for (my $i = 0; $i < scalar @char; ++$i) {
  824. print DATA_TABLES " {";
  825. print DATA_TABLES $char[$i];
  826. print DATA_TABLES $i < $#char ? "},\n" : "}\n";
  827. }
  828. print DATA_TABLES "};\n";
  829. print DATA_TABLES "$guardEnd\n";
  830. my $dataSize = $pmCount * $indexLen * $pmBits/8 +
  831. $chCount * $pageLen * $bytesPerEntry +
  832. $maxPlane;
  833. $totalData += $dataSize;
  834. print STDERR "Data for $prefix = $dataSize\n";
  835. }
  836. print DATA_TABLES <<__END;
  837. /*
  838. * * * * * This file contains MACHINE-GENERATED DATA, do not edit! * * * * *
  839. */
  840. __END
  841. close DATA_TABLES;
  842. print HEADER "namespace mozilla {\n";
  843. print HEADER "namespace unicode {\n";
  844. print HEADER "enum class Script {\n";
  845. for (my $i = 0; $i < scalar @scriptCodeToName; ++$i) {
  846. print HEADER " ", $scriptCodeToName[$i], " = ", $i, ",\n";
  847. }
  848. print HEADER "\n NUM_SCRIPT_CODES = ", scalar @scriptCodeToName, ",\n";
  849. print HEADER "\n INVALID = -1\n";
  850. print HEADER "};\n";
  851. print HEADER "} // namespace unicode\n";
  852. print HEADER "} // namespace mozilla\n\n";
  853. print HEADER <<__END;
  854. #endif
  855. /*
  856. * * * * * This file contains MACHINE-GENERATED DATA, do not edit! * * * * *
  857. */
  858. __END
  859. close HEADER;