From: Andreas Beeker Date: Mon, 14 Mar 2016 00:42:59 +0000 (+0000) Subject: HSLF: Add support for system colors X-Git-Tag: REL_3_15_BETA2~448 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fef2ce53cdc91280e89726e84affc634004e32e6;p=poi.git HSLF: Add support for system colors git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734865 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/ddf/EscherColorRef.java b/src/java/org/apache/poi/ddf/EscherColorRef.java index dd2626b11e..d48c5de256 100644 --- a/src/java/org/apache/poi/ddf/EscherColorRef.java +++ b/src/java/org/apache/poi/ddf/EscherColorRef.java @@ -243,7 +243,7 @@ public class EscherColorRef { */ public SysIndexProcedure getSysIndexProcedure() { if (!hasSysIndexFlag()) return null; - int val = FLAG_RED.getValue(colorRef); + int val = FLAG_GREEN.getValue(colorRef); for (SysIndexProcedure sip : SysIndexProcedure.values()) { if (sip == SysIndexProcedure.INVERT_AFTER || sip == SysIndexProcedure.INVERT_HIGHBIT_AFTER) continue; if (sip.mask.isSet(val)) return sip; @@ -277,7 +277,19 @@ public class EscherColorRef { * @return index of current palette (color) or -1 if {@link #hasPaletteIndexFlag()} is {@code false} */ public int getPaletteIndex() { - if (!hasPaletteIndexFlag()) return -1; - return (FLAG_GREEN.getValue(colorRef) << 8) & FLAG_RED.getValue(colorRef); + return (hasPaletteIndexFlag()) ? getIndex() : -1; + } + + /** + * @return index of system color table or -1 if {@link #hasSysIndexFlag()} is {@code false} + * + * @see org.apache.poi.sl.usermodel.PresetColor + */ + public int getSysIndex() { + return (hasSysIndexFlag()) ? getIndex() : -1; + } + + private int getIndex() { + return (FLAG_GREEN.getValue(colorRef) << 8) | FLAG_RED.getValue(colorRef); } } diff --git a/src/java/org/apache/poi/sl/draw/DrawTextShape.java b/src/java/org/apache/poi/sl/draw/DrawTextShape.java index 648dd48cec..24944c83b0 100644 --- a/src/java/org/apache/poi/sl/draw/DrawTextShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawTextShape.java @@ -82,7 +82,7 @@ public class DrawTextShape extends DrawSimpleShape { } // first dry-run to calculate the total height of the text - double textHeight = s.getTextHeight(); + double textHeight = getTextHeight(graphics); switch (s.getVerticalAlignment()){ default: @@ -170,11 +170,27 @@ public class DrawTextShape extends DrawSimpleShape { /** * Compute the cumulative height occupied by the text + * + * @return the height in points */ - public double getTextHeight(){ + public double getTextHeight() { + return getTextHeight(null); + } + + /** + * Compute the cumulative height occupied by the text + * + * @param oldGraphics the graphics context, which properties are to be copied, may be null + * @return the height in points + */ + protected double getTextHeight(Graphics2D oldGraphics) { // dry-run in a 1x1 image and return the vertical advance BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); + if (oldGraphics != null) { + graphics.addRenderingHints(oldGraphics.getRenderingHints()); + graphics.setTransform(oldGraphics.getTransform()); + } DrawFactory.getInstance(graphics).fixFonts(graphics); return drawParagraphs(graphics, 0, 0); } diff --git a/src/java/org/apache/poi/sl/draw/PathGradientPaint.java b/src/java/org/apache/poi/sl/draw/PathGradientPaint.java index 292ee3a7e7..cf4a89147c 100644 --- a/src/java/org/apache/poi/sl/draw/PathGradientPaint.java +++ b/src/java/org/apache/poi/sl/draw/PathGradientPaint.java @@ -45,8 +45,10 @@ class PathGradientPaint implements Paint { // determine transparency boolean opaque = true; - for (int i = 0; i < colors.length; i++){ - opaque = opaque && (colors[i].getAlpha() == 0xff); + for (Color c : colors) { + if (c != null) { + opaque = opaque && (c.getAlpha() == 0xff); + } } this.transparency = opaque ? OPAQUE : TRANSLUCENT; } diff --git a/src/java/org/apache/poi/sl/usermodel/PresetColor.java b/src/java/org/apache/poi/sl/usermodel/PresetColor.java new file mode 100644 index 0000000000..0860e7dd12 --- /dev/null +++ b/src/java/org/apache/poi/sl/usermodel/PresetColor.java @@ -0,0 +1,277 @@ +/* + * ==================================================================== + * 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.sl.usermodel; + +import java.awt.Color; +import java.util.HashMap; +import java.util.Map; + +/** + * Preset colors defined in DrawingML aka known/system colors + * + * @see KnownColor Enumeration + * @see Colors Class + */ +public enum PresetColor { + // the order of this enum can be found in the definition of .net System.Drawing.KnownColor enumeration + // or by running the the program in the linked documentation + + // default colors for theme-depending colors taken from ... (last post): + // https://social.technet.microsoft.com/Forums/windows/en-US/ac76cc56-6ff2-4778-b260-8141d7170a3b/windows-7-highlight-text-color-or-selected-text-color-in-aero + + /** The system-defined color of the active window's border. */ + ActiveBorder (0xffb4b4b4, 1, null), + /** The system-defined color of the background of the active window's title bar. */ + ActiveCaption (0xff99b4d1, 2, null), + /** The system-defined color of the text in the active window's title bar. */ + ActiveCaptionText (0xff000000, 3, null), + /** The application workspace is the area in a multiple-document view that is not being occupied by documents. */ + AppWorkspace (0xffababab, 4, null), + /** The system-defined face color of a 3-D element. */ + Control (0xfff0f0f0, 5, null), + /** The system-defined shadow color of a 3-D element. The shadow color is applied to parts of a 3-D element that face away from the light source. */ + ControlDark (0xff696969, 6, null), + /** The system-defined color that is the dark shadow color of a 3-D element. The dark shadow color is applied to the parts of a 3-D element that are the darkest color. */ + ControlDarkDark (0xff000000, 7, null), + /** The system-defined color that is the light color of a 3-D element. The light color is applied to parts of a 3-D element that face the light source. */ + ControlLight (0xffe3e3e3, 8, null), + /** The system-defined highlight color of a 3-D element. The highlight color is applied to the parts of a 3-D element that are the lightest color. */ + ControlLightLight (0xffe3e3e3, 9, null), + /** The system-defined color of text in a 3-D element. */ + ControlText (0xff000000, 10, null), + /** The system-defined color of the desktop. */ + Desktop (0xff000000, 11, null), + /** The system-defined color of dimmed text. Items in a list that are disabled are displayed in dimmed text. */ + GrayText (0xff6d6d6d, 12, null), + /** The system-defined color of the background of selected items. This includes selected menu items as well as selected text. */ + Highlight (0xff3399ff, 13, null), + /** The system-defined color of the text of selected items. */ + HighlightText (0xffffffff, 14, null), + /** The system-defined color used to designate a hot-tracked item. Single-clicking a hot-tracked item executes the item. */ + HotTrack (0xff0066cc, 15, null), + /** The system-defined color of an inactive window's border. */ + InactiveBorder (0xfff4f7fc, 16, null), + /** The system-defined color of the background of an inactive window's title bar. */ + InactiveCaption (0xffbfcddb, 17, null), + /** The system-defined color of the text in an inactive window's title bar. */ + InactiveCaptionText (0xff000000, 18, null), + /** The system-defined color of the background of a ToolTip. */ + Info (0xffffffe1, 19, null), + /** The system-defined color of the text of a ToolTip. */ + InfoText (0xff000000, 20, null), + /** The system-defined color of a menu's background. */ + Menu (0xfff0f0f0, 21, null), + /** The system-defined color of a menu's text. */ + MenuText (0xff000000, 22, null), + /** The system-defined color of the background of a scroll bar. */ + ScrollBar (0xffc8c8c8, 23, null), + /** The system-defined color of the background in the client area of a window. */ + Window (0xffffffff, 24, null), + /** The system-defined color of a window frame. */ + WindowFrame (0xff646464, 25, null), + /** The system-defined color of the text in the client area of a window. */ + WindowText (0xff000000, 26, null), + Transparent (0x00ffffff, 27, null), + AliceBlue (0xfff0f8ff, 28, "aliceBlue"), + AntiqueWhite (0xfffaebd7, 29, "antiqueWhite"), + Aqua (0xff00ffff, 30, "aqua"), + Aquamarine (0xff7fffd4, 31, "aquamarine"), + Azure (0xfff0ffff, 32, "azure"), + Beige (0xfff5f5dc, 33, "beige"), + Bisque (0xffffe4c4, 34, "bisque"), + Black (0xff000000, 35, "black"), + BlanchedAlmond (0xffffebcd, 36, "blanchedAlmond"), + Blue (0xff0000ff, 37, "blue"), + BlueViolet (0xff8a2be2, 38, "blueViolet"), + Brown (0xffa52a2a, 39, "brown"), + BurlyWood (0xffdeb887, 40, "burlyWood"), + CadetBlue (0xff5f9ea0, 41, "cadetBlue"), + Chartreuse (0xff7fff00, 42, "chartreuse"), + Chocolate (0xffd2691e, 43, "chocolate"), + Coral (0xffff7f50, 44, "coral"), + CornflowerBlue (0xff6495ed, 45, "cornflowerBlue"), + Cornsilk (0xfffff8dc, 46, "cornsilk"), + Crimson (0xffdc143c, 47, "crimson"), + Cyan (0xff00ffff, 48, "cyan"), + DarkBlue (0xff00008b, 49, "dkBlue"), + DarkCyan (0xff008b8b, 50, "dkCyan"), + DarkGoldenrod (0xffb8860b, 51, "dkGoldenrod"), + DarkGray (0xffa9a9a9, 52, "dkGray"), + DarkGreen (0xff006400, 53, "dkGreen"), + DarkKhaki (0xffbdb76b, 54, "dkKhaki"), + DarkMagenta (0xff8b008b, 55, "dkMagenta"), + DarkOliveGreen (0xff556b2f, 56, "dkOliveGreen"), + DarkOrange (0xffff8c00, 57, "dkOrange"), + DarkOrchid (0xff9932cc, 58, "dkOrchid"), + DarkRed (0xff8b0000, 59, "dkRed"), + DarkSalmon (0xffe9967a, 60, "dkSalmon"), + DarkSeaGreen (0xff8fbc8b, 61, "dkSeaGreen"), + DarkSlateBlue (0xff483d8b, 62, "dkSlateBlue"), + DarkSlateGray (0xff2f4f4f, 63, "dkSlateGray"), + DarkTurquoise (0xff00ced1, 64, "dkTurquoise"), + DarkViolet (0xff9400d3, 65, "dkViolet"), + DeepPink (0xffff1493, 66, "deepPink"), + DeepSkyBlue (0xff00bfff, 67, "deepSkyBlue"), + DimGray (0xff696969, 68, "dimGray"), + DodgerBlue (0xff1e90ff, 69, "dodgerBlue"), + Firebrick (0xffb22222, 70, "firebrick"), + FloralWhite (0xfffffaf0, 71, "floralWhite"), + ForestGreen (0xff228b22, 72, "forestGreen"), + Fuchsia (0xffff00ff, 73, "fuchsia"), + Gainsboro (0xffdcdcdc, 74, "gainsboro"), + GhostWhite (0xfff8f8ff, 75, "ghostWhite"), + Gold (0xffffd700, 76, "gold"), + Goldenrod (0xffdaa520, 77, "goldenrod"), + Gray (0xff808080, 78, "gray"), + Green (0xff008000, 79, "green"), + GreenYellow (0xffadff2f, 80, "greenYellow"), + Honeydew (0xfff0fff0, 81, "honeydew"), + HotPink (0xffff69b4, 82, "hotPink"), + IndianRed (0xffcd5c5c, 83, "indianRed"), + Indigo (0xff4b0082, 84, "indigo"), + Ivory (0xfffffff0, 85, "ivory"), + Khaki (0xfff0e68c, 86, "khaki"), + Lavender (0xffe6e6fa, 87, "lavender"), + LavenderBlush (0xfffff0f5, 88, "lavenderBlush"), + LawnGreen (0xff7cfc00, 89, "lawnGreen"), + LemonChiffon (0xfffffacd, 90, "lemonChiffon"), + LightBlue (0xffadd8e6, 91, "ltBlue"), + LightCoral (0xfff08080, 92, "ltCoral"), + LightCyan (0xffe0ffff, 93, "ltCyan"), + LightGoldenrodYellow (0xfffafa78, 94, "ltGoldenrodYellow"), + LightGray (0xffd3d3d3, 95, "ltGray"), + LightGreen (0xff90ee90, 96, "ltGreen"), + LightPink (0xffffb6c1, 97, "ltPink"), + LightSalmon (0xffffa07a, 98, "ltSalmon"), + LightSeaGreen (0xff20b2aa, 99, "ltSeaGreen"), + LightSkyBlue (0xff87cefa, 100, "ltSkyBlue"), + LightSlateGray (0xff778899, 101, "ltSlateGray"), + LightSteelBlue (0xffb0c4de, 102, "ltSteelBlue"), + LightYellow (0xffffffe0, 103, "ltYellow"), + Lime (0xff00ff00, 104, "lime"), + LimeGreen (0xff32cd32, 105, "limeGreen"), + Linen (0xfffaf0e6, 106, "linen"), + Magenta (0xffff00ff, 107, "magenta"), + Maroon (0xff800000, 108, "maroon"), + MediumAquamarine (0xff66cdaa, 109, "medAquamarine"), + MediumBlue (0xff0000cd, 110, "medBlue"), + MediumOrchid (0xffba55d3, 111, "medOrchid"), + MediumPurple (0xff9370db, 112, "medPurple"), + MediumSeaGreen (0xff3cb371, 113, "medSeaGreen"), + MediumSlateBlue (0xff7b68ee, 114, "medSlateBlue"), + MediumSpringGreen (0xff00fa9a, 115, "medSpringGreen"), + MediumTurquoise (0xff48d1cc, 116, "medTurquoise"), + MediumVioletRed (0xffc71585, 117, "medVioletRed"), + MidnightBlue (0xff191970, 118, "midnightBlue"), + MintCream (0xfff5fffa, 119, "mintCream"), + MistyRose (0xffffe4e1, 120, "mistyRose"), + Moccasin (0xffffe4b5, 121, "moccasin"), + NavajoWhite (0xffffdead, 122, "navajoWhite"), + Navy (0xff000080, 123, "navy"), + OldLace (0xfffdf5e6, 124, "oldLace"), + Olive (0xff808000, 125, "olive"), + OliveDrab (0xff6b8e23, 126, "oliveDrab"), + Orange (0xffffa500, 127, "orange"), + OrangeRed (0xffff4500, 128, "orangeRed"), + Orchid (0xffda70d6, 129, "orchid"), + PaleGoldenrod (0xffeee8aa, 130, "paleGoldenrod"), + PaleGreen (0xff98fb98, 131, "paleGreen"), + PaleTurquoise (0xffafeeee, 132, "paleTurquoise"), + PaleVioletRed (0xffdb7093, 133, "paleVioletRed"), + PapayaWhip (0xffffefd5, 134, "papayaWhip"), + PeachPuff (0xffffdab9, 135, "peachPuff"), + Peru (0xffcd853f, 136, "peru"), + Pink (0xffffc0cb, 137, "pink"), + Plum (0xffdda0dd, 138, "plum"), + PowderBlue (0xffb0e0e6, 139, "powderBlue"), + Purple (0xff800080, 140, "purple"), + Red (0xffff0000, 141, "red"), + RosyBrown (0xffbc8f8f, 142, "rosyBrown"), + RoyalBlue (0xff4169e1, 143, "royalBlue"), + SaddleBrown (0xff8b4513, 144, "saddleBrown"), + Salmon (0xfffa8072, 145, "salmon"), + SandyBrown (0xfff4a460, 146, "sandyBrown"), + SeaGreen (0xff2e8b57, 147, "seaGreen"), + SeaShell (0xfffff5ee, 148, "seaShell"), + Sienna (0xffa0522d, 149, "sienna"), + Silver (0xffc0c0c0, 150, "silver"), + SkyBlue (0xff87ceeb, 151, "skyBlue"), + SlateBlue (0xff6a5acd, 152, "slateBlue"), + SlateGray (0xff708090, 153, "slateGray"), + Snow (0xfffffafa, 154, "snow"), + SpringGreen (0xff00ff7f, 155, "springGreen"), + SteelBlue (0xff4682b4, 156, "steelBlue"), + Tan (0xffd2b48c, 157, "tan"), + Teal (0xff008080, 158, "teal"), + Thistle (0xffd8bfd8, 159, "thistle"), + Tomato (0xffff6347, 160, "tomato"), + Turquoise (0xff40e0d0, 161, "turquoise"), + Violet (0xffee82ee, 162, "violet"), + Wheat (0xfff5deb3, 163, "wheat"), + White (0xffffffff, 164, "white"), + WhiteSmoke (0xfff5f5f5, 165, "whiteSmoke"), + Yellow (0xffffff00, 166, "yellow"), + YellowGreen (0xff9acd32, 167, "yellowGreen"), + /** The system-defined face color of a 3-D element. */ + ButtonFace (0xfff0f0f0, 168, null), + /** The system-defined color that is the highlight color of a 3-D element. This color is applied to parts of a 3-D element that face the light source. */ + ButtonHighlight (0xffffffff, 169, null), + /** The system-defined color that is the shadow color of a 3-D element. This color is applied to parts of a 3-D element that face away from the light source. */ + ButtonShadow (0xffa0a0a0, 170, null), + /** The system-defined color of the lightest color in the color gradient of an active window's title bar. */ + GradientActiveCaption (0xffb9d1ea, 171, null), + /** The system-defined color of the lightest color in the color gradient of an inactive window's title bar. */ + GradientInactiveCaption (0xffd7e4f2, 172, null), + /** The system-defined color of the background of a menu bar. */ + MenuBar (0xfff0f0f0, 173, null), + /** The system-defined color used to highlight menu items when the menu appears as a flat menu. */ + MenuHighlight (0xff3399ff, 174, null) + ; + + public Color color; + public int nativeId; + public String ooxmlId; + + PresetColor(Integer rgb, int nativeId, String ooxmlId) { + this.color = (rgb == null) ? null : new Color(rgb, true); + this.nativeId = nativeId; + this.ooxmlId = ooxmlId; + } + + private static final Map lookupOoxmlId; + + static { + lookupOoxmlId = new HashMap(); + for(PresetColor pc : PresetColor.values()) { + if (pc.ooxmlId != null) { + lookupOoxmlId.put(pc.ooxmlId, pc); + } + } + } + + public static PresetColor valueOfOoxmlId(String ooxmlId) { + return lookupOoxmlId.get(ooxmlId); + } + + public static PresetColor valueOfNativeId(int nativeId) { + PresetColor vals[] = values(); + return (0 < nativeId && nativeId <= vals.length) ? vals[nativeId-1] : null; + } +} diff --git a/src/java/org/apache/poi/sl/usermodel/TextShape.java b/src/java/org/apache/poi/sl/usermodel/TextShape.java index cc39a51819..d2f66e52f1 100644 --- a/src/java/org/apache/poi/sl/usermodel/TextShape.java +++ b/src/java/org/apache/poi/sl/usermodel/TextShape.java @@ -17,6 +17,7 @@ package org.apache.poi.sl.usermodel; +import java.awt.Graphics2D; import java.util.List; public interface TextShape< @@ -173,7 +174,7 @@ public interface TextShape< * Compute the cumulative height occupied by the text */ double getTextHeight(); - + /** * Returns the type of vertical alignment for the text. * diff --git a/src/java/org/apache/poi/util/Units.java b/src/java/org/apache/poi/util/Units.java index 496ca132d3..6649c1f860 100644 --- a/src/java/org/apache/poi/util/Units.java +++ b/src/java/org/apache/poi/util/Units.java @@ -96,8 +96,10 @@ public class Units { * @see [MS-OSHARED] - 2.2.1.6 FixedPoint */ public static int doubleToFixedPoint(double floatPoint) { - int i = (int)Math.floor(floatPoint); - int f = (int)((floatPoint % 1d)*65536d); + double fractionalPart = floatPoint % 1d; + double integralPart = floatPoint - fractionalPart; + int i = (int)Math.floor(integralPart); + int f = (int)Math.rint(fractionalPart*65536d); int fixedPoint = (i << 16) | (f & 0xFFFF); return fixedPoint; } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFColor.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFColor.java index 67a41faec2..f30fb5efd2 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFColor.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFColor.java @@ -19,11 +19,10 @@ package org.apache.poi.xslf.usermodel; import java.awt.Color; -import java.util.HashMap; -import java.util.Map; import org.apache.poi.sl.draw.DrawPaint; import org.apache.poi.sl.usermodel.ColorStyle; +import org.apache.poi.sl.usermodel.PresetColor; import org.apache.poi.util.Beta; import org.apache.poi.util.Internal; import org.apache.xmlbeans.XmlObject; @@ -126,7 +125,10 @@ public class XSLFColor { } else if (ch instanceof CTPresetColor) { CTPresetColor prst = (CTPresetColor)ch; String colorName = prst.getVal().toString(); - color = presetColors.get(colorName); + PresetColor pc = PresetColor.valueOfOoxmlId(colorName); + if (pc != null) { + color = pc.color; + } } else if (ch instanceof CTSchemeColor) { CTSchemeColor schemeColor = (CTSchemeColor)ch; String colorRef = schemeColor.getVal().toString(); @@ -402,153 +404,4 @@ public class XSLFColor { public int getTint(){ return getPercentageValue("tint"); } - - - /** - * Preset colors defined in DrawingML - */ - static final Map presetColors; - - static { - presetColors = new HashMap(); - 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)); - } } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFColor.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFColor.java index b335983d09..d789157789 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFColor.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFColor.java @@ -16,16 +16,25 @@ ==================================================================== */ package org.apache.poi.xslf.usermodel; -import junit.framework.TestCase; -import org.openxmlformats.schemas.drawingml.x2006.main.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.awt.Color; +import java.io.IOException; -/** - * @author Yegor Kozlov - */ -public class TestXSLFColor extends TestCase { +import org.apache.poi.sl.usermodel.PresetColor; +import org.junit.Test; +import org.openxmlformats.schemas.drawingml.x2006.main.CTColor; +import org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor; +import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor; +import org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor; +import org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal; +import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal; +import org.openxmlformats.schemas.drawingml.x2006.main.STSystemColorVal; +public class TestXSLFColor { + + @Test public void testGetters() { CTColor xml = CTColor.Factory.newInstance(); CTSRgbColor c = xml.addNewSrgbClr(); @@ -86,6 +95,7 @@ public class TestXSLFColor extends TestCase { assertEquals(50, color.getTint()); } + @Test public void testHSL() { CTColor xml = CTColor.Factory.newInstance(); CTHslColor c = xml.addNewHslClr(); @@ -97,6 +107,7 @@ public class TestXSLFColor extends TestCase { assertEquals(new Color(128, 00, 00), color.getColor()); } + @Test public void testSRgb() { CTColor xml = CTColor.Factory.newInstance(); xml.addNewSrgbClr().setVal(new byte[]{ (byte)0xFF, (byte)0xFF, 0}); @@ -105,7 +116,8 @@ public class TestXSLFColor extends TestCase { assertEquals(new Color(0xFF, 0xFF, 0), color.getColor()); } - public void testSchemeColor() { + @Test + public void testSchemeColor() throws IOException { XMLSlideShow ppt = new XMLSlideShow(); XSLFTheme theme = ppt.createSlide().getTheme(); @@ -127,8 +139,11 @@ public class TestXSLFColor extends TestCase { color = new XSLFColor(xml, theme, null); // assertEquals(Color.decode("0x000000"), color.getColor()); + + ppt.close(); } + @Test public void testPresetColor() { CTColor xml = CTColor.Factory.newInstance(); xml.addNewPrstClr().setVal(STPresetColorVal.AQUAMARINE); @@ -136,16 +151,18 @@ public class TestXSLFColor extends TestCase { assertEquals(new Color(127, 255, 212), color.getColor()); - for(String colorName : XSLFColor.presetColors.keySet()){ + for(PresetColor pc : PresetColor.values()) { + if (pc.ooxmlId == null) continue; xml = CTColor.Factory.newInstance(); - STPresetColorVal.Enum val = STPresetColorVal.Enum.forString(colorName); - assertNotNull(colorName, val); + STPresetColorVal.Enum val = STPresetColorVal.Enum.forString(pc.ooxmlId); + assertNotNull(pc.ooxmlId, val); xml.addNewPrstClr().setVal(val); color = new XSLFColor(xml, null, null); - assertEquals(XSLFColor.presetColors.get(colorName), color.getColor()); + assertEquals(pc.color, color.getColor()); } } + @Test public void testSys() { CTColor xml = CTColor.Factory.newInstance(); CTSystemColor sys = xml.addNewSysClr(); @@ -159,5 +176,4 @@ public class TestXSLFColor extends TestCase { color = new XSLFColor(xml, null, null); assertEquals(Color.red, color.getColor()); } - } \ No newline at end of file diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFill.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFill.java index 06e8d00f8c..4d68995480 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFill.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFill.java @@ -300,11 +300,19 @@ public final class HSLFFill { public void setForegroundColor(Color color){ AbstractEscherOptRecord opt = shape.getEscherOptRecord(); if (color == null) { + opt.removeEscherProperty(EscherProperties.FILL__FILLCOLOR); HSLFShape.setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 0x150000); } else { int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB(); HSLFShape.setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, rgb); + int alpha = color.getAlpha(); + if (alpha == 255) { + opt.removeEscherProperty(EscherProperties.FILL__FILLOPACITY); + } else { + int alphaFP = Units.doubleToFixedPoint(alpha/255d); + HSLFShape.setEscherProperty(opt, EscherProperties.FILL__FILLOPACITY, alphaFP); + } HSLFShape.setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 0x150011); } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShape.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShape.java index ff53b50759..51fc33b70f 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShape.java @@ -27,6 +27,8 @@ import org.apache.poi.ddf.AbstractEscherOptRecord; import org.apache.poi.ddf.EscherChildAnchorRecord; import org.apache.poi.ddf.EscherClientAnchorRecord; 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.EscherContainerRecord; import org.apache.poi.ddf.EscherProperties; import org.apache.poi.ddf.EscherProperty; @@ -40,6 +42,7 @@ import org.apache.poi.hslf.record.Record; import org.apache.poi.hslf.record.RecordTypes; 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; @@ -348,6 +351,9 @@ public abstract class HSLFShape implements Shape { EscherColorRef ecr = new EscherColorRef(val); Color 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)); @@ -371,19 +377,121 @@ public abstract class HSLFShape implements Shape { rgb[0] = (schemeColor >> 0) & 0xFF; rgb[1] = (schemeColor >> 8) & 0xFF; rgb[2] = (schemeColor >> 16) & 0xFF; - } else if (fPaletteIndex){ - //TODO - } else if (fPaletteRGB){ + } else if (fPaletteIndex) { //TODO - } else if (fSystemRGB){ + } else if (fPaletteRGB) { //TODO - } else if (fSysIndex){ + } 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 getFill().getForegroundColor(); + } + case LINE_OR_FILL_COLOR: { + Color col = null; + if (this instanceof HSLFSimpleShape) { + col = ((HSLFSimpleShape)this).getLineColor(); + } + if (col == null) { + col = getFill().getForegroundColor(); + } + return col; + } + case LINE_COLOR: { + if (this instanceof HSLFSimpleShape) { + return ((HSLFSimpleShape)this).getLineColor(); + } + 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 getFill().getBackgroundColor(); + } + case LINE_BACKGROUND_COLOR: { + if (this instanceof HSLFSimpleShape) { + return ((HSLFSimpleShape)this).getLineBackgroundColor(); + } + break; + } + case FILL_OR_LINE_COLOR: { + Color col = getFill().getForegroundColor(); + if (col == null && this instanceof HSLFSimpleShape) { + col = ((HSLFSimpleShape)this).getLineColor(); + } + 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 = (int)Math.rint(col.getRed()*FACTOR); + int g = (int)Math.rint(col.getGreen()*FACTOR); + int b = (int)Math.rint(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.rint((0xFF-r)*FACTOR); + g += Math.rint((0xFF-g)*FACTOR); + b += Math.rint((0xFF-b)*FACTOR); + + return new Color(r,g,b); + } + default: + // TODO ... + break; + } + + return col; + } + double getAlpha(short opacityProperty) { AbstractEscherOptRecord opt = getEscherOptRecord(); EscherSimpleProperty op = getEscherProperty(opt, opacityProperty); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java index 10adc71c82..b828886c95 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java @@ -170,6 +170,36 @@ public abstract class HSLFSimpleShape extends HSLFShape implements SimpleShape