Skip to content

Commit 713d31b

Browse files
committed
fix several obsolescence warnings
'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.' 'SKPaint.GetFontMetrics(out SKFontMetrics)' is obsolete: 'Use SKFont.GetFontMetrics() instead.' 'SKPaint.TextAlign' is obsolete: 'Use SKTextAlign method overloads instead.' 'SKCanvas.DrawText(string, float, float, SKPaint)' is obsolete: 'Use DrawText(string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.' 'CanvasExtensions.DrawShapedText(SKCanvas, SKShaper, string, float, float, SKPaint)' is obsolete: 'Use DrawShapedText(SKShaper shaper, string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.'
1 parent 156f152 commit 713d31b

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

Source/OxyPlot.Maui.Skia/SkiaRenderContext.cs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal class SkiaRenderContext : IRenderContext, IDisposable
1717
private readonly Dictionary<FontDescriptor, SKShaper> shaperCache = new();
1818
private readonly Dictionary<FontDescriptor, SKTypeface> typefaceCache = new();
1919
private SKPaint paint = new();
20+
private SKFont font = new();
2021
private SKPath path = new();
2122

2223
/// <summary>
@@ -364,14 +365,15 @@ public void DrawText(
364365
return;
365366
}
366367

367-
var paint = this.GetTextPaint(fontFamily, fontSize, fontWeight, out var shaper);
368+
var font = this.GetTextFont(fontFamily, fontSize, fontWeight, out var shaper);
369+
var paint = this.GetTextPaint();
368370
paint.Color = fill.ToSKColor();
369371

370372
var x = this.Convert(p.X);
371373
var y = this.Convert(p.Y);
372374

373375
var lines = StringHelper.SplitLines(text);
374-
var lineHeight = paint.GetFontMetrics(out var metrics);
376+
var lineHeight = font.GetFontMetrics(out var metrics);
375377

