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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.afp.util.BinaryUtils;
  22. /**
  23. * A base class encapsulating the structure of coordinate based GOCA objects
  24. */
  25. public abstract class AbstractGraphicsCoord extends AbstractGraphicsDrawingOrder {
  26. /** array of x/y coordinates */
  27. protected int[] coords;
  28. /** if true, then uses relative drawing order */
  29. protected boolean relative;
  30. /**
  31. * Constructor
  32. *
  33. * @param coords the x/y coordinates for this object
  34. */
  35. public AbstractGraphicsCoord(int[] coords) {
  36. if (coords == null) {
  37. relative = true;
  38. } else {
  39. this.coords = coords;
  40. }
  41. }
  42. /**
  43. * Constructor
  44. *
  45. * @param coords the x/y coordinates for this object
  46. * @param relative true if relative drawing order
  47. */
  48. public AbstractGraphicsCoord(int[] coords, boolean relative) {
  49. this(coords);
  50. this.relative = relative;
  51. }
  52. /**
  53. * Constructor
  54. *
  55. * @param x the x coordinate for this object
  56. * @param y the y coordinate for this object
  57. */
  58. public AbstractGraphicsCoord(int x, int y) {
  59. this(new int[] {x, y});
  60. }
  61. /**
  62. * Constructor
  63. *
  64. * @param x1 the x1 coordinate for this object
  65. * @param y1 the y1 coordinate for this object
  66. * @param x2 the x2 coordinate for this object
  67. * @param y2 the y2 coordinate for this object
  68. */
  69. public AbstractGraphicsCoord(int x1, int y1, int x2, int y2) {
  70. this(new int[] {x1, y1, x2, y2});
  71. }
  72. /** {@inheritDoc} */
  73. public int getDataLength() {
  74. return 2 + (coords != null ? coords.length * 2 : 0);
  75. }
  76. /**
  77. * Returns the coordinate data start index
  78. *
  79. * @return the coordinate data start index
  80. */
  81. int getCoordinateDataStartIndex() {
  82. return 2;
  83. }
  84. /**
  85. * Returns the coordinate data
  86. *
  87. * @return the coordinate data
  88. */
  89. byte[] getData() {
  90. byte[] data = super.getData();
  91. if (coords != null) {
  92. addCoords(data, getCoordinateDataStartIndex());
  93. }
  94. return data;
  95. }
  96. /** {@inheritDoc} */
  97. public void writeToStream(OutputStream os) throws IOException {
  98. os.write(getData());
  99. }
  100. /**
  101. * Adds the coordinates to the structured field data
  102. *
  103. * @param data the structured field data
  104. * @param fromIndex the start index
  105. */
  106. protected void addCoords(byte[] data, int fromIndex) {
  107. // X/Y POS
  108. for (int i = 0; i < coords.length; i++, fromIndex += 2) {
  109. byte[] coord = BinaryUtils.convert(coords[i], 2);
  110. data[fromIndex] = coord[0];
  111. data[fromIndex + 1] = coord[1];
  112. }
  113. }
  114. /** {@inheritDoc} */
  115. public String toString() {
  116. String coordsStr = "";
  117. for (int i = 0; i < coords.length; i++) {
  118. coordsStr += (i % 2 == 0) ? "x" : "y";
  119. coordsStr += (i / 2) + "=" + coords[i] + ",";
  120. }
  121. coordsStr = coordsStr.substring(0, coordsStr.length() - 1);
  122. return getName() + "{" + coordsStr + "}";
  123. }
  124. /**
  125. * Returns true if this is a relative drawing order
  126. *
  127. * @return true if this is a relative drawing order
  128. */
  129. protected boolean isRelative() {
  130. return this.relative;
  131. }
  132. }