Skip to content

Commit

Permalink
refactor(Studio): Migrate font preview to Skia
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Oct 26, 2024
1 parent b906b77 commit b113991
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions Studio/CelesteStudio/Controls/SkiaDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected override void OnPaint(PaintEventArgs e)
}
catch (Exception ex)
{
Console.WriteLine(ex);
e.Graphics.DrawText(Fonts.Monospace(12.0f), Colors.Red, PointF.Empty, ex.ToString());
}
}
Expand Down
12 changes: 6 additions & 6 deletions Studio/CelesteStudio/Dialog/FontDialog.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CelesteStudio.Controls;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand All @@ -10,7 +11,7 @@
namespace CelesteStudio.Dialog;

public class FontDialog : Dialog<bool> {
private class FontPreview : Drawable {
private class FontPreview : SkiaDrawable {
private SKFont font = null!;
private SyntaxHighlighter? highlighter;

Expand All @@ -30,7 +31,9 @@ public FontPreview() {
Settings.ThemeChanged += () => BackgroundColor = Settings.Instance.Theme.Background;
}

protected override void OnPaint(PaintEventArgs e) {
protected override void Draw(PaintEventArgs e, SKSurface surface, SKImageInfo imageInfo) {
surface.Canvas.Clear();

if (highlighter == null)
return;

Expand All @@ -49,15 +52,12 @@ protected override void OnPaint(PaintEventArgs e) {
float yPos = 0.0f;
float maxWidth = 0.0f;
foreach (var line in previewText) {
// Actually measure the fonts to avoid caching issues
// highlighter.DrawLine(e.Graphics, 0.0f, yPos, line, new SyntaxHighlighter.DrawLineOptions { MeasureReal = true });
highlighter.DrawLine(surface.Canvas, 0.0f, yPos, line);
maxWidth = Math.Max(maxWidth, font.MeasureWidth(line));
yPos += font.LineHeight();
}

Size = new((int)maxWidth, (int)yPos);

base.OnPaint(e);
}
}

Expand Down
4 changes: 3 additions & 1 deletion Studio/CelesteStudio/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public static float CharWidth(this SKFont font) {
return width;
}

widthCache[font] = width = font.Metrics.AverageCharacterWidth * font.ScaleX;
font.MeasureText([font.GetGlyph('X')]);
//widthCache[font] = width = font.Metrics.AverageCharacterWidth * font.ScaleX;
widthCache[font] = width = font.MeasureText([font.GetGlyph('X')]);
return width;
}
public static float MeasureWidth(this SKFont font, string text) {
Expand Down

0 comments on commit b113991

Please sign in to comment.