Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CharScript.java 35KB

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