Ejemplo n.º 1
0
        void TestLoadBitmapAtlas2(byte[] atlasInfo, byte[] atlasImg)
        {
            //bitmap atlas file


            _bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
            using (MemoryStream ms = new MemoryStream(atlasInfo))
            {
                _bitmapAtlas = _bmpAtlasBuilder.LoadAtlasInfo(ms)[0];//default atlas
            }

            _totalAtlasImg = LoadBmp2(atlasImg);

            //-----
            int count = _bitmapAtlas.ImgUrlDict.Count;

            listBox2.Items.Clear();

            foreach (var kv in _bitmapAtlas.ImgUrlDict)
            {
                listBox2.Items.Add(kv.Key);
            }

            DisposeExistingPictureBoxImage(pictureBox2);

            //save to file?
            string temp_file = Guid.NewGuid() + ".png";

            File.WriteAllBytes(temp_file, atlasImg);
            pictureBox2.Image = new Bitmap(temp_file);
        }
Ejemplo n.º 2
0
        public void Build(
            GlyphTextureBitmapGenerator glyphTextureGen,
            Typeface typeface, float fontSizeInPoints,
            TextureKind textureKind,
            GlyphTextureBuildDetail[] buildDetails)
        {
#if DEBUG
            //overall, glyph atlas generation time
            System.Diagnostics.Stopwatch dbugStopWatch = new System.Diagnostics.Stopwatch();
            dbugStopWatch.Start();
#endif
            var atlasBuilder = new SimpleBitmapAtlasBuilder();
            glyphTextureGen.CreateTextureFontFromBuildDetail(
                atlasBuilder,
                typeface,
                fontSizeInPoints,
                textureKind,
                buildDetails);

            //3. set information before write to font-info
            atlasBuilder.SpaceCompactOption = SimpleBitmapAtlasBuilder.CompactOption.ArrangeByHeight;
            atlasBuilder.SetAtlasFontInfo(typeface.Name, fontSizeInPoints);

            //4. merge all glyph in the builder into a single image
            using (MemBitmap totalGlyphsImg = atlasBuilder.BuildSingleImage(true))
            {
                if (TextureInfoFilename == null)
                {
                    //use random suffix
                    string random_suffix      = Guid.NewGuid().ToString().Substring(0, 7);
                    string textureName        = typeface.Name.ToLower() + "_" + random_suffix + ".info";
                    string output_imgFilename = textureName + ".png";

                    TextureInfoFilename = textureName;
                    OutputImgFilename   = output_imgFilename;
                }


                //5. save atlas info to disk
                using (FileStream fs = new FileStream(TextureInfoFilename, FileMode.Create))
                {
                    atlasBuilder.SaveAtlasInfo(fs);
                }

                //6. save total-glyph-image to disk
                totalGlyphsImg.SaveImage(OutputImgFilename);
            }

#if DEBUG
            dbugStopWatch.Stop();
            dbugBuildTimeMillisec = dbugStopWatch.ElapsedMilliseconds;
#endif
        }
