public static void Draw(SKCanvas skCanvas, SvgFragment svgFragment) { var size = SvgModelExtensions.GetDimensions(svgFragment); var bounds = Rect.Create(size); var drawable = DrawableFactory.Create(svgFragment, bounds, null, _assetLoader); if (drawable is { })
public static FragmentDrawable Create(SvgFragment svgFragment, SKRect skViewport, DrawableBase?parent, IAssetLoader assetLoader, HashSet <Uri>?references, DrawAttributes ignoreAttributes = DrawAttributes.None) { var drawable = new FragmentDrawable(assetLoader, references) { Element = svgFragment, Parent = parent, IgnoreAttributes = ignoreAttributes }; drawable.IsDrawable = drawable.HasFeatures(svgFragment, drawable.IgnoreAttributes); if (!drawable.IsDrawable) { return(drawable); } var svgFragmentParent = svgFragment.Parent; var x = svgFragmentParent is null ? 0f : svgFragment.X.ToDeviceValue(UnitRenderingType.Horizontal, svgFragment, skViewport); var y = svgFragmentParent is null ? 0f : svgFragment.Y.ToDeviceValue(UnitRenderingType.Vertical, svgFragment, skViewport); var skSize = SvgExtensions.GetDimensions(svgFragment); if (skViewport.IsEmpty) { skViewport = SKRect.Create(x, y, skSize.Width, skSize.Height); } drawable.CreateChildren(svgFragment, skViewport, drawable, assetLoader, references, ignoreAttributes); drawable.Initialize(skViewport, x, y, skSize); return(drawable); }
public static SKPicture?ToPicture(SvgFragment svgFragment) { var skSize = SvgExtensions.GetDimensions(svgFragment); var skBounds = SKRect.Create(skSize); using var drawable = DrawableFactory.Create(svgFragment, skBounds, null, null, Attributes.None); if (drawable == null) { return(null); } drawable.PostProcess(); if (skBounds.IsEmpty) { skBounds = GetBounds(drawable); } using var skPictureRecorder = new SKPictureRecorder(); using var skCanvas = skPictureRecorder.BeginRecording(skBounds); #if USE_EXPERIMENTAL_LINEAR_RGB // TODO: using var skPaint = new SKPaint(); using var skColorFilter = SKColorFilter.CreateTable(null, SvgPaintingExtensions.s_LinearRGBtoSRGB, SvgPaintingExtensions.s_LinearRGBtoSRGB, SvgPaintingExtensions.s_LinearRGBtoSRGB); using var skImageFilter = SKImageFilter.CreateColorFilter(skColorFilter); skPaint.ImageFilter = skImageFilter; skCanvas.SaveLayer(skPaint); #endif drawable?.Draw(skCanvas, 0f, 0f); #if USE_EXPERIMENTAL_LINEAR_RGB // TODO: skCanvas.Restore(); #endif return(skPictureRecorder.EndRecording()); }
public SvgElement GetPresentation(double width, double height) { if (LayerClass.IconSvg == null) { return(null); } SvgFragment copy = (SvgFragment)LayerClass.IconSvg.DeepCopy(); float imageAspectRatio = copy.Bounds.Width / copy.Bounds.Height; float restrictionAspectRation = (float)(width / height); copy.ViewBox = copy.Bounds; if (imageAspectRatio > restrictionAspectRation) { //image shape is wider then restriction area. //thus width is effective restriction copy.Width = (float)width; copy.Height = (float)(width / imageAspectRatio); } else { //hight is effective restiction copy.Width = (float)(height * imageAspectRatio); copy.Height = (float)(height); } return(copy); }
public static void Draw(SKCanvas skCanvas, SvgFragment svgFragment) { var skSize = SvgExtensions.GetDimensions(svgFragment); var skBounds = SKRect.Create(skSize); using var drawable = DrawableFactory.Create(svgFragment, skBounds, null, null, Attributes.None); drawable?.PostProcess(); drawable?.Draw(skCanvas, 0f, 0f); }
public static Drawable?ToDrawable(SvgFragment svgFragment) { var skSize = SvgExtensions.GetDimensions(svgFragment); var skBounds = SKRect.Create(skSize); var drawable = DrawableFactory.Create(svgFragment, skBounds, null, null, Attributes.None); drawable?.PostProcess(); return(drawable); }
public static void Draw(SkiaSharp.SKCanvas skCanvas, SvgFragment svgFragment) { var references = new HashSet <Uri> { svgFragment.OwnerDocument.BaseUri }; var size = SvgExtensions.GetDimensions(svgFragment); var bounds = SKRect.Create(size); var drawable = DrawableFactory.Create(svgFragment, bounds, null, s_assetLoader, references); if (drawable is { })
internal SKPicture Load(SvgFragment svgFragment) { var skSize = SkiaUtil.GetDimensions(svgFragment); var cullRect = SKRect.Create(skSize); using (var skPictureRecorder = new SKPictureRecorder()) using (var skCanvas = skPictureRecorder.BeginRecording(cullRect)) using (var renderer = new SKSvgRenderer(skCanvas, skSize)) { renderer.DrawFragment(svgFragment, false); return(skPictureRecorder.EndRecording()); } }
public Fragment(SvgFragment svgFragment) { x = svgFragment.X.ToDeviceValue(null, UnitRenderingType.Horizontal, svgFragment); y = svgFragment.Y.ToDeviceValue(null, UnitRenderingType.Vertical, svgFragment); width = svgFragment.Width.ToDeviceValue(null, UnitRenderingType.Horizontal, svgFragment); height = svgFragment.Height.ToDeviceValue(null, UnitRenderingType.Vertical, svgFragment); bounds = SKRect.Create(x, y, width, height); matrix = SvgHelper.GetSKMatrix(svgFragment.Transforms); var viewBoxMatrix = SvgHelper.GetSvgViewBoxTransform(svgFragment.ViewBox, svgFragment.AspectRatio, x, y, width, height); SKMatrix.Concat(ref matrix, ref matrix, ref viewBoxMatrix); }
internal SKPicture Load(SvgFragment svgFragment) { using (var disposable = new CompositeDisposable()) { float width = svgFragment.Width.ToDeviceValue(null, UnitRenderingType.Horizontal, svgFragment); float height = svgFragment.Height.ToDeviceValue(null, UnitRenderingType.Vertical, svgFragment); var skSize = new SKSize(width, height); var cullRect = SKRect.Create(skSize); using (var skPictureRecorder = new SKPictureRecorder()) using (var skCanvas = skPictureRecorder.BeginRecording(cullRect)) { SvgHelper.DrawSvgElement(skCanvas, skSize, svgFragment, disposable); return(skPictureRecorder.EndRecording()); } } }
public void TestGetAttribute() { var owner = new SvgCircle(); var parent = new SvgFragment(); parent.Children.Add(owner); parent.Attributes["test"] = "parent"; owner.Attributes["test"] = "owner"; Assert.AreEqual("owner", owner.Attributes.GetAttribute <string>("test")); owner.Attributes["test"] = "inherit"; Assert.AreEqual("inherit", owner.Attributes.GetAttribute <string>("test")); owner.Attributes.Remove("test"); Assert.IsNull(owner.Attributes.GetAttribute <string>("test")); }
public static DrawableBase?Create(SvgElement svgElement, Rect skOwnerBounds, DrawableBase?parent, IAssetLoader assetLoader, Attributes ignoreAttributes = Attributes.None) { return(svgElement switch { SvgAnchor svgAnchor => AnchorDrawable.Create(svgAnchor, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgFragment svgFragment => FragmentDrawable.Create(svgFragment, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgImage svgImage => ImageDrawable.Create(svgImage, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgSwitch svgSwitch => SwitchDrawable.Create(svgSwitch, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgUse svgUse => UseDrawable.Create(svgUse, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgCircle svgCircle => CircleDrawable.Create(svgCircle, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgEllipse svgEllipse => EllipseDrawable.Create(svgEllipse, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgRectangle svgRectangle => RectangleDrawable.Create(svgRectangle, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgGroup svgGroup => GroupDrawable.Create(svgGroup, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgLine svgLine => LineDrawable.Create(svgLine, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgPath svgPath => PathDrawable.Create(svgPath, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgPolyline svgPolyline => PolylineDrawable.Create(svgPolyline, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgPolygon svgPolygon => PolygonDrawable.Create(svgPolygon, skOwnerBounds, parent, assetLoader, ignoreAttributes), SvgText svgText => TextDrawable.Create(svgText, skOwnerBounds, parent, assetLoader, ignoreAttributes), _ => null, });
public static Drawable?Create(SvgElement svgElement, SKRect skOwnerBounds, Drawable?root, Drawable?parent, Attributes ignoreAttributes = Attributes.None) { return(svgElement switch { SvgAnchor svgAnchor => new AnchorDrawable(svgAnchor, skOwnerBounds, root, parent, ignoreAttributes), SvgFragment svgFragment => new FragmentDrawable(svgFragment, skOwnerBounds, root, parent, ignoreAttributes), SvgImage svgImage => new ImageDrawable(svgImage, skOwnerBounds, root, parent, ignoreAttributes), SvgSwitch svgSwitch => new SwitchDrawable(svgSwitch, skOwnerBounds, root, parent, ignoreAttributes), SvgUse svgUse => new UseDrawable(svgUse, skOwnerBounds, root, parent, ignoreAttributes), SvgCircle svgCircle => new CircleDrawable(svgCircle, skOwnerBounds, root, parent, ignoreAttributes), SvgEllipse svgEllipse => new EllipseDrawable(svgEllipse, skOwnerBounds, root, parent, ignoreAttributes), SvgRectangle svgRectangle => new RectangleDrawable(svgRectangle, skOwnerBounds, root, parent, ignoreAttributes), SvgGroup svgGroup => new GroupDrawable(svgGroup, skOwnerBounds, root, parent, ignoreAttributes), SvgLine svgLine => new LineDrawable(svgLine, skOwnerBounds, root, parent, ignoreAttributes), SvgPath svgPath => new PathDrawable(svgPath, skOwnerBounds, root, parent, ignoreAttributes), SvgPolyline svgPolyline => new PolylineDrawable(svgPolyline, skOwnerBounds, root, parent, ignoreAttributes), SvgPolygon svgPolygon => new PolygonDrawable(svgPolygon, skOwnerBounds, root, parent, ignoreAttributes), SvgText svgText => new TextDrawable(svgText, skOwnerBounds, root, parent, ignoreAttributes), _ => null, });
public void TestGetInheritedAttribute() { var owner = new SvgCircle(); var parent = new SvgFragment(); parent.Children.Add(owner); parent.Attributes["test"] = "parent"; owner.Attributes["test"] = "owner"; Assert.AreEqual("owner", owner.Attributes.GetInheritedAttribute("test", true, "default")); Assert.AreEqual("owner", owner.Attributes.GetInheritedAttribute("test", false, "default")); owner.Attributes["test"] = "inherit"; Assert.AreEqual("parent", owner.Attributes.GetInheritedAttribute("test", true, "default")); Assert.AreEqual("parent", owner.Attributes.GetInheritedAttribute("test", false, "default")); owner.Attributes.Remove("test"); Assert.AreEqual("parent", owner.Attributes.GetInheritedAttribute("test", true, "default")); Assert.AreEqual("default", owner.Attributes.GetInheritedAttribute("test", false, "default")); }
public static SKSize GetDimensions(SvgFragment svgFragment) { float w, h; var isWidthperc = svgFragment.Width.Type == SvgUnitType.Percentage; var isHeightperc = svgFragment.Height.Type == SvgUnitType.Percentage; var bounds = new SKRect(); if (isWidthperc || isHeightperc) { if (svgFragment.ViewBox.Width > 0 && svgFragment.ViewBox.Height > 0) { bounds = new SKRect(svgFragment.ViewBox.MinX, svgFragment.ViewBox.MinY, svgFragment.ViewBox.Width, svgFragment.ViewBox.Height); } else { // TODO: Calculate correct bounds using Children bounds. } } if (isWidthperc) { w = (bounds.Width + bounds.Left) * (svgFragment.Width.Value * 0.01f); } else { // NOTE: Pass bounds as SKRect.Empty because percentage case is handled before. w = svgFragment.Width.ToDeviceValue(UnitRenderingType.Horizontal, svgFragment, SKRect.Empty); } if (isHeightperc) { h = (bounds.Height + bounds.Top) * (svgFragment.Height.Value * 0.01f); } else { // NOTE: Pass bounds as SKRect.Empty because percentage case is handled before. h = svgFragment.Height.ToDeviceValue(UnitRenderingType.Vertical, svgFragment, SKRect.Empty); } return(new SKSize(w, h)); }
// TODO: //public void DrawAnchor(SvgAnchor svgAnchor, bool ignoreDisplay) //{ // _skCanvas.Save(); // // var skMatrix = SkiaUtil.GetSKMatrix(svgAnchor.Transforms); // SetTransform(skMatrix); // // var skPaintOpacity = SetOpacity(svgAnchor, _disposable); // // foreach (var svgElement in svgAnchor.Children) // { // Draw(svgElement, ignoreDisplay); // } // // if (skPaintOpacity != null) // { // _skCanvas.Restore(); // } // // _skCanvas.Restore(); //} public void DrawFragment(SvgFragment svgFragment, bool ignoreDisplay) { float x = svgFragment.X.ToDeviceValue(null, UnitRenderingType.Horizontal, svgFragment); float y = svgFragment.Y.ToDeviceValue(null, UnitRenderingType.Vertical, svgFragment); var skSize = SkiaUtil.GetDimensions(svgFragment); _skCanvas.Save(); switch (svgFragment.Overflow) { case SvgOverflow.Auto: case SvgOverflow.Visible: case SvgOverflow.Inherit: break; default: var skClipRect = SKRect.Create(x, y, skSize.Width, skSize.Height); _skCanvas.ClipRect(skClipRect, SKClipOperation.Intersect); break; } var skMatrixViewBox = SkiaUtil.GetSvgViewBoxTransform(svgFragment.ViewBox, svgFragment.AspectRatio, x, y, skSize.Width, skSize.Height); var skMatrix = SkiaUtil.GetSKMatrix(svgFragment.Transforms); SKMatrix.PreConcat(ref skMatrix, ref skMatrixViewBox); SetTransform(skMatrix); var skPaintOpacity = SetOpacity(svgFragment, _disposable); foreach (var svgElement in svgFragment.Children) { Draw(svgElement, ignoreDisplay); } if (skPaintOpacity != null) { _skCanvas.Restore(); } _skCanvas.Restore(); }
public static SKPicture?ToPicture(SvgFragment svgFragment, out Drawable?drawable) { var skSize = SvgExtensions.GetDimensions(svgFragment); var skBounds = SKRect.Create(skSize); drawable = DrawableFactory.Create(svgFragment, skBounds, null, null, Attributes.None); if (drawable == null) { return(null); } drawable.PostProcess(); if (skBounds.IsEmpty) { skBounds = GetBounds(drawable); } using var skPictureRecorder = new SKPictureRecorder(); using var skCanvas = skPictureRecorder.BeginRecording(skBounds); drawable?.Draw(skCanvas, 0f, 0f); return(skPictureRecorder.EndRecording()); }
public static SP.Picture?ToModel(SvgFragment svgFragment) { var size = SvgExtensions.GetDimensions(svgFragment); var bounds = SP.Rect.Create(size); using var drawable = DrawableFactory.Create(svgFragment, bounds, null, Attributes.None); if (drawable == null) { return(null); } drawable.PostProcess(); if (bounds.IsEmpty) { var drawableBounds = drawable.Bounds; bounds = SP.Rect.Create( 0f, 0f, Math.Abs(drawableBounds.Left) + drawableBounds.Width, Math.Abs(drawableBounds.Top) + drawableBounds.Height); } return(drawable.Snapshot(bounds)); }
public static SvgDocument Generate(ISvgRenderableColumn[] columns, ILegendGroup[] legend) { //generating headers SvgGroup headerGroup = new SvgGroup(); SvgPaintServer blackPaint = new SvgColourServer(System.Drawing.Color.Black); double horizontalOffset = 0.0; double headerHeight = 0; for (int i = 0; i < columns.Length; i++) { RenderedSvg heading = columns[i].RenderHeader(); SvgRectangle rect = new SvgRectangle(); headerHeight = heading.RenderedSize.Height; rect.Width = Helpers.dtos(heading.RenderedSize.Width); rect.Height = Helpers.dtos(heading.RenderedSize.Height); rect.X = Helpers.dtos(horizontalOffset); rect.Y = Helpers.dtos(0.0); rect.Stroke = blackPaint; heading.SVG.Transforms.Add(new SvgTranslate((float)(horizontalOffset + heading.RenderedSize.Width * 0.5), (float)heading.RenderedSize.Height * 0.9f)); heading.SVG.Transforms.Add(new SvgRotate((float)-90.0)); headerGroup.Children.Add(rect); headerGroup.Children.Add(heading.SVG); horizontalOffset += heading.RenderedSize.Width; } //generating columns SvgGroup columnsGroup = new SvgGroup(); double columnHeight = 0.0; horizontalOffset = 0.0; columnsGroup.Transforms.Add(new SvgTranslate(0.0f, (float)headerHeight)); for (int i = 0; i < columns.Length; i++) { RenderedSvg column = columns[i].RenderColumn(); SvgRectangle rect = new SvgRectangle(); columnHeight = column.RenderedSize.Height; rect.Width = Helpers.dtos(column.RenderedSize.Width); rect.Height = Helpers.dtos(column.RenderedSize.Height); rect.X = Helpers.dtos(horizontalOffset); rect.Y = Helpers.dtos(0.0); rect.Stroke = blackPaint; column.SVG.Transforms.Add(new SvgTranslate((float)(horizontalOffset))); columnsGroup.Children.Add(rect); columnsGroup.Children.Add(column.SVG); horizontalOffset += column.RenderedSize.Width; } //generating legend group SvgGroup legendGroup = new SvgGroup(); const float legendYGap = 30.0f; const float legendXOffset = 10.0f; legendGroup.Transforms.Add(new SvgTranslate(legendXOffset, Helpers.dtos(headerHeight + columnHeight + legendYGap))); const float titleYoffset = 60.0f; const float itemsYoffset = 20.0f; const float itemYgap = 20.0f; const float interGroupYgap = 15.0f; const float itemImageWidth = 64.0f; const float itemImageHeight = 32.0f; const float descrXoffset = 150.0f; SvgText legendTitle = new SvgText("Условные обозначения"); legendTitle.FontSize = 22; legendTitle.Fill = new SvgColourServer(System.Drawing.Color.Black); legendTitle.Transforms.Add(new SvgTranslate(30.0f, Helpers.dtos(titleYoffset * 0.25f))); legendGroup.Children.Add(legendTitle); float currGroupOffset = 0.0f; int k = 0; foreach (ILegendGroup group in legend) { //title SvgText groupTitle = new SvgText(group.GroupName); groupTitle.FontSize = new SvgUnit((float)18); groupTitle.Fill = new SvgColourServer(System.Drawing.Color.Black); groupTitle.Transforms.Add(new SvgTranslate(0.0f, currGroupOffset + titleYoffset)); legendGroup.Children.Add(groupTitle); //items var items = group.Items; int j = 0; SvgElement[] fragments = items.Select(item => item.GetPresentation(itemImageWidth, itemImageHeight)).ToArray(); foreach (var item in items) { if (fragments[j] == null) { continue; } float yOffset = currGroupOffset + titleYoffset + itemsYoffset + j * (itemYgap + itemImageHeight); if (fragments[j] is SvgFragment) { SvgFragment fragment = (SvgFragment)fragments[j]; fragment.X = 0; fragment.Y = yOffset; } else { fragments[j].Transforms.Add(new SvgTranslate(0, yOffset)); } legendGroup.Children.Add(fragments[j]); SvgText text = new SvgText(item.Description); text.FontSize = new SvgUnit((float)14); text.Fill = new SvgColourServer(System.Drawing.Color.Black); text.Transforms.Add(new SvgTranslate(descrXoffset, yOffset + itemImageHeight * 0.5f)); legendGroup.Children.Add(text); j++; } currGroupOffset += titleYoffset + itemsYoffset + (itemYgap + itemImageHeight) * items.Length + interGroupYgap; k++; } //gathering definitions SvgDefinitionList allDefs = new SvgDefinitionList(); for (int i = 0; i < columns.Length; i++) { SvgDefinitionList defs = columns[i].Definitions; foreach (SvgPatternServer def in defs.Children) { //overridings tile size allDefs.Children.Add(def); } } SvgDocument result = new SvgDocument(); result.Children.Add(allDefs); result.Width = Helpers.dtos(horizontalOffset); result.Height = Helpers.dtos((headerHeight + columnHeight + legendYGap + currGroupOffset)); result.Fill = new SvgColourServer(System.Drawing.Color.White); result.Children.Add(headerGroup); result.Children.Add(columnsGroup); result.Children.Add(legendGroup); return(result); }
public static SKPicture?ToPicture(SvgFragment svgFragment) { var picture = SvgModelExtensions.ToModel(svgFragment, _assetLoader); return(picture?.ToSKPicture()); }
public SvgElement Paint(LayerVM vm, double availableWidth, double availableHeight) { if (vm is ClassificationLayerTextPresentingVM) { ClassificationLayerTextPresentingVM cltpVM = (ClassificationLayerTextPresentingVM)vm; if (cltpVM.Text != null) { string[] spans = cltpVM.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(); SvgText text = new SvgText(); text.Transforms.Add(new Svg.Transforms.SvgTranslate((float)textXoffset, (float)(availableHeight * 0.5 + textYoffset))); foreach (var span in spans) { SvgTextSpan tspan = new SvgTextSpan(); tspan.Text = span; tspan.FontSize = Helpers.dtos(fontSize); tspan.Dy.Add(Helpers.dtos(fontSize * 1.2)); tspan.X.Add(0); tspan.Fill = blackPaint; text.Children.Add(tspan); } return(text); } else { return(new SvgGroup()); } } else if (vm is LengthLayerVM) { LengthLayerVM lrlcVM = (LengthLayerVM)vm; SvgText text = new SvgText(string.Format("{0:0.##} м", lrlcVM.RealLength)); text.FontSize = Helpers.dtos(fontSize); text.Fill = blackPaint; text.Transforms.Add(new Svg.Transforms.SvgTranslate((float)textXoffset, (float)(availableHeight * 0.5 + textYoffset))); return(text); } else if (vm is MultiClassificationLayerIconPresentingVM) { MultiClassificationLayerIconPresentingVM mclipVM = (MultiClassificationLayerIconPresentingVM)vm; SvgGroup group = new SvgGroup(); float iconWidth = 32.0f; if (mclipVM.IconsSVG != null) { SvgFragment[] fragments = mclipVM.IconsSVG.Where(f => f != null).ToArray(); if (fragments.Length == 0) { return(group); } float maxHeight = fragments.Select(f => f.Bounds.Height).Max(); float offset = (float)((availableWidth - fragments.Length * iconWidth) * 0.5); foreach (var item in fragments) { SvgFragment copy = (SvgFragment)item.DeepCopy(); float aspectRatio = copy.Bounds.Width / copy.Bounds.Height; copy.ViewBox = copy.Bounds; copy.X += offset; copy.Y = (float)(availableHeight - maxHeight) * 0.5f; //copy.Transforms.Add(new Svg.Transforms.SvgScale(ratio)); copy.Width = iconWidth; copy.Height = iconWidth / aspectRatio; group.Children.Add(copy); offset += iconWidth; } } return(group); } else { return(new SvgGroup()); } }
public static SkiaSharp.SKPicture?ToPicture(SvgFragment svgFragment) { var picture = SvgExtensions.ToModel(svgFragment, s_assetLoader, out _, out _); return(picture?.ToSKPicture()); }
public FragmentDrawable(SvgFragment svgFragment, SKRect skOwnerBounds, Drawable?root, Drawable?parent, Attributes ignoreAttributes = Attributes.None) : base(svgFragment, root, parent) { IgnoreAttributes = ignoreAttributes; IsDrawable = HasFeatures(svgFragment, IgnoreAttributes); if (!IsDrawable) { return; } var svgFragmentParent = svgFragment.Parent; float x = svgFragmentParent == null ? 0f : svgFragment.X.ToDeviceValue(UnitRenderingType.Horizontal, svgFragment, skOwnerBounds); float y = svgFragmentParent == null ? 0f : svgFragment.Y.ToDeviceValue(UnitRenderingType.Vertical, svgFragment, skOwnerBounds); var skSize = SvgExtensions.GetDimensions(svgFragment); if (skOwnerBounds.IsEmpty) { skOwnerBounds = SKRect.Create(x, y, skSize.Width, skSize.Height); } CreateChildren(svgFragment, skOwnerBounds, this, this, ignoreAttributes); IsAntialias = SvgPaintingExtensions.IsAntialias(svgFragment); TransformedBounds = skOwnerBounds; CreateTransformedBounds(); Transform = SvgTransformsExtensions.ToSKMatrix(svgFragment.Transforms); var skMatrixViewBox = SvgTransformsExtensions.ToSKMatrix(svgFragment.ViewBox, svgFragment.AspectRatio, x, y, skSize.Width, skSize.Height); SKMatrix.PreConcat(ref Transform, ref skMatrixViewBox); switch (svgFragment.Overflow) { case SvgOverflow.Auto: case SvgOverflow.Visible: case SvgOverflow.Inherit: break; default: if (skSize.IsEmpty) { Overflow = SKRect.Create( x, y, Math.Abs(TransformedBounds.Left) + TransformedBounds.Width, Math.Abs(TransformedBounds.Top) + TransformedBounds.Height); } else { Overflow = SKRect.Create(x, y, skSize.Width, skSize.Height); } break; } var clipPathUris = new HashSet <Uri>(); var svgClipPath = svgFragment.GetUriElementReference <SvgClipPath>("clip-path", clipPathUris); if (svgClipPath != null && svgClipPath.Children != null) { ClipPath = IgnoreAttributes.HasFlag(Attributes.ClipPath) ? null : SvgClippingExtensions.GetClipPath(svgClipPath, TransformedBounds, clipPathUris, _disposable); } else { ClipPath = null; } Fill = null; Stroke = null; // TODO: Transform _skBounds using _skMatrix. SKMatrix.MapRect(ref Transform, out TransformedBounds, ref TransformedBounds); }
public static FragmentDrawable Create(SvgFragment svgFragment, Rect skOwnerBounds, DrawableBase?parent, IAssetLoader assetLoader, Attributes ignoreAttributes = Attributes.None) { var drawable = new FragmentDrawable(assetLoader) { Element = svgFragment, Parent = parent, IgnoreAttributes = ignoreAttributes }; drawable.IsDrawable = drawable.HasFeatures(svgFragment, drawable.IgnoreAttributes); if (!drawable.IsDrawable) { return(drawable); } var svgFragmentParent = svgFragment.Parent; var x = svgFragmentParent is null ? 0f : svgFragment.X.ToDeviceValue(UnitRenderingType.Horizontal, svgFragment, skOwnerBounds); var y = svgFragmentParent is null ? 0f : svgFragment.Y.ToDeviceValue(UnitRenderingType.Vertical, svgFragment, skOwnerBounds); var skSize = SvgModelExtensions.GetDimensions(svgFragment); if (skOwnerBounds.IsEmpty) { skOwnerBounds = Rect.Create(x, y, skSize.Width, skSize.Height); } drawable.CreateChildren(svgFragment, skOwnerBounds, drawable, assetLoader, ignoreAttributes); drawable.IsAntialias = SvgModelExtensions.IsAntialias(svgFragment); drawable.TransformedBounds = skOwnerBounds; drawable.CreateTransformedBounds(); drawable.Transform = SvgModelExtensions.ToMatrix(svgFragment.Transforms); var skMatrixViewBox = SvgModelExtensions.ToMatrix(svgFragment.ViewBox, svgFragment.AspectRatio, x, y, skSize.Width, skSize.Height); drawable.Transform = drawable.Transform.PreConcat(skMatrixViewBox); switch (svgFragment.Overflow) { case SvgOverflow.Auto: case SvgOverflow.Visible: case SvgOverflow.Inherit: break; default: if (skSize.IsEmpty) { drawable.Overflow = Rect.Create( x, y, Math.Abs(drawable.TransformedBounds.Left) + drawable.TransformedBounds.Width, Math.Abs(drawable.TransformedBounds.Top) + drawable.TransformedBounds.Height); } else { drawable.Overflow = Rect.Create(x, y, skSize.Width, skSize.Height); } break; } var clipPathUris = new HashSet <Uri>(); var svgClipPath = svgFragment.GetUriElementReference <SvgClipPath>("clip-path", clipPathUris); if (svgClipPath?.Children is { })