001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.model;
019:
020: import org.apache.poi.ddf.*;
021: import org.apache.poi.hslf.record.*;
022: import org.apache.poi.hslf.usermodel.PictureData;
023: import org.apache.poi.hslf.usermodel.SlideShow;
024: import org.apache.poi.hslf.exceptions.HSLFException;
025:
026: import java.awt.*;
027: import java.util.*;
028:
029: /**
030: * Represents functionality provided by the 'Fill Effects' dialog in PowerPoint.
031: *
032: * @author Yegor Kozlov
033: */
034: public class Fill {
035: /**
036: * Fill with a solid color
037: */
038: public static final int FILL_SOLID = 0;
039:
040: /**
041: * Fill with a pattern (bitmap)
042: */
043: public static final int FILL_PATTERN = 1;
044:
045: /**
046: * A texture (pattern with its own color map)
047: */
048: public static final int FILL_TEXTURE = 2;
049:
050: /**
051: * Center a picture in the shape
052: */
053: public static final int FILL_PICTURE = 3;
054:
055: /**
056: * Shade from start to end points
057: */
058: public static final int FILL_SHADE = 4;
059:
060: /**
061: * Shade from bounding rectangle to end point
062: */
063: public static final int FILL_SHADE_CENTER = 5;
064:
065: /**
066: * Shade from shape outline to end point
067: */
068: public static final int FILL_SHADE_SHAPE = 6;
069:
070: /**
071: * Similar to FILL_SHADE, but the fill angle
072: * is additionally scaled by the aspect ratio of
073: * the shape. If shape is square, it is the same as FILL_SHADE
074: */
075: public static final int FILL_SHADE_SCALE = 7;
076:
077: /**
078: * shade to title
079: */
080: public static final int FILL_SHADE_TITLE = 8;
081:
082: /**
083: * Use the background fill color/pattern
084: */
085: public static final int FILL_BACKGROUND = 9;
086:
087: /**
088: * The shape this background applies to
089: */
090: protected Shape shape;
091:
092: /**
093: * Construct a <code>Fill</code> object for a shape.
094: * Fill information will be read from shape's escher properties.
095: *
096: * @param shape the shape this background applies to
097: */
098: public Fill(Shape shape) {
099: this .shape = shape;
100: }
101:
102: /**
103: * Returns fill type.
104: * Must be one of the <code>FILL_*</code> constants defined in this class.
105: *
106: * @return type of fill
107: */
108: public int getFillType() {
109: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
110: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
111: EscherSimpleProperty prop = (EscherSimpleProperty) Shape
112: .getEscherProperty(opt, EscherProperties.FILL__FILLTYPE);
113: return prop == null ? FILL_SOLID : prop.getPropertyValue();
114: }
115:
116: /**
117: * Sets fill type.
118: * Must be one of the <code>FILL_*</code> constants defined in this class.
119: *
120: * @param type type of the fill
121: */
122: public void setFillType(int type) {
123: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
124: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
125: Shape.setEscherProperty(opt, EscherProperties.FILL__FILLTYPE,
126: type);
127: }
128:
129: /**
130: * Foreground color
131: */
132: public Color getForegroundColor() {
133: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
134: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
135: EscherSimpleProperty p1 = (EscherSimpleProperty) Shape
136: .getEscherProperty(opt,
137: EscherProperties.FILL__FILLCOLOR);
138: EscherSimpleProperty p2 = (EscherSimpleProperty) Shape
139: .getEscherProperty(opt,
140: EscherProperties.FILL__NOFILLHITTEST);
141:
142: int p2val = p2 == null ? 0 : p2.getPropertyValue();
143:
144: Color clr = null;
145: if (p1 != null && (p2val & 0x10) != 0) {
146: int rgb = p1.getPropertyValue();
147: clr = shape.getColor(rgb);
148: }
149: return clr;
150: }
151:
152: /**
153: * Foreground color
154: */
155: public void setForegroundColor(Color color) {
156: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
157: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
158: if (color == null) {
159: Shape.setEscherProperty(opt,
160: EscherProperties.FILL__FILLCOLOR, -1);
161: Shape.setEscherProperty(opt,
162: EscherProperties.FILL__NOFILLHITTEST, 0x150010);
163: } else {
164: int rgb = new Color(color.getBlue(), color.getGreen(),
165: color.getRed(), 0).getRGB();
166: Shape.setEscherProperty(opt,
167: EscherProperties.FILL__FILLCOLOR, rgb);
168: Shape.setEscherProperty(opt,
169: EscherProperties.FILL__NOFILLHITTEST, 0x150011);
170: }
171: }
172:
173: /**
174: * Background color
175: */
176: public Color getBackgroundColor() {
177: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
178: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
179: EscherSimpleProperty p1 = (EscherSimpleProperty) Shape
180: .getEscherProperty(opt,
181: EscherProperties.FILL__FILLBACKCOLOR);
182: EscherSimpleProperty p2 = (EscherSimpleProperty) Shape
183: .getEscherProperty(opt,
184: EscherProperties.FILL__NOFILLHITTEST);
185:
186: int p2val = p2 == null ? 0 : p2.getPropertyValue();
187:
188: Color clr = null;
189: if (p1 != null && (p2val & 0x10) != 0) {
190: int rgb = p1.getPropertyValue();
191: clr = shape.getColor(rgb);
192: }
193: return clr;
194: }
195:
196: /**
197: * Background color
198: */
199: public void setBackgroundColor(Color color) {
200: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
201: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
202: if (color == null) {
203: Shape.setEscherProperty(opt,
204: EscherProperties.FILL__FILLBACKCOLOR, -1);
205: } else {
206: int rgb = new Color(color.getBlue(), color.getGreen(),
207: color.getRed(), 0).getRGB();
208: Shape.setEscherProperty(opt,
209: EscherProperties.FILL__FILLBACKCOLOR, rgb);
210: }
211: }
212:
213: /**
214: * <code>PictureData</code> object used in a texture, pattern of picture fill.
215: */
216: public PictureData getPictureData() {
217: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
218: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
219: EscherSimpleProperty p = (EscherSimpleProperty) Shape
220: .getEscherProperty(opt,
221: EscherProperties.FILL__PATTERNTEXTURE);
222: if (p == null)
223: return null;
224:
225: SlideShow ppt = shape.getSheet().getSlideShow();
226: PictureData[] pict = ppt.getPictureData();
227: Document doc = ppt.getDocumentRecord();
228:
229: EscherContainerRecord dggContainer = doc.getPPDrawingGroup()
230: .getDggContainer();
231: EscherContainerRecord bstore = (EscherContainerRecord) Shape
232: .getEscherChild(dggContainer,
233: EscherContainerRecord.BSTORE_CONTAINER);
234:
235: java.util.List lst = bstore.getChildRecords();
236: int idx = p.getPropertyValue();
237: EscherBSERecord bse = (EscherBSERecord) lst.get(idx);
238: for (int i = 0; i < pict.length; i++) {
239: if (pict[i].getOffset() == bse.getOffset()) {
240: return pict[i];
241: }
242: }
243: throw new HSLFException("Picture data not found: \n"
244: + " bse: " + bse + " at " + bse.getOffset());
245:
246: }
247:
248: /**
249: * Assign picture used to fill the underlying shape.
250: *
251: * @param idx 0-based index of the picture added to this ppt by <code>SlideShow.addPicture</code> method.
252: */
253: public void setPictureData(int idx) {
254: EscherOptRecord opt = (EscherOptRecord) Shape.getEscherChild(
255: shape.getSpContainer(), EscherOptRecord.RECORD_ID);
256: Shape
257: .setEscherProperty(
258: opt,
259: (short) (EscherProperties.FILL__PATTERNTEXTURE + 0x4000),
260: idx);
261: }
262:
263: }
|