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.

SimplePageMaster.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo.pagination;
  52. // FOP
  53. import org.apache.fop.fo.FObj;
  54. import org.apache.fop.fo.PropertyList;
  55. import org.apache.fop.layout.PageMaster;
  56. import org.apache.fop.layout.BodyRegionArea;
  57. import org.apache.fop.layout.MarginProps;
  58. import org.apache.fop.apps.FOPException;
  59. import org.apache.fop.datatypes.Length;
  60. import java.util.HashMap;
  61. import java.util.Iterator;
  62. /**
  63. * Class modeling the fo:simple-page-master object.
  64. *
  65. * @see <a href="@XSLFO-STD@#fo_simple-page-master"
  66. * target="_xslfostd">@XSLFO-STDID@
  67. * &para;6.4.12</a>
  68. */
  69. public class SimplePageMaster extends FObj {
  70. // Fallback values for "auto" page size: 8x11in
  71. private static final int FALLBACK_PAGE_HEIGHT = 792000;
  72. private static final int FALLBACK_PAGE_WIDTH = 576000;
  73. public static class Maker extends FObj.Maker {
  74. public FObj make(FObj parent, PropertyList propertyList,
  75. String systemId, int line, int column)
  76. throws FOPException {
  77. return new SimplePageMaster(parent, propertyList,
  78. systemId, line, column);
  79. }
  80. }
  81. public static FObj.Maker maker() {
  82. return new SimplePageMaster.Maker();
  83. }
  84. /**
  85. * Page regions (regionClass, Region)
  86. */
  87. private HashMap _regions;
  88. LayoutMasterSet layoutMasterSet;
  89. PageMaster pageMaster;
  90. String masterName;
  91. // before and after data as required by start and end
  92. boolean beforePrecedence = false;
  93. int beforeExtent, afterExtent, startExtent, endExtent;
  94. boolean afterPrecedence = false;
  95. protected SimplePageMaster(FObj parent, PropertyList propertyList,
  96. String systemId, int line, int column)
  97. throws FOPException {
  98. super(parent, propertyList, systemId, line, column);
  99. if (parent.getName().equals("fo:layout-master-set")) {
  100. this.layoutMasterSet = (LayoutMasterSet)parent;
  101. masterName = this.properties.get("master-name").getString();
  102. if (masterName == null) {
  103. log.warn("simple-page-master does not have "
  104. + "a master-name and so is being ignored");
  105. } else {
  106. this.layoutMasterSet.addSimplePageMaster(this);
  107. }
  108. } else {
  109. throw new FOPException("fo:simple-page-master must be child "
  110. + "of fo:layout-master-set, not "
  111. + parent.getName(), systemId, line, column);
  112. }
  113. _regions = new HashMap();
  114. }
  115. public String getName() {
  116. return "fo:simple-page-master";
  117. }
  118. protected void end() {
  119. Length pageWidthLen = this.properties.get("page-width").getLength();
  120. int pageWidth = pageWidthLen.isAuto() ? FALLBACK_PAGE_WIDTH : pageWidthLen.mvalue();
  121. Length pageHeightLen = this.properties.get("page-height").getLength();
  122. int pageHeight = pageHeightLen.isAuto() ? FALLBACK_PAGE_HEIGHT : pageHeightLen.mvalue();
  123. // this.properties.get("reference-orientation");
  124. // this.properties.get("writing-mode");
  125. // Common Margin Properties-Block
  126. MarginProps mProps = propMgr.getMarginProps();
  127. int contentRectangleXPosition = mProps.marginLeft;
  128. int contentRectangleYPosition = pageHeight - mProps.marginTop;
  129. int contentRectangleWidth = pageWidth - mProps.marginLeft
  130. - mProps.marginRight;
  131. int contentRectangleHeight = pageHeight - mProps.marginTop
  132. - mProps.marginBottom;
  133. this.pageMaster = new PageMaster(pageWidth, pageHeight);
  134. Region body = getRegion(RegionBody.REGION_CLASS);
  135. RegionBefore before = (RegionBefore)getRegion(RegionBefore.REGION_CLASS);
  136. RegionAfter after = (RegionAfter)getRegion(RegionAfter.REGION_CLASS);
  137. RegionStart start = (RegionStart)getRegion(RegionStart.REGION_CLASS);
  138. RegionEnd end = (RegionEnd)getRegion(RegionEnd.REGION_CLASS);
  139. if (before != null) {
  140. beforePrecedence = before.getPrecedence();
  141. beforeExtent = before.getExtent();
  142. }
  143. if (after != null) {
  144. afterPrecedence = after.getPrecedence();
  145. afterExtent = after.getExtent();
  146. }
  147. if (start != null)
  148. startExtent = start.getExtent();
  149. if (end != null)
  150. endExtent = end.getExtent();
  151. if (body != null)
  152. this.pageMaster.addBody((BodyRegionArea)body.makeRegionArea(
  153. contentRectangleXPosition,
  154. contentRectangleYPosition,
  155. contentRectangleWidth,
  156. contentRectangleHeight));
  157. else
  158. log.error("simple-page-master must have a region of class "
  159. + RegionBody.REGION_CLASS);
  160. if (before != null)
  161. this.pageMaster.addBefore(before.makeRegionArea(contentRectangleXPosition,
  162. contentRectangleYPosition, contentRectangleWidth,
  163. contentRectangleHeight, startExtent, endExtent));
  164. if (after != null)
  165. this.pageMaster.addAfter(after.makeRegionArea(contentRectangleXPosition,
  166. contentRectangleYPosition, contentRectangleWidth,
  167. contentRectangleHeight, startExtent, endExtent));
  168. if (start != null)
  169. this.pageMaster.addStart(start.makeRegionArea(contentRectangleXPosition,
  170. contentRectangleYPosition, contentRectangleWidth,
  171. contentRectangleHeight, beforePrecedence,
  172. afterPrecedence, beforeExtent, afterExtent));
  173. if (end != null)
  174. this.pageMaster.addEnd(end.makeRegionArea(contentRectangleXPosition,
  175. contentRectangleYPosition, contentRectangleWidth,
  176. contentRectangleHeight, beforePrecedence,
  177. afterPrecedence, beforeExtent, afterExtent));
  178. }
  179. public PageMaster getPageMaster() {
  180. return this.pageMaster;
  181. }
  182. public PageMaster getNextPageMaster() {
  183. return this.pageMaster;
  184. }
  185. public String getMasterName() {
  186. return masterName;
  187. }
  188. protected void addRegion(Region region) throws FOPException {
  189. if (_regions.containsKey(region.getRegionClass())) {
  190. throw new FOPException("Only one region of class "
  191. + region.getRegionClass()
  192. + " allowed within a simple-page-master.",
  193. systemId, line, column);
  194. } else {
  195. _regions.put(region.getRegionClass(), region);
  196. }
  197. }
  198. protected Region getRegion(String regionClass) {
  199. return (Region)_regions.get(regionClass);
  200. }
  201. protected HashMap getRegions() {
  202. return _regions;
  203. }
  204. protected boolean regionNameExists(String regionName) {
  205. for (Iterator i = _regions.values().iterator(); i.hasNext(); ) {
  206. Region r = (Region)i.next();
  207. if (r.getRegionName().equals(regionName)) {
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. }