Search code examples
macosmonomonodevelopgtk#visual-studio-mac

Fonts in GTK# on macOS 10.14 Mojave looking bold


There is something strange that started happening with macOS Mojave. I have a GTK# app that uses mono on macOS and it was running fine for years. Now all fonts in the application started to appear as bold. I have created a small test app to test the possible reasons for this but I am not going much further with it.

using System;
using Gtk;

namespace GtkKeyScan
{
    class Program
    {
        private static Label lblCount;
        private static DateTime? scanStart;

        static void Main (string [] args)
        {
            if (Environment.OSVersion.Platform != PlatformID.Unix)
                GLib.Thread.Init ();

            Application.Init ();

            var dlg = new Dialog { WindowPosition = WindowPosition.CenterAlways, WidthRequest = 200 };

            lblCount = new Label { Text = "Press key to see the code" };
            dlg.VBox.PackStart (lblCount, true, true, 10);

            var btnClear = new Button { Label = "Clear", WidthRequest = 110, HeightRequest = 34, CanFocus = false };
            btnClear.Clicked += btnClear_Clicked;
            dlg.VBox.PackStart (btnClear, false, true, 10);

            dlg.KeyPressEvent += ent_KeyPressEvent;
            dlg.ShowAll ();
            dlg.Run ();
        }

        static void btnClear_Clicked (object sender, EventArgs e)
        {
            lblCount.Text = "";
            scanStart = null;
        }

        [GLib.ConnectBefore]
        static void ent_KeyPressEvent (object o, KeyPressEventArgs args)
        {
            if (!string.IsNullOrWhiteSpace (lblCount.Text))
                lblCount.Text += "\n";

            lblCount.Text += args.Event.Key.ToString ();

            if (scanStart == null)
                scanStart = DateTime.Now;
            else
                lblCount.Text += " +" + (int) (DateTime.Now - scanStart.Value).TotalMilliseconds + "ms";

            args.RetVal = true;
        }
    }
}

I am using the latest Visual Studio Community for macOS version 7.6.8 and the latest mono that comes with it version 5.12.0.309.

If I build the application and run it from the command line using

mono GtkKeyScan.exe

This is how the application looks:

App normal look

But if I run it from Visual Studio the app looks like this:

App bold fonts look

The application shows with bold fonts also if I run the application from the terminal with an older version of Mono like 4.2.4 or 4.6.2.

My guess is that Visual Studio does some preparation similar to when the app is bundled for macOS into an .app and this part breaks the fonts somehow in the new macOS.


Solution

  • Looks like the problem is related to the fact that Mojave is the last macOS which will support 32 bit apps. The project I tried was built in 32 bit as on Windows GTK# can work only in 32 bit mode. It looks like in the new Mono versions GTK# works in 64 mode as well.

    So when I am starting the application from the command line it somehow runs in 64 mode despite of the fact the app was built in 32 bit mode. While Visual Studio for macOS runs the app in 32 bit mode.

    When running in 32 bit mode macOS shows the very first time that the app is not optimized for this macOS and runs it either way. But internally something breaks in the Pango part which is responsible for the text rendering. I am not sure what exactly, but switching to 64 bit mode while compiling the project made it work.