From 4c80c5fc427d7400228f5ef5b79dc0a7ec524036 Mon Sep 17 00:00:00 2001 From: dmg Date: Wed, 2 Jun 2021 22:33:03 +0300 Subject: [PATCH] Added/fixed some Pango APIs * Added pango_layout_get_direction * Added pango_layout_get_line_spacing / pango_layout_set_line_spacing * Added pango_layout_line_get_height * Added return type to pango_itemize_with_base_dir * Fixed pango_layout_line_get_x_ranges * Fixed pango_layout_get_log_attrs / pango_layout_get_log_attrs_readonly (using only readonly version because we make copy anyway, added LogAttr bitfield reading properties) --- Source/Libs/PangoSharp/Layout.cs | 53 ++++++++++++++++------ Source/Libs/PangoSharp/LayoutLine.cs | 35 +++++++------- Source/Libs/PangoSharp/LogAttr.cs | 25 ++++++++++ Source/Libs/PangoSharp/PangoSharp.metadata | 4 ++ 4 files changed, 88 insertions(+), 29 deletions(-) create mode 100644 Source/Libs/PangoSharp/LogAttr.cs diff --git a/Source/Libs/PangoSharp/Layout.cs b/Source/Libs/PangoSharp/Layout.cs index b71fbc638..86e4b0102 100644 --- a/Source/Libs/PangoSharp/Layout.cs +++ b/Source/Libs/PangoSharp/Layout.cs @@ -55,23 +55,20 @@ namespace Pango { accel_char = GLib.Marshaller.GUnicharToChar (ucs4_accel_char); } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - delegate void d_pango_layout_get_log_attrs(IntPtr raw, out IntPtr attrs, out int n_attrs); - static d_pango_layout_get_log_attrs pango_layout_get_log_attrs = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_get_log_attrs")); + delegate IntPtr d_pango_layout_get_log_attrs_readonly(IntPtr raw, out int n_attrs); + static d_pango_layout_get_log_attrs_readonly pango_layout_get_log_attrs_readonly = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_get_log_attrs_readonly")); public LogAttr [] LogAttrs { get { int count; - IntPtr array_ptr; - pango_layout_get_log_attrs (Handle, out array_ptr, out count); - if (array_ptr == IntPtr.Zero) + IntPtr array_ptr = pango_layout_get_log_attrs_readonly (Handle, out count); + if (array_ptr == IntPtr.Zero || count == 0) return new LogAttr [0]; - LogAttr [] result = new LogAttr [count]; - for (int i = 0; i < count; i++) { - IntPtr fam_ptr = Marshal.ReadIntPtr (array_ptr, i * IntPtr.Size); - result [i] = LogAttr.New (fam_ptr); - } - - GLib.Marshaller.Free (array_ptr); + LogAttr[] result = new LogAttr [count]; + int[] array = new int [count]; + Marshal.Copy(array_ptr, array, 0, count); + for (int i = 0; i < count; i++) + result[i] = new LogAttr ((uint)array[i]); return result; } } @@ -95,6 +92,36 @@ namespace Pango { pango_layout_set_markup (Handle, native_markup, -1); GLib.Marshaller.Free (native_markup); } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + delegate int d_pango_layout_get_direction(IntPtr raw, int index); + static d_pango_layout_get_direction pango_layout_get_direction = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_get_direction")); + + public Pango.Direction GetDirection(int index) + { + int raw_ret = pango_layout_get_direction(Handle, index); + return (Pango.Direction)raw_ret; + } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + delegate float d_pango_layout_get_line_spacing(IntPtr raw); + static d_pango_layout_get_line_spacing pango_layout_get_line_spacing = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_get_line_spacing")); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + delegate void d_pango_layout_set_line_spacing(IntPtr raw, float factor); + static d_pango_layout_set_line_spacing pango_layout_set_line_spacing = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_set_line_spacing")); + + public float LineSpacing + { + get + { + float raw_ret = pango_layout_get_line_spacing(Handle); + return raw_ret; + } + set + { + pango_layout_set_line_spacing(Handle, value); + } + } } } - diff --git a/Source/Libs/PangoSharp/LayoutLine.cs b/Source/Libs/PangoSharp/LayoutLine.cs index ac16223a6..41ee39d79 100644 --- a/Source/Libs/PangoSharp/LayoutLine.cs +++ b/Source/Libs/PangoSharp/LayoutLine.cs @@ -22,34 +22,37 @@ namespace Pango { using System; + using System.Runtime.InteropServices; public partial class LayoutLine { -#if NOT_BROKEN [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void d_pango_layout_line_get_x_ranges(IntPtr raw, int start_index, int end_index, out IntPtr ranges_handle, out int n_ranges); static d_pango_layout_line_get_x_ranges pango_layout_line_get_x_ranges = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_line_get_x_ranges")); -#endif - public void GetXRanges(int start_index, int end_index, out int[][] ranges) + public int[] GetXRanges(int start_index, int end_index) { - // FIXME: this is broken - throw new NotImplementedException (); -#if NOT_BROKEN int count; IntPtr array_ptr; pango_layout_line_get_x_ranges(Handle, start_index, end_index, out array_ptr, out count); - ranges = new int[count] []; - for (int i = 0; i < count; i++) { - IntPtr tmp = new IntPtr (array_ptr + 2 * i * IntPtr.Size); - IntPtr rng_ptr = Marshal.ReadIntPtr (tmp); - IntPtr end_ptr = Marshal.ReadIntPtr (tmp, IntPtr.Size); - - } - Marshal.Copy (array_ptr, ranges, 0, count); + int[] array = new int[count * 2]; + Marshal.Copy(array_ptr, array, 0, count * 2); GLib.Marshaller.Free (array_ptr); -#endif + return array; + } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + delegate void d_pango_layout_line_get_height(IntPtr raw, out int height); + static d_pango_layout_line_get_height pango_layout_line_get_height = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.Pango), "pango_layout_line_get_height")); + + public int Height + { + get + { + int height; + pango_layout_line_get_height(Handle, out height); + return height; + } } } } - diff --git a/Source/Libs/PangoSharp/LogAttr.cs b/Source/Libs/PangoSharp/LogAttr.cs new file mode 100644 index 000000000..f74c38932 --- /dev/null +++ b/Source/Libs/PangoSharp/LogAttr.cs @@ -0,0 +1,25 @@ +namespace Pango { + + using System; + + public partial struct LogAttr { + + public LogAttr(uint bitfield) => _bitfield0 = bitfield; + public override string ToString() => Convert.ToString(_bitfield0 & 0x1FFF, 2).PadLeft(13, '0'); + + public uint Bitfield => _bitfield0; + public bool IsLineBreak => (_bitfield0 & (1 << 0)) != 0; + public bool IsMandatoryBreak => (_bitfield0 & (1 << 1)) != 0; + public bool IsCharBreak => (_bitfield0 & (1 << 2)) != 0; + public bool IsWhite => (_bitfield0 & (1 << 3)) != 0; + public bool IsCursorPosition => (_bitfield0 & (1 << 4)) != 0; + public bool IsWordStart => (_bitfield0 & (1 << 5)) != 0; + public bool IsWordEnd => (_bitfield0 & (1 << 6)) != 0; + public bool IsSentenceBoundary => (_bitfield0 & (1 << 7)) != 0; + public bool IsSentenceStart => (_bitfield0 & (1 << 8)) != 0; + public bool IsSentenceEnd => (_bitfield0 & (1 << 9)) != 0; + public bool BackspaceDeletesCharacter => (_bitfield0 & (1 << 10)) != 0; + public bool IsExpandableSpace => (_bitfield0 & (1 << 11)) != 0; + public bool IsWordBoundary => (_bitfield0 & (1 << 12)) != 0; + } +} diff --git a/Source/Libs/PangoSharp/PangoSharp.metadata b/Source/Libs/PangoSharp/PangoSharp.metadata index ec9df6bac..497d0be9f 100644 --- a/Source/Libs/PangoSharp/PangoSharp.metadata +++ b/Source/Libs/PangoSharp/PangoSharp.metadata @@ -40,6 +40,9 @@ 1 1 ref + PangoItem* + true + true 1 1 1 @@ -67,6 +70,7 @@ PangoLayoutLine* GetLinesReadOnly 1 + 1 out out out