public void DrawText(string text, GLFont font, float startX, float startY, GLBrush brush, float width = 1000) { GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, font.Texture); GL.Color4(brush.Color0.R, brush.Color0.G, brush.Color0.B, (byte)255); GL.Begin(BeginMode.Quads); int alignmentOffsetX = 0; if (font.Alignment != 0) { font.MeasureString(text, out int minX, out int maxX); if (font.Alignment == 1) { alignmentOffsetX -= minX; alignmentOffsetX += ((int)width - maxX - minX) / 2; } else { alignmentOffsetX -= minX; alignmentOffsetX += ((int)width - maxX - minX); } } int x = (int)(startX + alignmentOffsetX); int y = (int)(startY + font.OffsetY); for (int i = 0; i < text.Length; i++) { var c0 = text[i]; var info = font.GetCharInfo(c0); int x0 = x + info.xoffset; int y0 = y + info.yoffset; int x1 = x0 + info.width; int y1 = y0 + info.height; GL.TexCoord2(info.u0, info.v0); GL.Vertex2(x0, y0); GL.TexCoord2(info.u1, info.v0); GL.Vertex2(x1, y0); GL.TexCoord2(info.u1, info.v1); GL.Vertex2(x1, y1); GL.TexCoord2(info.u0, info.v1); GL.Vertex2(x0, y1); x += info.xadvance; if (i != text.Length - 1) { char c1 = text[i + 1]; x += font.GetKerning(c0, c1); } } GL.End(); GL.Disable(EnableCap.Texture2D); }
public GLFont CreateFont(Gdk.Pixbuf pixbuf, string[] def, int size, int alignment, bool ellipsis, int existingTexture = -1) { var font = (GLFont)null; var lines = def; int baseValue = 0; int texSizeX = 256; int texSizeY = 256; foreach (var line in lines) { var splits = line.Split(new[] { ' ', '=', '\"' }, StringSplitOptions.RemoveEmptyEntries); switch (splits[0]) { case "common": { baseValue = ReadFontParam <int>(splits, "base"); texSizeX = ReadFontParam <int>(splits, "scaleW"); texSizeY = ReadFontParam <int>(splits, "scaleH"); int glTex = existingTexture; if (glTex == 0) { glTex = CreateGLTexture(pixbuf); } font = new GLFont(glTex, size - baseValue, alignment, ellipsis); break; } case "char": { var charInfo = new GLFont.CharInfo(); int c = ReadFontParam <int>(splits, "id"); int x = ReadFontParam <int>(splits, "x"); int y = ReadFontParam <int>(splits, "y"); charInfo.width = ReadFontParam <int>(splits, "width"); charInfo.height = ReadFontParam <int>(splits, "height"); charInfo.xoffset = ReadFontParam <int>(splits, "xoffset"); charInfo.yoffset = ReadFontParam <int>(splits, "yoffset"); charInfo.xadvance = ReadFontParam <int>(splits, "xadvance"); charInfo.u0 = (x + 0.0f) / (float)texSizeX; charInfo.v0 = (y + 0.0f) / (float)texSizeY; charInfo.u1 = (x + 0.0f + charInfo.width) / (float)texSizeX; charInfo.v1 = (y + 0.0f + charInfo.height) / (float)texSizeY; font.AddChar((char)c, charInfo); break; } case "kerning": { int c0 = ReadFontParam <int>(splits, "first"); int c1 = ReadFontParam <int>(splits, "second"); int amount = ReadFontParam <int>(splits, "amount"); font.AddKerningPair(c0, c1, amount); break; } } } return(font); }
public float MeasureString(string text, GLFont font) { font.MeasureString(text, out int minX, out int maxX); return(maxX - minX); }