skiasharp/binding/HarfBuzzSharp/UnicodeFunctions.cs
Max Katz 9cbf9c80b7
Function pointers and LibraryImport (#2917)
* Update generator to emit function pointers

* Regenerate interop files

* Enable USE_LIBRARY_IMPORT in HarfBuzzSharp

* Enable USE_LIBRARY_IMPORT on SkiaSharp

* Set DisableRuntimeMarshalling on HarfBuzzSharp

* Replace remaining DllImports with LibraryImport on SkiaSharp

* Set DisableRuntimeMarshalling on SkiaSharp as well

* Fix missed proxy definition

* Regenerate skia api with a correct submodule version

* Collections literals are not supported on the CI .NET SDK

* An attempt to fix Tizen build

* Forgot about partial

* Set UnmanagedType.LPStr on evas_gl_proc_address_get instead

* Set USE_LIBRARY_IMPORT on remaining projects too

* Update generator tool to generate DelegateProxy as well

* Regenerate HarfBuzz and SkiaSharp with new DelegateProxy source gen

* Regenerate other projects as well

* Add `protected internal` to test classes too, since this project has InternalsVisibleTo configured

* Disable DelegateTypesAreValid and DelegateTypesHaveAttributes tests on .NET 7+ build, see comments

* Reduce warnings noise

* Update binding/SkiaSharp/GRGlInterface.cs

Co-authored-by: Filip Navara <filip.navara@gmail.com>

* Add missing USE_LIBRARY_IMPORT defines

* Update binding/SkiaSharp/GRGlInterface.cs

* Also needs USE_LIBRARY_IMPORT

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Filip Navara <filip.navara@gmail.com>
2024-08-21 23:16:05 +08:00

179 lines
5.5 KiB
C#

#nullable disable
using System;
namespace HarfBuzzSharp
{
public unsafe class UnicodeFunctions : NativeObject
{
private static readonly Lazy<UnicodeFunctions> defaultFunctions =
new Lazy<UnicodeFunctions> (() => new StaticUnicodeFunctions (HarfBuzzApi.hb_unicode_funcs_get_default ()));
private static readonly Lazy<UnicodeFunctions> emptyFunctions =
new Lazy<UnicodeFunctions> (() => new StaticUnicodeFunctions (HarfBuzzApi.hb_unicode_funcs_get_empty ()));
public static UnicodeFunctions Default => defaultFunctions.Value;
public static UnicodeFunctions Empty => emptyFunctions.Value;
internal UnicodeFunctions (IntPtr handle)
: base (handle)
{
}
public UnicodeFunctions (UnicodeFunctions parent) : base (IntPtr.Zero)
{
if (parent == null)
throw new ArgumentNullException (nameof (parent));
if (parent.Handle == IntPtr.Zero)
throw new ArgumentException (nameof (parent.Handle));
Parent = parent;
Handle = HarfBuzzApi.hb_unicode_funcs_create (parent.Handle);
}
public UnicodeFunctions Parent { get; }
public bool IsImmutable => HarfBuzzApi.hb_unicode_funcs_is_immutable (Handle);
public void MakeImmutable () => HarfBuzzApi.hb_unicode_funcs_make_immutable (Handle);
public UnicodeCombiningClass GetCombiningClass (int unicode) => GetCombiningClass ((uint)unicode);
public UnicodeCombiningClass GetCombiningClass (uint unicode) =>
HarfBuzzApi.hb_unicode_combining_class (Handle, unicode);
public UnicodeGeneralCategory GetGeneralCategory (int unicode) => GetGeneralCategory ((uint)unicode);
public UnicodeGeneralCategory GetGeneralCategory (uint unicode) =>
HarfBuzzApi.hb_unicode_general_category (Handle, unicode);
public int GetMirroring (int unicode) => (int)GetMirroring ((uint)unicode);
public uint GetMirroring (uint unicode) => HarfBuzzApi.hb_unicode_mirroring (Handle, unicode);
public Script GetScript (int unicode) => GetScript ((uint)unicode);
public Script GetScript (uint unicode) => HarfBuzzApi.hb_unicode_script (Handle, unicode);
public bool TryCompose (int a, int b, out int ab)
{
var result = TryCompose ((uint)a, (uint)b, out var composed);
ab = (int)composed;
return result;
}
public bool TryCompose (uint a, uint b, out uint ab)
{
fixed (uint* abPtr = &ab) {
return HarfBuzzApi.hb_unicode_compose (Handle, a, b, abPtr);
}
}
public bool TryDecompose (int ab, out int a, out int b)
{
var result = TryDecompose ((uint)ab, out var decomposedA, out var decomposedB);
a = (int)decomposedA;
b = (int)decomposedB;
return result;
}
public bool TryDecompose (uint ab, out uint a, out uint b)
{
fixed (uint* aPtr = &a)
fixed (uint* bPtr = &b) {
return HarfBuzzApi.hb_unicode_decompose (Handle, ab, aPtr, bPtr);
}
}
public void SetCombiningClassDelegate (CombiningClassDelegate del, ReleaseDelegate destroy = null)
{
VerifyParameters (del);
var ctx = DelegateProxies.CreateMultiUserData (del, destroy, this);
HarfBuzzApi.hb_unicode_funcs_set_combining_class_func (
Handle, DelegateProxies.UnicodeCombiningClassProxy, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}
public void SetGeneralCategoryDelegate (GeneralCategoryDelegate del, ReleaseDelegate destroy = null)
{
VerifyParameters (del);
var ctx = DelegateProxies.CreateMultiUserData (del, destroy, this);
HarfBuzzApi.hb_unicode_funcs_set_general_category_func (
Handle, DelegateProxies.UnicodeGeneralCategoryProxy, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}
public void SetMirroringDelegate (MirroringDelegate del, ReleaseDelegate destroy = null)
{
VerifyParameters (del);
var ctx = DelegateProxies.CreateMultiUserData (del, destroy, this);
HarfBuzzApi.hb_unicode_funcs_set_mirroring_func (
Handle, DelegateProxies.UnicodeMirroringProxy, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}
public void SetScriptDelegate (ScriptDelegate del, ReleaseDelegate destroy = null)
{
VerifyParameters (del);
var ctx = DelegateProxies.CreateMultiUserData (del, destroy, this);
HarfBuzzApi.hb_unicode_funcs_set_script_func (
Handle, DelegateProxies.UnicodeScriptProxy, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}
public void SetComposeDelegate (ComposeDelegate del, ReleaseDelegate destroy = null)
{
VerifyParameters (del);
var ctx = DelegateProxies.CreateMultiUserData (del, destroy, this);
HarfBuzzApi.hb_unicode_funcs_set_compose_func (
Handle, DelegateProxies.UnicodeComposeProxy, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}
public void SetDecomposeDelegate (DecomposeDelegate del, ReleaseDelegate destroy = null)
{
VerifyParameters (del);
var ctx = DelegateProxies.CreateMultiUserData (del, destroy, this);
HarfBuzzApi.hb_unicode_funcs_set_decompose_func (
Handle, DelegateProxies.UnicodeDecomposeProxy, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}
private void VerifyParameters (Delegate del)
{
_ = del ?? throw new ArgumentNullException (nameof (del));
if (IsImmutable)
throw new InvalidOperationException ($"{nameof (UnicodeFunctions)} is immutable and can't be changed.");
}
protected override void Dispose (bool disposing) =>
base.Dispose (disposing);
protected override void DisposeHandler ()
{
if (Handle != IntPtr.Zero) {
HarfBuzzApi.hb_unicode_funcs_destroy (Handle);
}
}
private class StaticUnicodeFunctions : UnicodeFunctions
{
public StaticUnicodeFunctions (IntPtr handle)
: base (handle)
{
}
protected override void Dispose (bool disposing)
{
// do not dispose
}
}
}
}