Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExternalGraphic.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.*;
  54. import org.apache.fop.fo.properties.*;
  55. import org.apache.fop.layout.*;
  56. import org.apache.fop.apps.FOPException;
  57. import org.apache.fop.image.*;
  58. // Java
  59. import java.net.MalformedURLException;
  60. public class ExternalGraphic extends FObj {
  61. int breakAfter;
  62. int breakBefore;
  63. int align;
  64. int startIndent;
  65. int endIndent;
  66. int spaceBefore;
  67. int spaceAfter;
  68. String src;
  69. int height;
  70. int width;
  71. String id;
  72. ImageArea imageArea;
  73. public static class Maker extends FObj.Maker {
  74. public FObj make(FObj parent, PropertyList propertyList,
  75. String systemId, int line, int column)
  76. throws FOPException {
  77. return new ExternalGraphic(parent, propertyList,
  78. systemId, line, column);
  79. }
  80. }
  81. public static FObj.Maker maker() {
  82. return new ExternalGraphic.Maker();
  83. }
  84. public ExternalGraphic(FObj parent, PropertyList propertyList,
  85. String systemId, int line, int column) {
  86. super(parent, propertyList, systemId, line, column);
  87. }
  88. public String getName() {
  89. return "fo:external-graphic";
  90. }
  91. public int layout(Area area) throws FOPException {
  92. if (this.marker == START) {
  93. // Common Accessibility Properties
  94. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  95. // Common Aural Properties
  96. AuralProps mAurProps = propMgr.getAuralProps();
  97. // Common Border, Padding, and Background Properties
  98. BorderAndPadding bap = propMgr.getBorderAndPadding();
  99. BackgroundProps bProps = propMgr.getBackgroundProps();
  100. // Common Margin Properties-Inline
  101. MarginInlineProps mProps = propMgr.getMarginInlineProps();
  102. // Common Relative Position Properties
  103. RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
  104. // this.properties.get("alignment-adjust");
  105. // this.properties.get("alignment-baseline");
  106. // this.properties.get("baseline-shift");
  107. // this.properties.get("block-progression-dimension");
  108. // this.properties.get("content-height");
  109. // this.properties.get("content-type");
  110. // this.properties.get("content-width");
  111. // this.properties.get("display-align");
  112. // this.properties.get("dominant-baseline");
  113. // this.properties.get("height");
  114. // this.properties.get("id");
  115. // this.properties.get("inline-progression-dimension");
  116. // this.properties.get("keep-with-next");
  117. // this.properties.get("keep-with-previous");
  118. // this.properties.get("line-height");
  119. // this.properties.get("line-height-shift-adjustment");
  120. // this.properties.get("overflow");
  121. // this.properties.get("scaling");
  122. // this.properties.get("scaling-method");
  123. // this.properties.get("src");
  124. // this.properties.get("text-align");
  125. // this.properties.get("width");
  126. // FIXME
  127. this.align = this.properties.get("text-align").getEnum();
  128. this.startIndent =
  129. this.properties.get("start-indent").getLength().mvalue();
  130. this.endIndent =
  131. this.properties.get("end-indent").getLength().mvalue();
  132. this.spaceBefore =
  133. this.properties.get("space-before.optimum").getLength().mvalue();
  134. this.spaceAfter =
  135. this.properties.get("space-after.optimum").getLength().mvalue();
  136. this.src = this.properties.get("src").getString();
  137. this.width = this.properties.get("width").getLength().mvalue();
  138. this.height = this.properties.get("height").getLength().mvalue();
  139. this.id = this.properties.get("id").getString();
  140. try {
  141. area.getIDReferences().createID(id);
  142. }
  143. catch(FOPException e) {
  144. if (!e.isLocationSet()) {
  145. e.setLocation(systemId, line, column);
  146. }
  147. throw e;
  148. }
  149. /*
  150. * if (area instanceof BlockArea) {
  151. * area.end();
  152. * }
  153. * if (this.isInTableCell) {
  154. * startIndent += forcedStartOffset;
  155. * endIndent = area.getAllocationWidth() - forcedWidth -
  156. * forcedStartOffset;
  157. * }
  158. */
  159. this.marker = 0;
  160. }
  161. try {
  162. FopImage img = FopImageFactory.Make(src);
  163. // if width / height needs to be computed
  164. if ((width == 0) || (height == 0)) {
  165. // aspect ratio
  166. double imgWidth = img.getWidth();
  167. double imgHeight = img.getHeight();
  168. if ((width == 0) && (height == 0)) {
  169. width = (int)((imgWidth * 1000d));
  170. height = (int)((imgHeight * 1000d));
  171. } else if (height == 0) {
  172. height = (int)((imgHeight * ((double)width)) / imgWidth);
  173. } else if (width == 0) {
  174. width = (int)((imgWidth * ((double)height)) / imgHeight);
  175. }
  176. }
  177. // scale image if it doesn't fit in the area/page
  178. // Need to be more tested...
  179. double ratio = ((double)width) / ((double)height);
  180. int areaWidth = area.getAllocationWidth() - startIndent
  181. - endIndent;
  182. int pageHeight = area.getPage().getBody().getMaxHeight()
  183. - spaceBefore;
  184. if (height > pageHeight) {
  185. height = pageHeight;
  186. width = (int)(ratio * ((double)height));
  187. }
  188. if (width > areaWidth) {
  189. width = areaWidth;
  190. height = (int)(((double)width) / ratio);
  191. }
  192. if (area.spaceLeft() < (height + spaceBefore)) {
  193. return Status.AREA_FULL_NONE;
  194. }
  195. this.imageArea =
  196. new ImageArea(propMgr.getFontState(area.getFontInfo()), img,
  197. area.getAllocationWidth(), width, height,
  198. startIndent, endIndent, align);
  199. if ((spaceBefore != 0) && (this.marker == 0)) {
  200. area.addDisplaySpace(spaceBefore);
  201. }
  202. if (marker == 0) {
  203. // configure id
  204. area.getIDReferences().configureID(id, area);
  205. }
  206. imageArea.start();
  207. imageArea.end();
  208. // area.addChild(imageArea);
  209. // area.increaseHeight(imageArea.getHeight());
  210. if (spaceAfter != 0) {
  211. area.addDisplaySpace(spaceAfter);
  212. }
  213. if (breakBefore == BreakBefore.PAGE
  214. || ((spaceBefore + imageArea.getHeight())
  215. > area.spaceLeft())) {
  216. return Status.FORCE_PAGE_BREAK;
  217. }
  218. if (breakBefore == BreakBefore.ODD_PAGE) {
  219. return Status.FORCE_PAGE_BREAK_ODD;
  220. }
  221. if (breakBefore == BreakBefore.EVEN_PAGE) {
  222. return Status.FORCE_PAGE_BREAK_EVEN;
  223. }
  224. if (area instanceof BlockArea) {
  225. BlockArea ba = (BlockArea)area;
  226. LineArea la = ba.getCurrentLineArea();
  227. if (la == null) {
  228. return Status.AREA_FULL_NONE;
  229. }
  230. la.addPending();
  231. if (imageArea.getContentWidth() > la.getRemainingWidth()) {
  232. la = ba.createNextLineArea();
  233. if (la == null) {
  234. return Status.AREA_FULL_NONE;
  235. }
  236. }
  237. la.addInlineArea(imageArea, this.getLinkSet());
  238. } else {
  239. area.addChild(imageArea);
  240. area.increaseHeight(imageArea.getContentHeight());
  241. }
  242. imageArea.setPage(area.getPage());
  243. if (breakAfter == BreakAfter.PAGE) {
  244. this.marker = BREAK_AFTER;
  245. return Status.FORCE_PAGE_BREAK;
  246. }
  247. if (breakAfter == BreakAfter.ODD_PAGE) {
  248. this.marker = BREAK_AFTER;
  249. return Status.FORCE_PAGE_BREAK_ODD;
  250. }
  251. if (breakAfter == BreakAfter.EVEN_PAGE) {
  252. this.marker = BREAK_AFTER;
  253. return Status.FORCE_PAGE_BREAK_EVEN;
  254. }
  255. } catch (MalformedURLException urlex) {
  256. // bad URL
  257. log.error("Error while creating area : "
  258. + urlex.getMessage());
  259. } catch (FopImageException imgex) {
  260. // image error
  261. log.error("Error while creating area : "
  262. + imgex.getMessage());
  263. }
  264. // if (area instanceof BlockArea) {
  265. // area.start();
  266. // }
  267. return Status.OK;
  268. }
  269. }