org.apache.poi.hslf.usermodel.HSLFAutoShape Maven / Gradle / Ivy
Show all versions of apache-poi-ooxml Show documentation
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.usermodel;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ddf.AbstractEscherOptRecord;
import org.apache.poi.ddf.EscherArrayProperty;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.ddf.EscherProperty;
import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.sl.draw.binding.CTAdjPoint2D;
import org.apache.poi.sl.draw.binding.CTCustomGeometry2D;
import org.apache.poi.sl.draw.binding.CTPath2D;
import org.apache.poi.sl.draw.binding.CTPath2DArcTo;
import org.apache.poi.sl.draw.binding.CTPath2DCubicBezierTo;
import org.apache.poi.sl.draw.binding.CTPath2DLineTo;
import org.apache.poi.sl.draw.binding.CTPath2DList;
import org.apache.poi.sl.draw.binding.CTPath2DMoveTo;
import org.apache.poi.sl.draw.binding.ObjectFactory;
import org.apache.poi.sl.draw.geom.CustomGeometry;
import org.apache.poi.sl.usermodel.AutoShape;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/**
* Represents an AutoShape.
*
* AutoShapes are drawing objects with a particular shape that may be customized through smart resizing and adjustments.
* See {@link ShapeTypes}
*/
public class HSLFAutoShape extends HSLFTextShape implements AutoShape {
private static final POILogger LOG = POILogFactory.getLogger(HSLFAutoShape.class);
static final byte[] SEGMENTINFO_MOVETO = new byte[]{0x00, 0x40};
static final byte[] SEGMENTINFO_LINETO = new byte[]{0x00, (byte)0xAC};
static final byte[] SEGMENTINFO_ESCAPE = new byte[]{0x01, 0x00};
static final byte[] SEGMENTINFO_ESCAPE2 = new byte[]{0x01, 0x20};
static final byte[] SEGMENTINFO_CUBICTO = new byte[]{0x00, (byte)0xAD};
// OpenOffice inserts 0xB3 instead of 0xAD.
// protected static final byte[] SEGMENTINFO_CUBICTO2 = new byte[]{0x00, (byte)0xB3};
static final byte[] SEGMENTINFO_CLOSE = new byte[]{0x01, (byte)0x60};
static final byte[] SEGMENTINFO_END = new byte[]{0x00, (byte)0x80};
private static final BitField PATH_INFO = BitFieldFactory.getInstance(0xE000);
private static final BitField ESCAPE_INFO = BitFieldFactory.getInstance(0x1F00);
enum PathInfo {
lineTo(0),curveTo(1),moveTo(2),close(3),end(4),escape(5),clientEscape(6);
private final int flag;
PathInfo(int flag) {
this.flag = flag;
}
public int getFlag() {
return flag;
}
static PathInfo valueOf(int flag) {
for (PathInfo v : values()) {
if (v.flag == flag) {
return v;
}
}
return null;
}
}
enum EscapeInfo {
EXTENSION(0x0000),
ANGLE_ELLIPSE_TO(0x0001),
ANGLE_ELLIPSE(0x0002),
ARC_TO(0x0003),
ARC(0x0004),
CLOCKWISE_ARC_TO(0x0005),
CLOCKWISE_ARC(0x0006),
ELLIPTICAL_QUADRANT_X(0x0007),
ELLIPTICAL_QUADRANT_Y(0x0008),
QUADRATIC_BEZIER(0x0009),
NO_FILL(0X000A),
NO_LINE(0X000B),
AUTO_LINE(0X000C),
AUTO_CURVE(0X000D),
CORNER_LINE(0X000E),
CORNER_CURVE(0X000F),
SMOOTH_LINE(0X0010),
SMOOTH_CURVE(0X0011),
SYMMETRIC_LINE(0X0012),
SYMMETRIC_CURVE(0X0013),
FREEFORM(0X0014),
FILL_COLOR(0X0015),
LINE_COLOR(0X0016);
private final int flag;
EscapeInfo(int flag) {
this.flag = flag;
}
public int getFlag() {
return flag;
}
static EscapeInfo valueOf(int flag) {
for (EscapeInfo v : values()) {
if (v.flag == flag) {
return v;
}
}
return null;
}
}
protected HSLFAutoShape(EscherContainerRecord escherRecord, ShapeContainer parent){
super(escherRecord, parent);
}
public HSLFAutoShape(ShapeType type, ShapeContainer parent){
super(null, parent);
createSpContainer(type, parent instanceof HSLFGroupShape);
}
public HSLFAutoShape(ShapeType type){
this(type, null);
}
protected EscherContainerRecord createSpContainer(ShapeType shapeType, boolean isChild){
EscherContainerRecord ecr = super.createSpContainer(isChild);
setShapeType(shapeType);
//set default properties for an autoshape
setEscherProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x40000);
setEscherProperty(EscherProperties.FILL__FILLCOLOR, 0x8000004);
setEscherProperty(EscherProperties.FILL__FILLCOLOR, 0x8000004);
setEscherProperty(EscherProperties.FILL__FILLBACKCOLOR, 0x8000000);
setEscherProperty(EscherProperties.FILL__NOFILLHITTEST, 0x100010);
setEscherProperty(EscherProperties.LINESTYLE__COLOR, 0x8000001);
setEscherProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80008);
setEscherProperty(EscherProperties.SHADOWSTYLE__COLOR, 0x8000002);
return ecr;
}
@Override
protected void setDefaultTextProperties(HSLFTextParagraph _txtrun){
setVerticalAlignment(VerticalAlignment.MIDDLE);
setHorizontalCentered(true);
setWordWrap(false);
}
/**
* Gets adjust value which controls smart resizing of the auto-shape.
*
* The adjustment values are given in shape coordinates:
* the origin is at the top-left, positive-x is to the right, positive-y is down.
* The region from (0,0) to (S,S) maps to the geometry box of the shape (S=21600 is a constant).
*
* @param idx the adjust index in the [0, 9] range
* @return the adjustment value
*/
public int getAdjustmentValue(int idx){
if(idx < 0 || idx > 9) throw new IllegalArgumentException("The index of an adjustment value must be in the [0, 9] range");
return getEscherProperty((short)(EscherProperties.GEOMETRY__ADJUSTVALUE + idx));
}
/**
* Sets adjust value which controls smart resizing of the auto-shape.
*
* The adjustment values are given in shape coordinates:
* the origin is at the top-left, positive-x is to the right, positive-y is down.
* The region from (0,0) to (S,S) maps to the geometry box of the shape (S=21600 is a constant).
*
* @param idx the adjust index in the [0, 9] range
* @param val the adjustment value
*/
public void setAdjustmentValue(int idx, int val){
if(idx < 0 || idx > 9) throw new IllegalArgumentException("The index of an adjustment value must be in the [0, 9] range");
setEscherProperty((short)(EscherProperties.GEOMETRY__ADJUSTVALUE + idx), val);
}
@Override
public CustomGeometry getGeometry() {
return getGeometry(new Path2D.Double());
}
CustomGeometry getGeometry(Path2D path2D) {
final ObjectFactory of = new ObjectFactory();
final CTCustomGeometry2D cusGeo = of.createCTCustomGeometry2D();
cusGeo.setAvLst(of.createCTGeomGuideList());
cusGeo.setGdLst(of.createCTGeomGuideList());
cusGeo.setAhLst(of.createCTAdjustHandleList());
cusGeo.setCxnLst(of.createCTConnectionSiteList());
final AbstractEscherOptRecord opt = getEscherOptRecord();
EscherArrayProperty verticesProp = getShapeProp(opt, EscherProperties.GEOMETRY__VERTICES);
EscherArrayProperty segmentsProp = getShapeProp(opt, EscherProperties.GEOMETRY__SEGMENTINFO);
// return empty path if either GEOMETRY__VERTICES or GEOMETRY__SEGMENTINFO is missing, see Bugzilla 54188
//sanity check
if(verticesProp == null) {
LOG.log(POILogger.WARN, "Freeform is missing GEOMETRY__VERTICES ");
return super.getGeometry();
}
if(segmentsProp == null) {
LOG.log(POILogger.WARN, "Freeform is missing GEOMETRY__SEGMENTINFO ");
return super.getGeometry();
}
final Iterator vertIter = verticesProp.iterator();
final Iterator segIter = segmentsProp.iterator();
final int[] xyPoints = new int[2];
boolean isClosed = false;
final CTPath2DList pathLst = of.createCTPath2DList();
final CTPath2D pathCT = of.createCTPath2D();
final List