sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.7k stars 639 forks source link

[DWrite] GlyphRunDescription.ClusterMap would most appropriately marshal to a ushort[]. #1013

Open Jayonas opened 6 years ago

Jayonas commented 6 years ago

It's certainly understandable that the author of the custom marshaling code for GlyphRunDescription wouldn't really know what to do with ClusterMap except leave it as a pointer, since the DWrite docs aren't particularly descriptive. The length of the array is the stringLength field of DWRITE_GLYPH_RUN_DESCRIPTION. That and the existing Utilities.Read helper made a conversion method very simple for me to implement:

public static ushort[] MarshalClusterMap(this GlyphRunDescription grd)
{
    ushort[] ar = new ushort[grd.Text.Length];
    Utilities.Read(grd.ClusterMap, ar, 0, grd.Text.Length);
    return ar;
}

The point of the cluster map array is that you can index into it using character indices from the Text field of GlyphRunDescription. The value you get out of the cluster map is itself an index into the glyph array fields (Indices, Advances, and Offsets) of the corresponding GlyphRun. In other words, the array maps character indices to glyph indices. This is how you correctly map characters from the source string to the glyph(s) that represent them.

(Note that I'm basing this off my usage of SharpDX 3.1.1, which I realize is out of date, but it also doesn't look like much has happened in the DWrite code since then so I think it's likely that this is still an issue.)