Skip to content

Commit

Permalink
refactor(Studio): Drop Eto.SkiaDraw dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Oct 26, 2024
1 parent 9525e4a commit 27cdbc5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Studio/CelesteStudio.GTK/CelesteStudio.GTK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
<PackageReference Include="Eto.Platform.Gtk" Version="2.8.4-ci-20240710.9879198421" />
<!-- <PackageReference Include="Eto.Platform.Gtk" Version="2.8.3"/>-->

<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.80.3" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.8" />

<PackageReference Include="Tracy.NET" Version="0.11.1.7" Enalbed="true" ProfileAllMethods="true"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Studio/CelesteStudio.Mac/CelesteStudio.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<!-- Use an CI-build, since it contains a fix for using context menu hotkeys inside the editor -->
<PackageReference Include="Eto.Platform.Mac64" Version="2.8.4-ci-20240710.9879198421" />

<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.80.3" />
<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.88.8" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Studio/CelesteStudio.WPF/CelesteStudio.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="Eto.Platform.Wpf" Version="2.8.3"/>
<PackageReference Include="DarkNet" Version="2.3.0" />

<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.80.3" />
<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.88.8" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion Studio/CelesteStudio/CelesteStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

<ItemGroup>
<PackageReference Include="Eto.Forms" Version="2.8.3"/>
<PackageReference Include="Eto.SkiaDraw" Version="0.2.0"/>

<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="Samboy063.Tomlet" Version="5.3.1"/>
<PackageReference Include="Markdig" Version="0.37.0"/>
<PackageReference Include="Tracy.NET" Version="0.11.1.7" Enalbed="true" ProfileAllMethods="true"/>

<ProjectReference Include="..\..\StudioCommunication\StudioCommunication.csproj"/>
<ProjectReference Include="..\..\external\Featherline\Featherline.csproj"/>
Expand Down
43 changes: 43 additions & 0 deletions Studio/CelesteStudio/Controls/SkiaDrawable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Eto;
using Eto.Drawing;
using Eto.Forms;
using SkiaSharp;
using System;

namespace CelesteStudio.Controls;

