KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > inline > ExternalGraphicLayoutManager


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: ExternalGraphicLayoutManager.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import org.apache.fop.area.Area;
24 import org.apache.fop.area.inline.Image;
25 import org.apache.fop.fo.flow.ExternalGraphic;
26
27
28 /**
29  * LayoutManager for the fo:external-graphic formatting object
30  */

31 public class ExternalGraphicLayoutManager extends AbstractGraphicsLayoutManager {
32     
33     private ExternalGraphic fobj;
34
35     /**
36      * Constructor
37      *
38      * @param node the fo:external-graphic formatting object that creates the area
39      */

40     public ExternalGraphicLayoutManager(ExternalGraphic node) {
41         super(node);
42         fobj = node;
43     }
44
45     /**
46      * Setup this image.
47      * This gets the sizes for the image and the dimensions and clipping.
48      * @todo see if can simplify property handling logic
49      */

50     /*
51     private void setup() {
52         // assume lr-tb for now and just use the .optimum value of the range
53         Length ipd = fobj.getInlineProgressionDimension().getOptimum(this).getLength();
54         if (ipd.getEnum() != EN_AUTO) {
55             viewWidth = ipd.getValue(this);
56         } else {
57             ipd = fobj.getWidth();
58             if (ipd.getEnum() != EN_AUTO) {
59                 viewWidth = ipd.getValue(this);
60             }
61         }
62         Length bpd = fobj.getBlockProgressionDimension().getOptimum(this).getLength();
63         if (bpd.getEnum() != EN_AUTO) {
64             viewHeight = bpd.getValue(this);
65         } else {
66             bpd = fobj.getHeight();
67             if (bpd.getEnum() != EN_AUTO) {
68                 viewHeight = bpd.getValue(this);
69             }
70         }
71
72         int cwidth = -1;
73         int cheight = -1;
74         Length ch = fobj.getContentHeight();
75         if (ch.getEnum() != EN_AUTO) {
76             if (ch.getEnum() == EN_SCALE_TO_FIT) {
77                 if (viewHeight != -1) {
78                     cheight = viewHeight;
79                 }
80             } else {
81                 cheight = ch.getValue(this);
82             }
83         }
84         Length cw = fobj.getContentWidth();
85         if (cw.getEnum() != EN_AUTO) {
86             if (cw.getEnum() == EN_SCALE_TO_FIT) {
87                 if (viewWidth != -1) {
88                     cwidth = viewWidth;
89                 }
90             } else {
91                 cwidth = cw.getValue(this);
92             }
93         }
94
95         int scaling = fobj.getScaling();
96         if ((scaling == EN_UNIFORM) || (cwidth == -1) || cheight == -1) {
97             if (cwidth == -1 && cheight == -1) {
98                 cwidth = fobj.getIntrinsicWidth();
99                 cheight = fobj.getIntrinsicHeight();
100             } else if (cwidth == -1) {
101                 cwidth = (int)(fobj.getIntrinsicWidth() * (double)cheight
102                     / fobj.getIntrinsicHeight());
103             } else if (cheight == -1) {
104                 cheight = (int)(fobj.getIntrinsicHeight() * (double)cwidth
105                     / fobj.getIntrinsicWidth());
106             } else {
107                 // adjust the larger
108                 double rat1 = (double)cwidth / fobj.getIntrinsicWidth();
109                 double rat2 = (double)cheight / fobj.getIntrinsicHeight();
110                 if (rat1 < rat2) {
111                     // reduce cheight
112                     cheight = (int)(rat1 * fobj.getIntrinsicHeight());
113                 } else if (rat1 > rat2) {
114                     cwidth = (int)(rat2 * fobj.getIntrinsicWidth());
115                 }
116             }
117         }
118
119         if (viewWidth == -1) {
120             viewWidth = cwidth;
121         }
122         if (viewHeight == -1) {
123             viewHeight = cheight;
124         }
125
126         if (cwidth > viewWidth || cheight > viewHeight) {
127             int overflow = fobj.getOverflow();
128             if (overflow == EN_HIDDEN) {
129                 clip = true;
130             } else if (overflow == EN_ERROR_IF_OVERFLOW) {
131                 fobj.getLogger().error("Image: " + fobj.getURL()
132                                   + " overflows the viewport, clipping to viewport");
133                 clip = true;
134             }
135         }
136
137         int xoffset = 0;
138         int yoffset = 0;
139         switch(fobj.getDisplayAlign()) {
140             case EN_BEFORE:
141             break;
142             case EN_AFTER:
143                 yoffset = viewHeight - cheight;
144             break;
145             case EN_CENTER:
146                 yoffset = (viewHeight - cheight) / 2;
147             break;
148             case EN_AUTO:
149             default:
150             break;
151         }
152
153         switch(fobj.getTextAlign()) {
154             case EN_CENTER:
155                 xoffset = (viewWidth - cwidth) / 2;
156             break;
157             case EN_END:
158                 xoffset = viewWidth - cwidth;
159             break;
160             case EN_START:
161             break;
162             case EN_JUSTIFY:
163             default:
164             break;
165         }
166         
167         
168         CommonBorderPaddingBackground borderProps = fobj.getCommonBorderPaddingBackground();
169         
170         //Determine extra BPD from borders etc.
171         int beforeBPD = borderProps.getPadding(CommonBorderPaddingBackground.BEFORE, false, this);
172         beforeBPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.BEFORE,
173                                              false);
174         int afterBPD = borderProps.getPadding(CommonBorderPaddingBackground.AFTER, false, this);
175         afterBPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.AFTER, false);
176         
177         yoffset += beforeBPD;
178         viewBPD = viewHeight;
179         viewHeight += beforeBPD;
180         viewHeight += afterBPD;
181         
182         //Determine extra IPD from borders etc.
183         int startIPD = borderProps.getPadding(CommonBorderPaddingBackground.START,
184                 false, this);
185         startIPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.START,
186                  false);
187         int endIPD = borderProps.getPadding(CommonBorderPaddingBackground.END, false, this);
188         endIPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.END, false);
189         
190         xoffset += startIPD;
191         viewIPD = viewWidth;
192         viewWidth += startIPD;
193         viewWidth += endIPD;
194         
195         placement = new Rectangle2D.Float(xoffset, yoffset, cwidth, cheight);
196     }
197     */

198     
199     /**
200      * Get the inline area created by this element.
201      *
202      * @return the inline area
203      */

204     protected Area getChildArea() {
205         return new Image(fobj.getSrc());
206     }
207     
208 }
209
210
Popular Tags