223 lines
6.4 KiB
C#
223 lines
6.4 KiB
C#
namespace GraphTUI.Rendering;
|
|
|
|
public sealed class BrailleCanvas
|
|
{
|
|
private static readonly byte[,] DotMap = new byte[4, 2]
|
|
{
|
|
{ 0x01, 0x08 }, // row 0: dot 1, dot 4
|
|
{ 0x02, 0x10 }, // row 1: dot 2, dot 5
|
|
{ 0x04, 0x20 }, // row 2: dot 3, dot 6
|
|
{ 0x40, 0x80 } // row 3: dot 7, dot 8
|
|
};
|
|
|
|
public int CellWidth { get; private set; }
|
|
public int CellHeight { get; private set; }
|
|
|
|
public int SubpixelWidth => CellWidth * 2;
|
|
public int SubpixelHeight => CellHeight * 4;
|
|
|
|
private byte[] _dotMasks = [];
|
|
private ColorRgb[] _fgColors = [];
|
|
private ColorRgb[] _bgColors = [];
|
|
|
|
public BrailleCanvas(int cellWidth, int cellHeight)
|
|
{
|
|
Resize(cellWidth, cellHeight);
|
|
}
|
|
|
|
public void Resize(int cellWidth, int cellHeight)
|
|
{
|
|
cellWidth = System.Math.Max(1, cellWidth);
|
|
cellHeight = System.Math.Max(1, cellHeight);
|
|
|
|
if (CellWidth == cellWidth && CellHeight == cellHeight && _dotMasks != null)
|
|
return;
|
|
|
|
CellWidth = cellWidth;
|
|
CellHeight = cellHeight;
|
|
int total = cellWidth * cellHeight;
|
|
|
|
_dotMasks = new byte[total];
|
|
_fgColors = new ColorRgb[total];
|
|
_bgColors = new ColorRgb[total];
|
|
|
|
Clear(ColorRgb.Background);
|
|
}
|
|
|
|
public void Clear(ColorRgb bgColor)
|
|
{
|
|
Array.Clear(_dotMasks);
|
|
Array.Fill(_fgColors, ColorRgb.White);
|
|
Array.Fill(_bgColors, bgColor);
|
|
}
|
|
|
|
public void SetPixel(int subX, int subY, ColorRgb color)
|
|
{
|
|
if (subX < 0 || subX >= SubpixelWidth || subY < 0 || subY >= SubpixelHeight)
|
|
return;
|
|
|
|
int cellCol = subX / 2;
|
|
int cellRow = subY / 4;
|
|
int dotCol = subX % 2;
|
|
int dotRow = subY % 4;
|
|
|
|
int idx = cellRow * CellWidth + cellCol;
|
|
_dotMasks[idx] |= DotMap[dotRow, dotCol];
|
|
_fgColors[idx] = color;
|
|
}
|
|
|
|
public void SetCellBackground(int cellCol, int cellRow, ColorRgb color)
|
|
{
|
|
if (cellCol < 0 || cellCol >= CellWidth || cellRow < 0 || cellRow >= CellHeight)
|
|
return;
|
|
|
|
_bgColors[cellRow * CellWidth + cellCol] = color;
|
|
}
|
|
|
|
private const int CLIP_INSIDE = 0;
|
|
private const int CLIP_LEFT = 1;
|
|
private const int CLIP_RIGHT = 2;
|
|
private const int CLIP_BOTTOM = 4;
|
|
private const int CLIP_TOP = 8;
|
|
|
|
private static int ComputeOutCode(double x, double y, double xmin, double ymin, double xmax, double ymax)
|
|
{
|
|
int code = CLIP_INSIDE;
|
|
if (x < xmin) code |= CLIP_LEFT;
|
|
else if (x > xmax) code |= CLIP_RIGHT;
|
|
if (y < ymin) code |= CLIP_TOP;
|
|
else if (y > ymax) code |= CLIP_BOTTOM;
|
|
return code;
|
|
}
|
|
|
|
public void DrawLine(int x0Int, int y0Int, int x1Int, int y1Int, ColorRgb color)
|
|
{
|
|
double xmin = 0;
|
|
double ymin = 0;
|
|
double xmax = SubpixelWidth - 1;
|
|
double ymax = SubpixelHeight - 1;
|
|
|
|
double x0 = x0Int;
|
|
double y0 = y0Int;
|
|
double x1 = x1Int;
|
|
double y1 = y1Int;
|
|
|
|
int code0 = ComputeOutCode(x0, y0, xmin, ymin, xmax, ymax);
|
|
int code1 = ComputeOutCode(x1, y1, xmin, ymin, xmax, ymax);
|
|
bool accept = false;
|
|
|
|
for (int iter = 0; iter < 8; iter++)
|
|
{
|
|
if ((code0 | code1) == 0)
|
|
{
|
|
accept = true;
|
|
break;
|
|
}
|
|
if ((code0 & code1) != 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
int outcode = code0 != 0 ? code0 : code1;
|
|
double x = 0, y = 0;
|
|
|
|
if ((outcode & CLIP_TOP) != 0)
|
|
{
|
|
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
|
|
y = ymin;
|
|
}
|
|
else if ((outcode & CLIP_BOTTOM) != 0)
|
|
{
|
|
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
|
|
y = ymax;
|
|
}
|
|
else if ((outcode & CLIP_RIGHT) != 0)
|
|
{
|
|
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
|
|
x = xmax;
|
|
}
|
|
else if ((outcode & CLIP_LEFT) != 0)
|
|
{
|
|
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
|
|
x = xmin;
|
|
}
|
|
|
|
if (outcode == code0)
|
|
{
|
|
x0 = x;
|
|
y0 = y;
|
|
code0 = ComputeOutCode(x0, y0, xmin, ymin, xmax, ymax);
|
|
}
|
|
else
|
|
{
|
|
x1 = x;
|
|
y1 = y;
|
|
code1 = ComputeOutCode(x1, y1, xmin, ymin, xmax, ymax);
|
|
}
|
|
}
|
|
|
|
if (!accept)
|
|
{
|
|
accept = (code0 | code1) == 0;
|
|
}
|
|
|
|
if (!accept) return;
|
|
|
|
int cx0 = System.Math.Clamp((int)System.Math.Round(x0), 0, SubpixelWidth - 1);
|
|
int cy0 = System.Math.Clamp((int)System.Math.Round(y0), 0, SubpixelHeight - 1);
|
|
int cx1 = System.Math.Clamp((int)System.Math.Round(x1), 0, SubpixelWidth - 1);
|
|
int cy1 = System.Math.Clamp((int)System.Math.Round(y1), 0, SubpixelHeight - 1);
|
|
|
|
int dx = System.Math.Abs(cx1 - cx0);
|
|
int dy = System.Math.Abs(cy1 - cy0);
|
|
int sx = cx0 < cx1 ? 1 : -1;
|
|
int sy = cy0 < cy1 ? 1 : -1;
|
|
int err = dx - dy;
|
|
|
|
while (true)
|
|
{
|
|
SetPixel(cx0, cy0, color);
|
|
|
|
if (cx0 == cx1 && cy0 == cy1)
|
|
break;
|
|
|
|
int e2 = 2 * err;
|
|
if (e2 > -dy)
|
|
{
|
|
err -= dy;
|
|
cx0 += sx;
|
|
}
|
|
if (e2 < dx)
|
|
{
|
|
err += dx;
|
|
cy0 += sy;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DrawVerticalFill(int subX, int subY0, int subY1, ColorRgb color)
|
|
{
|
|
if (subX < 0 || subX >= SubpixelWidth) return;
|
|
|
|
int startY = System.Math.Clamp(System.Math.Min(subY0, subY1), 0, SubpixelHeight - 1);
|
|
int endY = System.Math.Clamp(System.Math.Max(subY0, subY1), 0, SubpixelHeight - 1);
|
|
|
|
// For shaded areas, sample every 2nd or all subpixels
|
|
for (int y = startY; y <= endY; y += 2)
|
|
{
|
|
SetPixel(subX, y, color);
|
|
}
|
|
}
|
|
|
|
public (char Ch, ColorRgb Fg, ColorRgb Bg) GetCell(int cellCol, int cellRow)
|
|
{
|
|
if (cellCol < 0 || cellCol >= CellWidth || cellRow < 0 || cellRow >= CellHeight)
|
|
return (' ', ColorRgb.White, ColorRgb.Background);
|
|
|
|
int idx = cellRow * CellWidth + cellCol;
|
|
byte mask = _dotMasks[idx];
|
|
|
|
char ch = mask == 0 ? ' ' : (char)(0x2800 | mask);
|
|
return (ch, _fgColors[idx], _bgColors[idx]);
|
|
}
|
|
}
|