using System; using CPF.Drawing; using CPF.ReoGrid.Common; using CPF.ReoGrid.Core; using CPF.ReoGrid.Drawing; using CPF.ReoGrid.Graphics; namespace CPF.ReoGrid.Rendering { internal class WPFRenderer : WPFGraphics, IRenderer, IGraphics { internal WPFRenderer(ReoGridControl gridControl) { this.gridControl = gridControl; } public Size MeasureCellText(Cell cell, DrawMode drawMode, float scale) { Worksheet worksheet = cell.Worksheet; bool flag = worksheet == null; Size result; if (flag) { result = default(Size); } else { WorksheetRangeStyle innerStyle = cell.InnerStyle; float num = 0f; float num2 = 0f; bool flag2 = cell.Style.RotationAngle != 0f; if (flag2) { double num3 = (double)innerStyle.RotationAngle * 3.141592653589793 / 180.0; num = (float)Math.Sin(num3); num2 = (float)Math.Cos(num3); } bool flag3 = innerStyle.TextWrapMode == TextWrapMode.NoWrap; float width; if (flag3) { width = 9999999f; } else { bool flag4 = cell.Style.RotationAngle != 0f; float num4; if (flag4) { num4 = Math.Abs(cell.Bounds.Width * num2) + Math.Abs(cell.Bounds.Height * num); } else { num4 = cell.Bounds.Width; bool flag5 = cell.InnerStyle != null; if (flag5) { bool flag6 = (cell.InnerStyle.Flag & PlainStyleFlag.Indent) == PlainStyleFlag.Indent; if (flag6) { num4 -= (float)((int)((float)cell.InnerStyle.Indent * worksheet.IndentSize)); } } num4 -= 2f; } width = (float)((int)Math.Round((double)(num4 * scale))); } bool fontDirty = cell.FontDirty; if (fontDirty) { worksheet.UpdateCellRenderFont(this, cell, drawMode, UpdateFontReason.FontChanged); } Font font; if (drawMode == DrawMode.View || drawMode - DrawMode.Preview > 1) { font = cell.RenderFont; } else { font = this.resourceManager.GetFont(cell.RenderFont.FontFamily, (double)(innerStyle.FontSize * scale), cell.RenderFont.FontStyle); } Size size = DrawingFactory.Default.MeasureString(cell.DisplayText, font, width); float num5 = size.Height; size.Height = num5 + 1f; bool flag7 = innerStyle.RotationAngle != 0f; if (flag7) { float num6 = Math.Abs(size.Width * num2) + Math.Abs(size.Height * num); float num7 = Math.Abs(size.Width * num) + Math.Abs(size.Height * num2); num5 = num6 + 1f; float num8 = num7 + 1f; size = new Size(ref num5, ref num8); } result = size; } return result; } public void DrawCellText(Cell cell, Color textColor, DrawMode drawMode, float scale) { Worksheet worksheet = cell.Worksheet; bool flag = worksheet == null; if (!flag) { Brush brush = this.GetBrush(textColor); bool flag2 = brush == null; if (!flag2) { Rect rect; Font font; TextDecoration textDecoration; if (drawMode == DrawMode.View || drawMode - DrawMode.Preview > 1) { rect = cell.TextBounds; font = cell.RenderFont; textDecoration = cell.TextDecoration; } else { rect = cell.PrintTextBounds; font = this.resourceManager.GetFont(cell.RenderFont.FontFamily, (double)(cell.InnerStyle.FontSize * scale), cell.RenderFont.FontStyle); textDecoration = cell.TextDecoration; } textDecoration.Brush = brush; textDecoration.Stroke = new Stroke(1f); TextAlignment textAlignment = TextAlignment.Left; ReoGridHorAlign halign = cell.InnerStyle.HAlign; ReoGridHorAlign reoGridHorAlign = halign; if (reoGridHorAlign != ReoGridHorAlign.Center) { if (reoGridHorAlign == ReoGridHorAlign.Right) { textAlignment = TextAlignment.Right; } } else { textAlignment = TextAlignment.Center; } bool flag3 = cell.InnerStyle.TextWrapMode == TextWrapMode.NoWrap; if (flag3) { rect.Width = float.MaxValue; } Size size = DrawingFactory.Default.MeasureString(cell.DisplayText, font, rect.Width); DrawingContext platformGraphics = base.PlatformGraphics; bool flag4 = cell.InnerStyle.RotationAngle != 0f; if (flag4) { base.PushTransform(); Point point = rect.Center; float x = point.X; point = rect.Center; base.TranslateTransform(x, point.Y); base.RotateTransform(-cell.InnerStyle.RotationAngle); AntialiasMode antialiasMode = platformGraphics.AntialiasMode; platformGraphics.AntialiasMode = AntialiasMode.AntiAlias; DrawingContext drawingContext = platformGraphics; point = default(Point); Brush fillBrush = brush; string displayText = cell.DisplayText; float width = rect.Width; float maxValue = float.MaxValue; TextTrimming textTrimming = TextTrimming.None; Stroke stroke = default(Stroke); drawingContext.DrawString(point, fillBrush, displayText, font, textAlignment, width, textDecoration, maxValue, textTrimming, stroke, null); platformGraphics.AntialiasMode = antialiasMode; base.PopTransform(); } else { Point location = rect.Location; ReoGridVerAlign valign = cell.InnerStyle.VAlign; ReoGridVerAlign reoGridVerAlign = valign; if (reoGridVerAlign != ReoGridVerAlign.Middle) { if (reoGridVerAlign == ReoGridVerAlign.Bottom) { location.Y += rect.Height - size.Height; } } else { location.Y += (rect.Height - size.Height) / 2f; } AntialiasMode antialiasMode2 = platformGraphics.AntialiasMode; platformGraphics.AntialiasMode = AntialiasMode.AntiAlias; DrawingContext drawingContext2 = platformGraphics; Brush fillBrush2 = brush; string displayText2 = cell.DisplayText; float width = rect.Width; float maxValue = float.MaxValue; TextTrimming textTrimming = TextTrimming.None; Stroke stroke = default(Stroke); drawingContext2.DrawString(location, fillBrush2, displayText2, font, textAlignment, width, textDecoration, maxValue, textTrimming, stroke, null); platformGraphics.AntialiasMode = antialiasMode2; } } } } private static Color DecideTextColor(Cell cell) { Worksheet worksheet = cell.Worksheet; ControlAppearanceStyle controlStyle = worksheet.controlAdapter.ControlStyle; bool flag = !cell.RenderColor.IsTransparent; Color result; if (flag) { result = cell.RenderColor; } else { bool flag2 = cell.InnerStyle.HasStyle(PlainStyleFlag.TextColor); if (flag2) { result = cell.InnerStyle.TextColor; } else { bool flag3 = !controlStyle.TryGetColor(ControlAppearanceColors.GridText, out result); if (flag3) { result = Color.Black; } } } return result; } public void UpdateCellRenderFont(Cell cell, UpdateFontReason reason) { Worksheet worksheet = cell.Worksheet; bool flag = worksheet == null || worksheet.controlAdapter == null; if (!flag) { WorksheetRangeStyle innerStyle = cell.InnerStyle; FontStyles fontStyles = PlatformUtility.ToWPFFontStyle(innerStyle.fontStyles); cell.TextDecoration = PlatformUtility.ToWPFFontDecorations(innerStyle.fontStyles); bool flag2 = innerStyle.FontSize <= 0f; if (flag2) { innerStyle.FontSize = 6f; } float num = (float)Math.Round((double)(innerStyle.FontSize * worksheet.renderScaleFactor), 1); Font renderFont = cell.RenderFont; bool flag3 = renderFont.FontFamily != innerStyle.FontName || renderFont.FontSize != num || renderFont.FontStyle != fontStyles; if (flag3) { cell.RenderFont = this.resourceManager.GetFont(innerStyle.FontName, (double)num, fontStyles); } } } public void DrawRunningFocusRect(float x, float y, float w, float h, Color color, int runningOffset) { } public void BeginCappedLine(LineCapStyles startCap, Size startSize, LineCapStyles endCap, Size endSize, Color color, float width) { this.capLinePen = new CPFPen(color, width); this.capLinePen.LineCap = PlatformUtility.ToWPFLineCap(startCap); this.capLinePen.LineCap = PlatformUtility.ToWPFLineCap(endCap); } public void DrawCappedLine(float x1, float y1, float x2, float y2) { bool flag = this.capLinePen != null; if (flag) { base.DrawLine(this.capLinePen, x1, y1, x2, y2); } } public void EndCappedLine() { this.capLinePen = null; } public void BeginDrawLine(float width, Color color) { this.cachePen = new CPFPen(color, width); } public void DrawLine(float x1, float y1, float x2, float y2) { base.DrawLine(this.cachePen, new Point(ref x1, ref y1), new Point(ref x2, ref y2)); } public void EndDrawLine() { } public void DrawLeadHeadArrow(Rect bounds, Color startColor, Color endColor) { } public CPFPen GetPen(Color color) { return this.resourceManager.GetPen(color); } public void ReleasePen(CPFPen pen) { } public Brush GetBrush(Color color) { return this.resourceManager.GetBrush(color); } public void BeginDrawHeaderText(float scale) { this.scaledHeaderFont = this.resourceManager.GetFont(this.gridControl.HeaderFontFamily, (double)(this.gridControl.HeaderFontSize * scale), this.gridControl.HeaderFontStyle); } public void DrawHeaderText(string text, Brush brush, Rect rect) { Size size = base.PlatformGraphics.DrawingFactory.MeasureString(text, this.scaledHeaderFont, rect.Width); Point location = rect.Location; location.Y = rect.Y + (rect.Height - size.Height) / 2f; AntialiasMode antialiasMode = base.PlatformGraphics.AntialiasMode; base.PlatformGraphics.AntialiasMode = AntialiasMode.AntiAlias; DrawingContext platformGraphics = base.PlatformGraphics; TextAlignment textAlignment = TextAlignment.Center; float width = rect.Width; TextDecoration textDecoration = default(TextDecoration); float maxValue = float.MaxValue; TextTrimming textTrimming = TextTrimming.None; Stroke stroke = default(Stroke); platformGraphics.DrawString(location, brush, text, this.scaledHeaderFont, textAlignment, width, textDecoration, maxValue, textTrimming, stroke, null); base.PlatformGraphics.AntialiasMode = antialiasMode; } public ResourcePoolManager GetResourcePoolManager { get { return this.resourceManager; } } private ReoGridControl gridControl; private CPFPen capLinePen = null; private CPFPen cachePen = null; private Font scaledHeaderFont; } }