Skip to content

feat: Add TextMetrics to API #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build/Build.LibAlphaSkia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ partial class Build
"../../wrapper/src/alphaskia_canvas.cpp",
"../../wrapper/src/alphaskia_image.cpp",
"../../wrapper/src/alphaskia_typeface.cpp",
"../../wrapper/src/alphaskia_textstyle.cpp",
"../../wrapper/src/alphaskia_text_style.cpp",
"../../wrapper/src/alphaskia_text_metrics.cpp",
"../../wrapper/src/alphaskia_data.cpp",
"../../wrapper/src/alphaskia_string.cpp"
]
Expand Down Expand Up @@ -158,6 +159,7 @@ partial class Build
"../../lib/java/jni/src/AlphaSkiaData.cpp",
"../../lib/java/jni/src/AlphaSkiaImage.cpp",
"../../lib/java/jni/src/AlphaSkiaTextStyle.cpp",
"../../lib/java/jni/src/AlphaSkiaTextMetrics.cpp",
"../../lib/java/jni/src/AlphaSkiaTypeface.cpp"
]
}
Expand Down
10 changes: 6 additions & 4 deletions lib/dotnet/AlphaSkia/AlphaSkiaCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,18 @@ public void FillText(string text, AlphaSkiaTextStyle textStyle, float fontSize,
}

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

/// <summary>
Expand Down
73 changes: 73 additions & 0 deletions lib/dotnet/AlphaSkia/AlphaSkiaTextMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace AlphaSkia;

