public static void Crop(Glyph glyph) { // Crop the top. while ((glyph.Subrect.Height > 1) && BitmapUtils.IsAlphaEntirely(0, glyph.Bitmap, new Rectangle(glyph.Subrect.X, glyph.Subrect.Y, glyph.Subrect.Width, 1))) { glyph.Subrect.Y++; glyph.Subrect.Height--; glyph.YOffset++; } // Crop the bottom. while ((glyph.Subrect.Height > 1) && BitmapUtils.IsAlphaEntirely(0, glyph.Bitmap, new Rectangle(glyph.Subrect.X, glyph.Subrect.Bottom - 1, glyph.Subrect.Width, 1))) { glyph.Subrect.Height--; } // Crop the left. while ((glyph.Subrect.Width > 1) && BitmapUtils.IsAlphaEntirely(0, glyph.Bitmap, new Rectangle(glyph.Subrect.X, glyph.Subrect.Y, 1, glyph.Subrect.Height))) { glyph.Subrect.X++; glyph.Subrect.Width--; glyph.XOffset++; } // Crop the right. while ((glyph.Subrect.Width > 1) && BitmapUtils.IsAlphaEntirely(0, glyph.Bitmap, new Rectangle(glyph.Subrect.Right - 1, glyph.Subrect.Y, 1, glyph.Subrect.Height))) { glyph.Subrect.Width--; } }
public void Import(SpriteFontAsset options, List <char> characters) { // Load the source bitmap. Bitmap bitmap; try { // TODO Check if source can be used as is from here bitmap = new Bitmap(options.FontSource.GetFontPath()); } catch { throw new FontNotFoundException(options.FontSource.GetFontPath()); } // Convert to our desired pixel format. bitmap = BitmapUtils.ChangePixelFormat(bitmap, PixelFormat.Format32bppArgb); // What characters are included in this font? int characterIndex = 0; char currentCharacter = '\0'; // Split the source image into a list of individual glyphs. var glyphList = new List <Glyph>(); Glyphs = glyphList; LineSpacing = 0; foreach (Rectangle rectangle in FindGlyphs(bitmap)) { if (characterIndex < characters.Count) { currentCharacter = characters[characterIndex++]; } else { currentCharacter++; } glyphList.Add(new Glyph(currentCharacter, bitmap, rectangle) { XAdvance = rectangle.Width }); LineSpacing = Math.Max(LineSpacing, rectangle.Height); } // If the bitmap doesn't already have an alpha channel, create one now. if (BitmapUtils.IsAlphaEntirely(255, bitmap)) { BitmapUtils.ConvertGreyToAlpha(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); } }