Example #1
0
        void AddExistingOrCreateNewSimpleFontAtlas(
            MultiSizeFontAtlasBuilder multisizeFontAtlasBuilder,
            RequestFont reqFont,
            BitmapFontManager <MemBitmap> bmpFontMx)
        {
            int fontKey = reqFont.FontKey;

            string fontTextureFile        = reqFont.Name + "_" + fontKey;
            string resolveFontTextureFile = fontTextureFile + ".info";
            string fontTextureInfoFile    = resolveFontTextureFile;
            string fontTextureImgFilename = fontTextureInfoFile + ".png";

            _textServices.ResolveTypeface(reqFont); //resolve for 'actual' font
            if (PixelFarm.Platforms.StorageService.Provider.DataExists(resolveFontTextureFile) &&
                File.Exists(fontTextureImgFilename))
            {
                multisizeFontAtlasBuilder.AddSimpleFontAtlasFile(reqFont,
                                                                 resolveFontTextureFile,
                                                                 fontTextureImgFilename,
                                                                 bmpFontMx.TextureKindForNewFont
                                                                 );
            }
            else
            {
                //create a new one
                PixelFarm.Drawing.Fonts.SimpleFontAtlas fontAtlas = bmpFontMx.GetFontAtlas(reqFont, out MemBitmap fontBmp);
                bmpFontMx.GetFontAtlas(reqFont, out fontBmp);
                multisizeFontAtlasBuilder.AddSimpleFontAtlasFile(reqFont,
                                                                 resolveFontTextureFile,
                                                                 fontTextureImgFilename,
                                                                 bmpFontMx.TextureKindForNewFont);
            }
        }
Example #2
0
 public TextureFontFace(FontFace primFontFace, SimpleFontAtlas fontAtlas)
 {
     //for msdf font
     //1 font atlas may support mutliple font size
     this.fontAtlas    = fontAtlas;
     this.primFontFace = primFontFace;
 }
        public static FontFace LoadFont(string fontfile,
                                        ScriptLang scriptLang,
                                        WriteDirection writeDirection,
                                        out SimpleFontAtlas fontAtlas)
        {
            //1. read font info
            ManagedFontFace openFont = (ManagedFontFace)OpenFontLoader.LoadFont(fontfile, scriptLang, writeDirection);

            //2. build texture font on the fly! OR load from prebuilt file
            //
            //2.1 test build texture on the fly
            SimpleFontAtlasBuilder atlas1    = CreateSampleMsdfTextureFont(fontfile, 14, 0, 255);
            GlyphImage             glyphImg2 = atlas1.BuildSingleImage();

            fontAtlas            = atlas1.CreateSimpleFontAtlas();
            fontAtlas.TotalGlyph = glyphImg2;

            //string xmlFontFileInfo = "";
            //GlyphImage glyphImg = null;
            //MySimpleFontAtlasBuilder atlasBuilder = new MySimpleFontAtlasBuilder();
            //SimpleFontAtlas fontAtlas = atlasBuilder.LoadFontInfo(xmlFontFileInfo);
            //glyphImg = atlasBuilder.BuildSingleImage(); //we can create a new glyph or load from prebuilt file
            //fontAtlas.TotalGlyph = glyphImg;


            var textureFontFace = new TextureFontFace(openFont, fontAtlas);

            return(textureFontFace);
        }
Example #4
0
        internal TextureFont(TextureFontFace typeface, float sizeInPoints)
        {
            this.typeface = typeface;
            this.fontAtlas = typeface.FontAtlas;
            actualFont = typeface.InnerFontFace.GetFontAtPointsSize(sizeInPoints);

        }
