用于生成 Office 文件的 .NET 组件。
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.

388 lines
16 KiB

8 years ago
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for Additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
namespace Npoi.Core.DDF
8 years ago
{
8 years ago
using Npoi.Core.Util;
8 years ago
using System;
using System.IO;
using System.Text;
/// <summary>
/// The BSE record is related closely to the EscherBlipRecord and stores
/// extra information about the blip. A blip record is actually stored inside
/// the BSE record even though the BSE record isn't actually a container record.
/// @author Glen Stampoultzis
/// @see EscherBlipRecord
/// </summary>
public class EscherBSERecord : EscherRecord
{
public const short RECORD_ID = unchecked((short)0xF007);
public const String RECORD_DESCRIPTION = "MsofbtBSE";
public const byte BT_ERROR = 0;
public const byte BT_UNKNOWN = 1;
public const byte BT_EMF = 2;
public const byte BT_WMF = 3;
public const byte BT_PICT = 4;
public const byte BT_JPEG = 5;
public const byte BT_PNG = 6;
public const byte BT_DIB = 7;
private byte field_1_blipTypeWin32;
private byte field_2_blipTypeMacOS;
private byte[] field_3_uid; // 16 bytes
private short field_4_tag;
private int field_5_size;
private int field_6_ref;
private int field_7_offset;
private byte field_8_usage;
private byte field_9_name;
private byte field_10_unused2;
private byte field_11_unused3;
private EscherBlipRecord field_12_blipRecord;
private byte[] _remainingData;
/// <summary>
/// This method deSerializes the record from a byte array.
/// </summary>
/// <param name="data">The byte array containing the escher record information</param>
/// <param name="offset">The starting offset into data</param>
/// <param name="recordFactory">May be null since this is not a container record.</param>
/// <returns>The number of bytes Read from the byte array.</returns>
public override int FillFields(byte[] data, int offset,
IEscherRecordFactory recordFactory
8 years ago
) {
8 years ago
int bytesRemaining = ReadHeader(data, offset);
int pos = offset + 8;
field_1_blipTypeWin32 = data[pos];
field_2_blipTypeMacOS = data[pos + 1];
field_3_uid = new byte[16];
Array.Copy(data, pos + 2, field_3_uid, 0, 16);
field_4_tag = LittleEndian.GetShort(data, pos + 18);
field_5_size = LittleEndian.GetInt(data, pos + 20);
field_6_ref = LittleEndian.GetInt(data, pos + 24);
field_7_offset = LittleEndian.GetInt(data, pos + 28);
field_8_usage = data[pos + 32];
field_9_name = data[pos + 33];
field_10_unused2 = data[pos + 34];
field_11_unused3 = data[pos + 35];
bytesRemaining -= 36;
int bytesRead = 0;
8 years ago
if (bytesRemaining > 0) {
8 years ago
field_12_blipRecord = (EscherBlipRecord)recordFactory.CreateRecord(data, pos + 36);
bytesRead = field_12_blipRecord.FillFields(data, pos + 36, recordFactory);
}
pos += 36 + bytesRead;
bytesRemaining -= bytesRead;
_remainingData = new byte[bytesRemaining];
Array.Copy(data, pos, _remainingData, 0, bytesRemaining);
return bytesRemaining + 8 + 36 + (field_12_blipRecord == null ? 0 : field_12_blipRecord.RecordSize);
}
/// <summary>
8 years ago
/// This method Serializes this escher record into a byte array.
8 years ago
/// </summary>
8 years ago
/// <param name="offset">The offset into
8 years ago
/// data to start writing the record data to</param>
/// <param name="data">The byte array to Serialize to.</param>
/// <param name="listener">a listener for begin and end serialization events.</param>
/// <returns>The number of bytes written.</returns>
8 years ago
public override int Serialize(int offset, byte[] data, EscherSerializationListener listener) {
8 years ago
listener.BeforeRecordSerialize(offset, RecordId, this);
if (_remainingData == null)
_remainingData = new byte[0];
LittleEndian.PutShort(data, offset, Options);
LittleEndian.PutShort(data, offset + 2, RecordId);
if (_remainingData == null) _remainingData = new byte[0];
int blipSize = field_12_blipRecord == null ? 0 : field_12_blipRecord.RecordSize;
int remainingBytes = _remainingData.Length + 36 + blipSize;
LittleEndian.PutInt(data, offset + 4, remainingBytes);
data[offset + 8] = field_1_blipTypeWin32;
data[offset + 9] = field_2_blipTypeMacOS;
for (int i = 0; i < 16; i++)
data[offset + 10 + i] = field_3_uid[i];
LittleEndian.PutShort(data, offset + 26, field_4_tag);
LittleEndian.PutInt(data, offset + 28, field_5_size);
LittleEndian.PutInt(data, offset + 32, field_6_ref);
LittleEndian.PutInt(data, offset + 36, field_7_offset);
data[offset + 40] = field_8_usage;
data[offset + 41] = field_9_name;
data[offset + 42] = field_10_unused2;
data[offset + 43] = field_11_unused3;
int bytesWritten = 0;
8 years ago
if (field_12_blipRecord != null) {
8 years ago
bytesWritten = field_12_blipRecord.Serialize(offset + 44, data);
}
if (_remainingData == null)
_remainingData = new byte[0];
Array.Copy(_remainingData, 0, data, offset + 44 + bytesWritten, _remainingData.Length);
int pos = offset + 8 + 36 + _remainingData.Length + bytesWritten;
listener.AfterRecordSerialize(pos, RecordId, pos - offset, this);
return pos - offset;
}
/// <summary>
/// Returns the number of bytes that are required to Serialize this record.
/// </summary>
/// <value>Number of bytes</value>
8 years ago
public override int RecordSize {
get
8 years ago
{
int field_12_size = 0;
8 years ago
if (field_12_blipRecord != null) {
8 years ago
field_12_size = field_12_blipRecord.RecordSize;
}
int remaining_size = 0;
8 years ago
if (_remainingData != null) {
8 years ago
remaining_size = _remainingData.Length;
}
return 8 + 1 + 1 + 16 + 2 + 4 + 4 + 4 + 1 + 1 +
8 years ago
1 + 1 + field_12_size + remaining_size;
8 years ago
}
}
/// <summary>
/// The short name for this record
/// </summary>
/// <value></value>
8 years ago
public override String RecordName {
8 years ago
get { return "BSE"; }
}
/// <summary>
/// Gets or sets the expected blip type under windows (failure to match this blip type will result in
/// Excel converting to this format).
/// </summary>
/// <value>The blip type win32.</value>
8 years ago
public byte BlipTypeWin32 {
8 years ago
get { return field_1_blipTypeWin32; }
set { field_1_blipTypeWin32 = value; }
}
/// <summary>
/// Gets or sets the expected blip type under MacOS (failure to match this blip type will result in
/// Excel converting to this format).
/// </summary>
/// <value>The blip type mac OS.</value>
8 years ago
public byte BlipTypeMacOS {
8 years ago
get { return field_2_blipTypeMacOS; }
set { field_2_blipTypeMacOS = value; }
}
/// <summary>
/// Gets or sets 16 byte MD4 checksum.
/// </summary>
/// <value>The UID.</value>
8 years ago
public byte[] UID {
8 years ago
get { return field_3_uid; }
set { field_3_uid = value; }
}
/// <summary>
/// Gets or sets the tag. (Unused)
/// </summary>
/// <value>The tag.</value>
8 years ago
public short Tag {
8 years ago
get { return field_4_tag; }
set { field_4_tag = value; }
}
/// <summary>
/// Gets or sets Blip size in stream..
/// </summary>
/// <value>The size.</value>
8 years ago
public int Size {
8 years ago
get { return field_5_size; }
set { field_5_size = value; }
}
/// <summary>
/// Gets or sets the reference count of this blip.
/// </summary>
/// <value>The ref.</value>
8 years ago
public int Ref {
8 years ago
get { return field_6_ref; }
set { field_6_ref = value; }
}
8 years ago
8 years ago
/// <summary>
/// Gets or sets the offset in the delay stream..
/// </summary>
/// <value>The offset.</value>
8 years ago
public int Offset {
8 years ago
get { return field_7_offset; }
set { field_7_offset = value; }
}
/// <summary>
/// Defines the way this blip is used.
/// </summary>
/// <value>The usage.</value>
8 years ago
public byte Usage {
8 years ago
get { return field_8_usage; }
set { field_8_usage = value; }
}
/// <summary>
/// Gets or sets the blip name.
/// </summary>
/// <value>The name.</value>
8 years ago
public byte Name {
8 years ago
get { return field_9_name; }
set { field_9_name = value; }
}
/// <summary>
/// Gets or sets the unused2.
/// </summary>
/// <value>The unused2.</value>
8 years ago
public byte Unused2 {
8 years ago
get { return field_10_unused2; }
set { field_10_unused2 = value; }
}
/// <summary>
/// Gets or sets the unused3.
/// </summary>
/// <value>The unused3.</value>
8 years ago
public byte Unused3 {
8 years ago
get { return field_11_unused3; }
set { field_11_unused3 = value; }
}
/// <summary>
/// Gets or sets the blip record.
/// </summary>
/// <value>The blip record.</value>
8 years ago
public EscherBlipRecord BlipRecord {
8 years ago
get { return field_12_blipRecord; }
set { field_12_blipRecord = value; }
}
/// <summary>
/// Gets or sets any remaining data in this record.
/// </summary>
/// <value>The remaining data.</value>
8 years ago
public byte[] RemainingData {
8 years ago
get { return _remainingData; }
set { _remainingData = value; }
}
8 years ago
8 years ago
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
8 years ago
public override String ToString() {
8 years ago
String nl = Environment.NewLine;
String extraData;
8 years ago
using (MemoryStream b = new MemoryStream()) {
try {
8 years ago
HexDump.Dump(this._remainingData, 0, b, 0);
//extraData = b.ToString();
extraData = Encoding.UTF8.GetString(b.ToArray());
}
8 years ago
catch (Exception e) {
8 years ago
extraData = e.ToString();
}
return GetType().Name + ":" + nl +
" RecordId: 0x" + HexDump.ToHex(RECORD_ID) + nl +
" Version: 0x" + HexDump.ToHex(Version) + '\n' +
" Instance: 0x" + HexDump.ToHex(Instance) + '\n' +
" BlipTypeWin32: " + field_1_blipTypeWin32 + nl +
" BlipTypeMacOS: " + field_2_blipTypeMacOS + nl +
" SUID: " + HexDump.ToHex(field_3_uid) + nl +
" Tag: " + field_4_tag + nl +
" Size: " + field_5_size + nl +
" Ref: " + field_6_ref + nl +
" Offset: " + field_7_offset + nl +
" Usage: " + field_8_usage + nl +
" Name: " + field_9_name + nl +
" Unused2: " + field_10_unused2 + nl +
" Unused3: " + field_11_unused3 + nl +
" blipRecord: " + (field_12_blipRecord == null ? "null" : field_12_blipRecord.ToString()) + nl +
" Extra Data:" + nl + extraData;
}
}
8 years ago
public override String ToXml(string tab) {
8 years ago
StringBuilder builder = new StringBuilder();
builder.Append(tab).Append(FormatXmlRecordHeader(GetType().Name, HexDump.ToHex(RecordId), HexDump.ToHex(Version), HexDump.ToHex(Instance)))
.Append(tab).Append("\t").Append("<BlipTypeWin32>").Append(field_1_blipTypeWin32).Append("</BlipTypeWin32>\n")
.Append(tab).Append("\t").Append("<BlipTypeMacOS>").Append(field_2_blipTypeMacOS).Append("</BlipTypeMacOS>\n")
.Append(tab).Append("\t").Append("<SUID>").Append(field_3_uid == null ? "" : HexDump.ToHex(field_3_uid)).Append("</SUID>\n")
.Append(tab).Append("\t").Append("<Tag>").Append(field_4_tag).Append("</Tag>\n")
.Append(tab).Append("\t").Append("<Size>").Append(field_5_size).Append("</Size>\n")
.Append(tab).Append("\t").Append("<Ref>").Append(field_6_ref).Append("</Ref>\n")
.Append(tab).Append("\t").Append("<Offset>").Append(field_7_offset).Append("</Offset>\n")
.Append(tab).Append("\t").Append("<Usage>").Append(field_8_usage).Append("</Usage>\n")
.Append(tab).Append("\t").Append("<Name>").Append(field_9_name).Append("</Name>\n")
.Append(tab).Append("\t").Append("<Unused2>").Append(field_10_unused2).Append("</Unused2>\n")
.Append(tab).Append("\t").Append("<Unused3>").Append(field_11_unused3).Append("</Unused3>\n");
builder.Append(tab).Append("</").Append(GetType().Name).Append(">\n");
return builder.ToString();
}
8 years ago
8 years ago
/// <summary>
/// Retrieve the string representation given a blip id.
/// </summary>
/// <param name="b">The b.</param>
/// <returns></returns>
8 years ago
public String GetBlipType(byte b) {
switch (b) {
8 years ago
case BT_ERROR:
return " ERROR";
8 years ago
8 years ago
case BT_UNKNOWN:
return " UNKNOWN";
8 years ago
8 years ago
case BT_EMF:
return " EMF";
8 years ago
8 years ago
case BT_WMF:
return " WMF";
8 years ago
8 years ago
case BT_PICT:
return " PICT";
8 years ago
8 years ago
case BT_JPEG:
return " JPEG";
8 years ago
8 years ago
case BT_PNG:
return " PNG";
8 years ago
8 years ago
case BT_DIB:
return " DIB";
8 years ago
8 years ago
default:
if (b < 32)
return " NotKnown";
else
return " Client";
}
}
}
}