SBitCache.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.*/
  19. #endregion
  20. using System;
  21. using System.Runtime.InteropServices;
  22. namespace SharpFont.Cache
  23. {
  24. /// <summary>
  25. /// A handle to a small bitmap cache. These are special cache objects used to store small glyph bitmaps (and
  26. /// anti-aliased pixmaps) in a much more efficient way than the traditional glyph image cache implemented by
  27. /// <see cref="ImageCache"/>.
  28. /// </summary>
  29. public class SBitCache
  30. {
  31. #region Fields
  32. private IntPtr reference;
  33. private Manager parentManager;
  34. #endregion
  35. #region Constructors
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="SBitCache"/> class.
  38. /// </summary>
  39. /// <param name="manager">A handle to the source cache manager.</param>
  40. public SBitCache(Manager manager)
  41. {
  42. if (manager == null)
  43. throw new ArgumentNullException("manager");
  44. IntPtr cacheRef;
  45. Error err = FT.FTC_SBitCache_New(manager.Reference, out cacheRef);
  46. if (err != Error.Ok)
  47. throw new FreeTypeException(err);
  48. Reference = cacheRef;
  49. parentManager = manager;
  50. }
  51. #endregion
  52. #region Properties
  53. internal IntPtr Reference
  54. {
  55. get
  56. {
  57. return reference;
  58. }
  59. set
  60. {
  61. reference = value;
  62. }
  63. }
  64. #endregion
  65. #region Methods
  66. /// <summary>
  67. /// Look up a given small glyph bitmap in a given sbit cache and ‘lock’ it to prevent its flushing from the
  68. /// cache until needed.
  69. /// </summary>
  70. /// <remarks><para>
  71. /// The small bitmap descriptor and its bit buffer are owned by the cache and should never be freed by the
  72. /// application. They might as well disappear from memory on the next cache lookup, so don't treat them as
  73. /// persistent data.
  74. /// </para><para>
  75. /// The descriptor's ‘buffer’ field is set to 0 to indicate a missing glyph bitmap.
  76. /// </para><para>
  77. /// If ‘node’ is not NULL, it receives the address of the cache node containing the bitmap, after
  78. /// increasing its reference count. This ensures that the node (as well as the image) will always be kept in
  79. /// the cache until you call <see cref="Node.Unref"/> to ‘release’ it.
  80. /// </para><para>
  81. /// If ‘node’ is NULL, the cache node is left unchanged, which means that the bitmap could be
  82. /// flushed out of the cache on the next call to one of the caching sub-system APIs. Don't assume that it is
  83. /// persistent!
  84. /// </para></remarks>
  85. /// <param name="type">A pointer to the glyph image type descriptor.</param>
  86. /// <param name="gIndex">The glyph index.</param>
  87. /// <param name="node">
  88. /// Used to return the address of of the corresponding cache node after incrementing its reference count (see
  89. /// note below).
  90. /// </param>
  91. /// <returns>A handle to a small bitmap descriptor.</returns>
  92. [CLSCompliant(false)]
  93. public SBit Lookup(ImageType type, uint gIndex, out Node node)
  94. {
  95. if (parentManager.IsDisposed)
  96. throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
  97. IntPtr sbitRef, nodeRef;
  98. Error err = FT.FTC_SBitCache_Lookup(Reference, type.Reference, gIndex, out sbitRef, out nodeRef);
  99. if (err != Error.Ok)
  100. throw new FreeTypeException(err);
  101. node = new Node(nodeRef);
  102. return new SBit(sbitRef);
  103. }
  104. /// <summary>
  105. /// A variant of <see cref="SBitCache.Lookup"/> that uses a <see cref="Scaler"/> to specify the face ID and its
  106. /// size.
  107. /// </summary>
  108. /// <remarks><para>
  109. /// The small bitmap descriptor and its bit buffer are owned by the cache and should never be freed by the
  110. /// application. They might as well disappear from memory on the next cache lookup, so don't treat them as
  111. /// persistent data.
  112. /// </para><para>
  113. /// The descriptor's ‘buffer’ field is set to 0 to indicate a missing glyph bitmap.
  114. /// </para><para>
  115. /// If ‘node’ is not NULL, it receives the address of the cache node containing the bitmap, after
  116. /// increasing its reference count. This ensures that the node (as well as the image) will always be kept in
  117. /// the cache until you call <see cref="Node.Unref"/> to ‘release’ it.
  118. /// </para><para>
  119. /// If ‘node’ is NULL, the cache node is left unchanged, which means that the bitmap could be
  120. /// flushed out of the cache on the next call to one of the caching sub-system APIs. Don't assume that it is
  121. /// persistent!
  122. /// </para></remarks>
  123. /// <param name="scaler">A pointer to the scaler descriptor.</param>
  124. /// <param name="loadFlags">The corresponding load flags.</param>
  125. /// <param name="gIndex">The glyph index.</param>
  126. /// <param name="node">
  127. /// Used to return the address of of the corresponding cache node after incrementing its reference count (see
  128. /// note below).
  129. /// </param>
  130. /// <returns>A handle to a small bitmap descriptor.</returns>
  131. [CLSCompliant(false)]
  132. public SBit LookupScaler(Scaler scaler, LoadFlags loadFlags, uint gIndex, out Node node)
  133. {
  134. if (parentManager.IsDisposed)
  135. throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
  136. IntPtr sbitRef, nodeRef;
  137. Error err = FT.FTC_SBitCache_LookupScaler(Reference, scaler.Reference, loadFlags, gIndex, out sbitRef, out nodeRef);
  138. if (err != Error.Ok)
  139. throw new FreeTypeException(err);
  140. node = new Node(nodeRef);
  141. return new SBit(sbitRef);
  142. }
  143. #endregion
  144. }
  145. }