private void cmdMakeFromSelectedString_Click(object sender, EventArgs e) { //create a simple stencil texture font //string sampleFontFile = "../../../TestFonts/tahoma.ttf"; if (this.textBox1.Text == null || _typeface == null) { return; } string sampleFontFile = _typeface.Filename ?? ""; TextureKind selectedTextureKind = (TextureKind)lstTextureType.SelectedItem; string bitmapImgSaveFileName = "d:\\WImageTest\\sample_" + selectedTextureKind + "_" + System.IO.Path.GetFileNameWithoutExtension(sampleFontFile); var textureGen = new GlyphTextureBitmapGenerator(); bool saveEachGlyphSeparatly = chkSaveEachGlyph.Checked; char[] chars = this.textBox1.Text.ToCharArray(); textureGen.CreateTextureFontFromInputChars( _typeface, FontSizeInPoints, selectedTextureKind, chars, //eg. ABCD (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"); } } }); }
private void cmdTestFontAtlas_Click(object sender, EventArgs e) { //read string from txtSampleChars //please make sure all are unique. (TODO: check it) //then create a font atlas from the sample chars char[] sampleChars = txtSampleChars.Text.ToCharArray(); if (sampleChars.Length == 0) { return; } // GlyphImage totalGlyphsImg = null; SimpleFontAtlasBuilder atlasBuilder = null; var glyphTextureGen = new GlyphTextureBitmapGenerator(); // Typeface typeface = _basicOptions.Typeface; float fontSizeInPoints = _basicOptions.FontSizeInPoints; // glyphTextureGen.CreateTextureFontFromInputChars( typeface, fontSizeInPoints, TextureKind.StencilLcdEffect, sampleChars, (glyphIndex, glyphImage, outputAtlasBuilder) => { if (outputAtlasBuilder != null) { //finish atlasBuilder = outputAtlasBuilder; } } ); atlasBuilder.SpaceCompactOption = SimpleFontAtlasBuilder.CompactOption.ArrangeByHeight; totalGlyphsImg = atlasBuilder.BuildSingleImage(); string fontTextureImg = "d:\\WImageTest\\test_glyph_atlas.png"; //create atlas SimpleFontAtlas fontAtlas = atlasBuilder.CreateSimpleFontAtlas(); fontAtlas.TotalGlyph = totalGlyphsImg; // using (MemBitmap memBmp = MemBitmap.CreateFromCopy(totalGlyphsImg.Width, totalGlyphsImg.Height, totalGlyphsImg.GetImageBuffer())) using (System.Drawing.Bitmap bmp = new Bitmap(memBmp.Width, memBmp.Height)) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, memBmp.Width, memBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var tmpMem = MemBitmap.GetBufferPtr(memBmp); unsafe { PixelFarm.CpuBlit.NativeMemMx.MemCopy((byte *)bmpdata.Scan0, (byte *)tmpMem.Ptr, tmpMem.LengthInBytes); } bmp.UnlockBits(bmpdata); bmp.Save(fontTextureImg); } #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()); /// }
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()); /// }