KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfShading


1 /*
2  * Copyright 2002 Paulo Soares
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47 package com.lowagie.text.pdf;
48
49 import java.awt.Color JavaDoc;
50 import java.io.IOException JavaDoc;
51 /** Implements the shading dictionary (or stream).
52  *
53  * @author Paulo Soares (psoares@consiste.pt)
54  */

55 public class PdfShading {
56
57     protected PdfDictionary shading;
58     
59     protected PdfWriter writer;
60     
61     protected int shadingType;
62     
63     protected ColorDetails colorDetails;
64     
65     protected PdfName shadingName;
66     
67     protected PdfIndirectReference shadingReference;
68     
69     private Color JavaDoc cspace;
70     
71     /** Holds value of property bBox. */
72     protected float[] bBox;
73     
74     /** Holds value of property antiAlias. */
75     protected boolean antiAlias = false;
76     
77     /** Creates new PdfShading */
78     protected PdfShading(PdfWriter writer) {
79         this.writer = writer;
80     }
81     
82     protected void setColorSpace(Color JavaDoc color) {
83         cspace = color;
84         int type = ExtendedColor.getType(color);
85         PdfObject colorSpace = null;
86         switch (type) {
87             case ExtendedColor.TYPE_GRAY: {
88                 colorSpace = PdfName.DEVICEGRAY;
89                 break;
90             }
91             case ExtendedColor.TYPE_CMYK: {
92                 colorSpace = PdfName.DEVICECMYK;
93                 break;
94             }
95             case ExtendedColor.TYPE_SEPARATION: {
96                 SpotColor spot = (SpotColor)color;
97                 colorDetails = writer.addSimple(spot.getPdfSpotColor());
98                 colorSpace = colorDetails.getIndirectReference();
99                 break;
100             }
101             case ExtendedColor.TYPE_PATTERN:
102             case ExtendedColor.TYPE_SHADING: {
103                 throwColorSpaceError();
104             }
105             default:
106                 colorSpace = PdfName.DEVICERGB;
107                 break;
108         }
109         shading.put(PdfName.COLORSPACE, colorSpace);
110     }
111     
112     public Color JavaDoc getColorSpace() {
113         return cspace;
114     }
115     
116     public static void throwColorSpaceError() {
117         throw new IllegalArgumentException JavaDoc("A tiling or shading pattern cannot be used as a color space in a shading pattern");
118     }
119     
120     public static void checkCompatibleColors(Color JavaDoc c1, Color JavaDoc c2) {
121         int type1 = ExtendedColor.getType(c1);
122         int type2 = ExtendedColor.getType(c2);
123         if (type1 != type2)
124             throw new IllegalArgumentException JavaDoc("Both colors must be of the same type.");
125         if (type1 == ExtendedColor.TYPE_SEPARATION && ((SpotColor)c1).getPdfSpotColor() != ((SpotColor)c2).getPdfSpotColor())
126             throw new IllegalArgumentException JavaDoc("The spot color must be the same, only the tint can vary.");
127         if (type1 == ExtendedColor.TYPE_PATTERN || type1 == ExtendedColor.TYPE_SHADING)
128             throwColorSpaceError();
129     }
130     
131     public static float[] getColorArray(Color JavaDoc color) {
132         int type = ExtendedColor.getType(color);
133         switch (type) {
134             case ExtendedColor.TYPE_GRAY: {
135                 return new float[]{((GrayColor)color).getGray()};
136             }
137             case ExtendedColor.TYPE_CMYK: {
138                 CMYKColor cmyk = (CMYKColor)color;
139                 return new float[]{cmyk.getCyan(), cmyk.getMagenta(), cmyk.getYellow(), cmyk.getBlack()};
140             }
141             case ExtendedColor.TYPE_SEPARATION: {
142                 return new float[]{((SpotColor)color).getTint()};
143             }
144             case ExtendedColor.TYPE_RGB: {
145                 return new float[]{color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f};
146             }
147         }
148         throwColorSpaceError();
149         return null;
150     }
151
152     public static PdfShading type1(PdfWriter writer, Color JavaDoc colorSpace, float domain[], float tMatrix[], PdfFunction function) {
153         PdfShading sp = new PdfShading(writer);
154         sp.shading = new PdfDictionary();
155         sp.shadingType = 1;
156         sp.shading.put(PdfName.SHADINGTYPE, new PdfNumber(sp.shadingType));
157         sp.setColorSpace(colorSpace);
158         if (domain != null)
159             sp.shading.put(PdfName.DOMAIN, new PdfArray(domain));
160         if (tMatrix != null)
161             sp.shading.put(PdfName.MATRIX, new PdfArray(tMatrix));
162         sp.shading.put(PdfName.FUNCTION, function.getReference());
163         return sp;
164     }
165     
166     public static PdfShading type2(PdfWriter writer, Color JavaDoc colorSpace, float coords[], float domain[], PdfFunction function, boolean extend[]) {
167         PdfShading sp = new PdfShading(writer);
168         sp.shading = new PdfDictionary();
169         sp.shadingType = 2;
170         sp.shading.put(PdfName.SHADINGTYPE, new PdfNumber(sp.shadingType));
171         sp.setColorSpace(colorSpace);
172         sp.shading.put(PdfName.COORDS, new PdfArray(coords));
173         if (domain != null)
174             sp.shading.put(PdfName.DOMAIN, new PdfArray(domain));
175         sp.shading.put(PdfName.FUNCTION, function.getReference());
176         if (extend != null && (extend[0] || extend[1])) {
177             PdfArray array = new PdfArray(extend[0] ? PdfBoolean.PDFTRUE : PdfBoolean.PDFFALSE);
178             array.add(extend[1] ? PdfBoolean.PDFTRUE : PdfBoolean.PDFFALSE);
179             sp.shading.put(PdfName.EXTEND, array);
180         }
181         return sp;
182     }
183
184     public static PdfShading type3(PdfWriter writer, Color JavaDoc colorSpace, float coords[], float domain[], PdfFunction function, boolean extend[]) {
185         PdfShading sp = type2(writer, colorSpace, coords, domain, function, extend);
186         sp.shadingType = 3;
187         sp.shading.put(PdfName.SHADINGTYPE, new PdfNumber(sp.shadingType));
188         return sp;
189     }
190     
191     public static PdfShading simpleAxial(PdfWriter writer, float x0, float y0, float x1, float y1, Color JavaDoc startColor, Color JavaDoc endColor, boolean extendStart, boolean extendEnd) {
192         checkCompatibleColors(startColor, endColor);
193         PdfFunction function = PdfFunction.type2(writer, new float[]{0, 1}, null, getColorArray(startColor),
194             getColorArray(endColor), 1);
195         return type2(writer, startColor, new float[]{x0, y0, x1, y1}, null, function, new boolean[]{extendStart, extendEnd});
196     }
197     
198     public static PdfShading simpleAxial(PdfWriter writer, float x0, float y0, float x1, float y1, Color JavaDoc startColor, Color JavaDoc endColor) {
199         return simpleAxial(writer, x0, y0, x1, y1, startColor, endColor, true, true);
200     }
201     
202     public static PdfShading simpleRadial(PdfWriter writer, float x0, float y0, float r0, float x1, float y1, float r1, Color JavaDoc startColor, Color JavaDoc endColor, boolean extendStart, boolean extendEnd) {
203         checkCompatibleColors(startColor, endColor);
204         PdfFunction function = PdfFunction.type2(writer, new float[]{0, 1}, null, getColorArray(startColor),
205             getColorArray(endColor), 1);
206         return type3(writer, startColor, new float[]{x0, y0, r0, x1, y1, r1}, null, function, new boolean[]{extendStart, extendEnd});
207     }
208
209     public static PdfShading simpleRadial(PdfWriter writer, float x0, float y0, float r0, float x1, float y1, float r1, Color JavaDoc startColor, Color JavaDoc endColor) {
210         return simpleRadial(writer, x0, y0, r0, x1, y1, r1, startColor, endColor, true, true);
211     }
212
213     PdfName getShadingName() {
214         return shadingName;
215     }
216     
217     PdfIndirectReference getShadingReference() {
218         if (shadingReference == null)
219             shadingReference = writer.getPdfIndirectReference();
220         return shadingReference;
221     }
222     
223     void setName(int number) {
224         shadingName = new PdfName("Sh" + number);
225     }
226     
227     void addToBody() throws IOException JavaDoc {
228         if (bBox != null)
229             shading.put(PdfName.BBOX, new PdfArray(bBox));
230         if (antiAlias)
231             shading.put(PdfName.ANTIALIAS, PdfBoolean.PDFTRUE);
232         writer.addToBody(shading, getShadingReference());
233     }
234     
235     PdfWriter getWriter() {
236         return writer;
237     }
238     
239     ColorDetails getColorDetails() {
240         return colorDetails;
241     }
242     
243     public float[] getBBox() {
244         return bBox;
245     }
246     
247     public void setBBox(float[] bBox) {
248         if (bBox.length != 4)
249             throw new IllegalArgumentException JavaDoc("BBox must be a 4 element array.");
250         this.bBox = bBox;
251     }
252     
253     public boolean isAntiAlias() {
254         return antiAlias;
255     }
256     
257     public void setAntiAlias(boolean antiAlias) {
258         this.antiAlias = antiAlias;
259     }
260     
261 }
262
Popular Tags