Skip to content

Commit

Permalink
+ more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jinek committed Dec 15, 2024
1 parent e6eea31 commit 293f7c3
Show file tree
Hide file tree
Showing 23 changed files with 152 additions and 222 deletions.
2 changes: 1 addition & 1 deletion src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ resharper_class_can_be_sealed_global_highlighting = none
resharper_class_can_be_sealed_local_highlighting = none
resharper_class_never_instantiated_global_highlighting = suggestion
resharper_class_never_instantiated_local_highlighting = suggestion
resharper_class_with_virtual_members_never_inherited_global_highlighting = suggestion
resharper_class_with_virtual_members_never_inherited_global_highlighting = hint
resharper_class_with_virtual_members_never_inherited_local_highlighting = suggestion
resharper_clear_attribute_is_obsolete_all_highlighting = warning
resharper_clear_attribute_is_obsolete_highlighting = warning
Expand Down
136 changes: 60 additions & 76 deletions src/Consolonia.Core/Drawing/ConsoloniaRenderInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,35 @@ public IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, IGe
throw new ArgumentException("Only StreamGeometryImpl is supported");

IStreamGeometryImpl newGeometry = CreateStreamGeometry();
using (IStreamGeometryContextImpl ctx = newGeometry.Open())
{
// Resharper disable UnusedVariable
bool hasLeftStroke = stream2.Bounds.X.IsNearlyEqual(1);
bool hasTopStroke = stream2.Bounds.Y.IsNearlyEqual(1);
bool hasRightStroke = (stream1.Bounds.Width - stream2.Bounds.Width).IsNearlyEqual(stream2.Bounds.X + 1);
bool hasBottomStroke =
(stream1.Bounds.Height - stream2.Bounds.Height).IsNearlyEqual(stream2.Bounds.Y + 1);
Point topLeft = stream1.Bounds.TopLeft;
Point topRight = stream1.Bounds.TopRight;
Point bottomLeft = stream1.Bounds.BottomLeft;
Point bottomRight = stream1.Bounds.BottomRight;
Line topStroke = stream1.Strokes[0];
Line rightStroke = stream1.Strokes[1];
Line bottomStroke = stream1.Strokes[2];
Line leftStroke = stream1.Strokes[3];
// Resharper enable UnusedVariable

// add "null" strokes to establish boundries of box even when there is a single real stroke.
AddStroke(ctx, topLeft, topLeft);
AddStroke(ctx, bottomRight, bottomRight);

if (hasTopStroke)
AddStroke(ctx, topStroke.PStart, topStroke.PEnd + new Vector(-1, 0));
if (hasRightStroke)
AddStroke(ctx, rightStroke.PStart + new Vector(-1, 0), rightStroke.PEnd + new Vector(-1, -1));
if (hasBottomStroke)
AddStroke(ctx, bottomStroke.PStart + new Vector(0, -1), bottomStroke.PEnd + new Vector(-1, -1));
if (hasLeftStroke)
AddStroke(ctx, leftStroke.PStart, leftStroke.PEnd + new Vector(0, -1));
}
using IStreamGeometryContextImpl ctx = newGeometry.Open();
// Resharper disable UnusedVariable
bool hasLeftStroke = stream2.Bounds.X.IsNearlyEqual(1);
bool hasTopStroke = stream2.Bounds.Y.IsNearlyEqual(1);
bool hasRightStroke = (stream1.Bounds.Width - stream2.Bounds.Width).IsNearlyEqual(stream2.Bounds.X + 1);
bool hasBottomStroke =
(stream1.Bounds.Height - stream2.Bounds.Height).IsNearlyEqual(stream2.Bounds.Y + 1);
Point topLeft = stream1.Bounds.TopLeft;
Point topRight = stream1.Bounds.TopRight;
Point bottomLeft = stream1.Bounds.BottomLeft;
Point bottomRight = stream1.Bounds.BottomRight;
Line topStroke = stream1.Strokes[0];
Line rightStroke = stream1.Strokes[1];
Line bottomStroke = stream1.Strokes[2];
Line leftStroke = stream1.Strokes[3];
// Resharper enable UnusedVariable

