You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AFPGraphicsConfiguration.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.afp;
  19. import java.awt.GraphicsDevice;
  20. import java.awt.Rectangle;
  21. import java.awt.Transparency;
  22. import java.awt.geom.AffineTransform;
  23. import java.awt.image.BufferedImage;
  24. import java.awt.image.ColorModel;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.fop.svg.GraphicsConfiguration;
  28. /**
  29. * Our implementation of the class that returns information about
  30. * roughly what we can handle and want to see (alpha for example).
  31. */
  32. public class AFPGraphicsConfiguration extends GraphicsConfiguration {
  33. // We use this to get a good colormodel..
  34. private static final BufferedImage BI_WITH_ALPHA
  35. = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  36. // We use this to get a good colormodel..
  37. private static final BufferedImage BI_WITHOUT_ALPHA
  38. = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  39. /**
  40. * Construct a buffered image with an alpha channel, unless
  41. * transparencty is OPAQUE (no alpha at all).
  42. *
  43. * @param width the width of the image
  44. * @param height the height of the image
  45. * @param transparency the alpha value of the image
  46. * @return the new buffered image
  47. */
  48. public BufferedImage createCompatibleImage(int width, int height,
  49. int transparency) {
  50. if (transparency == Transparency.OPAQUE) {
  51. return new BufferedImage(width, height,
  52. BufferedImage.TYPE_INT_RGB);
  53. } else {
  54. return new BufferedImage(width, height,
  55. BufferedImage.TYPE_INT_ARGB);
  56. }
  57. }
  58. /**
  59. * Construct a buffered image with an alpha channel.
  60. *
  61. * @param width the width of the image
  62. * @param height the height of the image
  63. * @return the new buffered image
  64. */
  65. public BufferedImage createCompatibleImage(int width, int height) {
  66. return new BufferedImage(width, height,
  67. BufferedImage.TYPE_INT_ARGB);
  68. }
  69. /**
  70. * TODO: This should return the page bounds in Pts,
  71. * I couldn't figure out how to get this for the current
  72. * page from the PDFDocument (this still works for now,
  73. * but it should be fixed...).
  74. *
  75. * @return the bounds of the PDF document page
  76. */
  77. public Rectangle getBounds() {
  78. return null;
  79. }
  80. /**
  81. * Return a good default color model for this 'device'.
  82. * @return the colour model for the configuration
  83. */
  84. public ColorModel getColorModel() {
  85. return BI_WITH_ALPHA.getColorModel();
  86. }
  87. /**
  88. * Return a good color model given <tt>transparency</tt>
  89. *
  90. * @param transparency the alpha value for the colour model
  91. * @return the colour model for the configuration
  92. */
  93. public ColorModel getColorModel(int transparency) {
  94. if (transparency == Transparency.OPAQUE) {
  95. return BI_WITHOUT_ALPHA.getColorModel();
  96. } else {
  97. return BI_WITH_ALPHA.getColorModel();
  98. }
  99. }
  100. private static final Log log = LogFactory.getLog(AFPGraphicsConfiguration.class);
  101. private AffineTransform defaultTransform = null;
  102. private AffineTransform normalizingTransform = null;
  103. private GraphicsDevice graphicsDevice = null;
  104. /**
  105. * The default transform (1:1).
  106. *
  107. * @return the default transform for the configuration
  108. */
  109. public AffineTransform getDefaultTransform() {
  110. log.debug("getDefaultTransform()");
  111. if (defaultTransform == null) {
  112. defaultTransform = new AffineTransform();
  113. }
  114. return defaultTransform;
  115. }
  116. /**
  117. * The normalizing transform (1:1) (since we currently
  118. * render images at 72dpi, which we might want to change
  119. * in the future).
  120. *
  121. * @return the normalizing transform for the configuration
  122. */
  123. public AffineTransform getNormalizingTransform() {
  124. log.debug("getNormalizingTransform()");
  125. if (normalizingTransform == null) {
  126. normalizingTransform = new AffineTransform(2, 0, 0, 2, 0, 0);
  127. }
  128. return normalizingTransform;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public GraphicsDevice getDevice() {
  134. log.debug("getDevice()");
  135. if (graphicsDevice == null) {
  136. graphicsDevice = new AFPGraphicsDevice(this);
  137. }
  138. return graphicsDevice;
  139. }
  140. }