Skip to content

Commit

Permalink
テキストの描画品質を改善
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Feb 7, 2024
1 parent 73b0a59 commit 08b8bd4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/Beutl.Engine/Graphics/BrushConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public void ConfigurePaint(SKPaint paint)
float opacity = (Brush?.Opacity ?? 0) / 100f;
paint.IsAntialias = true;
paint.BlendMode = (SKBlendMode)BlendMode;
paint.HintingLevel = SKPaintHinting.Full;
paint.LcdRenderText = true;
paint.SubpixelText = true;

paint.Color = new SKColor(255, 255, 255, (byte)(255 * opacity));

Expand Down
12 changes: 5 additions & 7 deletions src/Beutl.Engine/Graphics/ImmediateCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,22 +379,20 @@ public void DrawText(FormattedText text, IBrush? fill, IPen? pen)
{
VerifyAccess();

var typeface = new Typeface(text.Font, text.Style, text.Weight);
SKTypeface sktypeface = typeface.ToSkia();
_sharedFillPaint.Reset();
_sharedFillPaint.TextSize = text.Size;
_sharedFillPaint.Typeface = sktypeface;
using SKFont font = text.ToSKFont();

using var shaper = new SKShaper(sktypeface);
using var shaper = new SKShaper(font.Typeface);
using var buffer = new HarfBuzzSharp.Buffer();
buffer.AddUtf16(text.Text.AsSpan());
buffer.GuessSegmentProperties();

_sharedFillPaint.Reset();
_sharedFillPaint.TextSize = text.Size;
_sharedFillPaint.Typeface = font.Typeface;
SKShaper.Result result = shaper.Shape(buffer, _sharedFillPaint);

// create the text blob
using var builder = new SKTextBlobBuilder();
using SKFont font = _sharedFillPaint.ToFont();
SKPositionedRunBuffer run = builder.AllocatePositionedRun(font, result.Codepoints.Length);

// copy the glyphs
Expand Down
14 changes: 14 additions & 0 deletions src/Beutl.Engine/Media/Font/FontExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Beutl.Graphics;
using Beutl.Media.TextFormatting;

using SkiaSharp;

Expand All @@ -11,6 +12,19 @@ public static SKTypeface ToSkia(this Typeface typeface)
return FontManager.Instance._fonts[typeface.FontFamily].Get(typeface);
}

public static SKFont ToSKFont(this FormattedText text)
{
var typeface = new Typeface(text.Font, text.Style, text.Weight);
var font = new SKFont(typeface.ToSkia(), text.Size)
{
Edging = SKFontEdging.Antialias,
Subpixel = true,
Hinting = SKFontHinting.Full
};

return font;
}

public static Typeface ToTypeface(this SKTypeface typeface)
{
return new Typeface(new FontFamily(typeface.FamilyName), typeface.FontSlant.ToFontStyle(), (FontWeight)typeface.FontWeight);
Expand Down
31 changes: 15 additions & 16 deletions src/Beutl.Engine/Media/TextFormatting/FormattedText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,22 @@ public StringSpan Text

internal Point AddToSKPath(SKPath path, Point point)
{
SKTypeface typeface = new Typeface(Font, Style, Weight).ToSkia();
using SKPaint paint = new()
{
TextSize = Size,
Typeface = typeface
};
using SKFont font = this.ToSKFont();

using var shaper = new SKShaper(typeface);
using var shaper = new SKShaper(font.Typeface);
using var buffer = new HarfBuzzSharp.Buffer();
buffer.AddUtf16(Text.AsSpan());
buffer.GuessSegmentProperties();

using SKPaint paint = new()
{
TextSize = Size,
Typeface = font.Typeface
};
SKShaper.Result result = shaper.Shape(buffer, paint);

// create the text blob
using var builder = new SKTextBlobBuilder();
using SKFont font = paint.ToFont();
SKPositionedRunBuffer run = builder.AllocatePositionedRun(font, result.Codepoints.Length);

// copy the glyphs
Expand Down Expand Up @@ -130,21 +129,21 @@ internal Point AddToSKPath(SKPath path, Point point)

private (FontMetrics, Size) Measure()
{
SKTypeface typeface = new Typeface(Font, Style, Weight).ToSkia();
using SKPaint paint = new()
{
TextSize = Size,
Typeface = typeface
};
using SKFont font = this.ToSKFont();

using var shaper = new SKShaper(typeface);
using var shaper = new SKShaper(font.Typeface);
using var buffer = new HarfBuzzSharp.Buffer();
buffer.AddUtf16(Text.AsSpan());
buffer.GuessSegmentProperties();

using SKPaint paint = new()
{
TextSize = Size,
Typeface = font.Typeface
};
SKShaper.Result result = shaper.Shape(buffer, paint);

FontMetrics fontMetrics = paint.FontMetrics.ToFontMetrics();
FontMetrics fontMetrics = font.Metrics.ToFontMetrics();
float w = result.Width;
var size = new Size(
w + (buffer.Length - 1) * Spacing,
Expand Down

0 comments on commit 08b8bd4

Please sign in to comment.