Example #1
0
        static void CreateSampleMsdfTextureFont(
            string fontfile, float sizeInPoint,
            char[] chars, 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 GlyphPathBuilder(typeface);
                //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
                //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
                //-------------------------------------------------------------
                var atlasBuilder = new SimpleFontAtlasBuilder();

                MsdfGenParams msdfGenParams = new MsdfGenParams();

                int j = chars.Length;
                for (int i = 0; i < j; ++i)
                {
                    //build glyph
                    ushort gindex = typeface.LookupIndex(chars[i]);
                    builder.BuildFromGlyphIndex(gindex, -1);

                    var glyphToContour = new GlyphContourBuilder();
                    //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours());
                    builder.ReadShapes(glyphToContour);
                    msdfGenParams.shapeScale = 1f / 64;
                    GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, msdfGenParams);
                    atlasBuilder.AddGlyph(gindex, glyphImg);
                    int w = glyphImg.Width;
                    int h = glyphImg.Height;
                    using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    {
                        var   bmpdata   = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                        int[] imgBuffer = glyphImg.GetImageBuffer();
                        System.Runtime.InteropServices.Marshal.Copy(imgBuffer, 0, bmpdata.Scan0, imgBuffer.Length);
                        bmp.UnlockBits(bmpdata);
                        bmp.Save("d:\\WImageTest\\a001_xn2_" + (chars[i]) + ".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");
            }
        }
        public FontAtlasTextPrinter(AggPainter painter)
        {
            StartDrawOnLeftTop = true;
            this._painter      = painter;

            this.PositionTechnique = PositionTechnique.OpenFont;

            _textServices = new LayoutFarm.OpenFontTextService();
            //2. create manager
            _bmpFontMx = new BitmapFontManager <ActualBitmap>(
                TextureKind.StencilLcdEffect,
                _textServices,
                atlas =>
            {
                GlyphImage totalGlyphImg = atlas.TotalGlyph;
                return(new ActualBitmap(totalGlyphImg.Width, totalGlyphImg.Height, totalGlyphImg.GetImageBuffer()));
            }
                );
            _bmpFontMx.SetCurrentScriptLangs(new ScriptLang[]
            {
                ScriptLangs.Latin
            });

            //3.

            ChangeFont(new RequestFont("tahoma", 10));
            SetupMaskPixelBlender(painter.Width, painter.Height);
        }
Example #3
0
        static void BuildFontGlyphs(PixelFarm.Drawing.RequestFont font,
                                    Typography.Rendering.SimpleFontAtlasBuilder atlasBuilder,
                                    int startAt, int endAt)
        {
            //font glyph for specific font face
            ActualFont nativeFont = GetActualFont(font.Name, font.SizeInPoints);// nativeFontStore.GetResolvedNativeFont(font);

            for (int i = startAt; i <= endAt; ++i)
            {
                char      c         = (char)i;
                FontGlyph fontGlyph = nativeFont.GetGlyph(c);
                //-------------------
                GlyphImage glyphImg = NativeMsdfGen.BuildMsdfFontImage(fontGlyph);

                // Console.WriteLine(c.ToString() + " ox,oy" + glyphImg.OffsetX + "," + glyphImg.OffsetY);

                int   w      = glyphImg.Width;
                int   h      = glyphImg.Height;
                int[] buffer = glyphImg.GetImageBuffer();
                NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
                glyphImg.SetImageBuffer(buffer, false);
                // atlasBuilder.AddGlyph(fontGlyph.glyphMatrix.u c, fontGlyph, glyphImg);
                //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_x1_" + (int)c + ".png");
                //}
            }
        }
 static void SaveImgBufferToFile(GlyphImage glyphImg, string filename)
 {
     int[] intBuffer = glyphImg.GetImageBuffer();
     using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
     {
         unsafe
         {
             fixed(int *head = &intBuffer[0])
             {
                 CopyToGdiPlusBitmapSameSize((IntPtr)head, newBmp);
             }
         }
         //save
         newBmp.Save(filename);
     }
 }
