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.

DrawSheet.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.sl.draw;
  16. import java.awt.Color;
  17. import java.awt.Dimension;
  18. import java.awt.Graphics2D;
  19. import java.awt.geom.AffineTransform;
  20. import org.apache.poi.sl.usermodel.MasterSheet;
  21. import org.apache.poi.sl.usermodel.Shape;
  22. import org.apache.poi.sl.usermodel.Sheet;
  23. public class DrawSheet implements Drawable {
  24. protected final Sheet<?,?> sheet;
  25. public DrawSheet(Sheet<?,?> sheet) {
  26. this.sheet = sheet;
  27. }
  28. @Override
  29. public void draw(Graphics2D graphics) {
  30. Dimension dim = sheet.getSlideShow().getPageSize();
  31. Color whiteTrans = new Color(1f,1f,1f,0f);
  32. graphics.setColor(whiteTrans);
  33. graphics.fillRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
  34. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  35. MasterSheet<?,?> master = sheet.getMasterSheet();
  36. if(sheet.getFollowMasterGraphics() && master != null) {
  37. Drawable drawer = drawFact.getDrawable(master);
  38. drawer.draw(graphics);
  39. }
  40. graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, new AffineTransform());
  41. for (Shape<?,?> shape : sheet.getShapes()) {
  42. if(!canDraw(graphics, shape)) {
  43. continue;
  44. }
  45. // remember the initial transform and restore it after we are done with drawing
  46. AffineTransform at = graphics.getTransform();
  47. // concrete implementations can make sense of this hint,
  48. // for example PSGraphics2D or PDFGraphics2D would call gsave() / grestore
  49. graphics.setRenderingHint(Drawable.GSAVE, true);
  50. // apply rotation and flipping
  51. Drawable drawer = drawFact.getDrawable(shape);
  52. drawer.applyTransform(graphics);
  53. // draw stuff
  54. drawer.draw(graphics);
  55. // restore the coordinate system
  56. graphics.setTransform(at);
  57. graphics.setRenderingHint(Drawable.GRESTORE, true);
  58. }
  59. }
  60. @Override
  61. public void applyTransform(Graphics2D context) {
  62. }
  63. @Override
  64. public void drawContent(Graphics2D context) {
  65. }
  66. /**
  67. * Checks if this <code>sheet</code> displays the specified shape.
  68. *
  69. * Subclasses can override it and skip certain shapes from drawings,
  70. * for instance, slide masters and layouts don't display placeholders
  71. */
  72. protected boolean canDraw(Graphics2D graphics, Shape<?,?> shape){
  73. return true;
  74. }
  75. }