VgVisualDoc ReadSvgFile(string filename) { string svgContent = System.IO.File.ReadAllText(filename); VgDocBuilder docBuidler = new VgDocBuilder(); SvgParser parser = new SvgParser(docBuidler); WebLexer.TextSnapshot textSnapshot = new WebLexer.TextSnapshot(svgContent); parser.ParseDocument(textSnapshot); //TODO: review this step again VgVisualDocBuilder builder = new VgVisualDocBuilder(); return(builder.CreateVgVisualDoc(docBuidler.ResultDocument, _vgVisualDocHost)); }
MemBitmap ParseAndRenderSvg(System.Text.StringBuilder svgContent) { //---------- //copy from HtmlRenderer's SvgViewer demo //---------- var docBuilder = new VgDocBuilder(); var parser = new SvgParser(docBuilder); TextSnapshot textSnapshot = new TextSnapshot(svgContent.ToString()); parser.ParseDocument(textSnapshot); VgVisualDocBuilder builder = new VgVisualDocBuilder(); VgVisualElement vgVisElem = builder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem; PixelFarm.CpuBlit.VertexProcessing.Q1RectD bounds = vgVisElem.GetRectBounds(); float actualXOffset = (float)-bounds.Left; float actualYOffset = (float)-bounds.Bottom; int bmpW = (int)Math.Round(bounds.Width); int bmpH = (int)Math.Round(bounds.Height); if (bmpW == 0 || bmpH == 0) { return(null); } MemBitmap memBitmap = new MemBitmap(bmpW, bmpH); using (Tools.BorrowAggPainter(memBitmap, out var p)) using (Tools.More.BorrowVgPaintArgs(p, out var paintArgs)) { float orgX = p.OriginX; float orgY = p.OriginY; p.SetOrigin(actualXOffset, actualYOffset); p.Clear(PixelFarm.Drawing.Color.White); p.FillColor = PixelFarm.Drawing.Color.Black; double prevStrokeW = p.StrokeWidth; vgVisElem.Paint(paintArgs); p.StrokeWidth = prevStrokeW; //restore p.SetOrigin(orgX, orgY); //restore } return(memBitmap); }
void ParseAndRenderSvgFile(string svgFile) { var docBuilder = new VgDocBuilder(); var svgParser = new SvgParser(docBuilder); //TODO: ask file content from host, string svgContent = System.IO.File.ReadAllText(svgFile); WebLexer.TextSnapshot textSnapshot = new WebLexer.TextSnapshot(svgContent); svgParser.ParseDocument(textSnapshot); // #if DEBUG System.Diagnostics.Debug.WriteLine(svgFile); #endif var vgDocBuilder = new VgVisualDocBuilder(); VgVisualElement vgVisElem = vgDocBuilder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem; var uiSprite = new UISprite(10, 10); var evListener = new GeneralEventListener(); uiSprite.AttachExternalEventListener(evListener); evListener.MouseDown += (s, e) => { //hit on svg color- area VgHitInfo hitInfo = uiSprite.FindRenderElementAtPos(e.X, e.Y, false); #if DEBUG if (hitInfo.hitElem != null) { SvgElement domElem = hitInfo.hitElem.DomElem; if (domElem != null && domElem.ElemId != null) { Console.WriteLine(domElem.ElemId); } } #endif }; uiSprite.LoadVg(vgVisElem); _backBoard.ClearChildren(); _backBoard.Add(uiSprite); }
public static VgVisualElement CreateVgVisualElemFromSvgContent(string svgContent) { VgDocBuilder docBuidler = new VgDocBuilder(); SvgParser parser = new SvgParser(docBuidler); WebLexer.TextSnapshot textSnapshot = new WebLexer.TextSnapshot(svgContent); parser.ParseDocument(textSnapshot);//start document parsing //TODO: review this step again VgVisualDocBuilder builder = new VgVisualDocBuilder(); VgDocument svgDoc = docBuidler.ResultDocument; //optional svgDoc.OriginalContent = svgContent; //------------------------------------------------------------- VgVisualElement vgVisRootElem = builder.CreateVgVisualDoc(svgDoc, null).VgRootElem; // vgVisRootElem.OwnerDocument = svgDoc;//tmp return(vgVisRootElem); }
public static void ParseAndRenderSvg(PixelFarm.Drawing.SvgBmpBuilderReq req) { //---------- //copy from HtmlRenderer's SvgViewer demo //---------- var docBuilder = new VgDocBuilder(); var parser = new SvgParser(docBuilder); TextSnapshot textSnapshot = new TextSnapshot(req.SvgContent.ToString()); parser.ParseDocument(textSnapshot); VgVisualDocBuilder builder = new VgVisualDocBuilder(); VgVisualElement vgVisElem = builder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem; PixelFarm.CpuBlit.VertexProcessing.Q1RectD bounds = vgVisElem.GetRectBounds(); float actualXOffset = (float)-bounds.Left; float actualYOffset = (float)-bounds.Bottom; //original svg width, height int bmpW = (int)Math.Round(bounds.Width); int bmpH = (int)Math.Round(bounds.Height); if (bmpW == 0 || bmpH == 0) { return; } //scale svg to specific size float scale_w = req.ExpectedWidth / bmpW; //at this point, we have 2 options //1) create bitmap with original svg size and scale it down to expected size //2) scale svg to expected size and create a bitmap //we choose 2) int new_w = (int)Math.Round(bmpW * scale_w); int new_h = (int)Math.Round(bmpH * scale_w); MemBitmap memBitmap = new MemBitmap(new_w, new_h); using (Tools.BorrowAggPainter(memBitmap, out var p)) using (Tools.More.BorrowVgPaintArgs(p, out VgPaintArgs paintArgs)) { //pass by Affine tx = Affine.NewScaling(scale_w); paintArgs._currentTx = tx; float orgX = p.OriginX; float orgY = p.OriginY; p.SetOrigin(actualXOffset * scale_w, actualYOffset * scale_w); p.Clear(req.DefaultBgColor); p.FillColor = PixelFarm.Drawing.Color.Black; double prevStrokeW = p.StrokeWidth; vgVisElem.Paint(paintArgs); p.StrokeWidth = prevStrokeW; //restore p.SetOrigin(orgX, orgY); //restore } #if DEBUG //memBitmap.SaveImage("svg.png"); #endif req.Output = memBitmap; }