You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.complexscripts.util;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.apache.fop.util.CharUtilities;
  26. /**
  27. * <p>Script related utilities.</p>
  28. *
  29. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  30. */
  31. public final class CharScript {
  32. //
  33. // The following script codes are based on ISO 15924. Codes less than 1000 are
  34. // official assignments from 15924; those equal to or greater than 1000 are FOP
  35. // implementation specific.
  36. //
  37. /** hebrew script constant */
  38. public static final int SCRIPT_HEBREW = 125; // 'hebr'
  39. /** mongolian script constant */
  40. public static final int SCRIPT_MONGOLIAN = 145; // 'mong'
  41. /** arabic script constant */
  42. public static final int SCRIPT_ARABIC = 160; // 'arab'
  43. /** greek script constant */
  44. public static final int SCRIPT_GREEK = 200; // 'grek'
  45. /** latin script constant */
  46. public static final int SCRIPT_LATIN = 215; // 'latn'
  47. /** cyrillic script constant */
  48. public static final int SCRIPT_CYRILLIC = 220; // 'cyrl'
  49. /** georgian script constant */
  50. public static final int SCRIPT_GEORGIAN = 240; // 'geor'
  51. /** bopomofo script constant */
  52. public static final int SCRIPT_BOPOMOFO = 285; // 'bopo'
  53. /** hangul script constant */
  54. public static final int SCRIPT_HANGUL = 286; // 'hang'
  55. /** gurmukhi script constant */
  56. public static final int SCRIPT_GURMUKHI = 310; // 'guru'
  57. /** gurmukhi 2 script constant */
  58. public static final int SCRIPT_GURMUKHI_2 = 1310; // 'gur2' -- MSFT (pseudo) script tag for variant shaping semantics
  59. /** devanagari script constant */
  60. public static final int SCRIPT_DEVANAGARI = 315; // 'deva'
  61. /** devanagari 2 script constant */
  62. public static final int SCRIPT_DEVANAGARI_2 = 1315; // 'dev2' -- MSFT (pseudo) script tag for variant shaping semantics
  63. /** gujarati script constant */
  64. public static final int SCRIPT_GUJARATI = 320; // 'gujr'
  65. /** gujarati 2 script constant */
  66. public static final int SCRIPT_GUJARATI_2 = 1320; // 'gjr2' -- MSFT (pseudo) script tag for variant shaping semantics
  67. /** bengali script constant */
  68. public static final int SCRIPT_BENGALI = 326; // 'beng'
  69. /** bengali 2 script constant */
  70. public static final int SCRIPT_BENGALI_2 = 1326; // 'bng2' -- MSFT (pseudo) script tag for variant shaping semantics
  71. /** oriya script constant */
  72. public static final int SCRIPT_ORIYA = 327; // 'orya'
  73. /** oriya 2 script constant */
  74. public static final int SCRIPT_ORIYA_2 = 1327; // 'ory2' -- MSFT (pseudo) script tag for variant shaping semantics
  75. /** tibetan script constant */
  76. public static final int SCRIPT_TIBETAN = 330; // 'tibt'
  77. /** telugu script constant */
  78. public static final int SCRIPT_TELUGU = 340; // 'telu'
  79. /** telugu 2 script constant */
  80. public static final int SCRIPT_TELUGU_2 = 1340; // 'tel2' -- MSFT (pseudo) script tag for variant shaping semantics
  81. /** kannada script constant */
  82. public static final int SCRIPT_KANNADA = 345; // 'knda'
  83. /** kannada 2 script constant */
  84. public static final int SCRIPT_KANNADA_2 = 1345; // 'knd2' -- MSFT (pseudo) script tag for variant shaping semantics
  85. /** tamil script constant */
  86. public static final int SCRIPT_TAMIL = 346; // 'taml'
  87. /** tamil 2 script constant */
  88. public static final int SCRIPT_TAMIL_2 = 1346; // 'tml2' -- MSFT (pseudo) script tag for variant shaping semantics
  89. /** malayalam script constant */
  90. public static final int SCRIPT_MALAYALAM = 347; // 'mlym'
  91. /** malayalam 2 script constant */
  92. public static final int SCRIPT_MALAYALAM_2 = 1347; // 'mlm2' -- MSFT (pseudo) script tag for variant shaping semantics
  93. /** sinhalese script constant */
  94. public static final int SCRIPT_SINHALESE = 348; // 'sinh'
  95. /** burmese script constant */
  96. public static final int SCRIPT_BURMESE = 350; // 'mymr'
  97. /** thai script constant */
  98. public static final int SCRIPT_THAI = 352; // 'thai'
  99. /** khmer script constant */
  100. public static final int SCRIPT_KHMER = 355; // 'khmr'
  101. /** lao script constant */
  102. public static final int SCRIPT_LAO = 356; // 'laoo'
  103. /** hiragana script constant */
  104. public static final int SCRIPT_HIRAGANA = 410; // 'hira'
  105. /** ethiopic script constant */
  106. public static final int SCRIPT_ETHIOPIC = 430; // 'ethi'
  107. /** han script constant */
  108. public static final int SCRIPT_HAN = 500; // 'hani'
  109. /** katakana script constant */
  110. public static final int SCRIPT_KATAKANA = 410; // 'kana'
  111. /** math script constant */
  112. public static final int SCRIPT_MATH = 995; // 'zmth'
  113. /** symbol script constant */
  114. public static final int SCRIPT_SYMBOL = 996; // 'zsym'
  115. /** undetermined script constant */
  116. public static final int SCRIPT_UNDETERMINED = 998; // 'zyyy'
  117. /** uncoded script constant */
  118. public static final int SCRIPT_UNCODED = 999; // 'zzzz'
  119. /**
  120. * A static (class) parameter indicating whether V2 indic shaping
  121. * rules apply or not, with default being <code>true</code>.
  122. */
  123. private static final boolean USE_V2_INDIC = true;
  124. private CharScript() {
  125. }
  126. /**
  127. * Determine if character c is punctuation.
  128. * @param c a character represented as a unicode scalar value
  129. * @return true if character is punctuation
  130. */
  131. public static boolean isPunctuation(int c) {
  132. if ((c >= 0x0021) && (c <= 0x002F)) { // basic latin punctuation
  133. return true;
  134. } else if ((c >= 0x003A) && (c <= 0x0040)) { // basic latin punctuation
  135. return true;
  136. } else if ((c >= 0x005F) && (c <= 0x0060)) { // basic latin punctuation
  137. return true;
  138. } else if ((c >= 0x007E) && (c <= 0x007E)) { // basic latin punctuation
  139. return true;
  140. } else if ((c >= 0x007E) && (c <= 0x007E)) { // basic latin punctuation
  141. return true;
  142. } else if ((c >= 0x00A1) && (c <= 0x00BF)) { // latin supplement punctuation
  143. return true;
  144. } else if ((c >= 0x00D7) && (c <= 0x00D7)) { // latin supplement punctuation
  145. return true;
  146. } else if ((c >= 0x00F7) && (c <= 0x00F7)) { // latin supplement punctuation
  147. return true;
  148. } else if ((c >= 0x2000) && (c <= 0x206F)) { // general punctuation
  149. return true;
  150. } else { // [TBD] - not complete
  151. return false;
  152. }
  153. }
  154. /**
  155. * Determine if character c is a digit.
  156. * @param c a character represented as a unicode scalar value
  157. * @return true if character is a digit
  158. */
  159. public static boolean isDigit(int c) {
  160. if ((c >= 0x0030) && (c <= 0x0039)) { // basic latin digits
  161. return true;
  162. } else { // [TBD] - not complete
  163. return false;
  164. }
  165. }
  166. /**
  167. * Determine if character c belong to the hebrew script.
  168. * @param c a character represented as a unicode scalar value
  169. * @return true if character belongs to hebrew script
  170. */
  171. public static boolean isHebrew(int c) {
  172. if ((c >= 0x0590) && (c <= 0x05FF)) { // hebrew block
  173. return true;
  174. } else if ((c >= 0xFB00) && (c <= 0xFB4F)) { // hebrew presentation forms block
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. /**
  181. * Determine if character c belong to the mongolian script.
  182. * @param c a character represented as a unicode scalar value
  183. * @return true if character belongs to mongolian script
  184. */
  185. public static boolean isMongolian(int c) {
  186. if ((c >= 0x1800) && (c <= 0x18AF)) { // mongolian block
  187. return true;
  188. } else {
  189. return false;
  190. }
  191. }
  192. /**
  193. * Determine if character c belong to the arabic script.
  194. * @param c a character represented as a unicode scalar value
  195. * @return true if character belongs to arabic script
  196. */
  197. public static boolean isArabic(int c) {
  198. if ((c >= 0x0600) && (c <= 0x06FF)) { // arabic block
  199. return true;
  200. } else if ((c >= 0x0750) && (c <= 0x077F)) { // arabic supplement block
  201. return true;
  202. } else if ((c >= 0xFB50) && (c <= 0xFDFF)) { // arabic presentation forms a block
  203. return true;
  204. } else if ((c >= 0xFE70) && (c <= 0xFEFF)) { // arabic presentation forms b block
  205. return true;
  206. } else {
  207. return false;
  208. }
  209. }
  210. /**
  211. * Determine if character c belong to the greek script.
  212. * @param c a character represented as a unicode scalar value
  213. * @return true if character belongs to greek script
  214. */
  215. public static boolean isGreek(int c) {
  216. if ((c >= 0x0370) && (c <= 0x03FF)) { // greek (and coptic) block
  217. return true;
  218. } else if ((c >= 0x1F00) && (c <= 0x1FFF)) { // greek extended block
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. }
  224. /**
  225. * Determine if character c belong to the latin script.
  226. * @param c a character represented as a unicode scalar value
  227. * @return true if character belongs to latin script
  228. */
  229. public static boolean isLatin(int c) {
  230. if ((c >= 0x0041) && (c <= 0x005A)) { // basic latin upper case
  231. return true;
  232. } else if ((c >= 0x0061) && (c <= 0x007A)) { // basic latin lower case
  233. return true;
  234. } else if ((c >= 0x00C0) && (c <= 0x00D6)) { // latin supplement upper case
  235. return true;
  236. } else if ((c >= 0x00D8) && (c <= 0x00DF)) { // latin supplement upper case
  237. return true;
  238. } else if ((c >= 0x00E0) && (c <= 0x00F6)) { // latin supplement lower case
  239. return true;
  240. } else if ((c >= 0x00F8) && (c <= 0x00FF)) { // latin supplement lower case
  241. return true;
  242. } else if ((c >= 0x0100) && (c <= 0x017F)) { // latin extended a
  243. return true;
  244. } else if ((c >= 0x0180) && (c <= 0x024F)) { // latin extended b
  245. return true;
  246. } else if ((c >= 0x1E00) && (c <= 0x1EFF)) { // latin extended additional
  247. return true;
  248. } else if ((c >= 0x2C60) && (c <= 0x2C7F)) { // latin extended c
  249. return true;
  250. } else if ((c >= 0xA720) && (c <= 0xA7FF)) { // latin extended d
  251. return true;
  252. } else if ((c >= 0xFB00) && (c <= 0xFB0F)) { // latin ligatures
  253. return true;
  254. } else {
  255. return false;
  256. }
  257. }
  258. /**
  259. * Determine if character c belong to the cyrillic script.
  260. * @param c a character represented as a unicode scalar value
  261. * @return true if character belongs to cyrillic script
  262. */
  263. public static boolean isCyrillic(int c) {
  264. if ((c >= 0x0400) && (c <= 0x04FF)) { // cyrillic block
  265. return true;
  266. } else if ((c >= 0x0500) && (c <= 0x052F)) { // cyrillic supplement block
  267. return true;
  268. } else if ((c >= 0x2DE0) && (c <= 0x2DFF)) { // cyrillic extended-a block
  269. return true;
  270. } else if ((c >= 0xA640) && (c <= 0xA69F)) { // cyrillic extended-b block
  271. return true;
  272. } else {
  273. return false;
  274. }
  275. }
  276. /**
  277. * Determine if character c belong to the georgian script.
  278. * @param c a character represented as a unicode scalar value
  279. * @return true if character belongs to georgian script
  280. */
  281. public static boolean isGeorgian(int c) {
  282. if ((c >= 0x10A0) && (c <= 0x10FF)) { // georgian block
  283. return true;
  284. } else if ((c >= 0x2D00) && (c <= 0x2D2F)) { // georgian supplement block
  285. return true;
  286. } else {
  287. return false;
  288. }
  289. }
  290. /**
  291. * Determine if character c belong to the hangul script.
  292. * @param c a character represented as a unicode scalar value
  293. * @return true if character belongs to hangul script
  294. */
  295. public static boolean isHangul(int c) {
  296. if ((c >= 0x1100) && (c <= 0x11FF)) { // hangul jamo
  297. return true;
  298. } else if ((c >= 0x3130) && (c <= 0x318F)) { // hangul compatibility jamo
  299. return true;
  300. } else if ((c >= 0xA960) && (c <= 0xA97F)) { // hangul jamo extended a
  301. return true;
  302. } else if ((c >= 0xAC00) && (c <= 0xD7A3)) { // hangul syllables
  303. return true;
  304. } else if ((c >= 0xD7B0) && (c <= 0xD7FF)) { // hangul jamo extended a
  305. return true;
  306. } else {
  307. return false;
  308. }
  309. }
  310. /**
  311. * Determine if character c belong to the gurmukhi script.
  312. * @param c a character represented as a unicode scalar value
  313. * @return true if character belongs to gurmukhi script
  314. */
  315. public static boolean isGurmukhi(int c) {
  316. if ((c >= 0x0A00) && (c <= 0x0A7F)) { // gurmukhi block
  317. return true;
  318. } else {
  319. return false;
  320. }
  321. }
  322. /**
  323. * Determine if character c belong to the devanagari script.
  324. * @param c a character represented as a unicode scalar value
  325. * @return true if character belongs to devanagari script
  326. */
  327. public static boolean isDevanagari(int c) {
  328. if ((c >= 0x0900) && (c <= 0x097F)) { // devangari block
  329. return true;
  330. } else if ((c >= 0xA8E0) && (c <= 0xA8FF)) { // devangari extended block
  331. return true;
  332. } else {
  333. return false;
  334. }
  335. }
  336. /**
  337. * Determine if character c belong to the gujarati script.
  338. * @param c a character represented as a unicode scalar value
  339. * @return true if character belongs to gujarati script
  340. */
  341. public static boolean isGujarati(int c) {
  342. if ((c >= 0x0A80) && (c <= 0x0AFF)) { // gujarati block
  343. return true;
  344. } else {
  345. return false;
  346. }
  347. }
  348. /**
  349. * Determine if character c belong to the bengali script.
  350. * @param c a character represented as a unicode scalar value
  351. * @return true if character belongs to bengali script
  352. */
  353. public static boolean isBengali(int c) {
  354. if ((c >= 0x0980) && (c <= 0x09FF)) { // bengali block
  355. return true;
  356. } else {
  357. return false;
  358. }
  359. }
  360. /**
  361. * Determine if character c belong to the oriya script.
  362. * @param c a character represented as a unicode scalar value
  363. * @return true if character belongs to oriya script
  364. */
  365. public static boolean isOriya(int c) {
  366. if ((c >= 0x0B00) && (c <= 0x0B7F)) { // oriya block
  367. return true;
  368. } else {
  369. return false;
  370. }
  371. }
  372. /**
  373. * Determine if character c belong to the tibetan script.
  374. * @param c a character represented as a unicode scalar value
  375. * @return true if character belongs to tibetan script
  376. */
  377. public static boolean isTibetan(int c) {
  378. if ((c >= 0x0F00) && (c <= 0x0FFF)) { // tibetan block
  379. return true;
  380. } else {
  381. return false;
  382. }
  383. }
  384. /**
  385. * Determine if character c belong to the telugu script.
  386. * @param c a character represented as a unicode scalar value
  387. * @return true if character belongs to telugu script
  388. */
  389. public static boolean isTelugu(int c) {
  390. if ((c >= 0x0C00) && (c <= 0x0C7F)) { // telugu block
  391. return true;
  392. } else {
  393. return false;
  394. }
  395. }
  396. /**
  397. * Determine if character c belong to the kannada script.
  398. * @param c a character represented as a unicode scalar value
  399. * @return true if character belongs to kannada script
  400. */
  401. public static boolean isKannada(int c) {
  402. if ((c >= 0x0C00) && (c <= 0x0C7F)) { // kannada block
  403. return true;
  404. } else {
  405. return false;
  406. }
  407. }
  408. /**
  409. * Determine if character c belong to the tamil script.
  410. * @param c a character represented as a unicode scalar value
  411. * @return true if character belongs to tamil script
  412. */
  413. public static boolean isTamil(int c) {
  414. if ((c >= 0x0B80) && (c <= 0x0BFF)) { // tamil block
  415. return true;
  416. } else {
  417. return false;
  418. }
  419. }
  420. /**
  421. * Determine if character c belong to the malayalam script.
  422. * @param c a character represented as a unicode scalar value
  423. * @return true if character belongs to malayalam script
  424. */
  425. public static boolean isMalayalam(int c) {
  426. if ((c >= 0x0D00) && (c <= 0x0D7F)) { // malayalam block
  427. return true;
  428. } else {
  429. return false;
  430. }
  431. }
  432. /**
  433. * Determine if character c belong to the sinhalese script.
  434. * @param c a character represented as a unicode scalar value
  435. * @return true if character belongs to sinhalese script
  436. */
  437. public static boolean isSinhalese(int c) {
  438. if ((c >= 0x0D80) && (c <= 0x0DFF)) { // sinhala block
  439. return true;
  440. } else {
  441. return false;
  442. }
  443. }
  444. /**
  445. * Determine if character c belong to the burmese script.
  446. * @param c a character represented as a unicode scalar value
  447. * @return true if character belongs to burmese script
  448. */
  449. public static boolean isBurmese(int c) {
  450. if ((c >= 0x1000) && (c <= 0x109F)) { // burmese (myanmar) block
  451. return true;
  452. } else if ((c >= 0xAA60) && (c <= 0xAA7F)) { // burmese (myanmar) extended block
  453. return true;
  454. } else {
  455. return false;
  456. }
  457. }
  458. /**
  459. * Determine if character c belong to the thai script.
  460. * @param c a character represented as a unicode scalar value
  461. * @return true if character belongs to thai script
  462. */
  463. public static boolean isThai(int c) {
  464. if ((c >= 0x0E00) && (c <= 0x0E7F)) { // thai block
  465. return true;
  466. } else {
  467. return false;
  468. }
  469. }
  470. /**
  471. * Determine if character c belong to the khmer script.
  472. * @param c a character represented as a unicode scalar value
  473. * @return true if character belongs to khmer script
  474. */
  475. public static boolean isKhmer(int c) {
  476. if ((c >= 0x1780) && (c <= 0x17FF)) { // khmer block
  477. return true;
  478. } else if ((c >= 0x19E0) && (c <= 0x19FF)) { // khmer symbols block
  479. return true;
  480. } else {
  481. return false;
  482. }
  483. }
  484. /**
  485. * Determine if character c belong to the lao script.
  486. * @param c a character represented as a unicode scalar value
  487. * @return true if character belongs to lao script
  488. */
  489. public static boolean isLao(int c) {
  490. if ((c >= 0x0E80) && (c <= 0x0EFF)) { // lao block
  491. return true;
  492. } else {
  493. return false;
  494. }
  495. }
  496. /**
  497. * Determine if character c belong to the ethiopic (amharic) script.
  498. * @param c a character represented as a unicode scalar value
  499. * @return true if character belongs to ethiopic (amharic) script
  500. */
  501. public static boolean isEthiopic(int c) {
  502. if ((c >= 0x1200) && (c <= 0x137F)) { // ethiopic block
  503. return true;
  504. } else if ((c >= 0x1380) && (c <= 0x139F)) { // ethoipic supplement block
  505. return true;
  506. } else if ((c >= 0x2D80) && (c <= 0x2DDF)) { // ethoipic extended block
  507. return true;
  508. } else if ((c >= 0xAB00) && (c <= 0xAB2F)) { // ethoipic extended-a block
  509. return true;
  510. } else {
  511. return false;
  512. }
  513. }
  514. /**
  515. * Determine if character c belong to the han (unified cjk) script.
  516. * @param c a character represented as a unicode scalar value
  517. * @return true if character belongs to han (unified cjk) script
  518. */
  519. public static boolean isHan(int c) {
  520. if ((c >= 0x3400) && (c <= 0x4DBF)) {
  521. return true; // cjk unified ideographs extension a
  522. } else if ((c >= 0x4E00) && (c <= 0x9FFF)) {
  523. return true; // cjk unified ideographs
  524. } else if ((c >= 0xF900) && (c <= 0xFAFF)) {
  525. return true; // cjk compatibility ideographs
  526. } else if ((c >= 0x20000) && (c <= 0x2A6DF)) {
  527. return true; // cjk unified ideographs extension b
  528. } else if ((c >= 0x2A700) && (c <= 0x2B73F)) {
  529. return true; // cjk unified ideographs extension c
  530. } else if ((c >= 0x2F800) && (c <= 0x2FA1F)) {
  531. return true; // cjk compatibility ideographs supplement
  532. } else {
  533. return false;
  534. }
  535. }
  536. /**
  537. * Determine if character c belong to the bopomofo script.
  538. * @param c a character represented as a unicode scalar value
  539. * @return true if character belongs to bopomofo script
  540. */
  541. public static boolean isBopomofo(int c) {
  542. if ((c >= 0x3100) && (c <= 0x312F)) {
  543. return true;
  544. } else {
  545. return false;
  546. }
  547. }
  548. /**
  549. * Determine if character c belong to the hiragana script.
  550. * @param c a character represented as a unicode scalar value
  551. * @return true if character belongs to hiragana script
  552. */
  553. public static boolean isHiragana(int c) {
  554. if ((c >= 0x3040) && (c <= 0x309F)) {
  555. return true;
  556. } else {
  557. return false;
  558. }
  559. }
  560. /**
  561. * Determine if character c belong to the katakana script.
  562. * @param c a character represented as a unicode scalar value
  563. * @return true if character belongs to katakana script
  564. */
  565. public static boolean isKatakana(int c) {
  566. if ((c >= 0x30A0) && (c <= 0x30FF)) {
  567. return true;
  568. } else if ((c >= 0x31F0) && (c <= 0x31FF)) {
  569. return true;
  570. } else {
  571. return false;
  572. }
  573. }
  574. /**
  575. * Obtain ISO15924 numeric script code of character. If script is not or cannot be determined,
  576. * then the script code 998 ('zyyy') is returned.
  577. * @param c the character to obtain script
  578. * @return an ISO15924 script code
  579. */
  580. public static int scriptOf(int c) { // [TBD] - needs optimization!!!
  581. if (CharUtilities.isAnySpace(c)) {
  582. return SCRIPT_UNDETERMINED;
  583. } else if (isPunctuation(c)) {
  584. return SCRIPT_UNDETERMINED;
  585. } else if (isDigit(c)) {
  586. return SCRIPT_UNDETERMINED;
  587. } else if (isLatin(c)) {
  588. return SCRIPT_LATIN;
  589. } else if (isCyrillic(c)) {
  590. return SCRIPT_CYRILLIC;
  591. } else if (isGreek(c)) {
  592. return SCRIPT_GREEK;
  593. } else if (isHan(c)) {
  594. return SCRIPT_HAN;
  595. } else if (isBopomofo(c)) {
  596. return SCRIPT_BOPOMOFO;
  597. } else if (isKatakana(c)) {
  598. return SCRIPT_KATAKANA;
  599. } else if (isHiragana(c)) {
  600. return SCRIPT_HIRAGANA;
  601. } else if (isHangul(c)) {
  602. return SCRIPT_HANGUL;
  603. } else if (isArabic(c)) {
  604. return SCRIPT_ARABIC;
  605. } else if (isHebrew(c)) {
  606. return SCRIPT_HEBREW;
  607. } else if (isMongolian(c)) {
  608. return SCRIPT_MONGOLIAN;
  609. } else if (isGeorgian(c)) {
  610. return SCRIPT_GEORGIAN;
  611. } else if (isGurmukhi(c)) {
  612. return useV2IndicRules(SCRIPT_GURMUKHI);
  613. } else if (isDevanagari(c)) {
  614. return useV2IndicRules(SCRIPT_DEVANAGARI);
  615. } else if (isGujarati(c)) {
  616. return useV2IndicRules(SCRIPT_GUJARATI);
  617. } else if (isBengali(c)) {
  618. return useV2IndicRules(SCRIPT_BENGALI);
  619. } else if (isOriya(c)) {
  620. return useV2IndicRules(SCRIPT_ORIYA);
  621. } else if (isTibetan(c)) {
  622. return SCRIPT_TIBETAN;
  623. } else if (isTelugu(c)) {
  624. return useV2IndicRules(SCRIPT_TELUGU);
  625. } else if (isKannada(c)) {
  626. return useV2IndicRules(SCRIPT_KANNADA);
  627. } else if (isTamil(c)) {
  628. return useV2IndicRules(SCRIPT_TAMIL);
  629. } else if (isMalayalam(c)) {
  630. return useV2IndicRules(SCRIPT_MALAYALAM);
  631. } else if (isSinhalese(c)) {
  632. return SCRIPT_SINHALESE;
  633. } else if (isBurmese(c)) {
  634. return SCRIPT_BURMESE;
  635. } else if (isThai(c)) {
  636. return SCRIPT_THAI;
  637. } else if (isKhmer(c)) {
  638. return SCRIPT_KHMER;
  639. } else if (isLao(c)) {
  640. return SCRIPT_LAO;
  641. } else if (isEthiopic(c)) {
  642. return SCRIPT_ETHIOPIC;
  643. } else {
  644. return SCRIPT_UNDETERMINED;
  645. }
  646. }
  647. /**
  648. * Obtain the V2 indic script code corresponding to V1 indic script code SC if
  649. * and only iff V2 indic rules apply; otherwise return SC.
  650. * @param sc a V1 indic script code
  651. * @return either SC or the V2 flavor of SC if V2 indic rules apply
  652. */
  653. public static int useV2IndicRules(int sc) {
  654. if (USE_V2_INDIC) {
  655. return (sc < 1000) ? (sc + 1000) : sc;
  656. } else {
  657. return sc;
  658. }
  659. }
  660. /**
  661. * Obtain the script codes of each character in a character sequence. If script
  662. * is not or cannot be determined for some character, then the script code 998
  663. * ('zyyy') is returned.
  664. * @param cs the character sequence
  665. * @return a (possibly empty) array of script codes
  666. */
  667. public static int[] scriptsOf(CharSequence cs) {
  668. Set s = new HashSet();
  669. for (int i = 0, n = cs.length(); i < n; i++) {
  670. s.add(Integer.valueOf(scriptOf(cs.charAt(i))));
  671. }
  672. int[] sa = new int [ s.size() ];
  673. int ns = 0;
  674. for (Iterator it = s.iterator(); it.hasNext();) {
  675. sa [ ns++ ] = ((Integer) it.next()) .intValue();
  676. }
  677. Arrays.sort(sa);
  678. return sa;
  679. }
  680. /**
  681. * Determine the dominant script of a character sequence.
  682. * @param cs the character sequence
  683. * @return the dominant script or SCRIPT_UNDETERMINED
  684. */
  685. public static int dominantScript(CharSequence cs) {
  686. Map m = new HashMap();
  687. for (int i = 0, n = cs.length(); i < n; i++) {
  688. int c = cs.charAt(i);
  689. int s = scriptOf(c);
  690. Integer k = Integer.valueOf(s);
  691. Integer v = (Integer) m.get(k);
  692. if (v != null) {
  693. m.put(k, Integer.valueOf(v.intValue() + 1));
  694. } else {
  695. m.put(k, Integer.valueOf(0));
  696. }
  697. }
  698. int sMax = -1;
  699. int cMax = -1;
  700. for (Iterator it = m.entrySet().iterator(); it.hasNext();) {
  701. Map.Entry e = (Map.Entry) it.next();
  702. Integer k = (Integer) e.getKey();
  703. int s = k.intValue();
  704. switch (s) {
  705. case SCRIPT_UNDETERMINED:
  706. case SCRIPT_UNCODED:
  707. break;
  708. default:
  709. Integer v = (Integer) e.getValue();
  710. assert v != null;
  711. int c = v.intValue();
  712. if (c > cMax) {
  713. cMax = c;
  714. sMax = s;
  715. }
  716. break;
  717. }
  718. }
  719. if (sMax < 0) {
  720. sMax = SCRIPT_UNDETERMINED;
  721. }
  722. return sMax;
  723. }
  724. /**
  725. * Determine if script tag denotes an 'Indic' script, where a
  726. * script is an 'Indic' script if it is intended to be processed by
  727. * the generic 'Indic' Script Processor.
  728. * @param script a script tag
  729. * @return true if script tag is a designated 'Indic' script
  730. */
  731. public static boolean isIndicScript(String script) {
  732. return isIndicScript(scriptCodeFromTag(script));
  733. }
  734. /**
  735. * Determine if script tag denotes an 'Indic' script, where a
  736. * script is an 'Indic' script if it is intended to be processed by
  737. * the generic 'Indic' Script Processor.
  738. * @param script a script code
  739. * @return true if script code is a designated 'Indic' script
  740. */
  741. public static boolean isIndicScript(int script) {
  742. switch (script) {
  743. case SCRIPT_BENGALI:
  744. case SCRIPT_BENGALI_2:
  745. case SCRIPT_BURMESE:
  746. case SCRIPT_DEVANAGARI:
  747. case SCRIPT_DEVANAGARI_2:
  748. case SCRIPT_GUJARATI:
  749. case SCRIPT_GUJARATI_2:
  750. case SCRIPT_GURMUKHI:
  751. case SCRIPT_GURMUKHI_2:
  752. case SCRIPT_KANNADA:
  753. case SCRIPT_KANNADA_2:
  754. case SCRIPT_MALAYALAM:
  755. case SCRIPT_MALAYALAM_2:
  756. case SCRIPT_ORIYA:
  757. case SCRIPT_ORIYA_2:
  758. case SCRIPT_TAMIL:
  759. case SCRIPT_TAMIL_2:
  760. case SCRIPT_TELUGU:
  761. case SCRIPT_TELUGU_2:
  762. return true;
  763. default:
  764. return false;
  765. }
  766. }
  767. /**
  768. * Determine the script tag associated with an internal script code.
  769. * @param code the script code
  770. * @return a script tag
  771. */
  772. public static String scriptTagFromCode(int code) {
  773. Map<Integer, String> m = getScriptTagsMap();
  774. if (m != null) {
  775. String tag;
  776. if ((tag = m.get(Integer.valueOf(code))) != null) {
  777. return tag;
  778. } else {
  779. return "";
  780. }
  781. } else {
  782. return "";
  783. }
  784. }
  785. /**
  786. * Determine the internal script code associated with a script tag.
  787. * @param tag the script tag
  788. * @return a script code
  789. */
  790. public static int scriptCodeFromTag(String tag) {
  791. Map<String, Integer> m = getScriptCodeMap();
  792. if (m != null) {
  793. Integer c;
  794. if ((c = m.get(tag)) != null) {
  795. return (int) c;
  796. } else {
  797. return SCRIPT_UNDETERMINED;
  798. }
  799. } else {
  800. return SCRIPT_UNDETERMINED;
  801. }
  802. }
  803. private static Map<Integer, String> scriptTagsMap = null;
  804. private static Map<String, Integer> scriptCodeMap = null;
  805. private static void putScriptTag(Map tm, Map cm, int code, String tag) {
  806. assert tag != null;
  807. assert tag.length() != 0;
  808. assert code >= 0;
  809. assert code < 2000;
  810. tm.put(Integer.valueOf(code), tag);
  811. cm.put(tag, Integer.valueOf(code));
  812. }
  813. private static void makeScriptMaps() {
  814. HashMap<Integer, String> tm = new HashMap<Integer, String>();
  815. HashMap<String, Integer> cm = new HashMap<String, Integer>();
  816. putScriptTag(tm, cm, SCRIPT_HEBREW, "hebr");
  817. putScriptTag(tm, cm, SCRIPT_MONGOLIAN, "mong");
  818. putScriptTag(tm, cm, SCRIPT_ARABIC, "arab");
  819. putScriptTag(tm, cm, SCRIPT_GREEK, "grek");
  820. putScriptTag(tm, cm, SCRIPT_LATIN, "latn");
  821. putScriptTag(tm, cm, SCRIPT_CYRILLIC, "cyrl");
  822. putScriptTag(tm, cm, SCRIPT_GEORGIAN, "geor");
  823. putScriptTag(tm, cm, SCRIPT_BOPOMOFO, "bopo");
  824. putScriptTag(tm, cm, SCRIPT_HANGUL, "hang");
  825. putScriptTag(tm, cm, SCRIPT_GURMUKHI, "guru");
  826. putScriptTag(tm, cm, SCRIPT_GURMUKHI_2, "gur2");
  827. putScriptTag(tm, cm, SCRIPT_DEVANAGARI, "deva");
  828. putScriptTag(tm, cm, SCRIPT_DEVANAGARI_2, "dev2");
  829. putScriptTag(tm, cm, SCRIPT_GUJARATI, "gujr");
  830. putScriptTag(tm, cm, SCRIPT_GUJARATI_2, "gjr2");
  831. putScriptTag(tm, cm, SCRIPT_BENGALI, "beng");
  832. putScriptTag(tm, cm, SCRIPT_BENGALI_2, "bng2");
  833. putScriptTag(tm, cm, SCRIPT_ORIYA, "orya");
  834. putScriptTag(tm, cm, SCRIPT_ORIYA_2, "ory2");
  835. putScriptTag(tm, cm, SCRIPT_TIBETAN, "tibt");
  836. putScriptTag(tm, cm, SCRIPT_TELUGU, "telu");
  837. putScriptTag(tm, cm, SCRIPT_TELUGU_2, "tel2");
  838. putScriptTag(tm, cm, SCRIPT_KANNADA, "knda");
  839. putScriptTag(tm, cm, SCRIPT_KANNADA_2, "knd2");
  840. putScriptTag(tm, cm, SCRIPT_TAMIL, "taml");
  841. putScriptTag(tm, cm, SCRIPT_TAMIL_2, "tml2");
  842. putScriptTag(tm, cm, SCRIPT_MALAYALAM, "mlym");
  843. putScriptTag(tm, cm, SCRIPT_MALAYALAM_2, "mlm2");
  844. putScriptTag(tm, cm, SCRIPT_SINHALESE, "sinh");
  845. putScriptTag(tm, cm, SCRIPT_BURMESE, "mymr");
  846. putScriptTag(tm, cm, SCRIPT_THAI, "thai");
  847. putScriptTag(tm, cm, SCRIPT_KHMER, "khmr");
  848. putScriptTag(tm, cm, SCRIPT_LAO, "laoo");
  849. putScriptTag(tm, cm, SCRIPT_HIRAGANA, "hira");
  850. putScriptTag(tm, cm, SCRIPT_ETHIOPIC, "ethi");
  851. putScriptTag(tm, cm, SCRIPT_HAN, "hani");
  852. putScriptTag(tm, cm, SCRIPT_KATAKANA, "kana");
  853. putScriptTag(tm, cm, SCRIPT_MATH, "zmth");
  854. putScriptTag(tm, cm, SCRIPT_SYMBOL, "zsym");
  855. putScriptTag(tm, cm, SCRIPT_UNDETERMINED, "zyyy");
  856. putScriptTag(tm, cm, SCRIPT_UNCODED, "zzzz");
  857. scriptTagsMap = tm;
  858. scriptCodeMap = cm;
  859. }
  860. private static Map<Integer, String> getScriptTagsMap() {
  861. if (scriptTagsMap == null) {
  862. makeScriptMaps();
  863. }
  864. return scriptTagsMap;
  865. }
  866. private static Map<String, Integer> getScriptCodeMap() {
  867. if (scriptCodeMap == null) {
  868. makeScriptMaps();
  869. }
  870. return scriptCodeMap;
  871. }
  872. }