Example #5
0
        public static FontFace LoadFont(
            Typeface typeface,
            TextureFontCreationParams creationParams,
            out SimpleFontAtlas fontAtlas)
        {
            //1. read font info
            NOpenFontFace openFont = (NOpenFontFace)OpenFontLoader.LoadFont(typeface, creationParams.scriptLang, creationParams.writeDirection);

            //------------------------
            SimpleFontAtlasBuilder atlas1 = null;

            switch (creationParams.textureKind)
            {
            default: throw new System.NotSupportedException();

            case TextureKind.AggSubPixel:
                atlas1 = CreateAggSubPixelRenderingTextureFont(
                    typeface,
                    creationParams.originalFontSizeInPoint,
                    creationParams.hintTechnique,
                    GetGlyphIndexIter(typeface, creationParams.langBits)
                    );
                break;

            case TextureKind.AggGrayScale:
                atlas1 = CreateAggTextureFont(
                    typeface,
                    creationParams.originalFontSizeInPoint,
                    creationParams.hintTechnique,
                    GetGlyphIndexIter(typeface, creationParams.langBits)
                    );
                break;

            case TextureKind.Msdf:
                atlas1 = CreateSampleMsdfTextureFont(
                    typeface,
                    creationParams.originalFontSizeInPoint,
                    creationParams.hintTechnique,
                    GetGlyphIndexIter(typeface, creationParams.langBits)
                    );
                break;
            }

            GlyphImage glyphImg2 = atlas1.BuildSingleImage();

            fontAtlas            = atlas1.CreateSimpleFontAtlas();
            fontAtlas.TotalGlyph = glyphImg2;

            //string xmlFontFileInfo = "";
            //GlyphImage glyphImg = null;
            //MySimpleFontAtlasBuilder atlasBuilder = new MySimpleFontAtlasBuilder();
            //SimpleFontAtlas fontAtlas = atlasBuilder.LoadFontInfo(xmlFontFileInfo);
            //glyphImg = atlasBuilder.BuildSingleImage(); //we can create a new glyph or load from prebuilt file
            //fontAtlas.TotalGlyph = glyphImg;

            return(openFont);
            //var textureFontFace = new TextureFontFace(openFont, fontAtlas);
            //return textureFontFace;
        }
 public void ChangeFont(RequestFont font)
 {
     //call to service
     _font = font;
     _textServices.ResolveTypeface(font); //resolve for 'actual' font
     _fontAtlas       = _bmpFontMx.GetFontAtlas(_font, out _fontBmp);
     FontSizeInPoints = font.SizeInPoints;
 }
Example #7
0
 public TextureFontFace(FontFace nOpenTypeFontFace, string xmlFontInfo, GlyphImage glyphImg)
 {
     //for msdf font
     //1 font atlas may support mutliple font size 
     atlasBuilder = new SimpleFontAtlasBuilder();
     fontAtlas = atlasBuilder.LoadFontInfo(xmlFontInfo);
     fontAtlas.TotalGlyph = glyphImg;
     this.nOpenTypeFontFace = nOpenTypeFontFace; 
 }
Example #8
0
 public static FontFace LoadFont(
     string fontfile,
     TextureFontCreationParams creationParams,
     out SimpleFontAtlas fontAtlas)
 {
     using (FileStream fs = new FileStream(fontfile, FileMode.Open, FileAccess.Read))
     {
         var      reader   = new OpenFontReader();
         Typeface typeface = reader.Read(fs);
         return(LoadFont(typeface, creationParams, out fontAtlas));
     }
 }
Example #9
0
        public void Read(Stream inputStream)
        {
            //custom font atlas file
            _simpleFontAtlasList = new List <SimpleFontAtlas>();
            using (BinaryReader reader = new BinaryReader(inputStream, System.Text.Encoding.UTF8))
            {
                //1. version
                ushort fileversion = reader.ReadUInt16();
                bool   stop        = false;
                int    listCount   = 0;

                while (!stop)
                {
                    //2. read object kind
                    FontTextureObjectKind objKind = (FontTextureObjectKind)reader.ReadUInt16();
                    switch (objKind)
                    {
                    default: throw new NotSupportedException();

                    case FontTextureObjectKind.OverviewMultiSizeFontInfo:
                        listCount = reader.ReadUInt16();
                        break;

                    case FontTextureObjectKind.OverviewFontInfo:
                        //start new atlas
                        _atlas = new SimpleFontAtlas();
                        _simpleFontAtlasList.Add(_atlas);
                        ReadOverviewFontInfo(reader);
                        break;

                    case FontTextureObjectKind.End:
                        stop = true;
                        break;

                    case FontTextureObjectKind.GlyphList:
                        ReadGlyphList(reader);
                        break;

                    case FontTextureObjectKind.TotalImageInfo:
                        ReadTotalImageInfo(reader);
                        break;
                    }
                }
            }
        }
Example #10
0
 internal TextureFont(TextureFontFace typeface, float sizeInPoints)
 {
     this.typeface  = typeface;
     this.fontAtlas = typeface.FontAtlas;
     actualFont     = typeface.InnerFontFace.GetFontAtPointsSize(sizeInPoints);
 }
