private unsafe void DrawChar(BdfChar fontChar, IntPtr dispBuf, int dispBW, int dispBH, int dx, int dy, int icolor) { dx = dx - this.fbLeft + fontChar.bbLeft; dy = dy + this.fbH - fontChar.bbH + this.fbBottom - fontChar.bbBottom; int x1 = dx; int y1 = dy; int x2 = dx + fbW - 1; int y2 = dy + fbH - 1; if (x1 >= dispBW || x2 < 0 || y1 >= dispBH || y2 < 0) { return; } int fw = fontChar.bbW; int fh = fontChar.bbH; byte[] src = fontChar.bitmap; for (int y = 0; y < fh; y++) { if (dy + y < 0 || dy + y >= dispBH) { continue; } int *dst = (int *)dispBuf + dispBW * (dy + y) + dx; int srcIdx = y * fw; for (int x = 0; x < fw; x++, srcIdx++, dst++) { if (dx + x < 0 || dx + x >= dispBW) { continue; } if (src[srcIdx] != 0) { *dst = icolor; } } } }
public BdfFont(string bdf) { string[] lines = bdf.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); int bitmapIdx = -1; foreach (var line in lines) { string name = string.Empty; string data = string.Empty; var spaceIdx = line.IndexOf(' '); if (spaceIdx > 0) { name = line.Substring(0, spaceIdx); data = line.Substring(spaceIdx + 1); } else { name = line; } if (name == "ENDCHAR") { currChar = null; bitmapIdx = -1; } if (bitmapIdx != -1) { ParseBitmap(currChar.bitmap, bitmapIdx, line, currChar.bbW); bitmapIdx += currChar.bbW; } else { if (name == "FONTBOUNDINGBOX") { var words = data.Split(' '); this.fbW = int.Parse(words[0]); this.fbH = int.Parse(words[1]); this.fbLeft = int.Parse(words[2]); this.fbBottom = int.Parse(words[3]); } else if (name == "STARTCHAR") { currChar = new BdfChar(); currChar.name = data; } else if (name == "ENCODING") { int charIdx = int.Parse(data); fontChars[charIdx] = currChar; currChar.encoding = charIdx; } else if (name == "DWIDTH") { var words = data.Split(' '); currChar.width = int.Parse(words[0]); } else if (name == "BBX") { var words = data.Split(' '); currChar.bbW = int.Parse(words[0]); currChar.bbH = int.Parse(words[1]); currChar.bbLeft = int.Parse(words[2]); currChar.bbBottom = int.Parse(words[3]); currChar.bitmap = new byte[currChar.bbW * currChar.bbH]; } else if (name == "BITMAP") { bitmapIdx = 0; } } } }