001: /*
002: * $Id: PdfXConformanceImp.java 2832 2007-06-05 14:40:21Z psoares33 $
003: *
004: * Copyright 2006 Bruno Lowagie (based on code by Paulo Soares)
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049:
050: package com.lowagie.text.pdf.internal;
051:
052: import java.awt.Color;
053:
054: import com.lowagie.text.pdf.BaseFont;
055: import com.lowagie.text.pdf.ExtendedColor;
056: import com.lowagie.text.pdf.PatternColor;
057: import com.lowagie.text.pdf.PdfArray;
058: import com.lowagie.text.pdf.PdfDictionary;
059: import com.lowagie.text.pdf.PdfGState;
060: import com.lowagie.text.pdf.PdfImage;
061: import com.lowagie.text.pdf.PdfName;
062: import com.lowagie.text.pdf.PdfNumber;
063: import com.lowagie.text.pdf.PdfObject;
064: import com.lowagie.text.pdf.PdfString;
065: import com.lowagie.text.pdf.PdfWriter;
066: import com.lowagie.text.pdf.PdfXConformanceException;
067: import com.lowagie.text.pdf.ShadingColor;
068: import com.lowagie.text.pdf.SpotColor;
069: import com.lowagie.text.pdf.interfaces.PdfXConformance;
070:
071: public class PdfXConformanceImp implements PdfXConformance {
072:
073: /** A key for an aspect that can be checked for PDF/X Conformance. */
074: public static final int PDFXKEY_COLOR = 1;
075: /** A key for an aspect that can be checked for PDF/X Conformance. */
076: public static final int PDFXKEY_CMYK = 2;
077: /** A key for an aspect that can be checked for PDF/X Conformance. */
078: public static final int PDFXKEY_RGB = 3;
079: /** A key for an aspect that can be checked for PDF/X Conformance. */
080: public static final int PDFXKEY_FONT = 4;
081: /** A key for an aspect that can be checked for PDF/X Conformance. */
082: public static final int PDFXKEY_IMAGE = 5;
083: /** A key for an aspect that can be checked for PDF/X Conformance. */
084: public static final int PDFXKEY_GSTATE = 6;
085: /** A key for an aspect that can be checked for PDF/X Conformance. */
086: public static final int PDFXKEY_LAYER = 7;
087:
088: /**
089: * The value indicating if the PDF has to be in conformance with PDF/X.
090: */
091: protected int pdfxConformance = PdfWriter.PDFXNONE;
092:
093: /**
094: * @see com.lowagie.text.pdf.interfaces.PdfXConformance#setPDFXConformance(int)
095: */
096: public void setPDFXConformance(int pdfxConformance) {
097: this .pdfxConformance = pdfxConformance;
098: }
099:
100: /**
101: * @see com.lowagie.text.pdf.interfaces.PdfXConformance#getPDFXConformance()
102: */
103: public int getPDFXConformance() {
104: return pdfxConformance;
105: }
106:
107: /**
108: * Checks if the PDF/X Conformance is necessary.
109: * @return true if the PDF has to be in conformance with any of the PDF/X specifications
110: */
111: public boolean isPdfX() {
112: return pdfxConformance != PdfWriter.PDFXNONE;
113: }
114:
115: /**
116: * Checks if the PDF has to be in conformance with PDF/X-1a:2001
117: * @return true of the PDF has to be in conformance with PDF/X-1a:2001
118: */
119: public boolean isPdfX1A2001() {
120: return pdfxConformance == PdfWriter.PDFX1A2001;
121: }
122:
123: /**
124: * Checks if the PDF has to be in conformance with PDF/X-3:2002
125: * @return true of the PDF has to be in conformance with PDF/X-3:2002
126: */
127: public boolean isPdfX32002() {
128: return pdfxConformance == PdfWriter.PDFX32002;
129: }
130:
131: /**
132: * Checks if the PDF has to be in conformance with PDFA1
133: * @return true of the PDF has to be in conformance with PDFA1
134: */
135: public boolean isPdfA1() {
136: return pdfxConformance == PdfWriter.PDFA1A
137: || pdfxConformance == PdfWriter.PDFA1B;
138: }
139:
140: /**
141: * Checks if the PDF has to be in conformance with PDFA1A
142: * @return true of the PDF has to be in conformance with PDFA1A
143: */
144: public boolean isPdfA1A() {
145: return pdfxConformance == PdfWriter.PDFA1A;
146: }
147:
148: public void completeInfoDictionary(PdfDictionary info) {
149: if (isPdfX() && !isPdfA1()) {
150: if (info.get(PdfName.GTS_PDFXVERSION) == null) {
151: if (isPdfX1A2001()) {
152: info.put(PdfName.GTS_PDFXVERSION, new PdfString(
153: "PDF/X-1:2001"));
154: info.put(new PdfName("GTS_PDFXConformance"),
155: new PdfString("PDF/X-1a:2001"));
156: } else if (isPdfX32002())
157: info.put(PdfName.GTS_PDFXVERSION, new PdfString(
158: "PDF/X-3:2002"));
159: }
160: if (info.get(PdfName.TITLE) == null) {
161: info.put(PdfName.TITLE, new PdfString("Pdf document"));
162: }
163: if (info.get(PdfName.CREATOR) == null) {
164: info.put(PdfName.CREATOR, new PdfString("Unknown"));
165: }
166: if (info.get(PdfName.TRAPPED) == null) {
167: info.put(PdfName.TRAPPED, new PdfName("False"));
168: }
169: }
170: }
171:
172: public void completeExtraCatalog(PdfDictionary extraCatalog) {
173: if (isPdfX() && !isPdfA1()) {
174: if (extraCatalog.get(PdfName.OUTPUTINTENTS) == null) {
175: PdfDictionary out = new PdfDictionary(
176: PdfName.OUTPUTINTENT);
177: out.put(PdfName.OUTPUTCONDITION, new PdfString(
178: "SWOP CGATS TR 001-1995"));
179: out.put(PdfName.OUTPUTCONDITIONIDENTIFIER,
180: new PdfString("CGATS TR 001"));
181: out.put(PdfName.REGISTRYNAME, new PdfString(
182: "http://www.color.org"));
183: out.put(PdfName.INFO, new PdfString(""));
184: out.put(PdfName.S, PdfName.GTS_PDFX);
185: extraCatalog.put(PdfName.OUTPUTINTENTS, new PdfArray(
186: out));
187: }
188: }
189: }
190:
191: /**
192: * Business logic that checks if a certain object is in conformance with PDF/X.
193: * @param writer the writer that is supposed to write the PDF/X file
194: * @param key the type of PDF/X conformance that has to be checked
195: * @param obj1 the object that is checked for conformance
196: */
197: public static void checkPDFXConformance(PdfWriter writer, int key,
198: Object obj1) {
199: if (writer == null || !writer.isPdfX())
200: return;
201: int conf = writer.getPDFXConformance();
202: switch (key) {
203: case PDFXKEY_COLOR:
204: switch (conf) {
205: case PdfWriter.PDFX1A2001:
206: if (obj1 instanceof ExtendedColor) {
207: ExtendedColor ec = (ExtendedColor) obj1;
208: switch (ec.getType()) {
209: case ExtendedColor.TYPE_CMYK:
210: case ExtendedColor.TYPE_GRAY:
211: return;
212: case ExtendedColor.TYPE_RGB:
213: throw new PdfXConformanceException(
214: "Colorspace RGB is not allowed.");
215: case ExtendedColor.TYPE_SEPARATION:
216: SpotColor sc = (SpotColor) ec;
217: checkPDFXConformance(writer, PDFXKEY_COLOR, sc
218: .getPdfSpotColor().getAlternativeCS());
219: break;
220: case ExtendedColor.TYPE_SHADING:
221: ShadingColor xc = (ShadingColor) ec;
222: checkPDFXConformance(writer, PDFXKEY_COLOR, xc
223: .getPdfShadingPattern().getShading()
224: .getColorSpace());
225: break;
226: case ExtendedColor.TYPE_PATTERN:
227: PatternColor pc = (PatternColor) ec;
228: checkPDFXConformance(writer, PDFXKEY_COLOR, pc
229: .getPainter().getDefaultColor());
230: break;
231: }
232: } else if (obj1 instanceof Color)
233: throw new PdfXConformanceException(
234: "Colorspace RGB is not allowed.");
235: break;
236: }
237: break;
238: case PDFXKEY_CMYK:
239: break;
240: case PDFXKEY_RGB:
241: if (conf == PdfWriter.PDFX1A2001)
242: throw new PdfXConformanceException(
243: "Colorspace RGB is not allowed.");
244: break;
245: case PDFXKEY_FONT:
246: if (!((BaseFont) obj1).isEmbedded())
247: throw new PdfXConformanceException(
248: "All the fonts must be embedded.");
249: break;
250: case PDFXKEY_IMAGE:
251: PdfImage image = (PdfImage) obj1;
252: if (image.get(PdfName.SMASK) != null)
253: throw new PdfXConformanceException(
254: "The /SMask key is not allowed in images.");
255: switch (conf) {
256: case PdfWriter.PDFX1A2001:
257: PdfObject cs = image.get(PdfName.COLORSPACE);
258: if (cs == null)
259: return;
260: if (cs.isName()) {
261: if (PdfName.DEVICERGB.equals(cs))
262: throw new PdfXConformanceException(
263: "Colorspace RGB is not allowed.");
264: } else if (cs.isArray()) {
265: if (PdfName.CALRGB.equals(((PdfArray) cs)
266: .getArrayList().get(0)))
267: throw new PdfXConformanceException(
268: "Colorspace CalRGB is not allowed.");
269: }
270: break;
271: }
272: break;
273: case PDFXKEY_GSTATE:
274: PdfDictionary gs = (PdfDictionary) obj1;
275: PdfObject obj = gs.get(PdfName.BM);
276: if (obj != null && !PdfGState.BM_NORMAL.equals(obj)
277: && !PdfGState.BM_COMPATIBLE.equals(obj))
278: throw new PdfXConformanceException("Blend mode "
279: + obj.toString() + " not allowed.");
280: obj = gs.get(PdfName.CA);
281: double v = 0.0;
282: if (obj != null
283: && (v = ((PdfNumber) obj).doubleValue()) != 1.0)
284: throw new PdfXConformanceException(
285: "Transparency is not allowed: /CA = " + v);
286: obj = gs.get(PdfName.ca);
287: v = 0.0;
288: if (obj != null
289: && (v = ((PdfNumber) obj).doubleValue()) != 1.0)
290: throw new PdfXConformanceException(
291: "Transparency is not allowed: /ca = " + v);
292: break;
293: case PDFXKEY_LAYER:
294: throw new PdfXConformanceException(
295: "Layers are not allowed.");
296: }
297: }
298: }
|