Ejemplo n.º 3
0
        protected override void OnReadyForInitGLShaderProgram()
        {
            //just an example
            //this is slow on init.
            //since we must wait until msdf texture generation is complete.
            //in real-world, we should use caching.

            using (System.IO.FileStream fs = new System.IO.FileStream("Samples\\SourceSansPro-Regular.ttf", System.IO.FileMode.Open))
            {
                _typeface = new OpenFontReader().Read(fs);
            }

            var reqFont = new PixelFarm.Drawing.RequestFont("Source Sans Pro", 32);

            //1. create glyph-texture-bitmap generator
            var glyphTextureGen = new GlyphTextureBitmapGenerator();

            glyphTextureGen.MsdfGenVersion = 3;

            //2. generate the glyphs
            var atlasBuilder = new SimpleBitmapAtlasBuilder();

            glyphTextureGen.CreateTextureFontFromBuildDetail(
                atlasBuilder,
                _typeface,
                reqFont.SizeInPoints,
                TextureKind.Msdf,
                GlyphTextureCustomConfigs.TryGetGlyphTextureBuildDetail(
                    Typography.Text.GlobalTextService.TxtClient.ResolveFont(reqFont), false, false)
                );

            //3. set information before write to font-info

            atlasBuilder.SpaceCompactOption = SimpleBitmapAtlasBuilder.CompactOption.ArrangeByHeight;

            //4. merge all glyph in the builder into a single image
            PixelFarm.CpuBlit.MemBitmap totalGlyphsImg = atlasBuilder.BuildSingleImage(true);
            //-------------------------------------------------------------

            //5. create a simple font atlas from information inside this atlas builder.
            _fontAtlas = atlasBuilder.CreateSimpleBitmapAtlas();
            _fontAtlas.SetMainBitmap(totalGlyphsImg, true);

            byte[] codepoint = System.Text.Encoding.UTF8.GetBytes("AB");
            _glyphIndex_0 = _typeface.GetGlyphIndex(codepoint[0]);
            _glyphIndex_1 = _typeface.GetGlyphIndex(codepoint[1]);
        }
Ejemplo n.º 4
0
        public void LoadFontAtlasFile(string atlasInfo, string atlasImgFile)
        {
            _atlasInfo    = atlasInfo;
            _atlasImgFile = atlasImgFile;
            textBox1.Text = atlasImgFile;

            LoadAtlasImgFile(_atlasImgFile);

            //load img
            _atlasBuilder = new SimpleBitmapAtlasBuilder();
            List <SimpleBitmapAtlas> fontAtlasList = null;

            fontAtlasList = _atlasBuilder.LoadAtlasInfo(atlasInfo);


            int count = fontAtlasList.Count;

            treeView1.Nodes.Clear();

            for (int i = 0; i < count; ++i)
            {
                SimpleBitmapAtlas fontAtlas = fontAtlasList[i];

                TreeNode atlasNode = new TreeNode();
                atlasNode.Text = fontAtlas.FontName + ", count=" + fontAtlas.ItemDict.Count;

                treeView1.Nodes.Add(atlasNode);

                List <TempGlyphMap> tmpGlyphMaps = new List <TempGlyphMap>(fontAtlas.ItemDict.Count);
                foreach (var kv in fontAtlas.ItemDict)
                {
                    tmpGlyphMaps.Add(new TempGlyphMap {
                        glyphIndex = kv.Key, glyphMap = kv.Value
                    });
                }
                tmpGlyphMaps.Sort((a, b) => a.glyphIndex.CompareTo(b.glyphIndex));

                foreach (TempGlyphMap tmpGlyphMap in tmpGlyphMaps)
                {
                    TreeNode glyphMapNode = new TreeNode();
                    glyphMapNode.Tag  = tmpGlyphMap;
                    glyphMapNode.Text = glyphMapNode.Tag.ToString();
                    atlasNode.Nodes.Add(glyphMapNode);
                }
            }
            treeView1.ExpandAll();
        }
Ejemplo n.º 5
0
        void TestLoadBitmapAtlas(string atlas_file)
        {
            //bitmap atlas file


            _bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
            _bitmapAtlas     = _bmpAtlasBuilder.LoadAtlasInfo(atlas_file + ".info")[0];//default atlas

            _totalAtlasImg = LoadBmp(atlas_file + ".png");
            //-----
            int count = _bitmapAtlas.ImgUrlDict.Count;

            listBox2.Items.Clear();

            foreach (var kv in _bitmapAtlas.ImgUrlDict)
            {
                listBox2.Items.Add(kv.Key);
            }
            DisposeExistingPictureBoxImage(pictureBox2);
            pictureBox2.Image = new Bitmap(atlas_file + ".png");

            //for (int i = 0; i < count; ++i)
            //{
            //    if (bitmapAtlas.TryGetBitmapMapData((ushort)i, out BitmapMapData bmpMapData))
            //    {
            //        listBox2.Items.Add(bmpMapData);
            //        //test copy data from bitmap
            //        //MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
            //        //itemImg.SaveImage("test1_atlas_item" + i + ".png");
            //    }
            //}

            ////test,
            //{
            //    if (bitmapAtlas.TryGetBitmapMapData(@"\chk_checked.png", out BitmapMapData bmpMapData))
            //    {
            //        //MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
            //        //itemImg.SaveImage("test1_atlas_item_a.png");
            //    }
            //}
        }