Example #11
0
        public void BuildMultiFontSize(string multiFontSizrAtlasFilename, string imgOutputFilename)
        {
            //merge to the new one
            //1. ensure same atlas width
            int atlasW      = 0;
            int j           = _simpleFontInfoList.Count;
            int totalHeight = 0;

            const int interAtlasSpace = 2;

            for (int i = 0; i < j; ++i)
            {
                SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i];
                SimpleFontAtlas     fontAtlas = atlasInfo.fontAtlasFile.ResultSimpleFontAtlasList[0];
                totalHeight += fontAtlas.Height + interAtlasSpace;
                if (i == 0)
                {
                    atlasW = fontAtlas.Width;
                }
                else
                {
                    if (atlasW != fontAtlas.Width)
                    {
                        throw new NotSupportedException();
                    }
                }
            }
            //--------------------------------------------
            //in this version, the glyph offsetY is measure from bottom***
            int[] offsetFromBottoms = new int[j];
            int   offsetFromBottom  = interAtlasSpace;//start offset

            for (int i = j - 1; i >= 0; --i)
            {
                SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i];
                SimpleFontAtlas     fontAtlas = atlasInfo.fontAtlasFile.ResultSimpleFontAtlasList[0];
                offsetFromBottoms[i] = offsetFromBottom;
                offsetFromBottom    += fontAtlas.Height + interAtlasSpace;
            }
            //--------------------------------------------
            //merge all img to one
            int top = 0;

            using (PixelFarm.CpuBlit.MemBitmap memBitmap = new CpuBlit.MemBitmap(atlasW, totalHeight))
            {
                PixelFarm.CpuBlit.AggPainter painter = PixelFarm.CpuBlit.AggPainter.Create(memBitmap);
                for (int i = 0; i < j; ++i)
                {
                    SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i];
                    FontAtlasFile       atlasFile = atlasInfo.fontAtlasFile;
                    SimpleFontAtlas     fontAtlas = atlasInfo.fontAtlasFile.ResultSimpleFontAtlasList[0];

                    atlasInfo.NewCloneLocations = SimpleFontAtlas.CloneLocationWithOffset(fontAtlas, 0, offsetFromBottoms[i]);

                    using (System.IO.Stream fontImgStream = PixelFarm.Platforms.StorageService.Provider.ReadDataStream(atlasInfo.imgFile))
                        using (PixelFarm.CpuBlit.MemBitmap atlasBmp = PixelFarm.CpuBlit.MemBitmap.LoadBitmap(fontImgStream))
                        {
                            painter.DrawImage(atlasBmp, 0, top);
                            top += atlasBmp.Height + interAtlasSpace;
                        }
                }
                memBitmap.SaveImage(imgOutputFilename);
            }
            //--------------------------------------------
            //save merged font atlas
            //TODO: use 'File' provider to access system file
            using (FileStream fs = new FileStream(multiFontSizrAtlasFilename, FileMode.Create))
                using (BinaryWriter w = new BinaryWriter(fs))
                {
                    //-----------
                    //overview
                    //total img info
                    FontAtlasFile fontAtlasFile = new FontAtlasFile();
                    fontAtlasFile.StartWrite(fs);

                    //1. simple atlas count
                    fontAtlasFile.WriteOverviewMultiSizeFontInfo((ushort)j);
                    //2.
                    for (int i = 0; i < j; ++i)
                    {
                        SimpleFontAtlasInfo atlasInfo = _simpleFontInfoList[i];
                        RequestFont         reqFont   = atlasInfo.reqFont;
                        fontAtlasFile.WriteOverviewFontInfo(reqFont.Name, reqFont.FontKey, reqFont.SizeInPoints);//size in points

                        fontAtlasFile.WriteTotalImageInfo(
                            (ushort)atlasW,
                            (ushort)top,
                            4,
                            atlasInfo.textureKind);
                        //
                        //
                        fontAtlasFile.WriteGlyphList(atlasInfo.NewCloneLocations);
                    }
                    fontAtlasFile.EndWrite();
                }
        }
Example #12
0
        public static Dictionary <ushort, TextureGlyphMapData> CloneLocationWithOffset(SimpleFontAtlas org, int dx, int dy)
        {
            Dictionary <ushort, TextureGlyphMapData> cloneDic = new Dictionary <ushort, TextureGlyphMapData>();

            foreach (var kp in org._glyphLocations)
            {
                TextureGlyphMapData orgMapData = kp.Value;
                cloneDic.Add(kp.Key,
                             new TextureGlyphMapData()
                {
                    Left = orgMapData.Left + dx,
                    Top  = orgMapData.Top + dy,
                    //
                    Width          = orgMapData.Width,
                    Height         = orgMapData.Height,
                    TextureXOffset = orgMapData.TextureXOffset,
                    TextureYOffset = orgMapData.TextureYOffset
                });
            }
            return(cloneDic);
        }