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.

GraphicsString.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 java.io.UnsupportedEncodingException;
  20. import org.apache.fop.render.afp.modca.AFPConstants;
  21. import org.apache.fop.render.afp.modca.GraphicsObject;
  22. import org.apache.fop.render.afp.tools.BinaryUtils;
  23. /**
  24. * A GOCA graphics string
  25. */
  26. public class GraphicsString extends AbstractPreparedAFPObject {
  27. /** Up to 255 bytes of character data */
  28. private static final int MAX_STR_LEN = 255;
  29. /** drawn from the current position */
  30. private boolean fromCurrentPosition = false;
  31. /** the string to draw */
  32. private String str = null;
  33. /** x coordinate */
  34. private int x;
  35. /** y coordinate */
  36. private int y;
  37. /**
  38. * @param str the character string
  39. */
  40. public GraphicsString(String str) {
  41. this.str = str;
  42. fromCurrentPosition = true;
  43. prepareData();
  44. }
  45. /**
  46. * @param str the character string
  47. * @param x the x coordinate
  48. * @param y the y coordinate
  49. */
  50. public GraphicsString(String str, int x, int y) {
  51. this.str = str;
  52. this.x = x;
  53. this.y = y;
  54. prepareData();
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. protected void prepareData() {
  60. int maxStrLen = MAX_STR_LEN - (fromCurrentPosition ? 0 : 4);
  61. if (str.length() > maxStrLen) {
  62. str = str.substring(0, maxStrLen);
  63. log.warn("truncated character string, longer than " + maxStrLen + " chars");
  64. }
  65. byte[] strData = null;
  66. try {
  67. strData = str.getBytes(AFPConstants.EBCIDIC_ENCODING);
  68. } catch (UnsupportedEncodingException ex) {
  69. GraphicsObject.log.error("unsupported encoding: " + ex.getMessage());
  70. }
  71. int len = strData.length;
  72. if (fromCurrentPosition) {
  73. data = new byte[len + 2];
  74. data[0] = (byte)0x83;
  75. data[1] = (byte)len;
  76. System.arraycopy(strData, 0, data, 2, strData.length);
  77. } else {
  78. len += 4; // x/y coordinates
  79. byte[] osx = BinaryUtils.convert(x, 2);
  80. byte[] osy = BinaryUtils.convert(y, 2);
  81. data = new byte[len + 2];
  82. data[0] = (byte)0xC3;
  83. data[1] = (byte)len;
  84. data[2] = osx[0];
  85. data[3] = osx[1];
  86. data[4] = osy[0];
  87. data[5] = osy[1];
  88. System.arraycopy(strData, 0, data, 6, strData.length);
  89. }
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public String toString() {
  95. String string = "GraphicsString(str='" + str + "'";
  96. if (!fromCurrentPosition) {
  97. string += ",x=" + x + ",y=" + y;
  98. }
  99. string += ")";
  100. return string;
  101. }
  102. }