public SimpleBitmapAtlas CreateSimpleBitmapAtlas()
        {
            SimpleBitmapAtlas atlas = new SimpleBitmapAtlas
            {
                TextureKind = this.TextureKind,
                FontName    = this.FontName,
                SizeInPts   = this.FontSizeInPoints
            };

            foreach (BitmapAtlasItemSource src in _items.Values)
            {
                Rectangle area = src.Area;

                atlas.AddAtlasItem(new AtlasItem(src.UniqueInt16Name)
                {
                    Width  = src.Width,
                    Left   = area.X,
                    Top    = area.Top,
                    Height = area.Height,

                    TextureXOffset = src.TextureXOffset,
                    TextureYOffset = src.TextureYOffset
                });
            }

            return(atlas);
        }
 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 #3
0
        public void Read(Stream inputStream)
        {
            //custom font atlas file
            AtlasList = new List <SimpleBitmapAtlas>();
            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
                    ObjectKind objKind = (ObjectKind)reader.ReadUInt16();
                    switch (objKind)
                    {
                    default: throw new NotSupportedException();

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

                    case ObjectKind.OverviewFontInfo:
                        //start new atlas
                        _atlas = new SimpleBitmapAtlas();
                        AtlasList.Add(_atlas);
                        ReadOverviewFontInfo(reader);
                        break;

                    case ObjectKind.FontScriptTags:
                        ReadScriptTags(reader);
                        break;

                    case ObjectKind.End:
                        stop = true;
                        break;

                    case ObjectKind.GlyphList:
                        ReadAtlasItems(reader);
                        break;

                    case ObjectKind.TotalImageInfo:
                        ReadTotalImageInfo(reader);
                        break;

                    case ObjectKind.ImgUrlDic:
                        ReadImgUrlDict(reader);
                        break;
                    }
                }
            }
        }
Example #4
0
        public static Dictionary <ushort, AtlasItem> CloneLocationWithOffset(SimpleBitmapAtlas org, int dx, int dy)
        {
            Dictionary <ushort, AtlasItem> cloneDic = new Dictionary <ushort, AtlasItem>();

            foreach (var kp in org._atlasItems)
            {
                AtlasItem orgMapData = kp.Value;
                cloneDic.Add(kp.Key, new AtlasItem(orgMapData.UniqueUint16Name)
                {
                    Left = orgMapData.Left + dx,
                    Top  = orgMapData.Top + dy,
                    //
                    Width          = orgMapData.Width,
                    Height         = orgMapData.Height,
                    TextureXOffset = orgMapData.TextureXOffset,
                    TextureYOffset = orgMapData.TextureYOffset
                });
            }
            return(cloneDic);
        }
Example #5
0
        public void AddSimpleFontAtlas(SimpleBitmapAtlas[] simpleFontAtlases, System.IO.Stream totalGlyphImgStream)
        {
            //multiple font atlas that share the same glyphImg

            MemBitmap mainBmp = ReadGlyphImages(totalGlyphImgStream);

            for (int i = 0; i < simpleFontAtlases.Length; ++i)
            {
                SimpleBitmapAtlas simpleFontAtlas = simpleFontAtlases[i];
                simpleFontAtlas.SetMainBitmap(mainBmp, true);
                simpleFontAtlas.UseSharedImage = true;
                _createdAtlases.Add(simpleFontAtlas.FontKey, simpleFontAtlas);

                if (simpleFontAtlas.TextureKind == TextureKind.Msdf)
                {
                    //if we have msdf texture
                    //then we can use this to do autoscale
                    _msdfTextureFonts.Add(simpleFontAtlas.FontFilename, simpleFontAtlas);
                }
            }
        }
