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 
18 /* $Id: Region.java 1293736 2012-02-26 02:29:01Z gadams $ */
19 
20 package org.apache.fop.fo.pagination;
21 
22 import java.awt.Rectangle;
23 
24 import org.xml.sax.Locator;
25 
26 import org.apache.fop.apps.FOPException;
27 import org.apache.fop.datatypes.FODimension;
28 import org.apache.fop.datatypes.Numeric;
29 import org.apache.fop.datatypes.PercentBaseContext;
30 import org.apache.fop.fo.FONode;
31 import org.apache.fop.fo.FObj;
32 import org.apache.fop.fo.PropertyList;
33 import org.apache.fop.fo.ValidationException;
34 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
35 import org.apache.fop.traits.WritingMode;
36 
37 /**
38  * This is an abstract base class for pagination regions.
39  */
40 public abstract class Region extends FObj {
41     // The value of FO traits (refined properties) that apply to fo:region
42     private CommonBorderPaddingBackground commonBorderPaddingBackground;
43     // private ToBeImplementedProperty clip
44     private int displayAlign;
45     private int overflow;
46     private String regionName;
47     private Numeric referenceOrientation;
48     private WritingMode writingMode;
49     // End of FO trait values
50 
51     /** the parent {@link SimplePageMaster} */
52     protected final SimplePageMaster layoutMaster;
53 
54     /**
55      * Base constructor
56      *
57      * @param parent {@link FONode} that is the parent of this object
58      */
Region(FONode parent)59     protected Region(FONode parent) {
60         super(parent);
61         layoutMaster = (SimplePageMaster) parent;
62     }
63 
64     /** {@inheritDoc} */
bind(PropertyList pList)65     public void bind(PropertyList pList) throws FOPException {
66         commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
67         // clip = pList.get(PR_CLIP);
68         displayAlign = pList.get(PR_DISPLAY_ALIGN).getEnum();
69         overflow = pList.get(PR_OVERFLOW).getEnum();
70         regionName = pList.get(PR_REGION_NAME).getString();
71         referenceOrientation = pList.get(PR_REFERENCE_ORIENTATION).getNumeric();
72         writingMode = WritingMode.valueOf(pList.get(PR_WRITING_MODE).getEnum());
73 
74         // regions may have name, or default
75         if (regionName.equals("")) {
76             regionName = getDefaultRegionName();
77         } else {
78             // check that name is OK. Not very pretty.
79             if (isReserved(getRegionName())
80                     && !getRegionName().equals(getDefaultRegionName())) {
81                 getFOValidationEventProducer().illegalRegionName(this, getName(),
82                         regionName, getLocator());
83             }
84         }
85 
86         //TODO do we need context for getBPPaddingAndBorder() and getIPPaddingAndBorder()?
87         if ((getCommonBorderPaddingBackground().getBPPaddingAndBorder(false, null) != 0
88                 || getCommonBorderPaddingBackground().getIPPaddingAndBorder(false, null) != 0)) {
89             getFOValidationEventProducer().nonZeroBorderPaddingOnRegion(this, getName(),
90                     regionName, true, getLocator());
91         }
92     }
93 
94     /**
95      * {@inheritDoc} String, String)
96      * <br>XSL Content Model: empty
97      */
validateChildNode(Locator loc, String nsURI, String localName)98     protected void validateChildNode(Locator loc, String nsURI, String localName)
99                 throws ValidationException {
100         if (FO_URI.equals(nsURI)) {
101             invalidChildError(loc, nsURI, localName);
102         }
103     }
104 
105     /**
106      * @param pageRefRect reference dimension of the page area.
107      * @return the rectangle for the viewport area
108      */
getViewportRectangle(FODimension pageRefRect)109     public abstract Rectangle getViewportRectangle(FODimension pageRefRect);
110 
111     /**
112      * Returns the default region name (xsl-region-before, xsl-region-start,
113      * etc.)
114      * @return the default region name
115      */
getDefaultRegionName()116     protected abstract String getDefaultRegionName();
117 
118     /**
119      * Checks to see if a given region name is one of the reserved names
120      *
121      * @param name a region name to check
122      * @return true if the name parameter is a reserved region name
123      */
isReserved(String name)124     protected boolean isReserved(String name) {
125         return (name.equals("xsl-region-before")
126                 || name.equals("xsl-region-start")
127                 || name.equals("xsl-region-end")
128                 || name.equals("xsl-region-after")
129                 || name.equals("xsl-before-float-separator")
130                 || name.equals("xsl-footnote-separator"));
131     }
132 
133     /**
134      * Get the page-width context
135      * @param lengthBase    the lengthBase to use for resolving percentages
136      * @return  context for the width of the page-reference-area
137      */
getPageWidthContext(int lengthBase)138     protected PercentBaseContext getPageWidthContext(int lengthBase) {
139         return layoutMaster.getPageWidthContext(lengthBase);
140     }
141 
142     /**
143      * Get the page-width context
144      * @param lengthBase    the lengthBase to use for resolving percentages
145      * @return  context for the width of the page-reference-area
146      */
getPageHeightContext(int lengthBase)147     protected PercentBaseContext getPageHeightContext(int lengthBase) {
148         return layoutMaster.getPageHeightContext(lengthBase);
149     }
150 
151     /** {@inheritDoc} */
generatesReferenceAreas()152     public boolean generatesReferenceAreas() {
153         return true;
154     }
155 
156     /**
157      * Returns a sibling region for this region.
158      * @param regionId the Constants ID of the FO representing the region
159      * @return the requested region
160      */
getSiblingRegion(int regionId)161     protected Region getSiblingRegion(int regionId) {
162         // Ask parent for region
163         return layoutMaster.getRegion(regionId);
164     }
165 
166     /**
167      * @return the Background Properties (border and padding are not used here).
168      */
getCommonBorderPaddingBackground()169     public CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
170         return commonBorderPaddingBackground;
171     }
172 
173     /** @return the "region-name" FO trait. */
getRegionName()174     public String getRegionName() {
175         return regionName;
176     }
177 
178     /** @return the "overflow" FO trait. */
getOverflow()179     public int getOverflow() {
180         return overflow;
181     }
182 
183     /** @return the display-align FO trait. */
getDisplayAlign()184     public int getDisplayAlign() {
185         return displayAlign;
186     }
187 
188     /** @return the "reference-orientation" FO trait. */
getReferenceOrientation()189     public int getReferenceOrientation() {
190         return referenceOrientation.getValue();
191     }
192 
193     /** @return the "writing-mode" FO trait. */
getWritingMode()194     public WritingMode getWritingMode() {
195         return writingMode;
196     }
197 }
198