Custom Shape
Change a Shape Using Edit Points
Consider a square. In PowerPoint, using edit points, you can
- move the square’s corner in or out
- specify the curvature for a corner or point
- add new points to the square
- manipulate points on the square, etc.
Essentially, you can perform the described tasks on any shape. Using edit points, you get to change a shape or create a new shape from an existing shape.
Shape Editing Tips
Before you start editing PowerPoint shapes through edit points, you might want to consider these points about shapes:
- A shape (or its path) can either be closed or open.
- When a shape is closed, it lacks a start or end point. When a shape is open, it has a beginning and end.
- All shapes consist of at least 2 anchor points linked to each other by lines
- A line is either straight or curved. Anchor points determine the nature of the line.
- Anchor points exist as corner points, straight points, or smooth points:
- A corner point is a point where 2 straight lines join at an angle.
- A smooth point is a point where 2 handles exist in a straight line and the line’s segments join in a smooth curve. In this case, all handles are separated from the anchor point by an equal distance.
- A straight point is a point where 2 handles exist in a straight line and that line’s line segments joins in a smooth curve. In this case, the handles don’t have to be separated from the anchor point by an equal distance.
- By moving or editing anchor points (which changes the angle of lines), you can change the way a shape looks.
To edit PowerPoint shapes through edit points, Aspose.Slides provides the GeometryPath class and IGeometryPath interface.
- A GeometryPath instance represents a geometry path of the IGeometryShape object.
- To retrieve the
GeometryPath
from theIGeometryShape
instance, you can use the IGeometryShape.getGeometryPaths method. - To set the
GeometryPath
for a shape, you can use these methods: IGeometryShape.setGeometryPath for solid shapes and IGeometryShape.setGeometryPaths for composite shapes. - To add segments, you can use the methods under IGeometryPath.
- Using the IGeometryPath.setStroke and IGeometryPath.setFillMode methods, you can set the appearance for a geometry path.
- Using the IGeometryPath.getPathData method, you can retrieve the geometry path of a
GeometryShape
as an array of path segments. - To access additional shape geometry customization options, you can convert GeometryPath to java.awt.Shape
- Use geometryPathToGraphicsPath and graphicsPathToGeometryPath methods (from the ShapeUtil class) to convert GeometryPath to java.awt.Shape back and forth.
Simple Editing Operations
This PHP code shows you how to
Add a line to the end of a path
Add a line to a specified position on a path:
Add a cubic Bezier curve at the end of a path:
Add a cubic Bezier curve to the specified position on a path:
Add a quadratic Bezier curve at the end of a path:
Add quadratic Bezier curve to a specified position on a path:
Append a given arc to a path:
Close the current figure of a path:
Set the position for the next point:
Remove the path segment at a given index:
Add Custom Points to Shape
- Create an instance of the GeometryShape class and set the ShapeType::Rectangle type.
- Get an instance of the GeometryPath class from the shape.
- Add a new point between the two top points on the path.
- Add a new point between the two bottom points on the path.
- Apply the path to the shape.
This PHP code shows you how to add custom points to a shape:
$pres = new Presentation();
try {
$shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 200, 100);
$geometryPath = $shape->getGeometryPaths()[0];
$geometryPath->lineTo(100, 50, 1);
$geometryPath->lineTo(100, 50, 4);
$shape->setGeometryPath($geometryPath);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Remove Points From Shape
- Create an instance of GeometryShape class and set the ShapeType::Heart type.
- Get an instance of the GeometryPath class from the shape.
- Remove the segment for the path.
- Apply the path to the shape.
This PHP code shows you how to remove points from a shape:
$pres = new Presentation();
try {
$shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Heart, 100, 100, 300, 300);
$path = $shape->getGeometryPaths()[0];
$path->removeAt(2);
$shape->setGeometryPath($path);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Create Custom Shape
- Calculate the points for the shape.
- Create an instance of the GeometryPath class.
- Fill the path with the points.
- Create an instance of the GeometryShape class.
- Apply the path to the shape.
This Java shows you how to create a custom shape:
$points = new Java("java.util.ArrayList");
$R = 100;
$r = 50;
$step = 72;
for($angle = -90; $angle < 270; $angle += $step) {
$radians = $angle * java("java.lang.Math")->PI / 180.0;
$x = $R * java("java.lang.Math")->cos($radians);
$y = $R * java("java.lang.Math")->sin($radians);
$points->add(new Point2DFloat($x + $R, $y + $R));
$radians = java("java.lang.Math")->PI * $angle . $step / 2 / 180.0;
$x = $r * java("java.lang.Math")->cos($radians);
$y = $r * java("java.lang.Math")->sin($radians);
$points->add(new Point2DFloat($x + $R, $y + $R));
}
$starPath = new GeometryPath();
$starPath->moveTo($points->get(0));
for($i = 1; $i < java_values($points->size()) ; $i++) {
$starPath->lineTo($points->get($i));
}
$starPath->closeFigure();
$pres = new Presentation();
try {
$shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, $R * 2, $R * 2);
$shape->setGeometryPath($starPath);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Create Composite Custom Shape
- Create an instance of the GeometryShape class.
- Create a first instance of the GeometryPath class.
- Create a second instance of the GeometryPath class.
- Apply the paths to the shape.
This PHP code shows you to create a composite custom shape:
$pres = new Presentation();
try {
$shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 200, 100);
$geometryPath0 = new GeometryPath();
$geometryPath0->moveTo(0, 0);
$geometryPath0->lineTo($shape->getWidth(), 0);
$geometryPath0->lineTo($shape->getWidth(), $shape->getHeight() / 3);
$geometryPath0->lineTo(0, $shape->getHeight() / 3);
$geometryPath0->closeFigure();
$geometryPath1 = new GeometryPath();
$geometryPath1->moveTo(0, $shape->getHeight() / 3 * 2);
$geometryPath1->lineTo($shape->getWidth(), $shape->getHeight() / 3 * 2);
$geometryPath1->lineTo($shape->getWidth(), $shape->getHeight());
$geometryPath1->lineTo(0, $shape->getHeight());
$geometryPath1->closeFigure();
$shape->setGeometryPaths(array($geometryPath0, $geometryPath1 ));
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Create Custom Shape With Curved Corners
This PHP code shows you how to create a custom shape with curved corners (inwards);
$shapeX = 20.0;
$shapeY = 20.0;
$shapeWidth = 300.0;
$shapeHeight = 200.0;
$leftTopSize = 50.0;
$rightTopSize = 20.0;
$rightBottomSize = 40.0;
$leftBottomSize = 10.0;
$pres = new Presentation();
try {
$childShape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Custom, $shapeX, $shapeY, $shapeWidth, $shapeHeight);
$geometryPath = new GeometryPath();
$point1 = new Point2DFloat($leftTopSize, 0);
$point2 = new Point2DFloat($shapeWidth - $rightTopSize, 0);
$point3 = new Point2DFloat($shapeWidth, $shapeHeight - $rightBottomSize);
$point4 = new Point2DFloat($leftBottomSize, $shapeHeight);
$point5 = new Point2DFloat(0, $leftTopSize);
$geometryPath->moveTo($point1);
$geometryPath->lineTo($point2);
$geometryPath->arcTo($rightTopSize, $rightTopSize, 180, -90);
$geometryPath->lineTo($point3);
$geometryPath->arcTo($rightBottomSize, $rightBottomSize, -90, -90);
$geometryPath->lineTo($point4);
$geometryPath->arcTo($leftBottomSize, $leftBottomSize, 0, -90);
$geometryPath->lineTo($point5);
$geometryPath->arcTo($leftTopSize, $leftTopSize, 90, -90);
$geometryPath->closeFigure();
$childShape->setGeometryPath($geometryPath);
$pres->save("output.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Find Out If a Shape Geometry Is Closed
A closed shape is defined as one where all its sides connect, forming a single boundary without gaps. Such a shape can be a simple geometric form or a complex custom outline. The following code example shows how to check if a shape geometry is closed:
function isGeometryClosed($geometryShape)
{
$isClosed = null;
foreach ($geometryShape->getGeometryPaths() as $geometryPath) {
$dataLength = count(java_values($geometryPath->getPathData()));
if ($dataLength === 0) {
continue;
}
$lastSegment = java_values($geometryPath->getPathData())[$dataLength - 1];
$isClosed = $lastSegment->getPathCommand() === PathCommandType::Close;
if ($isClosed === false) {
return false;
}
}
return $isClosed === true;
}
Convert GeometryPath to java.awt.Shape
- Create an instance of the GeometryShape class.
- Create an instance of the java.awt.Shape class.
- Convert the java.awt.Shape instance to the GeometryPath instance using ShapeUtil.
- Apply the paths to the shape.
This PHP code—an implementation of the steps above—demonstrates the GeometryPath to GraphicsPath conversion process:
$pres = new Presentation();
try {
# Create new shape
$shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 300, 100);
# Get geometry path of the shape
$originalPath = $shape->getGeometryPaths()[0];
$originalPath->setFillMode(PathFillModeType::None);
# Create new graphics path with text
$graphicsPath;
$font = new Font("Arial", Font->PLAIN, 40);
$text = "Text in shape";
$img = new BufferedImage(100, 100, BufferedImage->TYPE_INT_ARGB);
$g2 = $img->createGraphics();
try {
$glyphVector = $font->createGlyphVector($g2->getFontRenderContext(), $text);
$graphicsPath = $glyphVector->getOutline(20.0, -$glyphVector->getVisualBounds()->getY() + 10);
} finally {
$g2->dispose();
}
# Convert graphics path to geometry path
$textPath = ShapeUtil->graphicsPathToGeometryPath($graphicsPath);
$textPath->setFillMode(PathFillModeType::Normal);
# Set combination of new geometry path and origin geometry path to the shape
$shape->setGeometryPaths(array($originalPath, $textPath ));
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}