static void BuildFontGlyphs(PixelFarm.Drawing.RequestFont font, Typography.Rendering.SimpleFontAtlasBuilder atlasBuilder, int startAt, int endAt) { //font glyph for specific font face ActualFont nativeFont = GetActualFont(font.Name, font.SizeInPoints);// nativeFontStore.GetResolvedNativeFont(font); for (int i = startAt; i <= endAt; ++i) { char c = (char)i; FontGlyph fontGlyph = nativeFont.GetGlyph(c); //------------------- GlyphImage glyphImg = NativeMsdfGen.BuildMsdfFontImage(fontGlyph); // Console.WriteLine(c.ToString() + " ox,oy" + glyphImg.OffsetX + "," + glyphImg.OffsetY); int w = glyphImg.Width; int h = glyphImg.Height; int[] buffer = glyphImg.GetImageBuffer(); NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); glyphImg.SetImageBuffer(buffer, false); // atlasBuilder.AddGlyph(fontGlyph.glyphMatrix.u c, fontGlyph, 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_x1_" + (int)c + ".png"); //} } }
private void button4_Click(object sender, EventArgs e) { string fontfilename = "d:\\WImageTest\\a_total.xml"; var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder(); SimpleFontAtlas fontAtlas = atlasBuilder.LoadFontInfo(fontfilename); using (Bitmap totalImg = new Bitmap("d:\\WImageTest\\a_total.png")) { var bmpdata = totalImg.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, totalImg.PixelFormat); var buffer = new int[totalImg.Width * totalImg.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, buffer, 0, buffer.Length); totalImg.UnlockBits(bmpdata); var glyImage = new GlyphImage(totalImg.Width, totalImg.Height); glyImage.SetImageBuffer(buffer, false); fontAtlas.TotalGlyph = glyImage; } }
private void button5_Click(object sender, EventArgs e) { //1. load font string fontName = "tahoma"; string fontfile = "c:\\Windows\\Fonts\\tahoma.ttf"; //string fontfile = @"D:\WImageTest\THSarabunNew\THSarabunNew.ttf"; ActualFont font = GetActualFont(fontfile, 18);// nativeFontStore.LoadFont(fontName, fontfile, 28); //2. get glyph var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder(); //for (int i = 0; i < 256; ++i) //BuildFontGlyphsByIndex(font, atlasBuilder, 0, 255); //BuildFontGlyphs(font, atlasBuilder, 0x0e00, 0x0e5b); //BuildFontGlyphsByIndex(font, atlasBuilder, 0, 3417); //BuildFontGlyphsByIndex(font, atlasBuilder, 0, 509); //BuildFontGlyphsByIndex(font, atlasBuilder, 97, 97); BuildFontGlyph(font, atlasBuilder, 'B'); //---------------------------------------------------- //GlyphImage totalImg = atlasBuilder.BuildSingleImage(); GlyphImage totalImg = atlasBuilder.BuildSingleImage(); using (Bitmap bmp = new Bitmap(totalImg.Width, totalImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { int[] buffer = totalImg.GetImageBuffer(); if (totalImg.IsBigEndian) { NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); totalImg.SetImageBuffer(buffer, false); } var bmpdata = bmp.LockBits( new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.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\\a_total.png"); } string fontfilename = "d:\\WImageTest\\a_total.xml"; atlasBuilder.SaveFontInfo(fontfilename); //---------------------------------- }
static void BuildFontGlyph(ActualFont nativefont, Typography.Rendering.SimpleFontAtlasBuilder atlasBuilder, char c) { //font glyph for specific font face FontGlyph fontGlyph = nativefont.GetGlyph(c); GlyphImage glyphImg = NativeMsdfGen.BuildMsdfFontImage(fontGlyph); int w = glyphImg.Width; int h = glyphImg.Height; int[] buffer = glyphImg.GetImageBuffer(); NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); glyphImg.SetImageBuffer(buffer, false); atlasBuilder.AddGlyph(0, 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_x1_" + (int)c + ".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 fontTextureImgFilename = fontTextureInfoFile + ".png"; //check if the file exist if (StorageService.Provider.DataExists(fontTextureInfoFile) && StorageService.Provider.DataExists(fontTextureImgFilename)) { SimpleFontAtlasBuilder atlasBuilder = new SimpleFontAtlasBuilder(); using (System.IO.Stream dataStream = StorageService.Provider.ReadDataStream(fontTextureInfoFile)) { try { fontAtlas = atlasBuilder.LoadFontInfo(dataStream); fontAtlas.TotalGlyph = ReadGlyphImages(fontTextureImgFilename); fontAtlas.OriginalFontSizePts = reqFont.SizeInPoints; _createdAtlases.Add(fontKey, fontAtlas); } catch (Exception ex) { throw ex; } } } else { GlyphImage totalGlyphsImg = null; SimpleFontAtlasBuilder atlasBuilder = null; var glyphTextureGen = new GlyphTextureBitmapGenerator(); glyphTextureGen.CreateTextureFontBuildDetail( resolvedTypeface, reqFont.SizeInPoints, _textureKind, GlyphTextureCustomConfigs.TryGetGlyphTextureBuildDetail(reqFont), (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, fontTextureImgFilename); #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()); #if DEBUG //write temp debug info System.IO.File.WriteAllText(fontTextureInfoFile + ".txt", reqFont.Name + ",size" + reqFont.SizeInPoints + "pts"); #endif } } } outputBitmap = _loadedGlyphs.GetOrCreateNewOne(fontAtlas); return(fontAtlas); }
private void button6_Click(object sender, EventArgs e) { //1. load font ActualFont font = GetActualFont("tahoma", 28);// nativeFontStore.LoadFont("tahoma", 28); //2. get glyph var g1 = font.GetGlyph('C'); var plans = new List <Typography.TextLayout.GlyphPlan>(); PixelFarm.Drawing.Text.TextShapingService.GetGlyphPos(font, "ABC".ToCharArray(), 0, 3, plans); int[] glyphIndice = new int[] { 1076, 1127, 1164 }; int j = glyphIndice.Length; var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder(); for (int i = 0; i < j; ++i) { int codepoint = glyphIndice[i]; FontGlyph fontGlyph = font.GetGlyphByIndex((uint)codepoint); GlyphImage glyphImg = NativeMsdfGen.BuildMsdfFontImage(fontGlyph); int w = glyphImg.Width; int h = glyphImg.Height; int[] buffer = glyphImg.GetImageBuffer(); NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); glyphImg.SetImageBuffer(buffer, false); atlasBuilder.AddGlyph(codepoint, glyphImg); 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(buffer, 0, bmpdata.Scan0, buffer.Length); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\a001_y1_" + codepoint + ".png"); } } //---------------------------------------------------- GlyphImage totalImg = atlasBuilder.BuildSingleImage(); using (Bitmap bmp = new Bitmap(totalImg.Width, totalImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { int[] buffer = totalImg.GetImageBuffer(); if (totalImg.IsBigEndian) { NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); totalImg.SetImageBuffer(buffer, false); } var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.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\\a_total.png"); } string fontfilename = "d:\\WImageTest\\a_total.xml"; atlasBuilder.SaveFontInfo(fontfilename); //---------------------------------- }
private void button3_Click(object sender, EventArgs e) { //1. load font string fontName = "tahoma"; int fontSizeInPts = 28; ActualFont font = GetActualFont(fontName, fontSizeInPts); //2. get glyph char[] fontChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); int j = fontChars.Length; var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder(); atlasBuilder.SetAtlasInfo(TextureKind.Msdf, fontSizeInPts); for (int i = 0; i < j; ++i) { char c = fontChars[i]; FontGlyph fontGlyph = font.GetGlyph(c); GlyphImage glyphImg = NativeMsdfGen.BuildMsdfFontImage(fontGlyph); int w = glyphImg.Width; int h = glyphImg.Height; int[] buffer = glyphImg.GetImageBuffer(); NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); glyphImg.SetImageBuffer(buffer, false); atlasBuilder.AddGlyph(0, glyphImg); 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(buffer, 0, bmpdata.Scan0, buffer.Length); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\a001_x1_" + (int)c + ".png"); } } //---------------------------------------------------- GlyphImage totalImg = atlasBuilder.BuildSingleImage(); using (Bitmap bmp = new Bitmap(totalImg.Width, totalImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { int[] buffer = totalImg.GetImageBuffer(); if (totalImg.IsBigEndian) { NativeMsdfGen.SwapColorComponentFromBigEndianToWinGdi(buffer); totalImg.SetImageBuffer(buffer, false); } var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.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\\a_total.png"); } string fontfilename = "d:\\WImageTest\\a_total.xml"; atlasBuilder.SaveFontInfo(fontfilename); //---------------------------------- }