コード例 #1
0
ファイル: Graphics.cs プロジェクト: datadiode/ReoGrid
        public void DrawCellText(Cell cell, Rectangle textBounds, SolidColor textColor, DrawMode drawMode, float scale)
        {
            var sheet = cell.Worksheet;

            if (sheet == null)
            {
                return;
            }

            var b = this.GetBrush(textColor);

            if (b == null)
            {
                return;
            }

            System.Drawing.Font scaledFont;

            #region Determine text bounds
            switch (drawMode)
            {
            default:
            case DrawMode.View:
                scaledFont = cell.RenderFont;
                break;

            case DrawMode.Preview:
            case DrawMode.Print:
                scaledFont = this.resourceManager.GetFont(cell.RenderFont.Name,
                                                          cell.InnerStyle.FontSize * scale, cell.RenderFont.Style);
                break;
            }
            #endregion             // Determine text bounds

            using (var sf = new StringFormat(this.measureSf))
            {
                #region Set sf wrap
                if (cell.InnerStyle.TextWrapMode == TextWrapMode.NoWrap)
                {
                    sf.FormatFlags |= StringFormatFlags.NoWrap;
                }
                else
                {
                    sf.FormatFlags &= ~StringFormatFlags.NoWrap;
                }
                #endregion                 // Set sf wrap

                #region Rotate text
                if (cell.InnerStyle.RotationAngle != 0)
                {
#if DEBUG1
                    g.DrawRectangle(Pens.Red, (System.Drawing.Rectangle)textBounds);
#endif // DEBUG

                    this.PushTransform();

                    this.TranslateTransform(textBounds.OriginX, textBounds.OriginY);
                    this.RotateTransform(-cell.InnerStyle.RotationAngle);

                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment     = StringAlignment.Center;

                    PlatformGraphics.DrawString(cell.DisplayText, scaledFont, b, 0, 0, sf);

                    this.PopTransform();
                }
                else
                #endregion                 // Rotate text
                {
                    #region Align StringFormat
                    switch (cell.RenderHorAlign)
                    {
                    default:
                    case ReoGridRenderHorAlign.Left:
                        sf.Alignment = System.Drawing.StringAlignment.Near;
                        break;

                    case ReoGridRenderHorAlign.Center:
                        sf.Alignment = System.Drawing.StringAlignment.Center;
                        if (scaledFont.Italic)
                        {
                            textBounds.X -= scaledFont.Size / 6;
                        }
                        break;

                    case ReoGridRenderHorAlign.Right:
                        sf.Alignment = System.Drawing.StringAlignment.Far;
                        if (scaledFont.Italic)
                        {
                            textBounds.X -= scaledFont.Size / 3;
                        }
                        break;
                    }

                    switch (cell.InnerStyle.VAlign)
                    {
                    case ReoGridVerAlign.Top:
                        sf.LineAlignment = System.Drawing.StringAlignment.Near;
                        break;

                    default:
                    case ReoGridVerAlign.Middle:
                        sf.LineAlignment = System.Drawing.StringAlignment.Center;
                        break;

                    case ReoGridVerAlign.Bottom:
                        sf.LineAlignment = System.Drawing.StringAlignment.Far;
                        break;
                    }
                    #endregion                     // Align StringFormat

                    PlatformGraphics.DrawString(cell.DisplayText, scaledFont, b, textBounds, sf);
                }
            }
        }
