#region MIT License /*Copyright (c) 2012 Robert Rouhani SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ #endregion using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using SharpFont.Internal; namespace SharpFont { /// /// A structure used to describe a bitmap or pixmap to the raster. Note that we now manage pixmaps of various /// depths through the field. /// /// /// For now, the only pixel modes supported by FreeType are mono and grays. However, drivers might be added in the /// future to support more ‘colorful’ options. /// public sealed class FTBitmap : IDisposable { #region Fields private IntPtr reference; private BitmapRec rec; private Library library; private bool disposed; //If the bitmap was generated with FT_Bitmap_New. private bool user; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The parent . public FTBitmap(Library library) { IntPtr bitmapRef; FT.FT_Bitmap_New(out bitmapRef); Reference = bitmapRef; this.library = library; this.user = true; } internal FTBitmap(IntPtr reference, Library library) { Reference = reference; this.library = library; user = true; } internal FTBitmap(BitmapRec bmpInt, Library library) { this.rec = bmpInt; this.library = library; } internal FTBitmap(IntPtr reference) : this(reference, null) { user = true; } internal FTBitmap(BitmapRec bmpInt) : this(bmpInt, null) { } /// /// Finalizes an instance of the class. /// ~FTBitmap() { Dispose(false); } #endregion #region Properties /// /// Gets a value indicating whether the has been disposed. /// public bool IsDisposed { get { return disposed; } } /// /// Gets the number of bitmap rows. /// public int Rows { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.rows; } } /// /// Gets the number of pixels in bitmap row. /// public int Width { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.width; } } /// /// Gets the pitch's absolute value is the number of bytes taken by one bitmap row, including padding. However, /// the pitch is positive when the bitmap has a ‘down’ flow, and negative when it has an ‘up’ flow. In all /// cases, the pitch is an offset to add to a bitmap pointer in order to go down one row. /// /// Note that ‘padding’ means the alignment of a bitmap to a byte border, and FreeType functions normally align /// to the smallest possible integer value. /// /// For the B/W rasterizer, ‘pitch’ is always an even number. /// /// To change the pitch of a bitmap (say, to make it a multiple of 4), use . /// Alternatively, you might use callback functions to directly render to the application's surface; see the /// file ‘example2.cpp’ in the tutorial for a demonstration. /// public int Pitch { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.pitch; } } /// /// Gets a typeless pointer to the bitmap buffer. This value should be aligned on 32-bit boundaries in most /// cases. /// public IntPtr Buffer { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.buffer; } } /// /// Gets the number of gray levels used in the bitmap. This field is only used with /// . /// public short GrayLevels { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.num_grays; } } /// /// Gets the pixel mode, i.e., how pixel bits are stored. /// public PixelMode PixelMode { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.pixel_mode; } } /// /// Gets how the palette is stored. This field is intended for paletted pixel modes. /// [Obsolete("Not used currently.")] public byte PaletteMode { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.palette_mode; } } /// /// Gets a typeless pointer to the bitmap palette; this field is intended for paletted pixel modes. /// [Obsolete("Not used currently.")] public IntPtr Palette { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return rec.palette; } } /// /// Gets the 's buffer as a byte array. /// public byte[] BufferData { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); byte[] data = new byte[rec.rows * rec.width]; Marshal.Copy(rec.buffer, data, 0, data.Length); return data; } } internal IntPtr Reference { get { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); return reference; } set { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); reference = value; rec = PInvokeHelper.PtrToStructure(reference); } } #endregion #region Methods /// /// Copy a bitmap into another one. /// /// A handle to a library object. /// A handle to the target bitmap. public FTBitmap Copy(Library library) { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); if (library == null) throw new ArgumentNullException("library"); IntPtr bitmapRef; Error err = FT.FT_Bitmap_Copy(library.Reference, Reference, out bitmapRef); if (err != Error.Ok) throw new FreeTypeException(err); return new FTBitmap(bitmapRef); } /// /// Embolden a bitmap. The new bitmap will be about ‘xStrength’ pixels wider and ‘yStrength’ pixels higher. The /// left and bottom borders are kept unchanged. /// /// /// The current implementation restricts ‘xStrength’ to be less than or equal to 8 if bitmap is of pixel_mode /// . /// /// If you want to embolden the bitmap owned by a , you should call /// on the slot first. /// /// A handle to a library object. /// /// How strong the glyph is emboldened horizontally. Expressed in 26.6 pixel format. /// /// /// How strong the glyph is emboldened vertically. Expressed in 26.6 pixel format. /// public void Embolden(Library library, int xStrength, int yStrength) { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); if (library == null) throw new ArgumentNullException("library"); Error err = FT.FT_Bitmap_Embolden(library.Reference, Reference, xStrength, yStrength); if (err != Error.Ok) throw new FreeTypeException(err); } /// /// Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, or 8bpp to a bitmap object with depth 8bpp, making the /// number of used bytes per line (a.k.a. the ‘pitch’) a multiple of ‘alignment’. /// /// /// It is possible to call multiple times without calling /// (the memory is simply reallocated). /// /// Use to finally remove the bitmap object. /// /// The ‘library’ argument is taken to have access to FreeType's memory handling functions. /// /// A handle to a library object. /// /// The pitch of the bitmap is a multiple of this parameter. Common values are 1, 2, or 4. /// /// The target bitmap. public FTBitmap Convert(Library library, int alignment) { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); if (library == null) throw new ArgumentNullException("library"); IntPtr bitmapRef; Error err = FT.FT_Bitmap_Convert(library.Reference, Reference, out bitmapRef, alignment); if (err != Error.Ok) throw new FreeTypeException(err); return new FTBitmap(bitmapRef); } /// /// Copies the contents of the to a . /// /// A containing this bitmap's data. public Bitmap ToGdipBitmap() { if (disposed) throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object."); switch (rec.pixel_mode) { case PixelMode.Mono: { Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format1bppIndexed); var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows); bmp.UnlockBits(locked); bmp.Palette.Entries[0] = Color.FromArgb(0, 0, 0, 0); bmp.Palette.Entries[1] = Color.FromArgb(1, 0, 0, 0); return bmp; } case PixelMode.Gray4: { Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format4bppIndexed); var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format4bppIndexed); Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows); bmp.UnlockBits(locked); for (int i = 0; i < 16; i++) { bmp.Palette.Entries[i] = Color.FromArgb((int)((i / 15) * 255), 0, 0, 0); } return bmp; } case PixelMode.Gray: { Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format8bppIndexed); var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows); bmp.UnlockBits(locked); for (int i = 0; i < 256; i++) { bmp.Palette.Entries[i] = Color.FromArgb(i, 0, 0, 0); } return bmp; } case PixelMode.Lcd: case PixelMode.VerticalLcd: { //TODO Should vertical LCD be different? Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format24bppRgb); var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows); bmp.UnlockBits(locked); for (int i = 0; i < 256; i++) { bmp.Palette.Entries[i] = Color.FromArgb(i, 0, 0, 0); } return bmp; } default: throw new InvalidOperationException("System.Drawing.Bitmap does not support this pixel mode."); } } #region IDisposable /// /// Disposes an instance of the class. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!disposed) { disposed = true; if (user) { Error err = FT.FT_Bitmap_Done(library.Reference, reference); if (err != Error.Ok) throw new FreeTypeException(err); } reference = IntPtr.Zero; library = null; } } #endregion #endregion } }