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"); } } }
static void Main(string[] args) { var font = new TrueTypeFont(@"Anonymous\Roboto-medium.ttf"); // Render some characters... for (uint ch = 32; ch <= 0x7f; 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"); } } }
public TrueTypeSharpTextRenderer(GraphicsDeviceManager graphicsDeviceManager, string fontFileName, int pixelSize) { _textBlockList = new Dictionary <int, TextBlock>(); _textureForChar = new Dictionary <char, Texture2D>(100); _offsetForChar = new Dictionary <char, Vector2>(100); _graphicsDeviceManager = graphicsDeviceManager; _spriteBatch = new SpriteBatch(_graphicsDeviceManager.GraphicsDevice); _font = new TrueTypeFont(new FileStream(fontFileName, FileMode.Open)); _scale = _font.GetScaleForPixelHeight(pixelSize); foreach (var letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789 ()+-.,!?\"%[]{}\\/*^'<>&=_") { createTextureForChar(letter); } }
public GlyphPalette() { appConfig = AppConfig.Get(); glyphs = new char[appConfig.glyphPaletteSize * appConfig.glyphPaletteSize]; textureSize = appConfig.glyphPaletteSize * appConfig.canvasTileSize; texture = new Texture2D(Window.Instance.GraphicsDevice, textureSize, textureSize); var blankTextureData = new Color[textureSize * textureSize]; for (var i = 0; i < blankTextureData.Length; i++) { blankTextureData[i] = Color.TransparentBlack; } texture.SetData(blankTextureData); var fontPath = Path.Combine(appConfig.resourcesDirectory, appConfig.fallbackFontFile); font = new TrueTypeFont(fontPath); fontScale = font.GetScaleForPixelHeight(appConfig.canvasTileSize - 1); }
static void Main(string[] args) { var font = new TrueTypeFont(new FileStream(@"Anonymous/Anonymous Pro.ttf", FileMode.Open)); // 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"); // TODO: implement serialization equivalent that is compatible with a Portable Class Library ... }