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.

XSLFTexturePaint.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.usermodel;
  16. import java.awt.geom.Dimension2D;
  17. import java.awt.geom.Point2D;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.function.Supplier;
  23. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  24. import org.apache.poi.openxml4j.opc.PackagePart;
  25. import org.apache.poi.openxml4j.opc.PackageRelationship;
  26. import org.apache.poi.sl.usermodel.ColorStyle;
  27. import org.apache.poi.sl.usermodel.Insets2D;
  28. import org.apache.poi.sl.usermodel.PaintStyle;
  29. import org.apache.poi.util.Dimension2DDouble;
  30. import org.apache.poi.util.Internal;
  31. import org.apache.poi.util.Units;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTDuotoneEffect;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTTileInfoProperties;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.STTileFlipMode;
  39. @Internal
  40. public class XSLFTexturePaint implements PaintStyle.TexturePaint {
  41. private final CTBlipFillProperties blipFill;
  42. private final PackagePart parentPart;
  43. private final CTBlip blip;
  44. private final CTSchemeColor phClr;
  45. private final XSLFTheme theme;
  46. private final XSLFSheet sheet;
  47. public XSLFTexturePaint(final CTBlipFillProperties blipFill, final PackagePart parentPart, CTSchemeColor phClr, final XSLFTheme theme, final XSLFSheet sheet) {
  48. this.blipFill = blipFill;
  49. this.parentPart = parentPart;
  50. blip = blipFill.getBlip();
  51. this.phClr = phClr;
  52. this.theme = theme;
  53. this.sheet = sheet;
  54. }
  55. private PackagePart getPart() {
  56. try {
  57. String blipId = blip.getEmbed();
  58. PackageRelationship rel = parentPart.getRelationship(blipId);
  59. return parentPart.getRelatedPart(rel);
  60. } catch (InvalidFormatException e) {
  61. throw new RuntimeException(e);
  62. }
  63. }
  64. @Override
  65. public InputStream getImageData() {
  66. try {
  67. return getPart().getInputStream();
  68. } catch (IOException e) {
  69. throw new RuntimeException(e);
  70. }
  71. }
  72. @Override
  73. public String getContentType() {
  74. if (blip == null || !blip.isSetEmbed() || blip.getEmbed().isEmpty()) {
  75. return null;
  76. }
  77. /* TOOD: map content-type */
  78. return getPart().getContentType();
  79. }
  80. @Override
  81. public int getAlpha() {
  82. return (blip.sizeOfAlphaModFixArray() > 0)
  83. ? blip.getAlphaModFixArray(0).getAmt()
  84. : 100000;
  85. }
  86. @Override
  87. public boolean isRotatedWithShape() {
  88. return blipFill.isSetRotWithShape() && blipFill.getRotWithShape();
  89. }
  90. @Override
  91. public Dimension2D getScale() {
  92. CTTileInfoProperties tile = blipFill.getTile();
  93. return (tile == null) ? null : new Dimension2DDouble(
  94. tile.isSetSx() ? tile.getSx()/100_000. : 1,
  95. tile.isSetSy() ? tile.getSy()/100_000. : 1);
  96. }
  97. @Override
  98. public Point2D getOffset() {
  99. CTTileInfoProperties tile = blipFill.getTile();
  100. return (tile == null) ? null : new Point2D.Double(
  101. tile.isSetTx() ? Units.toPoints(tile.getTx()) : 0,
  102. tile.isSetTy() ? Units.toPoints(tile.getTy()) : 0);
  103. }
  104. @Override
  105. public PaintStyle.FlipMode getFlipMode() {
  106. CTTileInfoProperties tile = blipFill.getTile();
  107. switch (tile == null || tile.getFlip() == null ? STTileFlipMode.INT_NONE : tile.getFlip().intValue()) {
  108. default:
  109. case STTileFlipMode.INT_NONE:
  110. return PaintStyle.FlipMode.NONE;
  111. case STTileFlipMode.INT_X:
  112. return PaintStyle.FlipMode.X;
  113. case STTileFlipMode.INT_Y:
  114. return PaintStyle.FlipMode.Y;
  115. case STTileFlipMode.INT_XY:
  116. return PaintStyle.FlipMode.XY;
  117. }
  118. }
  119. @Override
  120. public PaintStyle.TextureAlignment getAlignment() {
  121. CTTileInfoProperties tile = blipFill.getTile();
  122. return (tile == null || !tile.isSetAlgn()) ? null
  123. : PaintStyle.TextureAlignment.fromOoxmlId(tile.getAlgn().toString());
  124. }
  125. @Override
  126. public Insets2D getInsets() {
  127. return getRectVal(blipFill.getSrcRect());
  128. }
  129. @Override
  130. public Insets2D getStretch() {
  131. return getRectVal(blipFill.isSetStretch() ? blipFill.getStretch().getFillRect() : null);
  132. }
  133. @Override
  134. public List<ColorStyle> getDuoTone() {
  135. if (blip.sizeOfDuotoneArray() == 0) {
  136. return null;
  137. }
  138. List<ColorStyle> colors = new ArrayList<>();
  139. CTDuotoneEffect duoEff = blip.getDuotoneArray(0);
  140. for (CTSchemeColor phClrDuo : duoEff.getSchemeClrArray()) {
  141. colors.add(new XSLFColor(phClrDuo, theme, phClr, sheet).getColorStyle());
  142. }
  143. return colors;
  144. }
  145. private static Insets2D getRectVal(CTRelativeRect rect) {
  146. return rect == null ? null : new Insets2D(
  147. getRectVal(rect::isSetT, rect::getT),
  148. getRectVal(rect::isSetL, rect::getL),
  149. getRectVal(rect::isSetB, rect::getB),
  150. getRectVal(rect::isSetR, rect::getR)
  151. );
  152. }
  153. private static int getRectVal(Supplier<Boolean> isSet, Supplier<Integer> val) {
  154. return isSet.get() ? val.get() : 0;
  155. }
  156. }