Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.area;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.AffineTransform;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.Serializable;
  23. import org.apache.fop.datatypes.FODimension;
  24. import org.apache.fop.fo.Constants;
  25. /**
  26. * Describe a PDF or PostScript style coordinate transformation matrix (CTM).
  27. * The matrix encodes translations, scaling and rotations of the coordinate
  28. * system used to render pages.
  29. */
  30. public class CTM implements Serializable {
  31. private double a, b, c, d, e, f;
  32. private static final CTM CTM_LRTB = new CTM(1, 0, 0, 1, 0, 0);
  33. private static final CTM CTM_RLTB = new CTM(-1, 0, 0, 1, 0, 0);
  34. private static final CTM CTM_TBRL = new CTM(0, 1, -1, 0, 0, 0);
  35. /**
  36. * Create the identity matrix
  37. */
  38. public CTM() {
  39. a = 1;
  40. b = 0;
  41. c = 0;
  42. d = 1;
  43. e = 0;
  44. f = 0;
  45. }
  46. /**
  47. * Initialize a CTM from the passed arguments.
  48. *
  49. * @param a the x scale
  50. * @param b the x shear
  51. * @param c the y shear
  52. * @param d the y scale
  53. * @param e the x shift
  54. * @param f the y shift
  55. */
  56. public CTM(double a, double b, double c, double d, double e, double f) {
  57. this.a = a;
  58. this.b = b;
  59. this.c = c;
  60. this.d = d;
  61. this.e = e;
  62. this.f = f;
  63. }
  64. /**
  65. * Initialize a CTM to the identity matrix with a translation
  66. * specified by x and y
  67. *
  68. * @param x the x shift
  69. * @param y the y shift.
  70. */
  71. public CTM(double x, double y) {
  72. this.a = 1;
  73. this.b = 0;
  74. this.c = 0;
  75. this.d = 1;
  76. this.e = x;
  77. this.f = y;
  78. }
  79. /**
  80. * Initialize a CTM with the values of another CTM.
  81. *
  82. * @param ctm another CTM
  83. */
  84. protected CTM(CTM ctm) {
  85. this.a = ctm.a;
  86. this.b = ctm.b;
  87. this.c = ctm.c;
  88. this.d = ctm.d;
  89. this.e = ctm.e;
  90. this.f = ctm.f;
  91. }
  92. /**
  93. * Initialize a CTM with the values of an AffineTransform.
  94. *
  95. * @param at the transformation matrix
  96. */
  97. public CTM(AffineTransform at) {
  98. double[] matrix = new double[6];
  99. at.getMatrix(matrix);
  100. this.a = matrix[0];
  101. this.b = matrix[1];
  102. this.c = matrix[2];
  103. this.d = matrix[3];
  104. this.e = matrix[4];
  105. this.f = matrix[5];
  106. }
  107. /**
  108. * Return a CTM which will transform coordinates for a particular writing-mode
  109. * into normalized first quandrant coordinates.
  110. * @param wm A writing mode constant from fo.properties.WritingMode, ie.
  111. * one of LR_TB, RL_TB, TB_RL.
  112. * @param ipd The inline-progression dimension of the reference area whose
  113. * CTM is being set..
  114. * @param bpd The block-progression dimension of the reference area whose
  115. * CTM is being set.
  116. * @return a new CTM with the required transform
  117. */
  118. public static CTM getWMctm(int wm, int ipd, int bpd) {
  119. CTM wmctm;
  120. switch (wm) {
  121. case Constants.EN_LR_TB:
  122. return new CTM(CTM_LRTB);
  123. case Constants.EN_RL_TB:
  124. wmctm = new CTM(CTM_RLTB);
  125. wmctm.e = ipd;
  126. return wmctm;
  127. //return CTM_RLTB.translate(ipd, 0);
  128. case Constants.EN_TB_RL: // CJK
  129. wmctm = new CTM(CTM_TBRL);
  130. wmctm.e = bpd;
  131. return wmctm;
  132. //return CTM_TBRL.translate(0, ipd);
  133. default:
  134. return null;
  135. }
  136. }
  137. /**
  138. * Multiply new passed CTM with this one and generate a new result CTM.
  139. * @param premult The CTM to multiply with this one. The new one will be
  140. * the first multiplicand.
  141. * @return CTM The result of multiplying premult * this.
  142. */
  143. public CTM multiply(CTM premult) {
  144. CTM result = new CTM ((premult.a * a) + (premult.b * c),
  145. (premult.a * b) + (premult.b * d),
  146. (premult.c * a) + (premult.d * c),
  147. (premult.c * b) + (premult.d * d),
  148. (premult.e * a) + (premult.f * c) + e,
  149. (premult.e * b) + (premult.f * d) + f);
  150. return result;
  151. }
  152. /**
  153. * Rotate this CTM by "angle" radians and return a new result CTM.
  154. * This is used to account for reference-orientation.
  155. * @param angle The angle in radians. Positive angles are measured counter-
  156. * clockwise.
  157. * @return CTM The result of rotating this CTM.
  158. */
  159. public CTM rotate(double angle) {
  160. double cos, sin;
  161. if (angle == 90.0 || angle == -270.0) {
  162. cos = 0.0;
  163. sin = 1.0;
  164. } else if (angle == 270.0 || angle == -90.0) {
  165. cos = 0.0;
  166. sin = -1.0;
  167. } else if (angle == 180.0 || angle == -180.0) {
  168. cos = -1.0;
  169. sin = 0.0;
  170. } else {
  171. double rad = Math.toRadians(angle);
  172. cos = Math.cos(rad);
  173. sin = Math.sin(rad);
  174. }
  175. CTM rotate = new CTM(cos, -sin, sin, cos, 0, 0);
  176. return multiply(rotate);
  177. }
  178. /**
  179. * Translate this CTM by the passed x and y values and return a new result CTM.
  180. * @param x The amount to translate along the x axis.
  181. * @param y The amount to translate along the y axis.
  182. * @return CTM The result of translating this CTM.
  183. */
  184. public CTM translate(double x, double y) {
  185. CTM translate = new CTM(1, 0, 0, 1, x, y);
  186. return multiply(translate);
  187. }
  188. /**
  189. * Scale this CTM by the passed x and y values and return a new result CTM.
  190. * @param x The amount to scale along the x axis.
  191. * @param y The amount to scale along the y axis.
  192. * @return CTM The result of scaling this CTM.
  193. */
  194. public CTM scale(double x, double y) {
  195. CTM scale = new CTM(x, 0, 0, y, 0, 0);
  196. return multiply(scale);
  197. }
  198. /**
  199. * Transform a rectangle by the CTM to produce a rectangle in the transformed
  200. * coordinate system.
  201. * @param inRect The rectangle in the original coordinate system
  202. * @return Rectangle2D The rectangle in the transformed coordinate system.
  203. */
  204. public Rectangle2D transform(Rectangle2D inRect) {
  205. // Store as 2 sets of 2 points and transform those, then
  206. // recalculate the width and height
  207. int x1t = (int)(inRect.getX() * a + inRect.getY() * c + e);
  208. int y1t = (int)(inRect.getX() * b + inRect.getY() * d + f);
  209. int x2t = (int)((inRect.getX() + inRect.getWidth()) * a
  210. + (inRect.getY() + inRect.getHeight()) * c + e);
  211. int y2t = (int)((inRect.getX() + inRect.getWidth()) * b
  212. + (inRect.getY() + inRect.getHeight()) * d + f);
  213. // Normalize with x1 < x2
  214. if (x1t > x2t) {
  215. int tmp = x2t;
  216. x2t = x1t;
  217. x1t = tmp;
  218. }
  219. if (y1t > y2t) {
  220. int tmp = y2t;
  221. y2t = y1t;
  222. y1t = tmp;
  223. }
  224. return new Rectangle(x1t, y1t, x2t - x1t, y2t - y1t);
  225. }
  226. /**
  227. * Get string for this transform.
  228. *
  229. * @return a string with the transform values
  230. */
  231. public String toString() {
  232. return "[" + a + " " + b + " " + c + " " + d + " " + e + " "
  233. + f + "]";
  234. }
  235. /**
  236. * Get an array containing the values of this transform.
  237. * This creates and returns a new transform with the values in it.
  238. *
  239. * @return an array containing the transform values
  240. */
  241. public double[] toArray() {
  242. return new double[]{a, b, c, d, e, f};
  243. }
  244. /**
  245. * Returns this CTM as an AffineTransform object.
  246. * @return the AffineTransform representation
  247. */
  248. public AffineTransform toAffineTransform() {
  249. return new AffineTransform(toArray());
  250. }
  251. /**
  252. * Construct a coordinate transformation matrix (CTM).
  253. * @param absRefOrient absolute reference orientation
  254. * @param writingMode the writing mode
  255. * @param absVPrect absolute viewpoint rectangle
  256. * @param reldims relative dimensions
  257. * @return CTM the coordinate transformation matrix (CTM)
  258. */
  259. public static CTM getCTMandRelDims(int absRefOrient,
  260. int writingMode,
  261. Rectangle2D absVPrect,
  262. FODimension reldims) {
  263. int width, height;
  264. // We will use the absolute reference-orientation to set up the CTM.
  265. // The value here is relative to its ancestor reference area.
  266. if (absRefOrient % 180 == 0) {
  267. width = (int) absVPrect.getWidth();
  268. height = (int) absVPrect.getHeight();
  269. } else {
  270. // invert width and height since top left are rotated by 90 (cl or ccl)
  271. height = (int) absVPrect.getWidth();
  272. width = (int) absVPrect.getHeight();
  273. }
  274. /* Set up the CTM for the content of this reference area.
  275. * This will transform region content coordinates in
  276. * writing-mode relative into absolute page-relative
  277. * which will then be translated based on the position of
  278. * the region viewport.
  279. * (Note: scrolling between region vp and ref area when
  280. * doing online content!)
  281. */
  282. CTM ctm = new CTM(absVPrect.getX(), absVPrect.getY());
  283. // First transform for rotation
  284. if (absRefOrient != 0) {
  285. // Rotation implies translation to keep the drawing area in the
  286. // first quadrant. Note: rotation is counter-clockwise
  287. switch (absRefOrient) {
  288. case 90:
  289. case -270:
  290. ctm = ctm.translate(0, width); // width = absVPrect.height
  291. break;
  292. case 180:
  293. case -180:
  294. ctm = ctm.translate(width, height);
  295. break;
  296. case 270:
  297. case -90:
  298. ctm = ctm.translate(height, 0); // height = absVPrect.width
  299. break;
  300. default:
  301. throw new RuntimeException();
  302. }
  303. ctm = ctm.rotate(absRefOrient);
  304. }
  305. /* Since we've already put adjusted width and height values for the
  306. * top and left positions implied by the reference-orientation, we
  307. * can set ipd and bpd appropriately based on the writing mode.
  308. */
  309. if (writingMode == Constants.EN_LR_TB || writingMode == Constants.EN_RL_TB) {
  310. reldims.ipd = width;
  311. reldims.bpd = height;
  312. } else {
  313. reldims.ipd = height;
  314. reldims.bpd = width;
  315. }
  316. // Set a rectangle to be the writing-mode relative version???
  317. // Now transform for writing mode
  318. return ctm.multiply(CTM.getWMctm(writingMode, reldims.ipd, reldims.bpd));
  319. }
  320. }