static void CreateSampleMsdfTextureFont( string fontfile, float sizeInPoint, char[] chars, string outputFile) { //sample var reader = new OpenFontReader(); using (var fs = new FileStream(fontfile, FileMode.Open)) { //1. read typeface from font file Typeface typeface = reader.Read(fs); //sample: create sample msdf texture //------------------------------------------------------------- var builder = new GlyphPathBuilder(typeface); //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked; //builder.UseVerticalHinting = this.chkVerticalHinting.Checked; //------------------------------------------------------------- var atlasBuilder = new SimpleFontAtlasBuilder(); MsdfGenParams msdfGenParams = new MsdfGenParams(); int j = chars.Length; for (int i = 0; i < j; ++i) { //build glyph ushort gindex = typeface.LookupIndex(chars[i]); builder.BuildFromGlyphIndex(gindex, -1); var glyphToContour = new GlyphContourBuilder(); //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours()); builder.ReadShapes(glyphToContour); msdfGenParams.shapeScale = 1f / 64; GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, msdfGenParams); atlasBuilder.AddGlyph(gindex, glyphImg); int w = glyphImg.Width; int h = glyphImg.Height; using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); int[] imgBuffer = glyphImg.GetImageBuffer(); System.Runtime.InteropServices.Marshal.Copy(imgBuffer, 0, bmpdata.Scan0, imgBuffer.Length); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\a001_xn2_" + (chars[i]) + ".png"); } } var glyphImg2 = atlasBuilder.BuildSingleImage(); using (Bitmap bmp = new Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); int[] intBuffer = glyphImg2.GetImageBuffer(); System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\a_total.png"); } atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml"); } }
void CreateTextureFontFromGlyphIndices( Typeface typeface, float sizeInPoint, HintTechnique hintTechnique, SimpleFontAtlasBuilder atlasBuilder, bool applyFilter, ushort[] glyphIndices) { //sample: create sample msdf texture //------------------------------------------------------------- var builder = new GlyphPathBuilder(typeface); builder.SetHintTechnique(hintTechnique); // if (atlasBuilder.TextureKind == TextureKind.Msdf) { MsdfGenParams msdfGenParams = new MsdfGenParams(); int j = glyphIndices.Length; for (int i = 0; i < j; ++i) { ushort gindex = glyphIndices[i]; //create picture with unscaled version set scale=-1 //(we will create glyph contours and analyze them) builder.BuildFromGlyphIndex(gindex, -1); var glyphToContour = new GlyphContourBuilder(); builder.ReadShapes(glyphToContour); //msdfgen with scale the glyph to specific shapescale msdfGenParams.shapeScale = 1f / 64; //as original GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, msdfGenParams); // atlasBuilder.AddGlyph(gindex, glyphImg); } } else { AggGlyphTextureGen aggTextureGen = new AggGlyphTextureGen(); aggTextureGen.TextureKind = atlasBuilder.TextureKind; int j = glyphIndices.Length; for (int i = 0; i < j; ++i) { //build glyph ushort gindex = glyphIndices[i]; builder.BuildFromGlyphIndex(gindex, sizeInPoint); GlyphImage glyphImg = aggTextureGen.CreateGlyphImage(builder, 1); if (applyFilter) { glyphImg = Sharpen(glyphImg, 1); //TODO: //the filter make the image shift x and y 1 px //temp fix with this, glyphImg.TextureOffsetX += 1; glyphImg.TextureOffsetY += 1; } // atlasBuilder.AddGlyph(gindex, glyphImg); } } }
static void CreateSampleMsdfTextureFont(string fontfile, float sizeInPoint, ushort startGlyphIndex, ushort endGlyphIndex, string outputFile) { //sample var reader = new OpenFontReader(); using (var fs = new FileStream(fontfile, FileMode.Open)) { //1. read typeface from font file Typeface typeface = reader.Read(fs); //sample: create sample msdf texture //------------------------------------------------------------- var builder = new GlyphPathBuilder(typeface); //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked; //builder.UseVerticalHinting = this.chkVerticalHinting.Checked; //------------------------------------------------------------- var atlasBuilder = new SimpleFontAtlasBuilder(); for (ushort gindex = startGlyphIndex; gindex <= endGlyphIndex; ++gindex) { //build glyph builder.BuildFromGlyphIndex(gindex, sizeInPoint); var glyphToContour = new GlyphContourBuilder(); //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours()); var genParams = new MsdfGenParams(); builder.ReadShapes(glyphToContour); genParams.shapeScale = 1f / 64; //we scale later (as original C++ code use 1/64) GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, genParams); atlasBuilder.AddGlyph(gindex, glyphImg); //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) //{ // var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length); // bmp.UnlockBits(bmpdata); // bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png"); //} } var glyphImg2 = atlasBuilder.BuildSingleImage(); using (Bitmap bmp = new Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); int[] intBuffer = glyphImg2.GetImageBuffer(); System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\a_total.png"); } atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml"); } }
void RenderWithMsdfImg(Typeface typeface, char testChar, float sizeInPoint) { painter.FillColor = PixelFarm.Drawing.Color.Black; //p.UseSubPixelRendering = chkLcdTechnique.Checked; painter.Clear(PixelFarm.Drawing.Color.White); //---------------------------------------------------- var builder = new GlyphPathBuilder(typeface); builder.SetHintTechnique(_glyphRenderOptions.HintTechnique); //---------------------------------------------------- builder.Build(testChar, sizeInPoint); //---------------------------------------------------- var glyphToContour = new GlyphContourBuilder(); var msdfGenPars = new MsdfGenParams(); builder.ReadShapes(glyphToContour); //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours()); MsdfGenParams genParams = new MsdfGenParams(); GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, genParams); var actualImg = ActualImage.CreateFromBuffer(glyphImg.Width, glyphImg.Height, PixelFormat.ARGB32, glyphImg.GetImageBuffer()); painter.DrawImage(actualImg, 0, 0); //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) //{ // var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length); // bmp.UnlockBits(bmpdata); // bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png"); //} if (_contourAnalysisOpts.ShowGrid) { //render grid RenderGrids(800, 600, _gridSize, painter); } //6. use this util to copy image from Agg actual image to System.Drawing.Bitmap PixelFarm.Agg.Imaging.BitmapHelper.CopyToGdiPlusBitmapSameSize(destImg, winBmp); //--------------- //7. just render our bitmap g.Clear(Color.White); g.DrawImage(winBmp, new Point(30, 20)); }
static void CreateSampleMsdfImg(GlyphContourBuilder tx, string outputFile) { //sample MsdfGenParams msdfGenParams = new MsdfGenParams(); GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(tx, msdfGenParams); int w = glyphImg.Width; int h = glyphImg.Height; using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); int[] imgBuffer = glyphImg.GetImageBuffer(); System.Runtime.InteropServices.Marshal.Copy(imgBuffer, 0, bmpdata.Scan0, imgBuffer.Length); bmp.UnlockBits(bmpdata); bmp.Save(outputFile); } }
static SimpleFontAtlasBuilder CreateSampleMsdfTextureFont( Typeface typeface, float sizeInPoint, HintTechnique hintTech, IEnumerable <ushort> glyphIndexIter) { ////read type face from file //Typeface typeface; //using (var fs = new FileStream(fontfile, FileMode.Open, FileAccess.Read)) //{ // var reader = new OpenFontReader(); // //1. read typeface from font file // typeface = reader.Read(fs); //} //sample: create sample msdf texture //------------------------------------------------------------- var builder = new GlyphPathBuilder(typeface); builder.SetHintTechnique(hintTech); MsdfGenParams genParams = new MsdfGenParams(); var atlasBuilder = new SimpleFontAtlasBuilder(); atlasBuilder.SetAtlasInfo(TextureKind.Msdf, sizeInPoint); foreach (ushort gindex in glyphIndexIter) { //build glyph builder.BuildFromGlyphIndex(gindex, -1); //use original glyph size (assign -1) var glyphToContour = new GlyphContourBuilder(); //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours()); builder.ReadShapes(glyphToContour); genParams.shapeScale = 1f / 64; //we scale later (as original C++ code use 1/64) GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, genParams); atlasBuilder.AddGlyph(gindex, glyphImg); //int[] buffer = glyphImage.GetImageBuffer(); //using (var bmp = new System.Drawing.Bitmap(glyphImage.Width, glyphImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) //{ // var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImage.Width, glyphImage.Height), // System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length); // bmp.UnlockBits(bmpdata); // bmp.Save("d:\\WImageTest\\a001_xn2_" + c + ".png"); //} } //var glyphImg2 = atlasBuilder.BuildSingleImage(); //using (var bmp = new System.Drawing.Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) //{ // var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height), // System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // int[] intBuffer = glyphImg2.GetImageBuffer(); // System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length); // bmp.UnlockBits(bmpdata); // bmp.Save("d:\\WImageTest\\a_total.png"); //} //atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml"); return(atlasBuilder); }
void CreateTextureFontFromGlyphIndices( Typeface typeface, float sizeInPoint, HintTechnique hintTechnique, SimpleFontAtlasBuilder atlasBuilder, bool applyFilter, ushort[] glyphIndices) { //sample: create sample msdf texture //------------------------------------------------------------- var builder = new GlyphPathBuilder(typeface); builder.SetHintTechnique(hintTechnique); // if (atlasBuilder.TextureKind == TextureKind.Msdf) { MsdfGenParams msdfGenParams = new MsdfGenParams(); int j = glyphIndices.Length; for (int i = 0; i < j; ++i) { ushort gindex = glyphIndices[i]; //create picture with unscaled version set scale=-1 //(we will create glyph contours and analyze them) builder.BuildFromGlyphIndex(gindex, -1); var glyphToContour = new GlyphContourBuilder(); builder.ReadShapes(glyphToContour); //msdfgen with scale the glyph to specific shapescale msdfGenParams.shapeScale = 1f / 64; //as original GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, msdfGenParams); // atlasBuilder.AddGlyph(gindex, glyphImg); } } else { AggGlyphTextureGen aggTextureGen = new AggGlyphTextureGen(); aggTextureGen.TextureKind = atlasBuilder.TextureKind; //create reusable agg painter*** //assume each glyph size= 2 * line height //TODO: review here again... int tmpMemBmpHeight = (int)(2 * typeface.CalculateRecommendLineSpacing() * typeface.CalculateScaleToPixelFromPointSize(sizeInPoint)); //create glyph img using (PixelFarm.CpuBlit.MemBitmap tmpMemBmp = new PixelFarm.CpuBlit.MemBitmap(tmpMemBmpHeight, tmpMemBmpHeight)) //square { //draw a glyph into tmpMemBmp and then copy to a GlyphImage aggTextureGen.Painter = PixelFarm.CpuBlit.AggPainter.Create(tmpMemBmp); #if DEBUG tmpMemBmp._dbugNote = "CreateGlyphImage()"; #endif int j = glyphIndices.Length; for (int i = 0; i < j; ++i) { //build glyph ushort gindex = glyphIndices[i]; builder.BuildFromGlyphIndex(gindex, sizeInPoint); GlyphImage glyphImg = aggTextureGen.CreateGlyphImage(builder, 1); if (applyFilter) { glyphImg = Sharpen(glyphImg, 1); //TODO: //the filter make the image shift x and y 1 px //temp fix with this, glyphImg.TextureOffsetX += 1; glyphImg.TextureOffsetY += 1; } // atlasBuilder.AddGlyph(gindex, glyphImg); } } } }
void CreateTextureFontFromGlyphIndices( Typeface typeface, float sizeInPoint, TextureKind textureKind, ushort[] glyphIndices, OnEachFinishTotal onFinishTotal) { if (onFinishTotal == null) { return; } //sample: create sample msdf texture //------------------------------------------------------------- var builder = new GlyphPathBuilder(typeface); builder.UseTrueTypeInstructions = this.UseTrueTypeInstruction; //------------------------------------------------------------- var atlasBuilder = new SimpleFontAtlasBuilder(); atlasBuilder.SetAtlasInfo(textureKind, sizeInPoint); //------------------------------------------------------------- // MsdfGenParams msdfGenParams = null; AggGlyphTextureGen aggTextureGen = null; if (textureKind == TextureKind.Msdf) { msdfGenParams = new MsdfGenParams(); } else { aggTextureGen = new AggGlyphTextureGen(); } float pxscale = typeface.CalculateScaleToPixelFromPointSize(sizeInPoint); int j = glyphIndices.Length; for (int i = 0; i < j; ++i) { //build glyph ushort gindex = glyphIndices[i]; builder.BuildFromGlyphIndex(gindex, -1); GlyphImage glyphImg = null; if (textureKind == TextureKind.Msdf) { var glyphToContour = new GlyphContourBuilder(); //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours()); builder.ReadShapes(glyphToContour); msdfGenParams.shapeScale = 1f / 64; //as original glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, msdfGenParams); } else { //create alpha channel texture aggTextureGen.TextureKind = textureKind; glyphImg = aggTextureGen.CreateGlyphImage(builder, pxscale); } // atlasBuilder.AddGlyph(gindex, glyphImg); onFinishTotal(gindex, glyphImg, null); } onFinishTotal(0, null, atlasBuilder); }