KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pcl > PCLPageDefinition


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.render.pcl;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.fop.util.UnitConv;
28
29 /**
30  * This class represents a page format with PCL-specific properties.
31  */

32 public class PCLPageDefinition {
33
34     private static List JavaDoc pageDefinitions;
35     private static PCLPageDefinition defaultPageDefinition;
36     
37     private String JavaDoc name;
38     private int selector;
39     private Dimension JavaDoc physicalPageSize;
40     private Rectangle JavaDoc logicalPageRect;
41     private boolean landscape;
42     
43     static {
44         createPageDefinitions();
45     }
46     
47     /**
48      * Main constructor
49      * @param name the name of the page definition
50      * @param selector the selector used by the <ESC>&l#A command (page size)
51      * @param physicalPageSize the physical page size
52      * @param logicalPageRect the rectangle defining the logical page
53      * @param landscape true if it is a landscape format
54      */

55     public PCLPageDefinition(String JavaDoc name, int selector, Dimension JavaDoc physicalPageSize,
56             Rectangle JavaDoc logicalPageRect, boolean landscape) {
57         this.name = name;
58         this.selector = selector;
59         this.physicalPageSize = physicalPageSize;
60         this.logicalPageRect = logicalPageRect;
61         this.landscape = landscape;
62     }
63     
64     /** @return the name of the page definition */
65     public String JavaDoc getName() {
66         return this.name;
67     }
68     
69     /** @return the selector used by the <ESC>&l#A command (page size) */
70     public int getSelector() {
71         return this.selector;
72     }
73     
74     /** @return true if it is a landscape format */
75     public boolean isLandscapeFormat() {
76         return this.landscape;
77     }
78
79     /** @return the physical page size */
80     public Dimension JavaDoc getPhysicalPageSize() {
81         return this.physicalPageSize;
82     }
83     
84     /** @return the rectangle defining the logical page */
85     public Rectangle JavaDoc getLogicalPageRect() {
86         return this.logicalPageRect;
87     }
88     
89     private boolean matches(long width, long height, int errorMargin) {
90         return (Math.abs(this.physicalPageSize.width - width) < errorMargin)
91             && (Math.abs(this.physicalPageSize.height - height) < errorMargin);
92     }
93     
94     /** @see java.lang.Object#toString() */
95     public String JavaDoc toString() {
96         return getName();
97     }
98
99     /**
100      * Tries to determine a matching page definition.
101      * @param width the physical page width (in mpt)
102      * @param height the physical page height (in mpt)
103      * @param errorMargin the error margin for detecting the right page definition
104      * @return the page definition or null if no match was found
105      */

106     public static PCLPageDefinition getPageDefinition(long width, long height, int errorMargin) {
107         Iterator JavaDoc iter = pageDefinitions.iterator();
108         while (iter.hasNext()) {
109             PCLPageDefinition def = (PCLPageDefinition)iter.next();
110             if (def.matches(width, height, errorMargin)) {
111                 return def;
112             }
113         }
114         return null;
115     }
116     
117     /** @return the default page definition (letter) */
118     public static PCLPageDefinition getDefaultPageDefinition() {
119         return defaultPageDefinition;
120     }
121     
122     /**
123      * Converts an offset values for logical pages to millipoints. The values are given as pixels
124      * in a 300dpi environment.
125      * @param offset the offset as given in the PCL 5 specification (under "Printable Area")
126      * @return the converted value in millipoints
127      */

128     private static int convert300dpiDotsToMpt(int offset) {
129         return (int)Math.round(((double)offset) * 72000 / 300);
130     }
131     
132     private static Dimension JavaDoc createPhysicalPageSizeInch(float width, float height) {
133         return new Dimension JavaDoc(
134                 (int)Math.round(UnitConv.in2mpt(width)),
135                 (int)Math.round(UnitConv.in2mpt(height)));
136     }
137     
138     private static Dimension JavaDoc createPhysicalPageSizeMm(float width, float height) {
139         return new Dimension JavaDoc(
140                 (int)Math.round(UnitConv.mm2mpt(width)),
141                 (int)Math.round(UnitConv.mm2mpt(height)));
142     }
143     
144     private static Rectangle JavaDoc createLogicalPageRect(int x, int y, int width, int height) {
145         return new Rectangle JavaDoc(convert300dpiDotsToMpt(x), convert300dpiDotsToMpt(y),
146                 convert300dpiDotsToMpt(width), convert300dpiDotsToMpt(height));
147     }
148     
149     private static void createPageDefinitions() {
150         pageDefinitions = new java.util.ArrayList JavaDoc();
151         pageDefinitions.add(new PCLPageDefinition("Letter", 2,
152                 createPhysicalPageSizeInch(8.5f, 11),
153                 createLogicalPageRect(75, 0, 2400, 3300), false));
154         defaultPageDefinition = new PCLPageDefinition("Legal", 3,
155                 createPhysicalPageSizeInch(8.5f, 14),
156                 createLogicalPageRect(75, 0, 2400, 4200), false);
157         pageDefinitions.add(defaultPageDefinition);
158         pageDefinitions.add(new PCLPageDefinition("Executive", 1,
159                 createPhysicalPageSizeInch(7.25f, 10.5f),
160                 createLogicalPageRect(75, 0, 2025, 3150), false));
161         pageDefinitions.add(new PCLPageDefinition("Ledger", 6,
162                 createPhysicalPageSizeInch(11, 17),
163                 createLogicalPageRect(75, 0, 3150, 5100), false));
164         pageDefinitions.add(new PCLPageDefinition("A4", 26,
165                 createPhysicalPageSizeMm(210, 297),
166                 createLogicalPageRect(71, 0, 2338, 3507), false));
167         pageDefinitions.add(new PCLPageDefinition("A3", 27,
168                 createPhysicalPageSizeMm(297, 420),
169                 createLogicalPageRect(71, 0, 3365, 4960), false));
170
171         //TODO Add envelope definitions
172

173         pageDefinitions.add(new PCLPageDefinition("LetterL", 2,
174                 createPhysicalPageSizeInch(11, 8.5f),
175                 createLogicalPageRect(60, 0, 3180, 2550), true));
176         pageDefinitions.add(new PCLPageDefinition("LegalL", 3,
177                 createPhysicalPageSizeInch(14, 8.5f),
178                 createLogicalPageRect(60, 0, 4080, 2550), true));
179         pageDefinitions.add(new PCLPageDefinition("ExecutiveL", 1,
180                 createPhysicalPageSizeInch(10.5f, 7.25f),
181                 createLogicalPageRect(60, 0, 3030, 2175), true));
182         pageDefinitions.add(new PCLPageDefinition("LedgerL", 6,
183                 createPhysicalPageSizeInch(17, 11),
184                 createLogicalPageRect(60, 0, 4980, 3300), true));
185         pageDefinitions.add(new PCLPageDefinition("A4L", 26,
186                 createPhysicalPageSizeMm(297, 210),
187                 createLogicalPageRect(59, 0, 3389, 2480), true));
188         pageDefinitions.add(new PCLPageDefinition("A3L", 27,
189                 createPhysicalPageSizeMm(420, 297),
190                 createLogicalPageRect(59, 0, 4842, 3507), true));
191     }
192     
193 }
194
Popular Tags