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.

FOText.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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.fo;
  19. import java.awt.Color;
  20. import java.nio.CharBuffer;
  21. import java.util.Map;
  22. import java.util.NoSuchElementException;
  23. import java.util.Stack;
  24. import org.xml.sax.Locator;
  25. import org.apache.fop.accessibility.StructureTreeElement;
  26. import org.apache.fop.apps.FOPException;
  27. import org.apache.fop.complexscripts.bidi.DelimitedTextRange;
  28. import org.apache.fop.datatypes.Length;
  29. import org.apache.fop.fo.flow.Block;
  30. import org.apache.fop.fo.properties.CommonFont;
  31. import org.apache.fop.fo.properties.CommonHyphenation;
  32. import org.apache.fop.fo.properties.CommonTextDecoration;
  33. import org.apache.fop.fo.properties.KeepProperty;
  34. import org.apache.fop.fo.properties.Property;
  35. import org.apache.fop.fo.properties.SpaceProperty;
  36. import org.apache.fop.util.CharUtilities;
  37. /**
  38. * A text node (PCDATA) in the formatting object tree.
  39. */
  40. public class FOText extends FONode implements CharSequence {
  41. /** the <code>CharBuffer</code> containing the text */
  42. private CharBuffer charBuffer;
  43. // The value of FO traits (refined properties) that apply to #PCDATA
  44. // (aka implicit sequence of fo:character)
  45. private CommonFont commonFont;
  46. private CommonHyphenation commonHyphenation;
  47. private Color color;
  48. private KeepProperty keepTogether;
  49. private Property letterSpacing;
  50. private SpaceProperty lineHeight;
  51. private int whiteSpaceTreatment;
  52. private int whiteSpaceCollapse;
  53. private int textTransform;
  54. private Property wordSpacing;
  55. private int wrapOption;
  56. private Length baselineShift;
  57. private String country;
  58. private String language;
  59. private String script;
  60. // End of trait values
  61. /**
  62. * Points to the previous FOText object created within the current
  63. * block. If this is "null", this is the first such object.
  64. */
  65. private FOText prevFOTextThisBlock = null;
  66. /**
  67. * Points to the next FOText object created within the current
  68. * block. If this is "null", this is the last such object.
  69. */
  70. private FOText nextFOTextThisBlock = null;
  71. /**
  72. * Points to the ancestor Block object. This is used to keep track of
  73. * which FOText nodes are descendants of the same block.
  74. */
  75. private Block ancestorBlock = null;
  76. /** Holds the text decoration values. May be null */
  77. private CommonTextDecoration textDecoration;
  78. private StructureTreeElement structureTreeElement;
  79. /* bidi levels */
  80. private int[] bidiLevels;
  81. /* advanced script processing state */
  82. private Map/*<MapRange,String>*/ mappings;
  83. private static final int IS_WORD_CHAR_FALSE = 0;
  84. private static final int IS_WORD_CHAR_TRUE = 1;
  85. private static final int IS_WORD_CHAR_MAYBE = 2;
  86. /**
  87. * Creates a new FO text node.
  88. *
  89. * @param parent FONode that is the parent of this object
  90. */
  91. public FOText(FONode parent) {
  92. super(parent);
  93. }
  94. /** {@inheritDoc} */
  95. protected void characters(char[] data, int start, int length,
  96. PropertyList list, Locator locator) throws FOPException {
  97. if (charBuffer == null) {
  98. // buffer not yet initialized, do so now
  99. int newLength = ( length < 16 ) ? 16 : length;
  100. charBuffer = CharBuffer.allocate(newLength);
  101. } else {
  102. // allocate a larger buffer, and transfer contents
  103. int requires = charBuffer.position() + length;
  104. int capacity = charBuffer.capacity();
  105. if ( requires > capacity ) {
  106. int newCapacity = capacity * 2;
  107. if ( requires > newCapacity ) {
  108. newCapacity = requires;
  109. }
  110. CharBuffer newBuffer = CharBuffer.allocate(newCapacity);
  111. charBuffer.rewind();
  112. newBuffer.put(charBuffer);
  113. charBuffer = newBuffer;
  114. }
  115. }
  116. // extend limit to capacity
  117. charBuffer.limit(charBuffer.capacity());
  118. // append characters
  119. charBuffer.put(data, start, length);
  120. // shrink limit to position
  121. charBuffer.limit(charBuffer.position());
  122. }
  123. /**
  124. * Return the array of characters for this instance.
  125. *
  126. * @return a char sequence containing the text
  127. */
  128. public CharSequence getCharSequence() {
  129. if (this.charBuffer == null) {
  130. return null;
  131. }
  132. this.charBuffer.rewind();
  133. return this.charBuffer.asReadOnlyBuffer().subSequence(0, this.charBuffer.limit());
  134. }
  135. /** {@inheritDoc} */
  136. public FONode clone(FONode parent, boolean removeChildren)
  137. throws FOPException {
  138. FOText ft = (FOText) super.clone(parent, removeChildren);
  139. if (removeChildren) {
  140. // not really removing, just make sure the char buffer
  141. // pointed to is really a different one
  142. if (charBuffer != null) {
  143. ft.charBuffer = CharBuffer.allocate(charBuffer.limit());
  144. charBuffer.rewind();
  145. ft.charBuffer.put(charBuffer);
  146. ft.charBuffer.rewind();
  147. }
  148. }
  149. ft.prevFOTextThisBlock = null;
  150. ft.nextFOTextThisBlock = null;
  151. ft.ancestorBlock = null;
  152. return ft;
  153. }
  154. /** {@inheritDoc} */
  155. public void bind(PropertyList pList) throws FOPException {
  156. this.commonFont = pList.getFontProps();
  157. this.commonHyphenation = pList.getHyphenationProps();
  158. this.color = pList.get(Constants.PR_COLOR).getColor(getUserAgent());
  159. this.keepTogether = pList.get(Constants.PR_KEEP_TOGETHER).getKeep();
  160. this.lineHeight = pList.get(Constants.PR_LINE_HEIGHT).getSpace();
  161. this.letterSpacing = pList.get(Constants.PR_LETTER_SPACING);
  162. this.whiteSpaceCollapse = pList.get(Constants.PR_WHITE_SPACE_COLLAPSE).getEnum();
  163. this.whiteSpaceTreatment = pList.get(Constants.PR_WHITE_SPACE_TREATMENT).getEnum();
  164. this.textTransform = pList.get(Constants.PR_TEXT_TRANSFORM).getEnum();
  165. this.wordSpacing = pList.get(Constants.PR_WORD_SPACING);
  166. this.wrapOption = pList.get(Constants.PR_WRAP_OPTION).getEnum();
  167. this.textDecoration = pList.getTextDecorationProps();
  168. this.baselineShift = pList.get(Constants.PR_BASELINE_SHIFT).getLength();
  169. this.country = pList.get(Constants.PR_COUNTRY).getString();
  170. this.language = pList.get(Constants.PR_LANGUAGE).getString();
  171. this.script = pList.get(Constants.PR_SCRIPT).getString();
  172. }
  173. /** {@inheritDoc} */
  174. protected void endOfNode() throws FOPException {
  175. if ( charBuffer != null ) {
  176. charBuffer.rewind();
  177. }
  178. super.endOfNode();
  179. getFOEventHandler().characters(this);
  180. }
  181. /** {@inheritDoc} */
  182. public void finalizeNode() {
  183. textTransform();
  184. }
  185. /**
  186. * Check if this text node will create an area.
  187. * This means either there is non-whitespace or it is
  188. * preserved whitespace.
  189. * Maybe this just needs to check length > 0, since char iterators
  190. * handle whitespace.
  191. *
  192. * @return true if this will create an area in the output
  193. */
  194. public boolean willCreateArea() {
  195. if (whiteSpaceCollapse == Constants.EN_FALSE
  196. && charBuffer.limit() > 0) {
  197. return true;
  198. }
  199. char ch;
  200. charBuffer.rewind();
  201. while (charBuffer.hasRemaining()) {
  202. ch = charBuffer.get();
  203. if (!((ch == CharUtilities.SPACE)
  204. || (ch == CharUtilities.LINEFEED_CHAR)
  205. || (ch == CharUtilities.CARRIAGE_RETURN)
  206. || (ch == CharUtilities.TAB))) {
  207. // not whitespace
  208. charBuffer.rewind();
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. /**
  215. * @return a new TextCharIterator
  216. */
  217. public CharIterator charIterator() {
  218. return new TextCharIterator();
  219. }
  220. /**
  221. * This method is run as part of the ancestor Block's flushText(), to
  222. * create xref pointers to the previous FOText objects within the same Block
  223. * @param ancestorBlock the ancestor fo:block
  224. */
  225. protected void createBlockPointers(Block ancestorBlock) {
  226. this.ancestorBlock = ancestorBlock;
  227. // if the last FOText is a sibling, point to it, and have it point here
  228. if (ancestorBlock.lastFOTextProcessed != null) {
  229. if (ancestorBlock.lastFOTextProcessed.ancestorBlock
  230. == this.ancestorBlock) {
  231. prevFOTextThisBlock = ancestorBlock.lastFOTextProcessed;
  232. prevFOTextThisBlock.nextFOTextThisBlock = this;
  233. } else {
  234. prevFOTextThisBlock = null;
  235. }
  236. }
  237. }
  238. /**
  239. * This method is run as part of endOfNode(), to handle the
  240. * text-transform property for accumulated FOText
  241. */
  242. private void textTransform() {
  243. if (getBuilderContext().inMarker()
  244. || textTransform == Constants.EN_NONE) {
  245. return;
  246. }
  247. charBuffer.rewind();
  248. CharBuffer tmp = charBuffer.slice();
  249. char c;
  250. int lim = charBuffer.limit();
  251. int pos = -1;
  252. while (++pos < lim) {
  253. c = charBuffer.get();
  254. switch (textTransform) {
  255. case Constants.EN_UPPERCASE:
  256. tmp.put(Character.toUpperCase(c));
  257. break;
  258. case Constants.EN_LOWERCASE:
  259. tmp.put(Character.toLowerCase(c));
  260. break;
  261. case Constants.EN_CAPITALIZE:
  262. if (isStartOfWord(pos)) {
  263. /*
  264. Use toTitleCase here. Apparently, some languages use
  265. a different character to represent a letter when using
  266. initial caps than when all of the letters in the word
  267. are capitalized. We will try to let Java handle this.
  268. */
  269. tmp.put(Character.toTitleCase(c));
  270. } else {
  271. tmp.put(c);
  272. }
  273. break;
  274. default:
  275. //should never happen as the property subsystem catches that case
  276. assert false;
  277. //nop
  278. }
  279. }
  280. }
  281. /**
  282. * Determines whether a particular location in an FOText object's text is
  283. * the start of a new "word". The use of "word" here is specifically for
  284. * the text-transform property, but may be useful for other things as
  285. * well, such as word-spacing. The definition of "word" is somewhat ambiguous
  286. * and appears to be definable by the user agent.
  287. *
  288. * @param i index into charBuffer
  289. *
  290. * @return True if the character at this location is the start of a new
  291. * word.
  292. */
  293. private boolean isStartOfWord(int i) {
  294. char prevChar = getRelativeCharInBlock(i, -1);
  295. /* All we are really concerned about here is of what type prevChar
  296. * is. If inputChar is not part of a word, then the Java
  297. * conversions will (we hope) simply return inputChar.
  298. */
  299. switch (isWordChar(prevChar)) {
  300. case IS_WORD_CHAR_TRUE:
  301. return false;
  302. case IS_WORD_CHAR_FALSE:
  303. return true;
  304. /* "MAYBE" implies that additional context is needed. An example is a
  305. * single-quote, either straight or closing, which might be interpreted
  306. * as a possessive or a contraction, or might be a closing quote.
  307. */
  308. case IS_WORD_CHAR_MAYBE:
  309. char prevPrevChar = getRelativeCharInBlock(i, -2);
  310. switch (isWordChar(prevPrevChar)) {
  311. case IS_WORD_CHAR_TRUE:
  312. return false;
  313. case IS_WORD_CHAR_FALSE:
  314. return true;
  315. case IS_WORD_CHAR_MAYBE:
  316. return true;
  317. default:
  318. return false;
  319. }
  320. default:
  321. return false;
  322. }
  323. }
  324. /**
  325. * Finds a character within the current Block that is relative in location
  326. * to a character in the current FOText. Treats all FOText objects within a
  327. * block as one unit, allowing text in adjoining FOText objects to be
  328. * returned if the parameters are outside of the current object.
  329. *
  330. * @param i index into the CharBuffer
  331. * @param offset signed integer with relative position within the
  332. * block of the character to return. To return the character immediately
  333. * preceding i, pass -1. To return the character immediately after i,
  334. * pass 1.
  335. * @return the character in the offset position within the block; \u0000 if
  336. * the offset points to an area outside of the block.
  337. */
  338. private char getRelativeCharInBlock(int i, int offset) {
  339. int charIndex = i + offset;
  340. // The easy case is where the desired character is in the same FOText
  341. if (charIndex >= 0 && charIndex < this.length()) {
  342. return this.charAt(i + offset);
  343. }
  344. // For now, we can't look at following FOText nodes
  345. if (offset > 0) {
  346. return CharUtilities.NULL_CHAR;
  347. }
  348. // Remaining case has the text in some previous FOText node
  349. boolean foundChar = false;
  350. char charToReturn = CharUtilities.NULL_CHAR;
  351. FOText nodeToTest = this;
  352. int remainingOffset = offset + i;
  353. while (!foundChar) {
  354. if (nodeToTest.prevFOTextThisBlock == null) {
  355. break;
  356. }
  357. nodeToTest = nodeToTest.prevFOTextThisBlock;
  358. int diff = nodeToTest.length() + remainingOffset - 1;
  359. if (diff >= 0) {
  360. charToReturn = nodeToTest.charAt(diff);
  361. foundChar = true;
  362. } else {
  363. remainingOffset += diff;
  364. }
  365. }
  366. return charToReturn;
  367. }
  368. /**
  369. * @return The previous FOText node in this Block; null, if this is the
  370. * first FOText in this Block.
  371. */
  372. //public FOText getPrevFOTextThisBlock () {
  373. // return prevFOTextThisBlock;
  374. //}
  375. /**
  376. * @return The next FOText node in this Block; null if this is the last
  377. * FOText in this Block; null if subsequent FOText nodes have not yet been
  378. * processed.
  379. */
  380. //public FOText getNextFOTextThisBlock () {
  381. // return nextFOTextThisBlock;
  382. //}
  383. /**
  384. * @return The nearest ancestor block object which contains this FOText.
  385. */
  386. //public Block getAncestorBlock () {
  387. // return ancestorBlock;
  388. //}
  389. /**
  390. * Determines whether the input char should be considered part of a
  391. * "word". This is used primarily to determine whether the character
  392. * immediately following starts a new word, but may have other uses.
  393. * We have not found a definition of "word" in the standard (1.0), so the
  394. * logic used here is based on the programmer's best guess.
  395. *
  396. * @param inputChar the character to be tested.
  397. * @return int IS_WORD_CHAR_TRUE, IS_WORD_CHAR_FALSE, or IS_WORD_CHAR_MAYBE,
  398. * depending on whether the character should be considered part of a word
  399. * or not.
  400. */
  401. private static int isWordChar(char inputChar) {
  402. switch (Character.getType(inputChar)) {
  403. case Character.COMBINING_SPACING_MARK:
  404. return IS_WORD_CHAR_TRUE;
  405. case Character.CONNECTOR_PUNCTUATION:
  406. return IS_WORD_CHAR_TRUE;
  407. case Character.CONTROL:
  408. return IS_WORD_CHAR_FALSE;
  409. case Character.CURRENCY_SYMBOL:
  410. return IS_WORD_CHAR_TRUE;
  411. case Character.DASH_PUNCTUATION:
  412. if (inputChar == '-') {
  413. return IS_WORD_CHAR_TRUE; //hyphen
  414. }
  415. return IS_WORD_CHAR_FALSE;
  416. case Character.DECIMAL_DIGIT_NUMBER:
  417. return IS_WORD_CHAR_TRUE;
  418. case Character.ENCLOSING_MARK:
  419. return IS_WORD_CHAR_FALSE;
  420. case Character.END_PUNCTUATION:
  421. if (inputChar == '\u2019') {
  422. return IS_WORD_CHAR_MAYBE; //apostrophe, right single quote
  423. }
  424. return IS_WORD_CHAR_FALSE;
  425. case Character.FORMAT:
  426. return IS_WORD_CHAR_FALSE;
  427. case Character.LETTER_NUMBER:
  428. return IS_WORD_CHAR_TRUE;
  429. case Character.LINE_SEPARATOR:
  430. return IS_WORD_CHAR_FALSE;
  431. case Character.LOWERCASE_LETTER:
  432. return IS_WORD_CHAR_TRUE;
  433. case Character.MATH_SYMBOL:
  434. return IS_WORD_CHAR_FALSE;
  435. case Character.MODIFIER_LETTER:
  436. return IS_WORD_CHAR_TRUE;
  437. case Character.MODIFIER_SYMBOL:
  438. return IS_WORD_CHAR_TRUE;
  439. case Character.NON_SPACING_MARK:
  440. return IS_WORD_CHAR_TRUE;
  441. case Character.OTHER_LETTER:
  442. return IS_WORD_CHAR_TRUE;
  443. case Character.OTHER_NUMBER:
  444. return IS_WORD_CHAR_TRUE;
  445. case Character.OTHER_PUNCTUATION:
  446. if (inputChar == '\'') {
  447. return IS_WORD_CHAR_MAYBE; //ASCII apostrophe
  448. }
  449. return IS_WORD_CHAR_FALSE;
  450. case Character.OTHER_SYMBOL:
  451. return IS_WORD_CHAR_TRUE;
  452. case Character.PARAGRAPH_SEPARATOR:
  453. return IS_WORD_CHAR_FALSE;
  454. case Character.PRIVATE_USE:
  455. return IS_WORD_CHAR_FALSE;
  456. case Character.SPACE_SEPARATOR:
  457. return IS_WORD_CHAR_FALSE;
  458. case Character.START_PUNCTUATION:
  459. return IS_WORD_CHAR_FALSE;
  460. case Character.SURROGATE:
  461. return IS_WORD_CHAR_FALSE;
  462. case Character.TITLECASE_LETTER:
  463. return IS_WORD_CHAR_TRUE;
  464. case Character.UNASSIGNED:
  465. return IS_WORD_CHAR_FALSE;
  466. case Character.UPPERCASE_LETTER:
  467. return IS_WORD_CHAR_TRUE;
  468. default:
  469. return IS_WORD_CHAR_FALSE;
  470. }
  471. }
  472. private class TextCharIterator extends CharIterator {
  473. private int currentPosition = 0;
  474. private boolean canRemove = false;
  475. private boolean canReplace = false;
  476. public TextCharIterator() {
  477. }
  478. /** {@inheritDoc} */
  479. public boolean hasNext() {
  480. return (this.currentPosition < charBuffer.limit());
  481. }
  482. /** {@inheritDoc} */
  483. public char nextChar() {
  484. if (this.currentPosition < charBuffer.limit()) {
  485. this.canRemove = true;
  486. this.canReplace = true;
  487. return charBuffer.get(currentPosition++);
  488. } else {
  489. throw new NoSuchElementException();
  490. }
  491. }
  492. /** {@inheritDoc} */
  493. public void remove() {
  494. if (this.canRemove) {
  495. charBuffer.position(currentPosition);
  496. // Slice the buffer at the current position
  497. CharBuffer tmp = charBuffer.slice();
  498. // Reset position to before current character
  499. charBuffer.position(--currentPosition);
  500. if (tmp.hasRemaining()) {
  501. // Transfer any remaining characters
  502. charBuffer.mark();
  503. charBuffer.put(tmp);
  504. charBuffer.reset();
  505. }
  506. // Decrease limit
  507. charBuffer.limit(charBuffer.limit() - 1);
  508. // Make sure following calls fail, unless nextChar() was called
  509. this.canRemove = false;
  510. } else {
  511. throw new IllegalStateException();
  512. }
  513. }
  514. /** {@inheritDoc} */
  515. public void replaceChar(char c) {
  516. if (this.canReplace) {
  517. charBuffer.put(currentPosition - 1, c);
  518. } else {
  519. throw new IllegalStateException();
  520. }
  521. }
  522. }
  523. /**
  524. * @return the Common Font Properties.
  525. */
  526. public CommonFont getCommonFont() {
  527. return commonFont;
  528. }
  529. /**
  530. * @return the Common Hyphenation Properties.
  531. */
  532. public CommonHyphenation getCommonHyphenation() {
  533. return commonHyphenation;
  534. }
  535. /**
  536. * @return the "color" trait.
  537. */
  538. public Color getColor() {
  539. return color;
  540. }
  541. /**
  542. * @return the "keep-together" trait.
  543. */
  544. public KeepProperty getKeepTogether() {
  545. return keepTogether;
  546. }
  547. /**
  548. * @return the "letter-spacing" trait.
  549. */
  550. public Property getLetterSpacing() {
  551. return letterSpacing;
  552. }
  553. /**
  554. * @return the "line-height" trait.
  555. */
  556. public SpaceProperty getLineHeight() {
  557. return lineHeight;
  558. }
  559. /**
  560. * @return the "white-space-treatment" trait
  561. */
  562. public int getWhitespaceTreatment() {
  563. return whiteSpaceTreatment;
  564. }
  565. /**
  566. * @return the "word-spacing" trait.
  567. */
  568. public Property getWordSpacing() {
  569. return wordSpacing;
  570. }
  571. /**
  572. * @return the "wrap-option" trait.
  573. */
  574. public int getWrapOption() {
  575. return wrapOption;
  576. }
  577. /** @return the "text-decoration" trait. */
  578. public CommonTextDecoration getTextDecoration() {
  579. return textDecoration;
  580. }
  581. /** @return the baseline-shift trait */
  582. public Length getBaseLineShift() {
  583. return baselineShift;
  584. }
  585. /** @return the country trait */
  586. public String getCountry() {
  587. return country;
  588. }
  589. /** @return the language trait */
  590. public String getLanguage() {
  591. return language;
  592. }
  593. /** @return the script trait */
  594. public String getScript() {
  595. return script;
  596. }
  597. /** {@inheritDoc} */
  598. public String toString() {
  599. if ( charBuffer == null ) {
  600. return "";
  601. } else {
  602. CharBuffer cb = charBuffer.duplicate();
  603. cb.rewind();
  604. return cb.toString();
  605. }
  606. }
  607. /** {@inheritDoc} */
  608. public String getLocalName() {
  609. return "#PCDATA";
  610. }
  611. /** {@inheritDoc} */
  612. public String getNormalNamespacePrefix() {
  613. return null;
  614. }
  615. /** {@inheritDoc} */
  616. protected String gatherContextInfo() {
  617. if (this.locator != null) {
  618. return super.gatherContextInfo();
  619. } else {
  620. return this.toString();
  621. }
  622. }
  623. /** {@inheritDoc} */
  624. public char charAt(int position) {
  625. return charBuffer.get(position);
  626. }
  627. /** {@inheritDoc} */
  628. public CharSequence subSequence(int start, int end) {
  629. return charBuffer.subSequence(start, end);
  630. }
  631. /** {@inheritDoc} */
  632. public int length() {
  633. return charBuffer.limit();
  634. }
  635. /**
  636. * Resets the backing <code>java.nio.CharBuffer</code>
  637. */
  638. public void resetBuffer() {
  639. if (charBuffer != null) {
  640. charBuffer.rewind();
  641. }
  642. }
  643. @Override
  644. public boolean isDelimitedTextRangeBoundary ( int boundary ) {
  645. return false;
  646. }
  647. @Override
  648. public void setStructureTreeElement(StructureTreeElement structureTreeElement) {
  649. this.structureTreeElement = structureTreeElement;
  650. }
  651. /** @return the structure tree element. */
  652. public StructureTreeElement getStructureTreeElement() {
  653. return structureTreeElement;
  654. }
  655. /**
  656. * Set bidirectional level over interval [start,end).
  657. * @param level the resolved level
  658. * @param start the starting index of interval
  659. * @param end the ending index of interval
  660. */
  661. public void setBidiLevel ( int level, int start, int end ) {
  662. if ( start < end ) {
  663. if ( bidiLevels == null ) {
  664. bidiLevels = new int [ length() ];
  665. }
  666. for ( int i = start, n = end; i < n; i++ ) {
  667. bidiLevels [ i ] = level;
  668. }
  669. if ( parent != null ) {
  670. ( (FObj) parent ).setBidiLevel ( level );
  671. }
  672. } else {
  673. assert start < end;
  674. }
  675. }
  676. /**
  677. * Obtain bidirectional level of each character
  678. * represented by this FOText.
  679. * @return a (possibly empty) array of bidi levels or null
  680. * in case no bidi levels have been assigned
  681. */
  682. public int[] getBidiLevels() {
  683. return bidiLevels;
  684. }
  685. /**
  686. * Obtain bidirectional level of each character over
  687. * interval [start,end).
  688. * @param start the starting index of interval
  689. * @param end the ending index of interval
  690. * @return a (possibly empty) array of bidi levels or null
  691. * in case no bidi levels have been assigned
  692. */
  693. public int[] getBidiLevels ( int start, int end ) {
  694. if ( this.bidiLevels != null ) {
  695. assert start <= end;
  696. int n = end - start;
  697. int[] bidiLevels = new int [ n ];
  698. for ( int i = 0; i < n; i++ ) {
  699. bidiLevels[i] = this.bidiLevels [ start + i ];
  700. }
  701. return bidiLevels;
  702. } else {
  703. return null;
  704. }
  705. }
  706. /**
  707. * Obtain bidirectional level of character at
  708. * specified position, which must be a non-negative integer
  709. * less than the length of this FO.
  710. * @param position an offset position into FO's characters
  711. * @return a resolved bidi level or -1 if default
  712. * @throws IndexOutOfBoundsException if position is not non-negative integer
  713. * or is greater than or equal to length
  714. */
  715. public int bidiLevelAt ( int position ) throws IndexOutOfBoundsException {
  716. if ( ( position < 0 ) || ( position >= length() ) ) {
  717. throw new IndexOutOfBoundsException();
  718. } else if ( bidiLevels != null ) {
  719. return bidiLevels [ position ];
  720. } else {
  721. return -1;
  722. }
  723. }
  724. /**
  725. * Add characters mapped by script substitution processing.
  726. * @param start index in character buffer
  727. * @param end index in character buffer
  728. * @param mappedChars sequence of character codes denoting substituted characters
  729. */
  730. public void addMapping ( int start, int end, CharSequence mappedChars ) {
  731. if ( mappings == null ) {
  732. mappings = new java.util.HashMap();
  733. }
  734. mappings.put ( new MapRange ( start, end ), mappedChars.toString() );
  735. }
  736. /**
  737. * Determine if characters over specific interval have a mapping.
  738. * @param start index in character buffer
  739. * @param end index in character buffer
  740. * @return true if a mapping exist such that the mapping's interval is coincident to
  741. * [start,end)
  742. */
  743. public boolean hasMapping ( int start, int end ) {
  744. return ( mappings != null ) && ( mappings.containsKey ( new MapRange ( start, end ) ) );
  745. }
  746. /**
  747. * Obtain mapping of characters over specific interval.
  748. * @param start index in character buffer
  749. * @param end index in character buffer
  750. * @return a string of characters representing the mapping over the interval
  751. * [start,end)
  752. */
  753. public String getMapping ( int start, int end ) {
  754. if ( mappings != null ) {
  755. return (String) mappings.get ( new MapRange ( start, end ) );
  756. } else {
  757. return null;
  758. }
  759. }
  760. /**
  761. * Obtain length of mapping of characters over specific interval.
  762. * @param start index in character buffer
  763. * @param end index in character buffer
  764. * @return the length of the mapping (if present) or zero
  765. */
  766. public int getMappingLength ( int start, int end ) {
  767. if ( mappings != null ) {
  768. return ( (String) mappings.get ( new MapRange ( start, end ) ) ) .length();
  769. } else {
  770. return 0;
  771. }
  772. }
  773. /**
  774. * Obtain bidirectional levels of mapping of characters over specific interval.
  775. * @param start index in character buffer
  776. * @param end index in character buffer
  777. * @return a (possibly empty) array of bidi levels or null
  778. * in case no bidi levels have been assigned
  779. */
  780. public int[] getMappingBidiLevels ( int start, int end ) {
  781. if ( hasMapping ( start, end ) ) {
  782. int nc = end - start;
  783. int nm = getMappingLength ( start, end );
  784. int[] la = getBidiLevels ( start, end );
  785. if ( la == null ) {
  786. return null;
  787. } else if ( nm == nc ) { // mapping is same length as mapped range
  788. return la;
  789. } else if ( nm > nc ) { // mapping is longer than mapped range
  790. int[] ma = new int [ nm ];
  791. System.arraycopy ( la, 0, ma, 0, la.length );
  792. for ( int i = la.length,
  793. n = ma.length, l = ( i > 0 ) ? la [ i - 1 ] : 0; i < n; i++ ) {
  794. ma [ i ] = l;
  795. }
  796. return ma;
  797. } else { // mapping is shorter than mapped range
  798. int[] ma = new int [ nm ];
  799. System.arraycopy ( la, 0, ma, 0, ma.length );
  800. return ma;
  801. }
  802. } else {
  803. return getBidiLevels ( start, end );
  804. }
  805. }
  806. @Override
  807. protected Stack collectDelimitedTextRanges ( Stack ranges, DelimitedTextRange currentRange ) {
  808. if ( currentRange != null ) {
  809. currentRange.append ( charIterator(), this );
  810. }
  811. return ranges;
  812. }
  813. private static class MapRange {
  814. private int start;
  815. private int end;
  816. MapRange(int start, int end) {
  817. this.start = start;
  818. this.end = end;
  819. }
  820. public int hashCode() {
  821. return ( start * 31 ) + end;
  822. }
  823. public boolean equals ( Object o ) {
  824. if ( o instanceof MapRange ) {
  825. MapRange r = (MapRange) o;
  826. return ( r.start == start ) && ( r.end == end );
  827. } else {
  828. return false;
  829. }
  830. }
  831. }
  832. }