Ejemplo n.º 6
0
        static void CreateSampleMsdfTextureFont(string fontfile, float sizeInPoint, ushort startGlyphIndex, ushort endGlyphIndex, string outputFile)
        {
            //sample

            var reader = new OpenFontReader();

            Typeface typeface = null;

            using (var fs = new FileStream(fontfile, FileMode.Open))
            {
                //1. read typeface from font file
                typeface = reader.Read(fs);
            }

            //sample: create sample msdf texture
            //-------------------------------------------------------------
            var builder = new GlyphOutlineBuilder(typeface);
            //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
            //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
            //-------------------------------------------------------------
            RequestFont reqFont = new RequestFont(typeface.Name, sizeInPoint);

            var atlasBuilder = new SimpleBitmapAtlasBuilder();

            atlasBuilder.FontFilename = System.IO.Path.GetFileName(fontfile);
            atlasBuilder.FontKey      = reqFont.FontKey;
            //create temp folder for each glyph

            string tempFolderName = "tmp_msdf";

            if (Directory.Exists(tempFolderName))
            {
                //DANGER!
                Directory.Delete(tempFolderName, true);
            }
            Directory.CreateDirectory(tempFolderName);

            if (endGlyphIndex < 1)
            {
                endGlyphIndex = (ushort)(typeface.GlyphCount - 1);
            }


            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)
                BitmapAtlasItemSource glyphImg = MsdfImageGen.CreateMsdfImageV1(glyphContourBuilder, genParams);
                glyphImg.UniqueInt16Name = gindex;
                atlasBuilder.AddItemSource(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(tempFolderName + "//glyph_" + gindex + ".png");
                }
            }

            MemBitmap glyphImg2 = atlasBuilder.BuildSingleImage(true);

            glyphImg2.SaveImage(outputFile);


            string saveToFile = "a_info.bin";

            using (System.IO.FileStream saveFs = new FileStream(saveToFile, FileMode.Create))
            {
                atlasBuilder.SaveAtlasInfo(saveFs);
                saveFs.Flush();
                saveFs.Close();
            }

            //
            //-----------
            //test read texture info back
            var atlasBuilder2 = new SimpleBitmapAtlasBuilder();

            using (System.IO.FileStream readFromFs = new FileStream(saveToFile, FileMode.Open))
            {
                var readbackFontAtlas = atlasBuilder2.LoadAtlasInfo(readFromFs);
            }
        }
