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.

GlyphSequence.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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.nio.IntBuffer;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23. // CSOFF: LineLengthCheck
  24. /**
  25. * <p>A GlyphSequence encapsulates a sequence of character codes, a sequence of glyph codes,
  26. * and a sequence of character associations, where, for each glyph in the sequence of glyph
  27. * codes, there is a corresponding character association. Character associations server to
  28. * relate the glyph codes in a glyph sequence to the specific characters in an original
  29. * character code sequence with which the glyph codes are associated.</p>
  30. *
  31. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  32. */
  33. public class GlyphSequence implements Cloneable {
  34. /** default character buffer capacity in case new character buffer is created */
  35. private static final int DEFAULT_CHARS_CAPACITY = 8;
  36. /** character buffer */
  37. private IntBuffer characters;
  38. /** glyph buffer */
  39. private IntBuffer glyphs;
  40. /** association list */
  41. private List associations;
  42. /** predications flag */
  43. private boolean predications;
  44. /**
  45. * Instantiate a glyph sequence, reusing (i.e., not copying) the referenced
  46. * character and glyph buffers and associations. If characters is null, then
  47. * an empty character buffer is created. If glyphs is null, then a glyph buffer
  48. * is created whose capacity is that of the character buffer. If associations is
  49. * null, then identity associations are created.
  50. * @param characters a (possibly null) buffer of associated (originating) characters
  51. * @param glyphs a (possibly null) buffer of glyphs
  52. * @param associations a (possibly null) array of glyph to character associations
  53. * @param predications true if predications are enabled
  54. */
  55. public GlyphSequence(IntBuffer characters, IntBuffer glyphs, List associations, boolean predications) {
  56. if (characters == null) {
  57. characters = IntBuffer.allocate(DEFAULT_CHARS_CAPACITY);
  58. }
  59. if (glyphs == null) {
  60. glyphs = IntBuffer.allocate(characters.capacity());
  61. }
  62. if (associations == null) {
  63. associations = makeIdentityAssociations(characters.limit(), glyphs.limit());
  64. }
  65. this.characters = characters;
  66. this.glyphs = glyphs;
  67. this.associations = associations;
  68. this.predications = predications;
  69. }
  70. /**
  71. * Instantiate a glyph sequence, reusing (i.e., not copying) the referenced
  72. * character and glyph buffers and associations. If characters is null, then
  73. * an empty character buffer is created. If glyphs is null, then a glyph buffer
  74. * is created whose capacity is that of the character buffer. If associations is
  75. * null, then identity associations are created.
  76. * @param characters a (possibly null) buffer of associated (originating) characters
  77. * @param glyphs a (possibly null) buffer of glyphs
  78. * @param associations a (possibly null) array of glyph to character associations
  79. */
  80. public GlyphSequence(IntBuffer characters, IntBuffer glyphs, List associations) {
  81. this (characters, glyphs, associations, false);
  82. }
  83. /**
  84. * Instantiate a glyph sequence using an existing glyph sequence, where the new glyph sequence shares
  85. * the character array of the existing sequence (but not the buffer object), and creates new copies
  86. * of glyphs buffer and association list.
  87. * @param gs an existing glyph sequence
  88. */
  89. public GlyphSequence(GlyphSequence gs) {
  90. this (gs.characters.duplicate(), copyBuffer(gs.glyphs), copyAssociations(gs.associations), gs.predications);
  91. }
  92. /**
  93. * Instantiate a glyph sequence using an existing glyph sequence, where the new glyph sequence shares
  94. * the character array of the existing sequence (but not the buffer object), but uses the specified
  95. * backtrack, input, and lookahead glyph arrays to populate the glyphs, and uses the specified
  96. * of glyphs buffer and association list.
  97. * backtrack, input, and lookahead association arrays to populate the associations.
  98. * @param gs an existing glyph sequence
  99. * @param bga backtrack glyph array
  100. * @param iga input glyph array
  101. * @param lga lookahead glyph array
  102. * @param bal backtrack association list
  103. * @param ial input association list
  104. * @param lal lookahead association list
  105. */
  106. public GlyphSequence(GlyphSequence gs, int[] bga, int[] iga, int[] lga, CharAssociation[] bal, CharAssociation[] ial, CharAssociation[] lal) {
  107. this (gs.characters.duplicate(), concatGlyphs(bga, iga, lga), concatAssociations(bal, ial, lal), gs.predications);
  108. }
  109. /**
  110. * Obtain reference to underlying character buffer.
  111. * @return character buffer reference
  112. */
  113. public IntBuffer getCharacters() {
  114. return characters;
  115. }
  116. /**
  117. * Obtain array of characters. If <code>copy</code> is true, then
  118. * a newly instantiated array is returned, otherwise a reference to
  119. * the underlying buffer's array is returned. N.B. in case a reference
  120. * to the undelying buffer's array is returned, the length
  121. * of the array is not necessarily the number of characters in array.
  122. * To determine the number of characters, use {@link #getCharacterCount}.
  123. * @param copy true if to return a newly instantiated array of characters
  124. * @return array of characters
  125. */
  126. public int[] getCharacterArray(boolean copy) {
  127. if (copy) {
  128. return toArray(characters);
  129. } else {
  130. return characters.array();
  131. }
  132. }
  133. /**
  134. * Obtain the number of characters in character array, where
  135. * each character constitutes a unicode scalar value.
  136. * NB: Supplementary characters (non-BMP code points) count as 1
  137. * character, not as two UTF-16 code units.
  138. * @return number of characters available in character array
  139. */
  140. public int getCharacterCount() {
  141. return characters.limit();
  142. }
  143. /**
  144. * Obtain the number of characters in character array, where
  145. * each character constitutes a UTF-16 character. This means
  146. * that every non-BMP character is counted as 2 characters.
  147. * @return number of chars (UTF-16 code units) available in
  148. * character array
  149. */
  150. public int getUTF16CharacterCount() {
  151. int count = 0;
  152. for (int ch : characters.array()) {
  153. count += Character.charCount(ch);
  154. }
  155. return count;
  156. }
  157. /**
  158. * Obtain glyph id at specified index.
  159. * @param index to obtain glyph
  160. * @return the glyph identifier of glyph at specified index
  161. * @throws IndexOutOfBoundsException if index is less than zero
  162. * or exceeds last valid position
  163. */
  164. public int getGlyph(int index) throws IndexOutOfBoundsException {
  165. return glyphs.get(index);
  166. }
  167. /**
  168. * Set glyph id at specified index.
  169. * @param index to set glyph
  170. * @param gi glyph index
  171. * @throws IndexOutOfBoundsException if index is greater or equal to
  172. * the limit of the underlying glyph buffer
  173. */
  174. public void setGlyph(int index, int gi) throws IndexOutOfBoundsException {
  175. if (gi > 65535) {
  176. gi = 65535;
  177. }
  178. glyphs.put(index, gi);
  179. }
  180. /**
  181. * Obtain reference to underlying glyph buffer.
  182. * @return glyph buffer reference
  183. */
  184. public IntBuffer getGlyphs() {
  185. return glyphs;
  186. }
  187. /**
  188. * Obtain count glyphs starting at offset. If <code>count</code> is
  189. * negative, then it is treated as if the number of available glyphs
  190. * were specified.
  191. * @param offset into glyph sequence
  192. * @param count of glyphs to obtain starting at offset, or negative,
  193. * indicating all avaialble glyphs starting at offset
  194. * @return glyph array
  195. */
  196. public int[] getGlyphs(int offset, int count) {
  197. int ng = getGlyphCount();
  198. if (offset < 0) {
  199. offset = 0;
  200. } else if (offset > ng) {
  201. offset = ng;
  202. }
  203. if (count < 0) {
  204. count = ng - offset;
  205. }
  206. int[] ga = new int [ count ];
  207. for (int i = offset, n = offset + count, k = 0; i < n; i++) {
  208. if (k < ga.length) {
  209. ga [ k++ ] = glyphs.get(i);
  210. }
  211. }
  212. return ga;
  213. }
  214. /**
  215. * Obtain array of glyphs. If <code>copy</code> is true, then
  216. * a newly instantiated array is returned, otherwise a reference to
  217. * the underlying buffer's array is returned. N.B. in case a reference
  218. * to the undelying buffer's array is returned, the length
  219. * of the array is not necessarily the number of glyphs in array.
  220. * To determine the number of glyphs, use {@link #getGlyphCount}.
  221. * @param copy true if to return a newly instantiated array of glyphs
  222. * @return array of glyphs
  223. */
  224. public int[] getGlyphArray(boolean copy) {
  225. if (copy) {
  226. return toArray(glyphs);
  227. } else {
  228. return glyphs.array();
  229. }
  230. }
  231. /**
  232. * Obtain the number of glyphs in glyphs array, where
  233. * each glyph constitutes a font specific glyph index.
  234. * @return number of glyphs available in character array
  235. */
  236. public int getGlyphCount() {
  237. return glyphs.limit();
  238. }
  239. /**
  240. * Obtain association at specified index.
  241. * @param index into associations array
  242. * @return glyph to character associations at specified index
  243. * @throws IndexOutOfBoundsException if index is less than zero
  244. * or exceeds last valid position
  245. */
  246. public CharAssociation getAssociation(int index) throws IndexOutOfBoundsException {
  247. return (CharAssociation) associations.get(index);
  248. }
  249. /**
  250. * Obtain reference to underlying associations list.
  251. * @return associations list
  252. */
  253. public List getAssociations() {
  254. return associations;
  255. }
  256. /**
  257. * Obtain count associations starting at offset.
  258. * @param offset into glyph sequence
  259. * @param count of associations to obtain starting at offset, or negative,
  260. * indicating all avaialble associations starting at offset
  261. * @return associations
  262. */
  263. public CharAssociation[] getAssociations(int offset, int count) {
  264. int ng = getGlyphCount();
  265. if (offset < 0) {
  266. offset = 0;
  267. } else if (offset > ng) {
  268. offset = ng;
  269. }
  270. if (count < 0) {
  271. count = ng - offset;
  272. }
  273. CharAssociation[] aa = new CharAssociation [ count ];
  274. for (int i = offset, n = offset + count, k = 0; i < n; i++) {
  275. if (k < aa.length) {
  276. aa [ k++ ] = (CharAssociation) associations.get(i);
  277. }
  278. }
  279. return aa;
  280. }
  281. /**
  282. * Enable or disable predications.
  283. * @param enable true if predications are to be enabled; otherwise false to disable
  284. */
  285. public void setPredications(boolean enable) {
  286. this.predications = enable;
  287. }
  288. /**
  289. * Obtain predications state.
  290. * @return true if predications are enabled
  291. */
  292. public boolean getPredications() {
  293. return this.predications;
  294. }
  295. /**
  296. * Set predication &lt;KEY,VALUE&gt; at glyph sequence OFFSET.
  297. * @param offset offset (index) into glyph sequence
  298. * @param key predication key
  299. * @param value predication value
  300. */
  301. public void setPredication(int offset, String key, Object value) {
  302. if (predications) {
  303. CharAssociation[] aa = getAssociations(offset, 1);
  304. CharAssociation ca = aa[0];
  305. ca.setPredication(key, value);
  306. }
  307. }
  308. /**
  309. * Get predication KEY at glyph sequence OFFSET.
  310. * @param offset offset (index) into glyph sequence
  311. * @param key predication key
  312. * @return predication KEY at OFFSET or null if none exists
  313. */
  314. public Object getPredication(int offset, String key) {
  315. if (predications) {
  316. CharAssociation[] aa = getAssociations(offset, 1);
  317. CharAssociation ca = aa[0];
  318. return ca.getPredication(key);
  319. } else {
  320. return null;
  321. }
  322. }
  323. /**
  324. * Compare glyphs.
  325. * @param gb buffer containing glyph indices with which this glyph sequence's glyphs are to be compared
  326. * @return zero if glyphs are the same, otherwise returns 1 or -1 according to whether this glyph sequence's
  327. * glyphs are lexicographically greater or lesser than the glyphs in the specified string buffer
  328. */
  329. public int compareGlyphs(IntBuffer gb) {
  330. int ng = getGlyphCount();
  331. for (int i = 0, n = gb.limit(); i < n; i++) {
  332. if (i < ng) {
  333. int g1 = glyphs.get(i);
  334. int g2 = gb.get(i);
  335. if (g1 > g2) {
  336. return 1;
  337. } else if (g1 < g2) {
  338. return -1;
  339. }
  340. } else {
  341. return -1; // this gb is a proper prefix of specified gb
  342. }
  343. }
  344. return 0; // same lengths with no difference
  345. }
  346. /** {@inheritDoc} */
  347. public Object clone() {
  348. try {
  349. GlyphSequence gs = (GlyphSequence) super.clone();
  350. gs.characters = copyBuffer(characters);
  351. gs.glyphs = copyBuffer(glyphs);
  352. gs.associations = copyAssociations(associations);
  353. return gs;
  354. } catch (CloneNotSupportedException e) {
  355. return null;
  356. }
  357. }
  358. /** {@inheritDoc} */
  359. public String toString() {
  360. StringBuffer sb = new StringBuffer();
  361. sb.append('{');
  362. sb.append("chars = [");
  363. sb.append(characters);
  364. sb.append("], glyphs = [");
  365. sb.append(glyphs);
  366. sb.append("], associations = [");
  367. sb.append(associations);
  368. sb.append("]");
  369. sb.append('}');
  370. return sb.toString();
  371. }
  372. /**
  373. * Determine if two arrays of glyphs are identical.
  374. * @param ga1 first glyph array
  375. * @param ga2 second glyph array
  376. * @return true if arrays are botth null or both non-null and have identical elements
  377. */
  378. public static boolean sameGlyphs(int[] ga1, int[] ga2) {
  379. if (ga1 == ga2) {
  380. return true;
  381. } else if ((ga1 == null) || (ga2 == null)) {
  382. return false;
  383. } else if (ga1.length != ga2.length) {
  384. return false;
  385. } else {
  386. for (int i = 0, n = ga1.length; i < n; i++) {
  387. if (ga1[i] != ga2[i]) {
  388. return false;
  389. }
  390. }
  391. return true;
  392. }
  393. }
  394. /**
  395. * Concatenante glyph arrays.
  396. * @param bga backtrack glyph array
  397. * @param iga input glyph array
  398. * @param lga lookahead glyph array
  399. * @return new integer buffer containing concatenated glyphs
  400. */
  401. public static IntBuffer concatGlyphs(int[] bga, int[] iga, int[] lga) {
  402. int ng = 0;
  403. if (bga != null) {
  404. ng += bga.length;
  405. }
  406. if (iga != null) {
  407. ng += iga.length;
  408. }
  409. if (lga != null) {
  410. ng += lga.length;
  411. }
  412. IntBuffer gb = IntBuffer.allocate(ng);
  413. if (bga != null) {
  414. gb.put(bga);
  415. }
  416. if (iga != null) {
  417. gb.put(iga);
  418. }
  419. if (lga != null) {
  420. gb.put(lga);
  421. }
  422. gb.flip();
  423. return gb;
  424. }
  425. /**
  426. * Concatenante association arrays.
  427. * @param baa backtrack association array
  428. * @param iaa input association array
  429. * @param laa lookahead association array
  430. * @return new list containing concatenated associations
  431. */
  432. public static List concatAssociations(CharAssociation[] baa, CharAssociation[] iaa, CharAssociation[] laa) {
  433. int na = 0;
  434. if (baa != null) {
  435. na += baa.length;
  436. }
  437. if (iaa != null) {
  438. na += iaa.length;
  439. }
  440. if (laa != null) {
  441. na += laa.length;
  442. }
  443. if (na > 0) {
  444. List gl = new ArrayList(na);
  445. if (baa != null) {
  446. Collections.addAll(gl, baa);
  447. }
  448. if (iaa != null) {
  449. Collections.addAll(gl, iaa);
  450. }
  451. if (laa != null) {
  452. Collections.addAll(gl, laa);
  453. }
  454. return gl;
  455. } else {
  456. return null;
  457. }
  458. }
  459. /**
  460. * Join (concatenate) glyph sequences.
  461. * @param gs original glyph sequence from which to reuse character array reference
  462. * @param sa array of glyph sequences, whose glyph arrays and association lists are to be concatenated
  463. * @return new glyph sequence referring to character array of GS and concatenated glyphs and associations of SA
  464. */
  465. public static GlyphSequence join(GlyphSequence gs, GlyphSequence[] sa) {
  466. assert sa != null;
  467. int tg = 0;
  468. int ta = 0;
  469. for (GlyphSequence s : sa) {
  470. IntBuffer ga = s.getGlyphs();
  471. assert ga != null;
  472. int ng = ga.limit();
  473. List al = s.getAssociations();
  474. assert al != null;
  475. int na = al.size();
  476. assert na == ng;
  477. tg += ng;
  478. ta += na;
  479. }
  480. IntBuffer uga = IntBuffer.allocate(tg);
  481. ArrayList ual = new ArrayList(ta);
  482. for (GlyphSequence s : sa) {
  483. uga.put(s.getGlyphs());
  484. ual.addAll(s.getAssociations());
  485. }
  486. return new GlyphSequence(gs.getCharacters(), uga, ual, gs.getPredications());
  487. }
  488. /**
  489. * Reorder sequence such that [SOURCE,SOURCE+COUNT) is moved just prior to TARGET.
  490. * @param gs input sequence
  491. * @param source index of sub-sequence to reorder
  492. * @param count length of sub-sequence to reorder
  493. * @param target index to which source sub-sequence is to be moved
  494. * @return reordered sequence (or original if no reordering performed)
  495. */
  496. public static GlyphSequence reorder(GlyphSequence gs, int source, int count, int target) {
  497. if (source != target) {
  498. int ng = gs.getGlyphCount();
  499. int[] ga = gs.getGlyphArray(false);
  500. int[] nga = new int [ ng ];
  501. CharAssociation[] aa = gs.getAssociations(0, ng);
  502. CharAssociation[] naa = new CharAssociation [ ng ];
  503. if (source < target) {
  504. int t = 0;
  505. for (int s = 0, e = source; s < e; s++, t++) {
  506. nga[t] = ga[s];
  507. naa[t] = aa[s];
  508. }
  509. for (int s = source + count, e = target; s < e; s++, t++) {
  510. nga[t] = ga[s];
  511. naa[t] = aa[s];
  512. }
  513. for (int s = source, e = source + count; s < e; s++, t++) {
  514. nga[t] = ga[s];
  515. naa[t] = aa[s];
  516. }
  517. for (int s = target, e = ng; s < e; s++, t++) {
  518. nga[t] = ga[s];
  519. naa[t] = aa[s];
  520. }
  521. } else {
  522. int t = 0;
  523. for (int s = 0, e = target; s < e; s++, t++) {
  524. nga[t] = ga[s];
  525. naa[t] = aa[s];
  526. }
  527. for (int s = source, e = source + count; s < e; s++, t++) {
  528. nga[t] = ga[s];
  529. naa[t] = aa[s];
  530. }
  531. for (int s = target, e = source; s < e; s++, t++) {
  532. nga[t] = ga[s];
  533. naa[t] = aa[s];
  534. }
  535. for (int s = source + count, e = ng; s < e; s++, t++) {
  536. nga[t] = ga[s];
  537. naa[t] = aa[s];
  538. }
  539. }
  540. return new GlyphSequence(gs, null, nga, null, null, naa, null);
  541. } else {
  542. return gs;
  543. }
  544. }
  545. private static int[] toArray(IntBuffer ib) {
  546. if (ib != null) {
  547. int n = ib.limit();
  548. int[] ia = new int[n];
  549. ib.get(ia, 0, n);
  550. return ia;
  551. } else {
  552. return new int[0];
  553. }
  554. }
  555. private static List makeIdentityAssociations(int numChars, int numGlyphs) {
  556. int nc = numChars;
  557. int ng = numGlyphs;
  558. List av = new ArrayList(ng);
  559. for (int i = 0, n = ng; i < n; i++) {
  560. int k = (i > nc) ? nc : i;
  561. av.add(new CharAssociation(i, (k == nc) ? 0 : 1));
  562. }
  563. return av;
  564. }
  565. private static IntBuffer copyBuffer(IntBuffer ib) {
  566. if (ib != null) {
  567. int[] ia = new int [ ib.capacity() ];
  568. int p = ib.position();
  569. int l = ib.limit();
  570. System.arraycopy(ib.array(), 0, ia, 0, ia.length);
  571. return IntBuffer.wrap(ia, p, l - p);
  572. } else {
  573. return null;
  574. }
  575. }
  576. private static List copyAssociations(List ca) {
  577. if (ca != null) {
  578. return new ArrayList(ca);
  579. } else {
  580. return ca;
  581. }
  582. }
  583. }