// Imports the source font associated to the given command line options. public void Import(CommandLineOptions options) { if (options == null) { throw new NullReferenceException("The given command line options may not be equal to null."); } // Load the source bitmap. Bitmap bitmap; try { bitmap = new Bitmap(options.SourceFont); } catch { throw new Exception(string.Format("Unable to load '{0}'.", options.SourceFont)); } // Convert to our desired pixel format. bitmap = BitmapUtils.ConvertToPixelFormat(bitmap, PixelFormat.Format32bppArgb); // What characters are included in this font? var characters = CharacterRegion.Flatten(options.CharacterRegions).ToArray(); 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 region in BitmapUtils.GetGlyphRegions(bitmap, IsMarkerColor)) { if (characterIndex < characters.Length) { currentCharacter = characters[characterIndex]; } ++currentCharacter; glyphList.Add(new Glyph(currentCharacter, bitmap, region)); LineSpacing = Math.Max(LineSpacing, region.Height); } // If the bitmap doesn't already have an alpha channel, create one now. if (BitmapUtils.MatchesAlpha(255, bitmap)) { BitmapUtils.ConvertGreyToAlpha(bitmap); } }
// Crops unused space from around the edge of a glyph bitmap. public static void Crop(Glyph glyph) { if (glyph == null) { throw new NullReferenceException("The given glyph may not be equal to null."); } // Crop the top. while ((glyph.Region.Height > 1) && BitmapUtils.MatchesAlpha(0, glyph.Bitmap, new Rectangle(glyph.Region.X, glyph.Region.Y, glyph.Region.Width, 1))) { ++glyph.Region.Y; --glyph.Region.Height; ++glyph.OffsetY; } // Crop the bottom. while ((glyph.Region.Height > 1) && BitmapUtils.MatchesAlpha(0, glyph.Bitmap, new Rectangle(glyph.Region.X, glyph.Region.Bottom - 1, glyph.Region.Width, 1))) { --glyph.Region.Height; } // Crop the left. while ((glyph.Region.Width > 1) && BitmapUtils.MatchesAlpha(0, glyph.Bitmap, new Rectangle(glyph.Region.X, glyph.Region.Y, 1, glyph.Region.Height))) { ++glyph.Region.X; --glyph.Region.Width; ++glyph.OffsetX; } // Crop the right. while ((glyph.Region.Width > 1) && BitmapUtils.MatchesAlpha(0, glyph.Bitmap, new Rectangle(glyph.Region.Right - 1, glyph.Region.Y, 1, glyph.Region.Height))) { --glyph.Region.Width; ++glyph.AdvanceX; } }