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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * $Id: ExternalGraphic.java,v 1.32 2003/03/05 20:38:21 jeremias Exp $
  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.FONode;
  54. import org.apache.fop.fo.FObj;
  55. import org.apache.fop.fo.FOTreeVisitor;
  56. import org.apache.fop.fo.properties.CommonBorderAndPadding;
  57. import org.apache.fop.fo.properties.CommonBackground;
  58. import org.apache.fop.fo.properties.TextAlign;
  59. import org.apache.fop.fo.properties.Overflow;
  60. import org.apache.fop.fo.properties.DisplayAlign;
  61. import org.apache.fop.fo.properties.Scaling;
  62. import org.apache.fop.image.ImageFactory;
  63. import org.apache.fop.image.FopImage;
  64. import org.apache.fop.area.inline.InlineArea;
  65. import org.apache.fop.layoutmgr.TraitSetter;
  66. import org.apache.fop.area.inline.Image;
  67. import org.apache.fop.area.inline.Viewport;
  68. import org.apache.fop.datatypes.Length;
  69. // Java
  70. import java.awt.geom.Rectangle2D;
  71. import org.apache.fop.apps.*;
  72. /**
  73. * External graphic formatting object.
  74. * This FO node handles the external graphic. It creates an image
  75. * inline area that can be added to the area tree.
  76. */
  77. public class ExternalGraphic extends FObj {
  78. private String url;
  79. private int breakAfter;
  80. private int breakBefore;
  81. private int align;
  82. private int startIndent;
  83. private int endIndent;
  84. private int spaceBefore;
  85. private int spaceAfter;
  86. private int viewWidth = -1;
  87. private int viewHeight = -1;
  88. private boolean clip = false;
  89. private Rectangle2D placement = null;
  90. /**
  91. * Create a new External graphic node.
  92. *
  93. * @param parent the parent of this node
  94. */
  95. public ExternalGraphic(FONode parent) {
  96. super(parent);
  97. }
  98. /**
  99. * Get the inline area for this external grpahic.
  100. * This creates the image area and puts it inside a viewport.
  101. *
  102. * @return the viewport containing the image area
  103. */
  104. public InlineArea getInlineArea() {
  105. setup();
  106. if (url == null) {
  107. return null;
  108. }
  109. Image imArea = new Image(url);
  110. Viewport vp = new Viewport(imArea);
  111. vp.setWidth(viewWidth);
  112. vp.setHeight(viewHeight);
  113. vp.setClip(clip);
  114. vp.setContentPosition(placement);
  115. vp.setOffset(0);
  116. // Common Border, Padding, and Background Properties
  117. CommonBorderAndPadding bap = propMgr.getBorderAndPadding();
  118. CommonBackground bProps = propMgr.getBackgroundProps();
  119. TraitSetter.addBorders(vp, bap);
  120. TraitSetter.addBackground(vp, bProps);
  121. return vp;
  122. }
  123. /**
  124. * Setup this image.
  125. * This gets the sizes for the image and the dimensions and clipping.
  126. */
  127. public void setup() {
  128. url = this.properties.get("src").getString();
  129. if (url == null) {
  130. return;
  131. }
  132. url = ImageFactory.getURL(url);
  133. // assume lr-tb for now
  134. Length ipd = properties.get("inline-progression-dimension.optimum").getLength();
  135. if (!ipd.isAuto()) {
  136. viewWidth = ipd.getValue();
  137. } else {
  138. ipd = properties.get("width").getLength();
  139. if (!ipd.isAuto()) {
  140. viewWidth = ipd.getValue();
  141. }
  142. }
  143. Length bpd = properties.get("block-progression-dimension.optimum").getLength();
  144. if (!bpd.isAuto()) {
  145. viewHeight = bpd.getValue();
  146. } else {
  147. bpd = properties.get("height").getLength();
  148. if (!bpd.isAuto()) {
  149. viewHeight = bpd.getValue();
  150. }
  151. }
  152. // if we need to load this image to get its size
  153. FopImage fopimage = null;
  154. int cwidth = -1;
  155. int cheight = -1;
  156. Length ch = properties.get("content-height").getLength();
  157. if (!ch.isAuto()) {
  158. /*if (ch.scaleToFit()) {
  159. if (viewHeight != -1) {
  160. cheight = viewHeight;
  161. }
  162. } else {*/
  163. cheight = ch.getValue();
  164. }
  165. Length cw = properties.get("content-width").getLength();
  166. if (!cw.isAuto()) {
  167. /*if (cw.scaleToFit()) {
  168. if (viewWidth != -1) {
  169. cwidth = viewWidth;
  170. }
  171. } else {*/
  172. cwidth = cw.getValue();
  173. }
  174. int scaling = properties.get("scaling").getEnum();
  175. if ((scaling == Scaling.UNIFORM) || (cwidth == -1) || cheight == -1) {
  176. ImageFactory fact = ImageFactory.getInstance();
  177. fopimage = fact.getImage(url, userAgent);
  178. if (fopimage == null) {
  179. // error
  180. url = null;
  181. return;
  182. }
  183. // load dimensions
  184. if (!fopimage.load(FopImage.DIMENSIONS, userAgent)) {
  185. // error
  186. url = null;
  187. return;
  188. }
  189. if (cwidth == -1) {
  190. cwidth = (int)(fopimage.getWidth() * 1000);
  191. }
  192. if (cheight == -1) {
  193. cheight = (int)(fopimage.getHeight() * 1000);
  194. }
  195. if (scaling == Scaling.UNIFORM) {
  196. // adjust the larger
  197. double rat1 = cwidth / (fopimage.getWidth() * 1000f);
  198. double rat2 = cheight / (fopimage.getHeight() * 1000f);
  199. if (rat1 < rat2) {
  200. // reduce cheight
  201. cheight = (int)(rat1 * fopimage.getHeight() * 1000);
  202. } else {
  203. cwidth = (int)(rat2 * fopimage.getWidth() * 1000);
  204. }
  205. }
  206. }
  207. if (viewWidth == -1) {
  208. viewWidth = cwidth;
  209. }
  210. if (viewHeight == -1) {
  211. viewHeight = cheight;
  212. }
  213. if (cwidth > viewWidth || cheight > viewHeight) {
  214. int overflow = properties.get("overflow").getEnum();
  215. if (overflow == Overflow.HIDDEN) {
  216. clip = true;
  217. } else if (overflow == Overflow.ERROR_IF_OVERFLOW) {
  218. getLogger().error("Image: " + url
  219. + " overflows the viewport, clipping to viewport");
  220. clip = true;
  221. }
  222. }
  223. int xoffset = 0;
  224. int yoffset = 0;
  225. int da = properties.get("display-align").getEnum();
  226. switch(da) {
  227. case DisplayAlign.BEFORE:
  228. break;
  229. case DisplayAlign.AFTER:
  230. yoffset = viewHeight - cheight;
  231. break;
  232. case DisplayAlign.CENTER:
  233. yoffset = (viewHeight - cheight) / 2;
  234. break;
  235. case DisplayAlign.AUTO:
  236. default:
  237. break;
  238. }
  239. int ta = properties.get("text-align").getEnum();
  240. switch(ta) {
  241. case TextAlign.CENTER:
  242. xoffset = (viewWidth - cwidth) / 2;
  243. break;
  244. case TextAlign.END:
  245. xoffset = viewWidth - cwidth;
  246. break;
  247. case TextAlign.START:
  248. break;
  249. case TextAlign.JUSTIFY:
  250. default:
  251. break;
  252. }
  253. placement = new Rectangle2D.Float(xoffset, yoffset, cwidth, cheight);
  254. }
  255. /**
  256. * @return the ViewHeight (in millipoints??)
  257. */
  258. public int getViewHeight() {
  259. return viewHeight;
  260. }
  261. /**
  262. * This is a hook for an FOTreeVisitor subclass to be able to access
  263. * this object.
  264. * @param fotv the FOTreeVisitor subclass that can access this object.
  265. * @see org.apache.fop.fo.FOTreeVisitor
  266. */
  267. public void acceptVisitor(FOTreeVisitor fotv) {
  268. fotv.serveVisitor(this);
  269. }
  270. }