// add "null" strokes to establish boundaries of box even when there is a single real stroke.
AddStroke(ctx, topLeft, topLeft);
AddStroke(ctx, bottomRight, bottomRight);

if (hasTopStroke)
AddStroke(ctx, topStroke.PStart, topStroke.PEnd + new Vector(-1, 0));
if (hasRightStroke)
AddStroke(ctx, rightStroke.PStart + new Vector(-1, 0), rightStroke.PEnd + new Vector(-1, -1));
if (hasBottomStroke)
AddStroke(ctx, bottomStroke.PStart + new Vector(0, -1), bottomStroke.PEnd + new Vector(-1, -1));
if (hasLeftStroke)
AddStroke(ctx, leftStroke.PStart, leftStroke.PEnd + new Vector(0, -1));

return newGeometry;
}
Expand Down Expand Up @@ -112,10 +110,8 @@ public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, Pi

public IBitmapImpl LoadBitmap(string fileName)
{
using (FileStream stream = File.OpenRead(fileName))
{
return new BitmapImpl(stream);
}
using FileStream stream = File.OpenRead(fileName);
return new BitmapImpl(stream);
}

public IBitmapImpl LoadBitmap(Stream stream)
Expand All @@ -126,68 +122,56 @@ public IBitmapImpl LoadBitmap(Stream stream)
public IWriteableBitmapImpl LoadWriteableBitmapToHeight(Stream stream, int height,
BitmapInterpolationMode interpolationMode)
{
using (var skStream = new SKManagedStream(stream))
{
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int width = (int)(height * ((double)originalBitmap.Width / originalBitmap.Height));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}
using var skStream = new SKManagedStream(stream);
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int width = (int)(height * ((double)originalBitmap.Width / originalBitmap.Height));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}

public IWriteableBitmapImpl LoadWriteableBitmapToWidth(Stream stream, int width,
BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
using (var skStream = new SKManagedStream(stream))
{
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int height = (int)(width * ((double)originalBitmap.Height / originalBitmap.Width));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}
using var skStream = new SKManagedStream(stream);
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int height = (int)(width * ((double)originalBitmap.Height / originalBitmap.Width));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}

public IWriteableBitmapImpl LoadWriteableBitmap(string fileName)
{
using (FileStream stream = File.OpenRead(fileName))
{
return LoadWriteableBitmap(stream);
}
using FileStream stream = File.OpenRead(fileName);
return LoadWriteableBitmap(stream);
}

public IWriteableBitmapImpl LoadWriteableBitmap(Stream stream)
{
using (var skStream = new SKManagedStream(stream))
{
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
return new BitmapImpl(originalBitmap);
}
using var skStream = new SKManagedStream(stream);
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
return new BitmapImpl(originalBitmap);
}

public IBitmapImpl LoadBitmapToWidth(Stream stream, int width, BitmapInterpolationMode interpolationMode)
{
using (var skStream = new SKManagedStream(stream))
{
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int height = (int)(width * ((double)originalBitmap.Height / originalBitmap.Width));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}
using var skStream = new SKManagedStream(stream);
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int height = (int)(width * ((double)originalBitmap.Height / originalBitmap.Width));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}

public IBitmapImpl LoadBitmapToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode)
{
using (var skStream = new SKManagedStream(stream))
{
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int width = (int)(height * ((double)originalBitmap.Width / originalBitmap.Height));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}
using var skStream = new SKManagedStream(stream);
SKBitmap originalBitmap = SKBitmap.Decode(skStream);
int width = (int)(height * ((double)originalBitmap.Width / originalBitmap.Height));
SKBitmap resizedBitmap =
originalBitmap.Resize(new SKImageInfo(width, height), (SKFilterQuality)interpolationMode);
return new BitmapImpl(resizedBitmap);
}

