public static void CopyToGdiPlusGraphics(this AggPainter painter, Graphics g, int xpos = 0, int ypos = 0) { using (Bitmap gdiBmp = new Bitmap(painter.Width, painter.Height)) { CopyToGdiPlusBitmapSameSize(painter.RenderSurface.DestBitmap, gdiBmp); g.DrawImage(gdiBmp, new System.Drawing.Point(10, 0)); } }
public static TempContext <AggPainter> Borrow(MemBitmap bmp, out AggPainter painter) { if (!Temp <AggPainter> .IsInit()) { Temp <AggPainter> .SetNewHandler( () => new AggPainter(new AggRenderSurface()), p => { p.RenderSurface.DetachDstBitmap(); } ); } var tmpPainter = Temp <AggPainter> .Borrow(out painter); painter.RenderSurface.AttachDstBitmap(bmp); return(tmpPainter); }
public override void Draw(PixelFarm.Drawing.Painter p) { //specific for agg if (!(p is AggPainter)) { return; } AggPainter p2 = (AggPainter)p; AggRenderSurface asx = p2.RenderSurface; if (asx.DestBitmapBlender != null) { IBitmapBlender backBuffer = asx.DestBitmapBlender; //use different pixel blender var redImageBuffer = new SubBitmapBlender(backBuffer, new PixelBlenderGrey()); var greenImageBuffer = new SubBitmapBlender(backBuffer, new PixelBlenderGrey()); var blueImageBuffer = new SubBitmapBlender(backBuffer, new PixelBlenderGrey()); ClipProxyImage clippingProxy = new ClipProxyImage(backBuffer); ClipProxyImage clippingProxyRed = new ClipProxyImage(redImageBuffer); ClipProxyImage clippingProxyGreen = new ClipProxyImage(greenImageBuffer); ClipProxyImage clippingProxyBlue = new ClipProxyImage(blueImageBuffer); // ScanlineRasterizer sclineRas = asx.ScanlineRasterizer; ScanlinePacked8 scline = asx.ScanlinePacked8; Drawing.Color clearColor = this.UseBlackBlackground ? Drawing.Color.FromArgb(0, 0, 0) : Drawing.Color.FromArgb(255, 255, 255); clippingProxy.Clear(clearColor); Drawing.Color fillColor = this.UseBlackBlackground ? new Drawing.Color((byte)(this.AlphaValue), 255, 255, 255) : new Drawing.Color((byte)(this.AlphaValue), 0, 0, 0); DestBitmapRasterizer bmpRas = asx.BitmapRasterizer; using (VectorToolBox.Borrow(out Ellipse ellipse)) using (VxsTemp.Borrow(out var v1)) { ellipse.Set(Width / 2 - 0.87 * 50, Height / 2 - 0.5 * 50, 100, 100, 100); sclineRas.AddPath(ellipse.MakeVxs(v1)); v1.Clear();//** bmpRas.RenderWithColor(clippingProxyRed, sclineRas, scline, fillColor); //// ellipse.Set(Width / 2 + 0.87 * 50, Height / 2 - 0.5 * 50, 100, 100, 100); sclineRas.AddPath(ellipse.MakeVxs(v1)); v1.Clear();//*** bmpRas.RenderWithColor(clippingProxyGreen, sclineRas, scline, fillColor); // ellipse.Set(Width / 2, Height / 2 + 50, 100, 100, 100); sclineRas.AddPath(ellipse.MakeVxs(v1)); v1.Clear(); //*** bmpRas.RenderWithColor(clippingProxyBlue, sclineRas, scline, fillColor); } } // else if (graphics2D.DestImageFloat != null) // { //#if false // IImageFloat backBuffer = graphics2D.DestImageFloat; // int distBetween = backBuffer.GetFloatsBetweenPixelsInclusive(); // ImageBufferFloat redImageBuffer = new ImageBufferFloat(); // redImageBuffer.Attach(backBuffer, new blender_gray(distBetween), distBetween, 2, 8); // ImageBufferFloat greenImageBuffer = new ImageBufferFloat(); // greenImageBuffer.Attach(backBuffer, new blender_gray(distBetween), distBetween, 1, 8); // ImageBufferFloat blueImageBuffer = new ImageBufferFloat(); // blueImageBuffer.Attach(backBuffer, new blender_gray(distBetween), distBetween, 0, 8); // ImageClippingProxy clippingProxy = new ImageClippingProxy(backBuffer); // ImageClippingProxy clippingProxyRed = new ImageClippingProxy(redImageBuffer); // ImageClippingProxy clippingProxyGreen = new ImageClippingProxy(greenImageBuffer); // ImageClippingProxy clippingProxyBlue = new ImageClippingProxy(blueImageBuffer); // ScanlineRasterizer ras = new ScanlineRasterizer(); // ScanlineCachePacked8 sl = new ScanlineCachePacked8(); // RGBA_Bytes clearColor = useBlackBackgroundCheckbox.Checked ? new RGBA_Bytes(0, 0, 0) : new RGBA_Bytes(255, 255, 255); // clippingProxy.clear(clearColor); // alphaSlider.View.BackGroundColor = clearColor; // RGBA_Bytes FillColor = useBlackBackgroundCheckbox.Checked ? new RGBA_Bytes(255, 255, 255, (int)(alphaSlider.Value)) : new RGBA_Bytes(0, 0, 0, (int)(alphaSlider.Value)); // VertexSource.Ellipse er = new AGG.VertexSource.Ellipse(Width / 2 - 0.87 * 50, Height / 2 - 0.5 * 50, 100, 100, 100); // ras.add_path(er); // agg_renderer_scanline.Default.render_scanlines_aa_solid(clippingProxyRed, ras, sl, FillColor); // VertexSource.Ellipse eg = new AGG.VertexSource.Ellipse(Width / 2 + 0.87 * 50, Height / 2 - 0.5 * 50, 100, 100, 100); // ras.add_path(eg); // agg_renderer_scanline.Default.render_scanlines_aa_solid(clippingProxyGreen, ras, sl, FillColor); // VertexSource.Ellipse eb = new AGG.VertexSource.Ellipse(Width / 2, Height / 2 + 50, 100, 100, 100); // ras.add_path(eb); // agg_renderer_scanline.Default.render_scanlines_aa_solid(clippingProxyBlue, ras, sl, FillColor); //#endif // } }
public void BuildMultiFontSize(string multiFontSizrAtlasFilename, string imgOutputFilename) { //merge to the new one //1. ensure same atlas width int atlasW = 0; int j = _simpleFontInfoList.Count; int totalHeight = 0; const int interAtlasSpace = 2; for (int i = 0; i < j; ++i) { SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i]; SimpleFontAtlas fontAtlas = atlasInfo.fontAtlasFile.ResultSimpleFontAtlasList[0]; totalHeight += fontAtlas.Height + interAtlasSpace; if (i == 0) { atlasW = fontAtlas.Width; } else { if (atlasW != fontAtlas.Width) { throw new NotSupportedException(); } } } //-------------------------------------------- //in this version, the glyph offsetY is measure from bottom*** int[] offsetFromBottoms = new int[j]; int offsetFromBottom = interAtlasSpace;//start offset for (int i = j - 1; i >= 0; --i) { SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i]; SimpleFontAtlas fontAtlas = atlasInfo.fontAtlasFile.ResultSimpleFontAtlasList[0]; offsetFromBottoms[i] = offsetFromBottom; offsetFromBottom += fontAtlas.Height + interAtlasSpace; } //-------------------------------------------- //merge all img to one int top = 0; using (PixelFarm.CpuBlit.MemBitmap memBitmap = new CpuBlit.MemBitmap(atlasW, totalHeight)) { PixelFarm.CpuBlit.AggPainter painter = PixelFarm.CpuBlit.AggPainter.Create(memBitmap); for (int i = 0; i < j; ++i) { SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i]; FontAtlasFile atlasFile = atlasInfo.fontAtlasFile; SimpleFontAtlas fontAtlas = atlasInfo.fontAtlasFile.ResultSimpleFontAtlasList[0]; atlasInfo.NewCloneLocations = SimpleFontAtlas.CloneLocationWithOffset(fontAtlas, 0, offsetFromBottoms[i]); using (System.IO.Stream fontImgStream = PixelFarm.Platforms.StorageService.Provider.ReadDataStream(atlasInfo.imgFile)) using (PixelFarm.CpuBlit.MemBitmap atlasBmp = PixelFarm.CpuBlit.MemBitmap.LoadBitmap(fontImgStream)) { painter.DrawImage(atlasBmp, 0, top); top += atlasBmp.Height + interAtlasSpace; } } memBitmap.SaveImage(imgOutputFilename); } //-------------------------------------------- //save merged font atlas //TODO: use 'File' provider to access system file using (FileStream fs = new FileStream(multiFontSizrAtlasFilename, FileMode.Create)) using (BinaryWriter w = new BinaryWriter(fs)) { //----------- //overview //total img info FontAtlasFile fontAtlasFile = new FontAtlasFile(); fontAtlasFile.StartWrite(fs); //1. simple atlas count fontAtlasFile.WriteOverviewMultiSizeFontInfo((ushort)j); //2. for (int i = 0; i < j; ++i) { SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i]; RequestFont reqFont = atlasInfo.reqFont; fontAtlasFile.WriteOverviewFontInfo(reqFont.Name, reqFont.FontKey, reqFont.SizeInPoints);//size in points fontAtlasFile.WriteTotalImageInfo( (ushort)atlasW, (ushort)top, 4, atlasInfo.textureKind); // // fontAtlasFile.WriteGlyphList(atlasInfo.NewCloneLocations); } fontAtlasFile.EndWrite(); } }
protected override void PaintImp(PaintVisitor p) { #if DEBUG p.dbugEnterNewContext(this, PaintVisitor.PaintVisitorContextName.Init); #endif DrawBoard drawBoard = p.InnerDrawBoard; if (DisableBmpCache) { PixelFarm.CpuBlit.AggPainter painter = drawBoard.GetPainter() as PixelFarm.CpuBlit.AggPainter; if (painter == null) { return; } //TODO: review here //temp fix if (s_openfontTextService == null) { s_openfontTextService = new OpenFontTextService(); } //painter.CurrentFont = new RequestFont("tahoma", 14); //var textPrinter = new PixelFarm.Drawing.Fonts.VxsTextPrinter(painter, s_openfontTextService); //painter.TextPrinter = textPrinter; //painter.Clear(Color.White); // double prevStrokeW = painter.StrokeWidth; //Color fillColor = painter.FillColor; //painter.StrokeWidth = 1;//default //painter.FillColor = Color.Black; using (Tools.More.BorrowVgPaintArgs(painter, out var paintArgs)) { if (_vgVisualElem.CoordTx != null) { } _vgVisualElem.Paint(paintArgs); } painter.StrokeWidth = prevStrokeW;//restore //painter.FillColor = fillColor;////restore return; } if (_vgVisualElem.HasBitmapSnapshot) { Image backimg = _vgVisualElem.BackingImage; drawBoard.DrawImage(backimg, new RectangleF(0, 0, backimg.Width, backimg.Height)); } else { PixelFarm.CpuBlit.RectD bound = _vgVisualElem.GetRectBounds(); //create PixelFarm.CpuBlit.MemBitmap backimg = new PixelFarm.CpuBlit.MemBitmap((int)bound.Width + 10, (int)bound.Height + 10); #if DEBUG backimg._dbugNote = "cssBoxSvgRoot"; #endif PixelFarm.CpuBlit.AggPainter painter = PixelFarm.CpuBlit.AggPainter.Create(backimg); //TODO: review here //temp fix if (s_openfontTextService == null) { s_openfontTextService = new OpenFontTextService(); } // double prevStrokeW = painter.StrokeWidth; using (Tools.More.BorrowVgPaintArgs(painter, out VgPaintArgs paintArgs)) { if (_vgVisualElem.CoordTx != null) { } _vgVisualElem.Paint(paintArgs); } painter.StrokeWidth = prevStrokeW;//restore //painter.FillColor = fillColor;////restore #if DEBUG //test //PixelFarm.CpuBlit.Imaging.PngImageWriter.dbugSaveToPngFile(backimg, "d:\\WImageTest\\subimg1.png"); #endif _vgVisualElem.SetBitmapSnapshot(backimg, true); drawBoard.DrawImage(backimg, new RectangleF(0, 0, backimg.Width, backimg.Height)); } }