zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
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
/* ====================================================================
   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.UserModel
{
    using System;
 
 
    /// <summary>
    /// A client anchor Is attached to an excel worksheet.  It anchors against a
    /// top-left and buttom-right cell.
    /// @author Glen Stampoultzis (glens at apache.org)
    /// </summary>
    public class HSSFClientAnchor : HSSFAnchor, HH.WMS.Utils.NPOI.SS.UserModel.IClientAnchor
    {
        int col1;
        int row1;
        int col2;
        int row2;
        int anchorType;
 
        /// <summary>
        /// Creates a new client anchor and defaults all the anchor positions to 0.
        /// </summary>
        public HSSFClientAnchor()
        {
        }
 
        /// <summary>
        /// Creates a new client anchor and Sets the top-left and bottom-right
        /// coordinates of the anchor.
        /// </summary>
        /// <param name="dx1">the x coordinate within the first cell.</param>
        /// <param name="dy1">the y coordinate within the first cell.</param>
        /// <param name="dx2">the x coordinate within the second cell.</param>
        /// <param name="dy2">the y coordinate within the second cell.</param>
        /// <param name="col1">the column (0 based) of the first cell.</param>
        /// <param name="row1">the row (0 based) of the first cell.</param>
        /// <param name="col2">the column (0 based) of the second cell.</param>
        /// <param name="row2">the row (0 based) of the second cell.</param>
        public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2)
            : base(dx1, dy1, dx2, dy2)
        {
 
 
            CheckRange(dx1, 0, 1023, "dx1");
            CheckRange(dx2, 0, 1023, "dx2");
            CheckRange(dy1, 0, 255, "dy1");
            CheckRange(dy2, 0, 255, "dy2");
            CheckRange(col1, 0, 255, "col1");
            CheckRange(col2, 0, 255, "col2");
            CheckRange(row1, 0, 255 * 256, "row1");
            CheckRange(row2, 0, 255 * 256, "row2");
 
            this.col1 = col1;
            this.row1 = row1;
            this.col2 = col2;
            this.row2 = row2;
        }
 
        /// <summary>
        /// Calculates the height of a client anchor in points.
        /// </summary>
        /// <param name="sheet">the sheet the anchor will be attached to</param>
        /// <returns>the shape height.</returns>     
        public float GetAnchorHeightInPoints(HH.WMS.Utils.NPOI.SS.UserModel.ISheet sheet)
        {
            int y1 = Dy1;
            int y2 = Dy2;
            int row1 = Math.Min(Row1, Row2);
            int row2 = Math.Max(Row1, Row2);
 
            float points = 0;
            if (row1 == row2)
            {
                points = ((y2 - y1) / 256.0f) * GetRowHeightInPoints(sheet, row2);
            }
            else
            {
                points += ((256.0f - y1) / 256.0f) * GetRowHeightInPoints(sheet, row1);
                for (int i = row1 + 1; i < row2; i++)
                {
                    points += GetRowHeightInPoints(sheet, i);
                }
                points += (y2 / 256.0f) * GetRowHeightInPoints(sheet, row2);
            }
 
            return points;
        }
 
        /// <summary>
        /// Gets the row height in points.
        /// </summary>
        /// <param name="sheet">The sheet.</param>
        /// <param name="rowNum">The row num.</param>
        /// <returns></returns>
        private float GetRowHeightInPoints(HH.WMS.Utils.NPOI.SS.UserModel.ISheet sheet, int rowNum)
        {
            HH.WMS.Utils.NPOI.SS.UserModel.IRow row = sheet.GetRow(rowNum);
            if (row == null)
                return sheet.DefaultRowHeightInPoints;
            else
                return row.HeightInPoints;
        }
 
        /// <summary>
        /// Gets or sets the col1.
        /// </summary>
        /// <value>The col1.</value>
        public int Col1
        {
            get { return col1; }
            set
            {
                CheckRange(value, 0, 255, "col1");
                this.col1 = value;
            }
        }
 
        /// <summary>
        /// Gets or sets the col2.
        /// </summary>
        /// <value>The col2.</value>
        public int Col2
        {
            get { return col2; }
            set
            {
                CheckRange(value, 0, 255, "col2");
                this.col2 = value;
            }
        }
 
        /// <summary>
        /// Gets or sets the row1.
        /// </summary>
        /// <value>The row1.</value>
        public int Row1
        {
            get { return row1; }
            set
            {
                CheckRange(value, 0, 256 * 256, "row1");
                this.row1 = value;
            }
        }
 
        /// <summary>
        /// Gets or sets the row2.
        /// </summary>
        /// <value>The row2.</value>
        public int Row2
        {
            get { return row2; }
            set
            {
                CheckRange(value, 0, 256 * 256, "row2");
                this.row2 = value;
            }
        }
 
        /// <summary>
        /// Sets the top-left and bottom-right
        /// coordinates of the anchor
        /// </summary>
        /// <param name="col1">the column (0 based) of the first cell.</param>
        /// <param name="row1"> the row (0 based) of the first cell.</param>
        /// <param name="x1">the x coordinate within the first cell.</param>
        /// <param name="y1">the y coordinate within the first cell.</param>
        /// <param name="col2">the column (0 based) of the second cell.</param>
        /// <param name="row2">the row (0 based) of the second cell.</param>
        /// <param name="x2">the x coordinate within the second cell.</param>
        /// <param name="y2">the y coordinate within the second cell.</param>
        public void SetAnchor(short col1, int row1, int x1, int y1, short col2, int row2, int x2, int y2)
        {
            CheckRange(x1, 0, 1023, "dx1");
            CheckRange(x2, 0, 1023, "dx2");
            CheckRange(y1, 0, 255, "dy1");
            CheckRange(y2, 0, 255, "dy2");
            CheckRange(col1, 0, 255, "col1");
            CheckRange(col2, 0, 255, "col2");
            CheckRange(row1, 0, 255 * 256, "row1");
            CheckRange(row2, 0, 255 * 256, "row2");
 
            this.col1 = col1;
            this.row1 = row1;
            this.Dx1 = x1;
            this.Dy1 = y1;
            this.col2 = col2;
            this.row2 = row2;
            this.Dx2 = x2;
            this.Dy2 = y2;
        }
 
        /// <summary>
        /// Gets a value indicating whether this instance is horizontally flipped.
        /// </summary>
        /// <value>
        ///     <c>true</c> if the anchor goes from right to left; otherwise, <c>false</c>.
        /// </value>
        public override bool IsHorizontallyFlipped
        {
            get
            {
                if (col1 == col2)
                    return Dx1 > Dx2;
                else
                    return col1 > col2;
            }
        }
 
        /// <summary>
        /// Gets a value indicating whether this instance is vertically flipped.
        /// </summary>
        /// <value>
        ///     <c>true</c> if the anchor goes from bottom to top.; otherwise, <c>false</c>.
        /// </value>
        public override bool IsVerticallyFlipped
        {
            get
            {
                if (row1 == row2)
                    return Dy1 > Dy2;
                else
                    return row1 > row2;
            }
        }
 
        /// <summary>
        /// Gets the anchor type
        /// 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
        /// </summary>
        /// <value>The type of the anchor.</value>
        public int AnchorType
        {
            get{return anchorType;}
            set { this.anchorType = value; }
        }
 
 
        /// <summary>
        /// Checks the range.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="minRange">The min range.</param>
        /// <param name="maxRange">The max range.</param>
        /// <param name="varName">Name of the variable.</param>
        private void CheckRange(int value, int minRange, int maxRange, String varName)
        {
            if (value < minRange || value > maxRange)
                throw new ArgumentException(varName + " must be between " + minRange + " and " + maxRange);
        }
    }
}