376378
var deltaY = verticalAlignment switch
377379
{
@@ -389,7 +391,7 @@ public void DrawText(
389391
{
390392
if (this.UseTextShaping)
391393
{
392-
var width = this.MeasureText(line, shaper, paint);
394+
var width = this.MeasureText(line, shaper, font);
393395
var deltaX = horizontalAlignment switch
394396
{
395397
HorizontalAlignment.Left => 0,
@@ -398,20 +400,19 @@ public void DrawText(
398400
_ => throw new ArgumentOutOfRangeException(nameof(horizontalAlignment))
399401
};
400402

401-
this.paint.TextAlign = SKTextAlign.Left;
402-
this.SkCanvas.DrawShapedText(shaper, line, deltaX, deltaY, paint);
403+
this.SkCanvas.DrawShapedText(shaper, line, deltaX, deltaY, SKTextAlign.Left, font, paint);
403404
}
404405
else
405406
{
406-
paint.TextAlign = horizontalAlignment switch
407+
var align = horizontalAlignment switch
407408
{
408409
HorizontalAlignment.Left => SKTextAlign.Left,
409410
HorizontalAlignment.Center => SKTextAlign.Center,
410411
HorizontalAlignment.Right => SKTextAlign.Right,
411412
_ => throw new ArgumentOutOfRangeException(nameof(horizontalAlignment))
412413
};
413414

414-
this.SkCanvas.DrawText(line, 0, deltaY, paint);
415+
this.SkCanvas.DrawText(line, 0, deltaY, align, font, paint);
415416
}
416417

417418
deltaY += lineHeight;
@@ -427,9 +428,10 @@ public OxySize MeasureText(string text, string fontFamily = null, double fontSiz
427428
}
428429

429430
var lines = StringHelper.SplitLines(text);
430-
var paint = this.GetTextPaint(fontFamily, fontSize, fontWeight, out var shaper);
431-
var height = paint.GetFontMetrics(out _) * lines.Length;
432-
var width = lines.Max(line => this.MeasureText(line, shaper, paint));
431+
var font = this.GetTextFont(fontFamily, fontSize, fontWeight, out var shaper);
432+
var paint = this.GetTextPaint();
433+
var height = font.GetFontMetrics(out _) * lines.Length;
434+
var width = lines.Max(line => this.MeasureText(line, shaper, font));
433435

434436
return new OxySize(this.ConvertBack(width), this.ConvertBack(height));
435437
}
@@ -833,17 +835,17 @@ private SKPaint GetStrokePaint(OxyColor strokeColor, double strokeThickness, Edg
833835
}
834836

835837
/// <summary>
836-
/// Gets a <see cref="SKPaint"/> containing information needed to render text.
838+
/// Gets a <see cref="SKFont"/> containing information needed to render text.
837839
/// </summary>
838840
/// <remarks>
839-
/// This modifies and returns the local <see cref="paint"/> instance.
841+
/// This modifies and returns the local <see cref="font"/> instance.
840842
/// </remarks>
841843
/// <param name="fontFamily">The font family.</param>
842844
/// <param name="fontSize">The font size.</param>
843845
/// <param name="fontWeight">The font weight.</param>
844846
/// <param name="shaper">The font shaper.</param>
845-
/// <returns>The paint.</returns>
846-
private SKPaint GetTextPaint(string fontFamily, double fontSize, double fontWeight, out SKShaper shaper)
847+
/// <returns>The font.</returns>
848+
private SKFont GetTextFont(string fontFamily, double fontSize, double fontWeight, out SKShaper shaper)
847849
{
848850
var fontDescriptor = new FontDescriptor(fontFamily, fontWeight);
849851
if (!this.typefaceCache.TryGetValue(fontDescriptor, out var typeface))
@@ -865,12 +867,24 @@ private SKPaint GetTextPaint(string fontFamily, double fontSize, double fontWeig
865867
shaper = null;
866868
}
867869

868-
this.paint.Typeface = typeface;
869-
this.paint.TextSize = this.Convert(fontSize);
870+
this.font.Typeface = typeface;
871+
this.font.Size = this.Convert(fontSize);
872+
this.font.Hinting = this.RendersToScreen ? SKFontHinting.Full : SKFontHinting.None;
873+
this.font.Subpixel = this.RendersToScreen;
874+
return this.font;
875+
}
876+
877+
/// <summary>
878+
/// Gets a <see cref="SKPaint"/> containing information needed to render text.
879+
/// </summary>
880+
/// <remarks>
881+
/// This modifies and returns the local <see cref="paint"/> instance.
882+
/// </remarks>
883+
/// <returns>The paint.</returns>
884+
private SKPaint GetTextPaint()
885+
{
870886
this.paint.IsAntialias = true;
871887
this.paint.Style = SKPaintStyle.Fill;
872-
this.paint.HintingLevel = this.RendersToScreen ? SKPaintHinting.Full : SKPaintHinting.NoHinting;
873-
this.paint.SubpixelText = this.RendersToScreen;
874888
return this.paint;
875889
}
876890

@@ -879,13 +893,13 @@ private SKPaint GetTextPaint(string fontFamily, double fontSize, double fontWeig
879893
/// </summary>
880894
/// <param name="text">The text to measure.</param>
881895
/// <param name="shaper">The text shaper.</param>
882-
/// <param name="paint">The paint.</param>
896+
/// <param name="font">The font.</param>
883897
/// <returns>The width of the text when rendered using the specified shaper and paint.</returns>
884-
private float MeasureText(string text, SKShaper shaper, SKPaint paint)
898+
private float MeasureText(string text, SKShaper shaper, SKFont font)
885899
{
886900
if (!this.UseTextShaping)
887901
{
888-
return paint.MeasureText(text);
902+
return font.MeasureText(text);
889903
}
890904

891905
// we have to get a bit creative here as SKShaper does not offer a direct overload for this.
@@ -907,8 +921,8 @@ private float MeasureText(string text, SKShaper shaper, SKPaint paint)
907921
}
908922

909923
buffer.GuessSegmentProperties();
910-
shaper.Shape(buffer, paint);
911-
return buffer.GlyphPositions.Sum(gp => gp.XAdvance) * paint.TextSize / 512;
924+
shaper.Shape(buffer, font);
925+
return buffer.GlyphPositions.Sum(gp => gp.XAdvance) * font.Size / 512;
912926
}
913927

914928
/// <summary>

0 commit comments

Comments
 (0)