Ejemplo n.º 1
0
        void RenderWithMsdfImg(Typeface typeface, char testChar, float sizeInPoint)
        {
            p.FillColor = PixelFarm.Drawing.Color.Black;
            //p.UseSubPixelRendering = chkLcdTechnique.Checked;
            p.Clear(PixelFarm.Drawing.Color.White);
            //----------------------------------------------------
            var builder  = new MyGlyphPathBuilder(typeface);
            var hintTech = (HintTechnique)cmbHintTechnique.SelectedItem;

            builder.UseTrueTypeInstructions = false; //reset
            builder.UseVerticalHinting      = false; //reset
            switch (hintTech)
            {
            case HintTechnique.TrueTypeInstruction:
                builder.UseTrueTypeInstructions = true;
                break;

            case HintTechnique.TrueTypeInstruction_VerticalOnly:
                builder.UseTrueTypeInstructions = true;
                builder.UseVerticalHinting      = true;
                break;

            case HintTechnique.CustomAutoFit:
                //custom agg autofit
                break;
            }
            //----------------------------------------------------
            builder.Build(testChar, sizeInPoint);
            //----------------------------------------------------
            var         msdfGlyphGen = new MsdfGlyphGen();
            GlyphImage2 glyphImg     = msdfGlyphGen.CreateMsdfImage(
                builder.GetOutputPoints(),
                builder.GetOutputContours(),
                builder.GetPixelScale());
            var actualImg = ActualImage.CreateFromBuffer(glyphImg.Width, glyphImg.Height, PixelFormat.ARGB32, glyphImg.GetImageBuffer());

            p.DrawImage(actualImg, 0, 0);

            //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            //{
            //    var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
            //    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length);
            //    bmp.UnlockBits(bmpdata);
            //    bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png");
            //}

            if (chkShowGrid.Checked)
            {
                //render grid
                RenderGrid(800, 600, _gridSize, p);
            }

            //6. use this util to copy image from Agg actual image to System.Drawing.Bitmap
            PixelFarm.Agg.Imaging.BitmapHelper.CopyToGdiPlusBitmapSameSize(destImg, winBmp);
            //---------------
            //7. just render our bitmap
            g.Clear(Color.White);
            g.DrawImage(winBmp, new Point(30, 20));
        }
Ejemplo n.º 2
0
        void RenderWithGdiPlusPath(Typeface typeface, char testChar, float sizeInPoint, int resolution)
        {
            //render glyph path with Gdi+ path
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.White);
            //////credit:
            //////http://stackoverflow.com/questions/1485745/flip-coordinates-when-drawing-to-control
            g.ScaleTransform(1.0F, -1.0F);           // Flip the Y-Axis
            g.TranslateTransform(0.0F, -(float)300); // Translate the drawing area accordingly


            //----------------------------------------------------
            var builder  = new MyGlyphPathBuilder(typeface);
            var hintTech = (HintTechnique)cmbHintTechnique.SelectedItem;

            builder.UseTrueTypeInstructions = false; //reset
            builder.UseVerticalHinting      = false; //reset
            switch (hintTech)
            {
            case HintTechnique.TrueTypeInstruction:
                builder.UseTrueTypeInstructions = true;
                break;

            case HintTechnique.TrueTypeInstruction_VerticalOnly:
                builder.UseTrueTypeInstructions = true;
                builder.UseVerticalHinting      = true;
                break;

            case HintTechnique.CustomAutoFit:
                //custom agg autofit
                break;
            }
            //----------------------------------------------------
            builder.Build(testChar, sizeInPoint);
            var gdiPathBuilder = new GlyphPathBuilderGdi();

            builder.ReadShapes(gdiPathBuilder);
            float pxScale = builder.GetPixelScale();

            System.Drawing.Drawing2D.GraphicsPath path = gdiPathBuilder.ResultGraphicPath;
            path.Transform(
                new System.Drawing.Drawing2D.Matrix(
                    pxScale, 0,
                    0, pxScale,
                    0, 0
                    ));

            if (chkFillBackground.Checked)
            {
                g.FillPath(Brushes.Black, path);
            }
            if (chkBorder.Checked)
            {
                g.DrawPath(Pens.Green, path);
            }
            //transform back
            g.ScaleTransform(1.0F, -1.0F);           // Flip the Y-Axis
            g.TranslateTransform(0.0F, -(float)300); // Translate the drawing area accordingly
        }
