Example #1
0
        private static void ProcessFile(string inputFilename, string outputFilename, Regex keep, string keepPattern)
        {
            var inputFont = AdafruitGFXFontParser.ReadFile(inputFilename);

            var bitmap = new MemoryStream();
            var glyphs = new List <GFXglyph>();

            var  currentChar            = inputFont.FirstChar;
            char?firstChar              = null;
            char?lastChar               = null;
            var  matchedGlyphsCount     = 0;
            var  matchedGlyphsLastCount = 0;


            foreach (var glyph in inputFont.Glyphs)
            {
                if (keep.IsMatch(currentChar.ToString()))
                {
                    glyphs.Add(new GFXglyph
                    {
                        bitmapOffset = (int)bitmap.Length,
                        width        = glyph.width,
                        height       = glyph.height,
                        xAdvance     = glyph.xAdvance,
                        xOffset      = glyph.xOffset,
                        yOffset      = glyph.yOffset
                    });

                    var bitmapSize = (int)Math.Ceiling((glyph.width * glyph.height) / 8.0);
                    bitmap.Write(inputFont.Bitmap, glyph.bitmapOffset, bitmapSize);


                    if (!firstChar.HasValue)
                    {
                        firstChar = currentChar;
                    }

                    lastChar = currentChar;

                    matchedGlyphsCount++;
                    matchedGlyphsLastCount = glyphs.Count;
                }
                else if (firstChar.HasValue)
                {
                    // No gaps are allowed, insert dummy glyph
                    glyphs.Add(new GFXglyph
                    {
                        bitmapOffset = 0,
                        width        = 0,
                        height       = 0,
                        xAdvance     = 0,
                        xOffset      = 0,
                        yOffset      = 0
                    });
                }

                currentChar++;
            }


            if (!firstChar.HasValue)
            {
                throw new ArgumentException("No characters match the specified filter");
            }


            // Remove the empty glyphs at the end
            if (glyphs.Count > matchedGlyphsLastCount)
            {
                glyphs.RemoveRange(matchedGlyphsLastCount, glyphs.Count - matchedGlyphsLastCount);
            }


            var outputFont = new AdafruitGFXFont
            {
                FontName  = inputFont.FontName + "Trimmed",
                FirstChar = firstChar.Value,
                LastChar  = lastChar.Value,
                YAdvance  = inputFont.YAdvance,

                Bitmap = bitmap.ToArray(),
                Glyphs = glyphs
            };


            Console.WriteLine($"         Kept {matchedGlyphsCount} of {inputFont.Glyphs.Count} glyphs, ~ {inputFont.SizeEstimate} > {outputFont.SizeEstimate} bytes");
            Console.WriteLine();


            var output = AdafruitGFXFontWriter.WriteString(outputFont);

            using (var file = new StreamWriter(outputFilename))
            {
                file.WriteLine("/*");
                file.WriteLine("");
                file.WriteLine("  Generated by AdafruitGFXFontToBitmap");
                file.WriteLine("  https: //github.com/MvRens/AdafruitGFXFontTools");
                file.WriteLine("");
                file.WriteLine("  Source: " + Path.GetFileName(inputFilename));
                file.WriteLine("  Filter: " + CommentSafeRegex(keepPattern));
                file.WriteLine("");
                file.WriteLine("*/");
                file.WriteLine("");

                file.Write(output);
            }
        }
Example #2
0
        private static void ProcessFile(string inputFilename, string outputFilename)
        {
            var font = AdafruitGFXFontParser.ReadFile(inputFilename);

            var minYOffset = font.Glyphs.Min(g => g.yOffset);
            var maxYOffset = font.Glyphs.Max(g => g.height + g.yOffset);

            var totalWidth = font.Glyphs.Sum(g => g.ActualWidth) + (font.Glyphs.Count - 1);
            var maxHeight  = maxYOffset - minYOffset;
            var cursorY    = -minYOffset;

            var outputBitmap = new Bitmap(totalWidth, maxHeight);
            var graphics     = Graphics.FromImage(outputBitmap);

            graphics.Clear(Color.Black);

            var glyphX = 0;

            foreach (var glyph in font.Glyphs)
            {
                var bitmapOffset = glyph.bitmapOffset;
                var bitmapBit    = 0;
                var yOffset      = cursorY + glyph.yOffset;

                for (var y = glyph.Bounds.Top + yOffset; y < glyph.Bounds.Bottom + yOffset; y++)
                {
                    for (var x = glyph.Bounds.Left; x < glyph.Bounds.Right; x++)
                    {
                        var pixel = font.Bitmap[bitmapOffset] & (1 << (7 - bitmapBit));
                        if (pixel > 0)
                        {
                            outputBitmap.SetPixel(glyphX + x, y, Color.White);
                        }

                        bitmapBit++;

                        // ReSharper disable once InvertIf
                        if (bitmapBit > 7)
                        {
                            bitmapOffset++;
                            bitmapBit = 0;
                        }
                    }
                }


                glyphX += glyph.ActualWidth;
                if (glyphX >= outputBitmap.Width)
                {
                    continue;
                }


                // Draw separator line
                for (var y = 0; y < maxHeight; y++)
                {
                    outputBitmap.SetPixel(glyphX, y, Color.DimGray);
                }

                glyphX++;
            }

            outputBitmap.Save(outputFilename, ImageFormat.Bmp);
        }