zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
 
/* ====================================================================
   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 HH.WMS.Utils.NPOI.HSSF.Record.CF
{
    using System;
    using System.Text;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.Util;
    using HH.WMS.Utils.NPOI.SS.UserModel;
 
    /**
     * Font Formatting Block of the Conditional Formatting Rule Record.
     * 
     * @author Dmitriy Kumshayev
     */
    public class FontFormatting
    {
        private byte[] _rawData;
 
        private static int OFFSET_FONT_NAME = 0;
        private static int OFFSET_FONT_HEIGHT = 64;
        private static int OFFSET_FONT_OPTIONS = 68;
        private static int OFFSET_FONT_WEIGHT = 72;
        private static int OFFSET_ESCAPEMENT_TYPE = 74;
        private static int OFFSET_UNDERLINE_TYPE = 76;
        private static int OFFSET_FONT_COLOR_INDEX = 80;
        private static int OFFSET_OPTION_FLAGS = 88;
        private static int OFFSET_ESCAPEMENT_TYPE_MODIFIED = 92;
        private static int OFFSET_UNDERLINE_TYPE_MODIFIED = 96;
        private static int OFFSET_FONT_WEIGHT_MODIFIED = 100;
        private static int OFFSET_NOT_USED1 = 104;
        private static int OFFSET_NOT_USED2 = 108;
        private static int OFFSET_NOT_USED3 = 112; // for some reason Excel always Writes  0x7FFFFFFF at this offset   
        private static int OFFSET_FONT_FORMATING_END = 116;
        private static int RAW_DATA_SIZE = 118;
 
 
        public static int FONT_CELL_HEIGHT_PRESERVED = unchecked((int)0xFFFFFFFF);
 
        // FONT OPTIONS MASKS
        private static BitField posture = BitFieldFactory.GetInstance(0x00000002);
        private static BitField outline = BitFieldFactory.GetInstance(0x00000008);
        private static BitField shadow = BitFieldFactory.GetInstance(0x00000010);
        private static BitField cancellation = BitFieldFactory.GetInstance(0x00000080);
 
        // OPTION FLAGS MASKS
 
        private static BitField styleModified = BitFieldFactory.GetInstance(0x00000002);
        private static BitField outlineModified = BitFieldFactory.GetInstance(0x00000008);
        private static BitField shadowModified = BitFieldFactory.GetInstance(0x00000010);
        private static BitField cancellationModified = BitFieldFactory.GetInstance(0x00000080);
 
        /** Escapement type - None */
        public const short SS_NONE = 0;
        /** Escapement type - Superscript */
        public const short SS_SUPER = 1;
        /** Escapement type - Subscript */
        public const short SS_SUB = 2;
        /** Underline type - None */
        public const byte U_NONE = 0;
        /** Underline type - Single */
        public const byte U_SINGLE = 1;
        /** Underline type - double */
        public const byte U_DOUBLE = 2;
        /** Underline type - Single Accounting */
        public const byte U_SINGLE_ACCOUNTING = 0x21;
        /** Underline type - double Accounting */
        public const byte U_DOUBLE_ACCOUNTING = 0x22;
        /** Normal boldness (not bold) */
        private const short FONT_WEIGHT_NORMAL = 0x190;
 
        /**
         * Bold boldness (bold)
         */
        private const short FONT_WEIGHT_BOLD = 0x2bc;
 
        private FontFormatting(byte[] rawData)
        {
            _rawData = rawData;
        }
 
        public FontFormatting():this(new byte[RAW_DATA_SIZE])
        {
            
 
            FontHeight=-1;
            IsItalic=false;
            IsFontWeightModified=false;
            IsOutlineOn=false;
            IsShadowOn=false;
            IsStruckout=false;
            EscapementType=(FontSuperScript)0;
            UnderlineType=(FontUnderlineType)0;
            FontColorIndex=(short)-1;
 
            IsFontStyleModified=false;
            IsFontOutlineModified=false;
            IsFontShadowModified=false;
            IsFontCancellationModified=false;
 
            IsEscapementTypeModified=false;
            IsUnderlineTypeModified=false;
 
            SetShort(OFFSET_FONT_NAME, 0);
            SetInt(OFFSET_NOT_USED1, 0x00000001);
            SetInt(OFFSET_NOT_USED2, 0x00000000);
            SetInt(OFFSET_NOT_USED3, 0x7FFFFFFF);// for some reason Excel always Writes  0x7FFFFFFF at this offset
            SetShort(OFFSET_FONT_FORMATING_END, 0x0001);
        }
 
        /** Creates new FontFormatting */
        public FontFormatting(RecordInputStream in1):this(new byte[RAW_DATA_SIZE])
        {
            
            for (int i = 0; i < _rawData.Length; i++)
            {
                _rawData[i] =(byte) in1.ReadByte();
            }
        }
 
        private short GetShort(int offset)
        {
            return LittleEndian.GetShort(_rawData, offset);
        }
        private void SetShort(int offset, int value)
        {
            LittleEndian.PutShort(_rawData, offset, (short)value);
        }
        private int GetInt(int offset)
        {
            return LittleEndian.GetInt(_rawData, offset);
        }
        private void SetInt(int offset, int value)
        {
            LittleEndian.PutInt(_rawData, offset, value);
        }
 
        public byte[] GetRawRecord()
        {
            return _rawData;
        }
 
        /**
         * Gets the height of the font in 1/20th point Units
         *
         * @return fontheight (in points/20); or -1 if not modified
         */
        public int FontHeight
        {
            get{return GetInt(OFFSET_FONT_HEIGHT);}
            set { SetInt(OFFSET_FONT_HEIGHT, value); }
        }
 
        private void SetFontOption(bool option, BitField field)
        {
            int options = GetInt(OFFSET_FONT_OPTIONS);
            options = field.SetBoolean(options, option);
            SetInt(OFFSET_FONT_OPTIONS, options);
        }
 
        private bool GetFontOption(BitField field)
        {
            int options = GetInt(OFFSET_FONT_OPTIONS);
            return field.IsSet(options);
        }
 
 
        /**
         * Get whether the font Is to be italics or not
         *
         * @return italics - whether the font Is italics or not
         * @see #GetAttributes()
         */
 
        public bool IsItalic
        {
            get
            {
                return GetFontOption(posture);
            }
            set
            {
                SetFontOption(value, posture);
            }
        }
 
        public bool IsOutlineOn
        {
            get
            {
                return GetFontOption(outline);
            }
            set { SetFontOption(value, outline); }
        }
 
        public bool IsShadowOn
        {
            get
            {
                return GetFontOption(shadow);
            }
            set { SetFontOption(value, shadow); }
        }
 
 
        /**
         * Get whether the font Is to be stricken out or not
         *
         * @return strike - whether the font Is stricken out or not
         * @see #GetAttributes()
         */
 
        public bool IsStruckout
        {
            get
            {
                return GetFontOption(cancellation);
            }
            set { SetFontOption(value, cancellation); }
        }
 
 
        /// <summary>
        /// Get or set the font weight for this font (100-1000dec or 0x64-0x3e8).  
        /// Default Is 0x190 for normal and 0x2bc for bold
        /// </summary>
        public short FontWeight
        {
            get { return GetShort(OFFSET_FONT_WEIGHT); }
            set
            {
                short bw = value;
                if (bw < 100) { bw = 100; }
                if (bw > 1000) { bw = 1000; }
                SetShort(OFFSET_FONT_WEIGHT, bw);
            }
        }
 
 
        /// <summary>
        ///Get or set whether the font weight is set to bold or not 
        /// </summary>
        public bool IsBold
        {
            get
            {
                return FontWeight == FONT_WEIGHT_BOLD;
            }
            set { this.FontWeight = (value ? FONT_WEIGHT_BOLD : FONT_WEIGHT_NORMAL); }
        }
 
        /**
         * Get the type of base or subscript for the font
         *
         * @return base or subscript option
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_NONE
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_SUPER
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_SUB
         */
        public FontSuperScript EscapementType
        {
            get
            {
                return (FontSuperScript)GetShort(OFFSET_ESCAPEMENT_TYPE);
            }
            set { SetShort(OFFSET_ESCAPEMENT_TYPE, (short)value); }
        }
 
        /**
         * Get the type of Underlining for the font
         *
         * @return font Underlining type
         *
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_NONE
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_SINGLE
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_DOUBLE
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_SINGLE_ACCOUNTING
         * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_DOUBLE_ACCOUNTING
         */
 
        public FontUnderlineType UnderlineType
        {
            get
            {
                return (FontUnderlineType)GetShort(OFFSET_UNDERLINE_TYPE);
            }
            set { SetShort(OFFSET_UNDERLINE_TYPE, (short)value); }
        }
 
 
 
        public short FontColorIndex
        {
            get
            {
                return (short)GetInt(OFFSET_FONT_COLOR_INDEX);
            }
            set { SetInt(OFFSET_FONT_COLOR_INDEX, value); }
        }
 
 
        private bool GetOptionFlag(BitField field)
        {
            int optionFlags = GetInt(OFFSET_OPTION_FLAGS);
            int value = field.GetValue(optionFlags);
            return value == 0 ? true : false;
        }
 
        private void SetOptionFlag(bool modified, BitField field)
        {
            int value = modified ? 0 : 1;
            int optionFlags = GetInt(OFFSET_OPTION_FLAGS);
            optionFlags = field.SetValue(optionFlags, value);
            SetInt(OFFSET_OPTION_FLAGS, optionFlags);
        }
 
 
        public bool IsFontStyleModified
        {
            get { return GetOptionFlag(styleModified); }
            set { SetOptionFlag(value, styleModified); }
        }
 
        public bool IsFontOutlineModified
        {
            get { return GetOptionFlag(outlineModified); }
            set { SetOptionFlag(value, outlineModified); }
        }
 
        public bool IsFontShadowModified
        {
            get { return GetOptionFlag(shadowModified); }
            set { SetOptionFlag(value, shadowModified); }
        }
 
        public bool IsFontCancellationModified
        {
            get { return GetOptionFlag(cancellationModified); }
            set { SetOptionFlag(value, cancellationModified); }
        }
 
        public bool IsEscapementTypeModified
        {
            get
            {
                int escapementModified = GetInt(OFFSET_ESCAPEMENT_TYPE_MODIFIED);
                return escapementModified == 0;
            }
            set
            {
                int value1 = value ? 0 : 1;
                SetInt(OFFSET_ESCAPEMENT_TYPE_MODIFIED, value1);
            }
        }
 
        public bool IsUnderlineTypeModified
        {
            get
            {
                int underlineModified = GetInt(OFFSET_UNDERLINE_TYPE_MODIFIED);
                return underlineModified == 0;
            }
            set {
 
                int value1 = value ? 0 : 1;
                SetInt(OFFSET_UNDERLINE_TYPE_MODIFIED, value1);
            }
        }
 
        public bool IsFontWeightModified
        {
            get
            {
                int fontStyleModified = GetInt(OFFSET_FONT_WEIGHT_MODIFIED);
                return fontStyleModified == 0;
            }
            set
            {
                int value1 = value ? 0 : 1;
                SetInt(OFFSET_FONT_WEIGHT_MODIFIED, value1);
            }
        }
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
            buffer.Append("    [Font Formatting]\n");
 
            buffer.Append("    .font height = ").Append(FontHeight).Append(" twips\n");
 
            if (IsFontStyleModified)
            {
                buffer.Append("    .font posture = ").Append(IsItalic ? "Italic" : "Normal").Append("\n");
            }
            else
            {
                buffer.Append("    .font posture = ]not modified]").Append("\n");
            }
 
            if (IsFontOutlineModified)
            {
                buffer.Append("    .font outline = ").Append(IsOutlineOn).Append("\n");
            }
            else
            {
                buffer.Append("    .font outline Is not modified\n");
            }
 
            if (IsFontShadowModified)
            {
                buffer.Append("    .font shadow = ").Append(IsShadowOn).Append("\n");
            }
            else
            {
                buffer.Append("    .font shadow Is not modified\n");
            }
 
            if (IsFontCancellationModified)
            {
                buffer.Append("    .font strikeout = ").Append(IsStruckout).Append("\n");
            }
            else
            {
                buffer.Append("    .font strikeout Is not modified\n");
            }
 
            if (IsFontStyleModified)
            {
                buffer.Append("    .font weight = ").
                    Append(FontWeight).
                    Append(
                        FontWeight == FONT_WEIGHT_NORMAL ? "(Normal)"
                                : FontWeight == FONT_WEIGHT_BOLD ? "(Bold)" : "0x" + StringUtil.ToHexString(FontWeight)).
                    Append("\n");
            }
            else
            {
                buffer.Append("    .font weight = ]not modified]").Append("\n");
            }
 
            if (IsEscapementTypeModified)
            {
                buffer.Append("    .escapement type = ").Append(EscapementType).Append("\n");
            }
            else
            {
                buffer.Append("    .escapement type Is not modified\n");
            }
 
            if (IsUnderlineTypeModified)
            {
                buffer.Append("    .underline type = ").Append(UnderlineType).Append("\n");
            }
            else
            {
                buffer.Append("    .underline type Is not modified\n");
            }
            buffer.Append("    .color index = ").Append("0x" + StringUtil.ToHexString(FontColorIndex).ToUpper()).Append("\n");
 
            buffer.Append("    [/Font Formatting]\n");
            return buffer.ToString();
        }
 
        public Object Clone()
        {
            byte[] rawData = (byte[])_rawData.Clone();
            return new FontFormatting(rawData);
        }
    }
}