Лаб. 6 ТАЯК
.docx
if (!string.IsNullOrEmpty(row.TextContent))
{
int textColor = row.Attributes.ContainsKey("textcolor") ? int.Parse(row.Attributes["textcolor"]) : 15;
int bgColor = row.Attributes.ContainsKey("bgcolor") ? int.Parse(row.Attributes["bgcolor"]) : 0;
string halign = row.Attributes.ContainsKey("halign") ? row.Attributes["halign"] : "left";
string valign = row.Attributes.ContainsKey("valign") ? row.Attributes["valign"] : "top";
RenderText(row.TextContent, startX, startY, width, height, halign, valign, textColor, bgColor);
}
foreach (var child in row.Children)
{
if (child.Name == "block")
{
RenderBlock(child, startX, startY, width, height);
}
else if (child.Name == "column")
{
RenderColumn(child, startX, startY, width, height);
}
}
}
private void RenderColumn(Tag column, int startX, int startY, int width, int height)
{
if (width <= 0 || height <= 0) return;
if (!string.IsNullOrEmpty(column.TextContent))
{
int textColor = column.Attributes.ContainsKey("textcolor") ? int.Parse(column.Attributes["textcolor"]) : 15;
int bgColor = column.Attributes.ContainsKey("bgcolor") ? int.Parse(column.Attributes["bgcolor"]) : 0;
string halign = column.Attributes.ContainsKey("halign") ? column.Attributes["halign"] : "left";
string valign = column.Attributes.ContainsKey("valign") ? column.Attributes["valign"] : "top";
RenderText(column.TextContent, startX, startY, width, height, halign, valign, textColor, bgColor);
}
foreach (var child in column.Children)
{
if (child.Name == "block")
{
RenderBlock(child, startX, startY, width, height);
}
else if (child.Name == "row")
{
RenderRow(child, startX, startY, width, height);
}
}
}
// Рендерит текстовое содержимое с учетом выравнивания
private void RenderText(string text, int startX, int startY, int width, int height, string halign, string valign, int textColorCode, int bgColorCode)
{
if (string.IsNullOrEmpty(text) || width <= 0 || height <= 0) return;
if (startX >= ConsoleWidth || startY >= ConsoleHeight) return;
ConsoleColor fgColor = ConsoleColorConverter.GetConsoleColor(textColorCode);
ConsoleColor bgColor = ConsoleColorConverter.GetConsoleColor(bgColorCode);
var lines = text.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 0) return;
int linesToRender = Math.Min(lines.Length, height);
int textStartY = startY;
if (valign == "center")
{
textStartY = startY + (height - linesToRender) / 2;
}
else if (valign == "bottom")
{
textStartY = startY + height - linesToRender;
}
textStartY = Math.Max(0, Math.Min(textStartY, ConsoleHeight - 1));
for (int i = 0; i < linesToRender && textStartY + i < ConsoleHeight; i++)
{
string line = lines[i];
if (string.IsNullOrEmpty(line)) continue;
if (line.Length > width)
line = line.Substring(0, width);
int textStartX = startX;
if (halign == "center")
{
textStartX = startX + (width - line.Length) / 2;
}
else if (halign == "right")
{
textStartX = startX + width - line.Length;
}
textStartX = Math.Max(0, Math.Min(textStartX, ConsoleWidth - 1));
for (int j = 0; j < line.Length && textStartX + j < ConsoleWidth; j++)
{
int x = textStartX + j;
int y = textStartY + i;
if (x >= 0 && x < ConsoleWidth && y >= 0 && y < ConsoleHeight)
{
screen[y, x] = new Cell(line[j], fgColor, bgColor);
}
}
}
for (int y = startY; y < startY + height && y < ConsoleHeight; y++)
{
for (int x = startX; x < startX + width && x < ConsoleWidth; x++)
{
if (screen[y, x] == null || screen[y, x].Character == ' ')
{
screen[y, x] = new Cell(' ', fgColor, bgColor);
}
}
}
}
// Отображение на консоли
private void DisplayScreen()
{
Console.Clear();
ConsoleColor currentFg = ConsoleColor.White;
ConsoleColor currentBg = ConsoleColor.Black;
for (int i = 0; i < ConsoleHeight; i++)
{
for (int j = 0; j < ConsoleWidth; j++)
{
var cell = screen[i, j];
if (cell == null)
{
cell = new Cell(' ', ConsoleColor.White, ConsoleColor.Black);
}
if (cell.Foreground != currentFg)
{
Console.ForegroundColor = cell.Foreground;
currentFg = cell.Foreground;
}
if (cell.Background != currentBg)
{
Console.BackgroundColor = cell.Background;
currentBg = cell.Background;
}
Console.Write(cell.Character);
}
Console.WriteLine();
}
Console.ResetColor();
}
}
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.Title = "eMark Processor";
string eMarkDoc = @"<block rows=3 columns=1>
<row height=3 halign=center textcolor=14 bgcolor=4 valign=center>
Заголовок eMark
</row>
<row height=15>
<block rows=1 columns=3>
<column width=20 bgcolor=1>Меню</column>
<column width=40 bgcolor=0 textcolor=14 halign=center valign=center>
Основной текст
</column>
<column bgcolor=6 textcolor=10 halign=right valign=bottom>
Доп. панель
</column>
</block>
</row>
<row halign=right bgcolor=3 textcolor=12 valign=bottom>
Страница 1/1
</row>
</block>
";
var processor = new EMarkProcessor();
processor.Process(eMarkDoc);
}
}
}
