Skip to content

Commit 7fa3f85

Browse files
authored
feat: Add TextMetrics to API (#61)
1 parent 8f7b74a commit 7fa3f85

35 files changed

+1564
-270
lines changed

build/Build.LibAlphaSkia.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ partial class Build
8585
"../../wrapper/src/alphaskia_canvas.cpp",
8686
"../../wrapper/src/alphaskia_image.cpp",
8787
"../../wrapper/src/alphaskia_typeface.cpp",
88-
"../../wrapper/src/alphaskia_textstyle.cpp",
88+
"../../wrapper/src/alphaskia_text_style.cpp",
89+
"../../wrapper/src/alphaskia_text_metrics.cpp",
8990
"../../wrapper/src/alphaskia_data.cpp",
9091
"../../wrapper/src/alphaskia_string.cpp"
9192
]
@@ -158,6 +159,7 @@ partial class Build
158159
"../../lib/java/jni/src/AlphaSkiaData.cpp",
159160
"../../lib/java/jni/src/AlphaSkiaImage.cpp",
160161
"../../lib/java/jni/src/AlphaSkiaTextStyle.cpp",
162+
"../../lib/java/jni/src/AlphaSkiaTextMetrics.cpp",
161163
"../../lib/java/jni/src/AlphaSkiaTypeface.cpp"
162164
]
163165
}

lib/dotnet/AlphaSkia/AlphaSkiaCanvas.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,18 @@ public void FillText(string text, AlphaSkiaTextStyle textStyle, float fontSize,
296296
}
297297

298298
/// <summary>
299-
/// Measures the given text.
299+
/// Returns a <see cref="AlphaSkiaTextMetrics"/> object that contains information about the measured text (such as its width, for example).
300300
/// </summary>
301301
/// <param name="text">The text to measure.</param>
302302
/// <param name="textStyle">The text style to use for measuring the text.</param>
303303
/// <param name="fontSize">The font size to use when measuring the text.</param>
304-
/// <returns>The horizontal width of the text when it would be drawn.</returns>
305-
public float MeasureText(string text, AlphaSkiaTextStyle textStyle, float fontSize)
304+
/// <param name="textAlign">How to align the text at the given position horizontally.</param>
305+
/// <param name="baseline">How to align the text at the given position vertically.</param>
306+
/// <returns>The text metrics.</returns>
307+
public AlphaSkiaTextMetrics MeasureText(string text, AlphaSkiaTextStyle textStyle, float fontSize, AlphaSkiaTextAlign textAlign, AlphaSkiaTextBaseline baseline)
306308
{
307309
CheckDisposed();
308-
return NativeMethods.alphaskia_canvas_measure_text(Handle, text, text.Length, textStyle.Handle, fontSize);
310+
return new AlphaSkiaTextMetrics(NativeMethods.alphaskia_canvas_measure_text(Handle, text, text.Length, textStyle.Handle, fontSize, textAlign, baseline));
309311
}
310312