コード例 #2
0
ファイル: Graphics.cs プロジェクト: datadiode/ReoGrid
        public Graphics.Size MeasureCellText(Cell cell, DrawMode drawMode, float scale)
        {
            var sheet = cell.Worksheet;

            if (sheet == null)
            {
                return(Graphics.Size.Zero);
            }

            WorksheetRangeStyle style = cell.InnerStyle;

            int fieldWidth = 0;

            double s = 0, c = 0;

            if (cell.Style.RotationAngle != 0)
            {
                double d = (style.RotationAngle * Math.PI / 180.0d);
                s = Math.Sin(d);
                c = Math.Cos(d);
            }

            // merged cell need word break automatically
            if (style.TextWrapMode != TextWrapMode.NoWrap)
            {
                // get cell available width
                float cellWidth = 0;

                if (cell.Style.RotationAngle != 0)
                {
                    cellWidth = (float)(Math.Abs(cell.Bounds.Width * c) + Math.Abs(cell.Bounds.Height * s));
                }
                else
                {
                    cellWidth = cell.Bounds.Width;

                    if (cell.InnerStyle != null)
                    {
                        if ((cell.InnerStyle.Flag & PlainStyleFlag.Indent) == PlainStyleFlag.Indent)
                        {
                            cellWidth -= (int)(cell.InnerStyle.Indent * sheet.IndentSize);
                        }
                    }

                    // border width
                    cellWidth -= 2;
                }

                // word break
                fieldWidth = (int)Math.Round(cellWidth * scale);
            }

            var scaledFont = cell.RenderFont;

            switch (drawMode)
            {
            case DrawMode.Preview:
            case DrawMode.Print:
                scaledFont = resourceManager.GetFont(scaledFont.Name, style.FontSize * scale, scaledFont.Style);
                break;
            }

            SizeF size = cachedGraphics.MeasureString(cell.DisplayText, scaledFont, fieldWidth, measureSf);

            size.Height += scale;

            if (style.RotationAngle != 0)
            {
                float w = (float)(Math.Abs(size.Width * c) + Math.Abs(size.Height * s));
                float h = (float)(Math.Abs(size.Width * s) + Math.Abs(size.Height * c));

                size = new SizeF(w + scale, h + scale);
            }

            return(size);
        }
コード例 #3
0
ファイル: Graphics.cs プロジェクト: fredatgithub/ReoGrid
        public Graphics.Size MeasureCellText(Cell cell, DrawMode drawMode, float scale)
        {
            var sheet = cell.Worksheet;

            if (sheet == null)
            {
                return(Graphics.Size.Zero);
            }

            WorksheetRangeStyle style = cell.InnerStyle;

            int fieldWidth = 0;

            double s = 0, c = 0;

            //if (sf == null) sf = new System.Drawing.StringFormat(System.Drawing.StringFormat.GenericTypographic);

            if (cell.Style.RotateAngle != 0)
            {
                double d = (style.RotateAngle * Math.PI / 180.0d);
                s = Math.Sin(d);
                c = Math.Cos(d);
            }

            lock (sf)
            {
                // merged cell need word break automatically
                if (style.TextWrapMode == TextWrapMode.NoWrap)
                {
                    // no word break
                    fieldWidth      = 9999999;                // TODO: avoid magic number
                    sf.FormatFlags |= System.Drawing.StringFormatFlags.NoWrap;
                }
                else
                {
                    // get cell available width
                    float cellWidth = 0;

                    if (cell.Style.RotateAngle != 0)
                    {
                        cellWidth = (float)(Math.Abs(cell.Bounds.Width * c) + Math.Abs(cell.Bounds.Height * s));
                    }
                    else
                    {
                        cellWidth = cell.Bounds.Width;

                        if (cell.InnerStyle != null)
                        {
                            if ((cell.InnerStyle.Flag & PlainStyleFlag.Indent) == PlainStyleFlag.Indent)
                            {
                                cellWidth -= (int)(cell.InnerStyle.Indent * sheet.IndentSize);
                            }
                        }

                        // border width
                        cellWidth -= 2;
                    }

                    // word break
                    fieldWidth      = (int)Math.Round(cellWidth * scale);
                    sf.FormatFlags &= ~System.Drawing.StringFormatFlags.NoWrap;
                }

                if (cell.FontDirty)
                {
                    sheet.UpdateCellRenderFont(this, cell, drawMode, UpdateFontReason.FontChanged);
                }

                var g = this.cachedGraphics;

                System.Drawing.Font scaledFont;
                switch (drawMode)
                {
                default:
                case DrawMode.View:
                    scaledFont = cell.RenderFont;
                    break;

                case DrawMode.Preview:
                case DrawMode.Print:
                    scaledFont = this.resourceManager.GetFont(cell.RenderFont.Name,
                                                              style.FontSize * scale, cell.RenderFont.Style);
                    g = this.PlatformGraphics;
                    System.Diagnostics.Debug.Assert(g != null);
                    break;
                }

                SizeF size = g.MeasureString(cell.DisplayText, scaledFont, fieldWidth, sf);
                size.Height++;

                if (style.RotateAngle != 0)
                {
                    float w = (float)(Math.Abs(size.Width * c) + Math.Abs(size.Height * s));
                    float h = (float)(Math.Abs(size.Width * s) + Math.Abs(size.Height * c));

                    size = new SizeF(w + 1, h + 1);
                }

                return(size);
            }
        }