#pragma warning restore #endregion fields & pins //called when data for any output pin is requested public void Evaluate(int SpreadMax) { if (FViewCenterIn.IsChanged || FViewSizeIn.IsChanged) { FOutput.SliceCount = SpreadMax; //create views for (int i = 0; i < SpreadMax; i++) { FOutput[i] = new SvgViewBox(FViewCenterIn[i].X - FViewSizeIn[i].X * 0.5f, FViewCenterIn[i].Y - FViewSizeIn[i].Y * 0.5f, FViewSizeIn[i].X, FViewSizeIn[i].Y); } } }
//called when data for any output pin is requested public void Evaluate(int SpreadMax) { //update if (FSVGIn.IsChanged || FSizeIn.IsChanged || FBackgroundIn.IsChanged || FIgnoreView.IsChanged || FViewIn.IsChanged) { FOutput.SliceCount = SpreadMax; for (int i = 0; i < SpreadMax; i++) { var doc = new SvgDocument(); var elem = FSVGIn[i]; if (elem != null) { doc.Children.AddAndForceUniqueID(elem, true, true, LogIDFix); } doc.Transforms = new SvgTransformCollection(); //set view if (!FIgnoreView[i]) { var view = FViewIn[i]; if (view.Equals(SvgViewBox.Empty)) { view = new SvgViewBox(-1, -1, 2, 2); } doc.ViewBox = view; } //set size and output doc doc.Width = new SvgUnit(SvgUnitType.User, Math.Max(FSizeIn[i].X, 1)); doc.Height = new SvgUnit(SvgUnitType.User, Math.Max(FSizeIn[i].Y, 1)); doc.SetVVVVBackgroundColor(FBackgroundIn[i]); FOutput[i] = doc; } } }
//called when data for any output pin is requested public void Evaluate(int SpreadMax) { //update if (FSVGIn.IsChanged || FSizeIn.IsChanged || FBackgroundIn.IsChanged || FIgnoreView.IsChanged || FViewIn.IsChanged || FResized) { FResized = false; FSVGDoc = new SvgDocument(); foreach (var elem in FSVGIn) { if (elem != null) { FSVGDoc.Children.Add(elem); } } FSVGDoc.Transforms = new SvgTransformCollection(); //set view if (!FIgnoreView[0]) { var view = FViewIn[0]; if (view.Equals(SvgViewBox.Empty)) { view = new SvgViewBox(-1, -1, 2, 2); } FSVGDoc.ViewBox = view; } //calc size FSize.Width = FSizeIn[0].X; FSize.Height = FSizeIn[0].Y; if (FSize == SizeF.Empty) { FSize = new SizeF(this.Width, this.Height); } //set size and output doc FSVGDoc.Width = new SvgUnit(SvgUnitType.User, Math.Max(FSize.Width, 1)); FSVGDoc.Height = new SvgUnit(SvgUnitType.User, Math.Max(FSize.Height, 1)); FOutput[0] = new SvgDoc(FSVGDoc, FBackgroundIn[0].Color); FSizeOutput[0] = new Vector2(FSize.Width, FSize.Height); } //render to window if (FThisNode.Window != null) { if (FBitMap == null) { FBitMap = new Bitmap((int)Math.Ceiling(FSVGDoc.Width), (int)Math.Ceiling(FSVGDoc.Height)); } else if (FBitMap.Height != FSVGDoc.Height || FBitMap.Width != FSVGDoc.Width) { FBitMap.Dispose(); FBitMap = new Bitmap((int)Math.Ceiling(FSVGDoc.Width), (int)Math.Ceiling(FSVGDoc.Height)); } //clear bitmap var g = Graphics.FromImage(FBitMap); g.Clear(FBackgroundIn[0].Color); g.Dispose(); FSVGDoc.Draw(FBitMap); FPicBox.Image = FBitMap; } }
#pragma warning restore #endregion fields & pins //called when data for any output pin is requested public void Evaluate(int SpreadMax) { if (InputChanged() || FBackgroundIn.IsChanged || FReloadIn[0]) { FDocOut.SliceCount = SpreadMax; FLayerOut.SliceCount = SpreadMax; FViewOut.SliceCount = SpreadMax; FHasViewOut.SliceCount = SpreadMax; FSizeOut.SliceCount = SpreadMax; FHasSizeOut.SliceCount = SpreadMax; for (int i = 0; i < SpreadMax; i++) { var doc = ReadDocument(i); if (doc != null) { FDocOut[i] = new SvgDoc(doc, FBackgroundIn[i].Color); var spread = FLayerOut[i]; spread.SliceCount = doc.Children.Count; for (int j = 0; j < spread.SliceCount; j++) { spread[j] = doc.Children[j]; } //check view and size var noView = doc.ViewBox.Equals(SvgViewBox.Empty); var hp = new SvgUnit(SvgUnitType.Percentage, 100); var noSize = (doc.Width == hp && doc.Height == hp); var view = doc.ViewBox; if (noView) { if (noSize) { view = doc.Bounds; } else { view = doc.GetDimensions(); } doc.ViewBox = view; } if (noSize) { doc.Width = new SvgUnit(SvgUnitType.User, view.Width); doc.Height = new SvgUnit(SvgUnitType.User, view.Height); } FViewOut[i] = view; FHasViewOut[i] = !noView; var size = doc.GetDimensions(); FSizeOut[i] = new Vector2D(size.Width, size.Height); FHasSizeOut[i] = !noSize; } else { FDocOut[i] = new SvgDoc(); FLayerOut[i].SliceCount = 0; FViewOut[i] = new SvgViewBox(); FHasViewOut[i] = false; FSizeOut[i] = new Vector2D(); FHasSizeOut[i] = false; } } } //FLogger.Log(LogType.Debug, "hi tty!"); }
//called when data for any output pin is requested public void Evaluate(int SpreadMax) { //update if (FSVGIn.IsChanged || FSizeIn.IsChanged || FBackgroundIn.IsChanged || FIgnoreView.IsChanged || FViewIn.IsChanged || FResized) { FResized = false; FSVGDoc = new SvgDocument(); foreach (var elem in FSVGIn) { if (elem != null) { FSVGDoc.Children.AddAndForceUniqueID(elem, true, true, LogIDFix); } } FSVGDoc.Transforms = new SvgTransformCollection(); //set view if (!FIgnoreView[0]) { var view = FViewIn[0]; if (view.Equals(SvgViewBox.Empty)) { view = new SvgViewBox(-1, -1, 2, 2); } FSVGDoc.ViewBox = view; } //calc size FSize.Width = FSizeIn[0].X; FSize.Height = FSizeIn[0].Y; if (FSize == SizeF.Empty) { FSize = new SizeF(this.Width, this.Height); } //set size and output doc FSVGDoc.Width = new SvgUnit(SvgUnitType.User, Math.Max(FSize.Width, 1)); FSVGDoc.Height = new SvgUnit(SvgUnitType.User, Math.Max(FSize.Height, 1)); FSVGDoc.SetVVVVBackgroundColor(FBackgroundIn[0]); FOutput[0] = FSVGDoc; FSizeOutput[0] = new Vector2(FSize.Width, FSize.Height); } //render to window if (FThisNode.Window != null) { if (FBitMap == null) { FBitMap = new Bitmap((int)Math.Ceiling(FSVGDoc.Width), (int)Math.Ceiling(FSVGDoc.Height)); } else if (FBitMap.Height != FSVGDoc.Height || FBitMap.Width != FSVGDoc.Width) { FBitMap.Dispose(); FBitMap = new Bitmap((int)Math.Ceiling(FSVGDoc.Width), (int)Math.Ceiling(FSVGDoc.Height)); } //clear bitmap //also set controls backcolor so it does not flash when going fullscreen if (FBackgroundIn.IsChanged) { this.BackColor = FBackgroundIn[0].Color; } Draw(FSVGDoc, FBitMap); FPicBox.Image = FBitMap; } }
public static SKMatrix ToSKMatrix(this SvgViewBox svgViewBox, SvgAspectRatio svgAspectRatio, float x, float y, float width, float height) { if (svgViewBox.Equals(SvgViewBox.Empty)) { return(SKMatrix.MakeTranslation(x, y)); } float fScaleX = width / svgViewBox.Width; float fScaleY = height / svgViewBox.Height; float fMinX = -svgViewBox.MinX * fScaleX; float fMinY = -svgViewBox.MinY * fScaleY; if (svgAspectRatio == null) { svgAspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid); } if (svgAspectRatio.Align != SvgPreserveAspectRatio.none) { if (svgAspectRatio.Slice) { fScaleX = Math.Max(fScaleX, fScaleY); fScaleY = Math.Max(fScaleX, fScaleY); } else { fScaleX = Math.Min(fScaleX, fScaleY); fScaleY = Math.Min(fScaleX, fScaleY); } float fViewMidX = (svgViewBox.Width / 2) * fScaleX; float fViewMidY = (svgViewBox.Height / 2) * fScaleY; float fMidX = width / 2; float fMidY = height / 2; fMinX = -svgViewBox.MinX * fScaleX; fMinY = -svgViewBox.MinY * fScaleY; switch (svgAspectRatio.Align) { case SvgPreserveAspectRatio.xMinYMin: break; case SvgPreserveAspectRatio.xMidYMin: fMinX += fMidX - fViewMidX; break; case SvgPreserveAspectRatio.xMaxYMin: fMinX += width - svgViewBox.Width * fScaleX; break; case SvgPreserveAspectRatio.xMinYMid: fMinY += fMidY - fViewMidY; break; case SvgPreserveAspectRatio.xMidYMid: fMinX += fMidX - fViewMidX; fMinY += fMidY - fViewMidY; break; case SvgPreserveAspectRatio.xMaxYMid: fMinX += width - svgViewBox.Width * fScaleX; fMinY += fMidY - fViewMidY; break; case SvgPreserveAspectRatio.xMinYMax: fMinY += height - svgViewBox.Height * fScaleY; break; case SvgPreserveAspectRatio.xMidYMax: fMinX += fMidX - fViewMidX; fMinY += height - svgViewBox.Height * fScaleY; break; case SvgPreserveAspectRatio.xMaxYMax: fMinX += width - svgViewBox.Width * fScaleX; fMinY += height - svgViewBox.Height * fScaleY; break; default: break; } } var skMatrixTotal = SKMatrix.MakeIdentity(); var skMatrixXY = SKMatrix.MakeTranslation(x, y); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixXY); var skMatrixMinXY = SKMatrix.MakeTranslation(fMinX, fMinY); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixMinXY); var skMatrixScale = SKMatrix.MakeScale(fScaleX, fScaleY); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixScale); return(skMatrixTotal); }
/// <summary> /// Adjust SVG to fit content with return of SvgDocument /// </summary> /// <param name="svgXml"></param> /// <param name="docWidth">Desired document width</param> /// <param name="docUnits"></param> /// <returns></returns> public static SvgDocument AdjustSvgDocumentToFitContent( string svgXml, float docWidth, SvgUnitType docUnits) { string svgXml2 = null; SvgDocument doc = null; RectangleF bb = new RectangleF(); if (Lex.IsUndefined(svgXml)) { return(null); } try { doc = SvgDocument.FromSvg <SvgDocument>(svgXml); SizeF dr = doc.GetDimensions(); // document dimensions in current units (e.g. pixel, inch, millimeter...) SvgViewBox vb = doc.ViewBox; // internal coord view box (viewport) (undefined initially) //SvgAspectRatio aspectRatio = doc.AspectRatio; // aspect of the viewport. //RectangleF docBounds = doc.Bounds; // bounds of the svg element //SvgUnit docLeft = doc.X; // left position //SvgUnit docTop = doc.Y; // top position //SvgUnit docHeight = doc.Height; // height //SvgUnit docWidth = doc.Width; // width RectangleF b = doc.Bounds; // unitless bounds of the document svg elements float dy = 5, dx = 5; // padding around the doc elements (this is arbitrary, needs work) //if (b.Width > 0 && b.Height > 0) // add padding keeping aspect ration (not necessary) // dx = dy * (b.Width / b.Height); bb = new RectangleF(b.X - dx, b.Y - dy, b.Width + 2 * dx, b.Height + 2 * dy); float bbLeft = bb.Left; float bbTop = bb.Top; float bbWidth = bb.Width; if (bbWidth <= 0) { bbWidth = 1; // avoid possible zero divide } float bbHeight = bb.Height; SvgViewBox vb2 = new SvgViewBox(bbLeft, bbTop, bbWidth, bbHeight); // adjust the view box to just contain the image elements doc.ViewBox = vb2; docUnits = doc.Width.Type; doc.Width = new SvgUnit(docUnits, docWidth); float docHeight = docWidth * (bbHeight / bbWidth); doc.Height = new SvgUnit(docUnits, docHeight); doc.X = new SvgUnit(docUnits, 0); doc.Y = new SvgUnit(docUnits, 0); if (DebugMx.True && ClientState.IsDeveloper) // debug - dump xml and bitmap { try { FileUtil.WriteFile(@"c:\downloads\SvgUtilTestXml.html", svgXml); //bmp.Save(@"c:\downloads\SvgUtilTestBmp.bmp"); } catch (Exception ex) { ex = ex; } } return(doc); } catch (Exception ex) { return(null); // ignore exceptions for now } }
public void SetViewBox(int view) { ViewBox = new SvgViewBox(0, Height * view, Width, Height); }