001: package it.stefanochizzolini.clown.samples;
002:
003: import it.stefanochizzolini.clown.bytes.Buffer;
004: import it.stefanochizzolini.clown.documents.Document;
005: import it.stefanochizzolini.clown.documents.Page;
006: import it.stefanochizzolini.clown.documents.PageFormat;
007: import it.stefanochizzolini.clown.documents.contents.Contents;
008: import it.stefanochizzolini.clown.documents.contents.ContentScanner;
009: import it.stefanochizzolini.clown.documents.contents.Fonts;
010: import it.stefanochizzolini.clown.documents.contents.Resources;
011: import it.stefanochizzolini.clown.documents.contents.colorSpaces.Color;
012: import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor;
013: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
014: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
015: import it.stefanochizzolini.clown.documents.contents.composition.BlockFilter;
016: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
017: import it.stefanochizzolini.clown.documents.contents.fonts.Font;
018: import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
019: import it.stefanochizzolini.clown.documents.contents.objects.ModifyCTM;
020: import it.stefanochizzolini.clown.files.File;
021: import it.stefanochizzolini.clown.objects.PdfName;
022:
023: import java.awt.Dimension;
024: import java.awt.geom.Dimension2D;
025: import java.awt.geom.Point2D;
026: import java.awt.geom.Rectangle2D;
027:
028: /**
029: This sample shows the effects of the manipulation of the CTM (Current Transformation Matrix),
030: that is the logical device which affects the page coordinate system used to place graphics contents
031: onto the canvas.
032: */
033: public class PageCoordinatesSample implements ISample {
034: // <static>
035: // <fields>
036: private static final PdfName ResourceName_DefaultFont = new PdfName(
037: "default");
038:
039: // </fields>
040: // </static>
041:
042: // <dynamic>
043: // <interface>
044: // <public>
045: // <ISample>
046: public void run(PDFClownSampleLoader loader) {
047: // 1. Instantiate a new PDF file!
048: /* NOTE: a File object is the low-level (syntactic) representation of a PDF file. */
049: File file = new File();
050:
051: try {
052: // 2. Get its corresponding document!
053: /* NOTE: a Document object is the high-level (semantic) representation of a PDF file. */
054: Document document = file.getDocument();
055:
056: // 3. Set the document properties and resources!
057: build_initialize(document);
058:
059: // 4. Insert the contents into the document!
060: build_content(document);
061: } catch (Exception e) {
062: throw new RuntimeException(e);
063: }
064:
065: // 5. Serialize the PDF file!
066: loader.serialize(file, this .getClass().getSimpleName(), false);
067: }
068:
069: // </ISample>
070: // </public>
071:
072: // <private>
073: /**
074: Populates a PDF file with contents.
075: */
076: private void build_content(Document document) {
077: // Add the page to the document!
078: Page page = new Page(document); // Instantiates the page inside the document context.
079: document.getPages().add(page); // Puts the page in the pages collection.
080: Dimension2D pageSize = page.getSize();
081:
082: // Create a content builder for the page content stream!
083: PrimitiveFilter builder = new PrimitiveFilter(page);
084:
085: String[] steps = new String[5];
086: Color[] fillColors = new Color[5];
087:
088: build_content_steps(builder, steps, fillColors, pageSize);
089:
090: build_content_legend(builder, steps, fillColors, pageSize);
091:
092: builder.flush();
093: }
094:
095: private String build_content_getStepNote(PrimitiveFilter builder,
096: String comment) {
097: // Get the CTM!
098: double[] ctm = builder.getScanner().getState().getCTM();
099:
100: return "CTM (" + comment + "): " + ctm[0] + ", " + ctm[1]
101: + ", " + ctm[2] + ", " + ctm[3] + ", " + ctm[4] + ", "
102: + ctm[5];
103: }
104:
105: private void build_content_legend(PrimitiveFilter builder,
106: String[] steps, Color[] fillColors, Dimension2D pageSize) {
107: double[] finalCTM = builder.getScanner().getState().getCTM();
108:
109: final BlockFilter blockFilter = new BlockFilter(builder);
110:
111: builder.beginLocalState();
112: builder.setFillColor(new DeviceRGBColor(115f / 255, 164f / 255,
113: 232f / 255));
114: final Rectangle2D frame = new Rectangle2D.Double(18, 18,
115: pageSize.getWidth() * .5, pageSize.getHeight() * .5);
116: blockFilter.begin(frame, AlignmentXEnum.Left,
117: AlignmentYEnum.Top);
118: builder.setFont(ResourceName_DefaultFont, 24);
119: blockFilter.showText("Page coordinates sample");
120: Dimension2D breakSize = new Dimension(0, 8);
121: blockFilter.showBreak(breakSize);
122: builder.setFont(ResourceName_DefaultFont, 8);
123: blockFilter
124: .showText("This sample shows the effects of the manipulation of the CTM (Current Transformation Matrix), that is the device which affects the page coordinate system used to place graphics contents onto the canvas.");
125: blockFilter.showBreak(breakSize);
126: blockFilter
127: .showText("The following steps represent the operations applied to this page's CTM in order to alter it. Each step writes the tag \"Step\" at the current coordinates origin and draws the page frame:");
128: breakSize = new Dimension(8, 4);
129: blockFilter.showBreak(breakSize);
130: for (int i = 0; i < steps.length; i++) {
131: builder.setFillColor(fillColors[i]);
132: blockFilter.showText("Step " + i + ")");
133: builder.setFillColor(new DeviceRGBColor(115f / 255,
134: 164f / 255, 232f / 255));
135: blockFilter.showText(" " + steps[i]);
136: blockFilter.showBreak(breakSize);
137: }
138: blockFilter
139: .showText("Note that the (negligible: "
140: + Math.max(Math.max(Math.max(Math
141: .abs(finalCTM[1] % 1), Math
142: .abs(finalCTM[2] % 1)), Math
143: .abs(finalCTM[4] % 1)), Math
144: .abs(finalCTM[5] % 1))
145: + ") approximation of the CTM components at step 4 is due to floating point precision limits; their exact values should be 1.0, 0.0, 0.0, 1.0, 0.0, 0.0.");
146: blockFilter.end();
147: builder.end();
148: }
149:
150: private void build_content_steps(PrimitiveFilter builder,
151: String[] steps, Color[] fillColors, Dimension2D pageSize) {
152: builder.setFont(ResourceName_DefaultFont, 32);
153: Rectangle2D frame = new Rectangle2D.Double(0, 0, pageSize
154: .getWidth(), pageSize.getHeight());
155:
156: // Step 0.
157: {
158: fillColors[0] = new DeviceRGBColor(30f / 255, 10f / 255, 0);
159: builder.setFillColor(fillColors[0]);
160: builder.setStrokeColor(new DeviceRGBColor(0, 0, 0));
161: // Draw the page frame!
162: builder.drawRectangle(frame);
163: builder.stroke();
164: // Write at the current coordinates origin!
165: builder.showText("Step 0");
166:
167: steps[0] = build_content_getStepNote(builder, "default");
168: }
169:
170: // Step 1.
171: {
172: fillColors[1] = new DeviceRGBColor(80f / 255, 25f / 255, 0);
173: builder.setFillColor(fillColors[1]);
174: builder.setStrokeColor(new DeviceRGBColor(50f / 255,
175: 15f / 255, 0));
176:
177: builder.translate(72, 72);
178:
179: // Draw the page frame!
180: builder.drawRectangle(frame);
181: builder.stroke();
182: // Write at the current coordinates origin!
183: builder.showText("Step 1");
184:
185: steps[1] = build_content_getStepNote(builder,
186: "after translate(72,72)");
187: }
188:
189: // Step 2.
190: {
191: fillColors[2] = new DeviceRGBColor(130f / 255, 45f / 255, 0);
192: builder.setFillColor(fillColors[2]);
193: builder.setStrokeColor(new DeviceRGBColor(100f / 255,
194: 30f / 255, 0));
195:
196: builder.rotate(-20);
197:
198: // Draw the page frame!
199: builder.drawRectangle(frame);
200: builder.stroke();
201: // Write at the current coordinates origin!
202: builder.showText("Step 2");
203: builder.showText("Upper-left origin 2", new Point2D.Double(
204: 0, 0));
205:
206: steps[2] = build_content_getStepNote(builder,
207: "after rotate(20)");
208: }
209:
210: // Step 3.
211: {
212: fillColors[3] = new DeviceRGBColor(180f / 255, 60f / 255, 0);
213: builder.setFillColor(fillColors[3]);
214: builder.setStrokeColor(new DeviceRGBColor(150f / 255,
215: 50f / 255, 0));
216:
217: builder.translate(0, 72);
218: builder.scale(.5, .5);
219:
220: // Draw the page frame!
221: builder.drawRectangle(frame);
222: builder.stroke();
223: // Write at the current coordinates origin!
224: builder.showText("Step 3");
225:
226: steps[3] = build_content_getStepNote(builder,
227: "after translate(0,72) and scale(.5,.5)");
228: }
229:
230: // Step 4.
231: {
232: fillColors[4] = new DeviceRGBColor(230f / 255, 75f / 255, 0);
233: builder.setFillColor(fillColors[4]);
234: builder.setStrokeColor(new DeviceRGBColor(200f / 255,
235: 65f / 255, 0));
236:
237: builder.add(ModifyCTM.getResetCTM(builder.getScanner()
238: .getState().getCTM()));
239:
240: // Draw the page frame!
241: builder.drawRectangle(frame);
242: builder.stroke();
243: // Write at the current coordinates origin!
244: builder.showText("Step 4");
245:
246: steps[4] = build_content_getStepNote(builder,
247: "after resetting CTM");
248: }
249: }
250:
251: /**
252: Prepares the basic settings for populating a PDF file.
253: */
254: private void build_initialize(Document document) {
255: // 1. Set default page size (A4)!
256: document.setPageSize(PageFormat.getSize());
257:
258: // 2. Setting the document resources...
259: // 2.1. Resources collection.
260: Resources resources = new Resources(document); // Instantiates the resources collection inside the document context.
261: document.setResources(resources); // Puts the resources collection in the common resources role.
262: // 2.2. Fonts collection.
263: Fonts fonts = new Fonts(document); // Instantiates the fonts collection inside the document context.
264: resources.setFonts(fonts); // Puts the fonts collection in the common resources role.
265: // Add a font to the fonts collection!
266: fonts.put(ResourceName_DefaultFont, new StandardType1Font(
267: document, StandardType1Font.FamilyNameEnum.Courier,
268: true, false));
269: }
270: // </private>
271: // </interface>
272: // </dynamic>
273: }
|