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.

UnresolvedPageNumber.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.area.inline;
  19. import java.util.List;
  20. import org.apache.fop.area.PageViewport;
  21. import org.apache.fop.area.Resolvable;
  22. import org.apache.fop.complexscripts.bidi.InlineRun;
  23. import org.apache.fop.fonts.Font;
  24. /**
  25. * Unresolvable page number area.
  26. * This is a word area that resolves itself to a page number
  27. * from an id reference.
  28. */
  29. public class UnresolvedPageNumber extends TextArea implements Resolvable {
  30. private static final long serialVersionUID = -1758090835371647980L;
  31. private boolean resolved;
  32. private String pageIDRef;
  33. private String text;
  34. private boolean pageType;
  35. /** Indicates that the reference refers to the first area generated by a formatting object. */
  36. public static final boolean FIRST = true;
  37. /** Indicates that the reference refers to the last area generated by a formatting object. */
  38. public static final boolean LAST = false;
  39. //Transient fields
  40. private transient Font font;
  41. /**
  42. * Create a new unresolved page number.
  43. *
  44. * @param id the id reference for resolving this
  45. * @param f the font for formatting the page number
  46. */
  47. public UnresolvedPageNumber(String id, Font f) {
  48. this(id, f, FIRST);
  49. }
  50. /**
  51. * Create a new unresolved page number.
  52. *
  53. * @param id the id reference for resolving this
  54. * @param f the font for formatting the page number
  55. * @param type indicates whether the reference refers to the first or last area generated by
  56. * a formatting object
  57. */
  58. public UnresolvedPageNumber(String id, Font f, boolean type) {
  59. pageIDRef = id;
  60. font = f;
  61. text = "?";
  62. pageType = type;
  63. }
  64. /**
  65. * Get the id references for this area.
  66. *
  67. * @return the id reference for this unresolved page number
  68. */
  69. public String[] getIDRefs() {
  70. return new String[] {pageIDRef};
  71. }
  72. /**
  73. * Get the (resolved or unresolved) text.
  74. *
  75. * @return the text
  76. */
  77. public String getText() {
  78. return text;
  79. }
  80. /**
  81. * Resolve the page number idref
  82. * This resolves the idref for this object by getting the page number
  83. * string from the first page in the list of pages that apply
  84. * for this ID. The page number text is then set to the String value
  85. * of the page number.
  86. *
  87. * TODO: [GA] May need to run bidi algorithm and script processor
  88. * on resolved page number.
  89. *
  90. * @param id an id whose PageViewports have been determined
  91. * @param pages the list of PageViewports associated with this ID
  92. */
  93. public void resolveIDRef(String id, List<PageViewport> pages) {
  94. if (!resolved && pageIDRef.equals(id) && pages != null) {
  95. if (log.isDebugEnabled()) {
  96. log.debug("Resolving pageNumber: " + id);
  97. }
  98. resolved = true;
  99. int pageIndex = pageType ? 0 : pages.size() - 1;
  100. PageViewport page = pages.get(pageIndex);
  101. // replace the text
  102. removeText();
  103. text = page.getPageNumberString();
  104. addWord(text, 0, getBidiLevel());
  105. // update ipd
  106. if (font != null) {
  107. handleIPDVariation(font.getWordWidth(text) - getIPD());
  108. // set the Font object to null, as we don't need it any more
  109. font = null;
  110. } else {
  111. log.warn("Cannot update the IPD of an unresolved page number."
  112. + " No font information available.");
  113. }
  114. }
  115. }
  116. /**
  117. * Check if this is resolved.
  118. *
  119. * @return true when this has been resolved
  120. */
  121. public boolean isResolved() {
  122. return resolved;
  123. }
  124. /**
  125. * recursively apply the variation factor to all descendant areas
  126. * @param variationFactor the variation factor that must be applied to adjustment ratios
  127. * @param lineStretch the total stretch of the line
  128. * @param lineShrink the total shrink of the line
  129. * @return true if there is an UnresolvedArea descendant
  130. */
  131. @Override
  132. public boolean applyVariationFactor(double variationFactor,
  133. int lineStretch, int lineShrink) {
  134. return true;
  135. }
  136. /**
  137. * Collection bidi inline runs.
  138. * Override of @{link InlineParent} implementation.
  139. *
  140. * N.B. [GA] without this override, the page-number-citation_writing_mode_rl
  141. * layout engine test will fail. It may be that the test needs to
  142. * be updated rather than using this override.
  143. * @param runs current list of inline runs
  144. * @return modified list of inline runs, having appended new run
  145. */
  146. @Override
  147. public List collectInlineRuns(List runs) {
  148. assert runs != null;
  149. runs.add(new InlineRun(this, new int[] {getBidiLevel()}));
  150. return runs;
  151. }
  152. }