libgdiplus icon indicating copy to clipboard operation
libgdiplus copied to clipboard

Grouped fonts with same family

Open trungnt2910 opened this issue 4 years ago • 0 comments

This should fix #692.

I've changed GpFontFamily implementation, so that it keeps tracks of all fonts of the same family. I've also changed FontFamily functions so that they handle style properly like they should on Windows.

This code should work correctly after applying fix:

            fontCollection = new PrivateFontCollection();

            var resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames()
                .Where((name) => name.StartsWith("UI.Fonts.OpenSans"));
            
            foreach (var resoureName in resourceNames)
            {
                var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resoureName);
                var fontData = new byte[stream.Length];
                stream.Read(fontData, 0, (int)stream.Length);
                stream.Dispose();

                var fontPtr = Marshal.AllocCoTaskMem(fontData.Length);
                Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
                fontCollection.AddMemoryFont(fontPtr, fontData.Length);
                Marshal.FreeCoTaskMem(fontPtr);
            }

            Light = fontCollection.Families.FirstOrDefault(font => font.Name == "Open Sans Light");
            Regular = fontCollection.Families.FirstOrDefault(font => font.Name == "Open Sans");
            ExtraBold = fontCollection.Families.FirstOrDefault(font => font.Name == "Open Sans ExtraBold");
            SemiBold = fontCollection.Families.FirstOrDefault(font => font.Name == "Open Sans SemiBold");

            System.Windows.Forms.MessageBox.Show(string.Join("\n", fontCollection.Families.Select(f => f.Name)));
            Preview(new Font(Regular, 72, FontStyle.Bold | FontStyle.Italic), "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^");

Before applying fix, with build from head of master: On my Ubuntu 20.04 with latest mono, the library seems to pick a random style: This is output from multiple runs: First run: image Second run: Screenshot from 2021-02-09 22-29-15

After applying fix, the correct style (Bold Itatlic) is applied: Screenshot from 2021-02-09 22-35-27

trungnt2910 avatar Feb 09 '21 15:02 trungnt2910