Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PCLPageDefinition.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* $Id$ */
  18. package org.apache.fop.render.pcl;
  19. import java.awt.Dimension;
  20. import java.awt.Rectangle;
  21. import java.util.List;
  22. import org.apache.xmlgraphics.util.UnitConv;
  23. /**
  24. * This class represents a page format with PCL-specific properties.
  25. */
  26. public class PCLPageDefinition {
  27. private static List pageDefinitions;
  28. private static PCLPageDefinition defaultPageDefinition;
  29. private final String name;
  30. private final int selector;
  31. private final Dimension physicalPageSize;
  32. private final Rectangle logicalPageRect;
  33. private final boolean landscape;
  34. static {
  35. createPageDefinitions();
  36. }
  37. /**
  38. * Main constructor
  39. * @param name the name of the page definition
  40. * @param selector the selector used by the <ESC> command (page size)
  41. * @param physicalPageSize the physical page size
  42. * @param logicalPageRect the rectangle defining the logical page
  43. * @param landscape true if it is a landscape format
  44. */
  45. public PCLPageDefinition(String name, int selector, Dimension physicalPageSize,
  46. Rectangle logicalPageRect, boolean landscape) {
  47. this.name = name;
  48. this.selector = selector;
  49. this.physicalPageSize = physicalPageSize;
  50. this.logicalPageRect = logicalPageRect;
  51. this.landscape = landscape;
  52. }
  53. /** @return the name of the page definition */
  54. public String getName() {
  55. return this.name;
  56. }
  57. /** @return the selector used by the <ESC> command (page size) */
  58. public int getSelector() {
  59. return this.selector;
  60. }
  61. /** @return true if it is a landscape format */
  62. public boolean isLandscapeFormat() {
  63. return this.landscape;
  64. }
  65. /** @return the physical page size */
  66. public Dimension getPhysicalPageSize() {
  67. return this.physicalPageSize;
  68. }
  69. /** @return the rectangle defining the logical page */
  70. public Rectangle getLogicalPageRect() {
  71. return this.logicalPageRect;
  72. }
  73. private boolean matches(long width, long height, int errorMargin) {
  74. return (Math.abs(this.physicalPageSize.width - width) < errorMargin)
  75. && (Math.abs(this.physicalPageSize.height - height) < errorMargin);
  76. }
  77. /** {@inheritDoc} */
  78. public String toString() {
  79. return getName();
  80. }
  81. /**
  82. * Tries to determine a matching page definition.
  83. * @param width the physical page width (in mpt)
  84. * @param height the physical page height (in mpt)
  85. * @param errorMargin the error margin for detecting the right page definition
  86. * @return the page definition or null if no match was found
  87. */
  88. public static PCLPageDefinition getPageDefinition(long width, long height, int errorMargin) {
  89. for (Object pageDefinition : pageDefinitions) {
  90. PCLPageDefinition def = (PCLPageDefinition) pageDefinition;
  91. if (def.matches(width, height, errorMargin)) {
  92. return def;
  93. }
  94. }
  95. return null;
  96. }
  97. /**
  98. * Returns a page definition based on a page format.
  99. * @param name the name of the page format (ex. "A4" or "Letter")
  100. * @return the page definition or null if no match was found
  101. */
  102. public static PCLPageDefinition getPageDefinition(String name) {
  103. for (Object pageDefinition : pageDefinitions) {
  104. PCLPageDefinition def = (PCLPageDefinition) pageDefinition;
  105. if (def.getName().equalsIgnoreCase(name)) {
  106. return def;
  107. }
  108. }
  109. return null;
  110. }
  111. /** @return the default page definition (letter) */
  112. public static PCLPageDefinition getDefaultPageDefinition() {
  113. return defaultPageDefinition;
  114. }
  115. /**
  116. * Converts an offset values for logical pages to millipoints. The values are given as pixels
  117. * in a 300dpi environment.
  118. * @param offset the offset as given in the PCL 5 specification (under "Printable Area")
  119. * @return the converted value in millipoints
  120. */
  121. private static int convert300dpiDotsToMpt(int offset) {
  122. return (int)Math.round(((double)offset) * 72000 / 300);
  123. }
  124. private static Dimension createPhysicalPageSizeInch(float width, float height) {
  125. return new Dimension(
  126. (int)Math.round(UnitConv.in2mpt(width)),
  127. (int)Math.round(UnitConv.in2mpt(height)));
  128. }
  129. private static Dimension createPhysicalPageSizeMm(float width, float height) {
  130. return new Dimension(
  131. (int)Math.round(UnitConv.mm2mpt(width)),
  132. (int)Math.round(UnitConv.mm2mpt(height)));
  133. }
  134. private static Rectangle createLogicalPageRect(int x, int y, int width, int height) {
  135. return new Rectangle(convert300dpiDotsToMpt(x), convert300dpiDotsToMpt(y),
  136. convert300dpiDotsToMpt(width), convert300dpiDotsToMpt(height));
  137. }
  138. private static void createPageDefinitions() {
  139. pageDefinitions = new java.util.ArrayList();
  140. pageDefinitions.add(new PCLPageDefinition("Letter", 2,
  141. createPhysicalPageSizeInch(8.5f, 11),
  142. createLogicalPageRect(75, 0, 2400, 3300), false));
  143. defaultPageDefinition = new PCLPageDefinition("Legal", 3,
  144. createPhysicalPageSizeInch(8.5f, 14),
  145. createLogicalPageRect(75, 0, 2400, 4200), false);
  146. pageDefinitions.add(defaultPageDefinition);
  147. pageDefinitions.add(new PCLPageDefinition("Executive", 1,
  148. createPhysicalPageSizeInch(7.25f, 10.5f),
  149. createLogicalPageRect(75, 0, 2025, 3150), false));
  150. pageDefinitions.add(new PCLPageDefinition("Ledger", 6,
  151. createPhysicalPageSizeInch(11, 17),
  152. createLogicalPageRect(75, 0, 3150, 5100), false));
  153. pageDefinitions.add(new PCLPageDefinition("A5", 25,
  154. createPhysicalPageSizeMm(148, 210),
  155. createLogicalPageRect(71, 0, 1745, 2480), false));
  156. pageDefinitions.add(new PCLPageDefinition("A4", 26,
  157. createPhysicalPageSizeMm(210, 297),
  158. createLogicalPageRect(71, 0, 2338, 3507), false));
  159. pageDefinitions.add(new PCLPageDefinition("A3", 27,
  160. createPhysicalPageSizeMm(297, 420),
  161. createLogicalPageRect(71, 0, 3365, 4960), false));
  162. //TODO Add envelope definitions
  163. pageDefinitions.add(new PCLPageDefinition("LetterL", 2,
  164. createPhysicalPageSizeInch(11, 8.5f),
  165. createLogicalPageRect(60, 0, 3180, 2550), true));
  166. pageDefinitions.add(new PCLPageDefinition("LegalL", 3,
  167. createPhysicalPageSizeInch(14, 8.5f),
  168. createLogicalPageRect(60, 0, 4080, 2550), true));
  169. pageDefinitions.add(new PCLPageDefinition("ExecutiveL", 1,
  170. createPhysicalPageSizeInch(10.5f, 7.25f),
  171. createLogicalPageRect(60, 0, 3030, 2175), true));
  172. pageDefinitions.add(new PCLPageDefinition("LedgerL", 6,
  173. createPhysicalPageSizeInch(17, 11),
  174. createLogicalPageRect(60, 0, 4980, 3300), true));
  175. pageDefinitions.add(new PCLPageDefinition("A5L", 25,
  176. createPhysicalPageSizeMm(210, 148),
  177. createLogicalPageRect(59, 0, 2362, 1747), true));
  178. pageDefinitions.add(new PCLPageDefinition("A4L", 26,
  179. createPhysicalPageSizeMm(297, 210),
  180. createLogicalPageRect(59, 0, 3389, 2480), true));
  181. pageDefinitions.add(new PCLPageDefinition("A3L", 27,
  182. createPhysicalPageSizeMm(420, 297),
  183. createLogicalPageRect(59, 0, 4842, 3507), true));
  184. }
  185. }