protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Create a Font, Brush and TextFormat to draw our text. // For more information on drawing Text, please see the source code for the Text DrawingTool var font = new Gui.Tools.SimpleFont("Arial", 20); SharpDX.Direct2D1.Brush tmpBrush = Brushes.Red.ToDxBrush(RenderTarget); SharpDX.DirectWrite.TextFormat textFormat = font.ToDirectWriteTextFormat(); // Create a TextLayout for our text to draw. var cachedTextLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, "Hello, I am sideways text.", textFormat, 600, textFormat.FontSize); // Rotate the RenderTarget by setting the Matrix3x2 Transform property // Matrix3x2.Rotation() will return a rotated Matrix3x2 based off of the angle specified, and the center point where you draw the object RenderTarget.Transform = Matrix3x2.Rotation(1.5708f, new Vector2(100, 100)); // Draw the text on the rotated RenderTarget RenderTarget.DrawTextLayout(new SharpDX.Vector2(100, 100), cachedTextLayout, tmpBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap); // Dispose of resources textFormat.Dispose(); cachedTextLayout.Dispose(); tmpBrush.Dispose(); // Rotate the RenderTarget back RenderTarget.Transform = Matrix3x2.Identity; // Return rendering to base class base.OnRender(chartControl, chartScale); }
protected override void OnStateChange() { base.OnStateChange(); if (State == State.SetDefaults) { Name = "Labeled Line"; OutlineStroke = new Stroke(Brushes.Black, 2f); BackgroundBrush = Brushes.Black; OffScreenBrush = Brushes.Red; DisplayText = String.Empty; AppendPriceTime = true; Font = null; AreaOpacity = 75; TextDisplayMode = TextMode.EndPointAtPriceScale; HorizontalOffset = 0.5; VerticalOffset = 3; offScreenDXBrushNeedsUpdate = true; backgroundDXBrushNeedsUpdate = true; } else if (State == State.Terminated) { if (cachedTextLayout != null) { cachedTextLayout.Dispose(); } cachedTextLayout = null; } }
private void DrawString(string text, SimpleFont font, DXMediaBrush brush, double pointX, double pointY, DXMediaBrush areaBrush) { SharpDX.DirectWrite.TextFormat textFormat = font.ToDirectWriteTextFormat(); SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, text, textFormat, ChartPanel.X + ChartPanel.W, textFormat.FontSize); float newW = textLayout.Metrics.Width; float newH = textLayout.Metrics.Height; SharpDX.Vector2 TextPlotPoint = new System.Windows.Point(pointX - newW, pointY - textLayout.Metrics.Height / 2 - 1).ToVector2(); SharpDX.RectangleF PLBoundRect = new SharpDX.RectangleF((float)pointX - newW - 4, (float)pointY - textLayout.Metrics.Height / 2 - 1, newW + 6, newH + 2); SharpDX.Direct2D1.RoundedRectangle PLRoundedRect = new SharpDX.Direct2D1.RoundedRectangle(); PLRoundedRect.RadiusX = newW / 4; PLRoundedRect.RadiusY = newH / 4; PLRoundedRect.Rect = PLBoundRect; RenderTarget.FillRoundedRectangle(PLRoundedRect, areaBrush.DxBrush); RenderTarget.DrawTextLayout(TextPlotPoint, textLayout, brush.DxBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); textLayout = null; textFormat.Dispose(); textFormat = null; }
protected override void Dispose(bool disposing) { base.Dispose(disposing); try { if (textLayout != null) { textLayout.Dispose(); } textFormat = null; // this triggers native brush disposes textDeviceBrush.RenderTarget = null; textBackgroundDeviceBrush.RenderTarget = null; } catch { } finally { LineColor = null; } }
private void Init() { Color = Base.Brushes.GetColor(Settings.Colors.PidBox); var text = new SharpDX.DirectWrite.TextLayout(Base.DWFactory, "999 FPS", Base.DefaultTextFormat, Screen.ScreenSize.Width, Screen.ScreenSize.Height); if (Math.Round(text.Metrics.Width) != text.Metrics.Width) { Debug.WriteLine("Text metrics produce float values"); } Size = new RawVector2(text.Metrics.Width + 2, text.Metrics.Height + 2); text.Dispose(); }
protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { /// get the starting and ending bars from what is rendered on the chart float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex); float endX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); /// Loop through each Plot Values on the chart for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { /// get the value at the last bar on the chart (if it has been set) if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) { double plotValue = Values[seriesCount].GetValueAt(ChartBars.ToIndex); /// convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); /// calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); /// draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX, Plots[seriesCount].Width, Plots[seriesCount].StrokeStyle); /// use the chart control text form to draw plot values along the line SharpDX.DirectWrite.TextFormat textFormat = chartControl.Properties.LabelFont.ToDirectWriteTextFormat(); /// calculate the which will be rendered at each plot using it the plot name and its price string textToRender = Plots[seriesCount].Name + ": " + plotValue; /// calculate the layout of the text to be drawn SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, textToRender, textFormat, 200, textFormat.FontSize); /// draw a line at each plot using the plots SharpDX Brush color at the calculated start point RenderTarget.DrawTextLayout(startPoint, textLayout, Plots[seriesCount].BrushDX); // /// dipose of the unmanaged resources used textLayout.Dispose(); textFormat.Dispose(); } } }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (cachedTextLayout != null) { cachedTextLayout.Dispose(); } if (textBrushDevice != null) { textBrushDevice.RenderTarget = null; } if (areaBrushDevice != null) { areaBrushDevice.RenderTarget = null; } cachedTextLayout = null; textBrushDevice = null; areaBrushDevice = null; }
private void DrawString(string text, SimpleFont font, string brushName, double pointX, double pointY, string areaBrushName) { SharpDX.DirectWrite.TextFormat textFormat = font.ToDirectWriteTextFormat(); SharpDX.Vector2 TextPlotPoint = new System.Windows.Point(pointX, pointY).ToVector2(); SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, text, textFormat, ChartPanel.X + ChartPanel.W, textFormat.FontSize); float newW = textLayout.Metrics.Width; float newH = textLayout.Metrics.Height; SharpDX.RectangleF PLBoundRect = new SharpDX.RectangleF((float)pointX + 2, (float)pointY, newW + 5, newH + 2); RenderTarget.FillRectangle(PLBoundRect, dxmBrushes[areaBrushName].DxBrush); RenderTarget.DrawTextLayout(TextPlotPoint, textLayout, dxmBrushes[brushName].DxBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); textFormat.Dispose(); }
protected void DrawPriceLevelText(double minX, double maxX, Point endPoint, PriceLevel priceLevel, ChartPanel panel) { Gui.Tools.SimpleFont wpfFont = panel.ChartControl.Properties.LabelFont ?? new Gui.Tools.SimpleFont(); SharpDX.DirectWrite.TextFormat dxTextFormat = wpfFont.ToDirectWriteTextFormat(); string str = string.Format("{0}", (priceLevel.Value / 100).ToString("P")); SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, str, dxTextFormat, panel.H, dxTextFormat.FontSize); float usedFontHeight = textLayout.Metrics.Height; float usedFontWidth = textLayout.Metrics.Width; Point textEndPoint = endPoint; double maxWidth = panel.X + panel.W; double maxHeight = panel.Y + panel.H; double minWidth = panel.X; double minHeight = panel.Y; if (textEndPoint.Y + usedFontHeight >= maxHeight) { textEndPoint.Y = maxHeight - usedFontHeight; // Set to bottom } if (textEndPoint.Y < minHeight) // Set to top { textEndPoint.Y = minHeight; } if (textEndPoint.X + usedFontWidth >= maxWidth) { textEndPoint.X = maxWidth - usedFontWidth; //Set to right side; } if (textEndPoint.X < minWidth) { textEndPoint.X = minWidth; // Set to left side; } RenderTarget.DrawTextLayout(new SharpDX.Vector2((float)(textEndPoint.X), (float)(textEndPoint.Y)), textLayout, priceLevel.Stroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); dxTextFormat.Dispose(); textLayout.Dispose(); }
public override void OnRender(ChartControl chartControl, ChartScale chartScale) { if (firstTime && DrawingState == DrawingState.Normal) { firstTime = false; Cbi.License.Log("Path"); } RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive; Stroke outlineStroke = OutlineStroke; outlineStroke.RenderTarget = RenderTarget; ChartPanel chartPanel = chartControl.ChartPanels[PanelIndex]; double strokePixAdjust = outlineStroke.Width % 2 == 0 ? 0.5d : 0d; Vector pixelAdjustVec = new Vector(strokePixAdjust, strokePixAdjust); SharpDX.Direct2D1.PathGeometry polyGeo = CreatePathGeometry(chartControl, chartPanel, chartScale, strokePixAdjust); SharpDX.Direct2D1.Brush tmpBrush = IsInHitTest ? chartControl.SelectionBrush : outlineStroke.BrushDX; RenderTarget.DrawGeometry(polyGeo, tmpBrush, outlineStroke.Width, outlineStroke.StrokeStyle); polyGeo.Dispose(); if (PathBegin == PathToolCapMode.Arrow || PathEnd == PathToolCapMode.Arrow) { Point[] points = GetPathAnchorPoints(chartControl, chartScale); if (points.Length > 1) { if (arrowPathGeometry == null) { arrowPathGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); SharpDX.Direct2D1.GeometrySink geometrySink = arrowPathGeometry.Open(); float arrowWidth = 6f; SharpDX.Vector2 top = new SharpDX.Vector2(0, outlineStroke.Width * 0.5f); geometrySink.BeginFigure(top, SharpDX.Direct2D1.FigureBegin.Filled); geometrySink.AddLine(new SharpDX.Vector2(arrowWidth, -arrowWidth)); geometrySink.AddLine(new SharpDX.Vector2(-arrowWidth, -arrowWidth)); geometrySink.AddLine(top); // cap off figure geometrySink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed); geometrySink.Close(); } if (PathBegin == PathToolCapMode.Arrow) { Vector lineVector = points[0] - points[1]; lineVector.Normalize(); Point pointAdjusted = points[0] + pixelAdjustVec; SharpDX.Vector2 pointVec = pointAdjusted.ToVector2(); float vectorAngle = -(float)Math.Atan2(lineVector.X, lineVector.Y); Vector adjustVector = lineVector * 5; SharpDX.Vector2 arrowPointVec = new SharpDX.Vector2((float)(pointVec.X + adjustVector.X), (float)(pointVec.Y + adjustVector.Y)); SharpDX.Matrix3x2 transformMatrix2 = SharpDX.Matrix3x2.Rotation(vectorAngle, SharpDX.Vector2.Zero) * SharpDX.Matrix3x2.Scaling((float)Math.Max(1.0f, outlineStroke.Width * .45) + 0.25f) * SharpDX.Matrix3x2.Translation(arrowPointVec); RenderTarget.Transform = transformMatrix2; RenderTarget.FillGeometry(arrowPathGeometry, tmpBrush); RenderTarget.Transform = SharpDX.Matrix3x2.Identity; } if (PathEnd == PathToolCapMode.Arrow) { Vector lineVector = points[points.Length - 1] - points[points.Length - 2]; lineVector.Normalize(); Point pointAdjusted = points[points.Length - 1] + pixelAdjustVec; SharpDX.Vector2 pointVec = pointAdjusted.ToVector2(); float vectorAngle = -(float)Math.Atan2(lineVector.X, lineVector.Y); Vector adjustVector = lineVector * 5; SharpDX.Vector2 arrowPointVec = new SharpDX.Vector2((float)(pointVec.X + adjustVector.X), (float)(pointVec.Y + adjustVector.Y)); SharpDX.Matrix3x2 transformMatrix2 = SharpDX.Matrix3x2.Rotation(vectorAngle, SharpDX.Vector2.Zero) * SharpDX.Matrix3x2.Scaling((float)Math.Max(1.0f, outlineStroke.Width * .45) + 0.25f) * SharpDX.Matrix3x2.Translation(arrowPointVec); RenderTarget.Transform = transformMatrix2; RenderTarget.FillGeometry(arrowPathGeometry, tmpBrush); RenderTarget.Transform = SharpDX.Matrix3x2.Identity; } } } if (ShowCount) { SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont(); SharpDX.DirectWrite.TextFormat textFormat = wpfFont.ToDirectWriteTextFormat(); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; textFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap; for (int i = 1; i < ChartAnchors.Count; i++) { Point p = ChartAnchors[i - 1].GetPoint(chartControl, chartPanel, chartScale); Point p1 = ChartAnchors[i].GetPoint(chartControl, chartPanel, chartScale); if (i + 1 < ChartAnchors.Count) { Point p2 = ChartAnchors[i + 1].GetPoint(chartControl, chartPanel, chartScale); Vector v1 = p - p1; v1.Normalize(); Vector v2 = p2 - p1; v2.Normalize(); Vector vector = v1 + v2; vector.Normalize(); SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, i.ToString(), textFormat, 250, textFormat.FontSize); Point textPoint = p1 - vector * textFormat.FontSize; textPoint.X -= textLayout.Metrics.Width / 2f; textPoint.Y -= textLayout.Metrics.Height / 2f; RenderTarget.DrawTextLayout((textPoint + pixelAdjustVec).ToVector2(), textLayout, outlineStroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); } else { SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, i.ToString(), textFormat, 250, textFormat.FontSize); Vector vector = (p - p1); vector.Normalize(); Point textPoint = p1 - vector * textFormat.FontSize; textPoint.X -= textLayout.Metrics.Width / 2f; textPoint.Y -= textLayout.Metrics.Height / 2f; RenderTarget.DrawTextLayout((textPoint + pixelAdjustVec).ToVector2(), textLayout, outlineStroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); } } textFormat.Dispose(); } }
private static void Main() { RenderForm renderForm = new RenderForm("SharpFontWrapper - Simple Text"); renderForm.Width = 1024; renderForm.Height = 768; viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f); device = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); deviceContext = device.ImmediateContext; using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1()) { SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription() { BufferCount = 2, Flags = SharpDX.DXGI.SwapChainFlags.None, IsWindowed = true, ModeDescription = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm), OutputHandle = renderForm.Handle, SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0), SwapEffect = SharpDX.DXGI.SwapEffect.Discard, Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput }; swapChain = new SwapChain(dxgiFactory, device, swapChainDesc); using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) { renderView = new RenderTargetView(device, backBuffer); } } fontFactory = new SharpFontWrapper.Factory(); fontWrapper = fontFactory.CreateFontWrapper(device, "Arial"); colorStyle = fontFactory.CreateColor(SharpDX.Color.Green); textFormat = new SharpDX.DirectWrite.TextFormat(fontWrapper.DWriteFactory, "Consolas", 32.0f); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center; textFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near; textLayout = new SharpDX.DirectWrite.TextLayout(fontWrapper.DWriteFactory, "DirectWrite Text Layout with formatting and color", textFormat, 1000.0f, 1000.0f); textLayout.SetFontStyle(SharpDX.DirectWrite.FontStyle.Italic, new SharpDX.DirectWrite.TextRange(0, 11)); textLayout.SetFontFamilyName("Arial", new SharpDX.DirectWrite.TextRange(24, 4)); textLayout.SetFontSize(72.0f, new SharpDX.DirectWrite.TextRange(29, 10)); //Stylistic typeography SharpDX.DirectWrite.Typography typoGraphy = new SharpDX.DirectWrite.Typography(fontWrapper.DWriteFactory); typoGraphy.AddFontFeature(new SharpDX.DirectWrite.FontFeature(SharpDX.DirectWrite.FontFeatureTag.StylisticSet7, 1)); textLayout.SetFontFamilyName("Gabriola", new SharpDX.DirectWrite.TextRange(12, 4)); textLayout.SetTypography(typoGraphy, new SharpDX.DirectWrite.TextRange(12, 4)); //Note : to pass color, make sure to use native pointer, as in that case we do not need sharpdx/.net to build com wrapper, colorStyle is already one textLayout.SetDrawingEffect(colorStyle.NativePointer, new SharpDX.DirectWrite.TextRange(44, 5)); RenderLoop.Run(renderForm, () => { float t = (float)watch.Elapsed.TotalMilliseconds; float x = ((float)Math.Sin(t * 0.002f) * 100.0f) + (renderForm.Width * 0.5f); deviceContext.OutputMerger.SetRenderTargets(renderView); deviceContext.Rasterizer.SetViewport(viewPort); deviceContext.ClearRenderTargetView(renderView, Color.Black); SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping | SharpFontWrapper.TextFlags.VerticalCenter | SharpFontWrapper.TextFlags.Center; fontWrapper.DrawString(deviceContext, "Hello SharpFontWrapper", 64.0f, new Vector2(x, renderForm.Height * 0.25f), Color.Yellow, flags); fontWrapper.DrawString(deviceContext, "This is another text", 64.0f, new Vector2(renderForm.Width * 0.5f, renderForm.Height * 0.25f + 100.0f), Color.White, flags); fontWrapper.DrawTextLayout(deviceContext, textLayout, new Vector2(0, renderForm.Height * 0.25f + 200.0f), Color.White, flags); swapChain.Present(1, SharpDX.DXGI.PresentFlags.None); }); typoGraphy.Dispose(); colorStyle.Dispose(); textLayout.Dispose(); textFormat.Dispose(); fontWrapper.Dispose(); fontFactory.Dispose(); deviceContext.ClearState(); deviceContext.Flush(); renderView.Dispose(); swapChain.Dispose(); deviceContext.Dispose(); device.Dispose(); }
public override void OnRender(ChartControl chartControl, ChartScale chartScale) { RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive; ChartPanel panel = chartControl.ChartPanels[PanelIndex]; Point anchorPoint = Anchor.GetPoint(chartControl, panel, chartScale); Point lastEndPoint = new Point(0, 0); SharpDX.Direct2D1.Brush lastBrush = null; foreach (GannAngle gannAngle in GannAngles.Where(ga => ga.IsVisible && ga.Stroke != null).OrderBy(ga => (ga.RatioX / ga.RatioY))) { gannAngle.Stroke.RenderTarget = RenderTarget; double dx = gannAngle.RatioX * chartControl.Properties.BarDistance; double dVal = gannAngle.RatioY * PointsPerBar; //NT7, just multiple directly this is price not pixels //chartScale.GetPixelsForDistance(gannAngle.RatioY * PointsPerBar); Vector gannDataVector = GetGannStepDataVector(dx, dVal); Point extendedEndPoint = CalculateExtendedDataPoint(panel, chartScale, Convert.ToInt32(anchorPoint.X), Anchor.Price, gannDataVector); // align to full pixel to avoid unneeded aliasing double strokePixAdj = ((double)(gannAngle.Stroke.Width % 2)).ApproxCompare(0) == 0 ? 0.5d : 0d; Vector pixelAdjustVec = new Vector(0, strokePixAdj); SharpDX.Direct2D1.Brush tmpBrush = IsInHitTest ? chartControl.SelectionBrush : gannAngle.Stroke.BrushDX; RenderTarget.DrawLine((anchorPoint + pixelAdjustVec).ToVector2(), (extendedEndPoint + pixelAdjustVec).ToVector2(), tmpBrush, gannAngle.Stroke.Width, gannAngle.Stroke.StrokeStyle); if (lastBrush != null) { float oldOpacity = lastBrush.Opacity; lastBrush.Opacity = PriceLevelOpacity / 100f; // create geometry SharpDX.Direct2D1.PathGeometry lineGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); SharpDX.Direct2D1.GeometrySink sink = lineGeometry.Open(); sink.BeginFigure(lastEndPoint.ToVector2(), SharpDX.Direct2D1.FigureBegin.Filled); // Does the fill color need to fill a corner? Check and add a point if (Math.Abs(lastEndPoint.Y - extendedEndPoint.Y) > 0.1 && Math.Abs(lastEndPoint.X - extendedEndPoint.X) > 0.1) { double boundaryX; double boundaryY; if (lastEndPoint.Y <= ChartPanel.Y || lastEndPoint.Y >= ChartPanel.Y + ChartPanel.H) { if (FanDirection == GannFanDirection.UpLeft || FanDirection == GannFanDirection.UpRight) { boundaryY = extendedEndPoint.Y; boundaryX = lastEndPoint.X; } else { boundaryY = lastEndPoint.Y; boundaryX = extendedEndPoint.X; } } else { if (FanDirection == GannFanDirection.UpLeft || FanDirection == GannFanDirection.UpRight) { boundaryY = lastEndPoint.Y; boundaryX = extendedEndPoint.X; } else { boundaryY = extendedEndPoint.Y; boundaryX = lastEndPoint.X; } } sink.AddLine(new SharpDX.Vector2((float)boundaryX, (float)boundaryY)); } sink.AddLine(extendedEndPoint.ToVector2()); sink.AddLine((anchorPoint + pixelAdjustVec).ToVector2()); sink.AddLine((lastEndPoint).ToVector2()); sink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed); sink.Close(); RenderTarget.FillGeometry(lineGeometry, lastBrush); lineGeometry.Dispose(); lastBrush.Opacity = oldOpacity; } lastEndPoint = extendedEndPoint + pixelAdjustVec; lastBrush = tmpBrush; } if (!IsTextDisplayed || IsInHitTest) { return; } foreach (GannAngle gannAngle in GannAngles.Where(ga => ga.IsVisible && ga.Stroke != null).OrderBy(ga => (ga.RatioX / ga.RatioY))) { gannAngle.Stroke.RenderTarget = RenderTarget; double dx = gannAngle.RatioX * chartControl.Properties.BarDistance; double dVal = gannAngle.RatioY * PointsPerBar; //NT7, just multiple directly this is price not pixels //chartScale.GetPixelsForDistance(gannAngle.RatioY * PointsPerBar); Vector gannDataVector = GetGannStepDataVector(dx, dVal); Point extendedEndPoint = CalculateExtendedDataPoint(panel, chartScale, Convert.ToInt32(anchorPoint.X), Anchor.Price, gannDataVector); if (!IsTextDisplayed || IsInHitTest) { continue; } SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont(); SharpDX.DirectWrite.TextFormat textFormat = wpfFont.ToDirectWriteTextFormat(); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; textFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap; SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, gannAngle.Name, textFormat, 100, textFormat.FontSize); // once text is laid out, update used width to calcuated space required float fontHeight = textLayout.Metrics.Height; Point textEndPoint = new Point(extendedEndPoint.X, extendedEndPoint.Y); if (textEndPoint.X > panel.X + panel.W - textLayout.Metrics.Width) { textEndPoint.X = panel.X + panel.W - textLayout.Metrics.Width; textEndPoint.Y += textLayout.Metrics.Width; } if (gannDataVector.Y > 0) { if (textEndPoint.Y < panel.Y + (fontHeight * 0.5)) { textEndPoint.Y = panel.Y + (fontHeight * 0.5); } } else { if (textEndPoint.Y > panel.Y + panel.H - (fontHeight * 1.5)) { textEndPoint.Y = panel.Y + panel.H - (fontHeight * 1.5); } } float?marginResource = Application.Current.FindResource("FontModalTitleMargin") as float?; float margin = 2f + (marginResource.HasValue ? marginResource.Value : 3f); // Allow for changes in X position based on whether text is aligned to left or right edge of screen float marginX = FanDirection == GannFanDirection.DownLeft || FanDirection == GannFanDirection.UpLeft ? margin : -2 * margin; SharpDX.Vector2 endVec = new SharpDX.Vector2((float)textEndPoint.X, (float)textEndPoint.Y); SharpDX.Matrix3x2 transformMatrix = SharpDX.Matrix3x2.Translation(endVec); RenderTarget.Transform = transformMatrix; RenderTarget.DrawTextLayout(new SharpDX.Vector2(marginX + margin, margin), textLayout, gannAngle.Stroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); RenderTarget.Transform = SharpDX.Matrix3x2.Identity; textFormat.Dispose(); textLayout.Dispose(); } }
public override void Load( CanvasRenderArguments args ) { if ( lineBrush != null && !lineBrush.IsDisposed ) lineBrush.Dispose(); if (DW_textFormat != null && !DW_textFormat.IsDisposed) DW_textFormat.Dispose(); // init the lines brushs lineBrush = new SolidColorBrush( args.renderTarget, _FontColor ); _TextFormat.fontCollection = args.WriteFactory.GetSystemFontCollection(false); // init the text format DW_textFormat = new SharpDX.DirectWrite.TextFormat( args.WriteFactory, _TextFormat.familyName, _TextFormat.fontCollection, _TextFormat.weight, _TextFormat.fontStyle, _TextFormat.fontStretch, _TextFormat.fontSize, "en-us" ); // get the size of the string SharpDX.DirectWrite.TextLayout textL= new SharpDX.DirectWrite.TextLayout( args.WriteFactory, Internal_String, DW_textFormat, 1500, 1500 ); Size = new Vector.FxVector2f(textL.GetFontSize(0) * Internal_String.Length, textL.GetFontSize(0)); textL.Dispose(); // init text rectangle textRectangle = new RectangleF( 0, 0, Size.x, Size.y ); }
public override void Load( CanvasRenderArguments args ) { // dispose the old brush if (lineBrush != null && !lineBrush.IsDisposed) lineBrush.Dispose(); if (DW_textFormat != null && !DW_textFormat.IsDisposed) DW_textFormat.Dispose(); // init the lines brushs lineBrush = new SolidColorBrush( args.renderTarget, _AxesColor.ToColor4() ); _TextFormat.fontCollection = args.WriteFactory.GetSystemFontCollection(false); // init the text format DW_textFormat = new SharpDX.DirectWrite.TextFormat(args.WriteFactory, _TextFormat.familyName, _TextFormat.fontCollection, _TextFormat.weight, _TextFormat.fontStyle, _TextFormat.fontStretch, _TextFormat.fontSize, "en-us"); // get the size of the string SharpDX.DirectWrite.TextLayout textL = new SharpDX.DirectWrite.TextLayout(args.WriteFactory, "(0,0)", DW_textFormat, 1500, 1500); // init text rectangle textRectangle = new RectangleF(0, 0, textL.GetFontSize(0) * 10, textL.GetFontSize(0)); textL.Dispose(); // refresh the geometrys RefreshGeometry( args.renderTarget ); }