Question

I want to call a GDI method that I can't find in GDI+ in a .NET app.

In particular this method which gets kerning pairs for a specified font. I want to implement kerning (letter-spacing) on HTML5 canvas which isn't currently supported and I figured the best way was to pull out kerning pairs on my server and return a kerning table to the client.

How do you use windows library functions like this from .NET?

Was it helpful?

Solution

you can find this method in Windows API for calling this method :

  • 1 : add System.Runtime.InteropServices namespace to your project
  • 2 : add the API class to your project and let it use GetKerningPairs method please remember that this function uses a struct called KERNINGPAIR we need to make sure it's defined in our class otherwise we'll get compile error !

    class API
    {
        [DllImport("gdi32.dll")]
        static extern uint GetKerningPairs(IntPtr hdc, uint nNumPairs,
        [Out] KERNINGPAIR[] lpkrnpair);
    
        [StructLayout(LayoutKind.Sequential)]
        struct KERNINGPAIR
        {
            public ushort wFirst; // might be better off defined as char
            public ushort wSecond; // might be better off defined as char
            public int iKernAmount;
    
            public KERNINGPAIR(ushort wFirst, ushort wSecond, int iKernAmount)
            {
                this.wFirst = wFirst;
                this.wSecond = wSecond;
                this.iKernAmount = iKernAmount;
            }
    
            public override string ToString()
            {
                return (String.Format("{{First={0}, Second={1}, Amount={2}}}", wFirst, wSecond, iKernAmount));
            }
        }
    }
    

now you can call this method through API class

OTHER TIPS

I think you're out of luck.

According to this thread

It seems there’s no relevant classes for kerning pair. Glyphs will generate sensible default values for glyph indices and advance widths.

It may be possible to try pinvoke but as the note under that post says

Please note: it appears that the data returned is for the default unicode block only.

There may be a way to get more info by changing the code page ( strictly a guess on my part ).

The only way I've been able to get ALL the kerning data is to parse the files directly; not easy to say the least.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top