public IBitmapImpl ResizeBitmap(IBitmapImpl bitmapImpl, PixelSize destinationSize,
Expand Down
43 changes: 22 additions & 21 deletions src/Consolonia.Core/Drawing/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ internal class DrawingContextImpl : IDrawingContextImpl

// top left, top right, bottom right, bottom left,
private static readonly char[][] CornerChars =
{
[
// LineStyle=Edge we don't draw chars for edge corners
[' ', ' ', ' ', ' '],
// LineStyle=EdgeWide
['▗', '▖', '▘', '▝']
};
];

private readonly Stack<Rect> _clipStack = new(100);
private readonly ConsoleWindow _consoleWindow;
Expand Down Expand Up @@ -110,7 +110,7 @@ public void DrawBitmap(IBitmapImpl source, double opacity, Rect sourceRect, Rect
bitmap.GetPixel(x, y + 1), bitmap.GetPixel(x + 1, y + 1)
};

// map it to a single char to represet the 4 pixels
// map it to a single char to represent the 4 pixels
char quadPixel = GetQuadPixelCharacter(quadColors);

// get the combined colors for the quad pixel
Expand Down Expand Up @@ -160,15 +160,14 @@ public void DrawGeometry(IBrush brush, IPen pen, IGeometryImpl geometry)

var color = (Color)extractColorCheckPlatformSupported;

if (lineStyle == null)
lineStyle = LineStyle.SingleLine;
lineStyle ??= LineStyle.SingleLine;

var strokePostions = InferStrokePositions(streamGeometry);
var strokePositions = InferStrokePositions(streamGeometry);

bool hasTop = strokePostions.Contains(RectangleLinePosition.Top);
bool hasRight = strokePostions.Contains(RectangleLinePosition.Right);
bool hasBottom = strokePostions.Contains(RectangleLinePosition.Bottom);
bool hasLeft = strokePostions.Contains(RectangleLinePosition.Left);
bool hasTop = strokePositions.Contains(RectangleLinePosition.Top);
bool hasRight = strokePositions.Contains(RectangleLinePosition.Right);
bool hasBottom = strokePositions.Contains(RectangleLinePosition.Bottom);
bool hasLeft = strokePositions.Contains(RectangleLinePosition.Left);

if (lineStyle == LineStyle.Edge || lineStyle == LineStyle.EdgeWide)
{
Expand All @@ -179,10 +178,10 @@ public void DrawGeometry(IBrush brush, IPen pen, IGeometryImpl geometry)
if (stroke.Bounds.Width > 0 || stroke.Bounds.Height > 0)
{
if (stroke.Vertical)
DrawEdgeLine(stroke, strokePostions[iStroke], lineStyle.Value, color, hasTop,
DrawEdgeLine(stroke, strokePositions[iStroke], lineStyle.Value, color, hasTop,
hasBottom);
else
DrawEdgeLine(stroke, strokePostions[iStroke], lineStyle.Value, color, hasLeft,
DrawEdgeLine(stroke, strokePositions[iStroke], lineStyle.Value, color, hasLeft,
hasRight);
}
}
Expand All @@ -196,7 +195,7 @@ public void DrawGeometry(IBrush brush, IPen pen, IGeometryImpl geometry)
for (int iStroke = 0; iStroke < streamGeometry.Strokes.Count; iStroke++)
{
Line stroke = streamGeometry.Strokes[iStroke];
RectangleLinePosition strokePosition = strokePostions[iStroke];
RectangleLinePosition strokePosition = strokePositions[iStroke];
if (strokePosition == RectangleLinePosition.Left)
strokeLeft = stroke;
else if (strokePosition == RectangleLinePosition.Right)
Expand Down Expand Up @@ -411,7 +410,7 @@ private static RectangleLinePosition[] InferStrokePositions(StreamGeometryImpl s
for (int i = 0; i < streamGeometry.Strokes.Count; i++)
{
Line stroke = streamGeometry.Strokes[i];
if (stroke.Bounds.Width == 0 && stroke.Bounds.Height == 0)
if (stroke.Bounds is { Width: 0, Height: 0 })
{
// ignore zero length strokes
strokePositions[i] = RectangleLinePosition.Unknown;
Expand Down Expand Up @@ -547,7 +546,7 @@ private void DrawBoxLineInternal(IPen pen, Line line, RectangleLinePosition line
if (lineStyle == null || linePosition == RectangleLinePosition.Unknown)
lineStyle = LineStyle.SingleLine;

if (lineStyle == LineStyle.Edge || lineStyle == LineStyle.EdgeWide)
if (lineStyle is LineStyle.Edge or LineStyle.EdgeWide)
{
DrawEdgeLine(line, linePosition, lineStyle.Value, color, true, true);
}
Expand Down Expand Up @@ -670,7 +669,7 @@ private Line TransformLineInternal(Line line)
}

/// <summary>
/// Draw pixels for a line with linestyle and a pattern
/// Draw pixels for a line with line style and a pattern
/// </summary>
/// <param name="head">the current caret position</param>
/// <param name="line">line to render</param>
Expand Down Expand Up @@ -732,7 +731,7 @@ private void DrawLineSymbolAndMoveHead(ref Point head, bool isVertical, ISymbol

// Each glyph maps to a pixel as a starting point.
// Emoji's and Ligatures are complex strings, so they start at a point and then overlap following pixels
// the x and y are adjusted accodingly.
// the x and y are adjusted accordingly.
foreach (string glyph in text.GetGlyphs(_consoleWindow.Console.SupportsComplexEmoji))
{
Point characterPoint =
Expand Down Expand Up @@ -840,6 +839,7 @@ private static char GetQuadPixelCharacter(params SKColor[] colors)
{
char character = GetColorsPattern(colors) switch
{
// ReSharper disable StringLiteralTypo
"FFFF" => ' ',
"TFFF" => '▘',
"FTFF" => '▝',
Expand All @@ -856,6 +856,7 @@ private static char GetQuadPixelCharacter(params SKColor[] colors)
"TFTT" => '▙',
"FTTT" => '▟',
"TTTT" => '█',
// ReSharper restore StringLiteralTypo
_ => throw new NotImplementedException()
};
return character;
Expand Down Expand Up @@ -974,7 +975,7 @@ private static string GetColorsPattern(SKColor[] colors)
if (colors.Length != 4) throw new ArgumentException("Array must contain exactly 4 colors.");

// Initial guess: two clusters with the first two colors as centers
SKColor[] clusterCenters = { colors[0], colors[1] };
SKColor[] clusterCenters = [colors[0], colors[1]];
int[] clusters = new int[colors.Length];

for (int iteration = 0; iteration < 10; iteration++) // limit iterations to avoid infinite loop
Expand All @@ -989,9 +990,9 @@ private static string GetColorsPattern(SKColor[] colors)
var clusteredColors = colors.Where((_, i) => clusters[i] == cluster).ToList();
if (clusteredColors.Any())
newClusterCenters[cluster] = GetAverageColor(clusteredColors);
if (clusteredColors.Count == 4)
if (clusteredColors.All(c => c.Alpha == 0))
return "FFFF";
if (clusteredColors.Count != 4) continue;
if (clusteredColors.All(c => c.Alpha == 0))
return "FFFF";
// return "TTTT";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public class ColorConverter : JsonConverter<Color>
public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue,
JsonSerializer serializer)
{
if (Color.TryParse(reader.Value!.ToString(), out Color color))
return color;
return Colors.Transparent;
return Color.TryParse(reader.Value!.ToString(), out Color color) ? color : Colors.Transparent;
}

public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ISymbol Blend(ref ISymbol symbolAbove)
/// </summary>
private static char GetBoxSymbol(byte upRightDownLeft)
{
//DOS linedraw characters are not ordered in any programmatic manner, and calculating a particular character shape needs to use a look-up table. from https://en.wikipedia.org/wiki/Box-drawing_character
//DOS line draw characters are not ordered in any programmatic manner, and calculating a particular character shape needs to use a look-up table. from https://en.wikipedia.org/wiki/Box-drawing_character

byte leftPart = (byte)(upRightDownLeft & 0b1111_0000);
bool hasLeftPart = leftPart > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public bool Equals(Pixel other)
}

/// <summary>
/// Blend the pixelAbove with the this pixel.
/// Blend the pixelAbove with this pixel.
/// </summary>
/// <param name="pixelAbove"></param>
/// <returns></returns>
Expand Down Expand Up @@ -172,7 +172,7 @@ private static Color MergeColors(Color target, Color source)

public override bool Equals([NotNullWhen(true)] object obj)
{
return obj is Pixel && Equals((Pixel)obj);
return obj is Pixel pixel && Equals(pixel);
}

public override int GetHashCode()
Expand Down
Loading

0 comments on commit 293f7c3

Please sign in to comment.