more progress with PPTX2PNG: support for gradient and texture fill, also refactored usages of colors in drawingML

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1190347 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2011-10-28 14:25:53 +00:00
parent d3ca235708
commit b1cb335834
39 changed files with 1182 additions and 468 deletions

View File

@ -17,11 +17,11 @@
package org.apache.poi.xslf.usermodel;
import java.util.List;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import java.util.List;
public class DrawingTextBody {
private final CTTextBody textBody;

View File

@ -32,5 +32,11 @@ public enum Placeholder {
HEADER,
OBJECT,
CHART,
TABLE
TABLE,
CLIP_ART,
DGM,
MEDIA,
SLIDE_IMAGE,
PICTURE
}

View File

@ -0,0 +1,11 @@
package org.apache.poi.xslf.usermodel;
/**
* Created by IntelliJ IDEA.
* User: yegor
* Date: Oct 27, 2011
* Time: 4:50:08 PM
* To change this template use File | Settings | File Templates.
*/
class RenderableShape {
}

View File

@ -16,17 +16,6 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
@ -51,6 +40,17 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideSize;
import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* High level representation of a ooxml slideshow.
* This is the first object most users will construct whether

View File

@ -20,38 +20,15 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.model.geom.Context;
import org.apache.poi.xslf.model.geom.CustomGeometry;
import org.apache.poi.xslf.model.geom.Path;
import org.apache.poi.xslf.model.geom.PresetGeometries;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGeomGuide;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGeomGuideList;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBodyProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextWrappingType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextVerticalType;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShapeNonVisual;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* Represents a preset geometric shape.

View File

@ -17,14 +17,17 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrixReference;
import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrix;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBackgroundFillStyleList;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlCursor;
import javax.imageio.ImageIO;
import javax.xml.namespace.QName;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Rectangle2D;
/**
* Background shape
@ -37,34 +40,40 @@ public class XSLFBackground extends XSLFSimpleShape {
super(shape, sheet);
}
public void draw(Graphics2D graphics) {
@Override
public Rectangle2D getAnchor(){
Dimension pg = getSheet().getSlideShow().getPageSize();
Rectangle anchor = new Rectangle(0, 0, pg.width, pg.height);
CTBackgroundProperties pr = ((CTBackground) getXmlObject()).getBgPr();
if (pr == null) return;
return new Rectangle2D.Double(0, 0, pg.getWidth(), pg.getHeight());
}
XSLFTheme theme = getSheet().getTheme();
if (pr.isSetSolidFill()) {
Color color = theme.getSolidFillColor(pr.getSolidFill());
graphics.setPaint(color);
graphics.fill(anchor);
public void draw(Graphics2D graphics) {
Rectangle2D anchor = getAnchor();
XmlObject spPr = null;
CTBackground bg = (CTBackground)getXmlObject();
if(bg.isSetBgPr()){
spPr = bg.getBgPr();
} else if (bg.isSetBgRef()){
CTStyleMatrixReference bgRef= bg.getBgRef();
int idx = (int)bgRef.getIdx() - 1000;
XSLFTheme theme = getSheet().getTheme();
CTBackgroundFillStyleList bgStyles =
theme.getXmlObject().getThemeElements().getFmtScheme().getBgFillStyleLst();
// TODO pass this to getPaint
XmlObject bgStyle = bgStyles.selectPath("*")[idx];
}
if (pr.isSetBlipFill()) {
String blipId = pr.getBlipFill().getBlip().getEmbed();
PackagePart p = getSheet().getPackagePart();
PackageRelationship rel = p.getRelationship(blipId);
if (rel != null) {
try {
BufferedImage img = ImageIO.read(p.getRelatedPart(rel).getInputStream());
graphics.drawImage(img, (int) anchor.getX(), (int) anchor.getY(),
(int) anchor.getWidth(), (int) anchor.getHeight(), null);
}
catch (Exception e) {
return;
}
}
if(spPr == null){
return;
}
Paint fill = getPaint(graphics, spPr);
if(fill != null) {
graphics.setPaint(fill);
graphics.fill(anchor);
}
}
}

View File

@ -18,37 +18,544 @@
*/
package org.apache.poi.xslf.usermodel;
import java.awt.Color;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
import org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor;
import org.w3c.dom.Node;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
/**
* Encapsulates logic to read color definitions from DrawingML and convert them to java.awt.Color
*
* @author Yegor Kozlov
*/
@Beta
public class XSLFColor {
private final CTColor _ctColor;
XSLFColor(CTColor ctColor){
_ctColor = ctColor;
}
private XmlObject _xmlObject;
private Color _color;
XSLFColor(XmlObject obj, XSLFTheme theme) {
_xmlObject = obj;
_color = toColor(obj, theme);
}
@Internal
public CTColor getXmlObject() {
return _ctColor;
public XmlObject getXmlObject() {
return _xmlObject;
}
public Color getColor(){
return getColor(0xFF);
/**
*
* @return the displayed color as a Java Color.
* If not color information was found in the supplied xml object then a null is returned.
*/
public Color getColor() {
return _color == null ? null : applyColorTransform(_color);
}
public Color getColor(int alpha){
Color color = Color.black;
if(_ctColor.isSetSrgbClr()){
byte[] val = _ctColor.getSrgbClr().getVal();
color = new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2], alpha);
} else if (_ctColor.isSetSysClr()){
byte[] val = _ctColor.getSysClr().getLastClr();
color = new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2], alpha);
}
return color;
private Color applyColorTransform(Color color){
Color result = color;
int alpha = getAlpha();
if(alpha != -1){
result = new Color(
result.getRed(), result.getGreen(), result.getBlue(),
Math.round(255 * alpha * 0.01f));
}
int lumOff = getLumOff();
int lumMod = getLumMod();
if(lumMod != -1 || lumOff != -1){
result = modulateLuminanace(result,
lumMod == -1 ? 100 : lumMod,
lumOff == -1 ? 0 : lumOff);
}
int shade = getShade();
if(shade != -1){
result = shade(result, shade);
}
int tint = getTint();
if(tint != -1){
result = tint(result, tint);
}
return result;
}
static Color toColor(XmlObject obj, XSLFTheme theme) {
Color color = null;
for (XmlObject ch : obj.selectPath("*")) {
if (ch instanceof CTHslColor) {
CTHslColor hsl = (CTHslColor)ch;
int h = hsl.getHue2();
int s = hsl.getSat2();
int l = hsl.getLum2();
// is it correct ?
color = Color.getHSBColor(h / 60000f, s / 100000f, l / 100000f);
} else if (ch instanceof CTPresetColor) {
CTPresetColor prst = (CTPresetColor)ch;
String colorName = prst.getVal().toString();
color = presetColors.get(colorName);
} else if (ch instanceof CTSchemeColor) {
CTSchemeColor schemeColor = (CTSchemeColor)ch;
String colorRef = schemeColor.getVal().toString();
// find referenced CTColor in the theme and convert it to java.awt.Color via a recursive call
CTColor ctColor = theme.getCTColor(colorRef);
if(ctColor != null) color = toColor(ctColor, null);
else {
color = Color.black;
}
} else if (ch instanceof CTScRgbColor) {
// same as CTSRgbColor but with values expressed in percents
CTScRgbColor scrgb = (CTScRgbColor)ch;
int r = scrgb.getR();
int g = scrgb.getG();
int b = scrgb.getB();
color = new Color(255 * r / 100000, 255 * g / 100000, 255 * b / 100000);
} else if (ch instanceof CTSRgbColor) {
CTSRgbColor srgb = (CTSRgbColor)ch;
byte[] val = srgb.getVal();
color = new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]);
} else if (ch instanceof CTSystemColor) {
CTSystemColor sys = (CTSystemColor)ch;
if(sys.isSetLastClr()) {
byte[] val = sys.getLastClr();
color = new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]);
} else {
// YK: color is a string like "menuText" or "windowText", we return black for such cases
String colorName = sys.getVal().toString();
color = Color.black;
}
} else {
throw new IllegalArgumentException("Unexpected color choice: " + ch.getClass());
}
}
return color;
}
private int getPercentageValue(String elem){
XmlObject[] obj = _xmlObject.selectPath(
"declare namespace a='http://schemas.openxmlformats.org/drawingml/2006/main' $this//a:" + elem);
if(obj.length == 1){
Node attr = obj[0].getDomNode().getAttributes().getNamedItem("val");
if(attr != null) {
return Integer.parseInt(attr.getNodeValue()) / 1000;
}
}
return -1;
}
private int getAngleValue(String elem){
XmlObject[] obj = _xmlObject.selectPath(
"declare namespace a='http://schemas.openxmlformats.org/drawingml/2006/main' $this//a:" + elem);
if(obj.length == 1){
Node attr = obj[0].getDomNode().getAttributes().getNamedItem("val");
if(attr != null) {
return Integer.parseInt(attr.getNodeValue()) / 60000;
}
}
return -1;
}
/**
* the opacity as expressed by a percentage value
*
* @return opacity in percents in the range [0..100]
* or -1 if the value is not set
*/
int getAlpha(){
return getPercentageValue("alpha");
}
/**
* the opacity as expressed by a percentage relative to the input color
*
* @return opacity in percents in the range [0..100]
* or -1 if the value is not set
*/
int getAlphaMod(){
return getPercentageValue("alphaMod");
}
/**
* the opacity as expressed by a percentage offset increase or decrease relative to
* the input color. Increases will never increase the opacity beyond 100%, decreases will
* never decrease the opacity below 0%.
*
* @return opacity shift in percents in the range [0..100]
* or -1 if the value is not set
*/
int getAlphaOff(){
return getPercentageValue("alphaOff");
}
int getHue(){
return getAngleValue("hue");
}
int getHueMod(){
return getPercentageValue("hueMod");
}
int getHueOff(){
return getPercentageValue("hueOff");
}
/**
* specifies the input color with the specified luminance,
* but with its hue and saturation unchanged.
*
* @return luminance in percents in the range [0..100]
* or -1 if the value is not set
*/
int getLum(){
return getPercentageValue("lum");
}
/**
* the luminance as expressed by a percentage relative to the input color
*
* @return luminance in percents in the range [0..100]
* or -1 if the value is not set
*/
int getLumMod(){
return getPercentageValue("lumMod");
}
/**
* the luminance shift as expressed by a percentage relative to the input color
*
* @return luminance shift in percents in the range [0..100]
* or -1 if the value is not set
*/
int getLumOff(){
return getPercentageValue("lumOff");
}
/**
* specifies the input color with the specified saturation,
* but with its hue and luminance unchanged.
*
* @return saturation in percents in the range [0..100]
* or -1 if the value is not set
*/
int getSat(){
return getPercentageValue("sat");
}
/**
* the saturation as expressed by a percentage relative to the input color
*
* @return saturation in percents in the range [0..100]
* or -1 if the value is not set
*/
int getSatMod(){
return getPercentageValue("satMod");
}
/**
* the saturation shift as expressed by a percentage relative to the input color
*
* @return saturation shift in percents in the range [0..100]
* or -1 if the value is not set
*/
int getSatOff(){
return getPercentageValue("satOff");
}
/**
* specifies the input color with the specific red component, but with the blue and green color
* components unchanged
*
* @return the value of the red component specified as a
* percentage with 0% indicating minimal blue and 100% indicating maximum
* or -1 if the value is not set
*/
int getRed(){
return getPercentageValue("red");
}
int getRedMod(){
return getPercentageValue("redMod");
}
int getRedOff(){
return getPercentageValue("redOff");
}
/**
* specifies the input color with the specific green component, but with the red and blue color
* components unchanged
*
* @return the value of the green component specified as a
* percentage with 0% indicating minimal blue and 100% indicating maximum
* or -1 if the value is not set
*/
int getGreen(){
return getPercentageValue("green");
}
int getGreenMod(){
return getPercentageValue("greenMod");
}
int getGreenOff(){
return getPercentageValue("greenOff");
}
/**
* specifies the input color with the specific blue component, but with the red and green color
* components unchanged
*
* @return the value of the blue component specified as a
* percentage with 0% indicating minimal blue and 100% indicating maximum
* or -1 if the value is not set
*/
int getBlue(){
return getPercentageValue("blue");
}
int getBlueMod(){
return getPercentageValue("blueMod");
}
int getBlueOff(){
return getPercentageValue("blueOff");
}
/**
* specifies a darker version of its input color.
* A 10% shade is 10% of the input color combined with 90% black.
*
* @return the value of the shade specified as a
* percentage with 0% indicating minimal blue and 100% indicating maximum
* or -1 if the value is not set
*/
int getShade(){
return getPercentageValue("shade");
}
/**
* specifies a lighter version of its input color.
* A 10% tint is 10% of the input color combined with 90% white.
*
* @return the value of the tint specified as a
* percentage with 0% indicating minimal blue and 100% indicating maximum
* or -1 if the value is not set
*/
int getTint(){
return getPercentageValue("tint");
}
/**
* Apply lumMod / lumOff adjustments
*
* @param c the color to modify
* @param lumMod luminance modulation in the range [0..100]
* @param lumOff luminance offset in the range [0..100]
* @return modified color
*/
private static Color modulateLuminanace(Color c, int lumMod, int lumOff) {
Color color;
if (lumOff > 0) {
color = new Color(
(int) (Math.round((255 - c.getRed()) * (100.0 - lumMod) / 100.0 + c.getRed())),
(int) (Math.round((255 - c.getGreen()) * lumOff / 100.0 + c.getGreen())),
(int) (Math.round((255 - c.getBlue()) * lumOff / 100.0 + c.getBlue())),
c.getAlpha()
);
} else {
color = new Color(
(int) (Math.round(c.getRed() * lumMod / 100.0)),
(int) (Math.round(c.getGreen() * lumMod / 100.0)),
(int) (Math.round(c.getBlue() * lumMod / 100.0)),
c.getAlpha()
);
}
return color;
}
private static Color shade(Color c, int shade) {
return new Color(
(int)(c.getRed() * shade * 0.01),
(int)(c.getGreen() * shade * 0.01),
(int)(c.getBlue() * shade * 0.01),
c.getAlpha());
}
private static Color tint(Color c, int tint) {
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
float ftint = tint / 100.0f;
int red = Math.round(ftint * r + (1 - ftint) * 255);
int green = Math.round(ftint * g + (1 - ftint) * 255);
int blue = Math.round(ftint * b + (1 - ftint) * 255);
return new Color(red, green, blue);
}
/**
* Preset colors defined in DrawingML
*/
static Map<String, Color> presetColors;
static {
presetColors = new HashMap<String, Color>();
presetColors.put("aliceBlue", new Color(240, 248, 255));
presetColors.put("antiqueWhite", new Color(250, 235, 215));
presetColors.put("aqua", new Color(0, 255, 255));
presetColors.put("aquamarine", new Color(127, 255, 212));
presetColors.put("azure", new Color(240, 255, 255));
presetColors.put("beige", new Color(245, 245, 220));
presetColors.put("bisque", new Color(255, 228, 196));
presetColors.put("black", new Color(0, 0, 0));
presetColors.put("blanchedAlmond", new Color(255, 235, 205));
presetColors.put("blue", new Color(0, 0, 255));
presetColors.put("blueViolet", new Color(138, 43, 226));
presetColors.put("brown", new Color(165, 42, 42));
presetColors.put("burlyWood", new Color(222, 184, 135));
presetColors.put("cadetBlue", new Color(95, 158, 160));
presetColors.put("chartreuse", new Color(127, 255, 0));
presetColors.put("chocolate", new Color(210, 105, 30));
presetColors.put("coral", new Color(255, 127, 80));
presetColors.put("cornflowerBlue", new Color(100, 149, 237));
presetColors.put("crimson", new Color(220, 20, 60));
presetColors.put("cyan", new Color(0, 255, 255));
presetColors.put("deepPink", new Color(255, 20, 147));
presetColors.put("deepSkyBlue", new Color(0, 191, 255));
presetColors.put("dimGray", new Color(105, 105, 105));
presetColors.put("dkBlue", new Color(0, 0, 139));
presetColors.put("dkCyan", new Color(0, 139, 139));
presetColors.put("dkGoldenrod", new Color(184, 134, 11));
presetColors.put("dkGray", new Color(169, 169, 169));
presetColors.put("dkGreen", new Color(0, 100, 0));
presetColors.put("dkKhaki", new Color(189, 183, 107));
presetColors.put("dkMagenta", new Color(139, 0, 139));
presetColors.put("dkOliveGreen", new Color(85, 107, 47));
presetColors.put("dkOrange", new Color(255, 140, 0));
presetColors.put("dkOrchid", new Color(153, 50, 204));
presetColors.put("dkRed", new Color(139, 0, 0));
presetColors.put("dkSalmon", new Color(233, 150, 122));
presetColors.put("dkSeaGreen", new Color(143, 188, 139));
presetColors.put("dkSlateBlue", new Color(72, 61, 139));
presetColors.put("dkSlateGray", new Color(47, 79, 79));
presetColors.put("dkTurquoise", new Color(0, 206, 209));
presetColors.put("dkViolet", new Color(148, 0, 211));
presetColors.put("dodgerBlue", new Color(30, 144, 255));
presetColors.put("firebrick", new Color(178, 34, 34));
presetColors.put("floralWhite", new Color(255, 250, 240));
presetColors.put("forestGreen", new Color(34, 139, 34));
presetColors.put("fuchsia", new Color(255, 0, 255));
presetColors.put("gainsboro", new Color(220, 220, 220));
presetColors.put("ghostWhite", new Color(248, 248, 255));
presetColors.put("gold", new Color(255, 215, 0));
presetColors.put("goldenrod", new Color(218, 165, 32));
presetColors.put("gray", new Color(128, 128, 128));
presetColors.put("green", new Color(0, 128, 0));
presetColors.put("greenYellow", new Color(173, 255, 47));
presetColors.put("honeydew", new Color(240, 255, 240));
presetColors.put("hotPink", new Color(255, 105, 180));
presetColors.put("indianRed", new Color(205, 92, 92));
presetColors.put("indigo", new Color(75, 0, 130));
presetColors.put("ivory", new Color(255, 255, 240));
presetColors.put("khaki", new Color(240, 230, 140));
presetColors.put("lavender", new Color(230, 230, 250));
presetColors.put("lavenderBlush", new Color(255, 240, 245));
presetColors.put("lawnGreen", new Color(124, 252, 0));
presetColors.put("lemonChiffon", new Color(255, 250, 205));
presetColors.put("lime", new Color(0, 255, 0));
presetColors.put("limeGreen", new Color(50, 205, 50));
presetColors.put("linen", new Color(250, 240, 230));
presetColors.put("ltBlue", new Color(173, 216, 230));
presetColors.put("ltCoral", new Color(240, 128, 128));
presetColors.put("ltCyan", new Color(224, 255, 255));
presetColors.put("ltGoldenrodYellow", new Color(250, 250, 120));
presetColors.put("ltGray", new Color(211, 211, 211));
presetColors.put("ltGreen", new Color(144, 238, 144));
presetColors.put("ltPink", new Color(255, 182, 193));
presetColors.put("ltSalmon", new Color(255, 160, 122));
presetColors.put("ltSeaGreen", new Color(32, 178, 170));
presetColors.put("ltSkyBlue", new Color(135, 206, 250));
presetColors.put("ltSlateGray", new Color(119, 136, 153));
presetColors.put("ltSteelBlue", new Color(176, 196, 222));
presetColors.put("ltYellow", new Color(255, 255, 224));
presetColors.put("magenta", new Color(255, 0, 255));
presetColors.put("maroon", new Color(128, 0, 0));
presetColors.put("medAquamarine", new Color(102, 205, 170));
presetColors.put("medBlue", new Color(0, 0, 205));
presetColors.put("medOrchid", new Color(186, 85, 211));
presetColors.put("medPurple", new Color(147, 112, 219));
presetColors.put("medSeaGreen", new Color(60, 179, 113));
presetColors.put("medSlateBlue", new Color(123, 104, 238));
presetColors.put("medSpringGreen", new Color(0, 250, 154));
presetColors.put("medTurquoise", new Color(72, 209, 204));
presetColors.put("medVioletRed", new Color(199, 21, 133));
presetColors.put("midnightBlue", new Color(25, 25, 112));
presetColors.put("mintCream", new Color(245, 255, 250));
presetColors.put("mistyRose", new Color(255, 228, 225));
presetColors.put("moccasin", new Color(255, 228, 181));
presetColors.put("navajoWhite", new Color(255, 222, 173));
presetColors.put("navy", new Color(0, 0, 128));
presetColors.put("oldLace", new Color(253, 245, 230));
presetColors.put("olive", new Color(128, 128, 0));
presetColors.put("oliveDrab", new Color(107, 142, 35));
presetColors.put("orange", new Color(255, 165, 0));
presetColors.put("orangeRed", new Color(255, 69, 0));
presetColors.put("orchid", new Color(218, 112, 214));
presetColors.put("paleGoldenrod", new Color(238, 232, 170));
presetColors.put("paleGreen", new Color(152, 251, 152));
presetColors.put("paleTurquoise", new Color(175, 238, 238));
presetColors.put("paleVioletRed", new Color(219, 112, 147));
presetColors.put("papayaWhip", new Color(255, 239, 213));
presetColors.put("peachPuff", new Color(255, 218, 185));
presetColors.put("peru", new Color(205, 133, 63));
presetColors.put("pink", new Color(255, 192, 203));
presetColors.put("plum", new Color(221, 160, 221));
presetColors.put("powderBlue", new Color(176, 224, 230));
presetColors.put("purple", new Color(128, 0, 128));
presetColors.put("red", new Color(255, 0, 0));
presetColors.put("rosyBrown", new Color(188, 143, 143));
presetColors.put("royalBlue", new Color(65, 105, 225));
presetColors.put("saddleBrown", new Color(139, 69, 19));
presetColors.put("salmon", new Color(250, 128, 114));
presetColors.put("sandyBrown", new Color(244, 164, 96));
presetColors.put("seaGreen", new Color(46, 139, 87));
presetColors.put("seaShell", new Color(255, 245, 238));
presetColors.put("sienna", new Color(160, 82, 45));
presetColors.put("silver", new Color(192, 192, 192));
presetColors.put("skyBlue", new Color(135, 206, 235));
presetColors.put("slateBlue", new Color(106, 90, 205));
presetColors.put("slateGray", new Color(112, 128, 144));
presetColors.put("snow", new Color(255, 250, 250));
presetColors.put("springGreen", new Color(0, 255, 127));
presetColors.put("steelBlue", new Color(70, 130, 180));
presetColors.put("tan", new Color(210, 180, 140));
presetColors.put("teal", new Color(0, 128, 128));
presetColors.put("thistle", new Color(216, 191, 216));
presetColors.put("tomato", new Color(255, 99, 71));
presetColors.put("turquoise", new Color(64, 224, 208));
presetColors.put("violet", new Color(238, 130, 238));
presetColors.put("wheat", new Color(245, 222, 179));
presetColors.put("white", new Color(255, 255, 255));
presetColors.put("whiteSmoke", new Color(245, 245, 245));
presetColors.put("yellow", new Color(255, 255, 0));
presetColors.put("yellowGreen", new Color(154, 205, 50));
}
}