Example #5
0
        private void button11_Click(object sender, EventArgs e)
        {
            unsafe
            {
                GlyphImage g1 = BuildMsdfFontImage();

                int[] buffer = g1.GetImageBuffer();
                using (Bitmap bmp = new Bitmap(g1.Width, g1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, g1.Width, g1.Height), 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_xn1_.png");
                }
            }
        }
        /// <summary>
        /// test only, shapen org image with Paint.net sharpen filter
        /// </summary>
        /// <param name="org"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        static GlyphImage Sharpen(GlyphImage org, int radius)
        {
            GlyphImage newImg = new GlyphImage(org.Width, org.Height);

            Agg.Imaging.ShapenFilterPdn sharpen1 = new Agg.Imaging.ShapenFilterPdn();
            int[] orgBuffer = org.GetImageBuffer();
            unsafe
            {
                fixed(int *orgHeader = &orgBuffer[0])
                {
                    int[] output = sharpen1.Sharpen(orgHeader, org.Width, org.Height, org.Width * 4, radius);
                    newImg.SetImageBuffer(output, org.IsBigEndian);
                }
            }

            return(newImg);
        }
Example #7
0
 public MySimpleGLBitmapFontManager(TextureKind textureKind, LayoutFarm.OpenFontTextService textServices)
     : base(textureKind, textServices)
 {
     SetLoadNewBmpDel(atlas =>
     {
         //create new one
         GlyphImage totalGlyphImg = atlas.TotalGlyph;
         //load to glbmp
         GLBitmap found = new GLBitmap(
             PixelFarm.CpuBlit.MemBitmap.CreateFromCopy(
                 totalGlyphImg.Width,
                 totalGlyphImg.Height,
                 totalGlyphImg.GetImageBuffer()),
             true); //set true=> glbmp is the original owner of the membmp, when glbmp is disposed => the membmp is disposed too
         found.IsYFlipped = false;
         return(found);
     });
 }
Example #8
0
        static void CreateSampleMsdfImg(ContourBuilder tx, string outputFile)
        {
            //sample

            MsdfGenParams msdfGenParams = new MsdfGenParams();
            GlyphImage    glyphImg      = MsdfGlyphGen.CreateMsdfImage(tx, msdfGenParams);
            int           w             = glyphImg.Width;
            int           h             = glyphImg.Height;

            using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                var   bmpdata   = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                int[] imgBuffer = glyphImg.GetImageBuffer();
                System.Runtime.InteropServices.Marshal.Copy(imgBuffer, 0, bmpdata.Scan0, imgBuffer.Length);
                bmp.UnlockBits(bmpdata);
                bmp.Save(outputFile);
            }
        }
