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.

IFUtil.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.intermediate;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.AffineTransform;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.apps.FopFactory;
  23. import org.apache.fop.fonts.FontInfo;
  24. import org.apache.fop.util.DecimalFormatCache;
  25. /**
  26. * Utility functions for the intermediate format.
  27. */
  28. public class IFUtil {
  29. private static String format(double value) {
  30. if (value == -0.0) {
  31. //Don't allow negative zero because of testing
  32. //See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3
  33. value = 0.0;
  34. }
  35. return DecimalFormatCache.getDecimalFormat(6).format(value);
  36. }
  37. /**
  38. * Converts an {@code AffineTransform} instance to an SVG style transform method.
  39. * @param transform the transformation matrix
  40. * @param sb the StringBuffer to write the transform method to
  41. * @return the StringBuffer passed to this method
  42. */
  43. public static StringBuffer toString(AffineTransform transform, StringBuffer sb) {
  44. if (transform.isIdentity()) {
  45. return sb;
  46. }
  47. double[] matrix = new double[6];
  48. transform.getMatrix(matrix);
  49. if (matrix[0] == 1 && matrix[3] == 1 && matrix[1] == 0 && matrix[2] == 0) {
  50. sb.append("translate(");
  51. sb.append(format(matrix[4]));
  52. if (matrix[5] != 0) {
  53. sb.append(',').append(format(matrix[5]));
  54. }
  55. } else {
  56. sb.append("matrix(");
  57. for (int i = 0; i < 6; i++) {
  58. if (i > 0) {
  59. sb.append(',');
  60. }
  61. sb.append(format(matrix[i]));
  62. }
  63. }
  64. sb.append(')');
  65. return sb;
  66. }
  67. /**
  68. * Converts an {@code AffineTransform} array to an SVG style transform method sequence.
  69. * @param transforms the transformation matrix array
  70. * @param sb the StringBuffer to write the transform method sequence to
  71. * @return the StringBuffer passed to this method
  72. */
  73. public static StringBuffer toString(AffineTransform[] transforms, StringBuffer sb) {
  74. for (int i = 0, c = transforms.length; i < c; i++) {
  75. if (i > 0) {
  76. sb.append(' ');
  77. }
  78. toString(transforms[i], sb);
  79. }
  80. return sb;
  81. }
  82. /**
  83. * Converts an {@code AffineTransform} array to an SVG style transform method sequence.
  84. * @param transforms the transformation matrix array
  85. * @return the formatted array
  86. */
  87. public static String toString(AffineTransform[] transforms) {
  88. return toString(transforms, new StringBuffer()).toString();
  89. }
  90. /**
  91. * Converts an {@code AffineTransform} instance to an SVG style transform method.
  92. * @param transform the transformation matrix
  93. * @return the formatted array
  94. */
  95. public static String toString(AffineTransform transform) {
  96. return toString(transform, new StringBuffer()).toString();
  97. }
  98. /**
  99. * Converts an array of integer coordinates into a space-separated string.
  100. * @param coordinates the coordinates
  101. * @return the space-separated array of coordinates
  102. */
  103. public static String toString(int[] coordinates) {
  104. if (coordinates == null) {
  105. return "";
  106. }
  107. StringBuffer sb = new StringBuffer();
  108. for (int i = 0, c = coordinates.length; i < c; i++) {
  109. if (i > 0) {
  110. sb.append(' ');
  111. }
  112. sb.append(Integer.toString(coordinates[i]));
  113. }
  114. return sb.toString();
  115. }
  116. /**
  117. * Converts a rectangle into a space-separated string.
  118. * @param rect the rectangle
  119. * @return the space-separated array of coordinates
  120. */
  121. public static String toString(Rectangle rect) {
  122. if (rect == null) {
  123. return "";
  124. }
  125. StringBuffer sb = new StringBuffer();
  126. sb.append(rect.x).append(' ').append(rect.y).append(' ');
  127. sb.append(rect.width).append(' ').append(rect.height);
  128. return sb.toString();
  129. }
  130. /**
  131. * Sets up the fonts on a document handler. If the document handler provides a configurator
  132. * object the configuration from the {@link FopFactory} will be used. Otherwise,
  133. * a default font configuration will be set up.
  134. * @param documentHandler the document handler
  135. * @param fontInfo the font info object (may be null)
  136. * @throws FOPException if an error occurs while setting up the fonts
  137. */
  138. public static void setupFonts(IFDocumentHandler documentHandler, FontInfo fontInfo)
  139. throws FOPException {
  140. if (fontInfo == null) {
  141. fontInfo = new FontInfo();
  142. }
  143. IFDocumentHandlerConfigurator configurator = documentHandler.getConfigurator();
  144. if (configurator != null) {
  145. configurator.setupFontInfo(documentHandler, fontInfo);
  146. } else {
  147. documentHandler.setDefaultFontInfo(fontInfo);
  148. }
  149. }
  150. /**
  151. * Sets up the fonts on a document handler. If the document handler provides a configurator
  152. * object the configuration from the {@link FopFactory} will be used. Otherwise,
  153. * a default font configuration will be set up.
  154. * @param documentHandler the document handler
  155. * @throws FOPException if an error occurs while setting up the fonts
  156. */
  157. public static void setupFonts(IFDocumentHandler documentHandler) throws FOPException {
  158. setupFonts(documentHandler, null);
  159. }
  160. /**
  161. * Returns the MIME type of the output format that the given document handler is supposed to
  162. * handle. If the document handler is an {@link IFSerializer} it returns the MIME type of the
  163. * document handler it is mimicking.
  164. * @param documentHandler the document handler
  165. * @return the effective MIME type
  166. */
  167. public static String getEffectiveMIMEType(IFDocumentHandler documentHandler) {
  168. if (documentHandler instanceof IFSerializer) {
  169. IFDocumentHandler mimic = ((IFSerializer)documentHandler).getMimickedDocumentHandler();
  170. if (mimic != null) {
  171. return mimic.getMimeType();
  172. }
  173. }
  174. return documentHandler.getMimeType();
  175. }
  176. }