Skip to content

[CoreText] Implement Xcode 16.0 beta 1-6 changes. #20878

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

Merged
merged 9 commits into from
Aug 26, 2024
Merged
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
93 changes: 93 additions & 0 deletions src/CoreText/CTFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,99 @@ public CTFontTable [] GetAvailableTables (CTFontTableOptions options)
}

#endregion

#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
#else
[Watch (11, 0), TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
extern static /* CGRect */ CGRect CTFontGetTypographicBoundsForAdaptiveImageProvider (
/* CTFontRef */ IntPtr font,
/* id<CTAdaptiveImageProviding> __Nullable */ IntPtr provider);

/// <summary>Computes metrics that clients performing their own typesetting of an adaptive image glyph need.</summary>
/// <returns>The typographic bounds in points expressed as a rectangle, where the rectangle's Width property corresponds to the advance width, the rectangle's Bottom property corresponds to the ascent (above the baseline), and Top property corresponds to the descent (below the baseline).</returns>
/// <param name="provider">The adaptive image provider used during the computation. If null, then default results will be returned, on the assumption that an image is not yet available.</param>
#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
#else
[Watch (11, 0), TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
#endif
public CGRect GetTypographicBoundsForAdaptiveImageProvider (ICTAdaptiveImageProviding? provider)
{
return CTFontGetTypographicBoundsForAdaptiveImageProvider (Handle, provider.GetHandle ());
}

#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
#else
[Watch (11, 0), TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
extern static void CTFontDrawImageFromAdaptiveImageProviderAtPoint (
/* CTFontRef */ IntPtr font,
/* id<CTAdaptiveImageProviding> __Nullable */ IntPtr provider,
/* CGPoint */ CGPoint point,
/* CGContexRef */ IntPtr context);

/// <summary>Draws the image for an adaptive image glyph at the given point.</summary>
/// <param name="provider">The adaptive image provider used during the rendering.</param>
/// <param name="point">The adaptive image glyph is rendered relative to this point.</param>
/// <param name="context">The <see cref="CoreGraphics.CGBitmapContext" /> where the adaptive image glyph is drawn.</param>
#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
#else
[Watch (11, 0), TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
#endif
public void DrawImage (ICTAdaptiveImageProviding provider, CGPoint point, CGContext context)
{
CTFontDrawImageFromAdaptiveImageProviderAtPoint (Handle, provider.GetNonNullHandle (nameof (provider)), point, context.GetNonNullHandle (nameof (context)));
}

#if NET
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos13.0")]
#else
[Watch (6, 0), TV (13, 0), iOS (13, 0), MacCatalyst (13, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
extern static byte CTFontHasTable (
/* CTFontRef */ IntPtr font,
/* CTFontTableTag */ CTFontTable tag);

/// <summary>Checks whether a table is present in a font.</summary>
/// <param name="tag">The table identifier to check for.</param>
/// <returns>Whether the table is present in the font or not.</returns>
/// <remarks>The check behaves as if <see cref="CTFontTableOptions.None" /> was specified.</remarks>
#if NET
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos13.0")]
#else
[Watch (6, 0), TV (13, 0), iOS (13, 0), MacCatalyst (13, 0)]
#endif
public bool HasTable (CTFontTable tag)
{
return CTFontHasTable (GetCheckedHandle (), tag) != 0;
}


public override string? ToString ()
{
return FullName;
Expand Down
11 changes: 11 additions & 0 deletions src/CoreText/CTFontDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ public bool Enabled {
}
}
#endif // !XAMCORE_5_0

#if NET && (__IOS__ || __MACCATALYST__)
[SupportedOSPlatform ("ios13.0")]
[UnsupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("macos")]
public string? RegistrationUserInfo {
get { return Adapter.GetStringValue (Dictionary, CTFontDescriptorAttributeKey.RegistrationUserInfo); }
set { Adapter.SetValue (Dictionary, CTFontDescriptorAttributeKey.RegistrationUserInfo!, value); }
}
#endif
}

#if NET
Expand Down
66 changes: 65 additions & 1 deletion src/CoreText/CTFontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public enum CTFontManagerScope : uint {
#endif
Persistent = 2,
#if NET
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("ios")]
[UnsupportedOSPlatform ("tvos")]
Expand Down Expand Up @@ -442,9 +442,41 @@ public static CTFontDescriptor [] GetFonts (NSUrl url)
}
}

#if NET
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("tvos18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("ios18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("maccatalyst18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
#else
[Deprecated (PlatformName.iOS, 18, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.MacCatalyst, 18, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.TvOS, 18, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.MacOSX, 15, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.WatchOS, 11, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
#endif
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerRegisterGraphicsFont (IntPtr cgfont, IntPtr* error);

#if NET
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("tvos18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("ios18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("maccatalyst18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
#else
[Deprecated (PlatformName.iOS, 18, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.MacCatalyst, 18, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.TvOS, 18, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.MacOSX, 15, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[Deprecated (PlatformName.WatchOS, 11, 0, message: "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
#endif
public static bool RegisterGraphicsFont (CGFont font, [NotNullWhen (true)] out NSError? error)
{
if (font is null)
Expand All @@ -466,9 +498,41 @@ public static bool RegisterGraphicsFont (CGFont font, [NotNullWhen (true)] out N
return ret;
}

#if NET
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0")]
[ObsoletedOSPlatform ("tvos18.0")]
[ObsoletedOSPlatform ("ios18.0")]
[ObsoletedOSPlatform ("maccatalyst18.0")]
#else
[Deprecated (PlatformName.iOS, 18, 0)]
[Deprecated (PlatformName.MacCatalyst, 18, 0)]
[Deprecated (PlatformName.TvOS, 18, 0)]
[Deprecated (PlatformName.MacOSX, 15, 0)]
[Deprecated (PlatformName.WatchOS, 11, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerUnregisterGraphicsFont (IntPtr cgfont, IntPtr* error);

#if NET
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0")]
[ObsoletedOSPlatform ("tvos18.0")]
[ObsoletedOSPlatform ("ios18.0")]
[ObsoletedOSPlatform ("maccatalyst18.0")]
#else
[Deprecated (PlatformName.iOS, 18, 0)]
[Deprecated (PlatformName.MacCatalyst, 18, 0)]
[Deprecated (PlatformName.TvOS, 18, 0)]
[Deprecated (PlatformName.MacOSX, 15, 0)]
[Deprecated (PlatformName.WatchOS, 11, 0)]
#endif
public static bool UnregisterGraphicsFont (CGFont font, out NSError? error)
{
if (font is null)
Expand Down
17 changes: 17 additions & 0 deletions src/CoreText/CTStringAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,22 @@ public void SetWritingDirection (params CTWritingDirection [] writingDirections)
CFMutableDictionary.SetValue (Dictionary.Handle, CTStringAttributeKey.WritingDirection.GetHandle (), array);
GC.KeepAlive (numbers); // make sure the numbers aren't freed until we're done with them
}

#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
// The attribute value must be an object conforming to the CTAdaptiveImageProviding protocol.
public ICTAdaptiveImageProviding? AdaptiveImageProvider {
get {
var h = CFDictionary.GetValue (Dictionary.Handle, CTStringAttributeKey.AdaptiveImageProvider.GetHandle ());
return Runtime.GetINativeObject<ICTAdaptiveImageProviding> (h, owns: false);
}
set {
Adapter.SetNativeValue (Dictionary, CTStringAttributeKey.AdaptiveImageProvider!, value);
}
}
#endif
}
}
22 changes: 22 additions & 0 deletions src/coretext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//

using System;

using CoreGraphics;
using Foundation;
using ObjCRuntime;

Expand Down Expand Up @@ -224,6 +226,10 @@ interface CTFontDescriptorAttributeKey {

[Field ("kCTFontEnabledAttribute")]
NSString Enabled { get; }

[iOS (13, 0), NoTV, NoWatch, MacCatalyst (13, 1), NoMac]
[Field ("kCTFontRegistrationUserInfoAttribute")]
NSString RegistrationUserInfo { get; }
}

/// <summary>A class whose static properties can be used as keys for the <see cref="T:Foundation.NSDictionary" /> used by <see cref="T:CoreText.CTTextTabOptions" />.</summary>
Expand Down Expand Up @@ -444,11 +450,27 @@ interface CTStringAttributeKey {

[Field ("kCTWritingDirectionAttributeName")]
NSString WritingDirection { get; }

[Field ("kCTRubyAnnotationAttributeName")]
NSString RubyAnnotation { get; }

[Watch (11, 0), TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
[Field ("kCTAdaptiveImageProviderAttributeName")]
NSString AdaptiveImageProvider { get; }
#endif

[Watch (6, 0), TV (13, 0), iOS (13, 0)]
[MacCatalyst (13, 1)]
[Field ("kCTTrackingAttributeName")]
NSString TrackingAttributeName { get; }
}

[Watch (11, 0), TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
[Protocol (BackwardsCompatibleCodeGeneration = false)]
interface CTAdaptiveImageProviding {
[Abstract]
[Export ("imageForProposedSize:scaleFactor:imageOffset:imageSize:")]
[return: NullAllowed]
CGImage GetImage (CGSize proposedSize, nfloat scaleFactor, out CGPoint imageOffset, out CGSize imageSize);
}
}
7 changes: 7 additions & 0 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33248,6 +33248,7 @@ M:CoreText.CTTypesetter.SuggestLineBreak(System.Int32,System.Double,System.Doubl
M:CoreText.CTTypesetter.SuggestLineBreak(System.Int32,System.Double)
M:CoreText.CTTypesetterOptions.#ctor
M:CoreText.CTTypesetterOptions.#ctor(Foundation.NSDictionary)
M:CoreText.ICTAdaptiveImageProviding.GetImage(CoreGraphics.CGSize,System.Runtime.InteropServices.NFloat,CoreGraphics.CGPoint@,CoreGraphics.CGSize@)
M:CoreVideo.CVBuffer.GetAttachment(Foundation.NSString,CoreVideo.CVAttachmentMode@)
M:CoreVideo.CVBuffer.GetAttachment``1(Foundation.NSString,CoreVideo.CVAttachmentMode@)
M:CoreVideo.CVBuffer.GetAttachments(CoreVideo.CVAttachmentMode)
Expand Down Expand Up @@ -63663,6 +63664,7 @@ P:CoreText.CTFontDescriptorAttributeKey.Matrix
P:CoreText.CTFontDescriptorAttributeKey.Name
P:CoreText.CTFontDescriptorAttributeKey.Priority
P:CoreText.CTFontDescriptorAttributeKey.RegistrationScope
P:CoreText.CTFontDescriptorAttributeKey.RegistrationUserInfo
P:CoreText.CTFontDescriptorAttributeKey.Size
P:CoreText.CTFontDescriptorAttributeKey.StyleName
P:CoreText.CTFontDescriptorAttributeKey.Traits
Expand All @@ -63686,6 +63688,7 @@ P:CoreText.CTFontDescriptorAttributes.Matrix
P:CoreText.CTFontDescriptorAttributes.Name
P:CoreText.CTFontDescriptorAttributes.Priority
P:CoreText.CTFontDescriptorAttributes.RegistrationScope
P:CoreText.CTFontDescriptorAttributes.RegistrationUserInfo
P:CoreText.CTFontDescriptorAttributes.Size
P:CoreText.CTFontDescriptorAttributes.StyleName
P:CoreText.CTFontDescriptorAttributes.Traits
Expand Down Expand Up @@ -63839,6 +63842,7 @@ P:CoreText.CTRun.StringRange
P:CoreText.CTRun.TextMatrix
P:CoreText.CTRunDelegate.Operations
P:CoreText.CTRunDelegateOperations.Handle
P:CoreText.CTStringAttributeKey.AdaptiveImageProvider
P:CoreText.CTStringAttributeKey.BackgroundColor
P:CoreText.CTStringAttributeKey.BaselineClass
P:CoreText.CTStringAttributeKey.BaselineInfo
Expand All @@ -63853,6 +63857,7 @@ P:CoreText.CTStringAttributeKey.HorizontalInVerticalForms
P:CoreText.CTStringAttributeKey.KerningAdjustment
P:CoreText.CTStringAttributeKey.LigatureFormation
P:CoreText.CTStringAttributeKey.ParagraphStyle
P:CoreText.CTStringAttributeKey.RubyAnnotation
P:CoreText.CTStringAttributeKey.RunDelegate
P:CoreText.CTStringAttributeKey.StrokeColor
P:CoreText.CTStringAttributeKey.StrokeWidth
Expand All @@ -63862,6 +63867,7 @@ P:CoreText.CTStringAttributeKey.UnderlineColor
P:CoreText.CTStringAttributeKey.UnderlineStyle
P:CoreText.CTStringAttributeKey.VerticalForms
P:CoreText.CTStringAttributeKey.WritingDirection
P:CoreText.CTStringAttributes.AdaptiveImageProvider
P:CoreText.CTStringAttributes.BackgroundColor
P:CoreText.CTStringAttributes.BaselineClass
P:CoreText.CTStringAttributes.BaselineOffset
Expand Down Expand Up @@ -79542,6 +79548,7 @@ T:CoreText.CTUnderlineStyle
T:CoreText.CTUnderlineStyleModifiers
T:CoreText.CTWritingDirection
T:CoreText.FontFeatureGroup
T:CoreText.ICTAdaptiveImageProviding
T:CoreVideo.CVDisplayLink
T:CoreVideo.CVDisplayLink.DisplayLinkOutputCallback
T:CoreVideo.CVFillExtendedPixelsCallBack
Expand Down
40 changes: 40 additions & 0 deletions tests/monotouch-test/CoreText/FontTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public void CTFontCreateWithNameAndOptions ()

using (var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default)) {
Assert.That (font.Handle, Is.Not.EqualTo (IntPtr.Zero), "Handle");
if (TestRuntime.CheckXcodeVersion (11, 0))
Assert.That (font.HasTable (CTFontTable.ScalableVectorGraphics), Is.EqualTo (false), "HasTable");
}
}

Expand Down Expand Up @@ -129,5 +131,43 @@ public void CTFontCopyNameForGlyph ()
using (var ctfont = font.ToCTFont ((nfloat) 10.0))
Assert.Null (ctfont.GetGlyphName ('\ud83d'), "2");
}

[Test]
public void DrawImage ()
{
TestRuntime.AssertXcodeVersion (16, 0);

using var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default);
using var provider = new AdaptiveImageProvider ();
using var space = CGColorSpace.CreateDeviceRGB ();
using var context = new CGBitmapContext (null, 10, 10, 8, 40, space, CGBitmapFlags.PremultipliedLast);
font.DrawImage (provider, CGPoint.Empty, context);
Assert.AreEqual (1, provider.Count, "#Count");
}

#if !__WATCHOS__
[Test]
public void GetTypographicBoundsForAdaptiveImageProvider ()
{
TestRuntime.AssertXcodeVersion (16, 0);

using var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default);
using var provider = new AdaptiveImageProvider ();
var bounds = font.GetTypographicBoundsForAdaptiveImageProvider (provider);
Assert.AreEqual (new CGRect (0, -3.90625, 13, 16.40625), bounds, "Bounds");
Assert.AreEqual (0, provider.Count, "#Count");
}
#endif // !__WATCHOS__

class AdaptiveImageProvider : NSObject, ICTAdaptiveImageProviding {
public int Count;
public CGImage? GetImage (CGSize proposedSize, nfloat scaleFactor, out CGPoint imageOffset, out CGSize imageSize)
{
imageOffset = default (CGPoint);
imageSize = default (CGSize);
Count++;
return null;
}
}
}
}
Loading