311313
/// <summary>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace AlphaSkia;
2+
3+
/// <summary>
4+
/// The TextMetrics interface represents the dimensions of a piece of text in the canvas;
5+
/// A TextMetrics instance can be retrieved using the <see cref="AlphaSkiaCanvas.MeasureText"/> method.
6+
/// </summary>
7+
public sealed class AlphaSkiaTextMetrics : AlphaSkiaNative
8+
{
9+
internal AlphaSkiaTextMetrics(IntPtr handle)
10+
: base(handle, NativeMethods.alphaskia_text_metrics_free)
11+
{
12+
}
13+
14+
/// <summary>
15+
/// Returns the width of a segment of inline text in pixels. It takes into account the current font of the context.
16+
/// </summary>
17+
public float Width => NativeMethods.alphaskia_text_metrics_get_width(Handle);
18+
19+
/// <summary>
20+
/// Distance parallel to the baseline from the alignment point given by the textAlign parameter to the left side of the bounding rectangle of the given text, in pixels; positive numbers indicating a distance going left from the given alignment point.
21+
/// </summary>
22+
public float ActualBoundingBoxLeft => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_left(Handle);
23+
24+
/// <summary>
25+
/// Returns the distance from the alignment point given by the textAlign parameter to the right side of the bounding rectangle of the given text, in pixels. The distance is measured parallel to the baseline.
26+
/// </summary>
27+
public float ActualBoundingBoxRight => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_right(Handle);
28+
29+
/// <summary>
30+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the top of the highest bounding rectangle of all the fonts used to render the text, in pixels.
31+
/// </summary>
32+
public float FontBoundingBoxAscent => NativeMethods.alphaskia_text_metrics_get_font_bounding_box_ascent(Handle);
33+
34+
/// <summary>
35+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the bottom of the bounding rectangle of all the fonts used to render the text, in pixels.
36+
/// </summary>
37+
public float FontBoundingBoxDescent => NativeMethods.alphaskia_text_metrics_get_font_bounding_box_descent(Handle);
38+
39+
/// <summary>
40+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the top of the bounding rectangle used to render the text, in pixels.
41+
/// </summary>
42+
public float ActualBoundingBoxAscent => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_ascent(Handle);
43+
44+
/// <summary>
45+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the bottom of the bounding rectangle used to render the text, in pixels.
46+
/// </summary>
47+
public float ActualBoundingBoxDescent => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_descent(Handle);
48+
49+
/// <summary>
50+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the top of the em square in the line box, in pixels.
51+
/// </summary>
52+
public float EmHeightAscent => NativeMethods.alphaskia_text_metrics_get_em_height_ascent(Handle);
53+
54+
/// <summary>
55+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the bottom of the em square in the line box, in pixels.
56+
/// </summary>
57+
public float EmHeightDescent => NativeMethods.alphaskia_text_metrics_get_em_height_descent(Handle);
58+
59+
/// <summary>
60+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the hanging baseline of the line box, in pixels.
61+
/// </summary>
62+
public float HangingBaseline => NativeMethods.alphaskia_text_metrics_get_hanging_baseline(Handle);
63+
64+
/// <summary>
65+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the alphabetic baseline of the line box, in pixels.
66+
/// </summary>
67+
public float AlphabeticBaseline => NativeMethods.alphaskia_text_metrics_get_hanging_baseline(Handle);
68+
69+
/// <summary>
70+
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the ideographic baseline of the line box, in CSS pixels.
71+
/// </summary>
72+
public float IdeographicBaseline => NativeMethods.alphaskia_text_metrics_get_ideographic_baseline(Handle);
73+
}

