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.

Path.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.model.geom;
  20. import org.apache.poi.util.Units;
  21. import org.apache.xmlbeans.XmlObject;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.*;
  23. import java.awt.Shape;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.geom.GeneralPath;
  26. import java.awt.geom.Rectangle2D;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. /**
  30. * Specifies a creation path consisting of a series of moves, lines and curves
  31. * that when combined forms a geometric shape
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. public class Path {
  36. private final List<PathCommand> commands;
  37. boolean _fill, _stroke;
  38. long _w, _h;
  39. public Path(){
  40. this(true, true);
  41. }
  42. public Path(boolean fill, boolean stroke){
  43. commands = new ArrayList<PathCommand>();
  44. _w = -1;
  45. _h = -1;
  46. _fill = fill;
  47. _stroke = stroke;
  48. }
  49. public Path(CTPath2D spPath){
  50. _fill = spPath.getFill() != STPathFillMode.NONE;
  51. _stroke = spPath.getStroke();
  52. _w = spPath.isSetW() ? spPath.getW() : -1;
  53. _h = spPath.isSetH() ? spPath.getH() : -1;
  54. commands = new ArrayList<PathCommand>();
  55. for(XmlObject ch : spPath.selectPath("*")){
  56. if(ch instanceof CTPath2DMoveTo){
  57. CTAdjPoint2D pt = ((CTPath2DMoveTo)ch).getPt();
  58. commands.add(new MoveToCommand(pt));
  59. } else if (ch instanceof CTPath2DLineTo){
  60. CTAdjPoint2D pt = ((CTPath2DLineTo)ch).getPt();
  61. commands.add(new LineToCommand(pt));
  62. } else if (ch instanceof CTPath2DArcTo){
  63. CTPath2DArcTo arc = (CTPath2DArcTo)ch;
  64. commands.add(new ArcToCommand(arc));
  65. } else if (ch instanceof CTPath2DQuadBezierTo){
  66. CTPath2DQuadBezierTo bez = ((CTPath2DQuadBezierTo)ch);
  67. CTAdjPoint2D pt1 = bez.getPtArray(0);
  68. CTAdjPoint2D pt2 = bez.getPtArray(1);
  69. commands.add(new QuadToCommand(pt1, pt2));
  70. } else if (ch instanceof CTPath2DCubicBezierTo){
  71. CTPath2DCubicBezierTo bez = ((CTPath2DCubicBezierTo)ch);
  72. CTAdjPoint2D pt1 = bez.getPtArray(0);
  73. CTAdjPoint2D pt2 = bez.getPtArray(1);
  74. CTAdjPoint2D pt3 = bez.getPtArray(2);
  75. commands.add(new CurveToCommand(pt1, pt2, pt3));
  76. } else if (ch instanceof CTPath2DClose){
  77. commands.add(new ClosePathCommand());
  78. } else {
  79. throw new IllegalStateException("Unsupported path segment: " + ch);
  80. }
  81. }
  82. }
  83. public void addCommand(PathCommand cmd){
  84. commands.add(cmd);
  85. }
  86. /**
  87. * Convert the internal represenation to java.awt.GeneralPath
  88. */
  89. public GeneralPath getPath(Context ctx) {
  90. GeneralPath path = new GeneralPath();
  91. for(PathCommand cmd : commands)
  92. cmd.execute(path, ctx);
  93. return path;
  94. }
  95. public boolean isStroked(){
  96. return _stroke;
  97. }
  98. public boolean isFilled(){
  99. return _fill;
  100. }
  101. public long getW(){
  102. return _w;
  103. }
  104. public long getH(){
  105. return _h;
  106. }
  107. }