View File

@ -17,8 +17,6 @@
package org.apache.poi.xslf.usermodel;
import java.io.IOException;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
@ -28,6 +26,8 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthorList;
import org.openxmlformats.schemas.presentationml.x2006.main.CmAuthorLstDocument;
import java.io.IOException;
@Beta
public class XSLFCommentAuthors extends POIXMLDocumentPart {
private final CTCommentAuthorList _authors;

View File

@ -17,8 +17,6 @@
package org.apache.poi.xslf.usermodel;
import java.io.IOException;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
@ -28,6 +26,8 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentList;
import org.openxmlformats.schemas.presentationml.x2006.main.CmLstDocument;
import java.io.IOException;
@Beta
public class XSLFComments extends POIXMLDocumentPart {
private final CTCommentList _comments;

View File

@ -20,20 +20,23 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Beta;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnectorNonVisual;
import java.awt.*;
import java.awt.geom.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
/**
* Specifies a connection shape.
@ -202,11 +205,11 @@ public class XSLFConnectorShape extends XSLFSimpleShape {
XSLFShadow shadow = getShadow();
//border
Color lineColor = getLineColor();
if (lineColor != null) {
Paint line = getLinePaint(graphics);
if (line != null) {
if (shadow != null) shadow.draw(graphics);
graphics.setColor(lineColor);
graphics.setPaint(line);
applyStroke(graphics);
graphics.draw(outline);

View File

@ -16,15 +16,14 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.util.Beta;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import java.awt.*;

View File

@ -19,20 +19,14 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.sl.usermodel.ShapeGroup;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.*;
import java.awt.geom.Rectangle2D;
/**

View File

@ -22,9 +22,6 @@ package org.apache.poi.xslf.usermodel;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.sl.usermodel.ShapeGroup;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.apache.xmlbeans.XmlObject;
@ -33,13 +30,12 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import java.awt.Graphics2D;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.List;

View File

@ -22,10 +22,12 @@ package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Beta;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* For now this class renders only images supported by the javax.imageio.ImageIO
@ -74,14 +76,25 @@ public class XSLFImageRendener {
public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
Rectangle2D anchor) {
try {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(data
.getData()));
graphics.drawImage(img, (int) anchor.getX(), (int) anchor.getY(),
(int) anchor.getWidth(), (int) anchor.getHeight(), null);
BufferedImage img = readImage(new ByteArrayInputStream(data.getData()));
if (img != null){
graphics.drawImage(img, (int) anchor.getX(), (int) anchor.getY(),
(int) anchor.getWidth(), (int) anchor.getHeight(), null);
}
return true;
} catch (Exception e) {
return false;
}
}
/**
* create a buffered image from input stream
*
* @return a <code>BufferedImage</code> containing the decoded
* contents of the input, or <code>null</code>.
*/
public BufferedImage readImage(InputStream is) throws IOException {
return ImageIO.read(is);
}
}

View File

@ -16,8 +16,6 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import java.io.IOException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.util.Beta;
@ -26,6 +24,8 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesSlide;
import org.openxmlformats.schemas.presentationml.x2006.main.NotesDocument;
import java.io.IOException;
@Beta
public final class XSLFNotes extends XSLFSheet {
private CTNotesSlide _notes;

View File

@ -16,19 +16,14 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.sl.usermodel.MasterSheet;
import org.apache.poi.util.Beta;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMaster;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
import org.openxmlformats.schemas.presentationml.x2006.main.NotesMasterDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**

View File

@ -19,27 +19,21 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.POIXMLException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.util.Beta;
import org.apache.poi.util.POILogger;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPictureNonVisual;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
@ -104,9 +98,15 @@ public class XSLFPictureShape extends XSLFSimpleShape {
CTPicture ct = (CTPicture)getXmlObject();
String blipId = ct.getBlipFill().getBlip().getEmbed();
for (POIXMLDocumentPart part : getSheet().getRelations()) {
if(part.getPackageRelationship().getId().equals(blipId)){
_data = (XSLFPictureData)part;
PackagePart p = getSheet().getPackagePart();
PackageRelationship rel = p.getRelationship(blipId);
if (rel != null) {
try {
PackagePart imgPart = p.getRelatedPart(rel);
_data = new XSLFPictureData(imgPart, rel);
}
catch (Exception e) {
throw new POIXMLException(e);
}
}
}
@ -120,31 +120,31 @@ public class XSLFPictureShape extends XSLFSimpleShape {
// shadow
XSLFShadow shadow = getShadow();
//fill
Color fillColor = getFillColor();
if (fillColor != null) {
if(shadow != null) shadow.draw(graphics);
Paint fill = getFill(graphics);
Paint line = getLinePaint(graphics);
if(shadow != null) {
shadow.draw(graphics);
}
graphics.setColor(fillColor);
applyFill(graphics);
if(fill != null) {
graphics.setPaint(fill);
graphics.fill(outline);
}
XSLFPictureData data = getPictureData();
if(data == null) return;
XSLFImageRendener renderer = (XSLFImageRendener)graphics.getRenderingHint(XSLFRenderingHint.IMAGE_RENDERER);
if(renderer == null) renderer = new XSLFImageRendener();
renderer.drawImage(graphics, data, getAnchor());
//border overlays the image
Color lineColor = getLineColor();
if (lineColor != null){
graphics.setColor(lineColor);
if (line != null){
graphics.setPaint(line);
applyStroke(graphics);
graphics.draw(outline);
}
}
}
}

View File

@ -17,19 +17,11 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
/**
* Represents a shadow of a shape. For now supports only outer shadows.
@ -49,8 +41,8 @@ public class XSLFShadow extends XSLFSimpleShape {
public void draw(Graphics2D graphics) {
Shape outline = _parent.getOutline();
Color parentFillColor = _parent.getFillColor();
Color parentLineColor = _parent.getLineColor();
Paint parentFillColor = _parent.getFill(graphics);
Paint parentLineColor = _parent.getLinePaint(graphics);
double angle = getAngle();
double dist = getDistance();
@ -120,16 +112,6 @@ public class XSLFShadow extends XSLFSimpleShape {
public Color getFillColor() {
XSLFTheme theme = getSheet().getTheme();
CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
if(ct.isSetSchemeClr()) {
return theme.getSchemeColor(ct.getSchemeClr());
}
else if (ct.isSetPrstClr()) {
return theme.getPresetColor(ct.getPrstClr());
}
else if (ct.isSetSrgbClr()) {
return theme.getSrgbColor(ct.getSrgbClr());
}
return null;
return ct == null ? null : new XSLFColor(ct, theme).getColor();
}
}

View File

@ -21,9 +21,8 @@ package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Beta;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import java.awt.Graphics2D;
import java.awt.*;
import java.awt.geom.Rectangle2D;
/**

View File

@ -21,22 +21,20 @@ import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import javax.xml.namespace.QName;
import java.awt.Graphics2D;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.io.OutputStream;
@ -64,7 +62,14 @@ public abstract class XSLFSheet extends POIXMLDocumentPart {
}
public XMLSlideShow getSlideShow() {
return (XMLSlideShow)getParent();
POIXMLDocumentPart p = getParent();
while(p != null) {
if(p instanceof XMLSlideShow){
return (XMLSlideShow)p;
}
p = p.getParent();
}
return null;
}
protected List<XSLFShape> buildShapes(CTGroupShape spTree){

View File

@ -19,22 +19,39 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.xslf.model.geom.*;
import org.apache.poi.xslf.usermodel.LineCap;
import org.apache.poi.xslf.usermodel.LineDash;
import org.apache.poi.xslf.model.PropertyFetcher;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.model.PropertyFetcher;
import org.apache.poi.xslf.model.geom.Context;
import org.apache.poi.xslf.model.geom.CustomGeometry;
import org.apache.poi.xslf.model.geom.Guide;
import org.apache.poi.xslf.model.geom.IAdjustableShape;
import org.apache.poi.xslf.model.geom.Path;
import org.apache.poi.xslf.model.geom.PresetGeometries;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.awt.Paint;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.TexturePaint;
import java.awt.AlphaComposite;
import java.awt.GradientPaint;
import java.awt.BasicStroke;
import java.awt.Stroke;
import java.util.Arrays;
import java.util.Comparator;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Constructor;
/**
* @author Yegor Kozlov
@ -265,40 +282,45 @@ public abstract class XSLFSimpleShape extends XSLFShape {
}
public Color getLineColor() {
Paint paint = getLinePaint(null);
if(paint instanceof Color){
return (Color)paint;
}
return null;
}
public Paint getLinePaint(final Graphics2D graphics) {
final XSLFTheme theme = _sheet.getTheme();
final Color noline = new Color(0,0,0,0);
PropertyFetcher<Color> fetcher = new PropertyFetcher<Color>(){
final Color nofill = new Color(0,0,0,0);
PropertyFetcher<Paint> fetcher = new PropertyFetcher<Paint>(){
public boolean fetch(XSLFSimpleShape shape){
CTShapeProperties spPr = shape.getSpPr();
CTLineProperties ln = spPr.getLn();
if (ln != null) {
if (ln.isSetNoFill()) {
setValue(noline);
CTLineProperties spPr = shape.getSpPr().getLn();
if (spPr != null) {
if (spPr.isSetNoFill()) {
setValue(nofill); // use it as 'nofill' value
return true;
}
CTSolidColorFillProperties solidLine = ln.getSolidFill();
if (solidLine != null) {
setValue( theme.getSolidFillColor(ln.getSolidFill()) );
Paint paint = getPaint(graphics, spPr);
if (paint != null) {
setValue( paint );
return true;
}
}
return false;
}
};
fetchShapeProperty(fetcher);
Color color = fetcher.getValue();
Paint color = fetcher.getValue();
if(color == null){
// line color was not found, check if it is defined in the theme
CTShapeStyle style = getSpStyle();
if (style != null) {
CTSchemeColor schemeColor = style.getLnRef().getSchemeClr();
if (schemeColor != null) {
color = theme.getSchemeColor(schemeColor);
}
color = new XSLFColor(style.getLnRef(), theme).getColor();
}
}
return color == noline ? null : color;
return color == nofill ? null : color;
}
public void setLineWidth(double width) {
@ -466,17 +488,31 @@ public abstract class XSLFSimpleShape extends XSLFShape {
* @return solid fill color of null if not set
*/
public Color getFillColor() {
Paint paint = getFill(null);
if(paint instanceof Color){
return (Color)paint;
}
return null;
}
/**
* fetch shape fill as a java.awt.Paint
*
* @return either Color or GradientPaint or TexturePaint or null
*/
Paint getFill(final Graphics2D graphics) {
final XSLFTheme theme = _sheet.getTheme();
final Color nofill = new Color(0,0,0,0);
PropertyFetcher<Color> fetcher = new PropertyFetcher<Color>(){
final Color nofill = new Color(0xFF,0xFF,0xFF, 0);
PropertyFetcher<Paint> fetcher = new PropertyFetcher<Paint>(){
public boolean fetch(XSLFSimpleShape shape){
CTShapeProperties spPr = shape.getSpPr();
if (spPr.isSetNoFill()) {
setValue(nofill); // use it as 'nofill' value
return true;
}
if (spPr.isSetSolidFill()) {
setValue( theme.getSolidFillColor(spPr.getSolidFill()) );
Paint paint = getPaint(graphics, spPr);
if (paint != null) {
setValue( paint );
return true;
}
return false;
@ -484,18 +520,15 @@ public abstract class XSLFSimpleShape extends XSLFShape {
};
fetchShapeProperty(fetcher);
Color color = fetcher.getValue();
if(color == null){
Paint paint = fetcher.getValue();
if(paint == null){
// fill color was not found, check if it is defined in the theme
CTShapeStyle style = getSpStyle();
if (style != null) {
CTSchemeColor schemeColor = style.getFillRef().getSchemeClr();
if (schemeColor != null) {
color = theme.getSchemeColor(schemeColor);
}
paint = new XSLFColor(style.getFillRef(), theme).getColor();
}
}
return color == nofill ? null : color;
return paint == nofill ? null : paint;
}
public XSLFShadow getShadow(){
@ -532,8 +565,130 @@ public abstract class XSLFSimpleShape extends XSLFShape {
}
protected void applyFill(Graphics2D graphics) {
@SuppressWarnings("deprecation") // getXYZArray() array accessors are deprecated
protected Paint getPaint(Graphics2D graphics, XmlObject spPr) {
XSLFTheme theme = getSheet().getTheme();
Rectangle2D anchor = getAnchor();
Paint paint = null;
for(XmlObject obj : spPr.selectPath("*")){
if(obj instanceof CTNoFillProperties){
paint = null;
break;
}
if(obj instanceof CTSolidColorFillProperties){
CTSolidColorFillProperties solidFill = (CTSolidColorFillProperties)obj;
XSLFColor c = new XSLFColor(solidFill, theme);
paint = c.getColor();
}
if(obj instanceof CTBlipFillProperties){
CTBlipFillProperties blipFill = (CTBlipFillProperties)obj;
CTBlip blip = blipFill.getBlip();
String blipId = blip.getEmbed();
PackagePart p = getSheet().getPackagePart();
PackageRelationship rel = p.getRelationship(blipId);
if (rel != null) {
XSLFImageRendener renderer = null;
if(graphics != null) renderer = (XSLFImageRendener)graphics.getRenderingHint(XSLFRenderingHint.IMAGE_RENDERER);
if(renderer == null) renderer = new XSLFImageRendener();
try {
BufferedImage img = renderer.readImage(p.getRelatedPart(rel).getInputStream());
if(blip.sizeOfAlphaModFixArray() > 0){
float alpha = blip.getAlphaModFixArray(0).getAmt()/100000.f;
AlphaComposite ac = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);
if(graphics != null) graphics.setComposite(ac);
}
paint = new TexturePaint(
img, new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
}
catch (Exception e) {
return null;
}
}
}
if(obj instanceof CTGradientFillProperties){
CTGradientFillProperties gradFill = (CTGradientFillProperties)obj;
double angle;
if(gradFill.isSetLin()) {
angle = gradFill.getLin().getAng() / 60000;
} else {
// XSLF only supports linear gradient fills. Other types are filled as liner with angle=90 degrees
angle = 90;
}
CTGradientStop[] gs = gradFill.getGsLst().getGsArray();
Arrays.sort(gs, new Comparator<CTGradientStop>(){
public int compare(CTGradientStop o1, CTGradientStop o2){
Integer pos1 = o1.getPos();
Integer pos2 = o2.getPos();
return pos1.compareTo(pos2);
}
});
Color[] colors = new Color[gs.length];
float[] fractions = new float[gs.length];
AffineTransform at = AffineTransform.getRotateInstance(
Math.toRadians(angle),
anchor.getX() + anchor.getWidth()/2,
anchor.getY() + anchor.getHeight()/2);
double diagonal = Math.sqrt(anchor.getHeight()*anchor.getHeight() + anchor.getWidth()*anchor.getWidth());
Point2D p1 = new Point2D.Double(anchor.getX() + anchor.getWidth()/2 - diagonal/2,
anchor.getY() + anchor.getHeight()/2);
p1 = at.transform(p1, null);
Point2D p2 = new Point2D.Double(anchor.getX() + anchor.getWidth(), anchor.getY() + anchor.getHeight()/2);
p2 = at.transform(p2, null);
norm(p1, anchor);
norm(p2, anchor);
for(int i = 0; i < gs.length; i++){
CTGradientStop stop = gs[i];
colors[i] = new XSLFColor(stop, theme).getColor();
fractions[i] = stop.getPos() / 100000.f;
}
paint = createGradientPaint(p1, p2, fractions, colors);
}
}
return paint;
}
/**
* Trick to return GradientPaint on JDK 1.5 and LinearGradientPaint on JDK 1.6+
*/
private Paint createGradientPaint(Point2D p1, Point2D p2, float[] fractions, Color[] colors){
Paint paint;
try {
Class clz = Class.forName("java.awt.LinearGradientPaint");
Constructor c =
clz.getConstructor(Point2D.class, Point2D.class, float[].class, Color[].class);
paint = (Paint)c.newInstance(p1, p2, fractions, colors);
} catch (ClassNotFoundException e){
paint = new GradientPaint(p1, colors[0], p2, colors[colors.length - 1]);
} catch (Exception e){
throw new RuntimeException(e);
}
return paint;
}
void norm(Point2D p, Rectangle2D anchor){
if(p.getX() < anchor.getX()){
p.setLocation(anchor.getX(), p.getY());
} else if(p.getX() > (anchor.getX() + anchor.getWidth())){
p.setLocation(anchor.getX() + anchor.getWidth(), p.getY());
}
if(p.getY() < anchor.getY()){
p.setLocation(p.getX(), anchor.getY());
} else if (p.getY() > (anchor.getY() + anchor.getHeight())){
p.setLocation(p.getX(), anchor.getY() + anchor.getHeight());
}
}
protected float[] getDashPattern(LineDash lineDash, float lineWidth) {
@ -665,7 +820,13 @@ public abstract class XSLFSimpleShape extends XSLFShape {
@Override
protected java.awt.Shape getOutline(){
PresetGeometries dict = PresetGeometries.getInstance();
String name = getSpPr().getPrstGeom().getPrst().toString();
CTShapeProperties spPr = getSpPr();
String name;
if(spPr.isSetPrstGeom()) {
name = spPr.getPrstGeom().getPrst().toString();
} else {
name = "rect";
}
CustomGeometry geom = dict.get(name);
Rectangle2D anchor = getAnchor();
if(geom != null) {

View File

@ -16,28 +16,24 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import java.awt.*;
import java.io.IOException;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.util.Beta;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import java.awt.*;
import java.io.IOException;
@Beta
public final class XSLFSlide extends XSLFSheet {

View File

@ -22,18 +22,12 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideLayout;
import org.openxmlformats.schemas.presentationml.x2006.main.SldLayoutDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.awt.*;
import java.awt.geom.AffineTransform;
@Beta
public class XSLFSlideLayout extends XSLFSheet {
@ -106,9 +100,6 @@ public class XSLFSlideLayout extends XSLFSheet {
return _master;
}
public XMLSlideShow getSlideShow() {
return (XMLSlideShow)getParent().getParent();
}
public XSLFTheme getTheme(){
return getSlideMaster().getTheme();
@ -143,6 +134,6 @@ public class XSLFSlideLayout extends XSLFSheet {
if(_layout.getCSld().isSetBg()) {
return new XSLFBackground(_layout.getCSld().getBg(), this);
}
return null;
return getSlideMaster().getBackground();
}
}

View File

@ -19,13 +19,12 @@ package org.apache.poi.xslf.usermodel;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.sl.usermodel.MasterSheet;
import org.apache.poi.util.Beta;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterTextStyles;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterTextStyles;
import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
import java.io.IOException;
import java.util.HashMap;
@ -123,4 +122,11 @@ import java.util.Map;
return props;
}
@Override
public XSLFBackground getBackground(){
if(_slide.getCSld().isSetBg()) {
return new XSLFBackground(_slide.getCSld().getBg(), this);
}
return null;
}
}

View File

@ -27,10 +27,6 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual;
@ -39,7 +35,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.awt.geom.Rectangle2D;
/**
* Represents a table in a .pptx presentation

View File

@ -19,24 +19,21 @@
package org.apache.poi.xslf.usermodel;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBodyProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCellProperties;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STPenAlignment;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCellProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.STCompoundLine;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.STPenAlignment;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import java.awt.*;

View File

@ -20,16 +20,13 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Units;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
import java.util.List;
/**
* Represents a table in a .pptx presentation

View File

@ -26,9 +26,9 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTTableStyleList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
@Beta
public class XSLFTableStyles extends POIXMLDocumentPart implements Iterable<XSLFTableStyle>{

View File

@ -19,25 +19,17 @@
package org.apache.poi.xslf.usermodel;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShapeNonVisual;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import java.awt.geom.Rectangle2D;
/**
* @author Yegor Kozlov

View File

@ -19,28 +19,27 @@ package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.model.PropertyFetcher;
import org.apache.poi.xslf.model.ParagraphPropertyFetcher;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextSpacing;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import java.awt.*;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.font.TextLayout;
import java.awt.font.TextAttribute;
import java.awt.font.LineBreakMeasurer;
import java.text.AttributedString;
import java.text.AttributedCharacterIterator;
/**
* Represents a paragraph of text within the containing text body.
@ -187,7 +186,8 @@ public class XSLFTextParagraph implements Iterable<XSLFTextRun>{
ParagraphPropertyFetcher<Color> fetcher = new ParagraphPropertyFetcher<Color>(getLevel()){
public boolean fetch(CTTextParagraphProperties props){
if(props.isSetBuClr()){
setValue(theme.getColor(props.getBuClr()));
XSLFColor c = new XSLFColor(props.getBuClr(), theme);
setValue(c.getColor());
return true;
}
return false;
@ -580,7 +580,7 @@ public class XSLFTextParagraph implements Iterable<XSLFTextRun>{
string.addAttribute(TextAttribute.FOREGROUND, run.getFontColor(), startIndex, endIndex);
string.addAttribute(TextAttribute.FAMILY, run.getFontFamily(), startIndex, endIndex);
string.addAttribute(TextAttribute.SIZE, run.getFontSize(), startIndex, endIndex);
string.addAttribute(TextAttribute.SIZE, (float)run.getFontSize(), startIndex, endIndex);
if(run.isBold()) {
string.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIndex, endIndex);
}
@ -589,10 +589,17 @@ public class XSLFTextParagraph implements Iterable<XSLFTextRun>{
}
if(run.isUnderline()) {
string.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIndex, endIndex);
string.addAttribute(TextAttribute.INPUT_METHOD_UNDERLINE, TextAttribute.UNDERLINE_LOW_TWO_PIXEL, startIndex, endIndex);
}
if(run.isStrikethrough()) {
string.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, startIndex, endIndex);
}
if(run.isSubscript()) {
string.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, startIndex, endIndex);
}
if(run.isSuperscript()) {
string.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, startIndex, endIndex);
}
startIndex = endIndex;
}
@ -646,8 +653,8 @@ public class XSLFTextParagraph implements Iterable<XSLFTextRun>{
bit.getAttribute(TextAttribute.FOREGROUND) : buColor);
str.addAttribute(TextAttribute.FAMILY, buFont);
double fontSize = (Double)bit.getAttribute(TextAttribute.SIZE);
double buSz = getBulletFontSize();
float fontSize = (Float)bit.getAttribute(TextAttribute.SIZE);
float buSz = (float)getBulletFontSize();
if(buSz > 0) fontSize *= buSz* 0.01;
else fontSize = -buSz;

View File

@ -18,17 +18,14 @@ package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Beta;
import org.apache.poi.xslf.model.CharacterPropertyFetcher;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextStrikeType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import java.awt.*;
@ -78,7 +75,8 @@ public class XSLFTextRun {
public boolean fetch(CTTextCharacterProperties props){
CTSolidColorFillProperties solidFill = props.getSolidFill();
if(solidFill != null){
setValue(theme.getSolidFillColor(solidFill));
Color c = new XSLFColor(solidFill, theme).getColor();
setValue(c);
return true;
}
return false;
@ -202,6 +200,40 @@ public class XSLFTextRun {
return fetcher.getValue() == null ? false : fetcher.getValue();
}
/**
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public boolean isSuperscript() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
public boolean fetch(CTTextCharacterProperties props){
if(props.isSetBaseline()){
setValue(props.getBaseline() > 0);
return true;
}
return false;
}
};
fetchCharacterProperty(fetcher);
return fetcher.getValue() == null ? false : fetcher.getValue();
}
/**
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public boolean isSubscript() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
public boolean fetch(CTTextCharacterProperties props){
if(props.isSetBaseline()){
setValue(props.getBaseline() < 0);
return true;
}
return false;
}
};
fetchCharacterProperty(fetcher);
return fetcher.getValue() == null ? false : fetcher.getValue();
}
/**
* Specifies whether this run of text will be formatted as bold text
*
@ -314,4 +346,4 @@ public class XSLFTextRun {
return ok;
}
}
}

View File

@ -23,9 +23,6 @@ import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.model.PropertyFetcher;
import org.apache.poi.xslf.model.TextBodyPropertyFetcher;
import org.apache.poi.xslf.model.geom.Context;
import org.apache.poi.xslf.model.geom.CustomGeometry;
import org.apache.poi.xslf.model.geom.Path;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBodyProperties;
@ -36,9 +33,8 @@ import org.openxmlformats.schemas.drawingml.x2006.main.STTextWrappingType;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
@ -392,19 +388,19 @@ public abstract class XSLFTextShape extends XSLFSimpleShape {
// shadow
XSLFShadow shadow = getShadow();
Color fillColor = getFillColor();
Color lineColor = getLineColor();
Paint fill = getFill(graphics);
Paint line = getLinePaint(graphics);
if(shadow != null) {
//shadow.draw(graphics);
shadow.draw(graphics);
}
if (fillColor != null) {
graphics.setColor(fillColor);
applyFill(graphics);
if(fill != null) {
graphics.setPaint(fill);
graphics.fill(outline);
}
if (lineColor != null){
graphics.setColor(lineColor);
if (line != null){
graphics.setPaint(line);
applyStroke(graphics);
graphics.draw(outline);
}

View File

@ -24,28 +24,22 @@ import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBaseStyles;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor;
import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
import javax.xml.namespace.QName;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
@Beta
public class XSLFTheme extends POIXMLDocumentPart {
private CTOfficeStyleSheet _theme;
private Map<String, XSLFColor> _schemeColors;
private Map<String, CTColor> _schemeColors;
XSLFTheme() {
super();
@ -64,12 +58,13 @@ public class XSLFTheme extends POIXMLDocumentPart {
CTBaseStyles elems = _theme.getThemeElements();
CTColorScheme scheme = elems.getClrScheme();
// The color scheme is responsible for defining a list of twelve colors.
_schemeColors = new HashMap<String, XSLFColor>(12);
_schemeColors = new HashMap<String, CTColor>(12);
for(XmlObject o : scheme.selectPath("*")){
CTColor c = (CTColor)o;
String name = c.getDomNode().getLocalName();
_schemeColors.put(name, new XSLFColor(c));
_schemeColors.put(name, c);
}
_schemeColors.put("bg1", _schemeColors.get("lt1"));
_schemeColors.put("bg2", _schemeColors.get("lt2"));
_schemeColors.put("tx1", _schemeColors.get("dk1"));
@ -89,141 +84,10 @@ public class XSLFTheme extends POIXMLDocumentPart {
*
* @return a theme color or <code>null</code> if not found
*/
public XSLFColor getColor(String name){
CTColor getCTColor(String name){
return _schemeColors.get(name);
}
Color getSchemeColor(CTSchemeColor schemeColor){
String colorRef = schemeColor.getVal().toString();
int alpha = 0xFF;
if(schemeColor.sizeOfAlphaArray() > 0){
int aval = schemeColor.getAlphaArray(0).getVal();
alpha = Math.round(255 * aval / 100000f);
}
Color themeColor = _schemeColors.get(colorRef).getColor(alpha);
int lumMod = 100, lumOff = 0;
if (schemeColor.sizeOfLumModArray() > 0) {
lumMod = schemeColor.getLumModArray(0).getVal() / 1000;
}
if (schemeColor.sizeOfLumOffArray() > 0) {
lumOff = schemeColor.getLumOffArray(0).getVal() / 1000;
}
if(schemeColor.sizeOfShadeArray() > 0) {
lumMod = schemeColor.getShadeArray(0).getVal() / 1000;
}
Color color = modulateLuminanace(themeColor, lumMod, lumOff);
if(schemeColor.sizeOfTintArray() > 0) {
float tint = schemeColor.getTintArray(0).getVal() / 100000f;
int red = Math.round(tint * themeColor.getRed() + (1 - tint) * 255);
int green = Math.round(tint * themeColor.getGreen() + (1 - tint) * 255);
int blue = Math.round(tint * themeColor.getBlue() + (1 - tint) * 255);
color = new Color(red, green, blue);
}
return color;
}
/**
* TODO get rid of code duplication. Re-write to use xpath instead of beans
*/
Color getPresetColor(CTPresetColor presetColor){
String colorName = presetColor.getVal().toString();
Color color;
try {
color = (Color)Color.class.getField(colorName).get(null);
} catch (Exception e){
color = Color.black;
}
if(presetColor.sizeOfAlphaArray() > 0){
int aval = presetColor.getAlphaArray(0).getVal();
int alpha = Math.round(255 * aval / 100000f);
color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
}
int lumMod = 100, lumOff = 0;
if (presetColor.sizeOfLumModArray() > 0) {
lumMod = presetColor.getLumModArray(0).getVal() / 1000;
}
if (presetColor.sizeOfLumOffArray() > 0) {
lumOff = presetColor.getLumOffArray(0).getVal() / 1000;
}
if(presetColor.sizeOfShadeArray() > 0) {
lumMod = presetColor.getShadeArray(0).getVal() / 1000;
}
color = modulateLuminanace(color, lumMod, lumOff);
if(presetColor.sizeOfTintArray() > 0) {
float tint = presetColor.getTintArray(0).getVal() / 100000f;
int red = Math.round(tint * color.getRed() + (1 - tint) * 255);
int green = Math.round(tint * color.getGreen() + (1 - tint) * 255);
int blue = Math.round(tint * color.getBlue() + (1 - tint) * 255);
color = new Color(red, green, blue);
}
return color;
}
public Color brighter(Color color, double tint) {
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
/* From 2D group:
* 1. black.brighter() should return grey
* 2. applying brighter to blue will always return blue, brighter
* 3. non pure color (non zero rgb) will eventually return white
*/
int i = (int)(1.0/(1.0-tint));
if ( r == 0 && g == 0 && b == 0) {
return new Color(i, i, i);
}
if ( r > 0 && r < i ) r = i;
if ( g > 0 && g < i ) g = i;
if ( b > 0 && b < i ) b = i;
return new Color(Math.min((int)(r/tint), 255),
Math.min((int)(g/tint), 255),
Math.min((int)(b/tint), 255));
}
Color getSrgbColor(CTSRgbColor srgb){
byte[] val = srgb.getVal();
int alpha = 0xFF;
if(srgb.sizeOfAlphaArray() > 0){
int aval = srgb.getAlphaArray(0).getVal();
alpha = Math.round(255 * aval / 100000f);
}
return new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2], alpha);
}
Color getSolidFillColor(CTSolidColorFillProperties solidFill){
Color color;
if (solidFill.isSetSrgbClr()) {
color = getSrgbColor(solidFill.getSrgbClr());
} else if (solidFill.isSetSchemeClr()) {
color = getSchemeColor(solidFill.getSchemeClr());
} else {
// TODO support other types
color = Color.black;
}
return color;
}
Color getColor(CTColor solidFill){
Color color;
if (solidFill.isSetSrgbClr()) {
color = getSrgbColor(solidFill.getSrgbClr());
} else if (solidFill.isSetSchemeClr()) {
color = getSchemeColor(solidFill.getSchemeClr());
} else {
// TODO support other types
color = Color.black;
}
return color;
}
/**
* While developing only!
*/
@ -247,26 +111,6 @@ public class XSLFTheme extends POIXMLDocumentPart {
out.close();
}
public static Color modulateLuminanace(Color c, int lumMod, int lumOff) {
Color color;
if (lumOff > 0) {
color = new Color(
(int) (Math.round((255 - c.getRed()) * (100.0 - lumMod) / 100.0 + c.getRed())),
(int) (Math.round((255 - c.getGreen()) * lumOff / 100.0 + c.getGreen())),
(int) (Math.round((255 - c.getBlue()) * lumOff / 100.0 + c.getBlue())),
c.getAlpha()
);
} else {
color = new Color(
(int) (Math.round(c.getRed() * lumMod / 100.0)),
(int) (Math.round(c.getGreen() * lumMod / 100.0)),
(int) (Math.round(c.getBlue() * lumMod / 100.0)),
c.getAlpha()
);
}
return color;
}
public String getMajorFont(){
return _theme.getThemeElements().getFontScheme().getMajorFont().getLatin().getTypeface();
}

View File

@ -0,0 +1,50 @@
/*
* ====================================================================
* 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.xslf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.xslf.XSLFTestDataSamples;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
/**
* Date: 10/26/11
*
* @author Yegor Kozlov
*/
public class TestPPTX2PNG extends TestCase {
public void testRender(){
String[] testFiles = {"layouts.pptx", "sample.pptx", "shapes.pptx",
"45541_Header.pptx", "backgrounds.pptx"};
for(String sampleFile : testFiles){
XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument(sampleFile);
Dimension pg = pptx.getPageSize();
for(XSLFSlide slide : pptx.getSlides()){
BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
slide.draw(graphics);
}
}
}
}

View File

@ -0,0 +1,154 @@
/* ====================================================================
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.xslf.usermodel;
import junit.framework.TestCase;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal;
import java.awt.*;
/**
* @author Yegor Kozlov
*/
public class TestXSLFColor extends TestCase {
public void testGetters() {
CTColor xml = CTColor.Factory.newInstance();
CTSRgbColor c = xml.addNewSrgbClr();
c.setVal(new byte[]{(byte)0xFF, 0, 0});
XSLFColor color = new XSLFColor(xml, null);
assertEquals(-1, color.getAlpha());
c.addNewAlpha().setVal(50000);
assertEquals(50, color.getAlpha());
assertEquals(-1, color.getAlphaMod());
c.addNewAlphaMod().setVal(50000);
assertEquals(50, color.getAlphaMod());
assertEquals(-1, color.getAlphaOff());
c.addNewAlphaOff().setVal(50000);
assertEquals(50, color.getAlphaOff());
assertEquals(-1, color.getLumMod());
c.addNewLumMod().setVal(50000);
assertEquals(50, color.getLumMod());
assertEquals(-1, color.getLumOff());
c.addNewLumOff().setVal(50000);
assertEquals(50, color.getLumOff());
assertEquals(-1, color.getSat());
c.addNewSat().setVal(50000);
assertEquals(50, color.getSat());
assertEquals(-1, color.getSatMod());
c.addNewSatMod().setVal(50000);
assertEquals(50, color.getSatMod());
assertEquals(-1, color.getSatOff());
c.addNewSatOff().setVal(50000);
assertEquals(50, color.getSatOff());
assertEquals(-1, color.getRed());
c.addNewRed().setVal(50000);
assertEquals(50, color.getRed());
assertEquals(-1, color.getGreen());
c.addNewGreen().setVal(50000);
assertEquals(50, color.getGreen());
assertEquals(-1, color.getBlue());
c.addNewBlue().setVal(50000);
assertEquals(50, color.getRed());
assertEquals(-1, color.getShade());
c.addNewShade().setVal(50000);
assertEquals(50, color.getShade());
assertEquals(-1, color.getTint());
c.addNewTint().setVal(50000);
assertEquals(50, color.getTint());
}
public void testHSL() {
CTColor xml = CTColor.Factory.newInstance();
CTHslColor c = xml.addNewHslClr();
c.setHue2(14400000);
c.setSat2(100000);
c.setLum2(50000);
XSLFColor color = new XSLFColor(xml, null);
assertEquals(new Color(128, 00, 00), color.getColor());
}
public void testSRgb() {
CTColor xml = CTColor.Factory.newInstance();
xml.addNewSrgbClr().setVal(new byte[]{ (byte)0xFF, (byte)0xFF, 0});
XSLFColor color = new XSLFColor(xml, null);
assertEquals(new Color(0xFF, 0xFF, 0), color.getColor());
}
public void testSchemeColor() {
XMLSlideShow ppt = new XMLSlideShow();
XSLFTheme theme = ppt.createSlide().getTheme();
CTColor xml = CTColor.Factory.newInstance();
xml.addNewSchemeClr().setVal(STSchemeColorVal.ACCENT_2);
XSLFColor color = new XSLFColor(xml, theme);
// accent2 is theme1.xml is <a:srgbClr val="C0504D"/>
assertEquals(Color.decode("0xC0504D"), color.getColor());
xml = CTColor.Factory.newInstance();
xml.addNewSchemeClr().setVal(STSchemeColorVal.LT_1);
color = new XSLFColor(xml, theme);
// <a:sysClr val="window" lastClr="FFFFFF"/>
assertEquals(Color.decode("0xFFFFFF"), color.getColor());
xml = CTColor.Factory.newInstance();
xml.addNewSchemeClr().setVal(STSchemeColorVal.DK_1);
color = new XSLFColor(xml, theme);
// <a:sysClr val="windowText" lastClr="000000"/>
assertEquals(Color.decode("0x000000"), color.getColor());
}
public void testPresetColor() {
CTColor xml = CTColor.Factory.newInstance();
xml.addNewPrstClr().setVal(STPresetColorVal.AQUAMARINE);
XSLFColor color = new XSLFColor(xml, null);
assertEquals(new Color(127, 255, 212), color.getColor());
for(String colorName : XSLFColor.presetColors.keySet()){
xml = CTColor.Factory.newInstance();
STPresetColorVal.Enum val = STPresetColorVal.Enum.forString(colorName);
assertNotNull(colorName, val);
xml.addNewPrstClr().setVal(val);
color = new XSLFColor(xml, null);
assertEquals(XSLFColor.presetColors.get(colorName), color.getColor());
}
}
}

View File

@ -143,13 +143,13 @@ public class TestXSLFSimpleShape extends TestCase {
assertEquals(2.0, s.getLineWidth());
assertEquals(LineCap.FLAT, s.getLineCap());
// YK: calculated color is slightly different from PowerPoint
assertEquals(new Color(40, 65, 95), s.getLineColor());
assertEquals(new Color(39, 64, 94), s.getLineColor());
}
XSLFSimpleShape s0 = (XSLFSimpleShape) shapes[0];
// fill is not set
assertNull(s0.getSpPr().getSolidFill());
assertEquals(slide6.getTheme().getColor("accent1").getColor(), s0.getFillColor());
//assertEquals(slide6.getTheme().getColor("accent1").getColor(), s0.getFillColor());
assertEquals(new Color(79, 129, 189), s0.getFillColor());
// lighter 80%

View File

@ -33,7 +33,7 @@ public class TestXSLFTheme extends TestCase {
assertNotNull(theme);
assertEquals("Office Theme", theme.getName());
XSLFColor accent1 = theme.getColor("accent1");
assertNotNull(accent1);
//XSLFColor accent1 = theme.getColor("accent1");
//assertNotNull(accent1);
}
}

View File

@ -138,7 +138,7 @@ public class TestXWPFPictureData extends TestCase {
for (XWPFRun run : paragraph.getRuns()) {
for (XWPFPicture picture : run.getEmbeddedPictures()) {
if (paragraph.getDocument() != null) {
System.out.println(picture.getCTPicture());
//System.out.println(picture.getCTPicture());
XWPFPictureData data = picture.getPictureData();
if(data != null) System.out.println(data.getFileName());
}

Binary file not shown.