|
1 | | -using System.IO; |
| 1 | +using Oxage.Wmf.Extensions; |
| 2 | + |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
2 | 5 | using System.Text; |
3 | 6 |
|
4 | 7 | namespace Oxage.Wmf.Records |
5 | 8 | { |
6 | | - [WmfRecord(Type = RecordType.META_TEXTOUT, SizeIsVariable = true)] |
7 | | - public class WmfTextoutRecord : WmfBinaryRecord |
8 | | - { |
9 | | - public WmfTextoutRecord() : base() |
10 | | - { |
11 | | - } |
12 | | - |
13 | | - public short StringLength |
14 | | - { |
15 | | - get; |
16 | | - set; |
17 | | - } |
18 | | - |
19 | | - public string StringValue |
20 | | - { |
21 | | - get; |
22 | | - set; |
23 | | - } |
24 | | - |
25 | | - public short YStart |
26 | | - { |
27 | | - get; |
28 | | - set; |
29 | | - } |
30 | | - |
31 | | - public short XStart |
32 | | - { |
33 | | - get; |
34 | | - set; |
35 | | - } |
36 | | - |
37 | | - protected Encoding StringEncoding |
38 | | - { |
39 | | - get |
40 | | - { |
41 | | - return WmfHelper.GetAnsiEncoding(); |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - public override void Read(BinaryReader reader) |
46 | | - { |
47 | | - this.StringLength = reader.ReadInt16(); |
48 | | - |
49 | | - if (this.StringLength > 0) |
50 | | - { |
51 | | - byte[] ansi = reader.ReadBytes(this.StringLength); |
52 | | - this.StringValue = StringEncoding.GetString(ansi); |
53 | | - } |
54 | | - |
55 | | - this.YStart = reader.ReadInt16(); |
56 | | - this.XStart = reader.ReadInt16(); |
57 | | - } |
58 | | - |
59 | | - public override void Write(BinaryWriter writer) |
60 | | - { |
61 | | - byte[] ansi = StringEncoding.GetBytes(this.StringValue ?? ""); |
62 | | - int offset = (ansi.Length % 2 == 1 ? +1 : +0); //1 extra byte for odd-length string |
63 | | - |
64 | | - base.RecordSizeBytes = (uint)(6 /* RecordSize and RecordFunction */ + 2 + (ansi.Length + offset) + 2 + 2 /* Parameters */); |
65 | | - base.Write(writer); |
66 | | - |
67 | | - writer.Write(this.StringLength > 0 ? this.StringLength : (short)(ansi.Length + offset)); |
68 | | - writer.Write(ansi); |
69 | | - |
70 | | - if (ansi.Length % 2 == 1) |
71 | | - { |
72 | | - //Write a dummy byte after odd-length string so the record aligns to 16-bit boundary |
73 | | - writer.Write((byte)0x00); |
74 | | - } |
75 | | - |
76 | | - writer.Write(this.YStart); |
77 | | - writer.Write(this.XStart); |
78 | | - } |
79 | | - |
80 | | - protected override void Dump(StringBuilder builder) |
81 | | - { |
82 | | - base.Dump(builder); |
83 | | - builder.AppendLine("StringLength: " + this.StringLength); |
84 | | - builder.AppendLine("String: " + this.StringValue); |
85 | | - builder.AppendLine("YStart: " + this.YStart); |
86 | | - builder.AppendLine("XStart: " + this.XStart); |
87 | | - } |
88 | | - } |
| 9 | + [WmfRecord(Type = RecordType.META_TEXTOUT, SizeIsVariable = true)] |
| 10 | + public class WmfTextoutRecord : WmfBinaryRecord |
| 11 | + { |
| 12 | + public WmfTextoutRecord() : base() |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + public short StringLength |
| 17 | + { |
| 18 | + get; |
| 19 | + set; |
| 20 | + } |
| 21 | + |
| 22 | + public string GetText(Encoding encoding) |
| 23 | + { |
| 24 | + var text = encoding.GetString(TextByteArray); |
| 25 | + return text; |
| 26 | + } |
| 27 | + |
| 28 | + public string GetText(CharacterSet characterSet) => GetText(characterSet.ToEncoding()); |
| 29 | + |
| 30 | + public byte[] TextByteArray |
| 31 | + { |
| 32 | + get => _textByteArray; |
| 33 | + set |
| 34 | + { |
| 35 | + _textByteArray = value; |
| 36 | + StringLength = (short) value.Length; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private byte[] _textByteArray; |
| 41 | + |
| 42 | + |
| 43 | + public short YStart |
| 44 | + { |
| 45 | + get; |
| 46 | + set; |
| 47 | + } |
| 48 | + |
| 49 | + public short XStart |
| 50 | + { |
| 51 | + get; |
| 52 | + set; |
| 53 | + } |
| 54 | + |
| 55 | + protected Encoding StringEncoding |
| 56 | + { |
| 57 | + get |
| 58 | + { |
| 59 | + return WmfHelper.GetAnsiEncoding(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public override void Read(BinaryReader reader) |
| 64 | + { |
| 65 | + this.StringLength = reader.ReadInt16(); |
| 66 | + |
| 67 | + if (this.StringLength > 0) |
| 68 | + { |
| 69 | + byte[] textByteArray = reader.ReadBytes(this.StringLength); |
| 70 | + TextByteArray = textByteArray; |
| 71 | + } |
| 72 | + |
| 73 | + // String (variable): The size of this field MUST be a multiple of two. If StringLength is an odd number, then this field MUST be of a size greater than or equal to StringLength + 1. A variable-length string that specifies the text to be drawn. The string does not need to be null-terminated, because StringLength specifies the length of the string. The string is written at the location specified by the XStart and YStart fields. See section 2.3.3.5 for information about the encoding of the field. |
| 74 | + var isOdd = StringLength % 2 == 1; |
| 75 | + if (isOdd) |
| 76 | + { |
| 77 | + reader.ReadByte(); |
| 78 | + } |
| 79 | + |
| 80 | + this.YStart = reader.ReadInt16(); |
| 81 | + this.XStart = reader.ReadInt16(); |
| 82 | + } |
| 83 | + |
| 84 | + public override void Write(BinaryWriter writer) |
| 85 | + { |
| 86 | + byte[] textByteArray = TextByteArray; |
| 87 | + int offset = (textByteArray.Length % 2 == 1 ? +1 : +0); //1 extra byte for odd-length string |
| 88 | + |
| 89 | + base.RecordSizeBytes = (uint) (6 /* RecordSize and RecordFunction */ + 2 + (textByteArray.Length + offset) + 2 + 2 /* Parameters */); |
| 90 | + base.Write(writer); |
| 91 | + |
| 92 | + writer.Write(this.StringLength > 0 ? this.StringLength : (short) (textByteArray.Length + offset)); |
| 93 | + writer.Write(textByteArray); |
| 94 | + |
| 95 | + if (textByteArray.Length % 2 == 1) |
| 96 | + { |
| 97 | + //Write a dummy byte after odd-length string so the record aligns to 16-bit boundary |
| 98 | + writer.Write((byte) 0x00); |
| 99 | + } |
| 100 | + |
| 101 | + writer.Write(this.YStart); |
| 102 | + writer.Write(this.XStart); |
| 103 | + } |
| 104 | + |
| 105 | + protected override void Dump(StringBuilder builder) |
| 106 | + { |
| 107 | + base.Dump(builder); |
| 108 | + builder.AppendLine("StringLength: " + this.StringLength); |
| 109 | + builder.AppendLine("TextByteArray: " + string.Join(',', TextByteArray.Select(t => t.ToString("X2")))); |
| 110 | + builder.AppendLine("GuessText:" + WmfHelper.GetAnsiEncoding().GetString(TextByteArray)); |
| 111 | + builder.AppendLine("YStart: " + this.YStart); |
| 112 | + builder.AppendLine("XStart: " + this.XStart); |
| 113 | + } |
| 114 | + } |
89 | 115 | } |
0 commit comments