View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.foRenderer.figures;
13  
14  import java.awt.geom.Rectangle2D;
15  
16  import org.apache.fop.area.Page;
17  import org.apache.fop.area.PageViewport;
18  import org.apache.fop.fo.Constants;
19  import org.eclipse.draw2d.geometry.Dimension;
20  import org.eclipse.draw2d.geometry.Insets;
21  import org.eclipse.draw2d.geometry.Rectangle;
22  
23  /***
24   * This is model class for a page, that is represented by PageFigure. 
25   * Describes page that is in millipoints(that is gathered from Area Tree) dimensions by pixel dimensions
26   * (usable in figures) 
27   * @see PageFigure
28   * 
29   * @author Tibor Vyletel
30   * Refactored by Martin Kollar on 30.1.2006
31   */
32  public class PageDescriptor {
33  
34  
35    // dimensions of BodyRegion viewArea	
36    private int contentHeight;
37    private int contentWidth;
38    
39    private int marginBottom;
40    private int marginLeft;
41    private int marginRight;
42    private int marginTop;
43  
44    private Page page;
45  
46    // dimensions of the whole page = PageViewport.viewArea
47    private int pageHeight;
48    private int pageWidth;
49  
50    private PageViewport viewport;
51    
52    /*** 
53     * @param vp Page viewport that specifies the viewport area and holds the page contents
54     */
55    public PageDescriptor(PageViewport vp) {
56      this.viewport = vp;
57      this.page = vp.getPage();
58      
59      init();
60    }
61  
62    /***
63     * @return BodyRegion rectangle of page of PageViewport in pixels
64     */
65    public Rectangle getContentArea() {
66      return new Rectangle(marginLeft, marginTop, contentWidth, contentHeight);
67    }
68    
69    /***
70     * @return Insets of BodyRegion from PageViewport.viewArea in pixels
71     */
72    public Insets getMargin() {
73      return new Insets(marginTop, marginLeft, marginBottom, marginRight);
74    }
75    
76    /***
77     * @return page of PageViewport from AreaTree
78     */
79    public Page getPage() {
80      return this.page;
81    }
82    
83    /***
84     * @return Page size in pixels
85     */
86    public Dimension getPageSize() {
87      return new Dimension(pageWidth, pageHeight);
88    }
89     
90    private void init() {
91      Rectangle2D whole = viewport.getViewArea();
92      pageWidth = FOFigureUtils.mps2pxs(whole.getWidth());
93      pageHeight = FOFigureUtils.mps2pxs(whole.getHeight());
94  
95      Rectangle2D r  = page.getRegionViewport(Constants.FO_REGION_BODY).getViewArea();
96      marginLeft = FOFigureUtils.mps2pxs(r.getX());
97      marginTop = FOFigureUtils.mps2pxs(r.getY());
98  
99      marginRight = FOFigureUtils.mps2pxs(whole.getWidth() - r.getX() - r.getWidth());
100     marginBottom = FOFigureUtils.mps2pxs(whole.getHeight() - r.getY() - r.getHeight());
101   
102     contentWidth = FOFigureUtils.mps2pxs(r.getWidth());
103     contentHeight = FOFigureUtils.mps2pxs(r.getHeight());
104     
105     // check sums:
106     int h = contentHeight + marginTop + marginBottom; 
107     if (h != pageHeight) {
108       if (marginBottom != 0)
109         marginBottom += (pageHeight - h);
110       else if (marginTop != 0)
111         marginTop += (pageHeight - h);
112     }
113     int w = contentWidth + marginLeft + marginRight; 
114     if (w != pageWidth) {
115       if (marginRight != 0)
116         marginRight += (pageWidth - w);
117       else if (marginLeft != 0)
118         marginLeft += (pageWidth - w);
119     }
120   }
121 }