Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Conversation

@trungnt2910
Copy link

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

… and clean them when the GpFontCollection is destoryed
…ether. Behaviour more consistent with Windows
Base automatically changed from master to main March 12, 2021 13:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GdipGetFontCollectionFamilyList does not correctly identify font families

1 participant