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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo.flow;
  8. // FOP
  9. import org.apache.fop.fo.*;
  10. import org.apache.fop.fo.properties.*;
  11. import org.apache.fop.layout.*;
  12. import org.apache.fop.apps.FOPException;
  13. import org.apache.fop.image.*;
  14. import org.apache.fop.area.inline.InlineArea;
  15. import org.apache.fop.layoutmgr.LayoutManager;
  16. import org.apache.fop.layoutmgr.LeafNodeLayoutManager;
  17. import org.apache.fop.layoutmgr.LayoutInfo;
  18. import org.apache.fop.area.inline.Image;
  19. import org.apache.fop.area.inline.Viewport;
  20. import org.apache.fop.datatypes.*;
  21. // Java
  22. import java.net.URL;
  23. import java.net.MalformedURLException;
  24. import java.util.List;
  25. import java.awt.geom.Rectangle2D;
  26. public class ExternalGraphic extends FObj {
  27. String url;
  28. int breakAfter;
  29. int breakBefore;
  30. int align;
  31. int startIndent;
  32. int endIndent;
  33. int spaceBefore;
  34. int spaceAfter;
  35. int viewWidth = -1;
  36. int viewHeight = -1;
  37. boolean clip = false;
  38. Rectangle2D placement = null;
  39. public ExternalGraphic(FONode parent) {
  40. super(parent);
  41. }
  42. public void addLayoutManager(List list) {
  43. LeafNodeLayoutManager lm = new LeafNodeLayoutManager(this);
  44. lm.setCurrentArea(getInlineArea());
  45. list.add(lm);
  46. }
  47. protected InlineArea getInlineArea() {
  48. setup();
  49. if(url == null) {
  50. return null;
  51. }
  52. Image imArea = new Image(url);
  53. Viewport vp = new Viewport(imArea);
  54. vp.setWidth(viewWidth);
  55. vp.setHeight(viewHeight);
  56. vp.setClip(clip);
  57. vp.setContentPosition(placement);
  58. vp.setOffset(0);
  59. vp.info = new LayoutInfo();
  60. vp.info.alignment = properties.get("vertical-align").getEnum();
  61. vp.info.lead = vp.getHeight();
  62. return vp;
  63. }
  64. public void setup() {
  65. url = this.properties.get("src").getString();
  66. if(url == null) {
  67. return;
  68. }
  69. url = ImageFactory.getURL(url);
  70. // assume lr-tb for now
  71. Length ipd = properties.get("inline-progression-dimension.optimum").getLength();
  72. if(!ipd.isAuto()) {
  73. viewWidth = ipd.mvalue();
  74. } else {
  75. ipd = properties.get("width").getLength();
  76. if(!ipd.isAuto()) {
  77. viewWidth = ipd.mvalue();
  78. }
  79. }
  80. Length bpd = properties.get("block-progression-dimension.optimum").getLength();
  81. if(!bpd.isAuto()) {
  82. viewHeight = bpd.mvalue();
  83. } else {
  84. bpd = properties.get("height").getLength();
  85. if(!bpd.isAuto()) {
  86. viewHeight = bpd.mvalue();
  87. }
  88. }
  89. // if we need to load this image to get its size
  90. FopImage fopimage = null;
  91. int cwidth = -1;
  92. int cheight = -1;
  93. Length ch = properties.get("content-height").getLength();
  94. if(!ch.isAuto()) {
  95. /*if(ch.scaleToFit()) {
  96. if(viewHeight != -1) {
  97. cheight = viewHeight;
  98. }
  99. } else {*/
  100. cheight = ch.mvalue();
  101. }
  102. Length cw = properties.get("content-width").getLength();
  103. if(!cw.isAuto()) {
  104. /*if(cw.scaleToFit()) {
  105. if(viewWidth != -1) {
  106. cwidth = viewWidth;
  107. }
  108. } else {*/
  109. cwidth = cw.mvalue();
  110. }
  111. int scaling = properties.get("scaling").getEnum();
  112. if((scaling == Scaling.UNIFORM) || (cwidth == -1) || cheight == -1) {
  113. ImageFactory fact = ImageFactory.getInstance();
  114. fopimage = fact.getImage(url, userAgent);
  115. if(fopimage == null) {
  116. // error
  117. url = null;
  118. return;
  119. }
  120. // load dimensions
  121. if(!fopimage.load(FopImage.DIMENSIONS, userAgent)) {
  122. // error
  123. url = null;
  124. return;
  125. }
  126. if(cwidth == -1) {
  127. cwidth = (int)(fopimage.getWidth() * 1000);
  128. }
  129. if(cheight == -1) {
  130. cheight = (int)(fopimage.getHeight() * 1000);
  131. }
  132. if(scaling == Scaling.UNIFORM) {
  133. // adjust the larger
  134. double rat1 = cwidth / (fopimage.getWidth() * 1000f);
  135. double rat2 = cheight / (fopimage.getHeight() * 1000f);
  136. if(rat1 < rat2) {
  137. // reduce cheight
  138. cheight = (int)(rat1 * fopimage.getHeight() * 1000);
  139. } else {
  140. cwidth = (int)(rat2 * fopimage.getWidth() * 1000);
  141. }
  142. }
  143. }
  144. if(viewWidth == -1) {
  145. viewWidth = cwidth;
  146. }
  147. if(viewHeight == -1) {
  148. viewHeight = cheight;
  149. }
  150. if(cwidth > viewWidth || cheight > viewHeight) {
  151. int overflow = properties.get("overflow").getEnum();
  152. if(overflow == Overflow.HIDDEN) {
  153. clip = true;
  154. } else if(overflow == Overflow.ERROR_IF_OVERFLOW) {
  155. getLogger().error("Image: " + url + " overflows the viewport");
  156. clip = true;
  157. }
  158. }
  159. int xoffset = 0;
  160. int yoffset = 0;
  161. int da = properties.get("display-align").getEnum();
  162. switch(da) {
  163. case DisplayAlign.BEFORE:
  164. break;
  165. case DisplayAlign.AFTER:
  166. yoffset = viewHeight - cheight;
  167. break;
  168. case DisplayAlign.CENTER:
  169. yoffset = (viewHeight - cheight) / 2;
  170. break;
  171. case DisplayAlign.AUTO:
  172. default:
  173. break;
  174. }
  175. int ta = properties.get("text-align").getEnum();
  176. switch(ta) {
  177. case TextAlign.CENTER:
  178. xoffset = (viewWidth - cwidth) / 2;
  179. break;
  180. case TextAlign.END:
  181. xoffset = viewWidth - cwidth;
  182. break;
  183. case TextAlign.START:
  184. break;
  185. case TextAlign.JUSTIFY:
  186. default:
  187. break;
  188. }
  189. placement = new Rectangle2D.Float(xoffset, yoffset, cwidth, cheight);
  190. // Common Accessibility Properties
  191. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  192. // Common Aural Properties
  193. AuralProps mAurProps = propMgr.getAuralProps();
  194. // Common Border, Padding, and Background Properties
  195. BorderAndPadding bap = propMgr.getBorderAndPadding();
  196. BackgroundProps bProps = propMgr.getBackgroundProps();
  197. // Common Margin Properties-Inline
  198. MarginInlineProps mProps = propMgr.getMarginInlineProps();
  199. // Common Relative Position Properties
  200. RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
  201. // this.properties.get("alignment-adjust");
  202. // this.properties.get("alignment-baseline");
  203. // this.properties.get("baseline-shift");
  204. // this.properties.get("content-type");
  205. // this.properties.get("dominant-baseline");
  206. setupID();
  207. // this.properties.get("keep-with-next");
  208. // this.properties.get("keep-with-previous");
  209. // this.properties.get("line-height");
  210. // this.properties.get("line-height-shift-adjustment");
  211. // this.properties.get("scaling-method");
  212. }
  213. }