FTBitmap.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #region MIT License
  2. /*Copyright (c) 2012 Robert Rouhani <robert.rouhani@gmail.com>
  3. SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team
  4. SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.*/
  20. #endregion
  21. using System;
  22. using System.Drawing;
  23. using System.Drawing.Imaging;
  24. using System.Runtime.InteropServices;
  25. using SharpFont.Internal;
  26. namespace SharpFont
  27. {
  28. /// <summary>
  29. /// A structure used to describe a bitmap or pixmap to the raster. Note that we now manage pixmaps of various
  30. /// depths through the <see cref="PixelMode"/> field.
  31. /// </summary>
  32. /// <remarks>
  33. /// For now, the only pixel modes supported by FreeType are mono and grays. However, drivers might be added in the
  34. /// future to support more ‘colorful’ options.
  35. /// </remarks>
  36. public sealed class FTBitmap : IDisposable
  37. {
  38. #region Fields
  39. private IntPtr reference;
  40. private BitmapRec rec;
  41. private Library library;
  42. private bool disposed;
  43. //If the bitmap was generated with FT_Bitmap_New.
  44. private bool user;
  45. #endregion
  46. #region Constructors
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="FTBitmap"/> class.
  49. /// </summary>
  50. /// <param name="library">The parent <see cref="Library"/>.</param>
  51. public FTBitmap(Library library)
  52. {
  53. IntPtr bitmapRef;
  54. FT.FT_Bitmap_New(out bitmapRef);
  55. Reference = bitmapRef;
  56. this.library = library;
  57. this.user = true;
  58. }
  59. internal FTBitmap(IntPtr reference, Library library)
  60. {
  61. Reference = reference;
  62. this.library = library;
  63. user = true;
  64. }
  65. internal FTBitmap(BitmapRec bmpInt, Library library)
  66. {
  67. this.rec = bmpInt;
  68. this.library = library;
  69. }
  70. internal FTBitmap(IntPtr reference)
  71. : this(reference, null)
  72. {
  73. user = true;
  74. }
  75. internal FTBitmap(BitmapRec bmpInt)
  76. : this(bmpInt, null)
  77. {
  78. }
  79. /// <summary>
  80. /// Finalizes an instance of the <see cref="FTBitmap"/> class.
  81. /// </summary>
  82. ~FTBitmap()
  83. {
  84. Dispose(false);
  85. }
  86. #endregion
  87. #region Properties
  88. /// <summary>
  89. /// Gets a value indicating whether the <see cref="FTBitmap"/> has been disposed.
  90. /// </summary>
  91. public bool IsDisposed
  92. {
  93. get
  94. {
  95. return disposed;
  96. }
  97. }
  98. /// <summary>
  99. /// Gets the number of bitmap rows.
  100. /// </summary>
  101. public int Rows
  102. {
  103. get
  104. {
  105. if (disposed)
  106. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  107. return rec.rows;
  108. }
  109. }
  110. /// <summary>
  111. /// Gets the number of pixels in bitmap row.
  112. /// </summary>
  113. public int Width
  114. {
  115. get
  116. {
  117. if (disposed)
  118. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  119. return rec.width;
  120. }
  121. }
  122. /// <summary><para>
  123. /// Gets the pitch's absolute value is the number of bytes taken by one bitmap row, including padding. However,
  124. /// the pitch is positive when the bitmap has a ‘down’ flow, and negative when it has an ‘up’ flow. In all
  125. /// cases, the pitch is an offset to add to a bitmap pointer in order to go down one row.
  126. /// </para><para>
  127. /// Note that ‘padding’ means the alignment of a bitmap to a byte border, and FreeType functions normally align
  128. /// to the smallest possible integer value.
  129. /// </para><para>
  130. /// For the B/W rasterizer, ‘pitch’ is always an even number.
  131. /// </para><para>
  132. /// To change the pitch of a bitmap (say, to make it a multiple of 4), use <see cref="FTBitmap.Convert"/>.
  133. /// Alternatively, you might use callback functions to directly render to the application's surface; see the
  134. /// file ‘example2.cpp’ in the tutorial for a demonstration.
  135. /// </para></summary>
  136. public int Pitch
  137. {
  138. get
  139. {
  140. if (disposed)
  141. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  142. return rec.pitch;
  143. }
  144. }
  145. /// <summary>
  146. /// Gets a typeless pointer to the bitmap buffer. This value should be aligned on 32-bit boundaries in most
  147. /// cases.
  148. /// </summary>
  149. public IntPtr Buffer
  150. {
  151. get
  152. {
  153. if (disposed)
  154. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  155. return rec.buffer;
  156. }
  157. }
  158. /// <summary>
  159. /// Gets the number of gray levels used in the bitmap. This field is only used with
  160. /// <see cref="SharpFont.PixelMode.Gray"/>.
  161. /// </summary>
  162. public short GrayLevels
  163. {
  164. get
  165. {
  166. if (disposed)
  167. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  168. return rec.num_grays;
  169. }
  170. }
  171. /// <summary>
  172. /// Gets the pixel mode, i.e., how pixel bits are stored.
  173. /// </summary>
  174. public PixelMode PixelMode
  175. {
  176. get
  177. {
  178. if (disposed)
  179. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  180. return rec.pixel_mode;
  181. }
  182. }
  183. /// <summary>
  184. /// Gets how the palette is stored. This field is intended for paletted pixel modes.
  185. /// </summary>
  186. [Obsolete("Not used currently.")]
  187. public byte PaletteMode
  188. {
  189. get
  190. {
  191. if (disposed)
  192. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  193. return rec.palette_mode;
  194. }
  195. }
  196. /// <summary>
  197. /// Gets a typeless pointer to the bitmap palette; this field is intended for paletted pixel modes.
  198. /// </summary>
  199. [Obsolete("Not used currently.")]
  200. public IntPtr Palette
  201. {
  202. get
  203. {
  204. if (disposed)
  205. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  206. return rec.palette;
  207. }
  208. }
  209. /// <summary>
  210. /// Gets the <see cref="FTBitmap"/>'s buffer as a byte array.
  211. /// </summary>
  212. public byte[] BufferData
  213. {
  214. get
  215. {
  216. if (disposed)
  217. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  218. byte[] data = new byte[rec.rows * rec.width];
  219. Marshal.Copy(rec.buffer, data, 0, data.Length);
  220. return data;
  221. }
  222. }
  223. internal IntPtr Reference
  224. {
  225. get
  226. {
  227. if (disposed)
  228. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  229. return reference;
  230. }
  231. set
  232. {
  233. if (disposed)
  234. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  235. reference = value;
  236. rec = PInvokeHelper.PtrToStructure<BitmapRec>(reference);
  237. }
  238. }
  239. #endregion
  240. #region Methods
  241. /// <summary>
  242. /// Copy a bitmap into another one.
  243. /// </summary>
  244. /// <param name="library">A handle to a library object.</param>
  245. /// <returns>A handle to the target bitmap.</returns>
  246. public FTBitmap Copy(Library library)
  247. {
  248. if (disposed)
  249. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  250. if (library == null)
  251. throw new ArgumentNullException("library");
  252. IntPtr bitmapRef;
  253. Error err = FT.FT_Bitmap_Copy(library.Reference, Reference, out bitmapRef);
  254. if (err != Error.Ok)
  255. throw new FreeTypeException(err);
  256. return new FTBitmap(bitmapRef);
  257. }
  258. /// <summary>
  259. /// Embolden a bitmap. The new bitmap will be about ‘xStrength’ pixels wider and ‘yStrength’ pixels higher. The
  260. /// left and bottom borders are kept unchanged.
  261. /// </summary>
  262. /// <remarks><para>
  263. /// The current implementation restricts ‘xStrength’ to be less than or equal to 8 if bitmap is of pixel_mode
  264. /// <see cref="SharpFont.PixelMode.Mono"/>.
  265. /// </para><para>
  266. /// If you want to embolden the bitmap owned by a <see cref="GlyphSlot"/>, you should call
  267. /// <see cref="GlyphSlot.OwnBitmap"/> on the slot first.
  268. /// </para></remarks>
  269. /// <param name="library">A handle to a library object.</param>
  270. /// <param name="xStrength">
  271. /// How strong the glyph is emboldened horizontally. Expressed in 26.6 pixel format.
  272. /// </param>
  273. /// <param name="yStrength">
  274. /// How strong the glyph is emboldened vertically. Expressed in 26.6 pixel format.
  275. /// </param>
  276. public void Embolden(Library library, int xStrength, int yStrength)
  277. {
  278. if (disposed)
  279. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  280. if (library == null)
  281. throw new ArgumentNullException("library");
  282. Error err = FT.FT_Bitmap_Embolden(library.Reference, Reference, xStrength, yStrength);
  283. if (err != Error.Ok)
  284. throw new FreeTypeException(err);
  285. }
  286. /// <summary>
  287. /// Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, or 8bpp to a bitmap object with depth 8bpp, making the
  288. /// number of used bytes per line (a.k.a. the ‘pitch’) a multiple of ‘alignment’.
  289. /// </summary>
  290. /// <remarks><para>
  291. /// It is possible to call <see cref="Convert"/> multiple times without calling
  292. /// <see cref="Dispose()"/> (the memory is simply reallocated).
  293. /// </para><para>
  294. /// Use <see cref="Dispose()"/> to finally remove the bitmap object.
  295. /// </para><para>
  296. /// The ‘library’ argument is taken to have access to FreeType's memory handling functions.
  297. /// </para></remarks>
  298. /// <param name="library">A handle to a library object.</param>
  299. /// <param name="alignment">
  300. /// The pitch of the bitmap is a multiple of this parameter. Common values are 1, 2, or 4.
  301. /// </param>
  302. /// <returns>The target bitmap.</returns>
  303. public FTBitmap Convert(Library library, int alignment)
  304. {
  305. if (disposed)
  306. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  307. if (library == null)
  308. throw new ArgumentNullException("library");
  309. IntPtr bitmapRef;
  310. Error err = FT.FT_Bitmap_Convert(library.Reference, Reference, out bitmapRef, alignment);
  311. if (err != Error.Ok)
  312. throw new FreeTypeException(err);
  313. return new FTBitmap(bitmapRef);
  314. }
  315. /// <summary>
  316. /// Copies the contents of the <see cref="FTBitmap"/> to a <see cref="Bitmap"/>.
  317. /// </summary>
  318. /// <returns>A <see cref="Bitmap"/> containing this bitmap's data.</returns>
  319. public Bitmap ToGdipBitmap()
  320. {
  321. if (disposed)
  322. throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
  323. switch (rec.pixel_mode)
  324. {
  325. case PixelMode.Mono:
  326. {
  327. Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format1bppIndexed);
  328. var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
  329. Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
  330. bmp.UnlockBits(locked);
  331. bmp.Palette.Entries[0] = Color.FromArgb(0, 0, 0, 0);
  332. bmp.Palette.Entries[1] = Color.FromArgb(1, 0, 0, 0);
  333. return bmp;
  334. }
  335. case PixelMode.Gray4:
  336. {
  337. Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format4bppIndexed);
  338. var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format4bppIndexed);
  339. Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
  340. bmp.UnlockBits(locked);
  341. for (int i = 0; i < 16; i++)
  342. {
  343. bmp.Palette.Entries[i] = Color.FromArgb((int)((i / 15) * 255), 0, 0, 0);
  344. }
  345. return bmp;
  346. }
  347. case PixelMode.Gray:
  348. {
  349. Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format8bppIndexed);
  350. var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
  351. Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
  352. bmp.UnlockBits(locked);
  353. for (int i = 0; i < 256; i++)
  354. {
  355. bmp.Palette.Entries[i] = Color.FromArgb(i, 0, 0, 0);
  356. }
  357. return bmp;
  358. }
  359. case PixelMode.Lcd:
  360. case PixelMode.VerticalLcd:
  361. {
  362. //TODO Should vertical LCD be different?
  363. Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format24bppRgb);
  364. var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  365. Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
  366. bmp.UnlockBits(locked);
  367. for (int i = 0; i < 256; i++)
  368. {
  369. bmp.Palette.Entries[i] = Color.FromArgb(i, 0, 0, 0);
  370. }
  371. return bmp;
  372. }
  373. default:
  374. throw new InvalidOperationException("System.Drawing.Bitmap does not support this pixel mode.");
  375. }
  376. }
  377. #region IDisposable
  378. /// <summary>
  379. /// Disposes an instance of the <see cref="FTBitmap"/> class.
  380. /// </summary>
  381. public void Dispose()
  382. {
  383. Dispose(true);
  384. GC.SuppressFinalize(this);
  385. }
  386. private void Dispose(bool disposing)
  387. {
  388. if (!disposed)
  389. {
  390. disposed = true;
  391. if (user)
  392. {
  393. Error err = FT.FT_Bitmap_Done(library.Reference, reference);
  394. if (err != Error.Ok)
  395. throw new FreeTypeException(err);
  396. }
  397. reference = IntPtr.Zero;
  398. library = null;
  399. }
  400. }
  401. #endregion
  402. #endregion
  403. }
  404. }