Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TestDrawPictureShape.java 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.sl.draw;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertNotNull;
  22. import java.awt.Dimension;
  23. import java.awt.geom.Rectangle2D;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.sl.usermodel.PictureData;
  26. import org.apache.poi.sl.usermodel.PictureShape;
  27. import org.apache.poi.sl.usermodel.RectAlign;
  28. import org.apache.poi.sl.usermodel.Shape;
  29. import org.apache.poi.sl.usermodel.Slide;
  30. import org.apache.poi.sl.usermodel.SlideShow;
  31. import org.apache.poi.sl.usermodel.SlideShowFactory;
  32. import org.apache.poi.util.Units;
  33. import org.junit.Test;
  34. public class TestDrawPictureShape {
  35. final static POIDataSamples ssSamples = POIDataSamples.getSlideShowInstance();
  36. @Test
  37. public void testResize() throws Exception {
  38. String files[] = { "pictures.ppt", "shapes.pptx" };
  39. for (String file : files) {
  40. SlideShow<?,?> ss = SlideShowFactory.create(ssSamples.getFile(file));
  41. Slide<?,?> slide = ss.getSlides().get(0);
  42. PictureShape<?,?> picShape = null;
  43. for (Shape<?,?> shape : slide.getShapes()) {
  44. if (shape instanceof PictureShape) {
  45. picShape = (PictureShape<?,?>)shape;
  46. break;
  47. }
  48. }
  49. assertNotNull(picShape);
  50. PictureData pd = picShape.getPictureData();
  51. Dimension dimPd = pd.getImageDimension();
  52. new DrawPictureShape(picShape).resize();
  53. Dimension dimShape = new Dimension(
  54. (int)picShape.getAnchor().getWidth(),
  55. (int)picShape.getAnchor().getHeight()
  56. );
  57. assertEquals(dimPd, dimShape);
  58. double newWidth = (dimPd.getWidth()*(100d/dimPd.getHeight()));
  59. // ... -1 is a rounding error
  60. Rectangle2D expRect = new Rectangle2D.Double(rbf(50+300-newWidth, picShape), 50, rbf(newWidth, picShape), 100);
  61. Rectangle2D target = new Rectangle2D.Double(50,50,300,100);
  62. new DrawPictureShape(picShape).resize(target, RectAlign.BOTTOM_RIGHT);
  63. Rectangle2D actRect = picShape.getAnchor();
  64. assertEquals(expRect.getX(), actRect.getX(), .0001);
  65. assertEquals(expRect.getY(), actRect.getY(), .0001);
  66. assertEquals(expRect.getWidth(), actRect.getWidth(), .0001);
  67. assertEquals(expRect.getHeight(), actRect.getHeight(), .0001);
  68. ss.close();
  69. }
  70. }
  71. // round back and forth - points -> master -> points
  72. static double rbf(double val, PictureShape<?,?> picShape) {
  73. if (picShape.getClass().getName().contains("HSLF")) {
  74. return Units.masterToPoints(Units.pointsToMaster(val));
  75. } else {
  76. return val;
  77. }
  78. }
  79. }