Bug 35048 - CTFont GetGlyphsForCharacters does not work with UTF32
Summary: CTFont GetGlyphsForCharacters does not work with UTF32
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.0 (iOS9)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: (C7)
Assignee: Sebastien Pouliot
Depends on:
Reported: 2015-10-19 11:19 UTC by Adam Hartley [MSFT]
Modified: 2015-10-19 19:27 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Adam Hartley [MSFT] 2015-10-19 11:19:51 UTC
## Steps to reproduce

Use the following code:

public void TestFont ()
var font = CoreGraphics.CGFont.CreateWithFontName ("AppleColorEmoji");
var ctfont = font.ToCTFont ((nfloat)10.0);

string text = "\ud83d\ude00";

ushort[] gid = new ushort[2];
ushort[] tcs = new ushort[2];

tcs [0] = (ushort)text[0];
tcs [1] = (ushort)text[1];

bool validGlyph = CTFontGetGlyphsForCharacters (ctfont.Handle, tcs, gid, 2);

Console.WriteLine ("Test1 {0} {1}", validGlyph, gid [0]);

validGlyph = ctfont.GetGlyphsForCharacters (text.ToCharArray (), gid);

Console.WriteLine ("Test2 {0} {1}", validGlyph, gid [0]);

This works with a UTF16 char, but not with a UTF32 char (like emoji)

## Workaround

The workaround is to override the binding and specify ushort instead of UInt16

[System.Runtime.InteropServices.DllImport ("/System/Library/Frameworks/CoreText.framework/CoreText")]
private static extern bool CTFontGetGlyphsForCharacters (System.IntPtr font, [System.Runtime.InteropServices.In] ushort[] characters, [System.Runtime.InteropServices.Out] ushort[] glyphs, System.nint count);
Comment 1 Sebastien Pouliot 2015-10-19 15:22:43 UTC
`ushort` is C# syntactic sugar for `System.UInt16`, IOW it's compiled into the same thing.

OTOH it's `char` (System.Char) that's being used in the bindings - and while it's 2 bytes in .NET the marshalling for p/invokes can be different.
Comment 2 Sebastien Pouliot 2015-10-19 16:51:51 UTC
The [DllImport] does not specify a CharSet.

It looks like it's the only place we used char, without specifying a CharSet, in CT* API [1].

[1] git grep extern CoreText/*.cs | grep char
Comment 3 Sebastien Pouliot 2015-10-19 19:27:50 UTC
Fixed in maccore/master d55453c30ac917e32eb6f092fae2f8f644d807ed