1 51 package org.apache.fop.fo.pagination; 52 53 import org.apache.fop.fo.*; 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 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 64 71 public class SimplePageMaster extends FObj { 72 private static final int FALLBACK_PAGE_HEIGHT = 792000; 74 private static final int FALLBACK_PAGE_WIDTH = 576000; 75 76 public static class Maker extends FObj.Maker { 77 public FObj make(FObj parent, PropertyList propertyList, 78 String systemId, int line, int column) 79 throws FOPException { 80 return new SimplePageMaster(parent, propertyList, 81 systemId, line, column); 82 } 83 84 } 85 86 public static FObj.Maker maker() { 87 return new SimplePageMaster.Maker(); 88 } 89 90 93 private HashMap _regions; 94 95 96 LayoutMasterSet layoutMasterSet; 97 PageMaster pageMaster; 98 String masterName; 99 100 boolean beforePrecedence = false; 102 int beforeExtent, afterExtent, startExtent, endExtent; 103 boolean afterPrecedence = false; 104 105 protected SimplePageMaster(FObj parent, PropertyList propertyList, 106 String systemId, int line, int column) 107 throws FOPException { 108 super(parent, propertyList, systemId, line, column); 109 110 if (parent.getName().equals("fo:layout-master-set")) { 111 this.layoutMasterSet = (LayoutMasterSet)parent; 112 masterName = this.properties.get("master-name").getString(); 113 if (masterName == null) { 114 log.warn("simple-page-master does not have " 115 + "a master-name and so is being ignored"); 116 } else { 117 this.layoutMasterSet.addSimplePageMaster(this); 118 } 119 } else { 120 throw new FOPException("fo:simple-page-master must be child " 121 + "of fo:layout-master-set, not " 122 + parent.getName(), systemId, line, column); 123 } 124 _regions = new HashMap (); 125 126 } 127 128 public String getName() { 129 return "fo:simple-page-master"; 130 } 131 132 protected void end() { 133 Length pageWidthLen = this.properties.get("page-width").getLength(); 134 int pageWidth = pageWidthLen.isAuto() ? FALLBACK_PAGE_WIDTH : pageWidthLen.mvalue(); 135 Length pageHeightLen = this.properties.get("page-height").getLength(); 136 int pageHeight = pageHeightLen.isAuto() ? FALLBACK_PAGE_HEIGHT : pageHeightLen.mvalue(); 137 140 MarginProps mProps = propMgr.getMarginProps(); 142 143 int contentRectangleXPosition = mProps.marginLeft; 144 int contentRectangleYPosition = pageHeight - mProps.marginTop; 145 int contentRectangleWidth = pageWidth - mProps.marginLeft 146 - mProps.marginRight; 147 int contentRectangleHeight = pageHeight - mProps.marginTop 148 - mProps.marginBottom; 149 this.pageMaster = new PageMaster(pageWidth, pageHeight); 150 Region body = getRegion(RegionBody.REGION_CLASS); 151 RegionBefore before = (RegionBefore)getRegion(RegionBefore.REGION_CLASS); 152 RegionAfter after = (RegionAfter)getRegion(RegionAfter.REGION_CLASS); 153 RegionStart start = (RegionStart)getRegion(RegionStart.REGION_CLASS); 154 RegionEnd end = (RegionEnd)getRegion(RegionEnd.REGION_CLASS); 155 if (before != null) { 156 beforePrecedence = before.getPrecedence(); 157 beforeExtent = before.getExtent(); 158 } 159 if (after != null) { 160 afterPrecedence = after.getPrecedence(); 161 afterExtent = after.getExtent(); 162 } 163 if (start != null) 164 startExtent = start.getExtent(); 165 if (end != null) 166 endExtent = end.getExtent(); 167 168 if (body != null) 169 this.pageMaster.addBody((BodyRegionArea)body.makeRegionArea( 170 contentRectangleXPosition, 171 contentRectangleYPosition, 172 contentRectangleWidth, 173 contentRectangleHeight)); 174 else 175 log.error("simple-page-master must have a region of class " 176 + RegionBody.REGION_CLASS); 177 178 if (before != null) 179 this.pageMaster.addBefore(before.makeRegionArea(contentRectangleXPosition, 180 contentRectangleYPosition, contentRectangleWidth, 181 contentRectangleHeight, startExtent, endExtent)); 182 183 if (after != null) 184 this.pageMaster.addAfter(after.makeRegionArea(contentRectangleXPosition, 185 contentRectangleYPosition, contentRectangleWidth, 186 contentRectangleHeight, startExtent, endExtent)); 187 188 if (start != null) 189 this.pageMaster.addStart(start.makeRegionArea(contentRectangleXPosition, 190 contentRectangleYPosition, contentRectangleWidth, 191 contentRectangleHeight, beforePrecedence, 192 afterPrecedence, beforeExtent, afterExtent)); 193 194 if (end != null) 195 this.pageMaster.addEnd(end.makeRegionArea(contentRectangleXPosition, 196 contentRectangleYPosition, contentRectangleWidth, 197 contentRectangleHeight, beforePrecedence, 198 afterPrecedence, beforeExtent, afterExtent)); 199 } 200 201 public PageMaster getPageMaster() { 202 return this.pageMaster; 203 } 204 205 public PageMaster getNextPageMaster() { 206 return this.pageMaster; 207 } 208 209 public String getMasterName() { 210 return masterName; 211 } 212 213 214 protected void addRegion(Region region) throws FOPException { 215 if (_regions.containsKey(region.getRegionClass())) { 216 throw new FOPException("Only one region of class " 217 + region.getRegionClass() 218 + " allowed within a simple-page-master.", 219 systemId, line, column); 220 } else { 221 _regions.put(region.getRegionClass(), region); 222 } 223 } 224 225 protected Region getRegion(String regionClass) { 226 return (Region)_regions.get(regionClass); 227 } 228 229 protected HashMap getRegions() { 230 return _regions; 231 } 232 233 protected boolean regionNameExists(String regionName) { 234 for (Iterator i = _regions.values().iterator(); i.hasNext(); ) { 235 Region r = (Region)i.next(); 236 if (r.getRegionName().equals(regionName)) { 237 return true; 238 } 239 } 240 return false; 241 } 242 243 } 244 | Popular Tags |