Patch from bug #44937 from Squeeself- Partial support for extracting Escher images...
[poi.git] / src / java / org / apache / poi / ddf / EscherClientAnchorRecord.java
blobec3354c59952bac998f340f1d9fdaddd1afad284
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
19 package org.apache.poi.ddf;
21 import org.apache.poi.util.HexDump;
22 import org.apache.poi.util.LittleEndian;
24 import java.io.ByteArrayOutputStream;
26 /**
27 * The escher client anchor specifies which rows and cells the shape is bound to as well as
28 * the offsets within those cells. Each cell is 1024 units wide by 256 units long regardless
29 * of the actual size of the cell. The EscherClientAnchorRecord only applies to the top-most
30 * shapes. Shapes contained in groups are bound using the EscherChildAnchorRecords.
32 * @author Glen Stampoultzis
33 * @see EscherChildAnchorRecord
35 public class EscherClientAnchorRecord
36 extends EscherRecord
38 public static final short RECORD_ID = (short) 0xF010;
39 public static final String RECORD_DESCRIPTION = "MsofbtClientAnchor";
41 private short field_1_flag;
42 private short field_2_col1;
43 private short field_3_dx1;
44 private short field_4_row1;
45 private short field_5_dy1;
46 private short field_6_col2;
47 private short field_7_dx2;
48 private short field_8_row2;
49 private short field_9_dy2;
50 private byte[] remainingData;
51 private boolean shortRecord = false;
53 /**
54 * This method deserializes the record from a byte array.
56 * @param data The byte array containing the escher record information
57 * @param offset The starting offset into <code>data</code>.
58 * @param recordFactory May be null since this is not a container record.
59 * @return The number of bytes read from the byte array.
61 public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory )
63 int bytesRemaining = readHeader( data, offset );
64 int pos = offset + 8;
65 int size = 0;
67 // Always find 4 two byte entries. Sometimes find 9
68 if (bytesRemaining == 4) // Word format only 4 bytes
70 // Not sure exactly what the format is quite yet, likely a reference to a PLC
72 else
74 field_1_flag = LittleEndian.getShort( data, pos + size ); size += 2;
75 field_2_col1 = LittleEndian.getShort( data, pos + size ); size += 2;
76 field_3_dx1 = LittleEndian.getShort( data, pos + size ); size += 2;
77 field_4_row1 = LittleEndian.getShort( data, pos + size ); size += 2;
78 if(bytesRemaining >= 18) {
79 field_5_dy1 = LittleEndian.getShort( data, pos + size ); size += 2;
80 field_6_col2 = LittleEndian.getShort( data, pos + size ); size += 2;
81 field_7_dx2 = LittleEndian.getShort( data, pos + size ); size += 2;
82 field_8_row2 = LittleEndian.getShort( data, pos + size ); size += 2;
83 field_9_dy2 = LittleEndian.getShort( data, pos + size ); size += 2;
84 shortRecord = false;
85 } else {
86 shortRecord = true;
89 bytesRemaining -= size;
90 remainingData = new byte[bytesRemaining];
91 System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining );
92 return 8 + size + bytesRemaining;
95 /**
96 * This method serializes this escher record into a byte array.
98 * @param offset The offset into <code>data</code> to start writing the record data to.
99 * @param data The byte array to serialize to.
100 * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
101 * @return The number of bytes written.
102 * @see NullEscherSerializationListener
104 public int serialize( int offset, byte[] data, EscherSerializationListener listener )
106 listener.beforeRecordSerialize( offset, getRecordId(), this );
108 if (remainingData == null) remainingData = new byte[0];
109 LittleEndian.putShort( data, offset, getOptions() );
110 LittleEndian.putShort( data, offset + 2, getRecordId() );
111 int remainingBytes = remainingData.length + (shortRecord ? 8 : 18);
112 LittleEndian.putInt( data, offset + 4, remainingBytes );
113 LittleEndian.putShort( data, offset + 8, field_1_flag );
114 LittleEndian.putShort( data, offset + 10, field_2_col1 );
115 LittleEndian.putShort( data, offset + 12, field_3_dx1 );
116 LittleEndian.putShort( data, offset + 14, field_4_row1 );
117 if(!shortRecord) {
118 LittleEndian.putShort( data, offset + 16, field_5_dy1 );
119 LittleEndian.putShort( data, offset + 18, field_6_col2 );
120 LittleEndian.putShort( data, offset + 20, field_7_dx2 );
121 LittleEndian.putShort( data, offset + 22, field_8_row2 );
122 LittleEndian.putShort( data, offset + 24, field_9_dy2 );
124 System.arraycopy( remainingData, 0, data, offset + (shortRecord ? 16 : 26), remainingData.length );
125 int pos = offset + 8 + (shortRecord ? 8 : 18) + remainingData.length;
127 listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
128 return pos - offset;
132 * Returns the number of bytes that are required to serialize this record.
134 * @return Number of bytes
136 public int getRecordSize()
138 return 8 + (shortRecord ? 8 : 18) + (remainingData == null ? 0 : remainingData.length);
142 * The record id for this record.
144 public short getRecordId()
146 return RECORD_ID;
150 * The short name for this record
152 public String getRecordName()
154 return "ClientAnchor";
158 * Returns the string representation for this record.
160 * @return A string
162 public String toString()
164 String nl = System.getProperty("line.separator");
166 String extraData;
167 ByteArrayOutputStream b = new ByteArrayOutputStream();
170 HexDump.dump(this.remainingData, 0, b, 0);
171 extraData = b.toString();
173 catch ( Exception e )
175 extraData = "error\n";
177 return getClass().getName() + ":" + nl +
178 " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl +
179 " Options: 0x" + HexDump.toHex(getOptions()) + nl +
180 " Flag: " + field_1_flag + nl +
181 " Col1: " + field_2_col1 + nl +
182 " DX1: " + field_3_dx1 + nl +
183 " Row1: " + field_4_row1 + nl +
184 " DY1: " + field_5_dy1 + nl +
185 " Col2: " + field_6_col2 + nl +
186 " DX2: " + field_7_dx2 + nl +
187 " Row2: " + field_8_row2 + nl +
188 " DY2: " + field_9_dy2 + nl +
189 " Extra Data:" + nl + extraData;
194 * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
196 public short getFlag()
198 return field_1_flag;
202 * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
204 public void setFlag( short field_1_flag )
206 this.field_1_flag = field_1_flag;
210 * The column number for the top-left position. 0 based.
212 public short getCol1()
214 return field_2_col1;
218 * The column number for the top-left position. 0 based.
220 public void setCol1( short field_2_col1 )
222 this.field_2_col1 = field_2_col1;
226 * The x offset within the top-left cell. Range is from 0 to 1023.
228 public short getDx1()
230 return field_3_dx1;
234 * The x offset within the top-left cell. Range is from 0 to 1023.
236 public void setDx1( short field_3_dx1 )
238 this.field_3_dx1 = field_3_dx1;
242 * The row number for the top-left corner of the shape.
244 public short getRow1()
246 return field_4_row1;
250 * The row number for the top-left corner of the shape.
252 public void setRow1( short field_4_row1 )
254 this.field_4_row1 = field_4_row1;
258 * The y offset within the top-left corner of the current shape.
260 public short getDy1()
262 return field_5_dy1;
266 * The y offset within the top-left corner of the current shape.
268 public void setDy1( short field_5_dy1 )
270 shortRecord = false;
271 this.field_5_dy1 = field_5_dy1;
275 * The column of the bottom right corner of this shape.
277 public short getCol2()
279 return field_6_col2;
283 * The column of the bottom right corner of this shape.
285 public void setCol2( short field_6_col2 )
287 shortRecord = false;
288 this.field_6_col2 = field_6_col2;
292 * The x offset withing the cell for the bottom-right corner of this shape.
294 public short getDx2()
296 return field_7_dx2;
300 * The x offset withing the cell for the bottom-right corner of this shape.
302 public void setDx2( short field_7_dx2 )
304 shortRecord = false;
305 this.field_7_dx2 = field_7_dx2;
309 * The row number for the bottom-right corner of the current shape.
311 public short getRow2()
313 return field_8_row2;
317 * The row number for the bottom-right corner of the current shape.
319 public void setRow2( short field_8_row2 )
321 shortRecord = false;
322 this.field_8_row2 = field_8_row2;
326 * The y offset withing the cell for the bottom-right corner of this shape.
328 public short getDy2()
330 return field_9_dy2;
334 * The y offset withing the cell for the bottom-right corner of this shape.
336 public void setDy2( short field_9_dy2 )
338 shortRecord = false;
339 this.field_9_dy2 = field_9_dy2;
343 * Any remaining data in the record
345 public byte[] getRemainingData()
347 return remainingData;
351 * Any remaining data in the record
353 public void setRemainingData( byte[] remainingData )
355 this.remainingData = remainingData;