Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.render.rtf.rtflib.tools;
  19. import java.util.Map;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.fop.datatypes.LengthBase;
  23. import org.apache.fop.datatypes.PercentBaseContext;
  24. import org.apache.fop.fo.FONode;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.flow.table.Table;
  27. import org.apache.fop.fo.flow.table.TableColumn;
  28. import org.apache.fop.fo.pagination.PageSequence;
  29. /**
  30. * <p>PercentBaseContext implementation to track base widths for percentage calculations.</p>
  31. */
  32. public class PercentContext implements PercentBaseContext {
  33. private static Log log = LogFactory.getLog(PercentContext.class);
  34. /** Map containing the FObj and its width */
  35. private Map lengthMap = new java.util.HashMap();
  36. /** Map containing the Tables and their table units */
  37. private Map tableUnitMap = new java.util.HashMap();
  38. /** Variable to check if a base width is set */
  39. private boolean baseWidthSet;
  40. /**
  41. * Returns the available width for a specific FObj
  42. *
  43. * @param lengthBase
  44. * lengthBase not used
  45. * @param fobj
  46. * the FObj
  47. * @return Available Width
  48. */
  49. public int getBaseLength(int lengthBase, FObj fobj) {
  50. if (fobj == null) {
  51. return 0;
  52. }
  53. // Special handler for TableColumn width specifications, needs to be
  54. // relative to the parent!
  55. if ((fobj instanceof TableColumn) && (fobj.getParent() instanceof FObj)) {
  56. fobj = (FObj) fobj.getParent();
  57. }
  58. switch (lengthBase) {
  59. case LengthBase.CONTAINING_BLOCK_WIDTH:
  60. case LengthBase.PARENT_AREA_WIDTH:
  61. case LengthBase.CONTAINING_REFAREA_WIDTH:
  62. Object width = lengthMap.get(fobj);
  63. if (width != null) {
  64. return Integer.parseInt(width.toString());
  65. } else if (fobj.getParent() != null) {
  66. // If the object itself has no width the parent width will be used
  67. // because it is the base width of this object
  68. width = lengthMap.get(fobj.getParent());
  69. if (width != null) {
  70. return Integer.parseInt(width.toString());
  71. }
  72. }
  73. return 0;
  74. case LengthBase.TABLE_UNITS:
  75. Object unit = tableUnitMap.get(fobj);
  76. if (unit != null) {
  77. return (Integer) unit;
  78. } else if (fobj.getParent() != null) {
  79. // If the object itself has no width the parent width will be used
  80. unit = tableUnitMap.get(fobj.getParent());
  81. if (unit != null) {
  82. return (Integer) unit;
  83. }
  84. }
  85. return 0;
  86. default:
  87. log.error(new Exception("Unsupported base type for LengthBase:" + lengthBase));
  88. return 0;
  89. }
  90. }
  91. /**
  92. * Elements having a width property can call this function if their width is
  93. * calculated in RTFHandler
  94. *
  95. * @param fobj the FObj
  96. * @param width width of the FObj (in millipoints)
  97. */
  98. public void setDimension(FObj fobj, int width) {
  99. // TODO ACCEPT only objects above for setting a width
  100. if (fobj instanceof PageSequence) {
  101. baseWidthSet = true;
  102. }
  103. // width in mpt
  104. lengthMap.put(fobj, width);
  105. }
  106. /**
  107. * Records the calculated table unit for a given table.
  108. * @param table the table for which the table unit is set
  109. * @param tableUnit the table unit value (in millipoints)
  110. */
  111. public void setTableUnit(Table table, int tableUnit) {
  112. tableUnitMap.put(table, tableUnit);
  113. }
  114. /**
  115. * Searches for the parent object of fobj.
  116. */
  117. private Integer findParent(FONode fobj) {
  118. if (fobj.getRoot() != fobj) {
  119. if (lengthMap.containsKey(fobj)) {
  120. return Integer.valueOf(lengthMap.get(fobj).toString());
  121. } else {
  122. return findParent(fobj.getParent());
  123. }
  124. } else {
  125. log.error("Base Value for element " + fobj.getName() + " not found");
  126. return -1;
  127. }
  128. }
  129. /**
  130. * Elements willing to use this context have to register themselves by
  131. * calling this function.
  132. *
  133. * @param fobj the FObj
  134. */
  135. public void setDimension(FObj fobj) {
  136. if (baseWidthSet) {
  137. Integer width = findParent(fobj.getParent());
  138. if (width != -1) {
  139. lengthMap.put(fobj, width);
  140. }
  141. }
  142. }
  143. }