void AddExistingOrCreateNewSimpleFontAtlas( MultiGlyphSizeBitmapAtlasBuilder 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.AddSimpleAtlasFile(reqFont, resolveFontTextureFile, fontTextureImgFilename, bmpFontMx.TextureKindForNewFont ); } else { //create a new one SimpleBitmapAtlas fontAtlas = bmpFontMx.GetFontAtlas(reqFont, out MemBitmap fontBmp); bmpFontMx.GetFontAtlas(reqFont, out fontBmp); multisizeFontAtlasBuilder.AddSimpleAtlasFile(reqFont, resolveFontTextureFile, fontTextureImgFilename, bmpFontMx.TextureKindForNewFont); } }
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); }
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]); }
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(); }
public override void Init() { //steps : detail ... //1. create a text service (or get it from a singleton class) _textServices = new OpenFontTextService(); _txtClient = _textServices.CreateNewServiceClient(); //2. create manager _bmpFontMx = new BitmapFontManager <MemBitmap>( _textServices, atlas => MemBitmap.CreateFromCopy(atlas.MainBitmap) ); //3. _font = new RequestFont("tahoma", 16); _fontAtlas = _bmpFontMx.GetFontAtlas(_textServices.ResolveFont(_font), _font.StartCodePoint, _font.EndCodePoint, out _fontBmp); }
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"); // } //} }
public void DrawImage(GLPainter glPainter, AtlasImageBinder atlasImgBinder, float left, float top) { if (atlasImgBinder.State == BinderState.Loaded && atlasImgBinder.LatestPainterId != _painterId) { atlasImgBinder.State = BinderState.Unload; atlasImgBinder.LatestPainterId = _painterId; } switch (atlasImgBinder.State) { case BinderState.Loaded: { if (PixelFarm.Drawing.ImageBinder.GetCacheInnerImage(atlasImgBinder) is GLBitmap glbmp) { AtlasItem atlasItem = atlasImgBinder.AtlasItem; Rectangle srcRect = new Rectangle(atlasItem.Left, atlasItem.Top, //diff from font atlas*** atlasItem.Width, atlasItem.Height); switch (atlasImgBinder.TextureKind) { default: case TextureKind.Msdf: throw new NotSupportedException(); case TextureKind.Bitmap: { //atlasImgBinder.State = BinderState.Loaded; //ImageBinder.SetCacheInnerImage(atlasImgBinder, glbmp, false); //atlasImgBinder.AtlasItem = atlasItem; glPainter.Core.DrawSubImage(glbmp, srcRect, left, top); } break; } } } break; case BinderState.Unload: { atlasImgBinder.LatestPainterId = _painterId; //load img first if (_bmpAtlas == null || _lastestImgFile != atlasImgBinder.AtlasName) { _bmpAtlas = _atlasManager.GetBitmapAtlas(atlasImgBinder.AtlasName, out _glBmp); if (_bmpAtlas == null) { //error atlasImgBinder.State = BinderState.Error; //not found return; } _lastestImgFile = atlasImgBinder.AtlasName; } //-------- if (_bmpAtlas.TryGetItem(atlasImgBinder.ImageName, out AtlasItem atlasItem)) { //found map data Rectangle srcRect = new Rectangle(atlasItem.Left, atlasItem.Top, //diff from font atlas*** atlasItem.Width, atlasItem.Height); TextureKind textureKind = _bmpAtlas.TextureKind; switch (textureKind) { default: case TextureKind.Msdf: throw new NotSupportedException(); case TextureKind.Bitmap: { atlasImgBinder.State = BinderState.Loaded; ImageBinder.SetCacheInnerImage(atlasImgBinder, _glBmp, false); atlasImgBinder.AtlasItem = atlasItem; atlasImgBinder.SetPreviewImageSize(atlasItem.Width, atlasItem.Height); atlasImgBinder.RaiseImageChanged(); atlasImgBinder.TextureKind = textureKind; glPainter.Core.DrawSubImage(_glBmp, srcRect, left, top); } break; } } else { atlasImgBinder.State = BinderState.Error; //not found } } break; } #if DEBUG if (atlasImgBinder.State == BinderState.Unload) { } #endif }
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"); } } } }