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.

ImageLayout.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr.inline;
  19. import java.awt.Dimension;
  20. import java.awt.Rectangle;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.datatypes.Length;
  24. import org.apache.fop.datatypes.PercentBaseContext;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.GraphicsProperties;
  27. import org.apache.fop.fo.properties.LengthRangeProperty;
  28. /**
  29. * Helper class which calculates the size and position in the viewport of an image.
  30. */
  31. public class ImageLayout implements Constants {
  32. /** logging instance */
  33. protected static final Log log = LogFactory.getLog(ImageLayout.class);
  34. //Input
  35. private GraphicsProperties props;
  36. private PercentBaseContext percentBaseContext;
  37. private Dimension intrinsicSize;
  38. //Output
  39. private Rectangle placement;
  40. private Dimension viewportSize = new Dimension(-1, -1);
  41. private boolean clip;
  42. /**
  43. * Main constructor
  44. * @param props the properties for the image
  45. * @param percentBaseContext the context object for percentage calculations
  46. * @param intrinsicSize the image's intrinsic size
  47. */
  48. public ImageLayout(GraphicsProperties props, PercentBaseContext percentBaseContext,
  49. Dimension intrinsicSize) {
  50. this.props = props;
  51. this.percentBaseContext = percentBaseContext;
  52. this.intrinsicSize = intrinsicSize;
  53. doLayout();
  54. }
  55. /**
  56. * Does the actual calculations for the image.
  57. */
  58. protected void doLayout() {
  59. Length len;
  60. int bpd = -1;
  61. int ipd = -1;
  62. len = props.getBlockProgressionDimension().getOptimum(percentBaseContext).getLength();
  63. if (len.getEnum() != EN_AUTO) {
  64. bpd = len.getValue(percentBaseContext);
  65. }
  66. len = props.getBlockProgressionDimension().getMinimum(percentBaseContext).getLength();
  67. if (bpd == -1 && len.getEnum() != EN_AUTO) {
  68. //Establish minimum viewport size
  69. bpd = len.getValue(percentBaseContext);
  70. }
  71. len = props.getInlineProgressionDimension().getOptimum(percentBaseContext).getLength();
  72. if (len.getEnum() != EN_AUTO) {
  73. ipd = len.getValue(percentBaseContext);
  74. }
  75. len = props.getInlineProgressionDimension().getMinimum(percentBaseContext).getLength();
  76. if (ipd == -1 && len.getEnum() != EN_AUTO) {
  77. //Establish minimum viewport size
  78. ipd = len.getValue(percentBaseContext);
  79. }
  80. // if auto then use the intrinsic size of the content scaled
  81. // to the content-height and content-width
  82. boolean constrainIntrinsicSize = false;
  83. int cwidth = -1;
  84. int cheight = -1;
  85. len = props.getContentWidth();
  86. if (len.getEnum() != EN_AUTO) {
  87. switch (len.getEnum()) {
  88. case EN_SCALE_TO_FIT:
  89. if (ipd != -1) {
  90. cwidth = ipd;
  91. }
  92. constrainIntrinsicSize = true;
  93. break;
  94. case EN_SCALE_DOWN_TO_FIT:
  95. if (ipd != -1 && intrinsicSize.width > ipd) {
  96. cwidth = ipd;
  97. }
  98. constrainIntrinsicSize = true;
  99. break;
  100. case EN_SCALE_UP_TO_FIT:
  101. if (ipd != -1 && intrinsicSize.width < ipd) {
  102. cwidth = ipd;
  103. }
  104. constrainIntrinsicSize = true;
  105. break;
  106. default:
  107. cwidth = len.getValue(percentBaseContext);
  108. }
  109. }
  110. len = props.getContentHeight();
  111. if (len.getEnum() != EN_AUTO) {
  112. switch (len.getEnum()) {
  113. case EN_SCALE_TO_FIT:
  114. if (bpd != -1) {
  115. cheight = bpd;
  116. }
  117. constrainIntrinsicSize = true;
  118. break;
  119. case EN_SCALE_DOWN_TO_FIT:
  120. if (bpd != -1 && intrinsicSize.height > bpd) {
  121. cheight = bpd;
  122. }
  123. constrainIntrinsicSize = true;
  124. break;
  125. case EN_SCALE_UP_TO_FIT:
  126. if (bpd != -1 && intrinsicSize.height < bpd) {
  127. cheight = bpd;
  128. }
  129. constrainIntrinsicSize = true;
  130. break;
  131. default:
  132. cheight = len.getValue(percentBaseContext);
  133. }
  134. }
  135. Dimension constrainedIntrinsicSize;
  136. if (constrainIntrinsicSize) {
  137. constrainedIntrinsicSize = constrain(intrinsicSize);
  138. } else {
  139. constrainedIntrinsicSize = intrinsicSize;
  140. }
  141. //Derive content extents where not explicit
  142. Dimension adjustedDim = adjustContentSize(cwidth, cheight, constrainedIntrinsicSize);
  143. cwidth = adjustedDim.width;
  144. cheight = adjustedDim.height;
  145. //Adjust viewport if not explicit
  146. if (ipd == -1) {
  147. ipd = constrainExtent(cwidth,
  148. props.getInlineProgressionDimension(), props.getContentWidth());
  149. }
  150. if (bpd == -1) {
  151. bpd = constrainExtent(cheight,
  152. props.getBlockProgressionDimension(), props.getContentHeight());
  153. }
  154. this.clip = false;
  155. int overflow = props.getOverflow();
  156. if (overflow == EN_HIDDEN) {
  157. this.clip = true;
  158. } else if (overflow == EN_ERROR_IF_OVERFLOW) {
  159. if (cwidth > ipd || cheight > bpd) {
  160. //TODO Don't use logging to report error!
  161. log.error("Object overflows the viewport: clipping");
  162. }
  163. this.clip = true;
  164. }
  165. int xoffset = computeXOffset(ipd, cwidth);
  166. int yoffset = computeYOffset(bpd, cheight);
  167. //Build calculation results
  168. this.viewportSize.setSize(ipd, bpd);
  169. this.placement = new Rectangle(xoffset, yoffset, cwidth, cheight);
  170. }
  171. private int constrainExtent(int extent, LengthRangeProperty range, Length contextExtent) {
  172. boolean mayScaleUp = (contextExtent.getEnum() != EN_SCALE_DOWN_TO_FIT);
  173. boolean mayScaleDown = (contextExtent.getEnum() != EN_SCALE_UP_TO_FIT);
  174. Length len;
  175. len = range.getMaximum(percentBaseContext).getLength();
  176. if (len.getEnum() != EN_AUTO) {
  177. int max = len.getValue(percentBaseContext);
  178. if (max != -1 && mayScaleDown) {
  179. extent = Math.min(extent, max);
  180. }
  181. }
  182. len = range.getMinimum(percentBaseContext).getLength();
  183. if (len.getEnum() != EN_AUTO) {
  184. int min = len.getValue(percentBaseContext);
  185. if (min != -1 && mayScaleUp) {
  186. extent = Math.max(extent, min);
  187. }
  188. }
  189. return extent;
  190. }
  191. private Dimension constrain(Dimension size) {
  192. Dimension adjusted = new Dimension(size);
  193. int effWidth = constrainExtent(size.width,
  194. props.getInlineProgressionDimension(), props.getContentWidth());
  195. int effHeight = constrainExtent(size.height,
  196. props.getBlockProgressionDimension(), props.getContentHeight());
  197. int scaling = props.getScaling();
  198. if (scaling == EN_UNIFORM) {
  199. double rat1 = (double)effWidth / size.width;
  200. double rat2 = (double)effHeight / size.height;
  201. if (rat1 < rat2) {
  202. adjusted.width = effWidth;
  203. adjusted.height = (int)(rat1 * size.height);
  204. } else if (rat1 > rat2) {
  205. adjusted.width = (int)(rat2 * size.width);
  206. adjusted.height = effHeight;
  207. }
  208. } else {
  209. adjusted.width = effWidth;
  210. adjusted.height = effHeight;
  211. }
  212. return adjusted;
  213. }
  214. private Dimension adjustContentSize(
  215. final int cwidth, final int cheight,
  216. Dimension defaultSize) {
  217. Dimension dim = new Dimension(cwidth, cheight);
  218. int scaling = props.getScaling();
  219. if ((scaling == EN_UNIFORM) || (cwidth == -1) || cheight == -1) {
  220. if (cwidth == -1 && cheight == -1) {
  221. dim.width = defaultSize.width;
  222. dim.height = defaultSize.height;
  223. } else if (cwidth == -1) {
  224. if (defaultSize.height == 0) {
  225. dim.width = 0;
  226. } else {
  227. dim.width = (int)(defaultSize.width * (double)cheight
  228. / defaultSize.height);
  229. }
  230. } else if (cheight == -1) {
  231. if (defaultSize.width == 0) {
  232. dim.height = 0;
  233. } else {
  234. dim.height = (int)(defaultSize.height * (double)cwidth
  235. / defaultSize.width);
  236. }
  237. } else {
  238. // adjust the larger
  239. if (defaultSize.width == 0 || defaultSize.height == 0) {
  240. dim.width = 0;
  241. dim.height = 0;
  242. } else {
  243. double rat1 = (double)cwidth / defaultSize.width;
  244. double rat2 = (double)cheight / defaultSize.height;
  245. if (rat1 < rat2) {
  246. // reduce height
  247. dim.height = (int)(rat1 * defaultSize.height);
  248. } else if (rat1 > rat2) {
  249. dim.width = (int)(rat2 * defaultSize.width);
  250. }
  251. }
  252. }
  253. }
  254. return dim;
  255. }
  256. /**
  257. * Given the ipd and the content width calculates the
  258. * required x offset based on the text-align property
  259. * @param ipd the inline-progression-dimension of the object
  260. * @param cwidth the calculated content width of the object
  261. * @return the X offset
  262. */
  263. public int computeXOffset (int ipd, int cwidth) {
  264. int xoffset = 0;
  265. switch (props.getTextAlign()) {
  266. case EN_CENTER:
  267. xoffset = (ipd - cwidth) / 2;
  268. break;
  269. case EN_END:
  270. xoffset = ipd - cwidth;
  271. break;
  272. case EN_START:
  273. break;
  274. case EN_JUSTIFY:
  275. default:
  276. break;
  277. }
  278. return xoffset;
  279. }
  280. /**
  281. * Given the bpd and the content height calculates the
  282. * required y offset based on the display-align property
  283. * @param bpd the block-progression-dimension of the object
  284. * @param cheight the calculated content height of the object
  285. * @return the Y offset
  286. */
  287. public int computeYOffset(int bpd, int cheight) {
  288. int yoffset = 0;
  289. switch (props.getDisplayAlign()) {
  290. case EN_BEFORE:
  291. break;
  292. case EN_AFTER:
  293. yoffset = bpd - cheight;
  294. break;
  295. case EN_CENTER:
  296. yoffset = (bpd - cheight) / 2;
  297. break;
  298. case EN_AUTO:
  299. default:
  300. break;
  301. }
  302. return yoffset;
  303. }
  304. /**
  305. * Returns the placement of the image inside the viewport.
  306. * @return the placement of the image inside the viewport (coordinates in millipoints)
  307. */
  308. public Rectangle getPlacement() {
  309. return this.placement;
  310. }
  311. /**
  312. * Returns the size of the image's viewport.
  313. * @return the viewport size (in millipoints)
  314. */
  315. public Dimension getViewportSize() {
  316. return this.viewportSize;
  317. }
  318. /**
  319. * Returns the size of the image's intrinsic (natural) size.
  320. * @return the intrinsic size (in millipoints)
  321. */
  322. public Dimension getIntrinsicSize() {
  323. return this.intrinsicSize;
  324. }
  325. /**
  326. * Indicates whether the image is clipped.
  327. * @return true if the image shall be clipped
  328. */
  329. public boolean isClipped() {
  330. return this.clip;
  331. }
  332. }