Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ExportSymbolicDataAnalysisSolutions/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/3.1.3/EPPlus-3.1.3/Drawing/ExcelDrawingBase.cs @ 9580

Last change on this file since 9580 was 9580, checked in by sforsten, 11 years ago

#1730:

  • added SymbolicDataAnalysisExpressionExcelFormatter
  • changed modifiers in SymbolicExpressionTreeChart of methods SaveImageAsBitmap and SaveImageAsEmf to public
  • added menu item ExportSymbolicSolutionToExcelMenuItem to export a symbolic solution to an excel file
  • added EPPlus-3.1.3 to ExtLibs
File size: 22.5 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Jan Källman    Initial Release           2010-06-01
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections;
34using System.Collections.Generic;
35using System.Text;
36using System.Xml;
37using OfficeOpenXml.Drawing.Chart;
38namespace OfficeOpenXml.Drawing
39{
40    /// <summary>
41    /// Text anchoring
42    /// </summary>
43    public enum eTextAnchoringType
44    {
45        Bottom,
46        Center,
47        Distributed,
48        Justify,
49        Top
50    }
51    /// <summary>
52    /// Vertical text type
53    /// </summary>
54    public enum eTextVerticalType
55    {
56        EastAsianVertical,
57        Horizontal,
58        MongolianVertical,
59        Vertical,
60        Vertical270,
61        WordArtVertical,
62        WordArtVerticalRightToLeft
63
64    }
65    /// <summary>
66    /// How the drawing will be resized.
67    /// </summary>
68    public enum eEditAs
69    {
70        /// <summary>
71        /// Specifies that the current start and end positions shall
72        /// be maintained with respect to the distances from the
73        /// absolute start point of the worksheet.
74        /// </summary>
75        Absolute,
76        /// <summary>
77        /// Specifies that the current drawing shall move with its
78        ///row and column (i.e. the object is anchored to the
79        /// actual from row and column), but that the size shall
80        ///remain absolute.
81        /// </summary>
82        OneCell,
83        /// <summary>
84        /// Specifies that the current drawing shall move and
85        /// resize to maintain its row and column anchors (i.e. the
86        /// object is anchored to the actual from and to row and column).
87        /// </summary>
88        TwoCell
89    }
90    /// <summary>
91    /// Base class for drawings.
92    /// Drawings are Charts, shapes and Pictures.
93    /// </summary>
94    public class ExcelDrawing : XmlHelper
95    {
96        /// <summary>
97        /// Position of the a drawing.
98        /// </summary>
99        public class ExcelPosition : XmlHelper
100        {
101            XmlNode _node;
102            XmlNamespaceManager _ns;           
103            internal ExcelPosition(XmlNamespaceManager ns, XmlNode node) :
104                base (ns,node)
105            {
106                _node = node;
107                _ns = ns;
108            }
109            const string colPath="xdr:col";
110            public int Column
111            {
112                get
113                {
114                    return GetXmlNodeInt(colPath);
115                }
116                set
117                {
118                    SetXmlNodeString(colPath, value.ToString());
119                }
120            }
121            const string rowPath="xdr:row";
122            public int Row
123            {
124                get
125                {
126                    return GetXmlNodeInt(rowPath);
127                }
128                set
129                {
130                    SetXmlNodeString(rowPath, value.ToString());
131                }
132            }
133            const string colOffPath = "xdr:colOff";
134            /// <summary>
135            /// Column Offset
136            ///
137            /// EMU units   1cm         =   1/360000
138            ///             1US inch    =   1/914400
139            ///             1pixel      =   1/9525
140            /// </summary>
141            public int ColumnOff
142            {
143                get
144                {
145                    return GetXmlNodeInt(colOffPath);
146                }
147                set
148                {
149                    SetXmlNodeString(colOffPath, value.ToString());
150                }
151            }
152            const string rowOffPath = "xdr:rowOff";
153            /// <summary>
154            /// Row Offset
155            ///
156            /// EMU units   1cm         =   1/360000
157            ///             1US inch    =   1/914400
158            ///             1pixel      =   1/9525
159            /// </summary>
160            public int RowOff
161            {
162                get
163                {
164                    return GetXmlNodeInt(rowOffPath);
165                }
166                set
167                {
168                    SetXmlNodeString(rowOffPath, value.ToString());
169                }
170            }
171        }
172        protected ExcelDrawings _drawings;
173        protected XmlNode _topNode;
174        string _nameXPath;
175        protected internal int _id;
176        const float STANDARD_DPI = 96;
177        public const int EMU_PER_PIXEL = 9525;
178
179        internal ExcelDrawing(ExcelDrawings drawings, XmlNode node, string nameXPath) :
180            base(drawings.NameSpaceManager, node)
181        {
182            _drawings = drawings;
183            _topNode = node;
184            _id = drawings.Worksheet.Workbook._nextDrawingID++;
185            XmlNode posNode = node.SelectSingleNode("xdr:from", drawings.NameSpaceManager);
186            if (node != null)
187            {
188                From = new ExcelPosition(drawings.NameSpaceManager, posNode);
189            }
190            posNode = node.SelectSingleNode("xdr:to", drawings.NameSpaceManager);
191            if (node != null)
192            {
193                To = new ExcelPosition(drawings.NameSpaceManager, posNode);
194            }
195            _nameXPath = nameXPath;
196            SchemaNodeOrder = new string[] { "from", "to", "graphicFrame", "sp", "clientData"  };
197        }
198        internal ExcelDrawing(XmlNamespaceManager nameSpaceManager, XmlNode node) :
199            base(nameSpaceManager, node)
200        {
201        }
202        /// <summary>
203        /// The name of the drawing object
204        /// </summary>
205        public string Name
206        {
207            get
208            {
209                try
210                {
211                    if (_nameXPath == "") return "";
212                    return GetXmlNodeString(_nameXPath);
213                }
214                catch
215                {
216                    return "";
217                }
218            }
219            set
220            {
221                try
222                {
223                    if (_nameXPath == "") throw new NotImplementedException();
224                    SetXmlNodeString(_nameXPath, value);
225                }
226                catch
227                {
228                    throw new NotImplementedException();
229                }
230            }
231        }
232        /// <summary>
233        /// How Excel resize drawings when the column width is changed within Excel.
234        /// The width of drawings are currently NOT resized in EPPLus when the column width changes
235        /// </summary>
236        public eEditAs EditAs
237        {
238            get
239            {
240                try
241                {
242                    string s = GetXmlNodeString("@editAs");
243                    if (s == "")
244                    {
245                        return eEditAs.TwoCell;
246                    }
247                    else
248                    {
249                        return (eEditAs)Enum.Parse(typeof(eEditAs), s,true);
250                    }
251                }
252                catch
253                {
254                    return eEditAs.TwoCell;
255                }
256            }
257            set
258            {
259                string s=value.ToString();
260                SetXmlNodeString("@editAs", s.Substring(0,1).ToLower()+s.Substring(1,s.Length-1));
261            }
262        }
263        const string lockedPath="xdr:clientData/@fLocksWithSheet";
264        /// <summary>
265        /// Lock drawing
266        /// </summary>
267        public bool Locked
268        {
269            get
270            {
271                return GetXmlNodeBool(lockedPath, true);
272            }
273            set
274            {
275                SetXmlNodeBool(lockedPath, value);
276            }
277        }
278        const string printPath = "xdr:clientData/@fPrintsWithSheet";
279        /// <summary>
280        /// Print drawing with sheet
281        /// </summary>
282        public bool Print
283        {
284            get
285            {
286                return GetXmlNodeBool(printPath, true);
287            }
288            set
289            {
290                SetXmlNodeBool(printPath, value);
291            }
292        }        /// <summary>
293        /// Top Left position
294        /// </summary>
295        public ExcelPosition From { get; set; }
296        /// <summary>
297        /// Bottom right position
298        /// </summary>
299        public ExcelPosition To { get; set; }
300        /// <summary>
301        /// Add new Drawing types here
302        /// </summary>
303        /// <param name="drawings">The drawing collection</param>
304        /// <param name="node">Xml top node</param>
305        /// <returns>The Drawing object</returns>
306        internal static ExcelDrawing GetDrawing(ExcelDrawings drawings, XmlNode node)
307        {
308            if (node.SelectSingleNode("xdr:sp", drawings.NameSpaceManager) != null)
309            {
310                return new ExcelShape(drawings, node);
311            }
312            else if (node.SelectSingleNode("xdr:pic", drawings.NameSpaceManager) != null)
313            {
314                return new ExcelPicture(drawings, node);
315            }
316            else if (node.SelectSingleNode("xdr:graphicFrame", drawings.NameSpaceManager) != null)
317            {
318                return ExcelChart.GetChart(drawings, node);
319            }
320            else
321            {
322                return new ExcelDrawing(drawings, node, "");
323            }
324        }
325        internal string Id
326        {
327            get { return _id.ToString(); }
328        }
329        #region "Internal sizing functions"
330        internal int GetPixelLeft()
331        {
332            ExcelWorksheet ws = _drawings.Worksheet;
333            decimal mdw=ws.Workbook.MaxFontWidth;
334
335            int pix = 0;
336            for (int col = 0; col < From.Column; col++)
337            {
338                pix += (int)decimal.Truncate(((256 * GetColumnWidth(col+1) + decimal.Truncate(128 / (decimal)mdw)) / 256) * mdw);
339            }
340            pix += From.ColumnOff / EMU_PER_PIXEL;
341            return pix;
342        }
343        internal int GetPixelTop()
344        {
345            ExcelWorksheet ws = _drawings.Worksheet;
346            int pix = 0;
347            for (int row = 0; row < From.Row; row++)
348            {
349                pix += (int)(GetRowWidth(row+1) / 0.75);
350            }
351            pix += From.RowOff / EMU_PER_PIXEL;
352            return pix;
353        }
354        internal int GetPixelWidth()
355        {
356            ExcelWorksheet ws = _drawings.Worksheet;
357            decimal mdw = ws.Workbook.MaxFontWidth;
358
359            int pix = -From.ColumnOff / EMU_PER_PIXEL;
360            for (int col = From.Column + 1; col <= To.Column; col++)
361            {
362                pix += (int)decimal.Truncate(((256 * GetColumnWidth(col) + decimal.Truncate(128 / (decimal)mdw)) / 256) * mdw);
363            }
364            pix += To.ColumnOff / EMU_PER_PIXEL;
365            return pix;
366        }
367        internal int GetPixelHeight()
368        {
369            ExcelWorksheet ws = _drawings.Worksheet;
370
371            int pix = -(From.RowOff / EMU_PER_PIXEL);
372            for (int row = From.Row + 1; row <= To.Row; row++)
373            {
374                pix += (int)(GetRowWidth(row) / 0.75);
375            }
376            pix += To.RowOff / EMU_PER_PIXEL;
377            return pix;
378        }
379
380        private decimal GetColumnWidth(int col)
381        {
382            ExcelWorksheet ws = _drawings.Worksheet;
383            if (ws._columns.ContainsKey(ExcelColumn.GetColumnID(ws.SheetID, col)))   //Check that the column exists
384            {
385                return (decimal)ws.Column(col).VisualWidth;
386            }
387            else
388            {
389                return (decimal)ws.DefaultColWidth;
390            }
391        }
392        private double GetRowWidth(int row)
393        {
394            ExcelWorksheet ws = _drawings.Worksheet;
395            if (ws._rows.ContainsKey(ExcelRow.GetRowID(ws.SheetID, row)))   //Check that the row exists
396            {
397                return (double)ws.Row(row).Height;
398            }
399            else
400            {
401                return (double)ws.DefaultRowHeight;
402            }
403        }
404        internal void SetPixelTop(int pixels)
405        {
406            ExcelWorksheet ws = _drawings.Worksheet;
407            decimal mdw = ws.Workbook.MaxFontWidth;
408            int prevPix = 0;
409            int pix = (int)(GetRowWidth(1) / 0.75);
410            int row = 2;
411
412            while (pix < pixels)
413            {
414                prevPix = pix;
415                pix += (int)(GetRowWidth(row++) / 0.75);
416            }
417
418            if (pix == pixels)
419            {
420                From.Row = row - 1;
421                From.RowOff = 0;
422            }   
423            else
424            {
425                From.Row = row - 2;
426                From.RowOff = (pixels - prevPix) * EMU_PER_PIXEL;
427            }
428        }
429        internal void SetPixelLeft(int pixels)
430        {
431            ExcelWorksheet ws = _drawings.Worksheet;
432            decimal mdw = ws.Workbook.MaxFontWidth;
433            int prevPix = 0;
434            int pix = (int)decimal.Truncate(((256 * GetColumnWidth(1) + decimal.Truncate(128 / (decimal)mdw)) / 256) * mdw);
435            int col = 2;
436
437            while (pix < pixels)
438            {
439                prevPix = pix;
440                pix += (int)decimal.Truncate(((256 * GetColumnWidth(col++) + decimal.Truncate(128 / (decimal)mdw)) / 256) * mdw);
441            }
442            if (pix == pixels)
443            {
444                From.Column = col - 1;
445                From.ColumnOff = 0;
446            }
447            else
448            {
449                From.Column = col - 2;
450                From.ColumnOff = (pixels - prevPix) * EMU_PER_PIXEL;
451            }
452        }
453        internal void SetPixelHeight(int pixels)
454        {
455            SetPixelHeight(pixels, STANDARD_DPI);
456        }
457        internal void SetPixelHeight(int pixels, float dpi)
458        {
459            ExcelWorksheet ws = _drawings.Worksheet;
460            //decimal mdw = ws.Workbook.MaxFontWidth;
461            pixels = (int)(pixels / (dpi / STANDARD_DPI) + .5);
462            int pixOff = pixels - ((int)(ws.Row(From.Row + 1).Height / 0.75) - (int)(From.RowOff / EMU_PER_PIXEL));
463            int prevPixOff = pixels;
464            int row = From.Row+1;
465
466            while (pixOff >= 0)
467            {
468                prevPixOff = pixOff;
469                pixOff -= (int)(GetRowWidth(++row) / 0.75);
470            }
471            To.Row = row - 1;
472            if (From.Row == To.Row)
473            {
474                To.RowOff = From.RowOff + (pixels) * EMU_PER_PIXEL;
475            }
476            else
477            {
478                To.RowOff = prevPixOff * EMU_PER_PIXEL;
479            }
480        }
481        internal void SetPixelWidth(int pixels)
482        {
483            SetPixelWidth(pixels, STANDARD_DPI);
484        }
485        internal void SetPixelWidth(int pixels, float dpi)
486        {
487            ExcelWorksheet ws = _drawings.Worksheet;
488            decimal mdw = ws.Workbook.MaxFontWidth;
489
490            pixels = (int)(pixels / (dpi / STANDARD_DPI) + .5);
491            int pixOff = (int)pixels - ((int)decimal.Truncate(((256 * GetColumnWidth(From.Column + 1) + decimal.Truncate(128 / (decimal)mdw)) / 256) * mdw) - From.ColumnOff / EMU_PER_PIXEL);
492            int prevPixOff = From.ColumnOff / EMU_PER_PIXEL + (int)pixels;
493            int col = From.Column + 2;
494
495            while (pixOff >= 0)
496            {
497                prevPixOff = pixOff;
498                pixOff -= (int)decimal.Truncate(((256 * GetColumnWidth(col++) + decimal.Truncate(128 / (decimal)mdw)) / 256) * mdw);
499            }
500
501            To.Column = col - 2;
502            To.ColumnOff = prevPixOff * EMU_PER_PIXEL;
503        }
504        #endregion
505        #region "Public sizing functions"
506        /// <summary>
507        /// Set the top left corner of a drawing.
508        /// Note that resizing columns / rows after using this function will effect the position of the drawing
509        /// </summary>
510        /// <param name="PixelTop">Top pixel</param>
511        /// <param name="PixelLeft">Left pixel</param>
512        public void SetPosition(int PixelTop, int PixelLeft)
513        {
514            int width = GetPixelWidth();
515            int height = GetPixelHeight();
516
517            SetPixelTop(PixelTop);
518            SetPixelLeft(PixelLeft);
519
520            SetPixelWidth(width);
521            SetPixelHeight(height);
522        }
523        /// <summary>
524        /// Set the top left corner of a drawing.
525        /// Note that resizing columns / rows after using this function will effect the position of the drawing
526        /// </summary>
527        /// <param name="Row">Start row</param>
528        /// <param name="RowOffsetPixels">Offset in pixels</param>
529        /// <param name="Column">Start Column</param>
530        /// <param name="ColumnOffsetPixels">Offset in pixels</param>
531        public void SetPosition(int Row, int RowOffsetPixels, int Column, int ColumnOffsetPixels)
532        {
533            int width = GetPixelWidth();
534            int height = GetPixelHeight();
535
536            From.Row = Row;
537            From.RowOff = RowOffsetPixels * EMU_PER_PIXEL;
538            From.Column = Column;
539            From.ColumnOff = ColumnOffsetPixels * EMU_PER_PIXEL;
540
541            SetPixelWidth(width);
542            SetPixelHeight(height);
543        }
544        /// <summary>
545        /// Set size in Percent
546        /// Note that resizing columns / rows after using this function will effect the size of the drawing
547        /// </summary>
548        /// <param name="Percent"></param>
549        public virtual void SetSize(int Percent)
550        {
551            int width = GetPixelWidth();
552            int height = GetPixelHeight();
553
554            width = (int)(width * ((decimal)Percent / 100));
555            height = (int)(height * ((decimal)Percent / 100));
556
557            SetPixelWidth(width, 96);
558            SetPixelHeight(height, 96);
559        }
560        /// <summary>
561        /// Set size in pixels
562        /// Note that resizing columns / rows after using this function will effect the size of the drawing
563        /// </summary>
564        /// <param name="PixelWidth">Width in pixels</param>
565        /// <param name="PixelHeight">Height in pixels</param>
566        public void SetSize(int PixelWidth, int PixelHeight)
567        {
568            SetPixelWidth(PixelWidth);
569            SetPixelHeight(PixelHeight);
570        }
571        #endregion
572
573        internal static string GetTextAchoringText(eTextAnchoringType value)
574        {
575            switch (value)
576            {
577                case eTextAnchoringType.Bottom:
578                    return "b";
579                case eTextAnchoringType.Center:
580                    return "ctr";
581                case eTextAnchoringType.Distributed:
582                    return "dist";
583                case eTextAnchoringType.Justify:
584                    return "just";
585                default:
586                    return "t";
587            }
588        }
589        internal static eTextAnchoringType GetTextAchoringEnum(string text)
590        {
591            switch (text)
592            {
593                case "b":
594                    return eTextAnchoringType.Bottom;
595                case "ctr":
596                    return eTextAnchoringType.Center;
597                case "dist":
598                    return eTextAnchoringType.Distributed;
599                case "just":
600                    return eTextAnchoringType.Justify;
601                default:
602                    return eTextAnchoringType.Top;
603            }
604        }
605        internal static string GetTextVerticalText(eTextVerticalType value)
606        {
607            switch (value)
608            {
609                case eTextVerticalType.EastAsianVertical:
610                    return "eaVert";
611                case eTextVerticalType.MongolianVertical:
612                    return "mongolianVert";
613                case eTextVerticalType.Vertical:
614                    return "vert";
615                case eTextVerticalType.Vertical270:
616                    return "vert270";
617                case eTextVerticalType.WordArtVertical:
618                    return "wordArtVert";
619                case eTextVerticalType.WordArtVerticalRightToLeft:
620                    return "wordArtVertRtl";
621                default:
622                    return "horz";
623            }
624        }
625        internal static eTextVerticalType GetTextVerticalEnum(string text)
626        {
627            switch (text)
628            {
629                case "eaVert":
630                    return eTextVerticalType.EastAsianVertical;
631                case "mongolianVert":
632                    return eTextVerticalType.MongolianVertical;
633                case "vert":
634                    return eTextVerticalType.Vertical;
635                case "vert270":
636                    return eTextVerticalType.Vertical270;
637                case "wordArtVert":
638                    return eTextVerticalType.WordArtVertical;
639                case "wordArtVertRtl":
640                    return eTextVerticalType.WordArtVerticalRightToLeft;
641                default:
642                    return eTextVerticalType.Horizontal;
643            }
644        }
645        internal virtual void DeleteMe()
646        {
647            TopNode.ParentNode.RemoveChild(TopNode);
648        }
649    }
650}
Note: See TracBrowser for help on using the repository browser.