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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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.xmlgraphics.util.DoubleFormatUtil;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.fonts.FontInfo;
  24. /**
  25. * Utility functions for the intermediate format.
  26. */
  27. public final class IFUtil {
  28. private IFUtil() {
  29. }
  30. private static String format(double value) {
  31. if (value == -0.0) {
  32. //Don't allow negative zero because of testing
  33. //See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3
  34. value = 0.0;
  35. }
  36. StringBuffer buf = new StringBuffer();
  37. DoubleFormatUtil.formatDouble(value, 6, 6, buf);
  38. return buf.toString();
  39. }
  40. /**
  41. * Converts an {@link AffineTransform} instance to an SVG style transform method.
  42. * @param transform the transformation matrix
  43. * @param sb the StringBuffer to write the transform method to
  44. * @return the StringBuffer passed to this method
  45. */
  46. public static StringBuffer toString(AffineTransform transform, StringBuffer sb) {
  47. if (transform.isIdentity()) {
  48. return sb;
  49. }
  50. double[] matrix = new double[6];
  51. transform.getMatrix(matrix);
  52. if (matrix[0] == 1 && matrix[3] == 1 && matrix[1] == 0 && matrix[2] == 0) {
  53. sb.append("translate(");
  54. sb.append(format(matrix[4]));
  55. if (matrix[5] != 0) {
  56. sb.append(',').append(format(matrix[5]));
  57. }
  58. } else {
  59. sb.append("matrix(");
  60. for (int i = 0; i < 6; i++) {
  61. if (i > 0) {
  62. sb.append(',');
  63. }
  64. sb.append(format(matrix[i]));
  65. }
  66. }
  67. sb.append(')');
  68. return sb;
  69. }
  70. /**
  71. * Converts an {@link AffineTransform} array to an SVG style transform method sequence.
  72. * @param transforms the transformation matrix array
  73. * @param sb the StringBuffer to write the transform method sequence to
  74. * @return the StringBuffer passed to this method
  75. */
  76. public static StringBuffer toString(AffineTransform[] transforms, StringBuffer sb) {
  77. for (int i = 0, c = transforms.length; i < c; i++) {
  78. if (i > 0) {
  79. sb.append(' ');
  80. }
  81. toString(transforms[i], sb);
  82. }
  83. return sb;
  84. }
  85. /**
  86. * Converts an {@link AffineTransform} array to an SVG style transform method sequence.
  87. * @param transforms the transformation matrix array
  88. * @return the formatted array
  89. */
  90. public static String toString(AffineTransform[] transforms) {
  91. return toString(transforms, new StringBuffer()).toString();
  92. }
  93. /**
  94. * Converts an {@link AffineTransform} instance to an SVG style transform method.
  95. * @param transform the transformation matrix
  96. * @return the formatted array
  97. */
  98. public static String toString(AffineTransform transform) {
  99. return toString(transform, new StringBuffer()).toString();
  100. }
  101. /**
  102. * Converts an array of integer coordinates into a space-separated string.
  103. * @param coordinates the coordinates
  104. * @return the space-separated array of coordinates
  105. */
  106. public static String toString(int[] coordinates) {
  107. if (coordinates == null) {
  108. return "";
  109. }
  110. StringBuffer sb = new StringBuffer();
  111. for (int i = 0, c = coordinates.length; i < c; i++) {
  112. if (i > 0) {
  113. sb.append(' ');
  114. }
  115. sb.append(Integer.toString(coordinates[i]));
  116. }
  117. return sb.toString();
  118. }
  119. /**
  120. * Converts a rectangle into a space-separated string.
  121. * @param rect the rectangle
  122. * @return the space-separated array of coordinates
  123. */
  124. public static String toString(Rectangle rect) {
  125. if (rect == null) {
  126. return "";
  127. }
  128. StringBuffer sb = new StringBuffer();
  129. sb.append(rect.x).append(' ').append(rect.y).append(' ');
  130. sb.append(rect.width).append(' ').append(rect.height);
  131. return sb.toString();
  132. }
  133. /**
  134. * Sets up the fonts on a document handler. If the document handler provides a configurator
  135. * object the configuration from the {@link org.apache.fop.apps.FopFactory} will be used.
  136. * Otherwise, a default font configuration will be set up.
  137. * @param documentHandler the document handler
  138. * @param fontInfo the font info object (may be null)
  139. * @throws FOPException if an error occurs while setting up the fonts
  140. */
  141. public static void setupFonts(IFDocumentHandler documentHandler, FontInfo fontInfo)
  142. throws FOPException {
  143. if (fontInfo == null) {
  144. fontInfo = new FontInfo();
  145. }
  146. if (documentHandler instanceof IFSerializer) {
  147. IFSerializer serializer = (IFSerializer)documentHandler;
  148. if (serializer.getMimickedDocumentHandler() != null) {
  149. //Use the mimicked document handler's configurator to set up fonts
  150. documentHandler = serializer.getMimickedDocumentHandler();
  151. }
  152. }
  153. IFDocumentHandlerConfigurator configurator = documentHandler.getConfigurator();
  154. if (configurator != null) {
  155. configurator.setupFontInfo(documentHandler.getMimeType(), fontInfo);
  156. documentHandler.setFontInfo(fontInfo);
  157. } else {
  158. documentHandler.setDefaultFontInfo(fontInfo);
  159. }
  160. }
  161. /**
  162. * Sets up the fonts on a document handler. If the document handler provides a configurator
  163. * object the configuration from the {@link org.apache.fop.apps.FopFactory} will be used.
  164. * Otherwise, a default font configuration will be set up.
  165. * @param documentHandler the document handler
  166. * @throws FOPException if an error occurs while setting up the fonts
  167. */
  168. public static void setupFonts(IFDocumentHandler documentHandler) throws FOPException {
  169. setupFonts(documentHandler, null);
  170. }
  171. /**
  172. * Returns the MIME type of the output format that the given document handler is supposed to
  173. * handle. If the document handler is an {@link IFSerializer} it returns the MIME type of the
  174. * document handler it is mimicking.
  175. * @param documentHandler the document handler
  176. * @return the effective MIME type
  177. */
  178. public static String getEffectiveMIMEType(IFDocumentHandler documentHandler) {
  179. if (documentHandler instanceof IFSerializer) {
  180. IFDocumentHandler mimic = ((IFSerializer)documentHandler).getMimickedDocumentHandler();
  181. if (mimic != null) {
  182. return mimic.getMimeType();
  183. }
  184. }
  185. return documentHandler.getMimeType();
  186. }
  187. /**
  188. * Convert the general gpos 'dp' adjustments to the older 'dx' adjustments.
  189. * This utility method is used to provide backward compatibility in implementations
  190. * of IFPainter that have not yet been upgraded to the general position adjustments format.
  191. * @param dp an array of 4-tuples, expressing [X,Y] placment
  192. * adjustments and [X,Y] advancement adjustments, in that order (may be null)
  193. * @param count if <code>dp</code> is not null, then a count of dp values to convert
  194. * @return if <code>dp</code> is not null, then an array of adjustments to the current
  195. * x position prior to rendering individual glyphs; otherwise, null
  196. */
  197. public static int[] convertDPToDX(int[][] dp, int count) {
  198. int[] dx;
  199. if (dp != null) {
  200. dx = new int [ count ];
  201. for (int i = 0, n = count; i < n; i++) {
  202. if (dp [ i ] != null) {
  203. dx [ i ] = dp [ i ] [ 0 ]; // xPlaAdjust[i]
  204. }
  205. }
  206. } else {
  207. dx = null;
  208. }
  209. return dx;
  210. }
  211. /**
  212. * Convert the general gpos 'dp' adjustments to the older 'dx' adjustments.
  213. * This utility method is used to provide backward compatibility in implementations
  214. * of IFPainter that have not yet been upgraded to the general position adjustments format.
  215. * @param dp an array of 4-tuples, expressing [X,Y] placment
  216. * adjustments and [X,Y] advancement adjustments, in that order (may be null)
  217. * @return if <code>dp</code> is not null, then an array of adjustments to the current
  218. * x position prior to rendering individual glyphs; otherwise, null
  219. */
  220. public static int[] convertDPToDX(int[][] dp) {
  221. return convertDPToDX(dp, (dp != null) ? dp.length : 0);
  222. }
  223. /**
  224. * Convert the general gpos 'dp' adjustments to the older 'dx' adjustments.
  225. * This utility method is used to provide backward compatibility in implementations
  226. * of IFPainter that have not yet been upgraded to the general position adjustments format.
  227. * @param dx an array of adjustments to the current x position prior to rendering
  228. * individual glyphs or null
  229. * @param count if <code>dx</code> is not null, then a count of dx values to convert
  230. * @return if <code>dx</code> is not null, then an array of 4-tuples, expressing [X,Y]
  231. * placment adjustments and [X,Y] advancement adjustments, in that order; otherwise, null
  232. */
  233. public static int[][] convertDXToDP(int[] dx, int count) {
  234. int[][] dp;
  235. if (dx != null) {
  236. dp = new int [ count ] [ 4 ];
  237. for (int i = 0, n = count; i < n; i++) {
  238. int[] pa = dp [ i ];
  239. int d = dx [ i ];
  240. pa [ 0 ] = d; // xPlaAdjust[i]
  241. pa [ 2 ] = d; // xAdvAdjust[i]
  242. }
  243. } else {
  244. dp = null;
  245. }
  246. return dp;
  247. }
  248. /**
  249. * Convert the general gpos 'dp' adjustments to the older 'dx' adjustments.
  250. * This utility method is used to provide backward compatibility in implementations
  251. * of IFPainter that have not yet been upgraded to the general position adjustments format.
  252. * @param dx an array of adjustments to the current x position prior to rendering
  253. * individual glyphs or null
  254. * @return if <code>dx</code> is not null, then an array of 4-tuples, expressing [X,Y]
  255. * placment adjustments and [X,Y] advancement adjustments, in that order; otherwise, null
  256. */
  257. public static int[][] convertDXToDP(int[] dx) {
  258. return convertDXToDP(dx, (dx != null) ? dx.length : 0);
  259. }
  260. /**
  261. * Determine if position adjustment is the identity adjustment, i.e., no non-zero adjustment.
  262. * @param pa a 4-tuple, expressing [X,Y] placment and [X,Y] advance adjuustments (may be null)
  263. * @return true if <code>dp</code> is null or contains no non-zero adjustment
  264. */
  265. public static boolean isPAIdentity(int[] pa) {
  266. if (pa == null) {
  267. return true;
  268. } else {
  269. for (int k = 0; k < 4; k++) {
  270. if (pa[k] != 0) {
  271. return false;
  272. }
  273. }
  274. return true;
  275. }
  276. }
  277. /**
  278. * Determine if position adjustments is the identity adjustment, i.e., no non-zero adjustment.
  279. * @param dp an array of 4-tuples, expressing [X,Y] placment
  280. * adjustments and [X,Y] advancement adjustments, in that order (may be null)
  281. * @return true if <code>dp</code> is null or contains no non-zero adjustment
  282. */
  283. public static boolean isDPIdentity(int[][] dp) {
  284. if (dp == null) {
  285. return true;
  286. } else {
  287. for (int[] aDp : dp) {
  288. if (!isPAIdentity(aDp)) {
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. }
  295. /**
  296. * Determine if position adjustments comprises only DX adjustments as encoded by
  297. * {@link #convertDPToDX}. Note that if given a set of all all zero position
  298. * adjustments, both this method and {@link #isDPIdentity} will return true;
  299. * however, this method may return true when {@link #isDPIdentity} returns false.
  300. * @param dp an array of 4-tuples, expressing [X,Y] placment
  301. * adjustments and [X,Y] advancement adjustments, in that order (may be null)
  302. * @return true if <code>dp</code> is not null and contains only xPlaAdjust
  303. * and xAdvAdjust values consistent with the output of {@link #convertDPToDX}.
  304. */
  305. public static boolean isDPOnlyDX(int[][] dp) {
  306. if (dp == null) {
  307. return false;
  308. } else {
  309. for (int[] pa : dp) {
  310. if ((pa != null) && (pa[0] != pa[2])) {
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. }
  317. /**
  318. * Adjust a position adjustments array. If both <code>paDst</code> and <code>paSrc</code> are
  319. * non-null, then <code>paSrc[i]</code> is added to <code>paDst[i]</code>.
  320. * @param paDst a 4-tuple, expressing [X,Y] placment
  321. * and [X,Y] advance adjuustments (may be null)
  322. * @param paSrc a 4-tuple, expressing [X,Y] placment
  323. * and [X,Y] advance adjuustments (may be null)
  324. */
  325. public static void adjustPA(int[] paDst, int[] paSrc) {
  326. if ((paDst != null) && (paSrc != null)) {
  327. assert paDst.length == 4;
  328. assert paSrc.length == 4;
  329. for (int i = 0; i < 4; i++) {
  330. paDst[i] += paSrc[i];
  331. }
  332. }
  333. }
  334. /**
  335. * Copy entries from position adjustments.
  336. * @param dp an array of 4-tuples, expressing [X,Y] placment
  337. * adjustments and [X,Y] advancement adjustments, in that order
  338. * @param offset starting offset from which to copy
  339. * @param count number of entries to copy
  340. * @return a deep copy of the count position adjustment entries start at
  341. * offset
  342. */
  343. public static int[][] copyDP(int[][] dp, int offset, int count) {
  344. if ((dp == null) || (offset > dp.length) || ((offset + count) > dp.length)) {
  345. throw new IllegalArgumentException();
  346. } else {
  347. int[][] dpNew = new int [ count ] [];
  348. for (int i = 0, n = count; i < n; i++) {
  349. int[] paSrc = dp [ i + offset ];
  350. if (paSrc != null) {
  351. int[] paDst = new int [ 4 ];
  352. System.arraycopy(paSrc, 0, paDst, 0, 4);
  353. dpNew [ i ] = paDst;
  354. }
  355. }
  356. return dpNew;
  357. }
  358. }
  359. }