/// <summary> /// Prints <paramref name="text"/> at coordinates <paramref name="x"/> and <paramref name="y"/>. /// </summary> /// <param name="text">The text to print.</param> /// <param name="x">X-coordinate in the area.</param> /// <param name="y">Y-coordinate in the area.</param> /// <param name="brush">The brush to print with.</param> public virtual void Print(string text, int x, int y, BrushSettings brush) { int CX = Width; int CY = Height; int MaxCX = x + text.Length > CX ? x + text.Length : CX; int MaxCY = y + 1 > CY ? y + 1 : CY; if (MaxCX > CX || MaxCY > CY) { PrintableCharacter[,] NewPrintArea = new PrintableCharacter[MaxCX, MaxCY]; for (int i = 0; i < CX; i++) { for (int j = 0; j < CY; j++) { NewPrintArea[i, j] = PrintArea[i, j]; } } PrintArea = NewPrintArea; } for (int n = 0; n < text.Length; n++) { PrintArea[x + n, y] = new PrintableCharacter(text[n], brush); } }
/// <summary> /// Returns a string representation of this instance. /// </summary> public virtual string ToStringContent(bool isSingleLine) { string[] Lines = new string[Height]; for (int i = 0; i < Height; i++) { int Length; for (Length = Width; Length > 0; Length--) { PrintableCharacter c = PrintArea[Length - 1, i]; if (c != null) { break; } } string Line = string.Empty; for (int j = 0; j < Length; j++) { PrintableCharacter c = PrintArea[j, i]; if (c != null) { Line += c.C; } else { Line += " "; } } Lines[i] = Line; } string Result; if (isSingleLine && Lines.Length == 1) { Result = Lines[0]; } else { Result = string.Empty; foreach (string Line in Lines) { Result += Line + "\r\n"; } } return(Result); }
/// <summary> /// Returns a string representation of this instance in markdown-compatible HTML format. /// </summary> public virtual string ToMarkdownHtmlContent(IReadOnlyDictionary <BrushSettings, Brush> brushTable) { string Result = string.Empty; Result += "<pre>\n"; string[] Lines = new string[Height]; for (int i = 0; i < Height; i++) { int Length; for (Length = Width; Length > 0; Length--) { PrintableCharacter c = PrintArea[Length - 1, i]; if (c != null) { break; } } BrushSettings CurrentBrush = BrushSettings.Default; string Line = string.Empty; string CurrentTag = StartBrushTag(CurrentBrush); Line += CurrentTag; for (int j = 0; j < Length; j++) { PrintableCharacter Character = PrintArea[j, i]; if (Character != null) { if (CurrentBrush != Character.Brush) { BrushSettings NewBrush = Character.Brush; string NewTag = StartBrushTag(NewBrush); if (NewTag != CurrentTag) { Line += EndBrushTag(CurrentBrush); Line += NewTag; } CurrentBrush = NewBrush; CurrentTag = NewTag; } char C = Character.C; if (C == ' ') { Line += " "; } else if (C <= 0x7F) { Line += Character.C; } else { Line += $"&#{Convert.ToInt32(C)};"; } } else { Line += " "; } } Line += EndBrushTag(CurrentBrush); Lines[i] = Line; } foreach (string Line in Lines) { Result += Line + "\n"; } Result += "</pre>\n"; return(Result); }
/// <summary> /// Returns a string representation of this instance in raw HTML format. /// </summary> public virtual string ToRawHtmlContent(IReadOnlyDictionary <BrushSettings, Brush> brushTable) { string Result = string.Empty; Result += "<div style=\"margin-top: 10px; line-height: 0; background-color: whitesmoke; border-style: solid; border-color: lightgray; border-width: thin; font-family: Courier\">\n"; Result += "<div style=\"margin-left: 10px\">\n"; Result += "<br/>\n"; string[] Lines = new string[Height]; for (int i = 0; i < Height; i++) { int Length; for (Length = Width; Length > 0; Length--) { PrintableCharacter c = PrintArea[Length - 1, i]; if (c != null) { break; } } BrushSettings CurrentBrush = BrushSettings.Default; string Line = $"<p><span style=\"color: {BrushToColorText(brushTable[CurrentBrush])};\">"; for (int j = 0; j < Length; j++) { PrintableCharacter Character = PrintArea[j, i]; if (Character != null) { if (CurrentBrush != Character.Brush) { CurrentBrush = Character.Brush; Line += $"</span><span style=\"color: {BrushToColorText(brushTable[CurrentBrush])};\">"; } char C = Character.C; if (C == ' ') { Line += " "; } else if (C <= 0x7F) { Line += Character.C; } else { Line += $"&#{Convert.ToInt32(C)};"; } } else { Line += " "; } } Line += "</span></p>"; Lines[i] = Line; } foreach (string Line in Lines) { Result += Line + "\n"; } Result += "<br/>\n"; Result += "</div>\n"; Result += "</div>\n"; return(Result); }
/// <summary> /// Initializes a new instance of the <see cref="PrintableArea"/> class. /// </summary> public PrintableArea() { PrintArea = new PrintableCharacter[0, 0]; }
/// <summary> /// Returns a string representation of this instance in RTF format. /// </summary> public virtual string ToRtfContent(IReadOnlyDictionary <BrushSettings, Brush> brushTable) { string Result = string.Empty; Result += "{\\rtf\\ansi"; Result += "{\\fonttbl{\\f0 Consolas;}}"; Result += "{\\colortbl;"; for (BrushSettings Index = 0; (int)Index < typeof(BrushSettings).GetEnumValues().Length; Index++) { SolidColorBrush b = brushTable.ContainsKey(Index) ? brushTable[Index] as SolidColorBrush : Brushes.Black; Result += $"\\red{b.Color.R}\\green{b.Color.G}\\blue{b.Color.B};"; } Result += @"}"; Result += "\\f0 \\fs19 \\cf1 \\cb0 \\highlight0"; string[] Lines = new string[Height]; for (int i = 0; i < Height; i++) { int Length; for (Length = Width; Length > 0; Length--) { PrintableCharacter c = PrintArea[Length - 1, i]; if (c != null) { break; } } BrushSettings CurrentBrush = BrushSettings.Default; string Line = $"\\cf{((int)CurrentBrush) + 1} "; for (int j = 0; j < Length; j++) { PrintableCharacter Character = PrintArea[j, i]; if (Character != null) { if (CurrentBrush != Character.Brush) { CurrentBrush = Character.Brush; Line += $"\\cf{((int)CurrentBrush) + 1} "; } char C = Character.C; if (C <= 0x7F) { Line += Character.C; } else { Line += $"\\u{Convert.ToInt32(C)}?"; } } else { Line += " "; } } Lines[i] = Line; } foreach (string Line in Lines) { Result += Line + @"\par "; } Result += "}"; return(Result); }