001: /*
002: * $Id: PDFPattern.java,v 1.3 2007/12/20 18:33:30 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview.pattern;
023:
024: import java.awt.geom.AffineTransform;
025: import java.io.IOException;
026: import java.util.Map;
027:
028: import com.sun.pdfview.PDFObject;
029: import com.sun.pdfview.PDFPaint;
030: import com.sun.pdfview.PDFParseException;
031:
032: /**
033: * The abstract superclass of all PDF Pattern types
034: *
035: */
036: public abstract class PDFPattern {
037:
038: /** the pattern type (1 or 2) */
039: private int type;
040:
041: /** the matrix to transform from pattern space to PDF space */
042: private AffineTransform xform;
043:
044: /** Creates a new instance of PDFPattern */
045: protected PDFPattern(int type) {
046: this .type = type;
047: }
048:
049: /**
050: * Read a pattern from the given pattern stream
051: */
052: public static PDFPattern getPattern(PDFObject patternObj,
053: Map resources) throws IOException {
054: // see if the pattern is already cached
055: PDFPattern pattern = (PDFPattern) patternObj.getCache();
056: if (pattern != null) {
057: return pattern;
058: }
059:
060: // get the pattern type
061: int type = patternObj.getDictRef("PatternType").getIntValue();
062:
063: // read the pattern transform matrix
064: PDFObject matrix = patternObj.getDictRef("Matrix");
065: AffineTransform xform = null;
066: if (matrix == null) {
067: xform = new AffineTransform();
068: } else {
069: float elts[] = new float[6];
070: for (int i = 0; i < elts.length; i++) {
071: elts[i] = ((PDFObject) matrix.getAt(i)).getFloatValue();
072: }
073:
074: xform = new AffineTransform(elts);
075: }
076:
077: switch (type) {
078: case 1:
079: pattern = new PatternType1();
080: break;
081: default:
082: throw new PDFParseException("Unknown pattern type " + type);
083: }
084:
085: // set the transform
086: pattern.setTransform(xform);
087:
088: // parse the pattern-specific data
089: pattern.parse(patternObj, resources);
090:
091: // set the cache
092: patternObj.setCache(pattern);
093:
094: return pattern;
095: }
096:
097: /**
098: * Get the type of this pattern
099: */
100: public int getPatternType() {
101: return type;
102: }
103:
104: /**
105: * Get the transform associated with this pattern
106: */
107: public AffineTransform getTransform() {
108: return xform;
109: }
110:
111: /**
112: * Set the transform associated with this pattern
113: */
114: protected void setTransform(AffineTransform xform) {
115: this .xform = xform;
116: }
117:
118: /**
119: * Parse the pattern-specific information from the pdf object
120: *
121: * @param patternObj the pdfobject with data for this pattern
122: */
123: protected abstract void parse(PDFObject patternObj, Map resources)
124: throws IOException;
125:
126: /**
127: * Returns paint that represents the selected pattern
128: *
129: * @param basePaint the background paint color, or null for none
130: */
131: public abstract PDFPaint getPaint(PDFPaint basePaint);
132: }
|