/// <summary> /// Creates a bitmap for each character in the font. /// Characters that the font does not include will not be assigned a bitmap. /// </summary> private void CreateBitmaps() { characters = new Dictionary <char, PixelSet>(); ImageSurface surface = new ImageSurface(Format.RGB24, 1, 1); Context context = new Context(surface); context.SelectFontFace(FontName, FontSlant.Normal, FontWeight.Normal); context.SetFontSize(Font.Size); TextExtents ext = context.TextExtents("@"); int charW = (int)Math.Ceiling(ext.Width); int charH = (int)Math.Ceiling(ext.Height); context.Dispose(); surface.Dispose(); surface = new ImageSurface(Format.RGB24, charW, charH); context = new Context(surface); PixelSet missingChar = null; for (int i = 0; i < asciiString.Length; i++) { // Render one character into a PixelSet string asciiChar = string.Empty + asciiString[i]; context.SelectFontFace(FontName, FontSlant.Normal, FontWeight.Normal); context.SetFontSize(Font.Size); context.SetSourceRGB(0.067, 0.067, 0.067); context.Paint(); context.SetSourceRGB(1, 1, 1); ext = context.TextExtents(asciiChar); context.MoveTo(ext.XBearing, ext.YBearing * -1); context.ShowText(asciiChar); PixelSet ch = new PixelSet(surface); // Filter out characters the font doesn't include // The first character is always unprintable, and serves as // a reference for what unprintable characters look like in this font if (i == 0) { missingChar = ch; continue; } else if (ch == missingChar) { continue; } characters.Add(asciiString[i], ch); } context.Dispose(); surface.Dispose(); // Add the space manually if it wasn't included if (!characters.ContainsKey(' ')) { var en = characters.Values.GetEnumerator(); en.MoveNext(); characters.Add(' ', new PixelSet(en.Current.Width, en.Current.Height)); } }
/// <summary> /// Inverts the color of a PixelSet. /// </summary> /// <returns>Inverted PixelSet</returns> public PixelSet Invert() { PixelSet p = new PixelSet(Width, Height); for (uint x = 0; x < Width; x++) { for (uint y = 0; y < Height; y++) { p.Pixels[0, x][y] = (byte)(255 - pixels[0, x][y]); p.Pixels[1, x][y] = (byte)(255 - pixels[1, x][y]); p.Pixels[2, x][y] = (byte)(255 - pixels[2, x][y]); } } return(p); }
/// <summary> /// Gets a list of all PixelSets that are associated to characters. /// </summary> /// <returns>Array of PixelSets</returns> public PixelSet[] GetCharacterPixels() { PixelSet[] chars = new PixelSet[characters.Count]; characters.Values.CopyTo(chars, 0); return(chars); }