0

I need to get the available OpenType features for a given font in my app (C#). I'm using DirectWrite through SharpDX and I'm having a really bad time.

I know that the best solution is to use this method:

SharpDX.DirectWrite.TextAnalyzer2.GetTypographicFeatures

but I don't know how to get a result from it, as I don't know where to get the parameters.

These are the parameters I need to provide in order to get the Font Features:

  1. fontFace FontFace
  2. scriptAnalysis ScriptAnalysis
  3. localName String
  4. maxTagCount int
  5. actualTagCount int
  6. tags FontFeatureTag

Can someone please provide me a better explanation or (ideally) some code. There is almost no documentation about it so I don't know where I can get these parameters and/or what they mean.

Thanks in advance.

Leisvan
  • 105
  • 1
  • 7
  • It's documented here https://learn.microsoft.com/en-us/windows/desktop/api/dwrite_2/nf-dwrite_2-idwritetextanalyzer2-gettypographicfeatures. Tags are returned with last argument. – bunglehead Apr 18 '19 at 08:38
  • Thanks bunglehead, but that's not what I asked. I need someone to explain where do i get those arguments or how to use it. For example, how do I know the actualTagCount or what value should I use in the maxTagCount parameter. Thanks for your answer though – Leisvan Apr 19 '19 at 06:34
  • For one, I don't see C# wrapper for this method in current SharpDX. Using it from C/C++ is quite obvious - first 4 arguments are input, 'tags' is an array of maxTagCount elements that you allocate. 'actualTagCount' is what method returns so you can check if maxTagCount was enough or if you have to realloc and call the method again. – bunglehead Apr 19 '19 at 07:05
  • Bunglehead it wasn't that obvious for me :) Thanks – Leisvan Apr 20 '19 at 05:10

1 Answers1

0

I figure it out lastly. Thanks to Buglehead who gave me the final piece of the puzzle.

Here is an example. In this code I first load all system fonts, then get a specific font, and then get the FontFeatureTags for that specific font.

using SharpDX.DirectWrite;

private void LoadFontFeatureTags()
{
    Factory f = new Factory(FactoryType.Isolated);
    Factory4 _factory = new Factory4(f.NativePointer);
    _factory.CreateFontCollectionFromFontSet(_factory.SystemFontSet, out FontCollection1 collection);
    List<SharpDX.DirectWrite.FontFamily> loadedFonts = new List<SharpDX.DirectWrite.FontFamily>();
    for (int i = 0; i < collection.FontFamilyCount; i++)
    {
        var family = collection.GetFontFamily(i);
        loadedFonts.Add(family);
    }
    var gabriolaFont = loadedFonts.FirstOrDefault(x => x.FamilyNames.GetString(0).Contains("Gabriola"));
    var gabriolaFirstChild = gabriolaFont.GetFont(0);
    Font3 f3 = new Font3(gabriolaFirstChild.NativePointer);
    f3.CreateFontFace(out FontFace3 face3);

    ScriptAnalysis analysis = new ScriptAnalysis();
    TextAnalyzer analyzer = new TextAnalyzer(f);
    TextAnalyzer2 analyzer2 = new TextAnalyzer2((IntPtr)analyzer);
    int maxTagCount = 32;
    FontFeatureTag[] featuresArray = new FontFeatureTag[maxTagCount];
    analyzer2.GetTypographicFeatures(face3, analysis, "es-US", maxTagCount, out int actualCount, featuresArray);
}
Leisvan
  • 105
  • 1
  • 7