12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Windows.Forms;
- using NTERA.Core;
- using NTERA.Core.Interop;
- namespace NTERA.Console.RenderItem
- {
- public class TextRenderItem : BaseRenderItem
- {
- public Font Font { get; set; } = new Font("MS UI Gothic", 12);
- public SolidBrush TextBrush { get; set; } = new SolidBrush(Color.White);
- public string Text { get; set; }
- public DisplayLineAlignment Alignment { get; set; }
- public PrintFlags Flags { get; set; }
- private static readonly string[] FullwidthCharacters = "0123456789。".ToCharArray().Select(x => x.ToString()).ToArray();
- private static string AlignText(string input)
- {
- foreach (var num in FullwidthCharacters)
- {
- input = input.Replace(num, $" {num}");
- }
- return input.Replace(" ", " ");
- }
- public TextRenderItem(string text, DisplayLineAlignment alignment = DisplayLineAlignment.LEFT, Color? color = null)
- {
- Text = AlignText(text); //text.Replace(" ", " ").Replace("6", " 6"); //" 6 ", " 6 "
- Alignment = alignment;
-
- if (color.HasValue)
- TextBrush = new SolidBrush(color.Value);
- }
-
- public override int Render(Graphics graphics, Rectangle renderArea, Rectangle invalidatedArea, Point mousePointer)
- {
- int width = (int)graphics.MeasureString(Text, Font).Width;
- int x;
- switch (Alignment)
- {
- default:
- case DisplayLineAlignment.LEFT:
- x = renderArea.X;
- break;
- case DisplayLineAlignment.CENTER:
- x = (renderArea.Width - width) / 2;
- break;
- case DisplayLineAlignment.RIGHT:
- x = renderArea.Width - width;
- break;
- }
- //graphics.DrawString(Text, Font, TextBrush, x, renderArea.Y);
- //return x + width;
- var point = new Point(x, renderArea.Y);
- var textFormatFlags = TextFormatFlags.ExpandTabs;
- if (!Flags.SingleLine) {
- // We want word wrapping
- textFormatFlags |= TextFormatFlags.WordBreak;
- // FIXME: We will need to make a rectangle, not a point,
- // to draw the text in
- }
- TextRenderer.DrawText(graphics, Text, Font, point, TextBrush.Color, textFormatFlags);
- return TextRenderer.MeasureText(graphics, Text, Font).Width + renderArea.X;
- }
- public static List<TextRenderItem> CreateFromLinedText(string text)
- {
- List<TextRenderItem> items = new List<TextRenderItem>();
- foreach (string line in text.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
- {
- items.Add(new TextRenderItem(line));
- }
- return items;
- }
- }
- }
|