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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001-2002 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.FontState;
  10. import org.apache.fop.layout.*;
  11. import org.apache.fop.datatypes.*;
  12. import org.apache.fop.fo.properties.*;
  13. import org.apache.fop.apps.FOPException;
  14. import org.apache.fop.layoutmgr.LayoutManager;
  15. import org.apache.fop.layoutmgr.TextLayoutManager;
  16. import org.apache.fop.apps.StructureHandler;
  17. import java.util.NoSuchElementException;
  18. import java.util.List;
  19. /**
  20. * a text node in the formatting object tree
  21. *
  22. * Modified by Mark Lillywhite, mark-fop@inomial.com.
  23. * Unfortunately the BufferManager implementatation holds
  24. * onto references to the character data in this object
  25. * longer than the lifetime of the object itself, causing
  26. * excessive memory consumption and OOM errors.
  27. */
  28. public class FOText extends FObj {
  29. protected char[] ca;
  30. protected int start;
  31. protected int length;
  32. TextInfo textInfo;
  33. TextState ts;
  34. public FOText(char[] chars, int s, int e, TextInfo ti) {
  35. super(null);
  36. this.start = 0;
  37. this.ca = new char[e - s];
  38. System.arraycopy(chars, s, ca, 0, e - s);
  39. this.length = e - s;
  40. textInfo = ti;
  41. }
  42. public void setStructHandler(StructureHandler st) {
  43. super.setStructHandler(st);
  44. structHandler.characters(ca, start, length);
  45. }
  46. public boolean willCreateArea() {
  47. if (textInfo.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE &&
  48. length > 0) {
  49. return true;
  50. }
  51. for (int i = start; i < start + length; i++) {
  52. char ch = ca[i];
  53. if (!((ch == ' ') || (ch == '\n') || (ch == '\r') ||
  54. (ch == '\t'))) { // whitespace
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. public void addLayoutManager(List list) {
  61. // if nothing left (length=0)?
  62. if(length == 0) { return; }
  63. if (length < ca.length) {
  64. char[] tmp = ca;
  65. ca = new char[length];
  66. System.arraycopy(tmp, 0, ca, 0, length);
  67. }
  68. list.add(new TextLayoutManager(this, ca, textInfo));
  69. }
  70. public CharIterator charIterator() {
  71. return new TextCharIterator();
  72. }
  73. private class TextCharIterator extends AbstractCharIterator {
  74. int curIndex = 0;
  75. public boolean hasNext() {
  76. return (curIndex < length);
  77. }
  78. public char nextChar() {
  79. if (curIndex < length) {
  80. // Just a char class? Don't actually care about the value!
  81. return ca[curIndex++];
  82. } else {
  83. throw new NoSuchElementException();
  84. }
  85. }
  86. public void remove() {
  87. if (curIndex > 0 && curIndex < length) {
  88. // copy from curIndex to end to curIndex-1
  89. System.arraycopy(ca, curIndex, ca, curIndex - 1,
  90. length - curIndex);
  91. length--;
  92. curIndex--;
  93. } else if (curIndex == length) {
  94. curIndex = --length;
  95. }
  96. }
  97. public void replaceChar(char c) {
  98. if (curIndex > 0 && curIndex <= length) {
  99. ca[curIndex - 1] = c;
  100. }
  101. }
  102. }
  103. }