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

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