public abstract class SkiaDrawable : Drawable {
private readonly SKColorType colorType = Platform.Instance.IsWinForms || Platform.Instance.IsWpf ? SKColorType.Bgra8888 : SKColorType.Rgba8888;

private Bitmap? image = null;
private SKImageInfo imageInfo = SKImageInfo.Empty;

protected abstract void Draw(PaintEventArgs e, SKSurface surface, SKImageInfo info);

protected override void OnPaint(PaintEventArgs e)
{
try {
if (Width <= 0 || Height <= 0) {
return;
}

if (Size != image?.Size)
{
image?.Dispose();
image = new Bitmap(Size, PixelFormat.Format32bppRgba);
imageInfo = new SKImageInfo(Width, Height, colorType, SKAlphaType.Unpremul);
}

using var bmp = image.Lock();
using var surface = SKSurface.Create(imageInfo, bmp.Data, bmp.ScanWidth);

Draw(e, surface, imageInfo);

e.Graphics.DrawImage(image, PointF.Empty);
}
catch (Exception ex)
{
e.Graphics.DrawText(Fonts.Monospace(12.0f), Colors.Red, PointF.Empty, ex.ToString());
}
}
}
19 changes: 11 additions & 8 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
using System.Threading;
using System.Threading.Tasks;
using CelesteStudio.Communication;
using CelesteStudio.Controls;
using CelesteStudio.Data;
using CelesteStudio.Dialog;
using CelesteStudio.Editing.ContextActions;
using CelesteStudio.Util;
using Eto.Drawing;
using Eto.Forms;
using Eto.SkiaDraw;
using SkiaSharp;
using StudioCommunication;
using StudioCommunication.Util;
Expand Down Expand Up @@ -3620,10 +3620,13 @@ private void UpdateMouseCursor(PointF location, Keys modifiers) {

#region Drawing

protected override void OnPaint(SKPaintEventArgs e) {
var canvas = e.Surface.Canvas;
protected override void Draw(PaintEventArgs e, SKSurface surface, SKImageInfo imageInfo) {
var canvas = surface.Canvas;
canvas.Clear();

// Adjust unit to points instead of pixels
//canvas.Scale(e.Graphics.PixelsPerPoint);

// To be reused below. Kinda annoying how C# handles out parameter conflicts
WrapEntry wrap;

Expand Down Expand Up @@ -3740,7 +3743,7 @@ protected override void OnPaint(SKPaintEventArgs e) {
suffixPaint.IsAntialias = true;
canvas.DrawText(CommunicationWrapper.CurrentLineSuffix,
x: scrollablePosition.X + scrollableSize.Width - suffixWidth - padding,
y: actualToVisualRows[CommunicationWrapper.CurrentLine] * font.LineHeight() - Font.Metrics.Ascent,
y: actualToVisualRows[CommunicationWrapper.CurrentLine] * font.LineHeight()+ Font.Offset(),
suffixPaint);
}

Expand Down Expand Up @@ -3829,7 +3832,7 @@ protected override void OnPaint(SKPaintEventArgs e) {
float h = Font.LineHeight();

canvas.DrawRoundRect(x, y, w, h, 4.0f, 4.0f, calcBgPaint);
canvas.DrawText(calculateLine, x + padding, y - Font.Metrics.Ascent, Font, calcFgPaint);
canvas.DrawText(calculateLine, x + padding, y+ Font.Offset(), Font, calcFgPaint);
}

// Draw line numbers
Expand Down Expand Up @@ -3907,10 +3910,10 @@ protected override void OnPaint(SKPaintEventArgs e) {
: Settings.Instance.Theme.LineNumber.ToSkia();

if (Settings.Instance.LineNumberAlignment == LineNumberAlignment.Left) {
canvas.DrawText(numberString, scrollablePosition.X + LineNumberPadding, yPos - Font.Metrics.Ascent, Font, textPaint);
canvas.DrawText(numberString, scrollablePosition.X + LineNumberPadding, yPos+ Font.Offset(), Font, textPaint);
} else if (Settings.Instance.LineNumberAlignment == LineNumberAlignment.Right) {
float ident = Font.CharWidth() * (Document.Lines.Count.Digits() - (row + 1).Digits());
canvas.DrawText(numberString, scrollablePosition.X + LineNumberPadding + ident, yPos - Font.Metrics.Ascent, Font, textPaint);
canvas.DrawText(numberString, scrollablePosition.X + LineNumberPadding + ident, yPos+ Font.Offset(), Font, textPaint);
}

bool collapsed = false;
Expand Down Expand Up @@ -3974,7 +3977,7 @@ protected override void OnPaint(SKPaintEventArgs e) {
popupFgPaint.IsAntialias = true;
foreach (var line in lines) {
// TODO: Use PopupFont
canvas.DrawText(line, x, y - Font.Metrics.Ascent, Font, popupFgPaint);
canvas.DrawText(line, x, y+ Font.Offset(), Font, popupFgPaint);
y += Font.LineHeight();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Studio/CelesteStudio/Editing/SyntaxHighlighter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void DrawLine(SKCanvas canvas, float x, float y, string line, DrawLineOpt
textPaint.Color = style.ForegroundColor.ToSkia();
// textPaint.Style = SKPaintStyle.Fill;
textPaint.IsAntialias = true;
canvas.DrawText(str, x + xOff, y - font.Metrics.Ascent, font, textPaint);
textPaint.TextAlign = SKTextAlign.Left;
canvas.DrawText(str, x + xOff, y + font.Offset(), font, textPaint);
// }

xOff += width;
Expand Down
20 changes: 14 additions & 6 deletions Studio/CelesteStudio/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class FontManager {
public static Font StatusFont => statusFont ??= CreateStatus();
public static Font PopupFont => popupFont ??= CreatePopup();

public static SKFont SKEditorFontRegular => skEditorFontRegular ??= CreateSKFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize * Settings.Instance.FontZoom);
public static SKFont SKEditorFontRegular => skEditorFontRegular ??= CreateSKFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize * Settings.Instance.FontZoom * (4.0f/3.0f));

private static FontFamily? builtinFontFamily;
public static Font CreateFont(string fontFamily, float size, FontStyle style = FontStyle.None) {
Expand Down Expand Up @@ -55,9 +55,13 @@ public static SKFont CreateSKFont(string fontFamily, float size) {

if (fontFamily == FontFamilyBuiltin) {
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("JetBrainsMono/JetBrainsMono-Regular");
return new SKFont(SKTypeface.FromStream(stream), size);
var typeface = SKTypeface.FromStream(stream);

return new SKFont(typeface, size) { LinearMetrics = true };
} else {
return new SKFont(SKTypeface.FromFamilyName(fontFamily), size);
var typeface = SKTypeface.FromFamilyName(fontFamily, SKFontStyleWeight.Light, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright);

return new SKFont(typeface, size) { LinearMetrics = true };
}
}

Expand Down Expand Up @@ -93,14 +97,18 @@ public static float CharWidth(this SKFont font) {
return width;
}

widthCache[font] = font.Metrics.AverageCharacterWidth;
return font.Metrics.AverageCharacterWidth;
widthCache[font] = width = font.Metrics.AverageCharacterWidth * font.ScaleX;
return width;
}
public static float MeasureWidth(this SKFont font, string text) {
return font.CharWidth() * text.Length;
}
// Apply +/- 1.0f for better visuals
public static float LineHeight(this SKFont font) {
return font.Spacing;
return font.Spacing + 0.6f;
}
public static float Offset(this SKFont font) {
return -font.Metrics.Ascent + 0.7f;
}

public static void OnFontChanged() {
Expand Down

0 comments on commit 27cdbc5

Please sign in to comment.