ImageCache.cs 6.4 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. namespace SharpFont.Cache
  22. {
  23. /// <summary>
  24. /// A handle to an glyph image cache object. They are designed to hold many distinct glyph images while not
  25. /// exceeding a certain memory threshold.
  26. /// </summary>
  27. public class ImageCache
  28. {
  29. #region Fields
  30. private IntPtr reference;
  31. private Manager parentManager;
  32. #endregion
  33. #region Constructors
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ImageCache"/> class.
  36. /// </summary>
  37. /// <param name="manager">The parent manager for the image cache.</param>
  38. public ImageCache(Manager manager)
  39. {
  40. if (manager == null)
  41. throw new ArgumentNullException("manager");
  42. IntPtr cacheRef;
  43. Error err = FT.FTC_ImageCache_New(manager.Reference, out cacheRef);
  44. if (err != Error.Ok)
  45. throw new FreeTypeException(err);
  46. parentManager = manager;
  47. Reference = cacheRef;
  48. }
  49. #endregion
  50. #region Properties
  51. internal IntPtr Reference
  52. {
  53. get
  54. {
  55. if (parentManager.IsDisposed)
  56. throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
  57. return reference;
  58. }
  59. set
  60. {
  61. if (parentManager.IsDisposed)
  62. throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
  63. reference = value;
  64. }
  65. }
  66. #endregion
  67. #region Methods
  68. /// <summary>
  69. /// Retrieve a given glyph image from a glyph image cache.
  70. /// </summary>
  71. /// <remarks><para>
  72. /// The returned glyph is owned and managed by the glyph image cache. Never try to transform or discard it
  73. /// manually! You can however create a copy with <see cref="Glyph.Copy"/> and modify the new one.
  74. /// </para><para>
  75. /// If ‘node’ is not NULL, it receives the address of the cache node containing the glyph image,
  76. /// after increasing its reference count. This ensures that the node (as well as the <see cref="Glyph"/>) will
  77. /// always be kept in the cache until you call <see cref="Node.Unref"/> to ‘release’ it.
  78. /// </para><para>
  79. /// If ‘node’ is NULL, the cache node is left unchanged, which means that the <see cref="Glyph"/>
  80. /// could be flushed out of the cache on the next call to one of the caching sub-system APIs. Don't assume that
  81. /// it is persistent!
  82. /// </para></remarks>
  83. /// <param name="type">A pointer to a glyph image type descriptor.</param>
  84. /// <param name="gIndex">The glyph index to retrieve.</param>
  85. /// <param name="node">
  86. /// Used to return the address of of the corresponding cache node after incrementing its reference count (see
  87. /// note below).
  88. /// </param>
  89. /// <returns>The corresponding <see cref="Glyph"/> object. 0 in case of failure.</returns>
  90. [CLSCompliant(false)]
  91. public Glyph Lookup(ImageType type, uint gIndex, out Node node)
  92. {
  93. if (parentManager.IsDisposed)
  94. throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
  95. IntPtr glyphRef, nodeRef;
  96. Error err = FT.FTC_ImageCache_Lookup(Reference, type.Reference, gIndex, out glyphRef, out nodeRef);
  97. if (err != Error.Ok)
  98. throw new FreeTypeException(err);
  99. node = new Node(nodeRef);
  100. return new Glyph(glyphRef, null);
  101. }
  102. /// <summary>
  103. /// A variant of <see cref="ImageCache.Lookup"/> that uses a <see cref="Scaler"/> to specify the face ID and its
  104. /// size.
  105. /// </summary>
  106. /// <remarks><para>
  107. /// The returned glyph is owned and managed by the glyph image cache. Never try to transform or discard it
  108. /// manually! You can however create a copy with <see cref="Glyph.Copy"/> and modify the new one.
  109. /// </para><para>
  110. /// If ‘node’ is not NULL, it receives the address of the cache node containing the glyph image,
  111. /// after increasing its reference count. This ensures that the node (as well as the <see cref="Glyph"/>) will
  112. /// always be kept in the cache until you call <see cref="Node.Unref"/> to ‘release’ it.
  113. /// </para><para>
  114. /// If ‘node’ is NULL, the cache node is left unchanged, which means that the <see cref="Glyph"/>
  115. /// could be flushed out of the cache on the next call to one of the caching sub-system APIs. Don't assume that
  116. /// it is persistent!
  117. /// </para><para>
  118. /// Calls to <see cref="Face.SetCharSize"/> and friends have no effect on cached glyphs; you should always use
  119. /// the FreeType cache API instead.
  120. /// </para></remarks>
  121. /// <param name="scaler">A pointer to a scaler descriptor.</param>
  122. /// <param name="loadFlags">The corresponding load flags.</param>
  123. /// <param name="gIndex">The glyph index to retrieve.</param>
  124. /// <param name="node">
  125. /// Used to return the address of of the corresponding cache node after incrementing its reference count (see
  126. /// note below).
  127. /// </param>
  128. /// <returns>The corresponding <see cref="Glyph"/> object. 0 in case of failure.</returns>
  129. [CLSCompliant(false)]
  130. public Glyph LookupScaler(Scaler scaler, LoadFlags loadFlags, uint gIndex, out Node node)
  131. {
  132. if (parentManager.IsDisposed)
  133. throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
  134. IntPtr glyphRef, nodeRef;
  135. Error err = FT.FTC_ImageCache_LookupScaler(Reference, scaler.Reference, loadFlags, gIndex, out glyphRef, out nodeRef);
  136. if (err != Error.Ok)
  137. throw new FreeTypeException(err);
  138. node = new Node(nodeRef);
  139. return new Glyph(glyphRef, null);
  140. }
  141. #endregion
  142. }
  143. }