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.

AFPUnitConverter.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.afp;
  19. import java.awt.geom.AffineTransform;
  20. /**
  21. * AFP Unit converter
  22. */
  23. public class AFPUnitConverter {
  24. /** the AFP state */
  25. private final AFPPaintingState paintingState;
  26. /**
  27. * Unit converter
  28. *
  29. * @param paintingState the AFP painting state
  30. */
  31. public AFPUnitConverter(AFPPaintingState paintingState) {
  32. this.paintingState = paintingState;
  33. }
  34. /**
  35. * Converts millipoints to units
  36. *
  37. * @param srcPts source points
  38. * @param dstPts destination points
  39. * @return transformed points
  40. */
  41. public int[] mpts2units(float[] srcPts, float[] dstPts) {
  42. return transformPoints(srcPts, dstPts, true);
  43. }
  44. /**
  45. * Converts points to units
  46. *
  47. * @param srcPts source points
  48. * @param dstPts destination points
  49. * @return transformed points
  50. */
  51. public int[] pts2units(float[] srcPts, float[] dstPts) {
  52. return transformPoints(srcPts, dstPts, false);
  53. }
  54. /**
  55. * Converts millipoints to units
  56. *
  57. * @param srcPts source points
  58. * @return transformed points
  59. */
  60. public int[] mpts2units(float[] srcPts) {
  61. return transformPoints(srcPts, null, true);
  62. }
  63. /**
  64. * Converts points to units
  65. *
  66. * @param srcPts source points
  67. * @return transformed points
  68. */
  69. public int[] pts2units(float[] srcPts) {
  70. return transformPoints(srcPts, null, false);
  71. }
  72. /**
  73. * Converts point to unit
  74. *
  75. * @param pt point
  76. * @return transformed point
  77. */
  78. public float pt2units(float pt) {
  79. return pt / ((float)AFPConstants.DPI_72 / paintingState.getResolution());
  80. }
  81. /**
  82. * Converts millipoint to unit
  83. *
  84. * @param mpt millipoint
  85. * @return transformed point
  86. */
  87. public float mpt2units(float mpt) {
  88. return mpt / ((float)AFPConstants.DPI_72_MPTS / paintingState.getResolution());
  89. }
  90. private int[] transformPoints(float[] srcPts, float[] dstPts, boolean milli) {
  91. if (dstPts == null) {
  92. dstPts = new float[srcPts.length];
  93. }
  94. AffineTransform at = paintingState.getData().getTransform();
  95. at.transform(srcPts, 0, dstPts, 0, srcPts.length / 2);
  96. int[] coords = new int[srcPts.length];
  97. for (int i = 0; i < srcPts.length; i++) {
  98. if (!milli) {
  99. dstPts[i] *= 1000;
  100. }
  101. coords[i] = Math.round(dstPts[i]);
  102. }
  103. return coords;
  104. }
  105. }