KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractGraphicsLayoutManager.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 java.util.LinkedList JavaDoc;
24
25 import org.apache.fop.area.Area;
26 import org.apache.fop.area.inline.Viewport;
27 import org.apache.fop.datatypes.Length;
28 import org.apache.fop.datatypes.LengthBase;
29 import org.apache.fop.fo.FObj;
30 import org.apache.fop.fo.flow.AbstractGraphics;
31 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
32 import org.apache.fop.layoutmgr.LayoutContext;
33 import org.apache.fop.layoutmgr.TraitSetter;
34
35
36 /**
37  * LayoutManager handling the common tasks for the fo:instream-foreign-object
38  * and fo:external-graphics formatting objects
39  */

40 public abstract class AbstractGraphicsLayoutManager extends LeafNodeLayoutManager {
41     
42     /** The graphics object this LM deals with */
43     protected AbstractGraphics fobj;
44     
45     /**
46      * Constructor
47      * @param node the formatting object that creates this area
48      */

49     public AbstractGraphicsLayoutManager(AbstractGraphics node) {
50         super(node);
51         fobj = node;
52     }
53
54     /**
55      * Get the inline area created by this element.
56      *
57      * @return the viewport inline area
58      */

59     private Viewport getInlineArea() {
60
61         // viewport size is determined by block-progression-dimension
62
// and inline-progression-dimension
63

64         // if replaced then use height then ignore block-progression-dimension
65
//int h = this.propertyList.get("height").getLength().mvalue();
66

67         // use specified line-height then ignore dimension in height direction
68
boolean hasLH = false; //propertyList.get("line-height").getSpecifiedValue() != null;
69

70         Length len;
71
72         int bpd = -1;
73         int ipd = -1;
74         if (hasLH) {
75             bpd = fobj.getLineHeight().getOptimum(this).getLength().getValue(this);
76         } else {
77             // this property does not apply when the line-height applies
78
// isn't the block-progression-dimension always in the same
79
// direction as the line height?
80
len = fobj.getBlockProgressionDimension().getOptimum(this).getLength();
81             if (len.getEnum() != EN_AUTO) {
82                 bpd = len.getValue(this);
83             } else {
84                 len = fobj.getHeight();
85                 if (len.getEnum() != EN_AUTO) {
86                     bpd = len.getValue(this);
87                 }
88             }
89         }
90
91         len = fobj.getInlineProgressionDimension().getOptimum(this).getLength();
92         if (len.getEnum() != EN_AUTO) {
93             ipd = len.getValue(this);
94         } else {
95             len = fobj.getWidth();
96             if (len.getEnum() != EN_AUTO) {
97                 ipd = len.getValue(this);
98             }
99         }
100
101         // if auto then use the intrinsic size of the content scaled
102
// to the content-height and content-width
103
int cwidth = -1;
104         int cheight = -1;
105         len = fobj.getContentWidth();
106         if (len.getEnum() != EN_AUTO) {
107             if (len.getEnum() == EN_SCALE_TO_FIT) {
108                 if (ipd != -1) {
109                     cwidth = ipd;
110                 }
111             } else {
112                 cwidth = len.getValue(this);
113             }
114         }
115         len = fobj.getContentHeight();
116         if (len.getEnum() != EN_AUTO) {
117             if (len.getEnum() == EN_SCALE_TO_FIT) {
118                 if (bpd != -1) {
119                     cheight = bpd;
120                 }
121             } else {
122                 cheight = len.getValue(this);
123             }
124         }
125
126         int scaling = fobj.getScaling();
127         if ((scaling == EN_UNIFORM) || (cwidth == -1) || cheight == -1) {
128             if (cwidth == -1 && cheight == -1) {
129                 cwidth = fobj.getIntrinsicWidth();
130                 cheight = fobj.getIntrinsicHeight();
131             } else if (cwidth == -1) {
132                 if (fobj.getIntrinsicHeight() == 0) {
133                     cwidth = 0;
134                 } else {
135                     cwidth = (int)(fobj.getIntrinsicWidth() * (double)cheight
136                             / fobj.getIntrinsicHeight());
137                 }
138             } else if (cheight == -1) {
139                 if (fobj.getIntrinsicWidth() == 0) {
140                     cheight = 0;
141                 } else {
142                     cheight = (int)(fobj.getIntrinsicHeight() * (double)cwidth
143                             / fobj.getIntrinsicWidth());
144                 }
145             } else {
146                 // adjust the larger
147
if (fobj.getIntrinsicWidth() == 0 || fobj.getIntrinsicHeight() == 0) {
148                     cwidth = 0;
149                     cheight = 0;
150                 } else {
151                     double rat1 = (double) cwidth / fobj.getIntrinsicWidth();
152                     double rat2 = (double) cheight / fobj.getIntrinsicHeight();
153                     if (rat1 < rat2) {
154                         // reduce cheight
155
cheight = (int)(rat1 * fobj.getIntrinsicHeight());
156                     } else if (rat1 > rat2) {
157                         cwidth = (int)(rat2 * fobj.getIntrinsicWidth());
158                     }
159                 }
160             }
161         }
162
163         if (ipd == -1) {
164             ipd = cwidth;
165         }
166         if (bpd == -1) {
167             bpd = cheight;
168         }
169
170         boolean clip = false;
171         if (cwidth > ipd || cheight > bpd) {
172             int overflow = fobj.getOverflow();
173             if (overflow == EN_HIDDEN) {
174                 clip = true;
175             } else if (overflow == EN_ERROR_IF_OVERFLOW) {
176                 fobj.getLogger().error("Object overflows the viewport: clipping");
177                 clip = true;
178             }
179         }
180
181         int xoffset = fobj.computeXOffset(ipd, cwidth);
182         int yoffset = fobj.computeYOffset(bpd, cheight);
183
184         CommonBorderPaddingBackground borderProps = fobj.getCommonBorderPaddingBackground();
185         
186         //Determine extra BPD from borders etc.
187
int beforeBPD = borderProps.getPadding(CommonBorderPaddingBackground.BEFORE, false, this);
188         beforeBPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.BEFORE,
189                                              false);
190         int afterBPD = borderProps.getPadding(CommonBorderPaddingBackground.AFTER, false, this);
191         afterBPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.AFTER, false);
192         
193         yoffset += beforeBPD;
194         //bpd += beforeBPD;
195
//bpd += afterBPD;
196

