private void Initialize(HashSet <Uri>?references) { if (Element is not SvgGroup svgGroup) { return;; } IsAntialias = SvgExtensions.IsAntialias(svgGroup); GeometryBounds = SKRect.Empty; CreateGeometryBounds(); Transform = SvgExtensions.ToMatrix(svgGroup.Transforms); if (SvgExtensions.IsValidFill(svgGroup)) { Fill = SvgExtensions.GetFillPaint(svgGroup, GeometryBounds, AssetLoader, references, IgnoreAttributes); } if (SvgExtensions.IsValidStroke(svgGroup, GeometryBounds)) { Stroke = SvgExtensions.GetStrokePaint(svgGroup, GeometryBounds, AssetLoader, references, IgnoreAttributes); } }
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 GroupDrawable Create(SvgGroup svgGroup, SKRect skViewport, DrawableBase?parent, IAssetLoader assetLoader, HashSet <Uri>?references, DrawAttributes ignoreAttributes = DrawAttributes.None) { var drawable = new GroupDrawable(assetLoader, references) { Element = svgGroup, Parent = parent, IgnoreAttributes = ignoreAttributes }; drawable.IsDrawable = drawable.CanDraw(svgGroup, drawable.IgnoreAttributes) && drawable.HasFeatures(svgGroup, drawable.IgnoreAttributes); // NOTE: Call AddMarkers only once. SvgExtensions.AddMarkers(svgGroup); drawable.CreateChildren(svgGroup, skViewport, drawable, assetLoader, references, ignoreAttributes); // TODO: Check if children are explicitly set to be visible. //foreach (var child in drawable.ChildrenDrawables) //{ // if (child.IsDrawable) // { // IsDrawable = true; // break; // } //} if (!drawable.IsDrawable) { return(drawable); } drawable.Initialize(references); return(drawable); }
private void Initialize() { if (Element is not SvgAnchor svgAnchor) { return;; } IsAntialias = SvgExtensions.IsAntialias(svgAnchor); GeometryBounds = SKRect.Empty; CreateGeometryBounds(); Transform = SvgExtensions.ToMatrix(svgAnchor.Transforms); Fill = null; Stroke = null; ClipPath = null; MaskDrawable = null; Opacity = IgnoreAttributes.HasFlag(DrawAttributes.Opacity) ? null : SvgExtensions.GetOpacityPaint(svgAnchor); Filter = null; }
public static ImageDrawable Create(SvgImage svgImage, SKRect skViewport, DrawableBase?parent, IAssetLoader assetLoader, HashSet <Uri>?references, DrawAttributes ignoreAttributes = DrawAttributes.None) { var drawable = new ImageDrawable(assetLoader, references) { Element = svgImage, Parent = parent, IgnoreAttributes = ignoreAttributes }; drawable.IsDrawable = drawable.CanDraw(svgImage, drawable.IgnoreAttributes) && drawable.HasFeatures(svgImage, drawable.IgnoreAttributes); if (!drawable.IsDrawable) { return(drawable); } var width = svgImage.Width.ToDeviceValue(UnitRenderingType.Horizontal, svgImage, skViewport); var height = svgImage.Height.ToDeviceValue(UnitRenderingType.Vertical, svgImage, skViewport); var x = svgImage.Location.X.ToDeviceValue(UnitRenderingType.Horizontal, svgImage, skViewport); var y = svgImage.Location.Y.ToDeviceValue(UnitRenderingType.Vertical, svgImage, skViewport); var location = new SKPoint(x, y); if (width <= 0f || height <= 0f || svgImage.Href is null) { drawable.IsDrawable = false; return(drawable); } var uri = SvgExtensions.GetImageUri(svgImage.Href, svgImage.OwnerDocument); if (references is { } && references.Contains(uri))
public static UseDrawable Create(SvgUse svgUse, SKRect skViewport, DrawableBase?parent, IAssetLoader assetLoader, HashSet <Uri>?references, DrawAttributes ignoreAttributes = DrawAttributes.None) { var drawable = new UseDrawable(assetLoader, references) { Element = svgUse, Parent = parent, IgnoreAttributes = ignoreAttributes }; drawable.IsDrawable = drawable.CanDraw(svgUse, drawable.IgnoreAttributes) && drawable.HasFeatures(svgUse, drawable.IgnoreAttributes); if (!drawable.IsDrawable) { return(drawable); } if (SvgExtensions.HasRecursiveReference(svgUse, (e) => e.ReferencedElement, new HashSet <Uri>())) { drawable.IsDrawable = false; return(drawable); } var svgReferencedElement = SvgExtensions.GetReference <SvgElement>(svgUse, svgUse.ReferencedElement); if (svgReferencedElement is null) { drawable.IsDrawable = false; return(drawable); } var x = svgUse.X.ToDeviceValue(UnitRenderingType.Horizontal, svgUse, skViewport); var y = svgUse.Y.ToDeviceValue(UnitRenderingType.Vertical, svgUse, skViewport); var width = svgUse.Width.ToDeviceValue(UnitRenderingType.Horizontal, svgUse, skViewport); var height = svgUse.Height.ToDeviceValue(UnitRenderingType.Vertical, svgUse, skViewport); if (width <= 0f) { width = new SvgUnit(SvgUnitType.Percentage, 100f).ToDeviceValue(UnitRenderingType.Horizontal, svgUse, skViewport); } if (height <= 0f) { height = new SvgUnit(SvgUnitType.Percentage, 100f).ToDeviceValue(UnitRenderingType.Vertical, svgUse, skViewport); } var originalReferencedElementParent = svgReferencedElement.Parent; try { if (s_referencedElementParent is { }) { s_referencedElementParent.SetValue(svgReferencedElement, svgUse); } }
public static MaskDrawable Create(SvgMask svgMask, SKRect skViewport, DrawableBase?parent, IAssetLoader assetLoader, HashSet <Uri>?references, DrawAttributes ignoreAttributes = DrawAttributes.None) { var drawable = new MaskDrawable(assetLoader, references) { Element = svgMask, Parent = parent, IgnoreAttributes = ignoreAttributes, IsDrawable = true }; if (!drawable.IsDrawable) { return(drawable); } var maskUnits = svgMask.MaskUnits; var maskContentUnits = svgMask.MaskContentUnits; var xUnit = svgMask.X; var yUnit = svgMask.Y; var widthUnit = svgMask.Width; var heightUnit = svgMask.Height; // TODO: Pass correct skViewport var skRectTransformed = SvgExtensions.CalculateRect(xUnit, yUnit, widthUnit, heightUnit, maskUnits, skViewport, skViewport, svgMask); if (skRectTransformed is null) { drawable.IsDrawable = false; return(drawable); } var skMatrix = SKMatrix.CreateIdentity(); if (maskContentUnits == SvgCoordinateUnits.ObjectBoundingBox) { var skBoundsTranslateTransform = SKMatrix.CreateTranslation(skViewport.Left, skViewport.Top); skMatrix = skMatrix.PreConcat(skBoundsTranslateTransform); var skBoundsScaleTransform = SKMatrix.CreateScale(skViewport.Width, skViewport.Height); skMatrix = skMatrix.PreConcat(skBoundsScaleTransform); } drawable.CreateChildren(svgMask, skViewport, drawable, assetLoader, references, ignoreAttributes); drawable.Initialize(skRectTransformed.Value, skMatrix); return(drawable); }
private void Initialize(SKRect skViewport, float x, float y, SKSize skSize) { if (Element is not SvgFragment svgFragment) { return;; } IsAntialias = SvgExtensions.IsAntialias(svgFragment); GeometryBounds = skViewport; CreateGeometryBounds(); Transform = SvgExtensions.ToMatrix(svgFragment.Transforms); var skViewBoxMatrix = SvgExtensions.ToMatrix(svgFragment.ViewBox, svgFragment.AspectRatio, x, y, skSize.Width, skSize.Height); Transform = Transform.PreConcat(skViewBoxMatrix); switch (svgFragment.Overflow) { case SvgOverflow.Auto: case SvgOverflow.Visible: case SvgOverflow.Inherit: break; default: if (skSize.IsEmpty) { Overflow = SKRect.Create( x, y, Math.Abs(GeometryBounds.Left) + GeometryBounds.Width, Math.Abs(GeometryBounds.Top) + GeometryBounds.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?.Children is { })
public override void PostProcess(SKRect?viewport, SKMatrix totalMatrix) { var element = Element; if (element is null) { return; } var enableMask = !IgnoreAttributes.HasFlag(DrawAttributes.Mask); ClipPath = null; if (enableMask) { MaskDrawable = SvgExtensions.GetSvgElementMask(element, GeometryBounds, new HashSet <Uri>(), AssetLoader, References); if (MaskDrawable is { })
private void Initialize(SKRect skRectTransformed, SKMatrix skMatrix) { if (Element is not SvgMask svgMask) { return; } Overflow = skRectTransformed; IsAntialias = SvgExtensions.IsAntialias(svgMask); GeometryBounds = skRectTransformed; Transform = skMatrix; Fill = null; Stroke = null; }
private void Initialize(SKRect skViewport, HashSet <Uri>?references) { if (Element is not SvgPath svgPath || Path is null) { return; } IsAntialias = SvgExtensions.IsAntialias(svgPath); GeometryBounds = Path.Bounds; Transform = SvgExtensions.ToMatrix(svgPath.Transforms); var canDrawFill = true; var canDrawStroke = true; if (SvgExtensions.IsValidFill(svgPath)) { Fill = SvgExtensions.GetFillPaint(svgPath, GeometryBounds, AssetLoader, references, IgnoreAttributes); if (Fill is null) { canDrawFill = false; } } if (SvgExtensions.IsValidStroke(svgPath, GeometryBounds)) { Stroke = SvgExtensions.GetStrokePaint(svgPath, GeometryBounds, AssetLoader, references, IgnoreAttributes); if (Stroke is null) { canDrawStroke = false; } } if (canDrawFill && !canDrawStroke) { IsDrawable = false; return; } SvgExtensions.CreateMarkers(svgPath, Path, skViewport, this, AssetLoader, references); }
private void Initialize(float x, float y, float width, float height) { if (Element is not SvgSymbol svgSymbol) { return; } IsAntialias = SvgExtensions.IsAntialias(svgSymbol); GeometryBounds = SKRect.Empty; CreateGeometryBounds(); Transform = SvgExtensions.ToMatrix(svgSymbol.Transforms); var skMatrixViewBox = SvgExtensions.ToMatrix(svgSymbol.ViewBox, svgSymbol.AspectRatio, x, y, width, height); Transform = Transform.PreConcat(skMatrixViewBox); Fill = null; Stroke = null; }
private void Initialize(HashSet <Uri>?references) { if (Element is not SvgCircle svgCircle || Path is null) { return;; } IsAntialias = SvgExtensions.IsAntialias(svgCircle); GeometryBounds = Path.Bounds; Transform = SvgExtensions.ToMatrix(svgCircle.Transforms); var canDrawFill = true; var canDrawStroke = true; if (SvgExtensions.IsValidFill(svgCircle)) { Fill = SvgExtensions.GetFillPaint(svgCircle, GeometryBounds, AssetLoader, references, IgnoreAttributes); if (Fill is null) { canDrawFill = false; } } if (SvgExtensions.IsValidStroke(svgCircle, GeometryBounds)) { Stroke = SvgExtensions.GetStrokePaint(svgCircle, GeometryBounds, AssetLoader, references, IgnoreAttributes); if (Stroke is null) { canDrawStroke = false; } } if (canDrawFill && !canDrawStroke) { IsDrawable = false; } }
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 override void PostProcess(SKRect?viewport, SKMatrix totalMatrix) { var element = Element; if (element is null) { return; } var enableOpacity = !IgnoreAttributes.HasFlag(DrawAttributes.Opacity); ClipPath = null; MaskDrawable = null; Opacity = enableOpacity ? SvgExtensions.GetOpacityPaint(element) : null; Filter = null; TotalTransform = totalMatrix.PreConcat(Transform); TransformedBounds = TotalTransform.MapRect(GeometryBounds); foreach (var child in ChildrenDrawables) { child.PostProcess(viewport, totalMatrix); } }
public void Execute(GeneratorExecutionContext context) { try { context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.NamespaceName", out var globalNamespaceName); var files = context.AdditionalFiles.Where(at => at.Path.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase)); foreach (var file in files) { string?namespaceName = null; if (!string.IsNullOrWhiteSpace(globalNamespaceName)) { namespaceName = globalNamespaceName; } if (context.AnalyzerConfigOptions.GetOptions(file).TryGetValue("build_metadata.AdditionalFiles.NamespaceName", out var perFilenamespaceName)) { if (!string.IsNullOrWhiteSpace(perFilenamespaceName)) { namespaceName = perFilenamespaceName; } } if (string.IsNullOrWhiteSpace(namespaceName)) { namespaceName = "Svg"; } context.AnalyzerConfigOptions.GetOptions(file).TryGetValue("build_metadata.AdditionalFiles.ClassName", out var className); if (string.IsNullOrWhiteSpace(className)) { className = CreateClassName(file.Path); } if (string.IsNullOrWhiteSpace(namespaceName)) { context.ReportDiagnostic(Diagnostic.Create(s_errorDescriptor, Location.None, "The specified namespace name is invalid.")); return; } if (string.IsNullOrWhiteSpace(className)) { context.ReportDiagnostic(Diagnostic.Create(s_errorDescriptor, Location.None, "The specified class name is invalid.")); return; } var svg = file.GetText(context.CancellationToken)?.ToString(); if (string.IsNullOrWhiteSpace(svg)) { context.ReportDiagnostic(Diagnostic.Create(s_errorDescriptor, Location.None, "Svg file is null or empty.")); return; } var svgDocument = SvgExtensions.FromSvg(svg !); if (svgDocument is { }) { var picture = SvgExtensions.ToModel(svgDocument, _assetLoader, out _, out _); if (picture is { } && picture.Commands is { })