/// <summary> /// get from cache or create a new one /// </summary> /// <param name="reqFont"></param> /// <returns></returns> public SimpleBitmapAtlas GetBitmapAtlas(string atlasName, out B outputBitmap) { #if DEBUG _dbugStopWatch.Reset(); _dbugStopWatch.Start(); #endif if (!_createdAtlases.TryGetValue(atlasName, out SimpleBitmapAtlas foundAtlas)) { //check from pre-built cache (if availiable) string textureInfoFile = atlasName + ".info"; string textureImgFilename = atlasName + ".png"; //check if the file exist if (StorageService.Provider.DataExists(textureInfoFile) && StorageService.Provider.DataExists(textureImgFilename)) { SimpleBitmapAtlasBuilder atlasBuilder = new SimpleBitmapAtlasBuilder(); using (System.IO.Stream fontAtlasTextureInfo = StorageService.Provider.ReadDataStream(textureInfoFile)) using (System.IO.Stream fontImgStream = StorageService.Provider.ReadDataStream(textureImgFilename)) { try { List <SimpleBitmapAtlas> atlasList = atlasBuilder.LoadAtlasInfo(fontAtlasTextureInfo); foundAtlas = atlasList[0]; foundAtlas.SetMainBitmap(MemBitmap.LoadBitmap(fontImgStream), true); _createdAtlases.Add(atlasName, foundAtlas); } catch (Exception ex) { throw ex; } } } } if (foundAtlas != null) { outputBitmap = _loadAtlases.GetOrCreateNewOne(foundAtlas); return(foundAtlas); } else { #if DEBUG //show warning about this System.Diagnostics.Debug.WriteLine("not found atlas:" + atlasName); #endif outputBitmap = default(B); return(null); } }
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; } } } }
/// <summary> /// get from cache or create a new one /// </summary> /// <param name="reqFont"></param> /// <returns></returns> public SimpleBitmapAtlas GetFontAtlas(RequestFont reqFont, out B outputBitmap) { #if DEBUG _dbugStopWatch.Reset(); _dbugStopWatch.Start(); #endif int fontKey = reqFont.FontKey; if (_createdAtlases.TryGetValue(fontKey, out SimpleBitmapAtlas fontAtlas)) { outputBitmap = _loadAtlases.GetOrCreateNewOne(fontAtlas); return(fontAtlas); } //check if we have small msdf texture or not if (_msdfTextureFonts.TryGetValue(reqFont.Name, out SimpleBitmapAtlas msdfTexture)) { //use this outputBitmap = _loadAtlases.GetOrCreateNewOne(msdfTexture); return(msdfTexture); } //-------------------------------- //check from pre-built cache (if availiable) Typeface resolvedTypeface = _textServices.ResolveTypeface(reqFont); string fontTextureFile = reqFont.Name + "_" + fontKey; string resolveFontFile = fontTextureFile + ".info"; string fontTextureInfoFile = resolveFontFile; string fontTextureImgFilename = fontTextureInfoFile + ".png"; if (StorageService.Provider.DataExists(fontTextureInfoFile) && StorageService.Provider.DataExists(fontTextureImgFilename)) { //check local caching, if found then load-> create it SimpleBitmapAtlasBuilder atlasBuilder = new SimpleBitmapAtlasBuilder(); lock (s_loadDataLock) { using (System.IO.Stream textureInfoFileStream = StorageService.Provider.ReadDataStream(fontTextureInfoFile)) using (System.IO.Stream fontAtlasImgStream = StorageService.Provider.ReadDataStream(fontTextureImgFilename)) { try { //TODO: review here fontAtlas = atlasBuilder.LoadAtlasInfo(textureInfoFileStream)[0]; fontAtlas.SetMainBitmap(ReadGlyphImages(fontAtlasImgStream), true); fontAtlas.OriginalFontSizePts = reqFont.SizeInPoints; _createdAtlases.Add(fontKey, fontAtlas); } catch (Exception ex) { throw ex; } } } } else { //------------- //if not found the request font //we generate it realtime here, (add add the cache '_createdAtlases') //------------- //1. create glyph-texture-bitmap generator var glyphTextureGen = new GlyphTextureBitmapGenerator(); //2. generate the glyphs SimpleBitmapAtlasBuilder atlasBuilder = glyphTextureGen.CreateTextureFontFromBuildDetail( resolvedTypeface, reqFont.SizeInPoints, TextureKindForNewFont, GlyphTextureCustomConfigs.TryGetGlyphTextureBuildDetail(reqFont, false, false) ); //3. set information before write to font-info atlasBuilder.FontFilename = reqFont.Name;//TODO: review here, check if we need 'filename' or 'fontname' atlasBuilder.FontKey = reqFont.FontKey; 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); #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 totalGlyphsImg.SaveImage(fontTextureImgFilename); #endif //6. cache this in the memory, _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()); /// #if DEBUG _dbugStopWatch.Stop(); System.Diagnostics.Debug.WriteLine("build font atlas: " + _dbugStopWatch.ElapsedMilliseconds + " ms"); #endif //TODO: review here again //save font info to local disk cache using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { atlasBuilder.SaveAtlasInfo(ms); StorageService.Provider.SaveData(fontTextureInfoFile, ms.ToArray()); #if DEBUG //write temp debug info #if !__MOBILE__ System.IO.File.WriteAllBytes(fontTextureInfoFile, ms.ToArray()); System.IO.File.WriteAllText(fontTextureInfoFile + ".txt", reqFont.Name + ",size" + reqFont.SizeInPoints + "pts"); #endif #endif } } outputBitmap = _loadAtlases.GetOrCreateNewOne(fontAtlas); return(fontAtlas); }