197         //Determine extra IPD from borders etc.
198
int startIPD = borderProps.getPadding(CommonBorderPaddingBackground.START,
199                 false, this);
200         startIPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.START,
201                  false);
202         int endIPD = borderProps.getPadding(CommonBorderPaddingBackground.END, false, this);
203         endIPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.END, false);
204         
205         xoffset += startIPD;
206         //ipd += startIPD;
207
//ipd += endIPD;
208

209         Rectangle2D JavaDoc placement = new Rectangle2D.Float JavaDoc(xoffset, yoffset, cwidth, cheight);
210
211         Area viewportArea = getChildArea();
212         TraitSetter.setProducerID(viewportArea, fobj.getId());
213         transferForeignAttributes(viewportArea);
214
215         Viewport vp = new Viewport(viewportArea);
216         TraitSetter.setProducerID(vp, fobj.getId());
217         vp.setIPD(ipd);
218         vp.setBPD(bpd);
219         vp.setContentPosition(placement);
220         vp.setClip(clip);
221         vp.setOffset(0);
222
223         // Common Border, Padding, and Background Properties
224
TraitSetter.addBorders(vp, fobj.getCommonBorderPaddingBackground()
225                                 , false, false, false, false, this);
226         TraitSetter.addPadding(vp, fobj.getCommonBorderPaddingBackground()
227                                 , false, false, false, false, this);
228         TraitSetter.addBackground(vp, fobj.getCommonBorderPaddingBackground(), this);
229
230         return vp;
231     }
232     
233     /**
234      * @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int)
235      */

236     public LinkedList JavaDoc getNextKnuthElements(LayoutContext context,
237                                            int alignment) {
238         Viewport areaCurrent = getInlineArea();
239         setCurrentArea(areaCurrent);
240         return super.getNextKnuthElements(context, alignment);
241     }
242     
243     /**
244      * @see LeafNodeLayoutManager#makeAlignmentContext(LayoutContext)
245      */

246     protected AlignmentContext makeAlignmentContext(LayoutContext context) {
247         return new AlignmentContext(
248                 get(context).getAllocBPD()
249                 , fobj.getAlignmentAdjust()
250                 , fobj.getAlignmentBaseline()
251                 , fobj.getBaselineShift()
252                 , fobj.getDominantBaseline()
253                 , context.getAlignmentContext()
254             );
255     }
256
257     /**
258      * @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#addId()
259      */

260     protected void addId() {
261         getPSLM().addIDToPage(fobj.getId());
262     }
263
264     /**
265      * Returns the image of foreign object area to be put into
266      * the viewport.
267      * @return the appropriate area
268      */

269     abstract Area getChildArea();
270     
271     // --------- Property Resolution related functions --------- //
272

273     /**
274      * @see org.apache.fop.datatypes.PercentBaseContext#getBaseLength(int, FObj)
275      */

276     public int getBaseLength(int lengthBase, FObj fobj) {
277         switch (lengthBase) {
278         case LengthBase.IMAGE_INTRINSIC_WIDTH:
279             return getIntrinsicWidth();
280         case LengthBase.IMAGE_INTRINSIC_HEIGHT:
281             return getIntrinsicHeight();
282         case LengthBase.ALIGNMENT_ADJUST:
283             return get(null).getBPD();
284         default: // Delegate to super class
285
return super.getBaseLength(lengthBase, fobj);
286         }
287     }
288
289     /**
290      * Returns the intrinsic width of the e-g.
291      * @return the width of the element
292      */

293     protected int getIntrinsicWidth() {
294         return fobj.getIntrinsicWidth();
295     }
296
297     /**
298      * Returns the intrinsic height of the e-g.
299      * @return the height of the element
300      */

301     protected int getIntrinsicHeight() {
302         return fobj.getIntrinsicHeight();
303     }
304
305 }
306
307
Popular Tags