make_unicode 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853
  1. #!/usr/bin/perl -w
  2. #
  3. # Generate code page .c files from ftp.unicode.org descriptions
  4. #
  5. # Copyright 2000 Alexandre Julliard
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. #
  21. use strict;
  22. # base URLs for www.unicode.org files
  23. my $UNIVERSION = "14.0.0";
  24. my $UNIDATA = "https://www.unicode.org/Public/$UNIVERSION/ucd/UCD.zip";
  25. my $IDNADATA = "https://www.unicode.org/Public/idna/$UNIVERSION";
  26. my $JISDATA = "https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS";
  27. my $KSCDATA = "https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/KSC";
  28. my $REPORTS = "http://www.unicode.org/reports";
  29. my $MSDATA = "https://download.microsoft.com/download/C/F/7/CF713A5E-9FBC-4FD6-9246-275F65C0E498";
  30. my $MSCODEPAGES = "$MSDATA/Windows Supported Code Page Data Files.zip";
  31. # Sort keys file
  32. my $SORTKEYS = "tr10/allkeys.txt";
  33. # Default char for undefined mappings
  34. my $DEF_CHAR = ord '?';
  35. # Last valid Unicode character
  36. my $MAX_CHAR = 0x10ffff;
  37. my @allfiles =
  38. (
  39. "CodpageFiles/037.txt",
  40. "CodpageFiles/437.txt",
  41. "CodpageFiles/500.txt",
  42. "CodpageFiles/708.txt",
  43. "CodpageFiles/720.txt",
  44. "CodpageFiles/737.txt",
  45. "CodpageFiles/775.txt",
  46. "CodpageFiles/850.txt",
  47. "CodpageFiles/852.txt",
  48. "CodpageFiles/855.txt",
  49. "CodpageFiles/857.txt",
  50. "CodpageFiles/860.txt",
  51. "CodpageFiles/861.txt",
  52. "CodpageFiles/862.txt",
  53. "CodpageFiles/863.txt",
  54. "CodpageFiles/864.txt",
  55. "CodpageFiles/865.txt",
  56. "CodpageFiles/866.txt",
  57. "CodpageFiles/869.txt",
  58. "CodpageFiles/874.txt",
  59. "CodpageFiles/875.txt",
  60. "CodpageFiles/932.txt",
  61. "CodpageFiles/936.txt",
  62. "CodpageFiles/949.txt",
  63. "CodpageFiles/950.txt",
  64. "CodpageFiles/1026.txt",
  65. "CodpageFiles/1250.txt",
  66. "CodpageFiles/1251.txt",
  67. "CodpageFiles/1252.txt",
  68. "CodpageFiles/1253.txt",
  69. "CodpageFiles/1254.txt",
  70. "CodpageFiles/1255.txt",
  71. "CodpageFiles/1256.txt",
  72. "CodpageFiles/1257.txt",
  73. "CodpageFiles/1258.txt",
  74. "CodpageFiles/1361.txt",
  75. "CodpageFiles/10000.txt",
  76. "CodpageFiles/10001.txt",
  77. "CodpageFiles/10002.txt",
  78. "CodpageFiles/10003.txt",
  79. "CodpageFiles/10004.txt",
  80. "CodpageFiles/10005.txt",
  81. "CodpageFiles/10006.txt",
  82. "CodpageFiles/10007.txt",
  83. "CodpageFiles/10008.txt",
  84. "CodpageFiles/10010.txt",
  85. "CodpageFiles/10017.txt",
  86. "CodpageFiles/10021.txt",
  87. "CodpageFiles/10029.txt",
  88. "CodpageFiles/10079.txt",
  89. "CodpageFiles/10081.txt",
  90. "CodpageFiles/10082.txt",
  91. "CodpageFiles/20127.txt",
  92. "CodpageFiles/20866.txt",
  93. "CodpageFiles/21866.txt",
  94. "CodpageFiles/28591.txt",
  95. "CodpageFiles/28592.txt",
  96. "CodpageFiles/28593.txt",
  97. "CodpageFiles/28594.txt",
  98. "CodpageFiles/28595.txt",
  99. "CodpageFiles/28596.txt",
  100. "CodpageFiles/28597.txt",
  101. "CodpageFiles/28598.txt",
  102. "CodpageFiles/28599.txt",
  103. "CodpageFiles/28603.txt",
  104. "CodpageFiles/28605.txt",
  105. );
  106. my %ctype =
  107. (
  108. # CT_CTYPE1
  109. "upper" => 0x0001,
  110. "lower" => 0x0002,
  111. "digit" => 0x0004,
  112. "space" => 0x0008,
  113. "punct" => 0x0010,
  114. "cntrl" => 0x0020,
  115. "blank" => 0x0040,
  116. "xdigit" => 0x0080,
  117. "alpha" => 0x0100 | 0x80000000,
  118. "defin" => 0x0200,
  119. # CT_CTYPE3 in high 16 bits
  120. "nonspacing" => 0x00010000,
  121. "diacritic" => 0x00020000,
  122. "vowelmark" => 0x00040000,
  123. "symbol" => 0x00080000,
  124. "katakana" => 0x00100000,
  125. "hiragana" => 0x00200000,
  126. "halfwidth" => 0x00400000,
  127. "fullwidth" => 0x00800000,
  128. "ideograph" => 0x01000000,
  129. "kashida" => 0x02000000,
  130. "lexical" => 0x04000000,
  131. "highsurrogate" => 0x08000000,
  132. "lowsurrogate" => 0x10000000,
  133. );
  134. my %bracket_types =
  135. (
  136. "o" => 0x0000,
  137. "c" => 0x0001,
  138. );
  139. my %indic_types =
  140. (
  141. "Other" => 0x0000,
  142. "Bindu" => 0x0001,
  143. "Visarga" => 0x0002,
  144. "Avagraha" => 0x0003,
  145. "Nukta" => 0x0004,
  146. "Virama" => 0x0005,
  147. "Vowel_Independent" => 0x0006,
  148. "Vowel_Dependent" => 0x0007,
  149. "Vowel" => 0x0008,
  150. "Consonant_Placeholder" => 0x0009,
  151. "Consonant" => 0x000a,
  152. "Consonant_Dead" => 0x000b,
  153. "Consonant_Succeeding_Repha" => 0x000c,
  154. "Consonant_Subjoined" => 0x000d,
  155. "Consonant_Medial" => 0x000e,
  156. "Consonant_Final" => 0x000f,
  157. "Consonant_Head_Letter" => 0x0010,
  158. "Modifying_Letter" => 0x0011,
  159. "Tone_Letter" => 0x0012,
  160. "Tone_Mark" => 0x0013,
  161. "Register_Shifter" => 0x0014,
  162. "Consonant_Preceding_Repha" => 0x0015,
  163. "Pure_Killer" => 0x0016,
  164. "Invisible_Stacker" => 0x0017,
  165. "Gemination_Mark" => 0x0018,
  166. "Cantillation_Mark" => 0x0019,
  167. "Non_Joiner" => 0x001a,
  168. "Joiner" => 0x001b,
  169. "Number_Joiner" => 0x001c,
  170. "Number" => 0x001d,
  171. "Brahmi_Joining_Number" => 0x001e,
  172. "Consonant_With_Stacker" => 0x001f,
  173. "Consonant_Prefixed" => 0x0020,
  174. "Syllable_Modifier" => 0x0021,
  175. "Consonant_Killer" => 0x0022,
  176. "Consonant_Initial_Postfixed" => 0x0023,
  177. );
  178. my %matra_types =
  179. (
  180. "Right" => 0x01,
  181. "Left" => 0x02,
  182. "Visual_Order_Left" => 0x03,
  183. "Left_And_Right" => 0x04,
  184. "Top" => 0x05,
  185. "Bottom" => 0x06,
  186. "Top_And_Bottom" => 0x07,
  187. "Top_And_Right" => 0x08,
  188. "Top_And_Left" => 0x09,
  189. "Top_And_Left_And_Right" => 0x0a,
  190. "Bottom_And_Right" => 0x0b,
  191. "Top_And_Bottom_And_Right" => 0x0c,
  192. "Overstruck" => 0x0d,
  193. "Invisible" => 0x0e,
  194. "Bottom_And_Left" => 0x0f,
  195. "Top_And_Bottom_And_Left" => 0x10,
  196. );
  197. my %break_types =
  198. (
  199. "BK" => 0x0001,
  200. "CR" => 0x0002,
  201. "LF" => 0x0003,
  202. "CM" => 0x0004,
  203. "SG" => 0x0005,
  204. "GL" => 0x0006,
  205. "CB" => 0x0007,
  206. "SP" => 0x0008,
  207. "ZW" => 0x0009,
  208. "NL" => 0x000a,
  209. "WJ" => 0x000b,
  210. "JL" => 0x000c,
  211. "JV" => 0x000d,
  212. "JT" => 0x000e,
  213. "H2" => 0x000f,
  214. "H3" => 0x0010,
  215. "XX" => 0x0011,
  216. "OP" => 0x0012,
  217. "CL" => 0x0013,
  218. "CP" => 0x0014,
  219. "QU" => 0x0015,
  220. "NS" => 0x0016,
  221. "EX" => 0x0017,
  222. "SY" => 0x0018,
  223. "IS" => 0x0019,
  224. "PR" => 0x001a,
  225. "PO" => 0x001b,
  226. "NU" => 0x001c,
  227. "AL" => 0x001d,
  228. "ID" => 0x001e,
  229. "IN" => 0x001f,
  230. "HY" => 0x0020,
  231. "BB" => 0x0021,
  232. "BA" => 0x0022,
  233. "SA" => 0x0023,
  234. "AI" => 0x0024,
  235. "B2" => 0x0025,
  236. "HL" => 0x0026,
  237. "CJ" => 0x0027,
  238. "RI" => 0x0028,
  239. "EB" => 0x0029,
  240. "EM" => 0x002a,
  241. "ZWJ" => 0x002b,
  242. );
  243. my %vertical_types =
  244. (
  245. "R" => 0x0000,
  246. "U" => 0x0001,
  247. "Tr" => 0x0002,
  248. "Tu" => 0x0003,
  249. );
  250. my %categories =
  251. (
  252. "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
  253. "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
  254. "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"}, # Letter, Titlecase
  255. "Mn" => $ctype{"defin"}|$ctype{"nonspacing"}, # Mark, Non-Spacing
  256. "Mc" => $ctype{"defin"}, # Mark, Spacing Combining
  257. "Me" => $ctype{"defin"}, # Mark, Enclosing
  258. "Nd" => $ctype{"defin"}|$ctype{"digit"}, # Number, Decimal Digit
  259. "Nl" => $ctype{"defin"}|$ctype{"alpha"}, # Number, Letter
  260. "No" => $ctype{"defin"}, # Number, Other
  261. "Zs" => $ctype{"defin"}|$ctype{"space"}, # Separator, Space
  262. "Zl" => $ctype{"defin"}|$ctype{"space"}, # Separator, Line
  263. "Zp" => $ctype{"defin"}|$ctype{"space"}, # Separator, Paragraph
  264. "Cc" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Control
  265. "Cf" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Format
  266. "Cs" => $ctype{"defin"}, # Other, Surrogate
  267. "Co" => $ctype{"defin"}, # Other, Private Use
  268. "Cn" => $ctype{"defin"}, # Other, Not Assigned
  269. "Lm" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Modifier
  270. "Lo" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Other
  271. "Pc" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Connector
  272. "Pd" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Dash
  273. "Ps" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Open
  274. "Pe" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Close
  275. "Pi" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Initial quote
  276. "Pf" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Final quote
  277. "Po" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Other
  278. "Sm" => $ctype{"defin"}|$ctype{"symbol"}, # Symbol, Math
  279. "Sc" => $ctype{"defin"}|$ctype{"symbol"}, # Symbol, Currency
  280. "Sk" => $ctype{"defin"}|$ctype{"symbol"}, # Symbol, Modifier
  281. "So" => $ctype{"defin"}|$ctype{"symbol"} # Symbol, Other
  282. );
  283. # a few characters need additional categories that cannot be determined automatically
  284. my %special_categories =
  285. (
  286. "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
  287. 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
  288. "space" => [ 0x09..0x0d, 0x85 ],
  289. "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
  290. "cntrl" => [ 0x070f, 0x200c, 0x200d,
  291. 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
  292. 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
  293. 0xfff9, 0xfffa, 0xfffb ],
  294. "punct" => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
  295. 0xd7, 0xf7 ],
  296. "digit" => [ 0xb2, 0xb3, 0xb9 ],
  297. "lower" => [ 0xaa, 0xba, 0x2071, 0x207f ],
  298. "nonspacing" => [ 0xc0..0xc5, 0xc7..0xcf, 0xd1..0xd6, 0xd8..0xdd, 0xe0..0xe5, 0xe7..0xef,
  299. 0xf1..0xf6, 0xf8..0xfd, 0xff, 0x6de, 0x1929..0x192b, 0x302e..0x302f ],
  300. "diacritic" => [ 0x5e, 0x60, 0xb7, 0xd8, 0xf8 ],
  301. "symbol" => [ 0x09..0x0d, 0x20..0x23, 0x25, 0x26, 0x28..0x2a, 0x2c, 0x2e..0x2f, 0x3a..0x40,
  302. 0x5b..0x60, 0x7b..0x7e, 0xa0..0xa9, 0xab..0xb1, 0xb4..0xb8, 0xbb, 0xbf,
  303. 0x02b9..0x02ba, 0x02c6..0x02cf ],
  304. "halfwidth" => [ 0x20..0x7e, 0xa2..0xa3, 0xa5..0xa6, 0xac, 0xaf, 0x20a9 ],
  305. "fullwidth" => [ 0x2018..0x2019, 0x201c..0x201d, 0x3000..0x3002, 0x300c..0x300d, 0x309b..0x309c,
  306. 0x30a1..0x30ab, 0x30ad, 0x30ad, 0x30af, 0x30b1, 0x30b3, 0x30b5, 0x30b7, 0x30b9,
  307. 0x30bb, 0x30bd, 0x30bf, 0x30c1, 0x30c3, 0x30c4, 0x30c6, 0x30c8, 0x30ca..0x30cf,
  308. 0x30d2, 0x30d5, 0x30d8, 0x30db, 0x30de..0x30ed, 0x30ef, 0x30f2..0x30f3, 0x30fb,
  309. 0x3131..0x3164 ],
  310. "ideograph" => [ 0x3006..0x3007 ],
  311. "lexical" => [ 0x22, 0x24, 0x27, 0x2d, 0x2f, 0x3d, 0x40, 0x5c, 0x5e..0x60, 0x7e,
  312. 0xa8, 0xaa, 0xad, 0xaf, 0xb4, 0xb8, 0xba,
  313. 0x02b0..0x02b8, 0x02bc, 0x02c7, 0x02ca..0x02cb, 0x02cf, 0x02d8..0x02dd, 0x02e0..0x02e3,
  314. 0x037a, 0x0384..0x0385, 0x0387, 0x0559..0x055a, 0x0640, 0x1fbd..0x1fc1,
  315. 0x1fcd..0x1fcf, 0x1fdd..0x1fdf, 0x1fed..0x1fef, 0x1ffd..0x1ffe, 0x2010..0x2015,
  316. 0x2032..0x2034, 0x2038, 0x2043..0x2044, 0x207b..0x207c, 0x207f, 0x208b..0x208c,
  317. 0x2212, 0x2215..0x2216, 0x2500, 0x2504..0x2505, 0x2508..0x2509, 0x254c..0x254d,
  318. 0x3003, 0x301c, 0x3030..0x3035, 0x309b..0x309e, 0x30fd..0x30fe, 0xfe31..0xfe32,
  319. 0xfe58, 0xfe63, 0xfe66, 0xfe68..0xfe69, 0xfe6b, 0xff04, 0xff07, 0xff0d, 0xff0f,
  320. 0xff1d, 0xff20, 0xff3c, 0xff3e, 0xff40, 0xff5e ],
  321. "kashida" => [ 0x0640 ],
  322. );
  323. my %directions =
  324. (
  325. "L" => 1, # Left-to-Right
  326. "R" => 2, # Right-to-Left
  327. "AL" => 12, # Right-to-Left Arabic
  328. "EN" => 3, # European Number
  329. "ES" => 4, # European Number Separator
  330. "ET" => 5, # European Number Terminator
  331. "AN" => 6, # Arabic Number
  332. "CS" => 7, # Common Number Separator
  333. "NSM" => 13, # Non-Spacing Mark
  334. "BN" => 14, # Boundary Neutral
  335. "B" => 8, # Paragraph Separator
  336. "S" => 9, # Segment Separator
  337. "WS" => 10, # Whitespace
  338. "ON" => 11, # Other Neutrals
  339. "LRE" => 15, # Left-to-Right Embedding
  340. "LRO" => 15, # Left-to-Right Override
  341. "RLE" => 15, # Right-to-Left Embedding
  342. "RLO" => 15, # Right-to-Left Override
  343. "PDF" => 15, # Pop Directional Format
  344. "LRI" => 15, # Left-to-Right Isolate
  345. "RLI" => 15, # Right-to-Left Isolate
  346. "FSI" => 15, # First Strong Isolate
  347. "PDI" => 15 # Pop Directional Isolate
  348. );
  349. my %c2_types =
  350. (
  351. "L" => 1, # C2_LEFTTORIGHT
  352. "R" => 2, # C2_RIGHTTOLEFT
  353. "AL" => 2, # C2_RIGHTTOLEFT
  354. "EN" => 3, # C2_EUROPENUMBER
  355. "ES" => 4, # C2_EUROPESEPARATOR
  356. "ET" => 5, # C2_EUROPETERMINATOR
  357. "AN" => 6, # C2_ARABICNUMBER
  358. "CS" => 7, # C2_COMMONSEPARATOR
  359. "NSM" => 11, # C2_OTHERNEUTRAL
  360. "BN" => 0, # C2_NOTAPPLICABLE
  361. "B" => 8, # C2_BLOCKSEPARATOR
  362. "S" => 9, # C2_SEGMENTSEPARATOR
  363. "WS" => 10, # C2_WHITESPACE
  364. "ON" => 11, # C2_OTHERNEUTRAL
  365. "LRE" => 11, # C2_OTHERNEUTRAL
  366. "LRO" => 11, # C2_OTHERNEUTRAL
  367. "RLE" => 11, # C2_OTHERNEUTRAL
  368. "RLO" => 11, # C2_OTHERNEUTRAL
  369. "PDF" => 11, # C2_OTHERNEUTRAL
  370. "LRI" => 11, # C2_OTHERNEUTRAL
  371. "RLI" => 11, # C2_OTHERNEUTRAL
  372. "FSI" => 11, # C2_OTHERNEUTRAL
  373. "PDI" => 11 # C2_OTHERNEUTRAL
  374. );
  375. my %bidi_types =
  376. (
  377. "ON" => 0, # Other Neutrals
  378. "L" => 1, # Left-to-Right
  379. "R" => 2, # Right-to-Left
  380. "AN" => 3, # Arabic Number
  381. "EN" => 4, # European Number
  382. "AL" => 5, # Right-to-Left Arabic
  383. "NSM" => 6, # Non-Spacing Mark
  384. "CS" => 7, # Common Number Separator
  385. "ES" => 8, # European Number Separator
  386. "ET" => 9, # European Number Terminator
  387. "BN" => 10, # Boundary Neutral
  388. "S" => 11, # Segment Separator
  389. "WS" => 12, # Whitespace
  390. "B" => 13, # Paragraph Separator
  391. "RLO" => 14, # Right-to-Left Override
  392. "RLE" => 15, # Right-to-Left Embedding
  393. "LRO" => 16, # Left-to-Right Override
  394. "LRE" => 17, # Left-to-Right Embedding
  395. "PDF" => 18, # Pop Directional Format
  396. "LRI" => 19, # Left-to-Right Isolate
  397. "RLI" => 20, # Right-to-Left Isolate
  398. "FSI" => 21, # First Strong Isolate
  399. "PDI" => 22 # Pop Directional Isolate
  400. );
  401. my %joining_types =
  402. (
  403. "U" => 0, # Non_Joining
  404. "L" => 1, # Left_Joining
  405. "R" => 2, # Right_Joining
  406. "D" => 3, # Dual_Joining
  407. "C" => 3, # Join_Causing
  408. "ALAPH" => 4, # Syriac ALAPH
  409. "DALATH RISH" => 5, # Syriac DALATH RISH group
  410. "T" => 6, # Transparent
  411. );
  412. my @cp2uni = ();
  413. my @glyph2uni = ();
  414. my @lead_bytes = ();
  415. my @uni2cp = ();
  416. my @tolower_table = ();
  417. my @toupper_table = ();
  418. my @digitmap_table = ();
  419. my @category_table = ();
  420. my @initial_joining_table = ();
  421. my @direction_table = ();
  422. my @decomp_table = ();
  423. my @combining_class_table = ();
  424. my @decomp_compat_table = ();
  425. my @comp_exclusions = ();
  426. my @idna_decomp_table = ();
  427. my @idna_disallowed = ();
  428. my %registry_keys;
  429. my $default_char;
  430. my $default_wchar;
  431. my %joining_forms =
  432. (
  433. "isolated" => [],
  434. "final" => [],
  435. "initial" => [],
  436. "medial" => []
  437. );
  438. sub to_utf16(@)
  439. {
  440. my @ret;
  441. foreach my $ch (@_)
  442. {
  443. if ($ch < 0x10000)
  444. {
  445. push @ret, $ch;
  446. }
  447. else
  448. {
  449. my $val = $ch - 0x10000;
  450. push @ret, 0xd800 | ($val >> 10), 0xdc00 | ($val & 0x3ff);
  451. }
  452. }
  453. return @ret;
  454. }
  455. ################################################################
  456. # fetch a unicode.org file and open it
  457. sub open_data_file($$)
  458. {
  459. my ($base, $name) = @_;
  460. my $cache = ($ENV{XDG_CACHE_HOME} || "$ENV{HOME}/.cache") . "/wine";
  461. (my $dir = "$cache/$name") =~ s/\/[^\/]+$//;
  462. my $suffix = ($base =~ /\/\Q$UNIVERSION\E/) ? "-$UNIVERSION" : "";
  463. local *FILE;
  464. if ($base =~ /.*\/([^\/]+)\.zip$/)
  465. {
  466. my $zip = "$1$suffix.zip";
  467. unless (-f "$cache/$zip")
  468. {
  469. system "mkdir", "-p", $cache;
  470. print "Fetching $base...\n";
  471. !system "wget", "-q", "-O", "$cache/$zip", $base or die "cannot fetch $base";
  472. }
  473. open FILE, "-|", "unzip", "-p", "$cache/$zip", $name or die "cannot extract $name from $zip";
  474. }
  475. else
  476. {
  477. (my $dest = "$cache/$name") =~ s/(.*)(\.[^\/.]+)$/$1$suffix$2/;
  478. unless (-f $dest)
  479. {
  480. system "mkdir", "-p", $dir;
  481. print "Fetching $base/$name...\n";
  482. !system "wget", "-q", "-O", $dest, "$base/$name" or die "cannot fetch $base/$name";
  483. }
  484. open FILE, "<$dest" or die "cannot open $dest";
  485. }
  486. return *FILE;
  487. }
  488. ################################################################
  489. # recursively get the decomposition for a character
  490. sub get_decomposition($$);
  491. sub get_decomposition($$)
  492. {
  493. my ($char, $table) = @_;
  494. my @ret;
  495. return $char unless defined $table->[$char];
  496. foreach my $ch (@{$table->[$char]})
  497. {
  498. push @ret, get_decomposition( $ch, $table );
  499. }
  500. return @ret;
  501. }
  502. ################################################################
  503. # get the composition that results in a given character
  504. sub get_composition($$)
  505. {
  506. my ($ch, $compat) = @_;
  507. return () unless defined $decomp_table[$ch]; # no decomposition
  508. my @ret = @{$decomp_table[$ch]};
  509. return () if @ret < 2; # singleton decomposition
  510. return () if $comp_exclusions[$ch]; # composition exclusion
  511. return () if $combining_class_table[$ch]; # non-starter
  512. return () if $combining_class_table[$ret[0]]; # first char is non-starter
  513. return () if $compat == 1 && !defined $decomp_table[$ret[0]] &&
  514. defined $decomp_compat_table[$ret[0]]; # first char has compat decomposition
  515. return () if $compat == 2 && !defined $decomp_table[$ret[0]] &&
  516. defined $idna_decomp_table[$ret[0]]; # first char has IDNA decomposition
  517. return () if $compat == 2 && defined $idna_decomp_table[$ret[0]] &&
  518. defined $idna_decomp_table[$idna_decomp_table[$ret[0]]->[0]]; # first char's decomposition has IDNA decomposition
  519. return () if $compat == 2 && defined $idna_decomp_table[$ret[1]]; # second char has IDNA decomposition
  520. return @ret;
  521. }
  522. ################################################################
  523. # recursively build decompositions
  524. sub build_decompositions(@)
  525. {
  526. my @src = @_;
  527. my @dst;
  528. for (my $i = 0; $i < @src; $i++)
  529. {
  530. next unless defined $src[$i];
  531. my @decomp = to_utf16( get_decomposition( $i, \@src ));
  532. $dst[$i] = \@decomp;
  533. }
  534. return @dst;
  535. }
  536. ################################################################
  537. # compose Hangul sequences
  538. sub compose_hangul(@)
  539. {
  540. my $SBASE = 0xac00;
  541. my $LBASE = 0x1100;
  542. my $VBASE = 0x1161;
  543. my $TBASE = 0x11a7;
  544. my $LCOUNT = 19;
  545. my $VCOUNT = 21;
  546. my $TCOUNT = 28;
  547. my $NCOUNT = $VCOUNT * $TCOUNT;
  548. my $SCOUNT = $LCOUNT * $NCOUNT;
  549. my @seq = @_;
  550. my @ret;
  551. my $i;
  552. for ($i = 0; $i < @seq; $i++)
  553. {
  554. my $ch = $seq[$i];
  555. if ($ch >= $LBASE && $ch < $LBASE + $LCOUNT && $i < @seq - 1 &&
  556. $seq[$i+1] >= $VBASE && $seq[$i+1] < $VBASE + $VCOUNT)
  557. {
  558. $ch = $SBASE + (($seq[$i] - $LBASE) * $VCOUNT + ($seq[$i+1] - $VBASE)) * $TCOUNT;
  559. $i++;
  560. }
  561. if ($ch >= $SBASE && $ch < $SBASE + $SCOUNT && !(($ch - $SBASE) % $TCOUNT) && $i < @seq - 1 &&
  562. $seq[$i+1] > $TBASE && $seq[$i+1] < $TBASE + $TCOUNT)
  563. {
  564. $ch += $seq[$i+1] - $TBASE;
  565. $i++;
  566. }
  567. push @ret, $ch;
  568. }
  569. return @ret;
  570. }
  571. ################################################################
  572. # remove linguistic-only mappings from the case table
  573. sub remove_linguistic_mappings($$)
  574. {
  575. my ($upper, $lower) = @_;
  576. # remove case mappings that don't round-trip
  577. for (my $i = 0; $i < @{$upper}; $i++)
  578. {
  579. next unless defined ${$upper}[$i];
  580. my $ch = ${$upper}[$i];
  581. ${$upper}[$i] = undef unless defined ${$lower}[$ch] && ${$lower}[$ch] == $i;
  582. }
  583. for (my $i = 0; $i < @{$lower}; $i++)
  584. {
  585. next unless defined ${$lower}[$i];
  586. my $ch = ${$lower}[$i];
  587. ${$lower}[$i] = undef unless defined ${$upper}[$ch] && ${$upper}[$ch] == $i;
  588. }
  589. }
  590. ################################################################
  591. # read in the Unicode database files
  592. sub load_data()
  593. {
  594. my $start;
  595. # now build mappings from the decomposition field of the Unicode database
  596. my $UNICODE_DATA = open_data_file( $UNIDATA, "UnicodeData.txt" );
  597. while (<$UNICODE_DATA>)
  598. {
  599. # Decode the fields ...
  600. my ($code, $name, $cat, $comb, $bidi,
  601. $decomp, $dec, $dig, $num, $mirror,
  602. $oldname, $comment, $upper, $lower, $title) = split /;/;
  603. my $src = hex $code;
  604. die "unknown category $cat" unless defined $categories{$cat};
  605. die "unknown directionality $bidi" unless defined $directions{$bidi};
  606. $category_table[$src] = $categories{$cat};
  607. $direction_table[$src] = $bidi;
  608. if ($cat eq "Mn" || $cat eq "Me" || $cat eq "Cf")
  609. {
  610. $initial_joining_table[$src] = $joining_types{"T"};
  611. }
  612. else
  613. {
  614. $initial_joining_table[$src] = $joining_types{"U"};
  615. }
  616. if ($lower ne "")
  617. {
  618. $tolower_table[$src] = hex $lower;
  619. }
  620. if ($upper ne "")
  621. {
  622. $toupper_table[$src] = hex $upper;
  623. }
  624. if ($dec ne "")
  625. {
  626. $category_table[$src] |= $ctype{"digit"};
  627. }
  628. if ($dig ne "")
  629. {
  630. $digitmap_table[$src] = ord $dig;
  631. }
  632. $combining_class_table[$src] = ($cat ne "Co") ? $comb : 0x100; # Private Use
  633. $category_table[$src] |= $ctype{"nonspacing"} if $bidi eq "NSM";
  634. $category_table[$src] |= $ctype{"diacritic"} if $name =~ /^(COMBINING)|(MODIFIER LETTER)\W/;
  635. $category_table[$src] |= $ctype{"vowelmark"} if $name =~ /\sVOWEL/ || $oldname =~ /\sVOWEL/;
  636. $category_table[$src] |= $ctype{"halfwidth"} if $name =~ /^HALFWIDTH\s/;
  637. $category_table[$src] |= $ctype{"fullwidth"} if $name =~ /^FULLWIDTH\s/;
  638. $category_table[$src] |= $ctype{"hiragana"} if $name =~ /(HIRAGANA)|(\WKANA\W)/;
  639. $category_table[$src] |= $ctype{"katakana"} if $name =~ /(KATAKANA)|(\WKANA\W)/;
  640. $category_table[$src] |= $ctype{"ideograph"} if $name =~ /^<CJK Ideograph/;
  641. $category_table[$src] |= $ctype{"ideograph"} if $name =~ /^CJK COMPATIBILITY IDEOGRAPH/;
  642. $category_table[$src] |= $ctype{"ideograph"} if $name =~ /^HANGZHOU/;
  643. $category_table[$src] |= $ctype{"highsurrogate"} if $name =~ /High Surrogate/;
  644. $category_table[$src] |= $ctype{"lowsurrogate"} if $name =~ /Low Surrogate/;
  645. # copy the category and direction for everything between First/Last pairs
  646. if ($name =~ /, First>/) { $start = $src; }
  647. if ($name =~ /, Last>/)
  648. {
  649. while ($start < $src)
  650. {
  651. $category_table[$start] = $category_table[$src];
  652. $direction_table[$start] = $direction_table[$src];
  653. $combining_class_table[$start] = $combining_class_table[$src];
  654. $start++;
  655. }
  656. }
  657. next if $decomp eq ""; # no decomposition, skip it
  658. if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)/)
  659. {
  660. my @seq = map { hex $_; } (split /\s+/, (split /\s+/, $decomp, 2)[1]);
  661. $decomp_compat_table[$src] = \@seq;
  662. }
  663. if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
  664. {
  665. # decomposition of the form "<foo> 1234" -> use char if type is known
  666. if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
  667. {
  668. ${joining_forms{$1}}[hex $2] = $src;
  669. }
  670. }
  671. elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
  672. {
  673. # decomposition "<compat> 0020 1234" -> combining accent
  674. }
  675. elsif ($decomp =~ /^([0-9a-fA-F]+)/)
  676. {
  677. # store decomposition
  678. if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
  679. {
  680. $decomp_table[$src] = $decomp_compat_table[$src] = [ hex $1, hex $2 ];
  681. }
  682. elsif ($decomp =~ /^([0-9a-fA-F]+)$/)
  683. {
  684. # Single char decomposition
  685. $decomp_table[$src] = $decomp_compat_table[$src] = [ hex $1 ];
  686. }
  687. }
  688. }
  689. close $UNICODE_DATA;
  690. # patch the category of some special characters
  691. for (my $i = 0; $i < @decomp_table; $i++)
  692. {
  693. next unless defined $decomp_table[$i];
  694. $category_table[$i] |= $category_table[$decomp_table[$i]->[0]];
  695. }
  696. foreach my $cat (keys %special_categories)
  697. {
  698. my $flag = $ctype{$cat};
  699. foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
  700. }
  701. for (my $i = 0; $i < @decomp_compat_table; $i++)
  702. {
  703. next unless defined $decomp_compat_table[$i];
  704. next unless @{$decomp_compat_table[$i]} == 2;
  705. $category_table[$i] |= $category_table[$decomp_compat_table[$i]->[1]] & $ctype{"diacritic"};
  706. }
  707. # load the composition exclusions
  708. my $EXCL = open_data_file( $UNIDATA, "CompositionExclusions.txt" );
  709. while (<$EXCL>)
  710. {
  711. s/\#.*//; # remove comments
  712. if (/^([0-9a-fA-F]+)\.\.([0-9a-fA-F]+)\s*$/)
  713. {
  714. foreach my $i (hex $1 .. hex $2) { $comp_exclusions[$i] = 1; }
  715. }
  716. elsif (/^([0-9a-fA-F]+)\s*$/)
  717. {
  718. $comp_exclusions[hex $1] = 1;
  719. }
  720. }
  721. close $EXCL;
  722. # load the IDNA mappings
  723. @idna_decomp_table = @decomp_compat_table;
  724. my $IDNA = open_data_file( $IDNADATA, "IdnaMappingTable.txt" );
  725. while (<$IDNA>)
  726. {
  727. s/\#.*//; # remove comments
  728. next if /^\s*$/;
  729. my ($char, $type, $mapping) = split /;/;
  730. my ($ch1, $ch2);
  731. if ($char =~ /([0-9a-fA-F]+)\.\.([0-9a-fA-F]+)/)
  732. {
  733. $ch1 = hex $1;
  734. $ch2 = hex $2;
  735. }
  736. elsif ($char =~ /([0-9a-fA-F]+)/)
  737. {
  738. $ch1 = $ch2 = hex $1;
  739. }
  740. if ($type =~ /mapped/ || $type =~ /deviation/)
  741. {
  742. $mapping =~ s/^\s*(([0-9a-fA-F]+\s+)+)\s*$/$1/;
  743. my @seq = map { hex $_; } split /\s+/, $mapping;
  744. foreach my $i ($ch1 .. $ch2) { $idna_decomp_table[$i] = @seq ? \@seq : [ 0 ]; }
  745. }
  746. elsif ($type =~ /valid/)
  747. {
  748. }
  749. elsif ($type =~ /ignored/)
  750. {
  751. foreach my $i ($ch1 .. $ch2) { $idna_decomp_table[$i] = [ 0 ]; }
  752. }
  753. elsif ($type =~ /disallowed/)
  754. {
  755. foreach my $i ($ch1 .. $ch2)
  756. {
  757. $idna_decomp_table[$i] = undef;
  758. $idna_disallowed[$i] = 1;
  759. }
  760. }
  761. }
  762. close $IDNA;
  763. }
  764. ################################################################
  765. # add a new registry key
  766. sub add_registry_key($$)
  767. {
  768. my ($key, $defval) = @_;
  769. $registry_keys{$key} = [ $defval ] unless defined $registry_keys{$key};
  770. }
  771. ################################################################
  772. # add a new registry value
  773. sub add_registry_value($$$)
  774. {
  775. my ($key, $name, $value) = @_;
  776. add_registry_key( $key, undef );
  777. push @{$registry_keys{$key}}, "'$name' = s '$value'";
  778. }
  779. ################################################################
  780. # define a new lead byte
  781. sub add_lead_byte($)
  782. {
  783. my $ch = shift;
  784. return if defined $cp2uni[$ch];
  785. push @lead_bytes, $ch;
  786. $cp2uni[$ch] = 0;
  787. }
  788. ################################################################
  789. # define a new char mapping
  790. sub add_mapping($$)
  791. {
  792. my ($cp, $uni) = @_;
  793. $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
  794. $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
  795. if ($cp > 0xff) { add_lead_byte( $cp >> 8 ); }
  796. }
  797. ################################################################
  798. # get a mapping including glyph chars for MB_USEGLYPHCHARS
  799. sub get_glyphs_mapping(@)
  800. {
  801. my @table = @_;
  802. for (my $i = 0; $i < @glyph2uni; $i++)
  803. {
  804. $table[$i] = $glyph2uni[$i] if defined $glyph2uni[$i];
  805. }
  806. return @table;
  807. }
  808. ################################################################
  809. # build EUC-JP table from the JIS 0208/0212 files
  810. sub dump_eucjp_codepage()
  811. {
  812. @cp2uni = ();
  813. @glyph2uni = ();
  814. @lead_bytes = ();
  815. @uni2cp = ();
  816. $default_char = $DEF_CHAR;
  817. $default_wchar = 0x30fb;
  818. # ASCII chars
  819. foreach my $i (0x00 .. 0x7f) { add_mapping( $i, $i ); }
  820. # lead bytes
  821. foreach my $i (0x8e, 0xa1 .. 0xfe) { add_lead_byte($i); }
  822. # JIS X 0201 right plane
  823. foreach my $i (0xa1 .. 0xdf) { add_mapping( 0x8e00 + $i, 0xfec0 + $i ); }
  824. # undefined chars
  825. foreach my $i (0x80 .. 0x8d, 0x8f .. 0x9f) { $cp2uni[$i] = $i; }
  826. $cp2uni[0xa0] = 0xf8f0;
  827. $cp2uni[0xff] = 0xf8f3;
  828. # Fix backslash conversion
  829. add_mapping( 0xa1c0, 0xff3c );
  830. # Add private mappings for rows undefined in JIS 0208/0212
  831. my $private = 0xe000;
  832. foreach my $hi (0xf5 .. 0xfe)
  833. {
  834. foreach my $lo (0xa1 .. 0xfe)
  835. {
  836. add_mapping( ($hi << 8) + $lo, $private++ );
  837. }
  838. }
  839. foreach my $hi (0xf5 .. 0xfe)
  840. {
  841. foreach my $lo (0x21 .. 0x7e)
  842. {
  843. add_mapping( ($hi << 8) + $lo, $private++ );
  844. }
  845. }
  846. my $INPUT = open_data_file( $JISDATA, "JIS0208.TXT" );
  847. while (<$INPUT>)
  848. {
  849. next if /^\#/; # skip comments
  850. next if /^$/; # skip empty lines
  851. next if /\x1a/; # skip ^Z
  852. if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
  853. {
  854. add_mapping( 0x8080 + hex $1, hex $2 );
  855. next;
  856. }
  857. die "Unrecognized line $_\n";
  858. }
  859. close $INPUT;
  860. $INPUT = open_data_file( $JISDATA, "JIS0212.TXT" );
  861. while (<$INPUT>)
  862. {
  863. next if /^\#/; # skip comments
  864. next if /^$/; # skip empty lines
  865. next if /\x1a/; # skip ^Z
  866. if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
  867. {
  868. add_mapping( 0x8000 + hex $1, hex $2 );
  869. next;
  870. }
  871. die "Unrecognized line $_\n";
  872. }
  873. close $INPUT;
  874. output_codepage_file( 20932 );
  875. }
  876. ################################################################
  877. # build Korean Wansung table from the KSX1001 file
  878. sub dump_krwansung_codepage(@)
  879. {
  880. my @cp949 = @_;
  881. @cp2uni = ();
  882. @glyph2uni = ();
  883. @lead_bytes = ();
  884. @uni2cp = ();
  885. $default_char = 0x3f;
  886. $default_wchar = 0x003f;
  887. # ASCII and undefined chars
  888. foreach my $i (0x00 .. 0x9f) { add_mapping( $i, $i ); }
  889. add_mapping( 0xa0, 0xf8e6 );
  890. add_mapping( 0xad, 0xf8e7 );
  891. add_mapping( 0xae, 0xf8e8 );
  892. add_mapping( 0xaf, 0xf8e9 );
  893. add_mapping( 0xfe, 0xf8ea );
  894. add_mapping( 0xff, 0xf8eb );
  895. my $INPUT = open_data_file( $KSCDATA, "KSX1001.TXT" );
  896. while (<$INPUT>)
  897. {
  898. next if /^\#/; # skip comments
  899. next if /^$/; # skip empty lines
  900. next if /\x1a/; # skip ^Z
  901. if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
  902. {
  903. add_mapping( 0x8080 + hex $1, hex $2 );
  904. next;
  905. }
  906. die "Unrecognized line $_\n";
  907. }
  908. close $INPUT;
  909. # get some extra mappings from cp 949
  910. my @defined_lb;
  911. map { $defined_lb[$_] = 1; } @lead_bytes;
  912. foreach my $i (0x0000 .. 0xffff)
  913. {
  914. next if ($i >= 0x1100 && $i <= 0x11ff); # range not used in 20949
  915. next unless defined $cp949[$i];
  916. if ($cp949[$i] >= 0xff)
  917. {
  918. # only add chars for lead bytes that exist in 20949
  919. my $hi = $cp949[$i] >> 8;
  920. my $lo = $cp949[$i] & 0xff;
  921. next unless $defined_lb[$hi];
  922. next unless $lo >= 0xa1 && $lo <= 0xfe;
  923. }
  924. add_mapping( $cp949[$i], $i );
  925. }
  926. output_codepage_file( 20949 );
  927. }
  928. ################################################################
  929. # build the sort keys table
  930. sub dump_sortkeys($)
  931. {
  932. my $filename = shift;
  933. my @sortkeys = ();
  934. my $INPUT = open_data_file( $REPORTS, $SORTKEYS );
  935. while (<$INPUT>)
  936. {
  937. next if /^\#/; # skip comments
  938. next if /^$/; # skip empty lines
  939. next if /\x1a/; # skip ^Z
  940. next if /^\@version/; # skip @version header
  941. if (/^([0-9a-fA-F]+)\s+;\s+\[([*.])([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
  942. {
  943. my ($uni,$variable) = (hex $1, $2);
  944. next if $uni > 65535;
  945. $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
  946. next;
  947. }
  948. if (/^([0-9a-fA-F]+\s+)+;\s+\[[*.]([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
  949. {
  950. # multiple character sequence, ignored for now
  951. next;
  952. }
  953. die "$SORTKEYS: Unrecognized line $_\n";
  954. }
  955. close $INPUT;
  956. # compress the keys to 32 bit:
  957. # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
  958. @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
  959. ${$a}[2] <=> ${$b}[2] or
  960. ${$a}[3] <=> ${$b}[3] or
  961. ${$a}[4] <=> ${$b}[4] or
  962. $a cmp $b; } @sortkeys;
  963. my ($n2, $n3) = (1, 1);
  964. my @keys = (-1, -1, -1, -1, -1 );
  965. my @flatkeys = ();
  966. for (my $i = 0; $i < @sortkeys; $i++)
  967. {
  968. next unless defined $sortkeys[$i];
  969. my @current = @{$sortkeys[$i]};
  970. if ($current[1] == $keys[1])
  971. {
  972. if ($current[2] == $keys[2])
  973. {
  974. if ($current[3] == $keys[3])
  975. {
  976. # nothing
  977. }
  978. else
  979. {
  980. $keys[3] = $current[3];
  981. $n3++;
  982. die if ($n3 >= 16);
  983. }
  984. }
  985. else
  986. {
  987. $keys[2] = $current[2];
  988. $keys[3] = $current[3];
  989. $n2++;
  990. $n3 = 1;
  991. die if ($n2 >= 256);
  992. }
  993. }
  994. else
  995. {
  996. $keys[1] = $current[1];
  997. $keys[2] = $current[2];
  998. $keys[3] = $current[3];
  999. $n2 = 1;
  1000. $n3 = 1;
  1001. }
  1002. if ($current[2]) { $current[2] = $n2; }
  1003. if ($current[3]) { $current[3] = $n3; }
  1004. if ($current[4]) { $current[4] = 1; }
  1005. $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
  1006. }
  1007. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1008. printf "Building $filename\n";
  1009. printf OUTPUT "/* Unicode collation element table */\n";
  1010. printf OUTPUT "/* generated from %s */\n", "$REPORTS/$SORTKEYS";
  1011. printf OUTPUT "/* DO NOT EDIT!! */\n\n";
  1012. print OUTPUT "#include \"windef.h\"\n\n";
  1013. dump_two_level_mapping( "collation_table", 0xffffffff, 32, @flatkeys );
  1014. close OUTPUT;
  1015. save_file($filename);
  1016. }
  1017. ################################################################
  1018. # dump an array of integers
  1019. sub dump_array($$@)
  1020. {
  1021. my ($bit_width, $default, @array) = @_;
  1022. my $format = sprintf "0x%%0%ux", $bit_width / 4;
  1023. my $i;
  1024. my $ret = " ";
  1025. for ($i = 0; $i < $#array; $i++)
  1026. {
  1027. $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
  1028. $ret .= (($i % 8) != 7) ? ", " : ",\n ";
  1029. }
  1030. $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
  1031. return $ret;
  1032. }
  1033. ################################################################
  1034. # dump an SBCS mapping table in binary format
  1035. sub dump_binary_sbcs_table($)
  1036. {
  1037. my $codepage = shift;
  1038. my @header = ( 13, $codepage, 1, $default_char, $default_wchar, $cp2uni[$default_char], $uni2cp[$default_wchar] );
  1039. my $wc_offset = 256 + 3 + (@glyph2uni ? 256 : 0);
  1040. print OUTPUT pack "S<*", @header;
  1041. print OUTPUT pack "C12", (0) x 12;
  1042. print OUTPUT pack "S<*", $wc_offset, map { $_ || 0; } @cp2uni[0 .. 255];
  1043. if (@glyph2uni)
  1044. {
  1045. print OUTPUT pack "S<*", 256, get_glyphs_mapping(@cp2uni[0 .. 255]);
  1046. }
  1047. else
  1048. {
  1049. print OUTPUT pack "S<*", 0;
  1050. }
  1051. print OUTPUT pack "S<*", 0, 0;
  1052. print OUTPUT pack "C*", map { defined $_ ? $_ : $default_char; } @uni2cp[0 .. 65535];
  1053. }
  1054. ################################################################
  1055. # dump a DBCS mapping table in binary format
  1056. sub dump_binary_dbcs_table($)
  1057. {
  1058. my $codepage = shift;
  1059. my @lb_ranges = get_lb_ranges();
  1060. my @header = ( 13, $codepage, 2, $default_char, $default_wchar, $cp2uni[$default_char], $uni2cp[$default_wchar] );
  1061. my @offsets = (0) x 256;
  1062. my $pos = 0;
  1063. foreach my $i (@lead_bytes)
  1064. {
  1065. $offsets[$i] = ($pos += 256);
  1066. $cp2uni[$i] = 0;
  1067. }
  1068. my $wc_offset = 256 + 3 + 256 * (1 + scalar @lead_bytes);
  1069. print OUTPUT pack "S<*", @header;
  1070. print OUTPUT pack "C12", @lb_ranges, 0 x 12;
  1071. print OUTPUT pack "S<*", $wc_offset, map { $_ || 0; } @cp2uni[0 .. 255];
  1072. print OUTPUT pack "S<*", 0, scalar @lb_ranges / 2, @offsets;
  1073. foreach my $i (@lead_bytes)
  1074. {
  1075. my $base = $i << 8;
  1076. print OUTPUT pack "S<*", map { defined $_ ? $_ : $default_wchar; } @cp2uni[$base .. $base + 255];
  1077. }
  1078. print OUTPUT pack "S<", 4;
  1079. print OUTPUT pack "S<*", map { defined $_ ? $_ : $default_char; } @uni2cp[0 .. 65535];
  1080. }
  1081. ################################################################
  1082. # get the list of defined lead byte ranges
  1083. sub get_lb_ranges()
  1084. {
  1085. my @list = ();
  1086. my @ranges = ();
  1087. foreach my $i (@lead_bytes) { $list[$i] = 1; }
  1088. my $on = 0;
  1089. for (my $i = 0; $i < 256; $i++)
  1090. {
  1091. if ($on)
  1092. {
  1093. if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
  1094. }
  1095. else
  1096. {
  1097. if ($list[$i]) { push @ranges, $i; $on = 1; }
  1098. }
  1099. }
  1100. if ($on) { push @ranges, 0xff; }
  1101. return @ranges;
  1102. }
  1103. ################################################################
  1104. # dump the Indic Syllabic Category table
  1105. sub dump_indic($)
  1106. {
  1107. my $filename = shift;
  1108. my @indic_table;
  1109. my $INPUT = open_data_file( $UNIDATA, "IndicSyllabicCategory.txt" );
  1110. while (<$INPUT>)
  1111. {
  1112. next if /^\#/; # skip comments
  1113. next if /^\s*$/; # skip empty lines
  1114. next if /\x1a/; # skip ^Z
  1115. if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
  1116. {
  1117. my $type = $2;
  1118. die "unknown indic $type" unless defined $indic_types{$type};
  1119. if (hex $1 < 65536)
  1120. {
  1121. $indic_table[hex $1] = $indic_types{$type};
  1122. }
  1123. next;
  1124. }
  1125. elsif (/^\s*([0-9a-fA-F]+)\.\.\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
  1126. {
  1127. my $type = $3;
  1128. die "unknown indic $type" unless defined $indic_types{$type};
  1129. if (hex $1 < 65536 and hex $2 < 65536)
  1130. {
  1131. foreach my $i (hex $1 .. hex $2)
  1132. {
  1133. $indic_table[$i] = $indic_types{$type};
  1134. }
  1135. }
  1136. next;
  1137. }
  1138. die "malformed line $_";
  1139. }
  1140. close $INPUT;
  1141. $INPUT = open_data_file( $UNIDATA, "IndicPositionalCategory.txt" );
  1142. while (<$INPUT>)
  1143. {
  1144. next if /^\#/; # skip comments
  1145. next if /^\s*$/; # skip empty lines
  1146. next if /\x1a/; # skip ^Z
  1147. if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
  1148. {
  1149. my $type = $2;
  1150. die "unknown matra $type" unless defined $matra_types{$type};
  1151. $indic_table[hex $1] |= $matra_types{$type} << 8;
  1152. next;
  1153. }
  1154. elsif (/^\s*([0-9a-fA-F]+)\.\.\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
  1155. {
  1156. my $type = $3;
  1157. die "unknown matra $type" unless defined $matra_types{$type};
  1158. foreach my $i (hex $1 .. hex $2)
  1159. {
  1160. $indic_table[$i] |= $matra_types{$type} << 8;
  1161. }
  1162. next;
  1163. }
  1164. die "malformed line $_";
  1165. }
  1166. close $INPUT;
  1167. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1168. print "Building $filename\n";
  1169. print OUTPUT "/* Unicode Indic Syllabic Category */\n";
  1170. print OUTPUT "/* generated from $UNIDATA:IndicSyllabicCategory.txt */\n";
  1171. print OUTPUT "/* and from $UNIDATA:IndicPositionalCategory.txt */\n";
  1172. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1173. print OUTPUT "#include \"windef.h\"\n\n";
  1174. dump_two_level_mapping( "indic_syllabic_table", $indic_types{'Other'}, 16, @indic_table );
  1175. close OUTPUT;
  1176. save_file($filename);
  1177. }
  1178. ################################################################
  1179. # dump the Line Break Properties table
  1180. sub dump_linebreak($)
  1181. {
  1182. my $filename = shift;
  1183. my @break_table;
  1184. my $INPUT = open_data_file( $UNIDATA, "LineBreak.txt" );
  1185. while (<$INPUT>)
  1186. {
  1187. next if /^\#/; # skip comments
  1188. next if /^\s*$/; # skip empty lines
  1189. next if /\x1a/; # skip ^Z
  1190. if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z][0-9A-Z])+\s*/)
  1191. {
  1192. my $type = $2;
  1193. die "unknown breaktype $type" unless defined $break_types{$type};
  1194. $break_table[hex $1] = $break_types{$type};
  1195. next;
  1196. }
  1197. elsif (/^\s*([0-9a-fA-F]+)\.\.\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z][0-9A-Z])+\s*/)
  1198. {
  1199. my $type = $3;
  1200. die "unknown breaktype $type" unless defined $break_types{$type};
  1201. foreach my $i (hex $1 .. hex $2)
  1202. {
  1203. $break_table[$i] = $break_types{$type};
  1204. }
  1205. next;
  1206. }
  1207. elsif (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
  1208. {
  1209. my $type = $2;
  1210. die "unknown breaktype $type" unless defined $break_types{$type};
  1211. $break_table[hex $1] = $break_types{$type};
  1212. next;
  1213. }
  1214. elsif (/^\s*([0-9a-fA-F]+)\.\.\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
  1215. {
  1216. my $type = $3;
  1217. die "unknown breaktype $type" unless defined $break_types{$type};
  1218. foreach my $i (hex $1 .. hex $2)
  1219. {
  1220. $break_table[$i] = $break_types{$type};
  1221. }
  1222. next;
  1223. }
  1224. die "malformed line $_";
  1225. }
  1226. close $INPUT;
  1227. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1228. print "Building $filename\n";
  1229. print OUTPUT "/* Unicode Line Break Properties */\n";
  1230. print OUTPUT "/* generated from $UNIDATA:LineBreak.txt */\n";
  1231. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1232. print OUTPUT "#include \"windef.h\"\n\n";
  1233. dump_two_level_mapping( "wine_linebreak_table", $break_types{'XX'}, 16, @break_table );
  1234. close OUTPUT;
  1235. save_file($filename);
  1236. }
  1237. my %scripts =
  1238. (
  1239. "Unknown" => 0,
  1240. "Common" => 1,
  1241. "Inherited" => 2,
  1242. "Arabic" => 3,
  1243. "Armenian" => 4,
  1244. "Avestan" => 5,
  1245. "Balinese" => 6,
  1246. "Bamum" => 7,
  1247. "Batak" => 8,
  1248. "Bengali" => 9,
  1249. "Bopomofo" => 10,
  1250. "Brahmi" => 11,
  1251. "Braille" => 12,
  1252. "Buginese" => 13,
  1253. "Buhid" => 14,
  1254. "Canadian_Aboriginal" => 15,
  1255. "Carian" => 16,
  1256. "Cham" => 17,
  1257. "Cherokee" => 18,
  1258. "Coptic" => 19,
  1259. "Cuneiform" => 20,
  1260. "Cypriot" => 21,
  1261. "Cyrillic" => 22,
  1262. "Deseret" => 23,
  1263. "Devanagari" => 24,
  1264. "Egyptian_Hieroglyphs" => 25,
  1265. "Ethiopic" => 26,
  1266. "Georgian" => 27,
  1267. "Glagolitic" => 28,
  1268. "Gothic" => 29,
  1269. "Greek" => 30,
  1270. "Gujarati" => 31,
  1271. "Gurmukhi" => 32,
  1272. "Han" => 33,
  1273. "Hangul" => 34,
  1274. "Hanunoo" => 35,
  1275. "Hebrew" => 36,
  1276. "Hiragana" => 37,
  1277. "Imperial_Aramaic" => 38,
  1278. "Inscriptional_Pahlavi" => 39,
  1279. "Inscriptional_Parthian" => 40,
  1280. "Javanese" => 41,
  1281. "Kaithi" => 42,
  1282. "Kannada" => 43,
  1283. "Katakana" => 44,
  1284. "Kayah_Li" => 45,
  1285. "Kharoshthi" => 46,
  1286. "Khmer" => 47,
  1287. "Lao" => 48,
  1288. "Latin" => 49,
  1289. "Lepcha" => 50,
  1290. "Limbu" => 51,
  1291. "Linear_B" => 52,
  1292. "Lisu" => 53,
  1293. "Lycian" => 54,
  1294. "Lydian" => 55,
  1295. "Malayalam" => 56,
  1296. "Mandaic" => 57,
  1297. "Meetei_Mayek" => 58,
  1298. "Mongolian" => 59,
  1299. "Myanmar" => 60,
  1300. "New_Tai_Lue" => 61,
  1301. "Nko" => 62,
  1302. "Ogham" => 63,
  1303. "Ol_Chiki" => 64,
  1304. "Old_Italic" => 65,
  1305. "Old_Persian" => 66,
  1306. "Old_South_Arabian" => 67,
  1307. "Old_Turkic" => 68,
  1308. "Oriya" => 69,
  1309. "Osmanya" => 70,
  1310. "Phags_Pa" => 71,
  1311. "Phoenician" => 72,
  1312. "Rejang" => 73,
  1313. "Runic" => 74,
  1314. "Samaritan" => 75,
  1315. "Saurashtra" => 76,
  1316. "Shavian" => 77,
  1317. "Sinhala" => 78,
  1318. "Sundanese" => 79,
  1319. "Syloti_Nagri" => 80,
  1320. "Syriac" => 81,
  1321. "Tagalog" => 82,
  1322. "Tagbanwa" => 83,
  1323. "Tai_Le" => 84,
  1324. "Tai_Tham" => 85,
  1325. "Tai_Viet" => 86,
  1326. "Tamil" => 87,
  1327. "Telugu" => 88,
  1328. "Thaana" => 89,
  1329. "Thai" => 90,
  1330. "Tibetan" => 91,
  1331. "Tifinagh" => 92,
  1332. "Ugaritic" => 93,
  1333. "Vai" => 94,
  1334. "Yi" => 95,
  1335. # Win8/Win8.1
  1336. "Chakma" => 96,
  1337. "Meroitic_Cursive" => 97,
  1338. "Meroitic_Hieroglyphs" => 98,
  1339. "Miao" => 99,
  1340. "Sharada" => 100,
  1341. "Sora_Sompeng" => 101,
  1342. "Takri" => 102,
  1343. # Win10
  1344. "Bassa_Vah" => 103,
  1345. "Caucasian_Albanian" => 104,
  1346. "Duployan" => 105,
  1347. "Elbasan" => 106,
  1348. "Grantha" => 107,
  1349. "Khojki" => 108,
  1350. "Khudawadi" => 109,
  1351. "Linear_A" => 110,
  1352. "Mahajani" => 111,
  1353. "Manichaean" => 112,
  1354. "Mende_Kikakui" => 113,
  1355. "Modi" => 114,
  1356. "Mro" => 115,
  1357. "Nabataean" => 116,
  1358. "Old_North_Arabian" => 117,
  1359. "Old_Permic" => 118,
  1360. "Pahawh_Hmong" => 119,
  1361. "Palmyrene" => 120,
  1362. "Pau_Cin_Hau" => 121,
  1363. "Psalter_Pahlavi" => 122,
  1364. "Siddham" => 123,
  1365. "Tirhuta" => 124,
  1366. "Warang_Citi" => 125,
  1367. # Win10 RS1
  1368. "Adlam" => 126,
  1369. "Ahom" => 127,
  1370. "Anatolian_Hieroglyphs" => 128,
  1371. "Bhaiksuki" => 129,
  1372. "Hatran" => 130,
  1373. "Marchen" => 131,
  1374. "Multani" => 132,
  1375. "Newa" => 133,
  1376. "Old_Hungarian" => 134,
  1377. "Osage" => 135,
  1378. "SignWriting" => 136,
  1379. "Tangut" => 137,
  1380. # Win10 RS4
  1381. "Masaram_Gondi" => 138,
  1382. "Nushu" => 139,
  1383. "Soyombo" => 140,
  1384. "Zanabazar_Square" => 141,
  1385. # Win10 1903
  1386. "Dogra" => 142,
  1387. "Gunjala_Gondi" => 143,
  1388. "Hanifi_Rohingya" => 144,
  1389. "Makasar" => 145,
  1390. "Medefaidrin" => 146,
  1391. "Old_Sogdian" => 147,
  1392. "Sogdian" => 148,
  1393. # Win10 2004
  1394. "Elymaic" => 149,
  1395. "Nyiakeng_Puachue_Hmong" => 150,
  1396. "Nandinagari" => 151,
  1397. "Wancho" => 152,
  1398. # Win11
  1399. "Chorasmian" => 153,
  1400. "Dives_Akuru" => 154,
  1401. "Khitan_Small_Script" => 155,
  1402. "Yezidi" => 156,
  1403. );
  1404. ################################################################
  1405. # dump Script IDs table
  1406. sub dump_scripts($)
  1407. {
  1408. my $filename = shift;
  1409. my $header = $filename;
  1410. my @scripts_table;
  1411. my $script_index;
  1412. my $i;
  1413. my $INPUT = open_data_file( $UNIDATA, "Scripts.txt" );
  1414. # Fill the table
  1415. # Unknown script id is always 0, so undefined scripts are automatically treated as such
  1416. while (<$INPUT>)
  1417. {
  1418. my $type = "";
  1419. next if /^\#/; # skip comments
  1420. next if /^\s*$/; # skip empty lines
  1421. next if /\x1a/; # skip ^Z
  1422. if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
  1423. {
  1424. $type = $2;
  1425. if (defined $scripts{$type})
  1426. {
  1427. $scripts_table[hex $1] = $scripts{$type};
  1428. }
  1429. next;
  1430. }
  1431. elsif (/^\s*([0-9a-fA-F]+)\.\.\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
  1432. {
  1433. $type = $3;
  1434. if (defined $scripts{$type})
  1435. {
  1436. foreach my $i (hex $1 .. hex $2)
  1437. {
  1438. $scripts_table[$i] = $scripts{$type};
  1439. }
  1440. }
  1441. next;
  1442. }
  1443. }
  1444. close $INPUT;
  1445. $header = "$filename.h";
  1446. open OUTPUT,">$header.new" or die "Cannot create $header";
  1447. print "Building $header\n";
  1448. print OUTPUT "/* Unicode Script IDs */\n";
  1449. print OUTPUT "/* generated from $UNIDATA:Scripts.txt */\n";
  1450. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1451. print OUTPUT "enum unicode_script_id {\n";
  1452. foreach my $script (sort { $scripts{$a} <=> $scripts{$b} } keys %scripts)
  1453. {
  1454. print OUTPUT " Script_$script = $scripts{$script},\n";
  1455. }
  1456. print OUTPUT " Script_LastId = ", (scalar keys %scripts) - 1, "\n";
  1457. print OUTPUT "};\n";
  1458. close OUTPUT;
  1459. save_file($header);
  1460. $filename = "$filename.c";
  1461. open OUTPUT,">$filename.new" or die "Cannot create $header";
  1462. print "Building $filename\n";
  1463. print OUTPUT "/* Unicode Script IDs */\n";
  1464. print OUTPUT "/* generated from $UNIDATA:Scripts.txt */\n";
  1465. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1466. print OUTPUT "#include \"windef.h\"\n\n";
  1467. dump_two_level_mapping( "wine_scripts_table", 0, 16, @scripts_table );
  1468. close OUTPUT;
  1469. save_file($filename);
  1470. }
  1471. ################################################################
  1472. # dump the BiDi mirroring table
  1473. sub dump_mirroring($)
  1474. {
  1475. my $filename = shift;
  1476. my @mirror_table = ();
  1477. my $INPUT = open_data_file( $UNIDATA, "BidiMirroring.txt" );
  1478. while (<$INPUT>)
  1479. {
  1480. next if /^\#/; # skip comments
  1481. next if /^$/; # skip empty lines
  1482. next if /\x1a/; # skip ^Z
  1483. if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
  1484. {
  1485. $mirror_table[hex $1] = hex $2;
  1486. next;
  1487. }
  1488. die "malformed line $_";
  1489. }
  1490. close $INPUT;
  1491. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1492. print "Building $filename\n";
  1493. print OUTPUT "/* Unicode BiDi mirroring */\n";
  1494. print OUTPUT "/* generated from $UNIDATA:BidiMirroring.txt */\n";
  1495. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1496. print OUTPUT "#include \"windef.h\"\n\n";
  1497. dump_two_level_mapping( "wine_mirror_map", 0, 16, @mirror_table );
  1498. close OUTPUT;
  1499. save_file($filename);
  1500. }
  1501. ################################################################
  1502. # dump the Bidi Brackets
  1503. sub dump_bracket($)
  1504. {
  1505. my $filename = shift;
  1506. my @bracket_table;
  1507. my $INPUT = open_data_file( $UNIDATA, "BidiBrackets.txt" );
  1508. while (<$INPUT>)
  1509. {
  1510. next if /^\#/; # skip comments
  1511. next if /^\s*$/; # skip empty lines
  1512. next if /\x1a/; # skip ^Z
  1513. if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+);\s*([con])/)
  1514. {
  1515. my $type = $3;
  1516. die "unknown bracket $type" unless defined $bracket_types{$type};
  1517. die "characters too distant $1 and $2" if abs(hex($2) - hex($1)) >= 128;
  1518. $bracket_table[hex $1] = (hex($2) - hex($1)) % 255;
  1519. $bracket_table[hex $1] += $bracket_types{$type} << 8;
  1520. next;
  1521. }
  1522. die "malformed line $_";
  1523. }
  1524. close $INPUT;
  1525. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1526. print "Building $filename\n";
  1527. print OUTPUT "/* Unicode Bidirectional Bracket table */\n";
  1528. print OUTPUT "/* generated from $UNIDATA:BidiBrackets.txt */\n";
  1529. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1530. print OUTPUT "#include \"windef.h\"\n\n";
  1531. dump_two_level_mapping( "bidi_bracket_table", 0, 16, @bracket_table );
  1532. close OUTPUT;
  1533. save_file($filename);
  1534. }
  1535. ################################################################
  1536. # dump the Arabic shaping table
  1537. sub dump_shaping($)
  1538. {
  1539. my $filename = shift;
  1540. my @joining_table = @initial_joining_table;
  1541. my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
  1542. while (<$INPUT>)
  1543. {
  1544. next if /^\#/; # skip comments
  1545. next if /^\s*$/; # skip empty lines
  1546. next if /\x1a/; # skip ^Z
  1547. if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
  1548. {
  1549. my $type = $2;
  1550. $joining_table[hex $1] = $joining_types{$type};
  1551. next;
  1552. }
  1553. die "malformed line $_";
  1554. }
  1555. close $INPUT;
  1556. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1557. print "Building $filename\n";
  1558. print OUTPUT "/* Unicode Arabic shaping */\n";
  1559. print OUTPUT "/* generated from $UNIDATA:ArabicShaping.txt */\n";
  1560. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1561. print OUTPUT "#include \"windef.h\"\n\n";
  1562. dump_two_level_mapping( "wine_shaping_table", 0, 16, @joining_table );
  1563. print OUTPUT "\nconst unsigned short DECLSPEC_HIDDEN wine_shaping_forms[256][4] =\n{\n";
  1564. for (my $i = 0x600; $i <= 0x6ff; $i++)
  1565. {
  1566. printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
  1567. ${joining_forms{"isolated"}}[$i] || $i,
  1568. ${joining_forms{"final"}}[$i] || $i,
  1569. ${joining_forms{"initial"}}[$i] || $i,
  1570. ${joining_forms{"medial"}}[$i] || $i;
  1571. }
  1572. print OUTPUT "};\n";
  1573. close OUTPUT;
  1574. save_file($filename);
  1575. }
  1576. ################################################################
  1577. # dump the Arabic shaping table
  1578. sub dump_arabic_shaping($)
  1579. {
  1580. my $filename = shift;
  1581. my @joining_table = @initial_joining_table;
  1582. my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
  1583. while (<$INPUT>)
  1584. {
  1585. next if /^\#/; # skip comments
  1586. next if /^\s*$/; # skip empty lines
  1587. next if /\x1a/; # skip ^Z
  1588. if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
  1589. {
  1590. my $type = $2;
  1591. my $group = $3;
  1592. if ($group eq "ALAPH" || $group eq "DALATH RISH")
  1593. {
  1594. $joining_table[hex $1] = $joining_types{$group};
  1595. }
  1596. else
  1597. {
  1598. $joining_table[hex $1] = $joining_types{$type};
  1599. }
  1600. next;
  1601. }
  1602. die "malformed line $_";
  1603. }
  1604. close $INPUT;
  1605. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1606. print "Building $filename\n";
  1607. print OUTPUT "/* Unicode Arabic shaping */\n";
  1608. print OUTPUT "/* generated from $UNIDATA:ArabicShaping.txt */\n";
  1609. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1610. print OUTPUT "#include \"windef.h\"\n\n";
  1611. dump_two_level_mapping( "arabic_shaping_table", 0, 16, @joining_table );
  1612. close OUTPUT;
  1613. save_file($filename);
  1614. }
  1615. ################################################################
  1616. # dump the Vertical Orientation table
  1617. sub dump_vertical($$)
  1618. {
  1619. my ($filename, $unix) = @_;
  1620. my @vertical_table;
  1621. my $INPUT = open_data_file( $UNIDATA, "VerticalOrientation.txt" );
  1622. while (<$INPUT>)
  1623. {
  1624. next if /^\#/; # skip comments
  1625. next if /^\s*$/; # skip empty lines
  1626. next if /\x1a/; # skip ^Z
  1627. if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
  1628. {
  1629. my $type = $2;
  1630. die "unknown vertical $type" unless defined $vertical_types{$type};
  1631. if (hex $1 < 65536)
  1632. {
  1633. $vertical_table[hex $1] = $vertical_types{$type};
  1634. }
  1635. next;
  1636. }
  1637. elsif (/^\s*([0-9a-fA-F]+)\.\.\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*/)
  1638. {
  1639. my $type = $3;
  1640. die "unknown vertical $type" unless defined $vertical_types{$type};
  1641. foreach my $i (hex $1 .. hex $2)
  1642. {
  1643. $vertical_table[$i] = $vertical_types{$type};
  1644. }
  1645. next;
  1646. }
  1647. die "malformed line $_";
  1648. }
  1649. close $INPUT;
  1650. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1651. print "Building $filename\n";
  1652. print OUTPUT "/* Unicode Vertical Orientation */\n";
  1653. print OUTPUT "/* generated from $UNIDATA:VerticalOrientation.txt */\n";
  1654. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1655. if ($unix)
  1656. {
  1657. print OUTPUT "#if 0\n";
  1658. print OUTPUT "#pragma makedep unix\n";
  1659. print OUTPUT "#endif\n\n";
  1660. }
  1661. print OUTPUT "#include \"windef.h\"\n\n";
  1662. dump_two_level_mapping( "vertical_orientation_table", $vertical_types{'R'}, 16, @vertical_table );
  1663. close OUTPUT;
  1664. save_file($filename);
  1665. }
  1666. ################################################################
  1667. # dump the digit folding tables
  1668. sub dump_digit_folding($)
  1669. {
  1670. my ($filename) = shift;
  1671. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1672. print "Building $filename\n";
  1673. print OUTPUT "/* Unicode digit folding mappings */\n";
  1674. print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
  1675. print OUTPUT "/* DO NOT EDIT!! */\n\n";
  1676. print OUTPUT "#include \"windef.h\"\n\n";
  1677. dump_two_level_mapping( "wine_digitmap", 0, 16, @digitmap_table );
  1678. close OUTPUT;
  1679. save_file($filename);
  1680. }
  1681. ################################################################
  1682. # compress a mapping table by removing identical rows
  1683. sub compress_array($$@)
  1684. {
  1685. my $rows = shift;
  1686. my $def = shift;
  1687. my @table = @_;
  1688. my $len = @table / $rows;
  1689. my @array;
  1690. my $data = "";
  1691. # try to merge table rows
  1692. for (my $row = 0; $row < $rows; $row++)
  1693. {
  1694. my $rowtxt = pack "U*", map { defined($_) ? $_ : $def; } @table[($row * $len)..(($row + 1) * $len - 1)];
  1695. my $pos = index $data, $rowtxt;
  1696. if ($pos == -1)
  1697. {
  1698. # check if the tail of the data can match the start of the new row
  1699. my $first = substr( $rowtxt, 0, 1 );
  1700. for (my $i = length($data) - 1; $i > 0; $i--)
  1701. {
  1702. $pos = index( substr( $data, -$i ), $first );
  1703. last if $pos == -1;
  1704. $i -= $pos;
  1705. next unless substr( $data, -$i ) eq substr( $rowtxt, 0, $i );
  1706. substr( $data, -$i ) = "";
  1707. last;
  1708. }
  1709. $pos = length $data;
  1710. $data .= $rowtxt;
  1711. }
  1712. $array[$row] = $rows + $pos;
  1713. }
  1714. return @array, unpack "U*", $data;
  1715. }
  1716. ################################################################
  1717. # dump a char -> 16-bit value mapping table using two-level tables
  1718. sub dump_two_level_mapping($$@)
  1719. {
  1720. my $name = shift;
  1721. my $def = shift;
  1722. my $size = shift;
  1723. my $type = $size == 16 ? "unsigned short" : "unsigned int";
  1724. my @row_array = compress_array( 4096, $def, @_[0..65535] );
  1725. my @array = compress_array( 256, 0, @row_array[0..4095] );
  1726. for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }
  1727. printf OUTPUT "const %s DECLSPEC_HIDDEN %s[%d] =\n{\n", $type, $name, @array + @row_array - 4096;
  1728. printf OUTPUT " /* level 1 offsets */\n%s,\n", dump_array( $size, 0, @array[0..255] );
  1729. printf OUTPUT " /* level 2 offsets */\n%s,\n", dump_array( $size, 0, @array[256..$#array] );
  1730. printf OUTPUT " /* values */\n%s\n};\n", dump_array( $size, 0, @row_array[4096..$#row_array] );
  1731. }
  1732. ################################################################
  1733. # dump a char -> value mapping table using three-level tables
  1734. sub dump_three_level_mapping($$@)
  1735. {
  1736. my $name = shift;
  1737. my $def = shift;
  1738. my $size = shift;
  1739. my $type = $size == 16 ? "unsigned short" : "unsigned int";
  1740. my $level3 = ($MAX_CHAR + 1) / 16;
  1741. my $level2 = $level3 / 16;
  1742. my $level1 = $level2 / 16;
  1743. my @array3 = compress_array( $level3, $def, @_[0..$MAX_CHAR] );
  1744. my @array2 = compress_array( $level2, 0, @array3[0..$level3-1] );
  1745. my @array1 = compress_array( $level1, 0, @array2[0..$level2-1] );
  1746. for (my $i = $level2; $i < @array2; $i++) { $array2[$i] += @array1 + @array2 - $level2 - $level3; }
  1747. for (my $i = $level1; $i < @array1; $i++) { $array1[$i] += @array1 - $level2; }
  1748. printf OUTPUT "const %s DECLSPEC_HIDDEN %s[%u] =\n{\n", $type, $name, @array1 + (@array2 - $level2) + (@array3 - $level3);
  1749. printf OUTPUT " /* level 1 offsets */\n%s,\n", dump_array( $size, 0, @array1[0..$level1-1] );
  1750. printf OUTPUT " /* level 2 offsets */\n%s,\n", dump_array( $size, 0, @array1[$level1..$#array1] );
  1751. printf OUTPUT " /* level 3 offsets */\n%s,\n", dump_array( $size, 0, @array2[$level2..$#array2] );
  1752. printf OUTPUT " /* values */\n%s\n};\n", dump_array( $size, 0, @array3[$level3..$#array3] );
  1753. }
  1754. ################################################################
  1755. # dump a binary case mapping table in l_intl.nls format
  1756. sub dump_binary_case_table(@)
  1757. {
  1758. my (@table) = @_;
  1759. my $max_char = 0x10000;
  1760. my $level1 = $max_char / 16;
  1761. my $level2 = $level1 / 16;
  1762. my @difftable;
  1763. for (my $i = 0; $i < @table; $i++)
  1764. {
  1765. next unless defined $table[$i];
  1766. $difftable[$i] = ($table[$i] - $i) & 0xffff;
  1767. }
  1768. my @row_array = compress_array( $level1, 0, @difftable[0..$max_char-1] );
  1769. my @array = compress_array( $level2, 0, @row_array[0..$level1-1] );
  1770. my $offset = @array - $level1;
  1771. for (my $i = $level2; $i < @array; $i++) { $array[$i] += $offset; }
  1772. return pack "S<*", 1 + $offset + @row_array, @array, @row_array[$level1..$#row_array];
  1773. }
  1774. ################################################################
  1775. # dump case mappings for l_intl.nls
  1776. sub dump_intl_nls($)
  1777. {
  1778. my @upper_table = @toupper_table;
  1779. my @lower_table = @tolower_table;
  1780. remove_linguistic_mappings( \@upper_table, \@lower_table );
  1781. my $upper = dump_binary_case_table( @upper_table );
  1782. my $lower = dump_binary_case_table( @lower_table );
  1783. my $filename = shift;
  1784. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1785. printf "Building $filename\n";
  1786. binmode OUTPUT;
  1787. print OUTPUT pack "S<", 1; # version
  1788. print OUTPUT $upper;
  1789. print OUTPUT $lower;
  1790. close OUTPUT;
  1791. save_file($filename);
  1792. }
  1793. ################################################################
  1794. # dump the bidi direction table
  1795. sub dump_bidi_dir_table($)
  1796. {
  1797. my $filename = shift;
  1798. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1799. printf "Building $filename\n";
  1800. printf OUTPUT "/* Unicode BiDi direction table */\n";
  1801. printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
  1802. printf OUTPUT "#include \"windef.h\"\n\n";
  1803. my @table;
  1804. for (my $i = 0; $i < 65536; $i++)
  1805. {
  1806. $table[$i] = $bidi_types{$direction_table[$i]} if defined $direction_table[$i];
  1807. }
  1808. dump_two_level_mapping( "bidi_direction_table", $bidi_types{"L"}, 16, @table );
  1809. close OUTPUT;
  1810. save_file($filename);
  1811. }
  1812. sub rol($$)
  1813. {
  1814. my ($byte, $count) = @_;
  1815. return (($byte << $count) | ($byte >> (8 - $count))) & 0xff;
  1816. }
  1817. ################################################################
  1818. # compress the character properties table
  1819. sub compress_char_props_table($@)
  1820. {
  1821. my $rows = shift;
  1822. my @table = @_;
  1823. my $len = @table / $rows;
  1824. my $pos = 0;
  1825. my @array = (0) x $rows;
  1826. my %sequences;
  1827. # add some predefined sequences
  1828. foreach my $i (0, 0xfb .. 0xff) { $sequences{pack "L*", (rol($i,5)) x $len} = $i; }
  1829. # try to merge table rows
  1830. for (my $row = 0; $row < $rows; $row++)
  1831. {
  1832. my @table_row = map { defined $_ ? $_ : 0x7f; } @table[($row * $len)..(($row + 1) * $len - 1)];
  1833. my $rowtxt = pack "L*", @table_row;
  1834. if (defined($sequences{$rowtxt}))
  1835. {
  1836. # reuse an existing row
  1837. $array[$row] = $sequences{$rowtxt};
  1838. }
  1839. else
  1840. {
  1841. # create a new row
  1842. $sequences{$rowtxt} = $array[$row] = ++$pos;
  1843. push @array, @table_row;
  1844. }
  1845. }
  1846. return @array;
  1847. }
  1848. ################################################################
  1849. # dump a normalization table in binary format
  1850. sub dump_norm_table($)
  1851. {
  1852. my $filename = shift;
  1853. my %forms = ( "nfc" => 1, "nfd" => 2, "nfkc" => 5, "nfkd" => 6, "idna" => 13 );
  1854. my %decomp = ( "nfc" => \@decomp_table,
  1855. "nfd" => \@decomp_table,
  1856. "nfkc" => \@decomp_compat_table,
  1857. "nfkd" => \@decomp_compat_table ,
  1858. "idna" => \@idna_decomp_table );
  1859. open OUTPUT,">$filename.new" or die "Cannot create $filename";
  1860. print "Building $filename\n";
  1861. my $type = $filename;
  1862. $type =~ s!.*/norm(\w+)\.nls!$1!;
  1863. my $compose = $forms{$type} & 1;
  1864. my $compat = !!($forms{$type} & 4) + ($type eq "idna");
  1865. my @version = split /\./, $UNIVERSION;
  1866. # combining classes
  1867. my @classes;
  1868. my @class_values;
  1869. foreach my $c (grep defined, @combining_class_table)
  1870. {
  1871. $classes[$c] = 1 if $c < 0x100;
  1872. }
  1873. for (my $i = 0; $i < @classes; $i++)
  1874. {
  1875. next unless defined $classes[$i];
  1876. $classes[$i] = @class_values;
  1877. push @class_values, $i;
  1878. }
  1879. push @class_values, 0 if (@class_values % 2);
  1880. die "too many classes" if @class_values >= 0x40;
  1881. # character properties
  1882. my @char_props;
  1883. my @decomposed;
  1884. my @comp_hash_table;
  1885. my $comp_hash_size = $compose ? 254 : 0;
  1886. for (my $i = 0; $i <= $MAX_CHAR; $i++)
  1887. {
  1888. next unless defined $combining_class_table[$i];
  1889. if (defined $decomp{$type}->[$i])
  1890. {
  1891. my @dec = get_decomposition( $i, $decomp{$type} );
  1892. if ($compose && (my @comp = get_composition( $i, $compat )))
  1893. {
  1894. my $hash = ($comp[0] + 95 * $comp[1]) % $comp_hash_size;
  1895. push @{$comp_hash_table[$hash]}, to_utf16( @comp, $i );
  1896. my $val = 0;
  1897. foreach my $d (@dec)
  1898. {
  1899. $val = $combining_class_table[$d];
  1900. last if $val;
  1901. }
  1902. $char_props[$i] = $classes[$val];
  1903. }
  1904. else
  1905. {
  1906. $char_props[$i] = 0xbf;
  1907. }
  1908. @dec = compose_hangul( @dec ) if $compose;
  1909. @dec = to_utf16( @dec );
  1910. push @dec, 0 if @dec >= 7;
  1911. $decomposed[$i] = \@dec;
  1912. }
  1913. else
  1914. {
  1915. if ($combining_class_table[$i] == 0x100)
  1916. {
  1917. $char_props[$i] = 0x7f;
  1918. }
  1919. elsif ($combining_class_table[$i])
  1920. {
  1921. $char_props[$i] = $classes[$combining_class_table[$i]] | 0x80;
  1922. }
  1923. elsif ($type eq "idna" && defined $idna_disallowed[$i])
  1924. {
  1925. $char_props[$i] = 0xff;
  1926. }
  1927. else
  1928. {
  1929. $char_props[$i] = 0;
  1930. }
  1931. }
  1932. }
  1933. if ($compose)
  1934. {
  1935. for (my $i = 0; $i <= $MAX_CHAR; $i++)
  1936. {
  1937. my @comp = get_composition( $i, $compat );
  1938. next unless @comp;
  1939. if ($combining_class_table[$comp[1]])
  1940. {
  1941. $char_props[$comp[0]] |= 0x40 unless $char_props[$comp[0]] & 0x80;
  1942. $char_props[$comp[1]] |= 0x40;
  1943. }
  1944. else
  1945. {
  1946. $char_props[$comp[0]] = ($char_props[$comp[0]] & ~0x40) | 0x80;
  1947. $char_props[$comp[1]] |= 0xc0;
  1948. }
  1949. }
  1950. }
  1951. # surrogates
  1952. foreach my $i (0xd800..0xdbff) { $char_props[$i] = 0xdf; }
  1953. foreach my $i (0xdc00..0xdfff) { $char_props[$i] = 0x9f; }
  1954. # Hangul
  1955. if ($type eq "nfc") { foreach my $i (0x1100..0x117f) { $char_props[$i] = 0xff; } }
  1956. elsif ($compose) { foreach my $i (0x1100..0x11ff) { $char_props[$i] = 0xff; } }
  1957. foreach my $i (0xac00..0xd7ff) { $char_props[$i] = 0xff; }
  1958. # invalid chars
  1959. if ($type eq "idna") { foreach my $i (0x00..0x1f, 0x7f) { $char_props[$i] = 0xff; } }
  1960. foreach my $i (0xfdd0..0xfdef) { $char_props[$i] = 0xff; }
  1961. foreach my $i (0x00..0x10)
  1962. {
  1963. $char_props[($i << 16) | 0xfffe] = 0xff;
  1964. $char_props[($i << 16) | 0xffff] = 0xff;
  1965. }
  1966. # decomposition hash table
  1967. my @decomp_hash_table;
  1968. my @decomp_hash_index;
  1969. my @decomp_hash_data;
  1970. my $decomp_hash_size = 944;
  1971. # build string of character data, reusing substrings when possible
  1972. my $decomp_char_data = "";
  1973. foreach my $i (sort { @{$b} <=> @{$a} } grep defined, @decomposed)
  1974. {
  1975. my $str = pack "U*", @{$i};
  1976. $decomp_char_data .= $str if index( $decomp_char_data, $str) == -1;
  1977. }
  1978. for (my $i = 0; $i < @decomposed; $i++)
  1979. {
  1980. next unless defined $decomposed[$i];
  1981. my $pos = index( $decomp_char_data, pack( "U*", @{$decomposed[$i]} ));
  1982. die "sequence not found" if $pos == -1;
  1983. my $len = @{$decomposed[$i]};
  1984. $len = 7 if $len > 7;
  1985. my $hash = $i % $decomp_hash_size;
  1986. push @{$decomp_hash_table[$hash]}, [ $i, ($len << 13) | $pos ];
  1987. }
  1988. for (my $i = 0; $i < $decomp_hash_size; $i++)
  1989. {
  1990. $decomp_hash_index[$i] = @decomp_hash_data / 2;
  1991. next unless defined $decomp_hash_table[$i];
  1992. if (@{$decomp_hash_table[$i]} == 1)
  1993. {
  1994. my $entry = $decomp_hash_table[$i]->[0];
  1995. if ($char_props[$entry->[0]] == 0xbf)
  1996. {
  1997. $decomp_hash_index[$i] = $entry->[1];
  1998. next;
  1999. }
  2000. }
  2001. foreach my $entry (@{$decomp_hash_table[$i]})
  2002. {
  2003. push @decomp_hash_data, $entry->[0] & 0xffff, $entry->[1];
  2004. }
  2005. }
  2006. push @decomp_hash_data, 0, 0;
  2007. # composition hash table
  2008. my @comp_hash_index;
  2009. my @comp_hash_data;
  2010. if (@comp_hash_table)
  2011. {
  2012. for (my $i = 0; $i < $comp_hash_size; $i++)
  2013. {
  2014. $comp_hash_index[$i] = @comp_hash_data;
  2015. push @comp_hash_data, @{$comp_hash_table[$i]} if defined $comp_hash_table[$i];
  2016. }
  2017. $comp_hash_index[$comp_hash_size] = @comp_hash_data;
  2018. push @comp_hash_data, 0, 0, 0;
  2019. }
  2020. my $level1 = ($MAX_CHAR + 1) / 128;
  2021. my @rows = compress_char_props_table( $level1, @char_props[0..$MAX_CHAR] );
  2022. my @header = ( $version[0], $version[1], $version[2], 0, $forms{$type}, $compat ? 18 : 3,
  2023. 0, $decomp_hash_size, $comp_hash_size, 0 );
  2024. my @tables = (0) x 8;
  2025. $tables[0] = 16 + @header + @tables;
  2026. $tables[1] = $tables[0] + @class_values / 2;
  2027. $tables[2] = $tables[1] + $level1 / 2;
  2028. $tables[3] = $tables[2] + (@rows - $level1) / 2;
  2029. $tables[4] = $tables[3] + @decomp_hash_index;
  2030. $tables[5] = $tables[4] + @decomp_hash_data;
  2031. $tables[6] = $tables[5] + length $decomp_char_data;
  2032. $tables[7] = $tables[6] + @comp_hash_index;
  2033. print OUTPUT pack "S<16", unpack "U*", "norm$type.nlp";
  2034. print OUTPUT pack "S<*", @header;
  2035. print OUTPUT pack "S<*", @tables;
  2036. print OUTPUT pack "C*", @class_values;
  2037. print OUTPUT pack "C*", @rows[0..$level1-1];
  2038. print OUTPUT pack "C*", @rows[$level1..$#rows];
  2039. print OUTPUT pack "S<*", @decomp_hash_index;
  2040. print OUTPUT pack "S<*", @decomp_hash_data;
  2041. print OUTPUT pack "S<*", unpack "U*", $decomp_char_data;
  2042. print OUTPUT pack "S<*", @comp_hash_index;
  2043. print OUTPUT pack "S<*", @comp_hash_data;
  2044. close OUTPUT;
  2045. save_file($filename);
  2046. add_registry_value( "Normalization", sprintf( "%x", $forms{$type} ), "norm$type.nls" );
  2047. }
  2048. ################################################################
  2049. # output a codepage definition file from the global tables
  2050. sub output_codepage_file($)
  2051. {
  2052. my $codepage = shift;
  2053. my $output = sprintf "nls/c_%03d.nls", $codepage;
  2054. open OUTPUT,">$output.new" or die "Cannot create $output";
  2055. printf "Building %s\n", $output;
  2056. if (!@lead_bytes) { dump_binary_sbcs_table( $codepage ); }
  2057. else { dump_binary_dbcs_table( $codepage ); }
  2058. close OUTPUT;
  2059. save_file($output);
  2060. add_registry_value( "Codepage", sprintf( "%d", $codepage ), sprintf( "c_%03d.nls", $codepage ));
  2061. }
  2062. ################################################################
  2063. # output a codepage table from a Microsoft-style mapping file
  2064. sub dump_msdata_codepage($)
  2065. {
  2066. my $filename = shift;
  2067. my $state = "";
  2068. my ($codepage, $width, $count);
  2069. my ($lb_cur, $lb_end);
  2070. @cp2uni = ();
  2071. @glyph2uni = ();
  2072. @lead_bytes = ();
  2073. @uni2cp = ();
  2074. $default_char = $DEF_CHAR;
  2075. $default_wchar = $DEF_CHAR;
  2076. my $INPUT = open_data_file( $MSCODEPAGES, $filename ) or die "Cannot open $filename";
  2077. while (<$INPUT>)
  2078. {
  2079. next if /^;/; # skip comments
  2080. next if /^\s*$/; # skip empty lines
  2081. next if /\x1a/; # skip ^Z
  2082. last if /^ENDCODEPAGE/;
  2083. if (/^CODEPAGE\s+(\d+)/)
  2084. {
  2085. $codepage = $1;
  2086. next;
  2087. }
  2088. if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
  2089. {
  2090. $width = $1;
  2091. $default_char = hex $2;
  2092. $default_wchar = hex $3;
  2093. next;
  2094. }
  2095. if (/^(MBTABLE|GLYPHTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
  2096. {
  2097. $state = $1;
  2098. $count = $2;
  2099. next;
  2100. }
  2101. if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
  2102. {
  2103. if ($state eq "MBTABLE")
  2104. {
  2105. my $cp = hex $1;
  2106. my $uni = hex $2;
  2107. $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
  2108. next;
  2109. }
  2110. if ($state eq "GLYPHTABLE")
  2111. {
  2112. my $cp = hex $1;
  2113. my $uni = hex $2;
  2114. $glyph2uni[$cp] = $uni unless defined($glyph2uni[$cp]);
  2115. next;
  2116. }
  2117. if ($state eq "WCTABLE")
  2118. {
  2119. my $uni = hex $1;
  2120. my $cp = hex $2;
  2121. $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
  2122. next;
  2123. }
  2124. if ($state eq "DBCSRANGE")
  2125. {
  2126. my $start = hex $1;
  2127. my $end = hex $2;
  2128. for (my $i = $start; $i <= $end; $i++) { add_lead_byte( $i ); }
  2129. $lb_cur = $start;
  2130. $lb_end = $end;
  2131. next;
  2132. }
  2133. if ($state eq "DBCSTABLE")
  2134. {
  2135. my $mb = hex $1;
  2136. my $uni = hex $2;
  2137. my $cp = ($lb_cur << 8) | $mb;
  2138. $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
  2139. if (!--$count)
  2140. {
  2141. if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
  2142. }
  2143. next;
  2144. }
  2145. }
  2146. die "$filename: Unrecognized line $_\n";
  2147. }
  2148. close $INPUT;
  2149. output_codepage_file( $codepage );
  2150. if ($codepage == 949) { dump_krwansung_codepage( @uni2cp ); }
  2151. }
  2152. ################################################################
  2153. # align a string length
  2154. sub align_string($$)
  2155. {
  2156. my ($align, $str) = @_;
  2157. $str .= pack "C*", (0) x ($align - length($str) % $align) if length($str) % $align;
  2158. return $str;
  2159. }
  2160. ################################################################
  2161. # pack a GUID string
  2162. sub pack_guid($)
  2163. {
  2164. $_ = shift;
  2165. /([0-9A-Fa-f]{8})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})/;
  2166. return pack "L<S<2C8", hex $1, hex $2, hex $3, hex $4, hex $5, hex $6, hex $7, hex $8, hex $9, hex $10, hex $11;
  2167. }
  2168. ################################################################
  2169. # comparison function for compression sort
  2170. sub cmp_compression
  2171. {
  2172. return scalar @{$a} <=> scalar @{$b} ||
  2173. $a->[4] <=> $b->[4] ||
  2174. $a->[5] <=> $b->[5] ||
  2175. $a->[6] <=> $b->[6] ||
  2176. $a->[7] <=> $b->[7] ||
  2177. $a->[8] <=> $b->[8] ||
  2178. $a->[9] <=> $b->[9] ||
  2179. $a->[10] <=> $b->[10] ||
  2180. $a->[11] <=> $b->[11] ||
  2181. $a->[12] <=> $b->[12];
  2182. }
  2183. ################################################################
  2184. # build a binary sort keys table
  2185. sub dump_sortkey_table($$)
  2186. {
  2187. my ($filename, $download) = @_;
  2188. my @keys;
  2189. my ($part, $section, $subsection, $guid, $version, $ling_flag);
  2190. my @multiple_weights;
  2191. my @expansions;
  2192. my @compressions;
  2193. my %exceptions;
  2194. my %guids;
  2195. my %compr_flags;
  2196. my %locales;
  2197. my $default_guid = "00000001-57ee-1e5c-00b4-d0000bb1e11e";
  2198. my $jamostr = "";
  2199. my $re_hex = '0x[0-9A-Fa-f]+';
  2200. my $re_key = '(\d+\s+\d+\s+\d+\s+\d+)';
  2201. $guids{$default_guid} = { };
  2202. my %flags = ( "HAS_3_BYTE_WEIGHTS" => 0x01, "REVERSEDIACRITICS" => 0x10, "DOUBLECOMPRESSION" => 0x20, "INVERSECASING" => 0x40 );
  2203. my $KEYS = open_data_file( $MSDATA, $download );
  2204. printf "Building $filename\n";
  2205. while (<$KEYS>)
  2206. {
  2207. s/\s*;.*$//;
  2208. next if /^\s*$/; # skip empty lines
  2209. if (/^\s*(SORTKEY|SORTTABLES)/)
  2210. {
  2211. $part = $1;
  2212. next;
  2213. }
  2214. if (/^\s*(ENDSORTKEY|ENDSORTTABLES)/)
  2215. {
  2216. $part = $section = "";
  2217. next;
  2218. }
  2219. if (/^\s*(DEFAULT|RELEASE|REVERSEDIACRITICS|DOUBLECOMPRESSION|INVERSECASING|MULTIPLEWEIGHTS|EXPANSION|COMPATIBILITY|COMPRESSION|EXCEPTION|JAMOSORT)\s+/)
  2220. {
  2221. $section = $1;
  2222. $guid = undef;
  2223. next;
  2224. }
  2225. next unless $part;
  2226. if ("$part.$section" eq "SORTKEY.DEFAULT")
  2227. {
  2228. if (/^\s*($re_hex)\s+$re_key/)
  2229. {
  2230. $keys[hex $1] = [ split(/\s+/,$2) ];
  2231. next;
  2232. }
  2233. }
  2234. elsif ("$part.$section" eq "SORTTABLES.RELEASE")
  2235. {
  2236. if (/^\s*NLSVERSION\s+0x([0-9A-Fa-f]+)/)
  2237. {
  2238. $version = hex $1;
  2239. next;
  2240. }
  2241. if (/^\s*DEFINEDVERSION\s+0x([0-9A-Fa-f]+)/)
  2242. {
  2243. # ignore for now
  2244. next;
  2245. }
  2246. }
  2247. elsif ("$part.$section" eq "SORTTABLES.REVERSEDIACRITICS" ||
  2248. "$part.$section" eq "SORTTABLES.DOUBLECOMPRESSION" ||
  2249. "$part.$section" eq "SORTTABLES.INVERSECASING")
  2250. {
  2251. if (/^\s*SORTGUID\s+([-0-9A-Fa-f]+)/)
  2252. {
  2253. $guid = lc $1;
  2254. $guids{$guid} = { } unless defined $guids{$guid};
  2255. $guids{$guid}->{flags} |= $flags{$section};
  2256. next;
  2257. }
  2258. if (/^\s*LOCALENAME\s+([A-Za-z0-9-_]+)/)
  2259. {
  2260. $locales{$1} = $guid;
  2261. next;
  2262. }
  2263. }
  2264. elsif ("$part.$section" eq "SORTTABLES.MULTIPLEWEIGHTS")
  2265. {
  2266. if (/^\s*(\d+)\s+(\d+)/)
  2267. {
  2268. push @multiple_weights, $1, $2;
  2269. next;
  2270. }
  2271. }
  2272. elsif ("$part.$section" eq "SORTTABLES.EXPANSION")
  2273. {
  2274. if (/^\s*0x([0-9A-Fa-f]+)\s+0x([0-9A-Fa-f]+)\s+0x([0-9A-Fa-f]+)/)
  2275. {
  2276. my $pos = scalar @expansions / 2;
  2277. $keys[hex $1] = [ 2, 0, $pos & 0xff, $pos >> 8 ] unless defined $keys[hex $1];
  2278. push @expansions, hex $2, hex $3;
  2279. next;
  2280. }
  2281. }
  2282. elsif ("$part.$section" eq "SORTTABLES.COMPATIBILITY")
  2283. {
  2284. if (/^\s*0x([0-9A-Fa-f]+)\s+0x([0-9A-Fa-f]+)/)
  2285. {
  2286. $keys[hex $1] = $keys[hex $2];
  2287. next;
  2288. }
  2289. }
  2290. elsif ("$part.$section" eq "SORTTABLES.COMPRESSION")
  2291. {
  2292. if (/^\s*SORTGUID\s+([-0-9A-Fa-f]+)\s+\d*\s*([A-Z0-9_]+)?/)
  2293. {
  2294. if ($subsection || !$guid) # start a new one
  2295. {
  2296. $guid = lc $1;
  2297. $subsection = "";
  2298. $guids{$guid} = { } unless defined $guids{$guid};
  2299. $guids{$guid}->{flags} |= $flags{$2} if $2;
  2300. $guids{$guid}->{compr} = @compressions;
  2301. $exceptions{"$guid-"} = [ ] unless defined $exceptions{"$guid-"};
  2302. $compr_flags{$guid} = [ ] unless defined $compr_flags{$guid};
  2303. push @compressions, [ ];
  2304. }
  2305. else # merge with current one
  2306. {
  2307. $guids{lc $1} = { } unless defined $guids{lc $1};
  2308. $guids{lc $1}->{flags} |= $flags{$2} if $2;
  2309. $guids{lc $1}->{compr} = $guids{$guid}->{compr};
  2310. $compr_flags{lc $1} = $compr_flags{$guid};
  2311. }
  2312. next;
  2313. }
  2314. if (/^\s*LOCALENAME\s+([A-Za-z0-9-_]+)/)
  2315. {
  2316. $locales{$1} = $guid;
  2317. next;
  2318. }
  2319. if (/^\s*(TWO|THREE|FOUR|FIVE|SIX|SEVEN|EIGHT)/)
  2320. {
  2321. $subsection = $1;
  2322. next;
  2323. }
  2324. if ($subsection && /^\s*(($re_hex\s+){2,8})$re_key/)
  2325. {
  2326. my @comp = map { hex $_; } split(/\s+/,$1);
  2327. push @{$compressions[$#compressions]}, [ split(/\s+/,$3), @comp ];
  2328. # add compression flags
  2329. $compr_flags{$guid}->[$comp[0]] |= @comp >= 6 ? 0xc0 : @comp >= 4 ? 0x80 : 0x40;
  2330. next;
  2331. }
  2332. }
  2333. elsif ("$part.$section" eq "SORTTABLES.EXCEPTION")
  2334. {
  2335. if (/^\s*SORTGUID\s+([-0-9A-Fa-f]+)\s+\d*\s*(LINGUISTIC_CASING)?/)
  2336. {
  2337. $guid = lc $1;
  2338. $guids{$guid} = { } unless defined $guids{lc $1};
  2339. $ling_flag = ($2 ? "+" : "-");
  2340. $exceptions{"$guid$ling_flag"} = [ ] unless defined $exceptions{"$guid$ling_flag"};
  2341. next;
  2342. }
  2343. if (/^\s*LOCALENAME\s+([A-Za-z0-9-_]+)/)
  2344. {
  2345. $locales{$1} = $guid;
  2346. next;
  2347. }
  2348. if (/^\s*($re_hex)\s+$re_key/)
  2349. {
  2350. $exceptions{"$guid$ling_flag"}->[hex $1] = [ split(/\s+/,$2) ];
  2351. next;
  2352. }
  2353. }
  2354. elsif ("$part.$section" eq "SORTTABLES.JAMOSORT")
  2355. {
  2356. if (/^\s*$re_hex\s+(($re_hex\s*){5})/)
  2357. {
  2358. $jamostr .= pack "C8", map { hex $_; } split /\s+/, $1;
  2359. next;
  2360. }
  2361. }
  2362. die "$download: $part.$section: unrecognized line $_\n";
  2363. }
  2364. close $KEYS;
  2365. # Sortkey table
  2366. my $table;
  2367. for (my $i = 0; $i < 0x10000; $i++)
  2368. {
  2369. my @k = defined $keys[$i] ? @{$keys[$i]} : (0) x 4;
  2370. $table .= pack "C4", $k[1], $k[0], $k[2], $k[3];
  2371. }
  2372. foreach my $id (sort keys %exceptions)
  2373. {
  2374. my $pos = length($table) / 4;
  2375. my @exc = @{$exceptions{$id}};
  2376. my @filled;
  2377. my $key = (substr( $id, -1 ) eq "+" ? "ling_except" : "except");
  2378. my $guid = substr( $id, 0, -1 );
  2379. $guids{$guid}->{$key} = $pos;
  2380. $pos += 0x100;
  2381. my @flags = @{$compr_flags{$guid}} if defined $compr_flags{$guid};
  2382. for (my $j = 0; $j < 0x10000; $j++)
  2383. {
  2384. next unless defined $exc[$j] || defined $flags[$j];
  2385. $filled[$j >> 8] = 1;
  2386. $j |= 0xff;
  2387. }
  2388. for (my $j = 0; $j < 0x100; $j++)
  2389. {
  2390. $table .= pack "L<", $filled[$j] ? $pos : $j * 0x100;
  2391. $pos += 0x100 if $filled[$j];
  2392. }
  2393. for (my $j = 0; $j < 0x10000; $j++)
  2394. {
  2395. next unless $filled[$j >> 8];
  2396. my @k = defined $exc[$j] ? @{$exc[$j]} : defined $keys[$j] ? @{$keys[$j]} : (0) x 4;
  2397. $k[3] |= $flags[$j] || 0;
  2398. $table .= pack "C4", $k[1], $k[0], $k[2], $k[3];
  2399. }
  2400. }
  2401. # Case mapping tables
  2402. # standard table
  2403. my @casemaps;
  2404. my @upper = @toupper_table;
  2405. my @lower = @tolower_table;
  2406. remove_linguistic_mappings( \@upper, \@lower );
  2407. $casemaps[0] = pack( "S<*", 1) . dump_binary_case_table( @upper ) . dump_binary_case_table( @lower );
  2408. # linguistic table
  2409. $casemaps[1] = pack( "S<*", 1) . dump_binary_case_table( @toupper_table ) . dump_binary_case_table( @tolower_table );
  2410. # Turkish table
  2411. @upper = @toupper_table;
  2412. @lower = @tolower_table;
  2413. $upper[ord 'i'] = 0x130; # LATIN CAPITAL LETTER I WITH DOT ABOVE
  2414. $lower[ord 'I'] = 0x131; # LATIN SMALL LETTER DOTLESS I
  2415. $casemaps[2] = pack( "S<*", 1) . dump_binary_case_table( @upper ) . dump_binary_case_table( @lower );
  2416. my $casemaps = align_string( 8, $casemaps[0] . $casemaps[1] . $casemaps[2] );
  2417. # Char type table
  2418. my @table;
  2419. my $types = "";
  2420. my %typestr;
  2421. for (my $i = 0; $i < 0x10000; $i++)
  2422. {
  2423. my $str = pack "S<3",
  2424. ($category_table[$i] || 0) & 0xffff,
  2425. defined($direction_table[$i]) ? $c2_types{$direction_table[$i]} : 0,
  2426. ($category_table[$i] || 0) >> 16;
  2427. if (!defined($typestr{$str}))
  2428. {
  2429. $typestr{$str} = length($types) / 6;
  2430. $types .= $str;
  2431. }
  2432. $table[$i] = $typestr{$str};
  2433. }
  2434. my @rows = compress_array( 4096, 0, @table[0..65535] );
  2435. my @array = compress_array( 256, 0, @rows[0..4095] );
  2436. for (my $i = 0; $i < 256; $i++) { $array[$i] *= 2; } # we need byte offsets
  2437. for (my $i = 256; $i < @array; $i++) { $array[$i] += 2 * @array - 4096; }
  2438. my $arraystr = pack("S<*", @array) . pack("C*", @rows[4096..$#rows]);
  2439. my $chartypes = pack "S<2", 4 + length($types) + length($arraystr), 2 + length($types);
  2440. $chartypes = align_string( 8, $chartypes . $types . $arraystr );
  2441. # Sort tables
  2442. # guids
  2443. my $sorttables = pack "L<2", $version, scalar %guids;
  2444. foreach my $id (sort keys %guids)
  2445. {
  2446. my %guid = %{$guids{$id}};
  2447. my $flags = $guid{flags} || 0;
  2448. my $map = length($casemaps[0]) + (defined $guid{ling_except} ? length($casemaps[1]) : 0);
  2449. $sorttables .= pack_guid($id) . pack "L<5",
  2450. $flags,
  2451. defined($guid{compr}) ? $guid{compr} : 0xffffffff,
  2452. $guid{except} || 0,
  2453. $guid{ling_except} || 0,
  2454. $map / 2;
  2455. }
  2456. # expansions
  2457. $sorttables .= pack "L<S<*", scalar @expansions / 2, @expansions;
  2458. # compressions
  2459. $sorttables .= pack "L<", scalar @compressions;
  2460. my $rowstr = "";
  2461. foreach my $c (@compressions)
  2462. {
  2463. my $pos = length($rowstr) / 2;
  2464. my $min = 0xffff;
  2465. my $max = 0;
  2466. my @lengths = (0) x 8;
  2467. foreach my $r (sort cmp_compression @{$c})
  2468. {
  2469. my @row = @{$r};
  2470. $lengths[scalar @row - 6]++;
  2471. foreach my $val (@row[4..$#row])
  2472. {
  2473. $min = $val if $min > $val;
  2474. $max = $val if $max < $val;
  2475. }
  2476. $rowstr .= align_string( 4, pack "S<*", @row[4..$#row] );
  2477. $rowstr .= pack "C4", $row[1], $row[0], $row[2], $row[3];
  2478. }
  2479. $sorttables .= pack "L<S<10", $pos, $min, $max, @lengths;
  2480. }
  2481. $sorttables .= $rowstr;
  2482. # multiple weights
  2483. $sorttables .= align_string( 4, pack "L<C*", scalar @multiple_weights / 2, @multiple_weights );
  2484. # jamo sort
  2485. $sorttables .= pack("L<", length($jamostr) / 8) . $jamostr;
  2486. # Locales
  2487. add_registry_key( "Sorting\\Ids", "{$default_guid}" );
  2488. foreach my $loc (sort keys %locales)
  2489. {
  2490. # skip specific locales that match more general ones
  2491. my @parts = split /[-_]/, $loc;
  2492. next if @parts > 1 && defined($locales{$parts[0]}) && $locales{$parts[0]} eq $locales{$loc};
  2493. next if @parts > 2 && defined($locales{"$parts[0]-$parts[1]"}) && $locales{"$parts[0]-$parts[1]"} eq $locales{$loc};
  2494. add_registry_value( "Sorting\\Ids", $loc, "\{$locales{$loc}\}" );
  2495. }
  2496. # File header
  2497. my @header;
  2498. $header[0] = 16;
  2499. $header[1] = $header[0] + length $table;
  2500. $header[2] = $header[1] + length $casemaps;
  2501. $header[3] = $header[2] + length $chartypes;
  2502. open OUTPUT, ">$filename.new" or die "Cannot create $filename";
  2503. print OUTPUT pack "L<*", @header;
  2504. print OUTPUT $table, $casemaps, $chartypes, $sorttables;
  2505. close OUTPUT;
  2506. save_file($filename);
  2507. }
  2508. ################################################################
  2509. # build the script to create registry keys
  2510. sub dump_registry_script($%)
  2511. {
  2512. my ($filename, %keys) = @_;
  2513. my $indent = 1;
  2514. printf "Building %s\n", $filename;
  2515. open OUTPUT, ">$filename.new" or die "Cannot create $filename";
  2516. print OUTPUT "HKLM\n{\n";
  2517. foreach my $k (split /\\/, "SYSTEM\\CurrentControlSet\\Control\\Nls")
  2518. {
  2519. printf OUTPUT "%*sNoRemove %s\n%*s{\n", 4 * $indent, "", $k, 4 * $indent, "";
  2520. $indent++;
  2521. }
  2522. foreach my $k (sort keys %keys)
  2523. {
  2524. my @subkeys = split /\\/, $k;
  2525. my ($def, @vals) = @{$keys{$k}};
  2526. for (my $i = 0; $i < @subkeys; $i++)
  2527. {
  2528. printf OUTPUT "%*s%s%s\n%*s{\n", 4 * $indent, "", $subkeys[$i],
  2529. $i == $#subkeys && $def ? " = s '$def'" : "", 4 * $indent, "";
  2530. $indent++;
  2531. }
  2532. foreach my $v (sort @vals) { printf OUTPUT "%*sval $v\n", 4 * $indent, ""; }
  2533. for (my $i = 0; $i < @subkeys; $i++) { printf OUTPUT "%*s}\n", 4 * --$indent, ""; }
  2534. }
  2535. while ($indent) { printf OUTPUT "%*s}\n", 4 * --$indent, ""; }
  2536. close OUTPUT;
  2537. save_file($filename);
  2538. }
  2539. ################################################################
  2540. # save a file if modified
  2541. sub save_file($)
  2542. {
  2543. my $file = shift;
  2544. if (-f $file && !system "cmp $file $file.new >/dev/null")
  2545. {
  2546. unlink "$file.new";
  2547. }
  2548. else
  2549. {
  2550. rename "$file.new", "$file";
  2551. }
  2552. }
  2553. ################################################################
  2554. # main routine
  2555. chdir ".." if -f "./make_unicode";
  2556. load_data();
  2557. dump_sortkeys( "dlls/kernelbase/collation.c" );
  2558. dump_bidi_dir_table( "dlls/gdi32/uniscribe/direction.c" );
  2559. dump_bidi_dir_table( "dlls/dwrite/direction.c" );
  2560. dump_digit_folding( "dlls/kernelbase/digitmap.c" );
  2561. dump_mirroring( "dlls/gdi32/uniscribe/mirror.c" );
  2562. dump_mirroring( "dlls/dwrite/mirror.c" );
  2563. dump_bracket( "dlls/gdi32/uniscribe/bracket.c" );
  2564. dump_bracket( "dlls/dwrite/bracket.c" );
  2565. dump_shaping( "dlls/gdi32/uniscribe/shaping.c" );
  2566. dump_arabic_shaping( "dlls/dwrite/shapers/arabic_table.c" );
  2567. dump_linebreak( "dlls/gdi32/uniscribe/linebreak.c" );
  2568. dump_linebreak( "dlls/dwrite/linebreak.c" );
  2569. dump_scripts( "dlls/dwrite/scripts" );
  2570. dump_indic( "dlls/gdi32/uniscribe/indicsyllable.c" );
  2571. dump_vertical( "dlls/win32u/vertical.c", 1 );
  2572. dump_vertical( "dlls/wineps.drv/vertical.c", 0 );
  2573. dump_intl_nls("nls/l_intl.nls");
  2574. dump_norm_table( "nls/normnfc.nls" );
  2575. dump_norm_table( "nls/normnfd.nls" );
  2576. dump_norm_table( "nls/normnfkc.nls" );
  2577. dump_norm_table( "nls/normnfkd.nls" );
  2578. dump_norm_table( "nls/normidna.nls" );
  2579. dump_sortkey_table( "nls/sortdefault.nls", "Windows 10 Sorting Weight Table.txt" );
  2580. foreach my $file (@allfiles) { dump_msdata_codepage( $file ); }
  2581. dump_eucjp_codepage();
  2582. dump_registry_script( "dlls/kernelbase/kernelbase.rgs", %registry_keys );
  2583. exit 0;
  2584. # Local Variables:
  2585. # compile-command: "./make_unicode"
  2586. # End: