Exemplo n.º 1
0
        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");
                }
            }
        }
Exemplo n.º 2
0
        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");
                }
            }
        }