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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources."
  6. */
  7. package org.apache.fop.fo;
  8. // FOP
  9. import org.apache.fop.layout.Area;
  10. import org.apache.fop.layout.BlockArea;
  11. import org.apache.fop.layout.FontState;
  12. import org.apache.fop.layout.*;
  13. import org.apache.fop.datatypes.*;
  14. import org.apache.fop.fo.properties.*;
  15. import org.apache.fop.apps.FOPException;
  16. import org.apache.fop.layoutmgr.LayoutManager;
  17. import org.apache.fop.layoutmgr.TextLayoutManager;
  18. import org.apache.fop.layoutmgr.TextBPLayoutManager;
  19. import java.util.NoSuchElementException;
  20. import java.util.List;
  21. /**
  22. * a text node in the formatting object tree
  23. *
  24. * Modified by Mark Lillywhite, mark-fop@inomial.com.
  25. * Unfortunately the BufferManager implementatation holds
  26. * onto references to the character data in this object
  27. * longer than the lifetime of the object itself, causing
  28. * excessive memory consumption and OOM errors.
  29. */
  30. public class FOText extends FObj {
  31. protected char[] ca;
  32. protected int start;
  33. protected int length;
  34. TextInfo textInfo;
  35. TextState ts;
  36. public FOText(char[] chars, int s, int e, TextInfo ti) {
  37. super(null);
  38. this.start = 0;
  39. this.ca = new char[e - s];
  40. System.arraycopy(chars, s, ca, 0, e - s);
  41. this.length = e - s;
  42. textInfo = ti;
  43. }
  44. public boolean willCreateArea() {
  45. if (textInfo.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE &&
  46. length > 0) {
  47. return true;
  48. }
  49. for (int i = start; i < start + length; i++) {
  50. char ch = ca[i];
  51. if (!((ch == ' ') || (ch == '\n') || (ch == '\r') ||
  52. (ch == '\t'))) { // whitespace
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. // Just to keep PageNumber and PageNumber citation happy for now.
  59. // The real code is moved to TextLayoutManager!
  60. public static int addText(BlockArea ba, FontState fontState,
  61. float red, float green, float blue, int wrapOption,
  62. LinkSet ls, int whiteSpaceCollapse, char data[],
  63. int start, int end, TextState textState, int vAlign) {
  64. return 0;
  65. }
  66. public void addLayoutManager(List list) {
  67. // What if nothing left (length=0)?
  68. if (length < ca.length) {
  69. char[] tmp = ca;
  70. ca = new char[length];
  71. System.arraycopy(tmp, 0, ca, 0, length);
  72. }
  73. list.add(new TextLayoutManager(this, ca, textInfo));
  74. // TEST VARIANT USING Karen's BreakPoss scheme
  75. // list.add(new TextBPLayoutManager(this, ca, textInfo));
  76. }
  77. public CharIterator charIterator() {
  78. return new TextCharIterator();
  79. }
  80. private class TextCharIterator extends AbstractCharIterator {
  81. int curIndex = 0;
  82. public boolean hasNext() {
  83. return (curIndex < length);
  84. }
  85. public char nextChar() {
  86. if (curIndex < length) {
  87. // Just a char class? Don't actually care about the value!
  88. return ca[curIndex++];
  89. } else
  90. throw new NoSuchElementException();
  91. }
  92. public void remove() {
  93. if (curIndex > 0 && curIndex < length) {
  94. // copy from curIndex to end to curIndex-1
  95. System.arraycopy(ca, curIndex, ca, curIndex - 1,
  96. length - curIndex);
  97. length--;
  98. curIndex--;
  99. } else if (curIndex == length) {
  100. curIndex = --length;
  101. }
  102. }
  103. public void replaceChar(char c) {
  104. if (curIndex > 0 && curIndex <= length) {
  105. ca[curIndex - 1] = c;
  106. }
  107. }
  108. }
  109. }