1 51 package org.apache.fop.fo.flow; 52 53 import org.apache.fop.fo.*; 55 import org.apache.fop.fo.properties.*; 56 import org.apache.fop.layout.*; 57 import org.apache.fop.apps.FOPException; 58 import org.apache.fop.image.*; 59 60 import java.net.MalformedURLException ; 62 63 public class ExternalGraphic extends FObj { 64 65 int breakAfter; 66 int breakBefore; 67 int align; 68 int startIndent; 69 int endIndent; 70 int spaceBefore; 71 int spaceAfter; 72 String src; 73 int height; 74 int width; 75 String id; 76 77 ImageArea imageArea; 78 79 public static class Maker extends FObj.Maker { 80 public FObj make(FObj parent, PropertyList propertyList, 81 String systemId, int line, int column) 82 throws FOPException { 83 return new ExternalGraphic(parent, propertyList, 84 systemId, line, column); 85 } 86 } 87 88 public static FObj.Maker maker() { 89 return new ExternalGraphic.Maker(); 90 } 91 92 public ExternalGraphic(FObj parent, PropertyList propertyList, 93 String systemId, int line, int column) { 94 super(parent, propertyList, systemId, line, column); 95 } 96 97 public String getName() { 98 return "fo:external-graphic"; 99 } 100 101 public int layout(Area area) throws FOPException { 102 103 if (this.marker == START) { 104 105 AccessibilityProps mAccProps = propMgr.getAccessibilityProps(); 107 108 AuralProps mAurProps = propMgr.getAuralProps(); 110 111 BorderAndPadding bap = propMgr.getBorderAndPadding(); 113 BackgroundProps bProps = propMgr.getBackgroundProps(); 114 115 MarginInlineProps mProps = propMgr.getMarginInlineProps(); 117 118 RelativePositionProps mRelProps = propMgr.getRelativePositionProps(); 120 121 144 this.align = this.properties.get("text-align").getEnum(); 146 147 this.startIndent = 148 this.properties.get("start-indent").getLength().mvalue(); 149 this.endIndent = 150 this.properties.get("end-indent").getLength().mvalue(); 151 152 this.spaceBefore = 153 this.properties.get("space-before.optimum").getLength().mvalue(); 154 this.spaceAfter = 155 this.properties.get("space-after.optimum").getLength().mvalue(); 156 157 this.src = this.properties.get("src").getString(); 158 159 this.width = this.properties.get("width").getLength().mvalue(); 160 161 this.height = this.properties.get("height").getLength().mvalue(); 162 163 this.id = this.properties.get("id").getString(); 164 165 try { 166 area.getIDReferences().createID(id); 167 } 168 catch(FOPException e) { 169 if (!e.isLocationSet()) { 170 e.setLocation(systemId, line, column); 171 } 172 throw e; 173 } 174 184 this.marker = 0; 185 } 186 187 try { 188 FopImage img = FopImageFactory.Make(src); 189 if ((width == 0) || (height == 0)) { 191 double imgWidth = img.getWidth(); 193 double imgHeight = img.getHeight(); 194 if ((width == 0) && (height == 0)) { 195 width = (int)((imgWidth * 1000d)); 196 height = (int)((imgHeight * 1000d)); 197 } else if (height == 0) { 198 height = (int)((imgHeight * ((double)width)) / imgWidth); 199 } else if (width == 0) { 200 width = (int)((imgWidth * ((double)height)) / imgHeight); 201 } 202 } 203 204 double ratio = ((double)width) / ((double)height); 207 int areaWidth = area.getAllocationWidth() - startIndent 208 - endIndent; 209 int pageHeight = area.getPage().getBody().getMaxHeight() 210 - spaceBefore; 211 if (height > pageHeight) { 212 height = pageHeight; 213 width = (int)(ratio * ((double)height)); 214 } 215 if (width > areaWidth) { 216 width = areaWidth; 217 height = (int)(((double)width) / ratio); 218 } 219 220 if (area.spaceLeft() < (height + spaceBefore)) { 221 return Status.AREA_FULL_NONE; 222 } 223 224 this.imageArea = 225 new ImageArea(propMgr.getFontState(area.getFontInfo()), img, 226 area.getAllocationWidth(), width, height, 227 startIndent, endIndent, align); 228 229 if ((spaceBefore != 0) && (this.marker == 0)) { 230 area.addDisplaySpace(spaceBefore); 231 } 232 233 if (marker == 0) { 234 area.getIDReferences().configureID(id, area); 236 } 237 238 imageArea.start(); 239 imageArea.end(); 240 243 if (spaceAfter != 0) { 244 area.addDisplaySpace(spaceAfter); 245 } 246 if (breakBefore == BreakBefore.PAGE 247 || ((spaceBefore + imageArea.getHeight()) 248 > area.spaceLeft())) { 249 return Status.FORCE_PAGE_BREAK; 250 } 251 252 if (breakBefore == BreakBefore.ODD_PAGE) { 253 return Status.FORCE_PAGE_BREAK_ODD; 254 } 255 256 if (breakBefore == BreakBefore.EVEN_PAGE) { 257 return Status.FORCE_PAGE_BREAK_EVEN; 258 } 259 260 261 if (area instanceof BlockArea) { 262 BlockArea ba = (BlockArea)area; 263 LineArea la = ba.getCurrentLineArea(); 264 if (la == null) { 265 return Status.AREA_FULL_NONE; 266 } 267 la.addPending(); 268 if (imageArea.getContentWidth() > la.getRemainingWidth()) { 269 la = ba.createNextLineArea(); 270 if (la == null) { 271 return Status.AREA_FULL_NONE; 272 } 273 } 274 la.addInlineArea(imageArea, this.getLinkSet()); 275 } else { 276 area.addChild(imageArea); 277 area.increaseHeight(imageArea.getContentHeight()); 278 } 279 imageArea.setPage(area.getPage()); 280 281 if (breakAfter == BreakAfter.PAGE) { 282 this.marker = BREAK_AFTER; 283 return Status.FORCE_PAGE_BREAK; 284 } 285 286 if (breakAfter == BreakAfter.ODD_PAGE) { 287 this.marker = BREAK_AFTER; 288 return Status.FORCE_PAGE_BREAK_ODD; 289 } 290 291 if (breakAfter == BreakAfter.EVEN_PAGE) { 292 this.marker = BREAK_AFTER; 293 return Status.FORCE_PAGE_BREAK_EVEN; 294 } 295 296 } catch (MalformedURLException urlex) { 297 log.error("Error while creating area : " 299 + urlex.getMessage()); 300 } catch (FopImageException imgex) { 301 log.error("Error while creating area : " 303 + imgex.getMessage()); 304 } 305 306 310 return Status.OK; 311 } 312 313 } 314 315 | Popular Tags |