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.

AbstractGraphicsCoord.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.goca;
  19. import org.apache.fop.afp.modca.AbstractPreparedAFPObject;
  20. import org.apache.fop.afp.util.BinaryUtils;
  21. /**
  22. * A base class encapsulating the structure of coordinate based GOCA objects
  23. */
  24. public abstract class AbstractGraphicsCoord extends AbstractPreparedAFPObject {
  25. /** array of x/y coordinates */
  26. protected int[] coords = null;
  27. /**
  28. * Constructor
  29. *
  30. * @param coords the x/y coordinates for this object
  31. */
  32. public AbstractGraphicsCoord(int[] coords) {
  33. this.coords = coords;
  34. prepareData();
  35. }
  36. /**
  37. * Constructor
  38. *
  39. * @param x the x coordinate for this object
  40. * @param y the y coordinate for this object
  41. */
  42. public AbstractGraphicsCoord(int x, int y) {
  43. this(new int[] {x, y});
  44. }
  45. /**
  46. * Constructor
  47. *
  48. * @param x1 the x1 coordinate for this object
  49. * @param y1 the y1 coordinate for this object
  50. * @param x2 the x2 coordinate for this object
  51. * @param y2 the y2 coordinate for this object
  52. */
  53. public AbstractGraphicsCoord(int x1, int y1, int x2, int y2) {
  54. this(new int[] {x1, y1, x2, y2});
  55. }
  56. /**
  57. * Returns the order code to use
  58. *
  59. * @return the order code to use
  60. */
  61. protected abstract byte getOrderCode();
  62. /**
  63. * Returns the length of this order code (typically this is the same as the coordinate length)
  64. *
  65. * @return the length of this order code
  66. */
  67. protected int getLength() {
  68. return this.coords.length * 2;
  69. }
  70. /**
  71. * Creates a newly created and initialized byte data
  72. *
  73. * @return a newly created and initialized byte data
  74. */
  75. protected byte[] createData() {
  76. int len = getLength();
  77. byte[] data = new byte[len + 2];
  78. data[0] = getOrderCode(); // ORDER CODE
  79. data[1] = (byte)len; // LENGTH
  80. return data;
  81. }
  82. /** {@inheritDoc} */
  83. protected void prepareData() {
  84. super.data = createData();
  85. int fromIndex = data.length - getLength();
  86. addCoords(data, fromIndex);
  87. }
  88. /**
  89. * Adds the coordinates to the structured field data
  90. *
  91. * @param data the structured field data
  92. * @param fromIndex the start index
  93. */
  94. protected void addCoords(byte[] data, int fromIndex) {
  95. // X/Y POS
  96. for (int i = 0; i < coords.length; i++, fromIndex += 2) {
  97. byte[] coord = BinaryUtils.convert(coords[i], 2);
  98. data[fromIndex] = coord[0];
  99. data[fromIndex + 1] = coord[1];
  100. }
  101. }
  102. /**
  103. * Returns the short name of this GOCA object
  104. *
  105. * @return the short name of this GOCA object
  106. */
  107. public String getName() {
  108. String className = getClass().getName();
  109. return className.substring(className.lastIndexOf(".") + 1);
  110. }
  111. /** {@inheritDoc} */
  112. public String toString() {
  113. String coordsStr = "";
  114. for (int i = 0; i < coords.length; i++) {
  115. coordsStr += (i % 2 == 0) ? "x" : "y";
  116. coordsStr += (i / 2) + "=" + coords[i] + ",";
  117. }
  118. coordsStr = coordsStr.substring(0, coordsStr.length() - 1);
  119. return getName() + "{" + coordsStr + "}";
  120. }
  121. }