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.

PercentContext.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.pagination.PageSequence;
  28. /**
  29. * PercentBaseContext implementation to track base widths for percentage calculations.
  30. */
  31. public class PercentContext implements PercentBaseContext {
  32. private static Log log = LogFactory.getLog(PercentContext.class);
  33. /** Map containing the FObj and its width */
  34. private Map lengthMap = new java.util.HashMap();
  35. /** Map containing the Tables and their table units */
  36. private Map tableUnitMap = new java.util.HashMap();
  37. /** Variable to check if a base width is set */
  38. private boolean baseWidthSet = false;
  39. /**
  40. * Returns the available width for a specific FObj
  41. *
  42. * @param lengthBase
  43. * lengthBase not used
  44. * @param fobj
  45. * the FObj
  46. * @return Available Width
  47. */
  48. public int getBaseLength(int lengthBase, FObj fobj) {
  49. if (fobj == null) {
  50. return 0;
  51. }
  52. switch (lengthBase) {
  53. case LengthBase.CONTAINING_BLOCK_WIDTH:
  54. case LengthBase.PARENT_AREA_WIDTH:
  55. case LengthBase.CONTAINING_REFAREA_WIDTH:
  56. Object width = lengthMap.get(fobj);
  57. if (width != null) {
  58. return Integer.parseInt(width.toString());
  59. } else {
  60. return -1;
  61. }
  62. case LengthBase.TABLE_UNITS:
  63. Object unit = tableUnitMap.get(fobj);
  64. return (unit != null) ? ((Integer)unit).intValue() : 0;
  65. default:
  66. log.error(new Exception("Unsupported base type for LengthBase:" + lengthBase));
  67. return 0;
  68. }
  69. }
  70. /**
  71. * Elements having a width property can call this function if their width is
  72. * calculated in RTFHandler
  73. *
  74. * @param fobj the FObj
  75. * @param width width of the FObj (in millipoints)
  76. */
  77. public void setDimension(FObj fobj, int width) {
  78. // TODO ACCEPT only objects above for setting a width
  79. if (fobj instanceof PageSequence) {
  80. baseWidthSet = true;
  81. }
  82. // width in mpt
  83. lengthMap.put(fobj, new Integer(width));
  84. }
  85. /**
  86. * Records the calculated table unit for a given table.
  87. * @param table the table for which the table unit is set
  88. * @param tableUnit the table unit value (in millipoints)
  89. */
  90. public void setTableUnit(Table table, int tableUnit) {
  91. tableUnitMap.put(table, new Integer(tableUnit));
  92. }
  93. /**
  94. * Searches for the parent object of fobj.
  95. */
  96. private Integer findParent(FONode fobj) {
  97. if (fobj.getRoot() != fobj) {
  98. if (lengthMap.containsKey(fobj)) {
  99. return new Integer(lengthMap.get(fobj).toString());
  100. } else {
  101. return findParent(fobj.getParent());
  102. }
  103. } else {
  104. log.error("Base Value for element " + fobj.getName() + " not found");
  105. return new Integer(-1);
  106. }
  107. }
  108. /**
  109. * Elements willing to use this context have to register themselves by
  110. * calling this function.
  111. *
  112. * @param fobj the FObj
  113. */
  114. public void setDimension(FObj fobj) {
  115. if (baseWidthSet) {
  116. Integer width = findParent(fobj.getParent());
  117. if (width.intValue() != -1) {
  118. lengthMap.put(fobj, width);
  119. }
  120. }
  121. }
  122. }