PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/CoreText/CTFontManager.cs

https://github.com/kjpou1/maccore
C# | 197 lines | 143 code | 27 blank | 27 comment | 30 complexity | e7852e7758b3a38efaa3622f818de598 MD5 | raw file
Possible License(s): Apache-2.0
  1. //
  2. // CTFont.cs: Implements the managed CTFont
  3. //
  4. // Authors: Mono Team
  5. //
  6. // Copyright 2010 Novell, Inc
  7. // Copyright 2011, 2012 Xamarin Inc
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.Runtime.InteropServices;
  32. using MonoMac.ObjCRuntime;
  33. using MonoMac.CoreFoundation;
  34. using MonoMac.CoreGraphics;
  35. using MonoMac.Foundation;
  36. using CGGlyph = System.UInt16;
  37. namespace MonoMac.CoreText {
  38. public enum CTFontManagerScope {
  39. None = 0, Process = 1, User = 2, Session = 3
  40. }
  41. public enum CTFontManagerAutoActivation {
  42. Default = 0, Disabled = 1, Enabled = 2, PromptUser = 3,
  43. }
  44. public enum CTFontManagerError {
  45. None = 0,
  46. FileNotFount = 101,
  47. InsufficientPermissions = 102,
  48. UnrecognizedFormat = 103,
  49. InvalidFontData = 104,
  50. AlreadyRegistered = 105,
  51. NotRegistered = 201,
  52. InUse = 202,
  53. SystemRequired = 202
  54. }
  55. [Since (4,1)]
  56. public class CTFontManager {
  57. [DllImport (Constants.CoreTextLibrary)]
  58. static extern bool CTFontManagerIsSupportedFont (IntPtr url);
  59. public static bool IsFontSupported (NSUrl url)
  60. {
  61. if (url == null)
  62. throw new ArgumentNullException ("url");
  63. return CTFontManagerIsSupportedFont (url.Handle);
  64. }
  65. [DllImport (Constants.CoreTextLibrary)]
  66. static extern bool CTFontManagerRegisterFontsForURL (IntPtr fontUrl, CTFontManagerScope scope, IntPtr error);
  67. public static NSError RegisterFontsForUrl (NSUrl fontUrl, CTFontManagerScope scope)
  68. {
  69. if (fontUrl == null)
  70. throw new ArgumentNullException ("fontUrl");
  71. NSError e = new NSError (ErrorDomain, 0);
  72. if (CTFontManagerRegisterFontsForURL (fontUrl.Handle, scope, e.Handle))
  73. return null;
  74. else
  75. return e;
  76. }
  77. [DllImport (Constants.CoreTextLibrary)]
  78. static extern bool CTFontManagerRegisterFontsForURLs(IntPtr arrayRef, CTFontManagerScope scope, IntPtr error);
  79. public static NSError [] RegisterFontsForUrl (NSUrl [] fontUrls, CTFontManagerScope scope)
  80. {
  81. if (fontUrls == null)
  82. throw new ArgumentNullException ("fontUrls");
  83. foreach (var furl in fontUrls)
  84. if (furl == null)
  85. throw new ArgumentException ("contains a null entry", "fontUrls");
  86. var arr = NSArray.FromNSObjects (fontUrls);
  87. var _errors = new NSError [fontUrls.Length];
  88. for (int i = 0; i < fontUrls.Length; i++)
  89. _errors [i] = new NSError (ErrorDomain, 0);
  90. var errors = NSArray.FromNSObjects (_errors);
  91. if (CTFontManagerRegisterFontsForURLs (arr.Handle, scope, errors.Handle))
  92. return null;
  93. else
  94. return _errors;
  95. }
  96. [DllImport (Constants.CoreTextLibrary)]
  97. static extern bool CTFontManagerUnregisterFontsForURL(IntPtr fotUrl, CTFontManagerScope scope, IntPtr error);
  98. public static NSError UnregisterFontsForUrl (NSUrl fontUrl, CTFontManagerScope scope)
  99. {
  100. if (fontUrl == null)
  101. throw new ArgumentNullException ("fontUrl");
  102. var e = new NSError (ErrorDomain, 0);
  103. if (CTFontManagerUnregisterFontsForURLs (fontUrl.Handle, scope, e.Handle))
  104. return null;
  105. else
  106. return e;
  107. }
  108. [DllImport (Constants.CoreTextLibrary)]
  109. static extern bool CTFontManagerUnregisterFontsForURLs(IntPtr arrayRef, CTFontManagerScope scope, IntPtr error);
  110. public static NSError [] UnregisterFontsForUrl (NSUrl [] fontUrls, CTFontManagerScope scope)
  111. {
  112. if (fontUrls == null)
  113. throw new ArgumentNullException ("fontUrls");
  114. foreach (var furl in fontUrls)
  115. if (furl == null)
  116. throw new ArgumentException ("contains a null entry", "fontUrls");
  117. var arr = NSArray.FromNSObjects (fontUrls);
  118. var _errors = new NSError [fontUrls.Length];
  119. for (int i = 0; i < fontUrls.Length; i++)
  120. _errors [i] = new NSError (ErrorDomain, 0);
  121. var errors = NSArray.FromNSObjects (_errors);
  122. if (CTFontManagerUnregisterFontsForURLs (arr.Handle, scope, errors.Handle))
  123. return null;
  124. else
  125. return _errors;
  126. }
  127. [DllImport (Constants.CoreTextLibrary)]
  128. static extern bool CTFontManagerRegisterGraphicsFont (IntPtr cgfont, out IntPtr error);
  129. public static bool RegisterGraphicsFont (CGFont font, out NSError error)
  130. {
  131. if (font == null)
  132. throw new ArgumentNullException ("font");
  133. IntPtr h;
  134. var ret = CTFontManagerRegisterGraphicsFont (font.Handle, out h);
  135. if (ret)
  136. error = null;
  137. else
  138. error = new NSError (h);
  139. return ret;
  140. }
  141. [DllImport (Constants.CoreTextLibrary)]
  142. static extern bool CTFontManagerUnregisterGraphicsFont (IntPtr cgfont, out IntPtr error);
  143. public static bool UnregisterGraphicsFont (CGFont font, out NSError error)
  144. {
  145. if (font == null)
  146. throw new ArgumentNullException ("font");
  147. IntPtr h;
  148. var ret = CTFontManagerUnregisterGraphicsFont (font.Handle, out h);
  149. if (ret)
  150. error = null;
  151. else
  152. error = new NSError (h);
  153. return ret;
  154. }
  155. static CTFontManager ()
  156. {
  157. var handle = Dlfcn.dlopen (Constants.CoreTextLibrary, 0);
  158. if (handle == IntPtr.Zero)
  159. return;
  160. try {
  161. ErrorDomain = Dlfcn.GetStringConstant (handle, "kCTFontManagerErrorDomain");
  162. ErrorFontUrlsKey = Dlfcn.GetStringConstant (handle, "kCTFontManagerErrorFontURLsKey");
  163. }
  164. finally {
  165. Dlfcn.dlclose (handle);
  166. }
  167. }
  168. public readonly static NSString ErrorDomain, ErrorFontUrlsKey;
  169. }
  170. }