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.

CTM.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * $Id: CTM.java,v 1.8 2003/03/05 15:19:31 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.area;
  52. import java.awt.geom.Rectangle2D;
  53. import java.awt.Rectangle;
  54. import java.io.Serializable;
  55. import org.apache.fop.fo.properties.WritingMode;
  56. /**
  57. * Describe a PDF or PostScript style coordinate transformation matrix (CTM).
  58. * The matrix encodes translations, scaling and rotations of the coordinate
  59. * system used to render pages.
  60. */
  61. public class CTM implements Serializable {
  62. private double a, b, c, d, e, f;
  63. private static final CTM CTM_LRTB = new CTM(1, 0, 0, 1, 0, 0);
  64. private static final CTM CTM_RLTB = new CTM(-1, 0, 0, 1, 0, 0);
  65. private static final CTM CTM_TBRL = new CTM(0, 1, -1, 0, 0, 0);
  66. /**
  67. * Create the identity matrix
  68. */
  69. public CTM() {
  70. a = 1;
  71. b = 0;
  72. c = 0;
  73. d = 1;
  74. e = 0;
  75. f = 0;
  76. }
  77. /**
  78. * Initialize a CTM from the passed arguments.
  79. *
  80. * @param a the x scale
  81. * @param b the x shear
  82. * @param c the y shear
  83. * @param d the y scale
  84. * @param e the x shift
  85. * @param f the y shift
  86. */
  87. public CTM(double a, double b, double c, double d, double e, double f) {
  88. this.a = a;
  89. this.b = b;
  90. this.c = c;
  91. this.d = d;
  92. this.e = e;
  93. this.f = f;
  94. }
  95. /**
  96. * Initialize a CTM to the identity matrix with a translation
  97. * specified by x and y
  98. *
  99. * @param x the x shift
  100. * @param y the y shift.
  101. */
  102. public CTM(double x, double y) {
  103. this.a = 1;
  104. this.b = 0;
  105. this.c = 0;
  106. this.d = 1;
  107. this.e = x;
  108. this.f = y;
  109. }
  110. /**
  111. * Initialize a CTM with the values of another CTM.
  112. *
  113. * @param ctm another CTM
  114. */
  115. protected CTM(CTM ctm) {
  116. this.a = ctm.a;
  117. this.b = ctm.b;
  118. this.c = ctm.c;
  119. this.d = ctm.d;
  120. this.e = ctm.e;
  121. this.f = ctm.f;
  122. }
  123. /**
  124. * Return a CTM which will transform coordinates for a particular writing-mode
  125. * into normalized first quandrant coordinates.
  126. * @param wm A writing mode constant from fo.properties.WritingMode, ie.
  127. * one of LR_TB, RL_TB, TB_RL.
  128. * @param ipd The inline-progression dimension of the reference area whose
  129. * CTM is being set..
  130. * @param bpd The block-progression dimension of the reference area whose
  131. * CTM is being set.
  132. * @return a new CTM with the required transform
  133. */
  134. public static CTM getWMctm(int wm, int ipd, int bpd) {
  135. CTM wmctm;
  136. switch (wm) {
  137. case WritingMode.LR_TB:
  138. return new CTM(CTM_LRTB);
  139. case WritingMode.RL_TB:
  140. {
  141. wmctm = new CTM(CTM_RLTB);
  142. wmctm.e = ipd;
  143. return wmctm;
  144. }
  145. //return CTM_RLTB.translate(ipd, 0);
  146. case WritingMode.TB_RL: // CJK
  147. {
  148. wmctm = new CTM(CTM_TBRL);
  149. wmctm.e = bpd;
  150. return wmctm;
  151. }
  152. //return CTM_TBRL.translate(0, ipd);
  153. default:
  154. return null;
  155. }
  156. }
  157. /**
  158. * Multiply new passed CTM with this one and generate a new result CTM.
  159. * @param premult The CTM to multiply with this one. The new one will be
  160. * the first multiplicand.
  161. * @return CTM The result of multiplying premult * this.
  162. */
  163. public CTM multiply(CTM premult) {
  164. CTM rslt = new CTM ((premult.a * a) + (premult.b * c),
  165. (premult.a * b) + (premult.b * d),
  166. (premult.c * a) + (premult.d * c),
  167. (premult.c * b) + (premult.d * d),
  168. (premult.e * a) + (premult.f * c) + e,
  169. (premult.e * b) + (premult.f * d) + f);
  170. return rslt;
  171. }
  172. /**
  173. * Rotate this CTM by "angle" radians and return a new result CTM.
  174. * This is used to account for reference-orientation.
  175. * @param angle The angle in radians. Positive angles are measured counter-
  176. * clockwise.
  177. * @return CTM The result of rotating this CTM.
  178. */
  179. public CTM rotate(double angle) {
  180. double cos, sin;
  181. if (angle == 90.0) {
  182. cos = 0.0;
  183. sin = 1.0;
  184. } else if (angle == 270.0) {
  185. cos = 0.0;
  186. sin = -1.0;
  187. } else if (angle == 180.0) {
  188. cos = -1.0;
  189. sin = 0.0;
  190. } else {
  191. double rad = Math.toRadians(angle);
  192. cos = Math.cos(rad);
  193. sin = Math.sin(rad);
  194. }
  195. CTM rotate = new CTM(cos, -sin, sin, cos, 0, 0);
  196. return multiply(rotate);
  197. }
  198. /**
  199. * Translate this CTM by the passed x and y values and return a new result CTM.
  200. * @param x The amount to translate along the x axis.
  201. * @param y The amount to translate along the y axis.
  202. * @return CTM The result of translating this CTM.
  203. */
  204. public CTM translate(double x, double y) {
  205. CTM translate = new CTM(1, 0, 0, 1, x, y);
  206. return multiply(translate);
  207. }
  208. /**
  209. * Scale this CTM by the passed x and y values and return a new result CTM.
  210. * @param x The amount to scale along the x axis.
  211. * @param y The amount to scale along the y axis.
  212. * @return CTM The result of scaling this CTM.
  213. */
  214. public CTM scale(double x, double y) {
  215. CTM scale = new CTM(x, 0, 0, y, 0, 0);
  216. return multiply(scale);
  217. }
  218. /**
  219. * Transform a rectangle by the CTM to produce a rectangle in the transformed
  220. * coordinate system.
  221. * @param inRect The rectangle in the original coordinate system
  222. * @return Rectangle2D The rectangle in the transformed coordinate system.
  223. */
  224. public Rectangle2D transform(Rectangle2D inRect) {
  225. // Store as 2 sets of 2 points and transform those, then
  226. // recalculate the width and height
  227. int x1t = (int)(inRect.getX() * a + inRect.getY() * c + e);
  228. int y1t = (int)(inRect.getX() * b + inRect.getY() * d + f);
  229. int x2t = (int)((inRect.getX() + inRect.getWidth()) * a
  230. + (inRect.getY() + inRect.getHeight()) * c + e);
  231. int y2t = (int)((inRect.getX() + inRect.getWidth()) * b
  232. + (inRect.getY() + inRect.getHeight()) * d + f);
  233. // Normalize with x1 < x2
  234. if (x1t > x2t) {
  235. int tmp = x2t;
  236. x2t = x1t;
  237. x1t = tmp;
  238. }
  239. if (y1t > y2t) {
  240. int tmp = y2t;
  241. y2t = y1t;
  242. y1t = tmp;
  243. }
  244. return new Rectangle(x1t, y1t, x2t - x1t, y2t - y1t);
  245. }
  246. /**
  247. * Get string for this transform.
  248. *
  249. * @return a string with the transform values
  250. */
  251. public String toString() {
  252. return "[" + a + " " + b + " " + c + " " + d + " " + e + " "
  253. + f + "]";
  254. }
  255. /**
  256. * Get an array containing the values of this transform.
  257. * This creates and returns a new transform with the values in it.
  258. *
  259. * @return an array containing the transform values
  260. */
  261. public double[] toArray() {
  262. return new double[]{a, b, c, d, e, f};
  263. }
  264. }