public static void BuildFontAtlas(AtlasProject atlasProj) { foreach (AtlasItemSourceFile atlasSourceFile in atlasProj.Items) { if (atlasSourceFile.Kind == AtlasItemSourceKind.FontAtlasConfig && atlasSourceFile.FontBuilderConfig != null) { FontBuilderConfig config = atlasSourceFile.FontBuilderConfig; foreach (FontBuilderTask builderTask in config.BuilderTasks) { //1. create glyph-texture-bitmap generator var glyphTextureGen = new GlyphTextureBitmapGenerator(); glyphTextureGen.SetSvgBmpBuilderFunc(SvgBuilderHelper.ParseAndRenderSvg); //2. generate the glyphs if (builderTask.TextureKind == TextureKind.Msdf) { glyphTextureGen.MsdfGenVersion = 3; } Typography.OpenFont.Typeface typeface = atlasProj.GetTypeface(config.FontFilename); //TODO: add other font styles RequestFont reqFont = new RequestFont(typeface.Name, builderTask.Size, FontStyle.Regular); string textureName = typeface.Name.ToLower() + "_" + reqFont.FontKey; string outputDir = Path.GetDirectoryName(atlasProj.OutputFilename); FontAtlasBuilderHelper builderHelper = new FontAtlasBuilderHelper(); builderHelper.TextureInfoFilename = outputDir + Path.DirectorySeparatorChar + textureName; builderHelper.OutputImgFilename = outputDir + Path.DirectorySeparatorChar + textureName + ".png"; builderHelper.Build(glyphTextureGen, typeface, builderTask.Size, builderTask.TextureKind, builderTask.TextureBuildDetails.ToArray(), reqFont.FontKey ); } } } }
//----------------------------------------------------------------------------------------------- void BuiltAtlas_FromUserOptions(List <GlyphTextureBuildDetail> buildDetails) { //we can create font atlas by specific script-langs //------------------------------------------------------------------------------- //setting... Typeface typeface = _typeface; if (!float.TryParse(txtSelectedFontSize.Text, out float fontSizeInPoints)) { MessageBox.Show("err: selected font size " + txtSelectedFontSize.Text); return; } //create request font, indeed we need its 'FontKey' PixelFarm.Drawing.RequestFont reqFont = new PixelFarm.Drawing.RequestFont( typeface.Name, fontSizeInPoints ); //user may want only some script lang (not all script in the font) //and each script user may want different glyph-gen technique. //so we use GlyphTextureBuildDetail to describe that information. if (buildDetails.Count == 0) { MessageBox.Show("please select some script"); return; } //------------------------------------------------------------------------------- //1. create glyph-texture-bitmap generator var glyphTextureGen = new GlyphTextureBitmapGenerator(); glyphTextureGen.SetSvgBmpBuilderFunc(SvgBuilderHelper.ParseAndRenderSvg); //2. generate the glyphs TextureKindAndDescription textureKindAndDesc = (TextureKindAndDescription)this.cmbTextureKind.SelectedItem; if (textureKindAndDesc.Kind == TextureKind.Msdf) { glyphTextureGen.MsdfGenVersion = textureKindAndDesc.TechniqueDetail; } FontAtlasBuilderHelper helper = new FontAtlasBuilderHelper(); helper.Build(glyphTextureGen, typeface, fontSizeInPoints, textureKindAndDesc.Kind, buildDetails.ToArray()); #if DEBUG this.Text += ", finished: build time(ms)=" + helper.dbugBuildTimeMillisec; System.Diagnostics.Debug.WriteLine("font atlas build time (ms): " + helper.dbugBuildTimeMillisec); #endif ///------------------------------------------------ //lets view result ... SimpleUtils.DisposeExistingPictureBoxImage(picOutput); uiFontAtlasFileViewer1.LoadFontAtlasFile(helper.TextureInfoFilename, helper.OutputImgFilename); this.picOutput.Image = new Bitmap(helper.OutputImgFilename); this.lblOutput.Text = "Output: " + helper.OutputImgFilename; ////read .info back and convert to base64 //byte[] atlas_info_content = File.ReadAllBytes(textureName + ".info"); //string base64 = Convert.ToBase64String(atlas_info_content); ////create atlas //SimpleFontAtlas fontAtlas = atlasBuilder.CreateSimpleFontAtlas(); //fontAtlas.TotalGlyph = totalGlyphsImg; }
void BuildAtlas_FromInputChars() { if (!float.TryParse(txtSelectedFontSize.Text, out float fontSizeInPoints)) { MessageBox.Show("err: selected font size " + txtSelectedFontSize.Text); return; } //1. create glyph-texture-bitmap generator var glyphTextureGen = new GlyphTextureBitmapGenerator(); glyphTextureGen.SetSvgBmpBuilderFunc(SvgBuilderHelper.ParseAndRenderSvg); //2. generate the glyphs TextureKindAndDescription textureKindAndDesc = (TextureKindAndDescription)this.cmbTextureKind.SelectedItem; if (textureKindAndDesc.Kind == TextureKind.Msdf) { glyphTextureGen.MsdfGenVersion = textureKindAndDesc.TechniqueDetail; } var atlasBuilder = new SimpleBitmapAtlasBuilder(); glyphTextureGen.CreateTextureFontFromInputChars( atlasBuilder, _typeface, fontSizeInPoints, textureKindAndDesc.Kind, GetUniqueChars() ); //3. set information before write to font-info atlasBuilder.SpaceCompactOption = SimpleBitmapAtlasBuilder.CompactOption.ArrangeByHeight; //4. merge all glyph in the builder into a single image MemBitmap totalGlyphsImg = atlasBuilder.BuildSingleImage(true); string fontTextureImg = "test_glyph_atlas.png"; //5. save to png totalGlyphsImg.SaveImage(fontTextureImg); //----------------------------------------------- //let view result... SimpleUtils.DisposeExistingPictureBoxImage(picOutput); this.lblOutput.Text = "output: " + fontTextureImg; this.picOutput.Image = new Bitmap(fontTextureImg); #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 //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()); /// }