/// <summary> /// get from cache or create a new one /// </summary> /// <param name="reqFont"></param> /// <returns></returns> public SimpleFontAtlas GetFontAtlas(RequestFont reqFont, out GLBitmap glBmp) { int fontKey = reqFont.FontKey; SimpleFontAtlas fontAtlas; if (!_createdAtlases.TryGetValue(fontKey, out fontAtlas)) { Typeface resolvedTypeface = textServices.ResolveTypeface(reqFont); //if we don't have //the create it SimpleFontAtlasBuilder atlasBuilder = null; var textureGen = new GlyphTextureBitmapGenerator(); textureGen.CreateTextureFontFromScriptLangs( resolvedTypeface, reqFont.SizeInPoints, _textureKind, _currentScriptLangs, (glyphIndex, glyphImage, outputAtlasBuilder) => { if (outputAtlasBuilder != null) { //finish atlasBuilder = outputAtlasBuilder; } } ); GlyphImage totalGlyphsImg = atlasBuilder.BuildSingleImage(); //create atlas fontAtlas = atlasBuilder.CreateSimpleFontAtlas(); fontAtlas.TotalGlyph = totalGlyphsImg; #if DEBUG //save glyph image for debug //PixelFarm.Agg.ActualImage.SaveImgBufferToPngFile( // totalGlyphsImg.GetImageBuffer(), // totalGlyphsImg.Width * 4, // totalGlyphsImg.Width, totalGlyphsImg.Height, // "d:\\WImageTest\\total_" + reqFont.Name + "_" + reqFont.SizeInPoints + ".png"); #endif //cache the atlas _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()); } glBmp = _loadedGlyphs.GetOrCreateNewOne(fontAtlas); return(fontAtlas); }
private void cmdMakeFromScriptLangs_Click(object sender, EventArgs e) { //create a simple stencil texture font //string sampleFontFile = "../../../TestFonts/tahoma.ttf"; if (this.textBox1.Text == null || _typeface == null) { return; } if (SelectedScriptLangs.Count == 0) { return; } // string sampleFontFile = _typeface.Filename ?? ""; TextureKind selectedTextureKind = (TextureKind)lstTextureType.SelectedItem; char[] chars = this.textBox1.Text.ToCharArray(); string bitmapImgSaveFileName = "d:\\WImageTest\\sample_" + selectedTextureKind + "_" + System.IO.Path.GetFileNameWithoutExtension(sampleFontFile); bool saveEachGlyphSeparatly = chkSaveEachGlyph.Checked; var textureGen = new GlyphTextureBitmapGenerator(); textureGen.CreateTextureFontFromScriptLangs( _typeface, FontSizeInPoints, selectedTextureKind, SelectedScriptLangs.ToArray(), (gindex, glyphImg, atlasBuilder) => { if (atlasBuilder != null) { atlasBuilder.CompactGlyphSpace = chkCompactGlyphSpace.Checked; GlyphImage totalGlyphs = atlasBuilder.BuildSingleImage(); SaveImgBufferToFile(totalGlyphs, bitmapImgSaveFileName + ".png"); atlasBuilder.SaveFontInfo(bitmapImgSaveFileName + ".xml"); MessageBox.Show("glyph gen " + bitmapImgSaveFileName); } else { //save each glyph if (saveEachGlyphSeparatly) { SaveImgBufferToFile(glyphImg, bitmapImgSaveFileName + "_" + gindex + ".png"); } } }); }
/// <summary> /// get from cache or create a new one /// </summary> /// <param name="reqFont"></param> /// <returns></returns> public SimpleFontAtlas GetFontAtlas(RequestFont reqFont, out B outputBitmap) { #if DEBUG _dbugStopWatch.Reset(); _dbugStopWatch.Start(); #endif int fontKey = reqFont.FontKey; SimpleFontAtlas fontAtlas; if (!_createdAtlases.TryGetValue(fontKey, out fontAtlas)) { //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 fontTextureImg = fontTextureInfoFile + ".png"; //check if the file exist if (StorageService.Provider.DataExists(fontTextureInfoFile)) { SimpleFontAtlasBuilder atlasBuilder2 = new SimpleFontAtlasBuilder(); using (System.IO.Stream dataStream = StorageService.Provider.ReadDataStream(fontTextureInfoFile)) { try { fontAtlas = atlasBuilder2.LoadFontInfo(dataStream); fontAtlas.TotalGlyph = ReadGlyphImages(fontTextureImg); fontAtlas.OriginalFontSizePts = reqFont.SizeInPoints; _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()); } catch (Exception ex) { throw ex; } } } else { GlyphImage totalGlyphsImg = null; SimpleFontAtlasBuilder atlasBuilder = null; var textureGen = new GlyphTextureBitmapGenerator(); textureGen.CreateTextureFontFromScriptLangs( resolvedTypeface, reqFont.SizeInPoints, _textureKind, _textureBuildDetails, (glyphIndex, glyphImage, outputAtlasBuilder) => { if (outputAtlasBuilder != null) { //finish atlasBuilder = outputAtlasBuilder; } } ); atlasBuilder.SpaceCompactOption = SimpleFontAtlasBuilder.CompactOption.ArrangeByHeight; totalGlyphsImg = atlasBuilder.BuildSingleImage(); //if (reqFont.SizeInPoints == 14 && cacheImg != null) //{ // totalGlyphsImg = cacheImg; //} //totalGlyphsImg = Sharpen(totalGlyphsImg, 1); //test shapen primary image //- // //create atlas fontAtlas = atlasBuilder.CreateSimpleFontAtlas(); fontAtlas.TotalGlyph = totalGlyphsImg; #if DEBUG //save glyph image for debug //PixelFarm.Agg.ActualImage.SaveImgBufferToPngFile( // totalGlyphsImg.GetImageBuffer(), // totalGlyphsImg.Width * 4, // totalGlyphsImg.Width, totalGlyphsImg.Height, // "d:\\WImageTest\\total_" + reqFont.Name + "_" + reqFont.SizeInPoints + ".png"); ////save image to cache SaveImgBufferToFile(totalGlyphsImg, fontTextureImg); #endif //cache the atlas _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 //save font info to cache using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { atlasBuilder.SaveFontInfo(ms); System.IO.File.WriteAllBytes(fontTextureInfoFile, ms.ToArray()); StorageService.Provider.SaveData(fontTextureInfoFile, ms.ToArray()); } } } outputBitmap = _loadedGlyphs.GetOrCreateNewOne(fontAtlas); return(fontAtlas); }
private void cmdMakeFromScriptLangs_Click(object sender, EventArgs e) { //create a simple stencil texture font //string sampleFontFile = "../../../TestFonts/tahoma.ttf"; if (this.textBox1.Text == null || _typeface == null) { return; } if (SelectedScriptLangs.Count == 0) { return; } // string sampleFontFile = _typeface.Filename ?? ""; TextureKind selectedTextureKind = (TextureKind)lstTextureType.SelectedItem; char[] chars = this.textBox1.Text.ToCharArray(); string bitmapImgSaveFileName = "d:\\WImageTest\\sample_" + selectedTextureKind + "_" + System.IO.Path.GetFileNameWithoutExtension(sampleFontFile); //------------ //TODO: review here again, this is not a good way, //it is only temp fix for Tahoma. var _textureBuildDetails = new GlyphTextureBuildDetail[] { new GlyphTextureBuildDetail { ScriptLang = ScriptLangs.Latin, DoFilter = false, HintTechnique = Typography.Contours.HintTechnique.TrueTypeInstruction_VerticalOnly }, new GlyphTextureBuildDetail { OnlySelectedGlyphIndices = new char[] { 'x', 'X', '7' }, DoFilter = true, HintTechnique = Typography.Contours.HintTechnique.TrueTypeInstruction_VerticalOnly }, // new GlyphTextureBuildDetail { ScriptLang = ScriptLangs.Thai, DoFilter = true, HintTechnique = Typography.Contours.HintTechnique.None }, }; //------------ bool saveEachGlyphSeparatly = chkSaveEachGlyph.Checked; var textureGen = new GlyphTextureBitmapGenerator(); textureGen.CreateTextureFontFromScriptLangs( _typeface, FontSizeInPoints, selectedTextureKind, _textureBuildDetails, (gindex, glyphImg, atlasBuilder) => { if (atlasBuilder != null) { atlasBuilder.CompactGlyphSpace = chkCompactGlyphSpace.Checked; GlyphImage totalGlyphs = atlasBuilder.BuildSingleImage(); SaveImgBufferToFile(totalGlyphs, bitmapImgSaveFileName + ".png"); using (System.IO.FileStream fs = new System.IO.FileStream(bitmapImgSaveFileName + ".xml", System.IO.FileMode.Create)) { atlasBuilder.SaveAtlasInfo(fs); } MessageBox.Show("glyph gen " + bitmapImgSaveFileName); } else { //save each glyph if (saveEachGlyphSeparatly) { SaveImgBufferToFile(glyphImg, bitmapImgSaveFileName + "_" + gindex + ".png"); } } }); }