Example #9
0
        private void button5_Click(object sender, EventArgs e)
        {
            //1. load font
            string fontName = "tahoma";
            string fontfile = "c:\\Windows\\Fonts\\tahoma.ttf";
            //string fontfile = @"D:\WImageTest\THSarabunNew\THSarabunNew.ttf";
            ActualFont font = GetActualFont(fontfile, 18);// nativeFontStore.LoadFont(fontName, fontfile, 28);
            //2. get glyph
            var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();

            //for (int i = 0; i < 256; ++i)
            //BuildFontGlyphsByIndex(font, atlasBuilder, 0, 255);
            //BuildFontGlyphs(font, atlasBuilder, 0x0e00, 0x0e5b);
            //BuildFontGlyphsByIndex(font, atlasBuilder, 0, 3417);
            //BuildFontGlyphsByIndex(font, atlasBuilder, 0, 509);
            //BuildFontGlyphsByIndex(font, atlasBuilder, 97, 97);
            BuildFontGlyph(font, atlasBuilder, 'B');
            //----------------------------------------------------
            //GlyphImage totalImg = atlasBuilder.BuildSingleImage();
            GlyphImage totalImg = atlasBuilder.BuildSingleImage();

            using (Bitmap bmp = new Bitmap(totalImg.Width, totalImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                int[] buffer = totalImg.GetImageBuffer();
                if (totalImg.IsBigEndian)
                {
                    NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
                    totalImg.SetImageBuffer(buffer, false);
                }

                var bmpdata = bmp.LockBits(
                    new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height),
                    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\\a_total.png");
            }

            string fontfilename = "d:\\WImageTest\\a_total.xml";

            atlasBuilder.SaveFontInfo(fontfilename);
            //----------------------------------
        }
Example #10
0
        private void button1_Click(object sender, EventArgs e)
        {
            //1.
            MsdfGenParams msdfGenParams = new MsdfGenParams();

            //GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(tx, msdfGenParams);
            Msdfgen.Shape shape1 = new Msdfgen.Shape();
            //
            Msdfgen.Contour cnt = new Msdfgen.Contour();
            //cnt.AddLine(0, 0, 50, 0);
            //cnt.AddLine(50, 0, 50, 50);
            //cnt.AddLine(50, 50, 0, 50);
            //cnt.AddLine(0, 50, 0, 0);
            //cnt.AddLine(10, 20, 50, 0);
            //cnt.AddLine(50, 0, 80, 20);
            //cnt.AddLine(80, 20, 50, 60);
            //cnt.AddLine(50, 60, 10, 20);

            //for msdf we draw shape clock-wise
            cnt.AddLine(10, 20, 50, 60);
            cnt.AddLine(50, 60, 80, 20);
            cnt.AddLine(80, 20, 50, 0);
            cnt.AddLine(50, 0, 10, 20);
            shape1.contours.Add(cnt);
            //
            //
            var        genParams = new MsdfGenParams();
            GlyphImage glyphImg  = MsdfGlyphGen.CreateMsdfImage(shape1, genParams);

            using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                int[] buffer = glyphImg.GetImageBuffer();

                var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg.Width, glyphImg.Height), 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\\msdf_shape.png");
                //
            }
        }
        static void SaveImgBufferToFile(GlyphImage glyphImg, string filename)
        {
            //-------------
            int[] intBuffer = glyphImg.GetImageBuffer();
            using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
            {
                int imgW = glyphImg.Width;
                int imgH = glyphImg.Height;

                Hjg.Pngcs.ImageInfo imgInfo = new Hjg.Pngcs.ImageInfo(imgW, imgH, 8, true); //8 bits per channel with alpha
                Hjg.Pngcs.PngWriter writer  = new Hjg.Pngcs.PngWriter(fs, imgInfo);
                Hjg.Pngcs.ImageLine iline   = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE);
                int startReadAt             = 0;

                int imgStride = imgW * 4;

                int srcIndex        = 0;
                int srcIndexRowHead = intBuffer.Length - imgW;

                for (int row = 0; row < imgH; row++)
                {
                    byte[] scanlineBuffer = iline.ScanlineB;
                    srcIndex = srcIndexRowHead;
                    for (int b = 0; b < imgStride;)
                    {
                        int srcInt = intBuffer[srcIndex];
                        srcIndex++;
                        scanlineBuffer[b]     = (byte)((srcInt >> 16) & 0xff);
                        scanlineBuffer[b + 1] = (byte)((srcInt >> 8) & 0xff);
                        scanlineBuffer[b + 2] = (byte)((srcInt) & 0xff);
                        scanlineBuffer[b + 3] = (byte)((srcInt >> 24) & 0xff);
                        b += 4;
                    }
                    srcIndexRowHead -= imgW;
                    startReadAt     += imgStride;
                    writer.WriteRow(iline, row);
                }
                writer.End();
            }
        }
