17 public static PresentationTransform Fit(
int bufferWidth,
int bufferHeight,
int adapterWidth,
int adapterHeight)
19 if (bufferWidth <= 0 || bufferHeight <= 0 || adapterWidth <= 0 || adapterHeight <= 0)
22 float scale = Math.Min((
float)adapterWidth / bufferWidth, (
float)adapterHeight / bufferHeight);
23 float width = bufferWidth * scale;
24 float height = bufferHeight * scale;
25 return new(scale, SKRect.Create((adapterWidth - width) / 2f, (adapterHeight - height) / 2f, width, height));
32 public bool TryAdapterPxToScreenPx(PointF adapterPx, out PointF screenPx)
35 ?
new((adapterPx.X - DestinationRect.Left) / Scale, (adapterPx.Y - DestinationRect.Top) / Scale)
37 return Scale > 0 && DestinationRect.Contains(adapterPx.X, adapterPx.Y);
43 public Rectangle ScreenRectToAdapterRect(Rectangle screenRect)
44 => Scale <= 0 ? Rectangle.Empty : Rectangle.FromLTRB(
45 (
int)Math.Floor(DestinationRect.Left + screenRect.Left * Scale),
46 (
int)Math.Floor(DestinationRect.Top + screenRect.Top * Scale),
47 (
int)Math.Ceiling(DestinationRect.Left + screenRect.Right * Scale),
48 (
int)Math.Ceiling(DestinationRect.Top + screenRect.Bottom * Scale));
50 internal static int ScaleDimension(
int dimension,
float renderScale)
52 double scaled = Math.Round(dimension * (
double)renderScale, MidpointRounding.AwayFromZero);
53 if (scaled >
int.MaxValue)
54 throw new ArgumentOutOfRangeException(nameof(renderScale),
"Scaled dimension exceeds the supported integer range.");
55 return Math.Max(1, (
int)scaled);