選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

XSLFAutoShape.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.xslf.usermodel;
  20. import org.apache.poi.util.Beta;
  21. import org.apache.poi.util.Units;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.CTGeomGuide;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTGeomGuideList;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBodyProperties;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.STTextWrappingType;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.STTextVerticalType;
  36. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  37. import org.openxmlformats.schemas.presentationml.x2006.main.CTShapeNonVisual;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
  40. import java.awt.*;
  41. import java.awt.geom.AffineTransform;
  42. import java.awt.geom.Rectangle2D;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. import java.util.regex.Pattern;
  46. import java.util.regex.Matcher;
  47. /**
  48. * Represents a preset geometric shape.
  49. *
  50. * @author Yegor Kozlov
  51. */
  52. @Beta
  53. public class XSLFAutoShape extends XSLFTextShape {
  54. private static final Pattern adjPtrn = Pattern.compile("val\\s+(\\d+)");
  55. /*package*/ XSLFAutoShape(CTShape shape, XSLFSheet sheet) {
  56. super(shape, sheet);
  57. }
  58. /*package*/
  59. static XSLFAutoShape create(CTShape shape, XSLFSheet sheet) {
  60. if (shape.getSpPr().isSetCustGeom()) {
  61. return new XSLFFreeformShape(shape, sheet);
  62. } else if (shape.getNvSpPr().getCNvSpPr().isSetTxBox()) {
  63. return new XSLFTextBox(shape, sheet);
  64. } else {
  65. return new XSLFAutoShape(shape, sheet);
  66. }
  67. }
  68. /**
  69. * @param shapeId 1-based shapeId
  70. */
  71. static CTShape prototype(int shapeId) {
  72. CTShape ct = CTShape.Factory.newInstance();
  73. CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
  74. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  75. cnv.setName("AutoShape " + shapeId);
  76. cnv.setId(shapeId + 1);
  77. nvSpPr.addNewCNvSpPr();
  78. nvSpPr.addNewNvPr();
  79. CTShapeProperties spPr = ct.addNewSpPr();
  80. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  81. prst.setPrst(STShapeType.RECT);
  82. prst.addNewAvLst();
  83. return ct;
  84. }
  85. protected CTTextBody getTextBody(boolean create){
  86. CTShape shape = (CTShape) getXmlObject();
  87. CTTextBody txBody = shape.getTxBody();
  88. if (txBody == null && create) {
  89. txBody = shape.addNewTxBody();
  90. txBody.addNewBodyPr();
  91. txBody.addNewLstStyle();
  92. }
  93. return txBody;
  94. }
  95. int getAdjustValue(String name, int defaultValue){
  96. /*
  97. CTShape shape = (CTShape) getXmlObject();
  98. CTGeomGuideList av = shape.getSpPr().getPrstGeom().getAvLst();
  99. if(av != null){
  100. for(CTGeomGuide gd : av.getGdList()){
  101. if(gd.getName().equals(name)) {
  102. String fmla = gd.getFmla();
  103. Matcher m = adjPtrn.matcher(fmla);
  104. if(m.matches()){
  105. int val = Integer.parseInt(m.group(1));
  106. return 21600*val/100000;
  107. }
  108. }
  109. }
  110. }
  111. */
  112. return defaultValue;
  113. }
  114. @Override
  115. protected java.awt.Shape getOutline(){
  116. java.awt.Shape outline = XSLFPresetGeometry.getOutline(this);
  117. Rectangle2D anchor = getAnchor();
  118. AffineTransform at = new AffineTransform();
  119. at.translate(anchor.getX(), anchor.getY());
  120. at.scale(
  121. 1.0f/21600*anchor.getWidth(),
  122. 1.0f/21600*anchor.getHeight()
  123. );
  124. return outline == null ? anchor : at.createTransformedShape(outline);
  125. }
  126. }