/* ==================================================================== 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.Color; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.util.Iterator; import java.util.List; import org.apache.logging.log4j.Logger; import org.apache.poi.logging.PoiLogManager; import org.apache.poi.ddf.AbstractEscherOptRecord; import org.apache.poi.ddf.EscherChildAnchorRecord; import org.apache.poi.ddf.EscherClientAnchorRecord; import org.apache.poi.ddf.EscherClientDataRecord; import org.apache.poi.ddf.EscherColorRef; import org.apache.poi.ddf.EscherColorRef.SysIndexProcedure; import org.apache.poi.ddf.EscherColorRef.SysIndexSource; import org.apache.poi.ddf.EscherComplexProperty; import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherProperty; import org.apache.poi.ddf.EscherPropertyTypes; import org.apache.poi.ddf.EscherRecord; import org.apache.poi.ddf.EscherRecordTypes; import org.apache.poi.ddf.EscherSimpleProperty; import org.apache.poi.ddf.EscherSpRecord; import org.apache.poi.ddf.EscherTextboxRecord; import org.apache.poi.hslf.record.ColorSchemeAtom; import org.apache.poi.hslf.record.HSLFEscherClientDataRecord; import org.apache.poi.hslf.record.Record; import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.sl.usermodel.FillStyle; import org.apache.poi.sl.usermodel.PresetColor; import org.apache.poi.sl.usermodel.Shape; import org.apache.poi.sl.usermodel.ShapeContainer; import org.apache.poi.sl.usermodel.ShapeType; import org.apache.poi.util.RecordFormatException; import org.apache.poi.util.Removal; import org.apache.poi.util.StringUtil; import org.apache.poi.util.Units; /** * Represents a Shape which is the elemental object that composes a drawing. * This class is a wrapper around EscherSpContainer which holds all information * about a shape in PowerPoint document. *
* When you add a shape, you usually specify the dimensions of the shape and the position
* of the upper'left corner of the bounding box for the shape relative to the upper'left
* corner of the page, worksheet, or slide. Distances in the drawing layer are measured
* in points (72 points = 1 inch).
*/
public abstract class HSLFShape implements Shape
* Default implementation does nothing.
*
* @param sh - owning shape
*/
protected void afterInsert(HSLFSheet sh){
if(_fill != null) {
_fill.afterInsert(sh);
}
}
/**
* @return the {@code SlideShow} this shape belongs to
*/
@Override
public HSLFSheet getSheet(){
return _sheet;
}
/**
* Assign the {@code SlideShow} this shape belongs to
*
* @param sheet owner of this shape
*/
public void setSheet(HSLFSheet sheet){
_sheet = sheet;
}
Color getColor(EscherPropertyTypes colorProperty, EscherPropertyTypes opacityProperty){
final AbstractEscherOptRecord opt = getEscherOptRecord();
final EscherSimpleProperty colProp = getEscherProperty(opt, colorProperty);
final Color col;
if (colProp == null) {
col = Color.WHITE;
} else {
EscherColorRef ecr = new EscherColorRef(colProp.getPropertyValue());
col = getColor(ecr);
if (col == null) {
return null;
}
}
double alpha = getAlpha(opacityProperty);
return new Color(col.getRed(), col.getGreen(), col.getBlue(), (int)(alpha*255.0));
}
Color getColor(EscherColorRef ecr) {
boolean fPaletteIndex = ecr.hasPaletteIndexFlag();
boolean fPaletteRGB = ecr.hasPaletteRGBFlag();
boolean fSystemRGB = ecr.hasSystemRGBFlag();
boolean fSchemeIndex = ecr.hasSchemeIndexFlag();
boolean fSysIndex = ecr.hasSysIndexFlag();
int[] rgb = ecr.getRGB();
HSLFSheet sheet = getSheet();
if (fSchemeIndex && sheet != null) {
//red is the index to the color scheme
ColorSchemeAtom ca = sheet.getColorScheme();
int schemeColor = ca.getColor(ecr.getSchemeIndex());
rgb[0] = (schemeColor >> 0) & 0xFF;
rgb[1] = (schemeColor >> 8) & 0xFF;
rgb[2] = (schemeColor >> 16) & 0xFF;
} else if (fPaletteIndex) {
//TODO
} else if (fPaletteRGB) {
//TODO
} else if (fSystemRGB) {
//TODO
} else if (fSysIndex) {
Color col = getSysIndexColor(ecr);
col = applySysIndexProcedure(ecr, col);
return col;
}
return new Color(rgb[0], rgb[1], rgb[2]);
}
private Color getSysIndexColor(EscherColorRef ecr) {
SysIndexSource sis = ecr.getSysIndexSource();
if (sis == null) {
int sysIdx = ecr.getSysIndex();
PresetColor pc = PresetColor.valueOfNativeId(sysIdx);
return (pc != null) ? pc.color : null;
}
// TODO: check for recursive loops, when color getter also reference
// a different color type
switch (sis) {
case FILL_COLOR: {
return getColor(EscherPropertyTypes.FILL__FILLCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
}
case LINE_OR_FILL_COLOR: {
Color col = null;
if (this instanceof HSLFSimpleShape) {
col = getColor(EscherPropertyTypes.LINESTYLE__COLOR, EscherPropertyTypes.LINESTYLE__OPACITY);
}
if (col == null) {
col = getColor(EscherPropertyTypes.FILL__FILLCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
}
return col;
}
case LINE_COLOR: {
if (this instanceof HSLFSimpleShape) {
return getColor(EscherPropertyTypes.LINESTYLE__COLOR, EscherPropertyTypes.LINESTYLE__OPACITY);
}
break;
}
case SHADOW_COLOR: {
if (this instanceof HSLFSimpleShape) {
return ((HSLFSimpleShape)this).getShadowColor();
}
break;
}
case CURRENT_OR_LAST_COLOR: {
// TODO ... read from graphics context???
break;
}
case FILL_BACKGROUND_COLOR: {
return getColor(EscherPropertyTypes.FILL__FILLBACKCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
}
case LINE_BACKGROUND_COLOR: {
if (this instanceof HSLFSimpleShape) {
return ((HSLFSimpleShape)this).getLineBackgroundColor();
}
break;
}
case FILL_OR_LINE_COLOR: {
Color col = getColor(EscherPropertyTypes.FILL__FILLCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
if (col == null && this instanceof HSLFSimpleShape) {
col = getColor(EscherPropertyTypes.LINESTYLE__COLOR, EscherPropertyTypes.LINESTYLE__OPACITY);
}
return col;
}
default:
break;
}
return null;
}
private Color applySysIndexProcedure(EscherColorRef ecr, Color col) {
final SysIndexProcedure sip = ecr.getSysIndexProcedure();
if (col == null || sip == null) {
return col;
}
switch (sip) {
case DARKEN_COLOR: {
// see java.awt.Color#darken()
double FACTOR = (ecr.getRGB()[2])/255.;
int r = Math.toIntExact(Math.round(col.getRed()*FACTOR));
int g = Math.toIntExact(Math.round(col.getGreen()*FACTOR));
int b = Math.toIntExact(Math.round(col.getBlue()*FACTOR));
return new Color(r,g,b);
}
case LIGHTEN_COLOR: {
double FACTOR = (0xFF-ecr.getRGB()[2])/255.;
int r = col.getRed();
int g = col.getGreen();
int b = col.getBlue();
r = Math.toIntExact(Math.round(r + (0xFF-r)*FACTOR));
g = Math.toIntExact(Math.round(g + (0xFF-g)*FACTOR));
b = Math.toIntExact(Math.round(b + (0xFF-b)*FACTOR));
return new Color(r,g,b);
}
default:
// TODO ...
break;
}
return col;
}
double getAlpha(EscherPropertyTypes opacityProperty) {
AbstractEscherOptRecord opt = getEscherOptRecord();
EscherSimpleProperty op = getEscherProperty(opt, opacityProperty);
int defaultOpacity = 0x00010000;
int opacity = (op == null) ? defaultOpacity : op.getPropertyValue();
return Units.fixedPointToDouble(opacity);
}
Color toRGB(int val){
int a = (val >> 24) & 0xFF;
int b = (val >> 16) & 0xFF;
int g = (val >> 8) & 0xFF;
int r = (val >> 0) & 0xFF;
if(a == 0xFE){
// Color is an sRGB value specified by red, green, and blue fields.
} else if (a == 0xFF){
// Color is undefined.
} else {
// index in the color scheme
ColorSchemeAtom ca = getSheet().getColorScheme();
int schemeColor = ca.getColor(a);
r = (schemeColor >> 0) & 0xFF;
g = (schemeColor >> 8) & 0xFF;
b = (schemeColor >> 16) & 0xFF;
}
return new Color(r, g, b);
}
@Override
public int getShapeId(){
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
return spRecord == null ? 0 : spRecord.getShapeId();
}
/**
* Sets shape ID
*
* @param id of the shape
*/
public void setShapeId(int id){
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
if(spRecord != null) spRecord.setShapeId(id);
}
/**
* Fill properties of this shape
*
* @return fill properties of this shape
*/
public HSLFFill getFill(){
if(_fill == null) {
_fill = new HSLFFill(this);
}
return _fill;
}
public FillStyle getFillStyle() {
return getFill().getFillStyle();
}
@Override
public void draw(Graphics2D graphics, Rectangle2D bounds){
DrawFactory.getInstance(graphics).drawShape(graphics, this, bounds);
}
public AbstractEscherOptRecord getEscherOptRecord() {
AbstractEscherOptRecord opt = getEscherChild(EscherRecordTypes.OPT);
if (opt == null) {
opt = getEscherChild(EscherRecordTypes.USER_DEFINED);
}
return opt;
}
public boolean getFlipHorizontal(){
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPHORIZ) != 0;
}
public void setFlipHorizontal(boolean flip) {
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
int flag = spRecord.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ;
spRecord.setFlags(flag);
}
public boolean getFlipVertical(){
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPVERT) != 0;
}
public void setFlipVertical(boolean flip) {
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
int flag = spRecord.getFlags() | EscherSpRecord.FLAG_FLIPVERT;
spRecord.setFlags(flag);
}
public double getRotation(){
int rot = getEscherProperty(EscherPropertyTypes.TRANSFORM__ROTATION);
return Units.fixedPointToDouble(rot);
}
public void setRotation(double theta){
int rot = Units.doubleToFixedPoint(theta % 360.0);
setEscherProperty(EscherPropertyTypes.TRANSFORM__ROTATION, rot);
}
public boolean isPlaceholder() {
return false;
}
/**
* Find a record in the underlying EscherClientDataRecord
*
* @param recordType type of the record to search
*/
@SuppressWarnings("unchecked")
public