public D2D.GeometryRealization CreateSymbol(ShowSymbol sym, DW.TextFormat format) { D2D.GeometryRealization cached_geo = null; bool result = symbol_cache.TryGetValue(sym, out cached_geo); if (!result) { const int margin = 2; D2D.Geometry geo = null; DW.TextLayout layout = null; D2D.PathGeometry path = null; DW.TextMetrics metrics; D2D.StrokeStyle stroke = null; switch (sym) { case ShowSymbol.FullSpace: layout = new DW.TextLayout(this._DWFactory, " ", format, float.MaxValue, float.MaxValue); metrics = layout.Metrics; Rectangle rect = new Rectangle(margin, margin, Math.Max(1, metrics.WidthIncludingTrailingWhitespace - margin * 2), Math.Max(1, metrics.Height - margin * 2)); geo = new D2D.RectangleGeometry(this.Factory, rect); stroke = this.GetStroke(HilightType.Dash); break; case ShowSymbol.HalfSpace: layout = new DW.TextLayout(this._DWFactory, " ", format, float.MaxValue, float.MaxValue); metrics = layout.Metrics; rect = new Rectangle(margin, margin, Math.Max(1, metrics.WidthIncludingTrailingWhitespace - margin * 2), Math.Max(1, metrics.Height - margin * 2)); geo = new D2D.RectangleGeometry(this.Factory, rect); stroke = this.GetStroke(HilightType.Sold); break; case ShowSymbol.Tab: layout = new DW.TextLayout(this._DWFactory, "0", format, float.MaxValue, float.MaxValue); metrics = layout.Metrics; path = new D2D.PathGeometry(this.Factory); var sink = path.Open(); sink.BeginFigure(new SharpDX.Mathematics.Interop.RawVector2(1, 1), D2D.FigureBegin.Filled); //少し隙間を開けないと描写されない sink.AddLine(new SharpDX.Mathematics.Interop.RawVector2((float)1, (float)metrics.Height)); sink.EndFigure(D2D.FigureEnd.Closed); sink.Close(); geo = path; stroke = this.GetStroke(HilightType.Sold); break; } cached_geo = new D2D.GeometryRealization(this.Device, geo, 1.0f, 1.0f, stroke); this.symbol_cache.Add(sym, cached_geo); } return(cached_geo); }
/// <summary> /// Draws a bitmap image. /// </summary> /// <param name="source">The bitmap image.</param> /// <param name="opacityMask">The opacity mask to draw with.</param> /// <param name="opacityMaskRect">The destination rect for the opacity mask.</param> /// <param name="destRect">The rect in the output to draw to.</param> public void DrawImage(IBitmapImpl source, IBrush opacityMask, Rect opacityMaskRect, Rect destRect) { using (var d2dSource = ((BitmapImpl)source).GetDirect2DBitmap(_renderTarget)) using (var sourceBrush = new BitmapBrush(_renderTarget, d2dSource)) using (var d2dOpacityMask = CreateBrush(opacityMask, opacityMaskRect.Size)) using (var geometry = new SharpDX.Direct2D1.RectangleGeometry(_renderTarget.Factory, destRect.ToDirect2D())) { d2dOpacityMask.PlatformBrush.Transform = Matrix.CreateTranslation(opacityMaskRect.Position).ToDirect2D(); _renderTarget.FillGeometry( geometry, sourceBrush, d2dOpacityMask.PlatformBrush); } }
/// <summary> /// Draws a bitmap image. /// </summary> /// <param name="source">The bitmap image.</param> /// <param name="opacityMask">The opacity mask to draw with.</param> /// <param name="opacityMaskRect">The destination rect for the opacity mask.</param> /// <param name="destRect">The rect in the output to draw to.</param> public void DrawImage(IRef <IBitmapImpl> source, IBrush opacityMask, Rect opacityMaskRect, Rect destRect) { using (var d2dSource = ((BitmapImpl)source.Item).GetDirect2DBitmap(_deviceContext)) using (var sourceBrush = new BitmapBrush(_deviceContext, d2dSource.Value)) using (var d2dOpacityMask = CreateBrush(opacityMask, opacityMaskRect.Size)) using (var geometry = new SharpDX.Direct2D1.RectangleGeometry(_deviceContext.Factory, destRect.ToDirect2D())) { if (d2dOpacityMask.PlatformBrush != null) { d2dOpacityMask.PlatformBrush.Transform = Matrix.CreateTranslation(opacityMaskRect.Position).ToDirect2D(); } _deviceContext.FillGeometry( geometry, sourceBrush, d2dOpacityMask.PlatformBrush); } }
public static Geometry MarkerGeometry(MarkersType markersType, Factory factory, float width, float height) { Geometry geometry = null; switch (markersType) { case MarkersType.None: break; case MarkersType.Square: geometry = new RectangleGeometry(factory, new SharpDX.RectangleF() { X = 0, Y = 0, Width = width, Height = height }); break; case MarkersType.Circle: geometry = new EllipseGeometry(factory, new Ellipse() { Point = new Vector2(0, 0), RadiusX = width / 2, RadiusY = height / 2, }); break; default: GenericMarker markerSpecification = MarkerGeometries.GenericMarkerLookup[markersType]; geometry = new PathGeometry(factory); using (GeometrySink sink = (geometry as PathGeometry).Open()) { Vector2 p0 = new Vector2((float)markerSpecification.X[0] * width, (float)markerSpecification.Y[0] * height); sink.BeginFigure(p0, FigureBegin.Hollow); int n = markerSpecification.X.Length; for (int i = 1; i < n; ++i) { sink.AddLine(new Vector2((float)markerSpecification.X[i] * width, (float)markerSpecification.Y[i] * height)); } sink.EndFigure(FigureEnd.Closed); sink.Close(); } break; } return geometry; }
/// <summary> /// 判断当前拼图碎片与指定矩形是否相交。 /// </summary> /// <param name="rect">要判断的矩形。</param> /// <returns>是否相交。</returns> public bool TestHit(RectangleF rect) { if (this.bounds.Right < rect.X) { return false; } if (this.bounds.X > rect.Right) { return false; } if (this.bounds.Bottom < rect.Y) { return false; } if (this.bounds.Y > rect.Bottom) { return false; } RectangleGeometry rectGeom = new RectangleGeometry(this.factory, rect); return this.path.Compare(rectGeom) != GeometryRelation.Disjoint; }
public void Rectangle(Rectangle rectangle) { endOpenFigure(); using (var geometry = new RectangleGeometry(_factory, Import.Rectangle(rectangle))) { geometry.Simplify(SimplificationOption, _sink); } }
public void Clear(SolidBrush brush) { if (Control != null) { var color = brush != null ? brush.Color : Colors.Transparent; // drawing to an image, so we can clear to transparent if (image != null) { if (clipParams != null) { // can't clear the current layer otherwise it will not be applied to main layer // This creates a copy of the current context, inverses the current clip, and draws the image back clipping // the cleared path. // end clip layer and current drawing session Control.PopLayer(); Control.EndDraw(); // create a copy of the current state var copy = image.Clone(); var bmp = copy.ToDx(Control); Control.BeginDraw(); // clear existing contents Control.Clear(null); var size = Control.Size; // create an inverse geometry var inverse = new sd.PathGeometry(SDFactory.D2D1Factory); var sink = inverse.Open(); var bounds = new s.RectangleF(0, 0, size.Width, size.Height); var geom = new sd.RectangleGeometry(SDFactory.D2D1Factory, bounds); geom.Combine(clipGeometry, sd.CombineMode.Exclude, sink); sink.Close(); // create a new mask layer with inverse geometry var parameters = new sd.LayerParameters { ContentBounds = bounds, GeometricMask = inverse, MaskAntialiasMode = Control.AntialiasMode, MaskTransform = s.Matrix3x2.Identity, Opacity = 1f }; Control.PushLayer(ref parameters, HelperLayer); // draw bitmap of contents back, clipping to the inverse of the clip region Control.DrawBitmap(bmp, 1f, sd.BitmapInterpolationMode.NearestNeighbor); Control.PopLayer(); // restore our clip path parameters = clipParams.Value; Control.PushLayer(ref parameters, HelperLayer); copy.Dispose(); } } else { // alpha is not supported on a drawable, so blend with black as the base color. color = Color.Blend(Colors.Black, color); } Control.Clear(color.ToDx()); } }
public Result DrawStrikethrough(object clientDrawingContext, float baselineOriginX, float baselineOriginY, ref Strikethrough strikethrough, ComObject clientDrawingEffect) { var rect = new SharpDX.RectangleF(0, strikethrough.Offset, strikethrough.Width, strikethrough.Offset + strikethrough.Thickness); var rectangleGeometry = new RectangleGeometry(_d2DFactory, rect); var matrix = new Matrix3x2() { M11 = 1, M12 = 0, M21 = 0, M22 = 1, M31 = baselineOriginX, M32 = baselineOriginY }; var transformedGeometry = new TransformedGeometry(_d2DFactory, rectangleGeometry, matrix); var brushColor = (Color4)Color.Black; if (clientDrawingEffect != null && clientDrawingEffect is ColorDrawingEffect) brushColor = (Color4)(clientDrawingEffect as ColorDrawingEffect).Color; var brush = new SolidColorBrush(_renderTarget, brushColor); _renderTarget.DrawGeometry(transformedGeometry, brush); _renderTarget.FillGeometry(transformedGeometry, brush); rectangleGeometry.Dispose(); transformedGeometry.Dispose(); brush.Dispose(); return Result.Ok; }
public Result DrawUnderline(object clientDrawingContext, float baselineOriginX, float baselineOriginY, ref Underline underline, ComObject clientDrawingEffect) { var rect = new SharpDX.RectangleF(0, underline.Offset, underline.Width, underline.Offset + underline.Thickness); var rectangleGeometry = new RectangleGeometry(_d2DFactory, rect); var matrix = new Matrix3x2() { M11 = 1, M12 = 0, M21 = 0, M22 = 1, M31 = baselineOriginX, M32 = baselineOriginY }; var transformedGeometry = new TransformedGeometry(_d2DFactory, rectangleGeometry, matrix); var brushColor = new Color4(1, 0, 0, 0); if (clientDrawingEffect != null && clientDrawingEffect is ColorDrawingEffect) brushColor = (clientDrawingEffect as ColorDrawingEffect).Color; var brush = new SolidColorBrush(_renderTarget, brushColor); _renderTarget.DrawGeometry(transformedGeometry, brush); _renderTarget.FillGeometry(transformedGeometry, brush); rectangleGeometry.Dispose(); transformedGeometry.Dispose(); brush.Dispose(); return SharpDX.Result.Ok; }