private void Form1_Load(object sender, EventArgs e) { if (DesignMode) { return; } // _basicOptions = basicFontOptionsUserControl1.Options; _basicOptions.TypefaceChanged += (s, e2) => { if (_devVxsTextPrinter != null) { _devVxsTextPrinter.Typeface = e2.SelectedTypeface; } }; _basicOptions.UpdateRenderOutput += (s, e2) => { UpdateRenderOutput(); }; // //---------- _renderOptions = glyphRenderOptionsUserControl1.Options; _renderOptions.UpdateRenderOutput += (s, e2) => { UpdateRenderOutput(); }; //share text printer to our sample textbox //but you can create another text printer that specific to text textbox control destImg = new MemBitmap(800, 600); aggsx = new AggRenderSurface(); aggsx.AttachDstBitmap(destImg); painter = new AggPainter(aggsx); winBmp = new Bitmap(destImg.Width, destImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); g = this.sampleTextBox1.CreateGraphics(); painter.CurrentFont = new PixelFarm.Drawing.RequestFont("tahoma", 14); _openFontTextService = new PixelFarm.Drawing.OpenFontTextService(); //.... _devVxsTextPrinter = new PixelFarm.Drawing.VxsTextPrinter(painter, _openFontTextService); _devVxsTextPrinter.ScriptLang = _basicOptions.ScriptLang; _devVxsTextPrinter.PositionTechnique = Typography.TextLayout.PositionTechnique.OpenFont; _devVxsTextPrinter.FontSizeInPoints = 10; _controllerForPixelFarm.BindHostGraphics(g); _controllerForPixelFarm.TextPrinter = _devVxsTextPrinter; this.sampleTextBox1.SetController(_controllerForPixelFarm); _readyToRender = true; _basicOptions.UpdateRenderOutput += (s, e2) => UpdateRenderOutput(); //.... //---------- //txtInputChar.TextChanged += (s, e2) => UpdateRenderOutput(); //---------- }
public void BindHostGraphics(Graphics hostControlGraphics) { g = hostControlGraphics; // destImg = new MemBitmap(400, 300); p = AggPainter.Create(destImg); winBmp = new Bitmap(400, 300, System.Drawing.Imaging.PixelFormat.Format32bppArgb); _openFontTextServices = new PixelFarm.Drawing.OpenFontTextService(); _printer = new PixelFarm.Drawing.VxsTextPrinter(p, _openFontTextServices); _visualLine = new VisualLine(); _visualLine.BindLine(_line); _visualLine.Y = 100; }
void UpdateRenderOutput() { if (!_readyToRender) { return; } // if (_g == null) { _destImg = new MemBitmap(800, 600); _painter = AggPainter.Create(_destImg); _winBmp = new Bitmap(_destImg.Width, _destImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); _g = this.CreateGraphics(); _painter.CurrentFont = new PixelFarm.Drawing.RequestFont("tahoma", 14); _textService = new PixelFarm.Drawing.OpenFontTextService(); _textService.LoadFontsFromFolder("../../../TestFonts"); _devVxsTextPrinter = new PixelFarm.Drawing.VxsTextPrinter(_painter, _textService); _devVxsTextPrinter.SetSvgBmpBuilderFunc(ParseAndRenderSvg); _devVxsTextPrinter.ScriptLang = _basicOptions.ScriptLang; _devVxsTextPrinter.PositionTechnique = Typography.TextLayout.PositionTechnique.OpenFont; } if (string.IsNullOrEmpty(this.txtInputChar.Text)) { return; } //test option use be used with lcd subpixel rendering. //this demonstrate how we shift a pixel for subpixel rendering tech if (_contourAnalysisOpts.SetupPrinterLayoutForLcdSubPix) { //TODO: set lcd or not here } else { //TODO: set lcd or not here } //1. read typeface from font file TypographyTest.RenderChoice renderChoice = _basicOptions.RenderChoice; switch (renderChoice) { case TypographyTest.RenderChoice.RenderWithGdiPlusPath: //not render in this example //see more at ... break; case TypographyTest.RenderChoice.RenderWithTextPrinterAndMiniAgg: { //clear previous draw _painter.Clear(PixelFarm.Drawing.Color.White); _painter.UseLcdEffectSubPixelRendering = _contourAnalysisOpts.LcdTechnique; _painter.FillColor = PixelFarm.Drawing.Color.Black; _selectedTextPrinter = _devVxsTextPrinter; _selectedTextPrinter.Typeface = _basicOptions.Typeface; _selectedTextPrinter.FontSizeInPoints = _basicOptions.FontSizeInPoints; _selectedTextPrinter.ScriptLang = _basicOptions.ScriptLang; _selectedTextPrinter.PositionTechnique = _basicOptions.PositionTech; _selectedTextPrinter.HintTechnique = _glyphRenderOptions.HintTechnique; _selectedTextPrinter.EnableLigature = _glyphRenderOptions.EnableLigature; _selectedTextPrinter.SimulateSlant = _contourAnalysisOpts.SimulateSlant; _selectedTextPrinter.TextBaseline = (PixelFarm.Drawing.TextBaseline)lstTextBaseline.SelectedItem; //test print 3 lines #if DEBUG DynamicOutline.dbugTestNewGridFitting = _contourAnalysisOpts.EnableGridFit; DynamicOutline.dbugActualPosToConsole = _contourAnalysisOpts.WriteFitOutputToConsole; DynamicOutline.dbugUseHorizontalFitValue = _contourAnalysisOpts.UseHorizontalFitAlignment; #endif char[] printTextBuffer = this.txtInputChar.Text.ToCharArray(); float x_pos = 0, y_pos = 0; float lineSpacingPx = _selectedTextPrinter.FontLineSpacingPx; const int REF_LINE_LEN = 300; for (int i = 0; i < 3; ++i) { _selectedTextPrinter.DrawString(printTextBuffer, x_pos, y_pos); #if DEBUG //show debug info... var prevColor = _painter.FillColor; var prevStrokColor = _painter.StrokeColor; _painter.FillColor = PixelFarm.Drawing.Color.Red; _painter.FillRect(x_pos, y_pos, 5, 5); // start point //see //https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline switch (_selectedTextPrinter.TextBaseline) { default: { System.Diagnostics.Debug.WriteLine("UNIMPLEMENTED" + _selectedTextPrinter.TextBaseline.ToString()); goto case PixelFarm.Drawing.TextBaseline.Alphabetic; // } case PixelFarm.Drawing.TextBaseline.Alphabetic: { //alphabetic baseline _painter.StrokeColor = _grayColor; _painter.DrawLine(x_pos, /**/ y_pos, x_pos + REF_LINE_LEN, y_pos); _painter.StrokeColor = PixelFarm.Drawing.Color.Blue; _painter.DrawLine(x_pos, /**/ y_pos - _selectedTextPrinter.FontDescedingPx, x_pos + REF_LINE_LEN, y_pos - _selectedTextPrinter.FontDescedingPx); //bottom most } break; case PixelFarm.Drawing.TextBaseline.Top: { //alphabetic baseline _painter.StrokeColor = _grayColor; _painter.DrawLine(x_pos, /**/ y_pos + _selectedTextPrinter.FontAscendingPx, x_pos + REF_LINE_LEN, y_pos + _selectedTextPrinter.FontAscendingPx); //em bottom _painter.StrokeColor = PixelFarm.Drawing.Color.Blue; _painter.DrawLine(x_pos, /**/ y_pos + (_selectedTextPrinter.FontAscendingPx - _selectedTextPrinter.FontDescedingPx), x_pos + REF_LINE_LEN, y_pos + (_selectedTextPrinter.FontAscendingPx - _selectedTextPrinter.FontDescedingPx)); //bottom most } break; case PixelFarm.Drawing.TextBaseline.Bottom: { //alphabetic baseline _painter.StrokeColor = _grayColor; _painter.DrawLine(x_pos, /**/ y_pos + _selectedTextPrinter.FontDescedingPx, x_pos + REF_LINE_LEN, y_pos + _selectedTextPrinter.FontDescedingPx); //em bottom _painter.StrokeColor = PixelFarm.Drawing.Color.Blue; _painter.DrawLine(x_pos, /**/ y_pos, x_pos + REF_LINE_LEN, y_pos); //bottom most } break; } _painter.FillColor = prevColor; _painter.StrokeColor = prevColor; #endif y_pos += (_selectedTextPrinter.FontAscendingPx - _selectedTextPrinter.FontDescedingPx); } //copy from Agg's memory buffer to gdi PixelFarm.CpuBlit.BitmapHelper.CopyToGdiPlusBitmapSameSizeNotFlip(_destImg, _winBmp); _g.Clear(Color.White); _g.DrawImage(_winBmp, new Point(0, 0)); } break; //============================================== //render 1 glyph for debug and test case TypographyTest.RenderChoice.RenderWithMsdfGen: case TypographyTest.RenderChoice.RenderWithSdfGen: { char testChar = this.txtInputChar.Text[0]; Typeface typeFace = _basicOptions.Typeface; RenderWithMsdfImg(typeFace, testChar, _basicOptions.FontSizeInPoints); } break; case TypographyTest.RenderChoice.RenderWithMiniAgg_SingleGlyph: { _selectedTextPrinter = _devVxsTextPrinter; //for test only 1 char RenderSingleCharWithMiniAgg( _basicOptions.Typeface, this.txtInputChar.Text[0], _basicOptions.FontSizeInPoints); } break; default: throw new NotSupportedException(); } }