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 3.8KB

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