lib/dotnet/AlphaSkia/AlphaSkiaTextStyle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public sealed class AlphaSkiaTextStyle : AlphaSkiaNative
3333
/// <param name="weight">The font weight typefaces should have.</param>
3434
/// <param name="isItalic">Whether typefaces should be italic.</param>
3535
public AlphaSkiaTextStyle(string[] fontFamilies, ushort weight, bool isItalic)
36-
: base(NativeMethods.alphaskia_textstyle_new((byte)fontFamilies.Length, fontFamilies, weight, isItalic ? (byte)1 : (byte)0), NativeMethods.alphaskia_textstyle_free)
36+
: base(NativeMethods.alphaskia_text_style_new((byte)fontFamilies.Length, fontFamilies, weight, isItalic ? (byte)1 : (byte)0), NativeMethods.alphaskia_text_style_free)
3737
{
3838
FontFamilies = fontFamilies;
3939
Weight = weight;

lib/dotnet/AlphaSkia/GlobalUsings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
global using alphaskia_canvas_t = System.IntPtr;
44
global using alphaskia_data_t = System.IntPtr;
55
global using alphaskia_string_t = System.IntPtr;
6-
global using alphaskia_textstyle_t = System.IntPtr;
6+
global using alphaskia_text_style_t = System.IntPtr;
7+
global using alphaskia_text_metrics_t = System.IntPtr;

lib/dotnet/AlphaSkia/NativeMethods.cs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ internal static class NativeMethods
4949
public static extern void alphaskia_typeface_free(alphaskia_typeface_t type_face);
5050

5151
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
52-
public static extern alphaskia_textstyle_t alphaskia_textstyle_new(byte family_name_count, string[] family_names, ushort weight, byte italic);
52+
public static extern alphaskia_text_style_t alphaskia_text_style_new(byte family_name_count, string[] family_names, ushort weight, byte italic);
5353
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
54-
public static extern byte alphaskia_textstyle_get_family_name_count(alphaskia_textstyle_t textstyle);
54+
public static extern byte alphaskia_text_style_get_family_name_count(alphaskia_text_style_t text_style);
5555
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
56-
public static extern alphaskia_string_t alphaskia_textstyle_get_family_name(alphaskia_textstyle_t textstyle, byte index);
56+
public static extern alphaskia_string_t alphaskia_text_style_get_family_name(alphaskia_text_style_t text_style, byte index);
5757
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
58-
public static extern ushort alphaskia_textstyle_get_weight(alphaskia_textstyle_t textstyle);
58+
public static extern ushort alphaskia_text_style_get_weight(alphaskia_text_style_t text_style);
5959
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
60-
public static extern byte alphaskia_textstyle_is_italic(alphaskia_textstyle_t textstyle);
60+
public static extern byte alphaskia_text_style_is_italic(alphaskia_text_style_t text_style);
6161
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
62-
public static extern void alphaskia_textstyle_free(alphaskia_textstyle_t textstyle);
62+
public static extern void alphaskia_text_style_free(alphaskia_text_style_t text_style);
6363

6464
[DllImport(AlphaSkiaNativeLibName)]
6565
public static extern alphaskia_typeface_t alphaskia_typeface_make_from_name(
@@ -168,15 +168,16 @@ public static extern void alphaskia_canvas_fill_text(alphaskia_canvas_t canvas,
168168
[MarshalAs(UnmanagedType.LPWStr)]
169169
string text,
170170
int text_length,
171-
alphaskia_textstyle_t textstyle, float font_size, float x, float y, AlphaSkiaTextAlign text_align,
171+
alphaskia_text_style_t text_style, float font_size, float x, float y, AlphaSkiaTextAlign text_align,
172172
AlphaSkiaTextBaseline baseline);
173173

174174
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
175-
public static extern float alphaskia_canvas_measure_text(alphaskia_canvas_t canvas,
175+
public static extern alphaskia_text_metrics_t alphaskia_canvas_measure_text(alphaskia_canvas_t canvas,
176176
[MarshalAs(UnmanagedType.LPWStr)]
177177
string text,
178178
int text_length,
179-
alphaskia_textstyle_t textstyle, float font_size);
179+
alphaskia_text_style_t text_style, float font_size, AlphaSkiaTextAlign text_align,
180+
AlphaSkiaTextBaseline baseline);
180181

181182
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
182183
public static extern void alphaskia_canvas_begin_rotate(alphaskia_canvas_t canvas, float center_x, float center_y,
@@ -188,4 +189,43 @@ public static extern void alphaskia_canvas_begin_rotate(alphaskia_canvas_t canva
188189
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
189190
public static extern void alphaskia_canvas_draw_image(alphaskia_canvas_t canvas, alphaskia_image_t image, float x,
190191
float y, float w, float h);
192+
193+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
194+
public static extern float alphaskia_text_metrics_get_width(alphaskia_text_metrics_t text_metrics);
195+
196+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
197+
public static extern float alphaskia_text_metrics_get_actual_bounding_box_left(alphaskia_text_metrics_t text_metrics);
198+
199+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
200+
public static extern float alphaskia_text_metrics_get_actual_bounding_box_right(alphaskia_text_metrics_t text_metrics);
201+
202+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
203+
public static extern float alphaskia_text_metrics_get_font_bounding_box_ascent(alphaskia_text_metrics_t text_metrics);
204+
205+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
206+
public static extern float alphaskia_text_metrics_get_font_bounding_box_descent(alphaskia_text_metrics_t text_metrics);
207+
208+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
209+
public static extern float alphaskia_text_metrics_get_actual_bounding_box_ascent(alphaskia_text_metrics_t text_metrics);
210+
211+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
212+
public static extern float alphaskia_text_metrics_get_actual_bounding_box_descent(alphaskia_text_metrics_t text_metrics);
213+
214+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
215+
public static extern float alphaskia_text_metrics_get_em_height_ascent(alphaskia_text_metrics_t text_metrics);
216+
217+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
218+
public static extern float alphaskia_text_metrics_get_em_height_descent(alphaskia_text_metrics_t text_metrics);
219+
220+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
221+
public static extern float alphaskia_text_metrics_get_hanging_baseline(alphaskia_text_metrics_t text_metrics);
222+
223+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
224+
public static extern float alphaskia_text_metrics_get_alphabetic_baseline(alphaskia_text_metrics_t text_metrics);
225+
226+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
227+
public static extern float alphaskia_text_metrics_get_ideographic_baseline(alphaskia_text_metrics_t text_metrics);
228+
229+
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
230+
public static extern void alphaskia_text_metrics_free(alphaskia_text_metrics_t text_metrics);
191231
}

lib/java/jni/include/alphaTab_alphaSkia_AlphaSkiaCanvas.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/java/jni/include/alphaTab_alphaSkia_AlphaSkiaTextMetrics.h

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)