make_unicode 90 KB

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