Example #12
0
        static void BuildFontGlyph(ActualFont nativefont, Typography.Rendering.SimpleFontAtlasBuilder atlasBuilder, char c)
        {
            //font glyph for specific font face



            FontGlyph  fontGlyph = nativefont.GetGlyph(c);
            GlyphImage glyphImg  = NativeMsdfGen.BuildMsdfFontImage(fontGlyph);

            int w = glyphImg.Width;
            int h = glyphImg.Height;

            int[] buffer = glyphImg.GetImageBuffer();
            NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
            glyphImg.SetImageBuffer(buffer, false);
            atlasBuilder.AddGlyph(0, glyphImg);
            //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_x1_" + (int)c + ".png");
            //}
        }
Example #13
0
        private void button6_Click(object sender, EventArgs e)
        {
            //1. load font
            ActualFont font = GetActualFont("tahoma", 28);// nativeFontStore.LoadFont("tahoma", 28);
            //2. get glyph

            var g1 = font.GetGlyph('C');

            var plans = new List <Typography.TextLayout.GlyphPlan>();

            PixelFarm.Drawing.Text.TextShapingService.GetGlyphPos(font, "ABC".ToCharArray(), 0, 3, plans);


            int[] glyphIndice = new int[] { 1076, 1127, 1164 };
            int   j           = glyphIndice.Length;

            var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();

            for (int i = 0; i < j; ++i)
            {
                int       codepoint = glyphIndice[i];
                FontGlyph fontGlyph = font.GetGlyphByIndex((uint)codepoint);

                GlyphImage glyphImg = NativeMsdfGen.BuildMsdfFontImage(fontGlyph);
                int        w        = glyphImg.Width;
                int        h        = glyphImg.Height;
                int[]      buffer   = glyphImg.GetImageBuffer();
                NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
                glyphImg.SetImageBuffer(buffer, false);
                atlasBuilder.AddGlyph(codepoint, glyphImg);

                using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.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_y1_" + codepoint + ".png");
                }
            }
            //----------------------------------------------------
            GlyphImage totalImg = atlasBuilder.BuildSingleImage();

            using (Bitmap bmp = new Bitmap(totalImg.Width, totalImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                int[] buffer = totalImg.GetImageBuffer();
                if (totalImg.IsBigEndian)
                {
                    NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
                    totalImg.SetImageBuffer(buffer, false);
                }

                var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), 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\\a_total.png");
            }

            string fontfilename = "d:\\WImageTest\\a_total.xml";

            atlasBuilder.SaveFontInfo(fontfilename);
            //----------------------------------
        }
        public FontAtlasTextPrinter(AggPainter painter)
        {
            StartDrawOnLeftTop = true;
            _painter           = painter;

            this.PositionTechnique = PositionTechnique.OpenFont;
            _textServices          = new LayoutFarm.OpenFontTextService();

            //2.
            _bmpFontMx = new BitmapFontManager <MemBitmap>(
                _textServices,
                atlas =>
            {
                GlyphImage totalGlyphImg = atlas.TotalGlyph;
                if (atlas.UseSharedGlyphImage)
                {
                    if (!_sharedGlyphImgs.TryGetValue(totalGlyphImg, out MemBitmap found))
                    {
                        found = MemBitmap.CreateFromCopy(totalGlyphImg.Width, totalGlyphImg.Height, totalGlyphImg.GetImageBuffer());
                        _sharedGlyphImgs.Add(totalGlyphImg, found);
                    }
                    return(found);
                }
                else
                {
                    return(MemBitmap.CreateFromCopy(totalGlyphImg.Width, totalGlyphImg.Height, totalGlyphImg.GetImageBuffer()));
                }
            }
                );

            //3.
            ChangeFont(painter.CurrentFont);
            SetupMaskPixelBlender(painter.Width, painter.Height);
        }
