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

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