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.

TextInterval.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.bidi;
  19. import org.apache.fop.fo.FONode;
  20. import org.apache.fop.fo.FOText;
  21. import org.apache.fop.fo.flow.AbstractGraphics;
  22. import org.apache.fop.fo.flow.AbstractPageNumberCitation;
  23. import org.apache.fop.fo.flow.BidiOverride;
  24. import org.apache.fop.fo.flow.Character;
  25. import org.apache.fop.fo.flow.Leader;
  26. import org.apache.fop.fo.flow.PageNumber;
  27. // CSOFF: LineLengthCheck
  28. /**
  29. * <p>The <code>TextInterval</code> class is a utility class, the instances of which are used
  30. * to record backpointers to associated nodes over sub-intervals of a delimited text range.</p>
  31. *
  32. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  33. */
  34. class TextInterval {
  35. private FONode fn; // associated node
  36. private int textStart; // starting index within delimited text range of associated node's text
  37. private int start; // starting index within delimited text range
  38. private int end; // ending index within delimited text range
  39. private int level; // resolved level or default (-1)
  40. TextInterval(FONode fn, int start, int end) {
  41. this (fn, start, start, end, -1);
  42. }
  43. TextInterval(FONode fn, int textStart, int start, int end, int level) {
  44. this.fn = fn;
  45. this.textStart = textStart;
  46. this.start = start;
  47. this.end = end;
  48. this.level = level;
  49. }
  50. FONode getNode() {
  51. return fn;
  52. }
  53. int getTextStart() {
  54. return textStart;
  55. }
  56. int getStart() {
  57. return start;
  58. }
  59. int getEnd() {
  60. return end;
  61. }
  62. int getLevel() {
  63. return level;
  64. }
  65. void setLevel(int level) {
  66. this.level = level;
  67. }
  68. public int length() {
  69. return end - start;
  70. }
  71. public String getText() {
  72. if (fn instanceof FOText) {
  73. return ((FOText) fn) .getCharSequence() .toString();
  74. } else if (fn instanceof Character) {
  75. return new String(new char[] {((Character) fn) .getCharacter()});
  76. } else {
  77. return null;
  78. }
  79. }
  80. public void assignTextLevels() {
  81. if (fn instanceof FOText) {
  82. ((FOText) fn) .setBidiLevel(level, start - textStart, end - textStart);
  83. } else if (fn instanceof Character) {
  84. ((Character) fn) .setBidiLevel(level);
  85. } else if (fn instanceof AbstractPageNumberCitation) {
  86. ((AbstractPageNumberCitation) fn) .setBidiLevel(level);
  87. } else if (fn instanceof AbstractGraphics) {
  88. ((AbstractGraphics) fn) .setBidiLevel(level);
  89. } else if (fn instanceof Leader) {
  90. ((Leader) fn) .setBidiLevel(level);
  91. } else if (fn instanceof PageNumber) {
  92. ((PageNumber) fn) .setBidiLevel(level);
  93. }
  94. }
  95. public boolean equals(Object o) {
  96. if (o instanceof TextInterval) {
  97. TextInterval ti = (TextInterval) o;
  98. if (ti.getNode() != fn) {
  99. return false;
  100. } else if (ti.getStart() != start) {
  101. return false;
  102. } else {
  103. return ti.getEnd() == end;
  104. }
  105. } else {
  106. return false;
  107. }
  108. }
  109. public int hashCode() {
  110. int l = (fn != null) ? fn.hashCode() : 0;
  111. l = (l ^ start) + (l << 19);
  112. l = (l ^ end) + (l << 11);
  113. return l;
  114. }
  115. public String toString() {
  116. StringBuffer sb = new StringBuffer();
  117. char c;
  118. if (fn instanceof FOText) {
  119. c = 'T';
  120. } else if (fn instanceof Character) {
  121. c = 'C';
  122. } else if (fn instanceof BidiOverride) {
  123. c = 'B';
  124. } else if (fn instanceof AbstractPageNumberCitation) {
  125. c = '#';
  126. } else if (fn instanceof AbstractGraphics) {
  127. c = 'G';
  128. } else if (fn instanceof Leader) {
  129. c = 'L';
  130. } else if (fn instanceof PageNumber) {
  131. c = '#';
  132. } else {
  133. c = '?';
  134. }
  135. sb.append(c);
  136. sb.append("[" + start + "," + end + "][" + textStart + "](" + level + ")");
  137. return sb.toString();
  138. }
  139. }