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); } }
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; }
/// <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); }