private byte[] CreateGlyph(char firstChar, int charCount, ref int width, ref int height) { byte[] bitmapData = null; //Read ttf file into byte array byte[] ttfFileContent = File.ReadAllBytes(ttfSampleDir + '\\' + FontSelectorComboBox.SelectedItem as string); using (var ttf = new PinnedArray <byte>(ttfFileContent)) { //get pointer of the ttf file content var ttf_buffer = ttf.Pointer; //Initialize fontinfo FontInfo font = new FontInfo(); STBTrueType.InitFont(ref font, ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0)); //set bitmap size const int BITMAP_W = 512; const int BITMAP_H = 512; //allocate bitmap buffer byte[] bitmapBuffer = new byte[BITMAP_W * BITMAP_W]; BakedChar[] cdata = new BakedChar[charCount]; //bake bitmap for codepoint from firstChar to firstChar + charCount - 1 STBTrueType.BakeFontBitmap(ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0), pixelHeight, bitmapBuffer, BITMAP_W, BITMAP_H, firstChar, charCount, cdata); // no guarantee this fits! bitmapData = bitmapBuffer; width = BITMAP_W; height = BITMAP_H; } return(bitmapData); }
public void Test_BakeMultipleCodepoint() { #region Init string assemblyDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string solution_dir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(assemblyDir))); #endregion //Read ttf file into byte array byte[] ttfFileContent = File.ReadAllBytes(solution_dir + @"\FontSamples\Windsong.ttf"); using (var ttf = new PinnedArray <byte>(ttfFileContent)) { //get pointer of the ttf file content var ttf_buffer = ttf.Pointer; //Initialize fontinfo FontInfo font = new FontInfo(); STBTrueType.InitFont(ref font, ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0)); //set bitmap size const int BITMAP_W = 512; const int BITMAP_H = 512; //allocate bitmap buffer byte[] bitmapBuffer = new byte[BITMAP_W * BITMAP_W]; BakedChar[] cdata = new BakedChar[256 * 2]; // ASCII 32..126 is 95 glyphs //bake bitmap for codepoint from 32 to 126 STBTrueType.BakeFontBitmap(ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0), 32.0f, bitmapBuffer, BITMAP_W, BITMAP_H, 32, 96, cdata); // no guarantee this fits! //output the bitmap to a text file WriteBitmapToFileAsText("testOuput.txt", BITMAP_H, BITMAP_W, bitmapBuffer); //Open the text file OpenFile("testOuput.txt"); } }
static void Main(string[] args) { var font = new TrueTypeFont(@"Anonymous\Anonymous Pro.ttf"); // Render some characters... for (char ch = 'A'; ch <= 'Z'; ch++) { int width, height, xOffset, yOffset; float scale = font.GetScaleForPixelHeight(80); byte[] data = font.GetCodepointBitmap(ch, scale, scale, out width, out height, out xOffset, out yOffset); SaveBitmap(data, 0, 0, width, height, width, "Char-" + ch.ToString() + ".png"); } // Let's try baking. Tasty tasty. BakedCharCollection characters; float pixelHeight = 18; var bitmap = font.BakeFontBitmap(pixelHeight, out characters, true); SaveBitmap(bitmap.Buffer, 0, 0, bitmap.Width, bitmap.Height, bitmap.Width, "BakeResult1.png"); // Now, let's give serialization a go. using (var file = File.OpenWrite("BakeResult2.temp")) { var bitmapSaver = new BinaryFormatter(); bitmapSaver.Serialize(file, bitmap); bitmapSaver.Serialize(file, characters); int ascent, descent, lineGap; float scale = font.GetScaleForPixelHeight(pixelHeight); font.GetFontVMetrics(out ascent, out descent, out lineGap); bitmapSaver.Serialize(file, (float)ascent * scale); bitmapSaver.Serialize(file, (float)descent * scale); bitmapSaver.Serialize(file, (float)lineGap * scale); } using (var file = File.OpenRead("BakeResult2.temp")) { var bitmapLoader = new BinaryFormatter(); var bitmapAgain = (FontBitmap)bitmapLoader.Deserialize(file); var charactersAgain = (BakedCharCollection)bitmapLoader.Deserialize(file); SaveBitmap(bitmapAgain.Buffer, 0, 0, bitmapAgain.Width, bitmapAgain.Height, bitmap.Width, "BakeResult2.png"); for (char ch = 'A'; ch <= 'Z'; ch++) { BakedChar bakedChar = charactersAgain[ch]; if (bakedChar.IsEmpty) { continue; } SaveBitmap(bitmapAgain.Buffer, bakedChar.X0, bakedChar.Y0, bakedChar.X1, bakedChar.Y1, bitmapAgain.Stride, "SmallChar-" + ch.ToString() + ".png"); } } }