1 17 18 19 20 package org.apache.fop.layoutmgr.inline; 21 22 import java.awt.geom.Rectangle2D ; 23 import java.util.LinkedList ; 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 40 public abstract class AbstractGraphicsLayoutManager extends LeafNodeLayoutManager { 41 42 43 protected AbstractGraphics fobj; 44 45 49 public AbstractGraphicsLayoutManager(AbstractGraphics node) { 50 super(node); 51 fobj = node; 52 } 53 54 59 private Viewport getInlineArea() { 60 61 64 67 boolean hasLH = false; 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 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 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 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 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 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 197 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 209 Rectangle2D placement = new Rectangle2D.Float (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 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 236 public LinkedList getNextKnuthElements(LayoutContext context, 237 int alignment) { 238 Viewport areaCurrent = getInlineArea(); 239 setCurrentArea(areaCurrent); 240 return super.getNextKnuthElements(context, alignment); 241 } 242 243 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 260 protected void addId() { 261 getPSLM().addIDToPage(fobj.getId()); 262 } 263 264 269 abstract Area getChildArea(); 270 271 273 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: return super.getBaseLength(lengthBase, fobj); 286 } 287 } 288 289 293 protected int getIntrinsicWidth() { 294 return fobj.getIntrinsicWidth(); 295 } 296 297 301 protected int getIntrinsicHeight() { 302 return fobj.getIntrinsicHeight(); 303 } 304 305 } 306 307 | Popular Tags |