Example #15
0
        private void button3_Click(object sender, EventArgs e)
        {
            //1. load font
            string     fontName      = "tahoma";
            int        fontSizeInPts = 28;
            ActualFont font          = GetActualFont(fontName, fontSizeInPts);

            //2. get glyph
            char[] fontChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
            int    j         = fontChars.Length;


            var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();

            atlasBuilder.SetAtlasInfo(TextureKind.Msdf, fontSizeInPts);

            for (int i = 0; i < j; ++i)
            {
                char c = fontChars[i];

                FontGlyph  fontGlyph = font.GetGlyph(c);
                GlyphImage glyphImg  = NativeMsdfGen.BuildMsdfFontImage(fontGlyph);

                int   w      = glyphImg.Width;
                int   h      = glyphImg.Height;
                int[] buffer = glyphImg.GetImageBuffer();
                NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
                glyphImg.SetImageBuffer(buffer, false);
                atlasBuilder.AddGlyph(0, glyphImg);

                using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    var bmpdata = bmp.LockBits(
                        new System.Drawing.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_x1_" + (int)c + ".png");
                }
            }
            //----------------------------------------------------
            GlyphImage totalImg = atlasBuilder.BuildSingleImage();

            using (Bitmap bmp = new Bitmap(totalImg.Width, totalImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                int[] buffer = totalImg.GetImageBuffer();
                if (totalImg.IsBigEndian)
                {
                    NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer);
                    totalImg.SetImageBuffer(buffer, false);
                }

                var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), 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\\a_total.png");
            }

            string fontfilename = "d:\\WImageTest\\a_total.xml";

            atlasBuilder.SaveFontInfo(fontfilename);
            //----------------------------------
        }
Example #16
0
        public override void Init()
        {
            //steps : detail ...
            //1. create a text service (or get it from a singleton class)

            _textServices = new LayoutFarm.OpenFontTextService();

            //2. create manager
            _bmpFontMx = new BitmapFontManager <MemBitmap>(
                TextureKind.StencilLcdEffect,
                _textServices,
                atlas =>
            {
                GlyphImage totalGlyphImg = atlas.TotalGlyph;
                return(MemBitmap.CreateFromCopy(totalGlyphImg.Width, totalGlyphImg.Height, totalGlyphImg.GetImageBuffer()));
            }
                );


            //3.
            _font      = new RequestFont("tahoma", 10);
            _fontAtlas = _bmpFontMx.GetFontAtlas(_font, out _fontBmp);


            //----------
        }
Example #17
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            PixelFarm.CpuBlit.MemBitmapExtensions.DefaultMemBitmapIO = new PixelFarm.Drawing.WinGdi.GdiBitmapIO();


            var storageProvider = new LocalFileStorageProvider("", true);

            PixelFarm.Platforms.StorageService.RegisterProvider(storageProvider);


            var bmpFontMx = new BitmapFontManager <MemBitmap>(
                _textServices,
                atlas =>
            {
                GlyphImage totalGlyphImg = atlas.TotalGlyph;
                return(MemBitmap.CreateFromCopy(totalGlyphImg.Width, totalGlyphImg.Height, totalGlyphImg.GetImageBuffer()));
            }
                );

            string multiSizeFontAtlasFilename = "tahoma_set1.multisize_fontAtlas";
            string totalImgAtlasFilename      = "tahoma_set1.multisize_fontAtlas.png";
            //in this version, mutlsize font texture must use the same typeface
            {
                MultiSizeFontAtlasBuilder multiSizeFontAtlasBuilder = new MultiSizeFontAtlasBuilder();
                {
                    bmpFontMx.TextureKindForNewFont = PixelFarm.Drawing.BitmapAtlas.TextureKind.StencilLcdEffect;
                    AddExistingOrCreateNewSimpleFontAtlas(multiSizeFontAtlasBuilder, new RequestFont("tahoma", 10), bmpFontMx);
                    AddExistingOrCreateNewSimpleFontAtlas(multiSizeFontAtlasBuilder, new RequestFont("tahoma", 11), bmpFontMx);
                    AddExistingOrCreateNewSimpleFontAtlas(multiSizeFontAtlasBuilder, new RequestFont("tahoma", 12), bmpFontMx);
                    AddExistingOrCreateNewSimpleFontAtlas(multiSizeFontAtlasBuilder, new RequestFont("tahoma", 13), bmpFontMx);
                    AddExistingOrCreateNewSimpleFontAtlas(multiSizeFontAtlasBuilder, new RequestFont("tahoma", 14), bmpFontMx);

                    bmpFontMx.TextureKindForNewFont = PixelFarm.Drawing.BitmapAtlas.TextureKind.Msdf;
                    AddExistingOrCreateNewSimpleFontAtlas(multiSizeFontAtlasBuilder, new RequestFont("tahoma", 24), bmpFontMx);
                }

                //-------
                multiSizeFontAtlasBuilder.BuildMultiFontSize(multiSizeFontAtlasFilename, totalImgAtlasFilename);
            }
            {
                //test load the font altas back
                FontAtlasFile atlasFile = new FontAtlasFile();
                using (FileStream fs = new FileStream(multiSizeFontAtlasFilename, FileMode.Open))
                {
                    atlasFile.Read(fs);
                }
            }
        }
