Bug #92889 The type initializer for 'HtmlRenderer.Utils.FontsUtils' threw an exception
Submitted: 23 Oct 2018 3:42 Modified: 1 Nov 2018 15:25
Reporter: TONY HUANG Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Workbench Severity:S1 (Critical)
Version:8.0.13 OS:Microsoft Windows (Windows 10 1803)
Assigned to: CPU Architecture:Any

[23 Oct 2018 3:42] TONY HUANG
Description:
Double click the connection in the front page, and it will crash with message "The type initializer for 'HtmlRenderer.Utils.FontsUtils' threw an exception"

The stack of this error can be found in the log file: 

Exception = System.TypeInitializationException
Message = The type initializer for 'HtmlRenderer.Utils.FontsUtils' threw an exception.
FullText = System.TypeInitializationException: The type initializer for 'HtmlRenderer.Utils.FontsUtils' threw an exception. ---> System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at HtmlRenderer.Utils.FontsUtils..cctor()
   --- End of inner exception stack trace ---
   at HtmlRenderer.Utils.FontsUtils.IsFontExists(String family)
   at HtmlRenderer.Parse.CssParser.ParseFontFamilyProperty(String propValue)
   at HtmlRenderer.Parse.CssParser.AddProperty(String propName, String propValue, Dictionary`2 properties)
   at HtmlRenderer.Parse.CssParser.ParseCssBlockProperties(String blockSource)
   at HtmlRenderer.Parse.CssParser.ParseCssBlockImp(String className, String blockSource)
   at HtmlRenderer.Parse.CssParser.FeedStyleBlock(CssData cssData, String block, String media)
   at HtmlRenderer.Parse.CssParser.ParseStyleBlocks(CssData cssData, String stylesheet)
   at HtmlRenderer.Utils.CssUtils.get_DefaultCssData()
   at HtmlRenderer.HtmlContainer.SetHtml(String htmlSource, CssData baseCssData)
   at HtmlRenderer.HtmlPanel.set_Text(String value)
   at MySQL.Forms.ViewEventTarget.HandleMouseClick(Object sender, MouseEventArgs e)
   at System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

How to repeat:
1. Install MySQL Workbench with Simplified Chinese Version of Windows 10, which contains the font 'Yuanti SC'

2. Start MySQL Workbench and connect to a database

3. Crash with above message

Suggested fix:
This issue is caused by 2 reasons:

1. There are multiple fonts with same name but different weight (which are 'Yuanti SC Bold', 'Yuanti SC Light', 'Yuanti SC Regular')

2. The .Net Framework returns these fonts in three font families with same name:

    The value of System.Drawing.FontFamily.Families contains three elements whose names are all 'Yuanti SC'

3. The HtmlRenderer 1.4.14 (which is used by current version of MySQL Workbench) cannot handle this properly. Please refer to the static constructor of HtmlRenderer.Utils.FontsUtils (Line 68 to 74 at Utils\FontsUtils.cs):

    foreach (var family in FontFamily.Families)
    {
        _existingFontFamilies.Add(family.Name, family);
    }

    It will throw "System.ArgumentException: An item with the same key has already been added."

The suggested fix is, add check in this loop like this:

    foreach (var family in FontFamily.Families)
    {
        if (!_existingFontFamilies.ContainsKey(family.Name))
            _existingFontFamilies.Add(family.Name, family);
    }
[25 Oct 2018 5:40] Umesh Shastry
Hello TONY,

Thank you for the report.
My colleague Wanzheng Li tried to reproduce this issue at his end but not seeing any issues. He confirmed that 'Yuanti SC' fonts( 'Yuanti SC Bold', 'Yuanti SC Light', 'Yuanti SC Regular') are not contained in Chinese Win10, but to confirm the issue he tried to download them from web. I'll join shortly screenshot of his steps.

If you can provide more information, feel free to add it to this bug and change the status back to 'Open'.

Thank you for your interest in MySQL.

Thanks,
Umesh
[25 Oct 2018 5:41] Umesh Shastry
Screenshot

Attachment: 92889.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document, text), 156.17 KiB.

[25 Oct 2018 10:06] TONY HUANG
The proof of the bug, and solution to this bug.

Attachment: proof.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document, text), 200.65 KiB.

[25 Oct 2018 10:09] TONY HUANG
Thank you for reply.

I added the proof and solution to this bug as a Word file `proof.docx'.

Please check it out.

And, it seems that I do not have the privilege to change the status to 'Open'.

Thanks.
[29 Oct 2018 5:03] Umesh Shastry
Thank you, TONY HUANG for the details.
My colleague Wanzheng Li, tried to reproduce but still not seeing the issue.
He confirmed to me that although they are both Yuanti SC font set but seems something different. In order to reproduce this issue at our end, may I request you to please provide font that you are using(you may wish to mark it as private after posting here)? Thank you.

regards,
Umesh
[29 Oct 2018 5:03] Umesh Shastry
FontDetails

Attachment: aboutFont.doc (application/msword, text), 114.50 KiB.

[1 Nov 2018 15:25] TONY HUANG
I uploaded these font files to the SFTP server, please check it out.

There are some compressed file segments attached to the Files section which are uploaded by mistake, please ignore them.

Thank you.
[15 Jan 8:34] Miguel Solorzano
https://bugs.mysql.com/bug.php?id=93928 marked as duplicate of this one.