You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExternalGraphic.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo.flow;
  52. // FOP
  53. import org.apache.fop.fo.FObj;
  54. import org.apache.fop.fo.PropertyList;
  55. import org.apache.fop.fo.Status;
  56. import org.apache.fop.fo.properties.BreakBefore;
  57. import org.apache.fop.fo.properties.BreakAfter;
  58. import org.apache.fop.layout.Area;
  59. import org.apache.fop.layout.AccessibilityProps;
  60. import org.apache.fop.layout.AuralProps;
  61. import org.apache.fop.layout.BorderAndPadding;
  62. import org.apache.fop.layout.BackgroundProps;
  63. import org.apache.fop.layout.MarginInlineProps;
  64. import org.apache.fop.layout.RelativePositionProps;
  65. import org.apache.fop.layout.BlockArea;
  66. import org.apache.fop.layout.LineArea;
  67. import org.apache.fop.apps.FOPException;
  68. import org.apache.fop.image.ImageArea;
  69. import org.apache.fop.image.FopImageException;
  70. import org.apache.fop.image.FopImage;
  71. import org.apache.fop.image.FopImageFactory;
  72. // Java
  73. import java.net.MalformedURLException;
  74. public class ExternalGraphic extends FObj {
  75. int breakAfter;
  76. int breakBefore;
  77. int align;
  78. int startIndent;
  79. int endIndent;
  80. int spaceBefore;
  81. int spaceAfter;
  82. String src;
  83. int height;
  84. int width;
  85. String id;
  86. ImageArea imageArea;
  87. public static class Maker extends FObj.Maker {
  88. public FObj make(FObj parent, PropertyList propertyList,
  89. String systemId, int line, int column)
  90. throws FOPException {
  91. return new ExternalGraphic(parent, propertyList,
  92. systemId, line, column);
  93. }
  94. }
  95. public static FObj.Maker maker() {
  96. return new ExternalGraphic.Maker();
  97. }
  98. public ExternalGraphic(FObj parent, PropertyList propertyList,
  99. String systemId, int line, int column) {
  100. super(parent, propertyList, systemId, line, column);
  101. }
  102. public String getName() {
  103. return "fo:external-graphic";
  104. }
  105. public int layout(Area area) throws FOPException {
  106. if (this.marker == START) {
  107. // Common Accessibility Properties
  108. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  109. // Common Aural Properties
  110. AuralProps mAurProps = propMgr.getAuralProps();
  111. // Common Border, Padding, and Background Properties
  112. BorderAndPadding bap = propMgr.getBorderAndPadding();
  113. BackgroundProps bProps = propMgr.getBackgroundProps();
  114. // Common Margin Properties-Inline
  115. MarginInlineProps mProps = propMgr.getMarginInlineProps();
  116. // Common Relative Position Properties
  117. RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
  118. // this.properties.get("alignment-adjust");
  119. // this.properties.get("alignment-baseline");
  120. // this.properties.get("baseline-shift");
  121. // this.properties.get("block-progression-dimension");
  122. // this.properties.get("content-height");
  123. // this.properties.get("content-type");
  124. // this.properties.get("content-width");
  125. // this.properties.get("display-align");
  126. // this.properties.get("dominant-baseline");
  127. // this.properties.get("height");
  128. // this.properties.get("id");
  129. // this.properties.get("inline-progression-dimension");
  130. // this.properties.get("keep-with-next");
  131. // this.properties.get("keep-with-previous");
  132. // this.properties.get("line-height");
  133. // this.properties.get("line-height-shift-adjustment");
  134. // this.properties.get("overflow");
  135. // this.properties.get("scaling");
  136. // this.properties.get("scaling-method");
  137. // this.properties.get("src");
  138. // this.properties.get("text-align");
  139. // this.properties.get("width");
  140. // FIXME
  141. this.align = this.properties.get("text-align").getEnum();
  142. this.startIndent =
  143. this.properties.get("start-indent").getLength().mvalue();
  144. this.endIndent =
  145. this.properties.get("end-indent").getLength().mvalue();
  146. this.spaceBefore =
  147. this.properties.get("space-before.optimum").getLength().mvalue();
  148. this.spaceAfter =
  149. this.properties.get("space-after.optimum").getLength().mvalue();
  150. this.src = this.properties.get("src").getString();
  151. this.width = this.properties.get("width").getLength().mvalue();
  152. this.height = this.properties.get("height").getLength().mvalue();
  153. this.id = this.properties.get("id").getString();
  154. try {
  155. area.getIDReferences().createID(id);
  156. }
  157. catch(FOPException e) {
  158. if (!e.isLocationSet()) {
  159. e.setLocation(systemId, line, column);
  160. }
  161. throw e;
  162. }
  163. /*
  164. * if (area instanceof BlockArea) {
  165. * area.end();
  166. * }
  167. * if (this.isInTableCell) {
  168. * startIndent += forcedStartOffset;
  169. * endIndent = area.getAllocationWidth() - forcedWidth -
  170. * forcedStartOffset;
  171. * }
  172. */
  173. this.marker = 0;
  174. }
  175. try {
  176. FopImage img = FopImageFactory.Make(src);
  177. // if width / height needs to be computed
  178. if ((width == 0) || (height == 0)) {
  179. // aspect ratio
  180. double imgWidth = img.getWidth();
  181. double imgHeight = img.getHeight();
  182. if ((width == 0) && (height == 0)) {
  183. width = (int)((imgWidth * 1000d));
  184. height = (int)((imgHeight * 1000d));
  185. } else if (height == 0) {
  186. height = (int)((imgHeight * ((double)width)) / imgWidth);
  187. } else if (width == 0) {
  188. width = (int)((imgWidth * ((double)height)) / imgHeight);
  189. }
  190. }
  191. // scale image if it doesn't fit in the area/page
  192. // Need to be more tested...
  193. double ratio = ((double)width) / ((double)height);
  194. int areaWidth = area.getAllocationWidth() - startIndent
  195. - endIndent;
  196. int pageHeight = area.getPage().getBody().getMaxHeight()
  197. - spaceBefore;
  198. if (height > pageHeight) {
  199. height = pageHeight;
  200. width = (int)(ratio * ((double)height));
  201. }
  202. if (width > areaWidth) {
  203. width = areaWidth;
  204. height = (int)(((double)width) / ratio);
  205. }
  206. if (area.spaceLeft() < (height + spaceBefore)) {
  207. return Status.AREA_FULL_NONE;
  208. }
  209. this.imageArea =
  210. new ImageArea(propMgr.getFontState(area.getFontInfo()), img,
  211. area.getAllocationWidth(), width, height,
  212. startIndent, endIndent, align);
  213. if ((spaceBefore != 0) && (this.marker == 0)) {
  214. area.addDisplaySpace(spaceBefore);
  215. }
  216. if (marker == 0) {
  217. // configure id
  218. area.getIDReferences().configureID(id, area);
  219. }
  220. imageArea.start();
  221. imageArea.end();
  222. // area.addChild(imageArea);
  223. // area.increaseHeight(imageArea.getHeight());
  224. if (spaceAfter != 0) {
  225. area.addDisplaySpace(spaceAfter);
  226. }
  227. if (breakBefore == BreakBefore.PAGE
  228. || ((spaceBefore + imageArea.getHeight())
  229. > area.spaceLeft())) {
  230. return Status.FORCE_PAGE_BREAK;
  231. }
  232. if (breakBefore == BreakBefore.ODD_PAGE) {
  233. return Status.FORCE_PAGE_BREAK_ODD;
  234. }
  235. if (breakBefore == BreakBefore.EVEN_PAGE) {
  236. return Status.FORCE_PAGE_BREAK_EVEN;
  237. }
  238. if (area instanceof BlockArea) {
  239. BlockArea ba = (BlockArea)area;
  240. LineArea la = ba.getCurrentLineArea();
  241. if (la == null) {
  242. return Status.AREA_FULL_NONE;
  243. }
  244. la.addPending();
  245. if (imageArea.getContentWidth() > la.getRemainingWidth()) {
  246. la = ba.createNextLineArea();
  247. if (la == null) {
  248. return Status.AREA_FULL_NONE;
  249. }
  250. }
  251. la.addInlineArea(imageArea, this.getLinkSet());
  252. } else {
  253. area.addChild(imageArea);
  254. area.increaseHeight(imageArea.getContentHeight());
  255. }
  256. imageArea.setPage(area.getPage());
  257. if (breakAfter == BreakAfter.PAGE) {
  258. this.marker = BREAK_AFTER;
  259. return Status.FORCE_PAGE_BREAK;
  260. }
  261. if (breakAfter == BreakAfter.ODD_PAGE) {
  262. this.marker = BREAK_AFTER;
  263. return Status.FORCE_PAGE_BREAK_ODD;
  264. }
  265. if (breakAfter == BreakAfter.EVEN_PAGE) {
  266. this.marker = BREAK_AFTER;
  267. return Status.FORCE_PAGE_BREAK_EVEN;
  268. }
  269. } catch (MalformedURLException urlex) {
  270. // bad URL
  271. log.error("Error while creating area : "
  272. + urlex.getMessage());
  273. } catch (FopImageException imgex) {
  274. // image error
  275. log.error("Error while creating area : "
  276. + imgex.getMessage());
  277. }
  278. // if (area instanceof BlockArea) {
  279. // area.start();
  280. // }
  281. return Status.OK;
  282. }
  283. }