Skip to content

Commit

Permalink
fix(Studio): Built-in font being broken on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Oct 27, 2024
1 parent 009d024 commit b419803
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Studio/CelesteStudio/Controls/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Font GetFont() {
var sysFont = SystemFonts.Default();

if (Code) {
var codeFont = FontManager.EditorFontRegular;
var codeFont = FontManager.EditorFont;
return new Font(codeFont.Family, sysFont.Size, fontStyle, fontDecoration);
}

Expand Down
2 changes: 1 addition & 1 deletion Studio/CelesteStudio/Dialog/SnippetDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void GenerateListEntries(ICollection<StackLayoutItem> items) {
var shortcutTextBox = new TextBox { Text = snippet.Shortcut };
shortcutTextBox.TextChanged += (_, _) => snippet.Shortcut = shortcutTextBox.Text.ReplaceLineEndings(Document.NewLine.ToString());

var textArea = new TextArea {Text = snippet.Insert, Font = FontManager.EditorFontRegular, Width = 500 };
var textArea = new TextArea {Text = snippet.Insert, Font = FontManager.EditorFont, Width = 500 };
textArea.TextChanged += (_, _) => snippet.Insert = textArea.Text.ReplaceLineEndings(Document.NewLine.ToString());

int idx = i;
Expand Down
35 changes: 7 additions & 28 deletions Studio/CelesteStudio/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@
using System.Reflection;
using Eto.Drawing;
using SkiaSharp;
using System;
using System.Diagnostics;

namespace CelesteStudio;

public static class FontManager {
public const string FontFamilyBuiltin = "<builtin>";
#if MACOS
public const string FontFamilyBuiltinDisplayName = "Monaco (builtin)";
#else
public const string FontFamilyBuiltinDisplayName = "JetBrains Mono (builtin)";
#endif

private static Font? editorFontRegular, editorFontBold, editorFontItalic, editorFontBoldItalic, statusFont;
private static Font? editorFont, statusFont;
private static SKFont? skEditorFontRegular, skEditorFontBold, skEditorFontItalic, skEditorFontBoldItalic, skStatusFont, skPopupFont;

public static Font EditorFontRegular => editorFontRegular ??= CreateEditor(FontStyle.None);
public static Font EditorFontBold => editorFontBold ??= CreateEditor(FontStyle.Bold);
public static Font EditorFontItalic => editorFontItalic ??= CreateEditor(FontStyle.Italic);
public static Font EditorFontBoldItalic => editorFontBoldItalic ??= CreateEditor(FontStyle.Bold | FontStyle.Italic);
public static Font StatusFont => statusFont ??= CreateStatus();
public static Font EditorFont => editorFont ??= CreateFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize);
public static Font StatusFont => statusFont ??= CreateFont(Settings.Instance.FontFamily, Settings.Instance.StatusFontSize);

public static SKFont SKEditorFontRegular => skEditorFontRegular ??= CreateSKFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize * Settings.Instance.FontZoom);
public static SKFont SKEditorFontBold => skEditorFontBold ??= CreateSKFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize * Settings.Instance.FontZoom, FontStyle.Bold);
Expand All @@ -35,11 +27,6 @@ public static class FontManager {

private static FontFamily? builtinFontFamily;
public static Font CreateFont(string fontFamily, float size, FontStyle style = FontStyle.None) {
if (Platform.Instance.IsMac && fontFamily == FontFamilyBuiltin) {
// The built-in font is broken on macOS for some reason, so fallback to a system font
fontFamily = "Monaco";
}

if (fontFamily == FontFamilyBuiltin) {
var asm = Assembly.GetExecutingAssembly();
builtinFontFamily ??= FontFamily.FromStreams(asm.GetManifestResourceNames()
Expand All @@ -56,11 +43,6 @@ public static SKFont CreateSKFont(string fontFamily, float size, FontStyle style
// TODO: Don't hardcode this
const float dpi = 96.0f / 72.0f;

if (Platform.Instance.IsMac && fontFamily == FontFamilyBuiltin) {
// The built-in font is broken on macOS for some reason, so fallback to a system font
fontFamily = "Monaco";
}

if (fontFamily == FontFamilyBuiltin) {
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(style switch {
FontStyle.None => "JetBrainsMono/JetBrainsMono-Regular",
Expand Down Expand Up @@ -96,7 +78,7 @@ public static float CharWidth(this Font font) {
return width;
}
public static float LineHeight(this Font font) {
if (Eto.Platform.Instance.IsWpf) {
if (Platform.Instance.IsWpf) {
// WPF reports the line height a bit to small for some reason?
return font.LineHeight + 5.0f;
}
Expand All @@ -118,7 +100,6 @@ public static float CharWidth(this SKFont font) {
}

font.MeasureText([font.GetGlyph('X')]);
//widthCache[font] = width = font.Metrics.AverageCharacterWidth * font.ScaleX;
widthCache[font] = width = font.MeasureText([font.GetGlyph('X')]);
return width;
}
Expand All @@ -135,21 +116,19 @@ public static float Offset(this SKFont font) {

public static void OnFontChanged() {
// Clear cached fonts
editorFontRegular?.Dispose();
editorFontBold?.Dispose();
editorFontItalic?.Dispose();
editorFontBoldItalic?.Dispose();
editorFont?.Dispose();
statusFont?.Dispose();
charWidthCache.Clear();

editorFontRegular = editorFontBold = editorFontItalic = editorFontBoldItalic = statusFont = null;
editorFont = statusFont = null;

skEditorFontRegular?.Dispose();
skEditorFontBold?.Dispose();
skEditorFontItalic?.Dispose();
skEditorFontBoldItalic?.Dispose();
skStatusFont?.Dispose();
skPopupFont?.Dispose();
widthCache.Clear();

skEditorFontRegular = skEditorFontBold = skEditorFontItalic = skEditorFontBoldItalic = skStatusFont = skPopupFont = null;
}
Expand Down
10 changes: 5 additions & 5 deletions Studio/CelesteStudio/Tool/FeatherlineForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public FeatherlineForm() {
gensPerTiming = new NumericStepper { MinValue = 1, MaxValue = 999999, Value = 150, DecimalPlaces = 0, Width = stepperWidth };
timingShuffles = new NumericStepper { MinValue = 0, MaxValue = 100, Value = 6, DecimalPlaces = 0, Width = stepperWidth };
testOnInitial = new CheckBox { Text = "Test Timing On\nInitial Inputs Directly", Checked = false };
checkpoints = new TextArea { Wrap = true, Font = FontManager.EditorFontRegular, Width = textWidth };
initialInputs = new TextArea { Wrap = true, Font = FontManager.EditorFontRegular, Width = textWidth };
customHitboxes = new TextArea { Wrap = true, Font = FontManager.EditorFontRegular, Width = textWidth };
output = new TextArea { ReadOnly = true, Font = FontManager.EditorFontRegular, Width = textWidth };
checkpoints = new TextArea { Wrap = true, Font = FontManager.EditorFont, Width = textWidth };
initialInputs = new TextArea { Wrap = true, Font = FontManager.EditorFont, Width = textWidth };
customHitboxes = new TextArea { Wrap = true, Font = FontManager.EditorFont, Width = textWidth };
output = new TextArea { ReadOnly = true, Font = FontManager.EditorFont, Width = textWidth };
var layout = new DynamicLayout { DefaultSpacing = new Size(10, 10) };
layout.BeginHorizontal();
layout.BeginVertical();
Expand Down Expand Up @@ -245,7 +245,7 @@ public FeatherlineHelpForm() {
layout.BeginVertical();
layout.AddCentered(new Label { Wrap = WrapMode.Word, Text = "Getting started with Featherline", Font = h1 });
layout.Add(new Label { Wrap = WrapMode.Word, Text = "1. Run your TAS up until the frame you featherboost, such as" });
layout.Add(new Label { Wrap = WrapMode.Word, Text = " 26\r\n > 1,R,U", Font = FontManager.EditorFontRegular });
layout.Add(new Label { Wrap = WrapMode.Word, Text = " 26\r\n > 1,R,U", Font = FontManager.EditorFont });
layout.Add(new Label { Wrap = WrapMode.Word, Text = "The TAS should be paused here." });
layout.Add(new Label { Wrap = WrapMode.Word, Text = "2. Click the Get Game Info button." });
layout.Add(new Label { Wrap = WrapMode.Word, Text = "3. Define a checkpoint at every turn or branching point of the path you want to TAS. Checkpoints are further explained later." });
Expand Down
2 changes: 1 addition & 1 deletion Studio/CelesteStudio/Tool/JadderlineForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public JadderlineForm() {
};
moveOnly = new CheckBox { Width = rowWidth };
additionalInputs = new TextBox { Width = rowWidth };
output = new TextArea { ReadOnly = true, Font = FontManager.EditorFontRegular, Width = 250 };
output = new TextArea { ReadOnly = true, Font = FontManager.EditorFont, Width = 250 };

var layout = new DynamicLayout { DefaultSpacing = new Size(10, 10) };
layout.BeginHorizontal();
Expand Down

0 comments on commit b419803

Please sign in to comment.