public void Import(FontDescription options) { // Create a bunch of GDI+ objects. using (Font font = CreateFont(options)) using (Brush brush = new SolidBrush(Color.White)) using (StringFormat stringFormat = new StringFormat(StringFormatFlags.NoFontFallback)) using (Bitmap bitmap = new Bitmap(MaxGlyphSize, MaxGlyphSize, PixelFormat.Format32bppArgb)) using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap)) { graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Which characters do we want to include? var characters = CharacterRegion.Flatten(options.CharacterRegions); var glyphList = new List <Glyph>(); // Rasterize each character in turn. foreach (char character in characters) { Glyph glyph = ImportGlyph(character, font, brush, stringFormat, bitmap, graphics); glyphList.Add(glyph); } Glyphs = glyphList; // Store the font height. LineSpacing = font.GetHeight(); } }
public void Import(FontDescription options) { // Load the source bitmap. Bitmap bitmap; try { bitmap = new Bitmap(options.FontName); } catch { throw new FontException(string.Format("Unable to load '{0}'.", options.FontName)); } // Convert to our desired pixel format. bitmap = BitmapUtils.ChangePixelFormat(bitmap, PixelFormat.Format32bppArgb); // What characters are included in this font? var characters = Utilities.ToArray(CharacterRegion.Flatten(options.CharacterRegions)); 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.Length) { currentCharacter = characters[characterIndex++]; } else { currentCharacter++; } glyphList.Add(new Glyph(currentCharacter, bitmap, rectangle)); 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); } }
public void Import(FontDescription options) { var factory = new DirectWrite.Factory(); DirectWrite.Font font = null; using (var fontCollection = factory.GetSystemFontCollection(false)) { int index; if (!fontCollection.FindFamilyName(options.FontName, out index)) { // Lets try to import System.Drawing for old system bitmap fonts (like MS Sans Serif) throw new FontException(string.Format("Can't find font '{0}'.", options.FontName)); } using (var fontFamily = fontCollection.GetFontFamily(index)) { var weight = FontWeight.Regular; var style = DirectWrite.FontStyle.Normal; switch (options.Style) { case FontStyle.Bold: weight = FontWeight.Bold; break; case FontStyle.Italic: weight = FontWeight.Regular; style = DirectWrite.FontStyle.Italic; break; case FontStyle.Regular: weight = FontWeight.Regular; break; } font = fontFamily.GetFirstMatchingFont(weight, DirectWrite.FontStretch.Normal, style); } } var fontFace = new FontFace(font); var fontMetrics = fontFace.Metrics; // Create a bunch of GDI+ objects. var fontSize = PointsToPixels(options.Size); // Which characters do we want to include? var characters = CharacterRegion.Flatten(options.CharacterRegions); var glyphList = new List <Glyph>(); // Store the font height. LineSpacing = (float)(fontMetrics.LineGap + fontMetrics.Ascent + fontMetrics.Descent) / fontMetrics.DesignUnitsPerEm * fontSize; var baseLine = (float)(fontMetrics.LineGap + fontMetrics.Ascent) / fontMetrics.DesignUnitsPerEm * fontSize; // Rasterize each character in turn. foreach (char character in characters) { var glyph = ImportGlyph(factory, fontFace, character, fontMetrics, fontSize, options.AntiAlias); glyph.YOffset += baseLine; glyphList.Add(glyph); } Glyphs = glyphList; factory.Dispose(); }