private void button1_Click(object sender, EventArgs e) { //msdfgen: see more https://github.com/Chlumsky/msdfgen //sdf – generates a conventional monochrome signed distance field. //psdf – generates a monochrome signed pseudo - distance field. //msdf(default) – generates a multi - channel signed distance field using my new method. //char[] fontChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); char[] fontChars = new char[] { (char)'X' }; int j = fontChars.Length; for (int i = 0; i < j; ++i) { char c = fontChars[i]; //msdf //string args = @"msdfgen msdf -font C:\Windows\Fonts\tahoma.ttf 'A' -o msdf.png -size 32 32 -pxrange 4 -autoframe -testrender render_msdf.png 1024 1024"; //MsdfParameters pars = new MsdfParameters(@"C:\Windows\Fonts\tahoma.ttf", c); MsdfParameters pars = new MsdfParameters(@"d:\\WImageTest\\shardailes.ttf", c); pars.enableRenderTestFile = false; string[] splitStr = pars.GetArgs(); MyFtLib.MyFtMSDFGEN(splitStr.Length, splitStr); } //for (int i = 0; i < j; ++i) //{ // char c = fontChars[i]; // MsdfParameters pars = new MsdfParameters(@"C:\Windows\Fonts\tahoma.ttf", c); // pars.enableRenderTestFile = false; // pars.useClassicSdf = true; // string[] splitStr = pars.GetArgs(); // MyFtLib.MyFtMSDFGEN(splitStr.Length, pars.GetArgs()); //} }
private void button2_Click(object sender, EventArgs e) { int version = MyFtLib.MyFtLibGetVersion(); IntPtr shape = MyFtLib.CreateShape(); IntPtr cnt = MyFtLib.ShapeAddBlankContour(shape); //MyFtLib.ContourAddLinearSegment(cnt, 10, 10, 25, 25); //MyFtLib.ContourAddLinearSegment(cnt, 25, 25, 15, 10); //MyFtLib.ContourAddLinearSegment(cnt, 15, 10, 10, 10); MyFtLib.ContourAddLinearSegment(cnt, 3.84f, 0, 1.64f, 0); MyFtLib.ContourAddLinearSegment(cnt, 1.64f, 0, 1.64f, 18.23f); MyFtLib.ContourAddLinearSegment(cnt, 1.64f, 18.23f, 3.84f, 18.23f); MyFtLib.ContourAddLinearSegment(cnt, 3.84f, 18.23f, 3.84f, 0); double s_left, s_bottom, s_right, s_top; MyFtLib.ShapeFindBounds(shape, out s_left, out s_bottom, out s_right, out s_top); //then create msdf texture if (!MyFtLib.ShapeValidate(shape)) { throw new NotSupportedException(); } MyFtLib.ShapeNormalize(shape); unsafe { int w = 32, h = 32; int[] output = new int[w * h]; fixed(int *output_h = &output[0]) { MyFtLib.MyFtGenerateMsdf(shape, w, h, 2, 1, 1, 1, -1, 3, output_h); } //save to bmp using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(output, 0, bmpdata.Scan0, output.Length); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\a001_x1.png"); } } MyFtLib.DeleteUnmanagedObj(shape); }
internal unsafe static GlyphImage BuildMsdfFontImage() { IntPtr shape = MyFtLib.CreateShape(); IntPtr cnt = MyFtLib.ShapeAddBlankContour(shape); //MyFtLib.ContourAddLinearSegment(cnt, 10, 10, 25, 25); //MyFtLib.ContourAddLinearSegment(cnt, 25, 25, 15, 10); //MyFtLib.ContourAddLinearSegment(cnt, 15, 10, 10, 10); // MyFtLib.ContourAddLinearSegment(cnt, 10, 10, 25, 25); MyFtLib.ContourAddQuadraticSegment(cnt, 25, 25, 15, 30, 10, 15); MyFtLib.ContourAddLinearSegment(cnt, 10, 15, 10, 10); double s_left, s_bottom, s_right, s_top; MyFtLib.ShapeFindBounds(shape, out s_left, out s_bottom, out s_right, out s_top); var glyphBounds = new PixelFarm.Drawing.RectangleF((float)s_left, (float)s_top, (float)(s_right - s_left), (float)(s_top - s_bottom)); //then create msdf texture if (!MyFtLib.ShapeValidate(shape)) { throw new NotSupportedException(); } MyFtLib.ShapeNormalize(shape); int borderXY = 0; int w = (int)Math.Ceiling(glyphBounds.Width) + (borderXY + borderXY); int h = (int)(Math.Ceiling(glyphBounds.Height)) + (borderXY + borderXY); if (w == 0) { w = 5; h = 5; } int[] outputBuffer = new int[w * h]; GlyphImage glyphImage = new GlyphImage(w, h); glyphImage.BorderXY = borderXY; glyphImage.OriginalGlyphBounds = Typography.Contours.RectangleF.FromLTRB( glyphBounds.Left, glyphBounds.Top, glyphBounds.Right, glyphBounds.Bottom); unsafe { fixed(int *output_header = &outputBuffer[0]) { float dx = 0; float dy = 0; if (s_left < 0) { //make it positive dx = (float)-s_left; } else if (s_left > 0) { } if (s_bottom < 0) { //make it positive dy = (float)-s_bottom; } else if (s_bottom > 0) { } //this glyph image has border (for msdf to render correctly) MyFtLib.MyFtGenerateMsdf(shape, w, h, 4, 1, dx + borderXY, dy + borderXY, -1, 3, output_header); MyFtLib.DeleteUnmanagedObj(shape); } glyphImage.SetImageBuffer(outputBuffer, true); } return(glyphImage); }