Example #18
0
        void RenderWithMsdfImg(Typeface typeface, char testChar, float sizeInPoint)
        {
            painter.FillColor = PixelFarm.Drawing.Color.Black;
            //p.UseSubPixelRendering = chkLcdTechnique.Checked;
            painter.Clear(PixelFarm.Drawing.Color.White);
            //----------------------------------------------------
            var builder = new GlyphPathBuilder(typeface);

            builder.SetHintTechnique(_glyphRenderOptions.HintTechnique);

            //----------------------------------------------------
            builder.Build(testChar, sizeInPoint);
            //----------------------------------------------------
            var glyphToContour = new GlyphContourBuilder();
            var msdfGenPars    = new MsdfGenParams();

            builder.ReadShapes(glyphToContour);
            //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours());
            MsdfGenParams genParams = new MsdfGenParams();
            GlyphImage    glyphImg  = MsdfGlyphGen.CreateMsdfImage(glyphToContour, genParams);

            var actualImg = ActualImage.CreateFromBuffer(glyphImg.Width, glyphImg.Height, PixelFormat.ARGB32, glyphImg.GetImageBuffer());

            painter.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 (_contourAnalysisOpts.ShowGrid)
            {
                //render grid
                RenderGrids(800, 600, _gridSize, painter);
            }

            //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));
        }
Example #19
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 GlyphPathBuilder(typeface);
                //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
                //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
                //-------------------------------------------------------------
                var atlasBuilder = new SimpleFontAtlasBuilder();


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

                    var glyphContourBuilder = new ContourBuilder();
                    //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours());
                    var genParams = new MsdfGenParams();
                    builder.ReadShapes(new GlyphContourBuilder2(glyphContourBuilder));
                    //genParams.shapeScale = 1f / 64; //we scale later (as original C++ code use 1/64)
                    GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphContourBuilder, genParams);
                    atlasBuilder.AddGlyph(gindex, glyphImg);

                    using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    {
                        int[] buffer = glyphImg.GetImageBuffer();

                        var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg.Width, glyphImg.Height), 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_" + gindex + ".png");
                    }
                }

                GlyphImage 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(outputFile);
                }

                string saveToFile = "d:\\WImageTest\\a_info.bin";
                using (System.IO.FileStream saveFs = new FileStream(saveToFile, FileMode.Create))
                {
                    atlasBuilder.SaveFontInfo(saveFs);
                    saveFs.Flush();
                    saveFs.Close();
                }

                //
                //-----------
                //test read texture info back
                var atlasBuilder2 = new SimpleFontAtlasBuilder();
                using (System.IO.FileStream readFromFs = new FileStream(saveToFile, FileMode.Open))
                {
                    var readbackFontAtlas = atlasBuilder2.LoadFontAtlasInfo(readFromFs);
                }
            }
        }
