OpenArgs.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. using SharpFont.Internal;
  23. namespace SharpFont
  24. {
  25. /// <summary>
  26. /// A structure used to indicate how to open a new font file or stream. A pointer to such a structure can be used
  27. /// as a parameter for the functions <see cref="Library.OpenFace"/> and <see cref="Face.AttachStream"/>.
  28. /// </summary>
  29. /// <remarks>
  30. /// The stream type is determined by the contents of <see cref="Flags"/> which are tested in the following order by
  31. /// <see cref="Library.OpenFace"/>:
  32. /// <list type="bullet">
  33. /// <item><description>
  34. /// If the <see cref="OpenFlags.Memory"/> bit is set, assume that this is a memory file of <see cref="MemorySize"/>
  35. /// bytes, located at <see cref="MemoryBase"/>. The data are are not copied, and the client is responsible for
  36. /// releasing and destroying them after the corresponding call to <see cref="Face.Dispose()"/>.
  37. /// </description></item>
  38. /// <item><description>
  39. /// Otherwise, if the <see cref="OpenFlags.Stream"/> bit is set, assume that a custom input stream
  40. /// <see cref="Stream"/> is used.
  41. /// </description></item>
  42. /// <item><description>
  43. /// Otherwise, if the <see cref="OpenFlags.PathName"/> bit is set, assume that this is a normal file and use
  44. /// <see cref="PathName"/> to open it.
  45. /// </description></item>
  46. /// <item><description>
  47. /// If the <see cref="OpenFlags.Driver"/> bit is set, <see cref="Library.OpenFace"/> only tries to open the file
  48. /// with the driver whose handler is in <see cref="Driver"/>.
  49. /// </description></item>
  50. /// <item><description>
  51. /// If the <see cref="OpenFlags.Params"/> bit is set, the parameters given by <see cref="ParamsCount"/> and
  52. /// <see cref="Params"/> is used. They are ignored otherwise.
  53. /// </description></item>
  54. /// </list>
  55. /// Ideally, both the <see cref="PathName"/> and <see cref="Params"/> fields should be tagged as ‘const’; this is
  56. /// missing for API backwards compatibility. In other words, applications should treat them as read-only.
  57. /// </remarks>
  58. public sealed class OpenArgs
  59. {
  60. #region Fields
  61. private IntPtr reference;
  62. private OpenArgsRec rec;
  63. #endregion
  64. #region Constructors
  65. internal OpenArgs(IntPtr reference)
  66. {
  67. Reference = reference;
  68. }
  69. #endregion
  70. #region Properties
  71. /// <summary>
  72. /// Gets a set of bit flags indicating how to use the structure.
  73. /// </summary>
  74. public OpenFlags Flags
  75. {
  76. get
  77. {
  78. return rec.flags;
  79. }
  80. }
  81. /// <summary>
  82. /// Gets the first byte of the file in memory.
  83. /// </summary>
  84. public IntPtr MemoryBase
  85. {
  86. get
  87. {
  88. return rec.memory_base;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets the size in bytes of the file in memory.
  93. /// </summary>
  94. public int MemorySize
  95. {
  96. get
  97. {
  98. return (int)rec.memory_size;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets a pointer to an 8-bit file pathname.
  103. /// </summary>
  104. public string PathName
  105. {
  106. get
  107. {
  108. return rec.pathname;
  109. }
  110. }
  111. /// <summary>
  112. /// Gets a handle to a source stream object.
  113. /// </summary>
  114. public FTStream Stream
  115. {
  116. get
  117. {
  118. return new FTStream(rec.stream);
  119. }
  120. }
  121. /// <summary>
  122. /// Gets the font driver to use to open the face. If set to 0, FreeType tries to load the face with each one of
  123. /// the drivers in its list.
  124. /// </summary>
  125. /// <remarks>This field is exclusively used by <see cref="Library.OpenFace"/>.</remarks>
  126. public Module Driver
  127. {
  128. get
  129. {
  130. return new Module(rec.driver);
  131. }
  132. }
  133. /// <summary>
  134. /// Gets the number of extra parameters.
  135. /// </summary>
  136. public int ParamsCount
  137. {
  138. get
  139. {
  140. return rec.num_params;
  141. }
  142. }
  143. /// <summary>
  144. /// Gets the extra parameters passed to the font driver when opening a new face.
  145. /// </summary>
  146. public Parameter[] Params
  147. {
  148. get
  149. {
  150. int count = ParamsCount;
  151. if (count == 0)
  152. return null;
  153. Parameter[] parameters = new Parameter[count];
  154. IntPtr array = rec.@params;
  155. for (int i = 0; i < count; i++)
  156. {
  157. parameters[i] = new Parameter(new IntPtr(array.ToInt64() + ParameterRec.SizeInBytes * i));
  158. }
  159. return parameters;
  160. }
  161. }
  162. internal IntPtr Reference
  163. {
  164. get
  165. {
  166. return reference;
  167. }
  168. set
  169. {
  170. reference = value;
  171. rec = PInvokeHelper.PtrToStructure<OpenArgsRec>(reference);
  172. }
  173. }
  174. #endregion
  175. }
  176. }