public static string WrapASCIIText(int fontNumber, string text, float maxLineWidth) { if (string.IsNullOrEmpty(text)) { text = string.Empty; } string[] words = text.Split(' '); StringBuilder sb = new StringBuilder(); float lineWidth = 0f; float spaceWidth = ASCIIFont.GetFixed(fontNumber).GetWidth(" "); foreach (string word in words) { Vector2 size = new Vector2(ASCIIFont.GetFixed(fontNumber).GetWidth(word), ASCIIFont.GetFixed(fontNumber).Height); if (lineWidth + size.X < maxLineWidth) { sb.Append(word + " "); lineWidth += size.X + spaceWidth; } else { sb.Append("\n" + word + " "); lineWidth = size.X + spaceWidth; } } return(sb.ToString()); }
public override void Initialize() { base.Initialize(); InstallLocation install = Install; _fonts = new ASCIIFont[10]; using (BinaryReader reader = new BinaryReader(File.Open(install.GetPath("fonts.mul"), FileMode.Open))) { for (int i = 0; i < 10; ++i) { reader.ReadByte(); //header var chars = new ASCIIChar[224]; int fontHeight = 0; for (int k = 0; k < 224; ++k) { byte width = reader.ReadByte(); byte height = reader.ReadByte(); reader.ReadByte(); // delimeter? if (width > 0 && height > 0) { if (height > fontHeight && k < 96) { fontHeight = height; } var pixels = new ushort[width * height]; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { pixels[y * width + x] = (ushort)(reader.ReadByte() | (reader.ReadByte() << 8)); } } chars[k] = new ASCIIChar { Pixels = pixels, Width = width, Height = height }; } } _fonts[i] = new ASCIIFont(fontHeight, chars); } } }
public static string ClipASCIIText(int fontNumber, string text, int width, int pixelBuffer) { int charIndex = text.Length; string textToWrite = text.Substring(0, charIndex); Vector2 fontDimensions = new Vector2(ASCIIFont.GetFixed(fontNumber).GetWidth(textToWrite), ASCIIFont.GetFixed(fontNumber).Height); while (fontDimensions.X > width - (pixelBuffer * 2)) { charIndex--; textToWrite = text.Substring(0, charIndex); fontDimensions = new Vector2(ASCIIFont.GetFixed(fontNumber).GetWidth(textToWrite), ASCIIFont.GetFixed(fontNumber).Height); } return(textToWrite); }
public unsafe ImageSource GetText(int fontId, string text, short hueId) { ASCIIFont font = _fonts[fontId]; int width = font.GetWidth(text); int height = font.Height; var bmp = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr555, null); bmp.Lock(); var line = (int *)bmp.BackBuffer; int delta = bmp.BackBufferStride >> 2; int dx = 0; for (int i = 0; i < text.Length; ++i) { int c = ((((text[i]) - 0x20) & 0x7FFFFFFF) % 224); ASCIIChar ch = font.Chars[c]; int charWidth = ch.Width; int charHeight = ch.Height; ushort[] pixels = ch.Pixels; for (int dy = 0; dy < charHeight; ++dy) { int *dest = (line + (delta * (dy + (font.Height - charHeight)))) + (dx); for (int k = 0; k < charWidth; ++k) { ushort pixel = pixels[charWidth * dy + k]; * dest++ = (pixel == 0 ? 0 : 255 << 24) | ((byte)((pixel & 0x7C00) >> 7) << 16) | ((byte)((pixel & 0x3E0) >> 2) << 8) | (byte)((pixel & 0x1F) << 3); } } dx += charWidth; } bmp.AddDirtyRect(new Int32Rect(0, 0, width, height)); bmp.Unlock(); return(bmp); }
public unsafe Bitmap GetText(int fontId, string text, short hueId) { ASCIIFont font = _fonts[fontId]; int width = font.GetWidth(text); int height = font.Height; Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData bd = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var line = (int *)bd.Scan0; int delta = bd.Stride >> 2; int dx = 0; for (int i = 0; i < text.Length; ++i) { int c = ((((text[i]) - 0x20) & 0x7FFFFFFF) % 224); ASCIIChar ch = font.Chars[c]; int charWidth = ch.Width; int charHeight = ch.Height; var pixels = ch.Pixels; for (int dy = 0; dy < charHeight; ++dy) { var dest = (line + (delta * (dy + (font.Height - charHeight)))) + (dx); for (int k = 0; k < charWidth; ++k) { ushort pixel = pixels[charWidth * dy + k]; * dest++ = Color.FromArgb(pixel == 0 ? 0 : 255, (pixel & 0x7C00) >> 7, (pixel & 0x3E0) >> 2, (pixel & 0x1F) << 3).ToArgb(); } } dx += charWidth; } bmp.UnlockBits(bd); return(bmp); }