Search code examples
c#sharpdxdirectwrite

How to use TextAnalyzer.GetTypographicFeatures method


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.


Solution

  • 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);
    }