Ejemplo n.º 7
0
        void BuildAtlas_FromInputChars()
        {
            if (!float.TryParse(txtSelectedFontSize.Text, out float fontSizeInPoints))
            {
                MessageBox.Show("err: selected font size " + txtSelectedFontSize.Text);
                return;
            }


            //1. create glyph-texture-bitmap generator
            var glyphTextureGen = new GlyphTextureBitmapGenerator();

            glyphTextureGen.SetSvgBmpBuilderFunc(SvgBuilderHelper.ParseAndRenderSvg);

            //2. generate the glyphs
            TextureKindAndDescription textureKindAndDesc = (TextureKindAndDescription)this.cmbTextureKind.SelectedItem;

            if (textureKindAndDesc.Kind == TextureKind.Msdf)
            {
                glyphTextureGen.MsdfGenVersion = textureKindAndDesc.TechniqueDetail;
            }

            var atlasBuilder = new SimpleBitmapAtlasBuilder();

            glyphTextureGen.CreateTextureFontFromInputChars(
                atlasBuilder,
                _typeface,
                fontSizeInPoints,
                textureKindAndDesc.Kind,
                GetUniqueChars()
                );

            //3. set information before write to font-info
            atlasBuilder.SpaceCompactOption = SimpleBitmapAtlasBuilder.CompactOption.ArrangeByHeight;


            //4. merge all glyph in the builder into a single image
            MemBitmap totalGlyphsImg = atlasBuilder.BuildSingleImage(true);
            string    fontTextureImg = "test_glyph_atlas.png";

            //5. save to png
            totalGlyphsImg.SaveImage(fontTextureImg);
            //-----------------------------------------------


            //let view result...
            SimpleUtils.DisposeExistingPictureBoxImage(picOutput);

            this.lblOutput.Text  = "output: " + fontTextureImg;
            this.picOutput.Image = new Bitmap(fontTextureImg);
#if DEBUG
            //save glyph image for debug
            //PixelFarm.Agg.ActualImage.SaveImgBufferToPngFile(
            //    totalGlyphsImg.GetImageBuffer(),
            //    totalGlyphsImg.Width * 4,
            //    totalGlyphsImg.Width, totalGlyphsImg.Height,
            //    "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());

            ///
        }
        public static void Test(string imgdir, Func <string, MemBitmap> imgLoader)
        {
            SimpleBitmapAtlasBuilder bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
            //test!
            int imgdirNameLen = imgdir.Length;

            string[] filenames = System.IO.Directory.GetFiles(imgdir, "*.png");
            ushort   index     = 0;

            Dictionary <string, ushort> imgDic = new Dictionary <string, ushort>();

            foreach (string f in filenames)
            {
                MemBitmap      itemBmp   = imgLoader(f);
                AtlasItemImage atlasItem = new AtlasItemImage(itemBmp.Width, itemBmp.Height);
                atlasItem.OriginalBounds = new PixelFarm.Drawing.RectangleF(0, 0, itemBmp.Width, itemBmp.Height);
                atlasItem.SetBitmap(itemBmp, false);
                //
                bmpAtlasBuilder.AddAtlasItemImage(index, atlasItem);
                string imgPath = f.Substring(imgdirNameLen);
                imgDic.Add(imgPath, index);
                index++;

                //------------
#if DEBUG
                if (index >= ushort.MaxValue)
                {
                    throw new NotSupportedException();
                }
#endif
                //------------
            }

            //string atlasInfoFile = "d:\\WImageTest\\test1_atlas.info";
            //string totalImgFile = "d:\\WImageTest\\test1_atlas.png";
            string atlasInfoFile = "test1_atlas.info";
            string totalImgFile  = "test1_atlas.png";
            //test, write data to disk
            AtlasItemImage totalImg = bmpAtlasBuilder.BuildSingleImage();
            bmpAtlasBuilder.ImgUrlDict = imgDic;
            bmpAtlasBuilder.SetAtlasInfo(TextureKind.Bitmap);
            bmpAtlasBuilder.SaveAtlasInfo(atlasInfoFile); //save to filename
            totalImg.Bitmap.SaveImage(totalImgFile);

            //-----
            //test, read data back
            bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
            SimpleBitmaptAtlas bitmapAtlas = bmpAtlasBuilder.LoadAtlasInfo(atlasInfoFile);
            //
            MemBitmap      totalAtlasImg = imgLoader(totalImgFile);
            AtlasItemImage atlasImg      = new AtlasItemImage(totalAtlasImg.Width, totalAtlasImg.Height);
            bitmapAtlas.TotalImg = atlasImg;

            for (int i = 0; i < index; ++i)
            {
                if (bitmapAtlas.TryGetBitmapMapData((ushort)i, out BitmapMapData bmpMapData))
                {
                    //test copy data from bitmap
                    MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                    itemImg.SaveImage("test1_atlas_item" + i + ".png");
                }
            }

            //test,
            {
                if (bitmapAtlas.TryGetBitmapMapData(@"\chk_checked.png", out BitmapMapData bmpMapData))
                {
                    MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                    itemImg.SaveImage("test1_atlas_item_a.png");
                }
            }
        }