/// <summary>
/// The TextMetrics interface represents the dimensions of a piece of text in the canvas;
/// A TextMetrics instance can be retrieved using the <see cref="AlphaSkiaCanvas.MeasureText"/> method.
/// </summary>
public sealed class AlphaSkiaTextMetrics : AlphaSkiaNative
{
private AlphaSkiaTextMetrics(IntPtr handle)
: base(handle, NativeMethods.alphaskia_text_metrics_free)
{
}

/// <summary>
/// Returns the width of a segment of inline text in pixels. It takes into account the current font of the context.
/// </summary>
public float Width => NativeMethods.alphaskia_text_metrics_get_width(Handle);

/// <summary>
/// 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.
/// </summary>
public float ActualBoundingBoxLeft => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_left(Handle);

/// <summary>
/// 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.
/// </summary>
public float ActualBoundingBoxRight => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_right(Handle);

/// <summary>
/// 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.
/// </summary>
public float FontBoundingBoxAscent => NativeMethods.alphaskia_text_metrics_get_font_bounding_box_ascent(Handle);

/// <summary>
/// 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.
/// </summary>
public float FontBoundingBoxDescent => NativeMethods.alphaskia_text_metrics_get_font_bounding_box_descent(Handle);

/// <summary>
/// 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.
/// </summary>
public float ActualBoundingBoxAscent => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_ascent(Handle);

/// <summary>
/// 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.
/// </summary>
public float ActualBoundingBoxDescent => NativeMethods.alphaskia_text_metrics_get_actual_bounding_box_descent(Handle);

/// <summary>
/// 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.
/// </summary>
public float EmHeightAscent => NativeMethods.alphaskia_text_metrics_get_em_height_ascent(Handle);

/// <summary>
/// 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.
/// </summary>
public float EmHeightDescent => NativeMethods.alphaskia_text_metrics_get_em_height_descent(Handle);

/// <summary>
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the hanging baseline of the line box, in pixels.
/// </summary>
public float HangingBaseline => NativeMethods.alphaskia_text_metrics_get_hanging_baseline(Handle);

/// <summary>
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the alphabetic baseline of the line box, in pixels.
/// </summary>
public float AlphabeticBaseline => NativeMethods.alphaskia_text_metrics_get_hanging_baseline(Handle);

/// <summary>
/// Returns the distance from the horizontal line indicated by the textBaseline parameter to the ideographic baseline of the line box, in CSS pixels.
/// </summary>
public float IdeographicBaseline => NativeMethods.alphaskia_text_metrics_get_ideographic_baseline(Handle);
}
2 changes: 1 addition & 1 deletion lib/dotnet/AlphaSkia/AlphaSkiaTextStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public sealed class AlphaSkiaTextStyle : AlphaSkiaNative
/// <param name="weight">The font weight typefaces should have.</param>
/// <param name="isItalic">Whether typefaces should be italic.</param>
public AlphaSkiaTextStyle(string[] fontFamilies, ushort weight, bool isItalic)
: base(NativeMethods.alphaskia_textstyle_new((byte)fontFamilies.Length, fontFamilies, weight, isItalic ? (byte)1 : (byte)0), NativeMethods.alphaskia_textstyle_free)
: base(NativeMethods.alphaskia_text_style_new((byte)fontFamilies.Length, fontFamilies, weight, isItalic ? (byte)1 : (byte)0), NativeMethods.alphaskia_text_style_free)
{
FontFamilies = fontFamilies;
Weight = weight;
Expand Down
3 changes: 2 additions & 1 deletion lib/dotnet/AlphaSkia/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
global using alphaskia_canvas_t = System.IntPtr;
global using alphaskia_data_t = System.IntPtr;
global using alphaskia_string_t = System.IntPtr;
global using alphaskia_textstyle_t = System.IntPtr;
global using alphaskia_text_style_t = System.IntPtr;
global using alphaskia_text_metrics_t = System.IntPtr;
56 changes: 48 additions & 8 deletions lib/dotnet/AlphaSkia/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ internal static class NativeMethods
public static extern void alphaskia_typeface_free(alphaskia_typeface_t type_face);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern alphaskia_textstyle_t alphaskia_textstyle_new(byte family_name_count, string[] family_names, ushort weight, byte italic);
public static extern alphaskia_text_style_t alphaskia_text_style_new(byte family_name_count, string[] family_names, ushort weight, byte italic);
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern byte alphaskia_textstyle_get_family_name_count(alphaskia_textstyle_t textstyle);
public static extern byte alphaskia_text_style_get_family_name_count(alphaskia_text_style_t text_style);
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern alphaskia_string_t alphaskia_textstyle_get_family_name(alphaskia_textstyle_t textstyle, byte index);
public static extern alphaskia_string_t alphaskia_text_style_get_family_name(alphaskia_text_style_t text_style, byte index);
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern ushort alphaskia_textstyle_get_weight(alphaskia_textstyle_t textstyle);
public static extern ushort alphaskia_text_style_get_weight(alphaskia_text_style_t text_style);
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern byte alphaskia_textstyle_is_italic(alphaskia_textstyle_t textstyle);
public static extern byte alphaskia_text_style_is_italic(alphaskia_text_style_t text_style);
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void alphaskia_textstyle_free(alphaskia_textstyle_t textstyle);
public static extern void alphaskia_text_style_free(alphaskia_text_style_t text_style);

[DllImport(AlphaSkiaNativeLibName)]
public static extern alphaskia_typeface_t alphaskia_typeface_make_from_name(
Expand Down Expand Up @@ -168,15 +168,16 @@ public static extern void alphaskia_canvas_fill_text(alphaskia_canvas_t canvas,
[MarshalAs(UnmanagedType.LPWStr)]
string text,
int text_length,
alphaskia_textstyle_t textstyle, float font_size, float x, float y, AlphaSkiaTextAlign text_align,
alphaskia_text_style_t text_style, float font_size, float x, float y, AlphaSkiaTextAlign text_align,
AlphaSkiaTextBaseline baseline);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_canvas_measure_text(alphaskia_canvas_t canvas,
[MarshalAs(UnmanagedType.LPWStr)]
string text,
int text_length,
alphaskia_textstyle_t textstyle, float font_size);
alphaskia_text_style_t text_style, float font_size, AlphaSkiaTextAlign text_align,
AlphaSkiaTextBaseline baseline);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void alphaskia_canvas_begin_rotate(alphaskia_canvas_t canvas, float center_x, float center_y,
Expand All @@ -188,4 +189,43 @@ public static extern void alphaskia_canvas_begin_rotate(alphaskia_canvas_t canva
[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void alphaskia_canvas_draw_image(alphaskia_canvas_t canvas, alphaskia_image_t image, float x,
float y, float w, float h);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_width(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_actual_bounding_box_left(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_actual_bounding_box_right(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_font_bounding_box_ascent(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_font_bounding_box_descent(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_actual_bounding_box_ascent(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_actual_bounding_box_descent(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_em_height_ascent(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_em_height_descent(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_hanging_baseline(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_alphabetic_baseline(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float alphaskia_text_metrics_get_ideographic_baseline(alphaskia_text_metrics_t text_metrics);

[DllImport(AlphaSkiaNativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void alphaskia_text_metrics_free(alphaskia_text_metrics_t text_metrics);
}
6 changes: 3 additions & 3 deletions lib/java/jni/include/alphaTab_alphaSkia_AlphaSkiaCanvas.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 117 additions & 0 deletions lib/java/jni/include/alphaTab_alphaSkia_AlphaSkiaTextMetrics.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading