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.

XSLFBackground.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 org.apache.xmlbeans.XmlObject;
  17. import org.openxmlformats.schemas.drawingml.x2006.main.CTBackgroundFillStyleList;
  18. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  19. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  20. import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrixReference;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  22. import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
  23. import java.awt.Color;
  24. import java.awt.Dimension;
  25. import java.awt.Graphics2D;
  26. import java.awt.Paint;
  27. import java.awt.geom.Rectangle2D;
  28. /**
  29. * Background shape
  30. *
  31. * @author Yegor Kozlov
  32. */
  33. public class XSLFBackground extends XSLFSimpleShape {
  34. /* package */XSLFBackground(CTBackground shape, XSLFSheet sheet) {
  35. super(shape, sheet);
  36. }
  37. @Override
  38. public Rectangle2D getAnchor(){
  39. Dimension pg = getSheet().getSlideShow().getPageSize();
  40. return new Rectangle2D.Double(0, 0, pg.getWidth(), pg.getHeight());
  41. }
  42. public void draw(Graphics2D graphics) {
  43. Rectangle2D anchor = getAnchor();
  44. Paint fill = getPaint(graphics);
  45. if(fill != null) {
  46. graphics.setPaint(fill);
  47. graphics.fill(anchor);
  48. }
  49. }
  50. /**
  51. * @return the Paint object to fill
  52. */
  53. Paint getPaint(Graphics2D graphics){
  54. RenderableShape rShape = new RenderableShape(this);
  55. Paint fill = null;
  56. CTBackground bg = (CTBackground)getXmlObject();
  57. if(bg.isSetBgPr()){
  58. XmlObject spPr = bg.getBgPr();
  59. fill = rShape.getPaint(graphics, spPr, null);
  60. } else if (bg.isSetBgRef()){
  61. CTStyleMatrixReference bgRef= bg.getBgRef();
  62. CTSchemeColor phClr = bgRef.getSchemeClr();
  63. int idx = (int)bgRef.getIdx() - 1001;
  64. XSLFTheme theme = getSheet().getTheme();
  65. CTBackgroundFillStyleList bgStyles =
  66. theme.getXmlObject().getThemeElements().getFmtScheme().getBgFillStyleLst();
  67. XmlObject bgStyle = bgStyles.selectPath("*")[idx];
  68. fill = rShape.selectPaint(graphics, bgStyle, phClr, theme.getPackagePart());
  69. }
  70. return fill;
  71. }
  72. @Override
  73. public Color getFillColor(){
  74. Paint p = getPaint(null);
  75. if(p instanceof Color){
  76. return (Color)p;
  77. }
  78. return null;
  79. }
  80. /**
  81. * background does not have a associated transform.
  82. * we return a dummy transform object to prevent exceptions in inherited methods.
  83. *
  84. * @return dummy CTTransform2D bean
  85. */
  86. @Override
  87. CTTransform2D getXfrm() {
  88. return CTTransform2D.Factory.newInstance();
  89. }
  90. }