Ejemplo n.º 9
0
        public static void BuildBitmapAtlas(AtlasProject atlasProj, Func <string, MemBitmap> imgLoader, bool test_extract = false)
        {
            //demonstrate how to build a bitmap atlas
            List <AtlasItemSourceFile> fileList = atlasProj.Items;
            //1. create builder
            var bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();

            ushort index = 0;
            Dictionary <string, ushort> imgDic = new Dictionary <string, ushort>();

            foreach (AtlasItemSourceFile f in fileList)
            {
                if (f.Kind != AtlasItemSourceKind.Image)
                {
                    continue;
                }

                //3. load a bitmap

                BitmapAtlasItemSource atlasItem = null;
                using (MemBitmap itemBmp = imgLoader(f.AbsoluteFilename))
                {
                    //4. get information about it
                    atlasItem = new BitmapAtlasItemSource(itemBmp.Width, itemBmp.Height);
                    atlasItem.SetImageBuffer(MemBitmap.CopyImgBuffer(itemBmp));
                }

                atlasItem.UniqueInt16Name = index;
                //5. add to builder
                bmpAtlasBuilder.AddItemSource(atlasItem);

                //get relative filename
                string imgPath = "//" + f.Link;
                imgDic.Add(imgPath, index);
                index++;

                //------------
#if DEBUG
                if (index >= ushort.MaxValue)
                {
                    throw new NotSupportedException();
                }
#endif
                //------------
            }
            if (imgDic.Count == 0)
            {
                //no file
                return;
            }

            string atlasInfoFile = atlasProj.OutputFilename + ".info";
            string totalImgFile  = atlasProj.OutputFilename + ".png";

            //5. merge all small images into a bigone
            using (MemBitmap totalImg = bmpAtlasBuilder.BuildSingleImage(false))
            {
                bmpAtlasBuilder.ImgUrlDict = imgDic;
                bmpAtlasBuilder.SetAtlasInfo(TextureKind.Bitmap, 0); //font size
                                                                     //6. save atlas info and total-img (.png file)
                bmpAtlasBuilder.SaveAtlasInfo(atlasInfoFile);
                totalImg.SaveImage(totalImgFile);
            }


            //----------------------
            //7. create an atlas file in a source file version, user can embed the source to file
            //easy, just read .info and .png then convert to binary buffer

            BuildAtlasInEmbededSourceVersion(atlasProj, atlasInfoFile, totalImgFile, imgDic);

            //----------------------
            //test, read data back
            //----------------------
            if (test_extract)
            {
                bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
                SimpleBitmapAtlas bitmapAtlas = bmpAtlasBuilder.LoadAtlasInfo(atlasInfoFile)[0];
                //
                MemBitmap totalAtlasImg = imgLoader(totalImgFile);
                bitmapAtlas.SetMainBitmap(imgLoader(totalImgFile), true);

                //-----
                for (int i = 0; i < index; ++i)
                {
                    if (bitmapAtlas.TryGetItem((ushort)i, out AtlasItem bmpMapData))
                    {
                        //test copy data from bitmap
                        MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                        itemImg.SaveImage("test1_atlas_item" + i + ".png");
                    }
                }
                //test,
                {
                    if (bitmapAtlas.TryGetItem(@"\chk_checked.png", out AtlasItem bmpMapData))
                    {
                        MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                        itemImg.SaveImage("test1_atlas_item_a.png");
                    }
                }
            }
        }