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
        }
Exemple #2
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]);
        }