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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo.pagination;
  8. // FOP
  9. import org.apache.fop.fo.*;
  10. import org.apache.fop.fo.properties.*;
  11. import org.apache.fop.area.CTM;
  12. import org.apache.fop.datatypes.FODimension;
  13. import org.apache.fop.area.PageViewport;
  14. import org.apache.fop.area.Page;
  15. import org.apache.fop.area.RegionViewport;
  16. import org.apache.fop.area.RegionReference;
  17. import org.apache.fop.layout.MarginProps;
  18. import org.apache.fop.layout.PageMaster;
  19. import org.apache.fop.apps.FOPException;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.Rectangle2D;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import org.xml.sax.Attributes;
  25. /**
  26. * A simple-page-master formatting object.
  27. * This creates a simple page from the specified regions
  28. * and attributes.
  29. */
  30. public class SimplePageMaster extends FObj {
  31. /**
  32. * Page regions (regionClass, Region)
  33. */
  34. private HashMap _regions;
  35. PageMaster pageMaster;
  36. String masterName;
  37. public SimplePageMaster(FONode parent) {
  38. super(parent);
  39. }
  40. public void handleAttrs(Attributes attlist) throws FOPException {
  41. super.handleAttrs(attlist);
  42. if (parent.getName().equals("fo:layout-master-set")) {
  43. LayoutMasterSet layoutMasterSet = (LayoutMasterSet)parent;
  44. masterName = this.properties.get("master-name").getString();
  45. if (masterName == null) {
  46. getLogger().warn("simple-page-master does not have "
  47. + "a master-name and so is being ignored");
  48. } else {
  49. layoutMasterSet.addSimplePageMaster(this);
  50. }
  51. } else {
  52. throw new FOPException("fo:simple-page-master must be child "
  53. + "of fo:layout-master-set, not "
  54. + parent.getName());
  55. }
  56. _regions = new HashMap();
  57. }
  58. /**
  59. * At the end of this element read all the information and create
  60. * the page master.
  61. */
  62. protected void end() {
  63. int pageWidth =
  64. this.properties.get("page-width").getLength().mvalue();
  65. int pageHeight =
  66. this.properties.get("page-height").getLength().mvalue();
  67. // this.properties.get("reference-orientation");
  68. // this.properties.get("writing-mode");
  69. // Get absolute margin properties (top, left, bottom, right)
  70. MarginProps mProps = propMgr.getMarginProps();
  71. /* Create the page reference area rectangle in first quadrant coordinates
  72. * (ie, 0,0 is at bottom,left of the "page media" and y increases
  73. * when moving towards the top of the page.
  74. * The media rectangle itself is (0,0,pageWidth,pageHeight).
  75. */
  76. Rectangle pageRefRect =
  77. new Rectangle(mProps.marginLeft, mProps.marginTop,
  78. pageWidth - mProps.marginLeft - mProps.marginRight,
  79. pageHeight - mProps.marginTop - mProps.marginBottom);
  80. // ??? KL shouldn't this take the viewport too???
  81. Page page = new Page(); // page reference area
  82. // Set up the CTM on the page reference area based on writing-mode
  83. // and reference-orientation
  84. FODimension reldims=new FODimension(0,0);
  85. CTM pageCTM = propMgr.getCTMandRelDims(pageRefRect, reldims);
  86. // Create a RegionViewport/ reference area pair for each page region
  87. boolean bHasBody=false;
  88. for (Iterator regenum = _regions.values().iterator();
  89. regenum.hasNext(); ) {
  90. Region r = (Region)regenum.next();
  91. RegionViewport rvp = r.makeRegionViewport(reldims, pageCTM);
  92. rvp.setRegion(r.makeRegionReferenceArea(rvp.getViewArea()));
  93. page.setRegion(r.getRegionAreaClass(), rvp);
  94. if (r.getRegionAreaClass() == RegionReference.BODY) {
  95. bHasBody = true;
  96. }
  97. }
  98. if (!bHasBody) {
  99. getLogger().error("simple-page-master has no region-body");
  100. }
  101. this.pageMaster = new PageMaster(new PageViewport(page,
  102. new Rectangle(0,0,
  103. pageWidth,pageHeight)));
  104. // _regions = null; // PageSequence access SimplePageMaster....
  105. children = null;
  106. properties = null;
  107. }
  108. public boolean generatesReferenceAreas() {
  109. return true;
  110. }
  111. public PageMaster getPageMaster() {
  112. return this.pageMaster;
  113. }
  114. public PageMaster getNextPageMaster() {
  115. return this.pageMaster;
  116. }
  117. public String getMasterName() {
  118. return masterName;
  119. }
  120. protected void addChild(FONode child) {
  121. if (child instanceof Region) {
  122. addRegion((Region)child);
  123. } else {
  124. getLogger().error("SimplePageMaster cannot have child of type " +
  125. child.getName());
  126. }
  127. }
  128. protected void addRegion(Region region) {
  129. String key = region.getRegionClass();
  130. if (_regions.containsKey(key)) {
  131. getLogger().error("Only one region of class "
  132. + key
  133. + " allowed within a simple-page-master.");
  134. // throw new FOPException("Only one region of class "
  135. // + key
  136. // + " allowed within a simple-page-master.");
  137. } else {
  138. _regions.put(key, region);
  139. }
  140. }
  141. protected Region getRegion(String regionClass) {
  142. return (Region)_regions.get(regionClass);
  143. }
  144. protected HashMap getRegions() {
  145. return _regions;
  146. }
  147. protected boolean regionNameExists(String regionName) {
  148. for (Iterator regenum = _regions.values().iterator();
  149. regenum.hasNext(); ) {
  150. Region r = (Region)regenum.next();
  151. if (r.getRegionName().equals(regionName)) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. }