Example #6
0
 public void RegisterBitmapAtlas(string atlasName, byte[] atlasInfoBuffer, byte[] totalImgBuffer)
 {
     //direct register atlas
     //instead of loading it from file
     if (!_createdAtlases.ContainsKey(atlasName))
     {
         SimpleBitmapAtlasBuilder atlasBuilder = new SimpleBitmapAtlasBuilder();
         using (System.IO.Stream fontAtlasTextureInfo = new MemoryStream(atlasInfoBuffer))
             using (System.IO.Stream fontImgStream = new MemoryStream(totalImgBuffer))
             {
                 try
                 {
                     List <SimpleBitmapAtlas> atlasList  = atlasBuilder.LoadAtlasInfo(fontAtlasTextureInfo);
                     SimpleBitmapAtlas        foundAtlas = atlasList[0];
                     foundAtlas.SetMainBitmap(MemBitmap.LoadBitmap(fontImgStream), true);
                     _createdAtlases.Add(atlasName, foundAtlas);
                 }
                 catch (Exception ex)
                 {
                     throw ex;
                 }
             }
     }
 }
Example #7
0
        public void BuildMultiFontSize(string multiFontSizeAtlasFilename, string imgOutputFilename)
        {
            //merge to the new one
            //1. ensure same atlas width
            int atlasW      = 0;
            int j           = _atlasList.Count;
            int totalHeight = 0;

            const int interAtlasSpace = 2;

            for (int i = 0; i < j; ++i)
            {
                TempMergingAtlasInfo atlasInfo = _atlasList[i];
                SimpleBitmapAtlas    fontAtlas = atlasInfo.fontAtlasFile.AtlasList[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)
            {
                TempMergingAtlasInfo atlasInfo = _atlasList[i];
                SimpleBitmapAtlas    fontAtlas = atlasInfo.fontAtlasFile.AtlasList[0];
                offsetFromBottoms[i] = offsetFromBottom;
                offsetFromBottom    += fontAtlas.Height + interAtlasSpace;
            }
            //--------------------------------------------
            //merge all img to one
            int top = 0;

            using (MemBitmap memBitmap = new MemBitmap(atlasW, totalHeight))
            {
                AggPainter painter = AggPainter.Create(memBitmap);
                for (int i = 0; i < j; ++i)
                {
                    TempMergingAtlasInfo atlasInfo = _atlasList[i];
                    BitmapAtlasFile      atlasFile = atlasInfo.fontAtlasFile;
                    SimpleBitmapAtlas    fontAtlas = atlasInfo.fontAtlasFile.AtlasList[0];

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

                    using (Stream fontImgStream = PixelFarm.Platforms.StorageService.Provider.ReadDataStream(atlasInfo.imgFile))
                        using (MemBitmap atlasBmp = MemBitmapExt.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(multiFontSizeAtlasFilename, FileMode.Create))
                using (BinaryWriter w = new BinaryWriter(fs))
                {
                    //-----------
                    //overview
                    //total img info
                    BitmapAtlasFile fontAtlasFile = new BitmapAtlasFile();
                    fontAtlasFile.StartWrite(fs);

                    //1. simple atlas count
                    fontAtlasFile.WriteOverviewMultiSizeFontInfo((ushort)j);
                    //2.
                    for (int i = 0; i < j; ++i)
                    {
                        TempMergingAtlasInfo atlasInfo = _atlasList[i];

                        RequestFont reqFont = atlasInfo.reqFont;
                        fontAtlasFile.WriteOverviewFontInfo(reqFont.Name, reqFont.GetReqKey(), reqFont.SizeInPoints);//size in points

                        fontAtlasFile.WriteTotalImageInfo(
                            (ushort)atlasW,
                            (ushort)top,
                            4,
                            atlasInfo.textureKind);
                        //

                        fontAtlasFile.WriteAtlasItems(atlasInfo.NewCloneLocations);

                        if (atlasInfo.ImgUrlDict != null)
                        {
                            fontAtlasFile.WriteImgUrlDict(atlasInfo.ImgUrlDict);
                        }
                    }
                    fontAtlasFile.EndWrite();
                }
        }