001/*
002 * $Id: PdfPatternPainter.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045
046import com.itextpdf.text.error_messages.MessageLocalization;
047
048import com.itextpdf.text.DocumentException;
049import com.itextpdf.text.Image;
050import com.itextpdf.text.Rectangle;
051import com.itextpdf.text.BaseColor;
052
053/**
054 * Implements the pattern.
055 */
056
057public final class PdfPatternPainter extends PdfTemplate {
058    
059    float xstep, ystep;
060    boolean stencil = false;
061    BaseColor defaultColor;
062    
063    /**
064     *Creates a <CODE>PdfPattern</CODE>.
065     */
066    
067    private PdfPatternPainter() {
068        super();
069        type = TYPE_PATTERN;
070    }
071    
072    /**
073     * Creates new PdfPattern
074     *
075     * @param wr the <CODE>PdfWriter</CODE>
076     */
077    
078    PdfPatternPainter(PdfWriter wr) {
079        super(wr);
080        type = TYPE_PATTERN;
081    }
082    
083    PdfPatternPainter(PdfWriter wr, BaseColor defaultColor) {
084        this(wr);
085        stencil = true;
086        if (defaultColor == null)
087            this.defaultColor = BaseColor.GRAY;
088        else
089            this.defaultColor = defaultColor;
090    }
091    
092    /**
093     * Sets the horizontal interval of this pattern.
094     *
095     * @param xstep the xstep in horizontal painting
096     */
097    
098    public void setXStep(float xstep) {
099        this.xstep = xstep;
100    }
101    
102    /**
103     * Sets the vertical interval of this pattern.
104     *
105     * @param ystep in vertical painting
106     */
107    
108    public void setYStep(float ystep) {
109        this.ystep = ystep;
110    }
111    
112    /**
113     * Returns the horizontal interval when repeating the pattern.
114     * @return a value
115     */
116    public float getXStep() {
117        return this.xstep;
118    }
119    
120    /**
121     * Returns the vertical interval when repeating the pattern.
122     * @return a value
123     */
124    public float getYStep() {
125        return this.ystep;
126    }
127    
128    /**
129     * Tells you if this pattern is colored/uncolored (stencil = uncolored, you need to set a default color).
130     * @return true if the pattern is an uncolored tiling pattern (stencil).
131     */
132    public boolean isStencil() {
133        return stencil;
134    }
135    
136    /**
137     * Sets the transformation matrix for the pattern.
138     * @param a
139     * @param b
140     * @param c
141     * @param d
142     * @param e
143     * @param f
144     */
145    public void setPatternMatrix(float a, float b, float c, float d, float e, float f) {
146        setMatrix(a, b, c, d, e, f);
147    }
148    /**
149     * Gets the stream representing this pattern
150     * @return the stream representing this pattern
151     */
152    PdfPattern getPattern() {
153        return new PdfPattern(this);
154    }
155    
156    /**
157     * Gets the stream representing this pattern
158     * @param   compressionLevel        the compression level of the stream
159     * @return the stream representing this pattern
160     * @since   2.1.3
161     */
162    PdfPattern getPattern(int compressionLevel) {
163        return new PdfPattern(this, compressionLevel);
164    }
165    
166    /**
167     * Gets a duplicate of this <CODE>PdfPatternPainter</CODE>. All
168     * the members are copied by reference but the buffer stays different.
169     * @return a copy of this <CODE>PdfPatternPainter</CODE>
170     */
171    
172    public PdfContentByte getDuplicate() {
173        PdfPatternPainter tpl = new PdfPatternPainter();
174        tpl.writer = writer;
175        tpl.pdf = pdf;
176        tpl.thisReference = thisReference;
177        tpl.pageResources = pageResources;
178        tpl.bBox = new Rectangle(bBox);
179        tpl.xstep = xstep;
180        tpl.ystep = ystep;
181        tpl.matrix = matrix;
182        tpl.stencil = stencil;
183        tpl.defaultColor = defaultColor;
184        return tpl;
185    }
186    
187    /**
188     * Returns the default color of the pattern.
189     * @return a BaseColor
190     */
191    public BaseColor getDefaultColor() {
192        return defaultColor;
193    }
194    
195    /**
196     * @see com.itextpdf.text.pdf.PdfContentByte#setGrayFill(float)
197     */
198    public void setGrayFill(float gray) {
199        checkNoColor();
200        super.setGrayFill(gray);
201    }
202    
203    /**
204     * @see com.itextpdf.text.pdf.PdfContentByte#resetGrayFill()
205     */
206    public void resetGrayFill() {
207        checkNoColor();
208        super.resetGrayFill();
209    }
210    
211    /**
212     * @see com.itextpdf.text.pdf.PdfContentByte#setGrayStroke(float)
213     */
214    public void setGrayStroke(float gray) {
215        checkNoColor();
216        super.setGrayStroke(gray);
217    }
218    
219    /**
220     * @see com.itextpdf.text.pdf.PdfContentByte#resetGrayStroke()
221     */
222    public void resetGrayStroke() {
223        checkNoColor();
224        super.resetGrayStroke();
225    }
226    
227    /**
228     * @see com.itextpdf.text.pdf.PdfContentByte#setRGBColorFillF(float, float, float)
229     */
230    public void setRGBColorFillF(float red, float green, float blue) {
231        checkNoColor();
232        super.setRGBColorFillF(red, green, blue);
233    }
234    
235    /**
236     * @see com.itextpdf.text.pdf.PdfContentByte#resetRGBColorFill()
237     */
238    public void resetRGBColorFill() {
239        checkNoColor();
240        super.resetRGBColorFill();
241    }
242    
243    /**
244     * @see com.itextpdf.text.pdf.PdfContentByte#setRGBColorStrokeF(float, float, float)
245     */
246    public void setRGBColorStrokeF(float red, float green, float blue) {
247        checkNoColor();
248        super.setRGBColorStrokeF(red, green, blue);
249    }
250    
251    /**
252     * @see com.itextpdf.text.pdf.PdfContentByte#resetRGBColorStroke()
253     */
254    public void resetRGBColorStroke() {
255        checkNoColor();
256        super.resetRGBColorStroke();
257    }
258    
259    /**
260     * @see com.itextpdf.text.pdf.PdfContentByte#setCMYKColorFillF(float, float, float, float)
261     */
262    public void setCMYKColorFillF(float cyan, float magenta, float yellow, float black) {
263        checkNoColor();
264        super.setCMYKColorFillF(cyan, magenta, yellow, black);
265    }
266    
267    /**
268     * @see com.itextpdf.text.pdf.PdfContentByte#resetCMYKColorFill()
269     */
270    public void resetCMYKColorFill() {
271        checkNoColor();
272        super.resetCMYKColorFill();
273    }
274    
275    /**
276     * @see com.itextpdf.text.pdf.PdfContentByte#setCMYKColorStrokeF(float, float, float, float)
277     */
278    public void setCMYKColorStrokeF(float cyan, float magenta, float yellow, float black) {
279        checkNoColor();
280        super.setCMYKColorStrokeF(cyan, magenta, yellow, black);
281    }
282    
283    /**
284     * @see com.itextpdf.text.pdf.PdfContentByte#resetCMYKColorStroke()
285     */
286    public void resetCMYKColorStroke() {
287        checkNoColor();
288        super.resetCMYKColorStroke();
289    }
290    
291    /**
292     * @see com.itextpdf.text.pdf.PdfContentByte#addImage(com.itextpdf.text.Image, float, float, float, float, float, float)
293     */
294    public void addImage(Image image, float a, float b, float c, float d, float e, float f) throws DocumentException {
295        if (stencil && !image.isMask())
296            checkNoColor();
297        super.addImage(image, a, b, c, d, e, f);
298    }
299    
300    /**
301     * @see com.itextpdf.text.pdf.PdfContentByte#setCMYKColorFill(int, int, int, int)
302     */
303    public void setCMYKColorFill(int cyan, int magenta, int yellow, int black) {
304        checkNoColor();
305        super.setCMYKColorFill(cyan, magenta, yellow, black);
306    }
307    
308    /**
309     * @see com.itextpdf.text.pdf.PdfContentByte#setCMYKColorStroke(int, int, int, int)
310     */
311    public void setCMYKColorStroke(int cyan, int magenta, int yellow, int black) {
312        checkNoColor();
313        super.setCMYKColorStroke(cyan, magenta, yellow, black);
314    }
315    
316    /**
317     * @see com.itextpdf.text.pdf.PdfContentByte#setRGBColorFill(int, int, int)
318     */
319    public void setRGBColorFill(int red, int green, int blue) {
320        checkNoColor();
321        super.setRGBColorFill(red, green, blue);
322    }
323    
324    /**
325     * @see com.itextpdf.text.pdf.PdfContentByte#setRGBColorStroke(int, int, int)
326     */
327    public void setRGBColorStroke(int red, int green, int blue) {
328        checkNoColor();
329        super.setRGBColorStroke(red, green, blue);
330    }
331    
332    /**
333     * @see com.itextpdf.text.pdf.PdfContentByte#setColorStroke(com.itextpdf.text.BaseColor)
334     */
335    public void setColorStroke(BaseColor color) {
336        checkNoColor();
337        super.setColorStroke(color);
338    }
339    
340    /**
341     * @see com.itextpdf.text.pdf.PdfContentByte#setColorFill(com.itextpdf.text.BaseColor)
342     */
343    public void setColorFill(BaseColor color) {
344        checkNoColor();
345        super.setColorFill(color);
346    }
347    
348    /**
349     * @see com.itextpdf.text.pdf.PdfContentByte#setColorFill(com.itextpdf.text.pdf.PdfSpotColor, float)
350     */
351    public void setColorFill(PdfSpotColor sp, float tint) {
352        checkNoColor();
353        super.setColorFill(sp, tint);
354    }
355    
356    /**
357     * @see com.itextpdf.text.pdf.PdfContentByte#setColorStroke(com.itextpdf.text.pdf.PdfSpotColor, float)
358     */
359    public void setColorStroke(PdfSpotColor sp, float tint) {
360        checkNoColor();
361        super.setColorStroke(sp, tint);
362    }
363    
364    /**
365     * @see com.itextpdf.text.pdf.PdfContentByte#setPatternFill(com.itextpdf.text.pdf.PdfPatternPainter)
366     */
367    public void setPatternFill(PdfPatternPainter p) {
368        checkNoColor();
369        super.setPatternFill(p);
370    }
371    
372    /**
373     * @see com.itextpdf.text.pdf.PdfContentByte#setPatternFill(com.itextpdf.text.pdf.PdfPatternPainter, com.itextpdf.text.BaseColor, float)
374     */
375    public void setPatternFill(PdfPatternPainter p, BaseColor color, float tint) {
376        checkNoColor();
377        super.setPatternFill(p, color, tint);
378    }
379    
380    /**
381     * @see com.itextpdf.text.pdf.PdfContentByte#setPatternStroke(com.itextpdf.text.pdf.PdfPatternPainter, com.itextpdf.text.BaseColor, float)
382     */
383    public void setPatternStroke(PdfPatternPainter p, BaseColor color, float tint) {
384        checkNoColor();
385        super.setPatternStroke(p, color, tint);
386    }
387    
388    /**
389     * @see com.itextpdf.text.pdf.PdfContentByte#setPatternStroke(com.itextpdf.text.pdf.PdfPatternPainter)
390     */
391    public void setPatternStroke(PdfPatternPainter p) {
392        checkNoColor();
393        super.setPatternStroke(p);
394    }
395    
396    void checkNoColor() {
397        if (stencil)
398            throw new RuntimeException(MessageLocalization.getComposedMessage("colors.are.not.allowed.in.uncolored.tile.patterns"));
399    }
400}