Ejemplo n.º 3
0
        static void CreateSampleMsdfTextureFont(string fontfile, float sizeInPoint, ushort startGlyphIndex, ushort endGlyphIndex, string outputFile)
        {
            //sample
            var reader = new OpenFontReader();

            using (var fs = new FileStream(fontfile, FileMode.Open))
            {
                //1. read typeface from font file
                Typeface typeface = reader.Read(fs);
                //sample: create sample msdf texture
                //-------------------------------------------------------------
                var builder = new MyGlyphPathBuilder(typeface);
                //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
                //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
                //-------------------------------------------------------------
                var atlasBuilder = new SimpleFontAtlasBuilder2();
                var msdfBuilder  = new MsdfGlyphGen();

                for (ushort n = startGlyphIndex; n <= endGlyphIndex; ++n)
                {
                    //build glyph
                    builder.BuildFromGlyphIndex(n, sizeInPoint);

                    var msdfGlyphGen = new MsdfGlyphGen();
                    var actualImg    = msdfGlyphGen.CreateMsdfImage(
                        builder.GetOutputPoints(),
                        builder.GetOutputContours(),
                        builder.GetPixelScale());
                    atlasBuilder.AddGlyph((int)n, actualImg);

                    //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    //{
                    //    var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                    //    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length);
                    //    bmp.UnlockBits(bmpdata);
                    //    bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png");
                    //}
                }

                var glyphImg2 = atlasBuilder.BuildSingleImage();
                using (Bitmap bmp = new Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height),
                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                    int[] intBuffer = glyphImg2.GetImageBuffer();

                    System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length);
                    bmp.UnlockBits(bmpdata);
                    bmp.Save("d:\\WImageTest\\a_total.png");
                }
                atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml");
            }
        }
Ejemplo n.º 4
0
        List <ushort> inputGlyphs = new List <ushort>(); //not thread safe***
        public void Print(Typeface typeface, float size, char[] str, List <GlyphPlan> glyphPlanBuffer)
        {
            //1. layout
            _glyphLayout.Layout(typeface, size, str, glyphPlanBuffer);
            var glyphPathBuilder = new MyGlyphPathBuilder(typeface);
            int j = glyphPlanBuffer.Count;

            float pxScale = typeface.CalculateFromPointToPixelScale(size);

            for (int i = 0; i < j; ++i)
            {
                GlyphPlan glyphPlan = glyphPlanBuffer[i];
                //-----------------------------------
                //check if we static vxs/bmp for this glyph
                //if not, create and cache
                //-----------------------------------
                glyphPathBuilder.BuildFromGlyphIndex(glyphPlan.glyphIndex, size);
                //-----------------------------------
                var vxsBuilder = new GlyphPathBuilderVxs();
                glyphPathBuilder.ReadShapes(vxsBuilder);
                glyphPlan.vxs = vxsBuilder.GetVxs(pxScale);
            }
        }
Ejemplo n.º 5
0
        void RenderWithMiniAgg(Typeface typeface, char testChar, float sizeInPoint)
        {
            //----------------------------------------------------
            var builder  = new MyGlyphPathBuilder(typeface);
            var hintTech = (HintTechnique)cmbHintTechnique.SelectedItem;

            builder.UseTrueTypeInstructions = false; //reset
            builder.UseVerticalHinting      = false; //reset
            switch (hintTech)
            {
            case HintTechnique.TrueTypeInstruction:
                builder.UseTrueTypeInstructions = true;
                break;

            case HintTechnique.TrueTypeInstruction_VerticalOnly:
                builder.UseTrueTypeInstructions = true;
                builder.UseVerticalHinting      = true;
                break;

            case HintTechnique.CustomAutoFit:
                //custom agg autofit
                break;
            }
            //----------------------------------------------------

            builder.Build(testChar, sizeInPoint);

            var vxsShapeBuilder = new GlyphPathBuilderVxs();

            builder.ReadShapes(vxsShapeBuilder);
            VertexStore vxs = vxsShapeBuilder.GetVxs();

            p.UseSubPixelRendering = chkLcdTechnique.Checked;

            //5. use PixelFarm's Agg to render to bitmap...
            //5.1 clear background
            p.Clear(PixelFarm.Drawing.Color.White);

            if (chkFillBackground.Checked)
            {
                //5.2
                p.FillColor = PixelFarm.Drawing.Color.Black;
                //5.3
                if (!chkYGridFitting.Checked)
                {
                    p.Fill(vxs);
                }
            }
            if (chkBorder.Checked)
            {
                //5.4
                // p.StrokeWidth = 3;
                p.StrokeColor = PixelFarm.Drawing.Color.Green;
                //user can specific border width here...
                //p.StrokeWidth = 2;
                //5.5
                p.Draw(vxs);
            }


            float pxScale = builder.GetPixelScale();
            //1. autofit
            var autoFit = new GlyphAutoFit();

            autoFit.Hint(
                builder.GetOutputPoints(),
                builder.GetOutputContours(), pxScale);
            var vxsShapeBuilder2 = new GlyphPathBuilderVxs();

            autoFit.ReadOutput(vxsShapeBuilder2);
            VertexStore vxs2 = vxsShapeBuilder2.GetVxs();

            //
            p.FillColor = PixelFarm.Drawing.Color.Black;
            p.Fill(vxs2);

            if (chkShowTess.Checked)
            {
#if DEBUG
                debugDrawTriangulatedGlyph(autoFit.FitOutput, pxScale);
#endif
            }

            if (chkShowGrid.Checked)
            {
                //render grid
                RenderGrid(800, 600, _gridSize, p);
            }



            //6. use this util to copy image from Agg actual image to System.Drawing.Bitmap
            PixelFarm.Agg.Imaging.BitmapHelper.CopyToGdiPlusBitmapSameSize(destImg, winBmp);
            //---------------
            //7. just render our bitmap
            g.Clear(Color.White);
            g.DrawImage(winBmp, new Point(30, 20));
        }