1
2
3
4
5
6
7
8
9
10
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
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
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
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 }