public int GetStringWidth(FontInfo fontInfo, string text) { var width = 0; var startChar = fontInfo.StartChar; foreach (var characterToOutput in text) { FontCharInfo fontCharInfo = fontInfo.GetFontCharInfo(characterToOutput); width += fontCharInfo.WidthBits + 1; } return width; }
// Draws a string using the supplied font // x: Starting x co-ordinate // y: Starting y co-ordinate // color: Color to use when rendering the font // fontInfo: FontInfo reference to use when drawing the string // str: The string to render // Example // DrawString(0, 90, BasicColor.BLACK, bitstreamVeraSansMono9ptFontInfo, "Vera Mono 9 (30 chars wide)"); // DrawString(0, 105, BasicColor.BLACK, bitstreamVeraSansMono9ptFontInfo, "123456789012345678901234567890"); public virtual void DrawString(int x, int y, BasicColor color, FontInfo fontInfo, string text) { // set current x, y to that of requested var currentX = x; // Send individual characters foreach (var characterToOutput in text) { // We need to manually calculate width in pages since this is screwy with variable width fonts // var heightPages = charWidth % 8 ? charWidth / 8 : charWidth / 8 + 1; FontCharInfo fontCharInfo = fontInfo.GetFontCharInfo(characterToOutput); DrawCharBitmap(currentX, y, color, fontInfo.Data, fontCharInfo.Offset, fontCharInfo.WidthBits, fontInfo.Height); // next char X currentX += fontCharInfo.WidthBits + 1; } }