private void bannerSaveFileDialog_FileOk(object sender, CancelEventArgs e) { byte[] fntData = findFile(fontTextBox.Text); if (fntData == null) { MessageBox.Show(this, "Cannot find the given font", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string text = textTextBox.Text; LibDescent.Data.Font font = MainForm.LoadFont(new MemoryStream(fntData)); FNTRenderer frend = new FNTRenderer(font); Bitmap result; if (testing) { int cw = Math.Max(8, font.GetCharWidth('\0')) * 2; result = new Bitmap(cw * 16, (font.Height + 3) * 16); Graphics gdi = Graphics.FromImage(result); int x = 0, lx, ly, lw; Pen pen = new Pen(Color.Red); for (int c = 0; c < 256; ++c) { lx = (c % 16) * cw; ly = (font.Height + 3) * (c / 16); x = lx; frend.Reset(); frend.DrawCharacterRaw(result, (char)c, Color.Green, ref x, ly); x = lx; ly += font.Height; lw = font.GetCharWidth((char)c); gdi.DrawLine(pen, lx, ly, lx + lw, ly); } } else { result = new Bitmap(font.MeasureWidth(text), font.Height); int x = 0; foreach (char c in text) { frend.DrawCharacterRaw(result, c, Color.Green, ref x, 0); } } result.Save(bannerSaveFileDialog.FileName); }
public virtual void DrawCharacterRaw(Bitmap b, char c, Color clr, ref int x, int y) { int thisWidth = font.GetCharWidth(c); if (font.GetCharacterOffset(c, out int offset, out int size)) { //byte[] charData = font.fontData[c - minchar]; if (x < -thisWidth || x > b.Width || y < -this.font.Height || y > b.Height) { return; } bufferGraphics.Clear(Color.Transparent); BitmapData data = buffer.LockBits(bufferRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, buffer.PixelFormat); byte cr = clr.R; byte cg = clr.G; byte cb = clr.B; int cptr = offset; IntPtr ptr = data.Scan0; int bytes = data.Stride * data.Height; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); for (int yo = 0; yo < font.Height; ++yo) { int p = yo * data.Stride; if ((font.Flags & LibDescent.Data.Font.FT_COLOR) != 0) { for (int xo = 0; xo < thisWidth; ++xo) { byte color = font.FontData[cptr++]; if (color < 255) { rgbValues[p + xo * 4] = (byte)(font.Palette[color * 3 + 2] << 2); rgbValues[p + xo * 4 + 1] = (byte)(font.Palette[color * 3 + 1] << 2); rgbValues[p + xo * 4 + 2] = (byte)(font.Palette[color * 3] << 2); rgbValues[p + xo * 4 + 3] = 255; } } } else { for (int xo = 0; xo < thisWidth; xo += 8) { byte sliver = font.FontData[cptr++]; for (int xs = 0; xs < 8; ++xs) { if (xo + xs >= thisWidth) { break; } if ((sliver & 0x80) != 0) { rgbValues[p + (xo + xs) * 4] = cb; rgbValues[p + (xo + xs) * 4 + 1] = cg; rgbValues[p + (xo + xs) * 4 + 2] = cr; rgbValues[p + (xo + xs) * 4 + 3] = 255; } sliver <<= 1; } } } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); buffer.UnlockBits(data); // blit Graphics.FromImage(b).DrawImage(buffer, x, y); } x += thisWidth; x += font.GetKernOffset(c, prevChar); prevChar = c; }