Example #20
0
        private void cmdTestFontAtlas_Click(object sender, EventArgs e)
        {
            //read string from txtSampleChars
            //please make sure all are unique. (TODO: check it)
            //then create a font atlas from the sample chars

            char[] sampleChars = txtSampleChars.Text.ToCharArray();
            if (sampleChars.Length == 0)
            {
                return;
            }
            //

            GlyphImage             totalGlyphsImg = null;
            SimpleFontAtlasBuilder atlasBuilder   = null;
            var glyphTextureGen = new GlyphTextureBitmapGenerator();
            //
            Typeface typeface         = _basicOptions.Typeface;
            float    fontSizeInPoints = _basicOptions.FontSizeInPoints;

            //
            glyphTextureGen.CreateTextureFontFromInputChars(
                typeface,
                fontSizeInPoints,
                TextureKind.StencilLcdEffect,
                sampleChars,
                (glyphIndex, glyphImage, outputAtlasBuilder) =>
            {
                if (outputAtlasBuilder != null)
                {
                    //finish
                    atlasBuilder = outputAtlasBuilder;
                }
            }
                );

            atlasBuilder.SpaceCompactOption = SimpleFontAtlasBuilder.CompactOption.ArrangeByHeight;
            totalGlyphsImg = atlasBuilder.BuildSingleImage();
            string fontTextureImg = "d:\\WImageTest\\test_glyph_atlas.png";


            //create atlas
            SimpleFontAtlas fontAtlas = atlasBuilder.CreateSimpleFontAtlas();

            fontAtlas.TotalGlyph = totalGlyphsImg;
            //
            using (MemBitmap memBmp = MemBitmap.CreateFromCopy(totalGlyphsImg.Width, totalGlyphsImg.Height, totalGlyphsImg.GetImageBuffer()))
                using (System.Drawing.Bitmap bmp = new Bitmap(memBmp.Width, memBmp.Height))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, memBmp.Width, memBmp.Height),
                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    var tmpMem = MemBitmap.GetBufferPtr(memBmp);
                    unsafe
                    {
                        PixelFarm.CpuBlit.NativeMemMx.MemCopy((byte *)bmpdata.Scan0,
                                                              (byte *)tmpMem.Ptr,
                                                              tmpMem.LengthInBytes);
                    }
                    bmp.UnlockBits(bmpdata);
                    bmp.Save(fontTextureImg);
                }


#if DEBUG
            //save glyph image for debug
            //PixelFarm.Agg.ActualImage.SaveImgBufferToPngFile(
            //    totalGlyphsImg.GetImageBuffer(),
            //    totalGlyphsImg.Width * 4,
            //    totalGlyphsImg.Width, totalGlyphsImg.Height,
            //    "d:\\WImageTest\\total_" + reqFont.Name + "_" + reqFont.SizeInPoints + ".png");
            ////save image to cache
            //SaveImgBufferToFile(totalGlyphsImg, fontTextureImg);
#endif

            //cache the atlas
            //_createdAtlases.Add(fontKey, fontAtlas);
            ////
            ////calculate some commonly used values
            //fontAtlas.SetTextureScaleInfo(
            //    resolvedTypeface.CalculateScaleToPixelFromPointSize(fontAtlas.OriginalFontSizePts),
            //    resolvedTypeface.CalculateScaleToPixelFromPointSize(reqFont.SizeInPoints));
            ////TODO: review here, use scaled or unscaled values
            //fontAtlas.SetCommonFontMetricValues(
            //    resolvedTypeface.Ascender,
            //    resolvedTypeface.Descender,
            //    resolvedTypeface.LineGap,
            //    resolvedTypeface.CalculateRecommendLineSpacing());

            ///
        }