001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.documents.contents.objects;
028:
029: import it.stefanochizzolini.clown.bytes.IBuffer;
030: import it.stefanochizzolini.clown.objects.PdfDirectObject;
031: import it.stefanochizzolini.clown.util.NotImplementedException;
032:
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: /**
037: Content stream instruction [PDF:1.6:3.7.1].
038:
039: @author Stefano Chizzolini
040: @version 0.0.5
041: @since 0.0.2
042: */
043: public class Operation extends ContentObject {
044: // <class>
045: // <static>
046: // <interface>
047: // <public>
048: /**
049: Gets a specific operation.
050: @param operator Operator.
051: @param operands List of operands.
052: @return Operation associated to the operator.
053: @version 0.0.5
054: @since 0.0.4
055: */
056: public static Operation get(String operator,
057: List<PdfDirectObject> operands) {
058: /*
059: NOTE: This is a factory method for any operation-derived object.
060: */
061: if (operator == null)
062: return null;
063:
064: if (operator.equals(SaveGraphicsState.Operator))
065: return SaveGraphicsState.Value;
066: else if (operator.equals(SetFont.Operator))
067: return new SetFont(operands);
068: else if (operator.equals(SetStrokeColor.Operator))
069: return new SetStrokeColor(operands);
070: else if (operator.equals(SetStrokeColorSpace.Operator))
071: return new SetStrokeColorSpace(operands);
072: else if (operator.equals(SetFillColor.Operator))
073: return new SetFillColor(operands);
074: else if (operator.equals(SetFillColorSpace.Operator))
075: return new SetFillColorSpace(operands);
076: else if (operator.equals(RestoreGraphicsState.Operator))
077: return RestoreGraphicsState.Value;
078: else if (operator.equals(Fill.Operator)
079: || operator.equals(Fill.ObsoleteOperator))
080: return Fill.Value;
081: else if (operator.equals(FillStroke.Operator))
082: return FillStroke.Value;
083: else if (operator.equals(TranslateTextToNextLine.Operator))
084: return TranslateTextToNextLine.Value;
085: else if (operator.equals(ShowText.Operator))
086: return new ShowText(operands);
087: else if (operator.equals(TranslateTextRelative.Operator))
088: return new TranslateTextRelative(operands);
089: else if (operator.equals(SetTextMatrix.Operator))
090: return new SetTextMatrix(operands);
091: else if (operator.equals(ModifyCTM.Operator))
092: return new ModifyCTM(operands);
093: else if (operator.equals(PaintXObject.Operator))
094: return new PaintXObject(operands);
095: else if (operator.equals(PaintShadingObject.Operator))
096: return new PaintShadingObject(operands);
097: else if (operator.equals(SetCharSpace.Operator))
098: return new SetCharSpace(operands);
099: else if (operator.equals(SetLineCap.Operator))
100: return new SetLineCap(operands);
101: else if (operator.equals(SetLineDash.Operator))
102: return new SetLineDash(operands);
103: else if (operator.equals(SetLineJoin.Operator))
104: return new SetLineJoin(operands);
105: else if (operator.equals(SetLineWidth.Operator))
106: return new SetLineWidth(operands);
107: else if (operator.equals(SetMiterLimit.Operator))
108: return new SetMiterLimit(operands);
109: else if (operator.equals(SetTextLead.Operator))
110: return new SetTextLead(operands);
111: else if (operator.equals(SetTextRise.Operator))
112: return new SetTextRise(operands);
113: else if (operator.equals(SetTextScale.Operator))
114: return new SetTextScale(operands);
115: else if (operator.equals(SetTextRenderMode.Operator))
116: return new SetTextRenderMode(operands);
117: else if (operator.equals(SetWordSpace.Operator))
118: return new SetWordSpace(operands);
119: else if (operator.equals(DrawRectangle.Operator))
120: return new DrawRectangle(operands);
121: else if (operator.equals(EndInlineImage.Operator))
122: return EndInlineImage.Value;
123: else if (operator.equals(BeginText.Operator))
124: return BeginText.Value;
125: else if (operator.equals(EndText.Operator))
126: return EndText.Value;
127: else if (operator.equals(BeginInlineImage.Operator))
128: return BeginInlineImage.Value;
129: else if (operator.equals(EndInlineImage.Operator))
130: return EndInlineImage.Value;
131: else
132: // No explicit operation implementation available.
133: return new Operation(operator, operands);
134: }
135:
136: // </public>
137: // </interface>
138: // </static>
139:
140: // <dynamic>
141: // <fields>
142: protected String operator;
143: protected List<PdfDirectObject> operands;
144:
145: // </fields>
146:
147: // <constructors>
148: public Operation(String operator) {
149: this .operator = operator;
150: }
151:
152: public Operation(String operator, PdfDirectObject operand) {
153: this .operator = operator;
154:
155: this .operands = new ArrayList<PdfDirectObject>();
156: this .operands.add(operand);
157: }
158:
159: public Operation(String operator, List<PdfDirectObject> operands) {
160: this .operator = operator;
161: this .operands = operands;
162: }
163:
164: // </constructors>
165:
166: // <interface>
167: // <public>
168: public String getOperator() {
169: return operator;
170: }
171:
172: public List<PdfDirectObject> getOperands() {
173: return operands;
174: }
175:
176: @Override
177: public String toString() {
178: return "{" + operator + " "
179: + (operands == null ? "" : operands.toString()) + "}";
180: }
181:
182: @Override
183: public void writeTo(IBuffer buffer) {
184: if (operands != null) {
185: for (PdfDirectObject operand : operands) {
186: buffer.append(operand.toPdf());
187: buffer.append(" ");
188: }
189: }
190:
191: buffer.append(operator);
192: buffer.append("\n");
193: }
194: // </public>
195: // </interface>
196: // </dynamic>
197: // </class>
198: }
|