public int GetCharWidth(char ch, IFont font) { OpenGLFont f = (OpenGLFont)font; ABC abc = f.GLYPHINFO[(int)ch]; return(abc.abcA + (int)abc.abcB + abc.abcC); }
public Size MeasureString(string str, IFont f) { //return Size.Empty; //empty? if (String.IsNullOrEmpty(str)) { return(Size.Empty); } //get the OpenGL font for this font. OpenGLFont font = (OpenGLFont)f; //calculate string height from line count string[] lines = str.Split('\n'); int lineCount = lines.Length; int maxWidth = 0; //find the largest width of all the lines for (int y = 0; y < lineCount; y++) { //get the line render width. int lineWidth = 0; fixed(char *line = lines[y].ToCharArray()) { //empty? if (line == (char *)0) { continue; } int lineLength = lines[y].Length; char *ptr = line; char *ptrEnd = ptr + lineLength; while (ptr != ptrEnd) { ABC g = font.GLYPHINFO[(int)(*ptr++)]; lineWidth += g.abcA + (int)g.abcB + g.abcC; } } //largest width so far? if (lineWidth > maxWidth) { maxWidth = lineWidth; } } return(new Size( maxWidth, font.METRIC.tmAscent * lineCount)); }
public void DrawString(string txt, int x, int y) { if (txt.Length == 0) { return; } //get the font selected. OpenGLFont font = p_Font; //is there any lines? if (txt.Contains("\n")) { //recall DrawString per line since OpenGL //bitmap calllists do not support multi-line int currentY = y; string[] lines = txt.Split('\n'); int lineLength = lines.Length; for (int c = 0; c < lineLength; c++) { DrawString( lines[c], x, currentY); //move y to the start of the next line currentY += font.METRIC.tmAscent; } return; } /* * since OpenGL will render the bottom of the text * along the y coord we give it, we need to offset * it by the font height - the gap between the top * of quad and the character. */ y += font.METRIC.tmAscent; //jump to the start of the compiled GL list for //rendering the font bitmaps. glListBase(font.LIST); glRasterPos2f(x, y); //call it. glCallLists( txt.Length, UNSIGNED_BYTE, txt); /*we assume every character is drawn as a textured quad...*/ p_CurrentVertCount += (txt.Length << 2); // (*4) }
void InitGL() { gl.ShadeModel(gl.GL_SMOOTH); gl.ClearColor(0.0f, 0.0f, 0.0f, 0.5f); gl.ClearDepthf(1.0f); //gl.Enable(gl.GL_DEPTH_TEST); gl.BlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA); gl.DepthFunc(gl.GL_LEQUAL); gl.Hint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST); myFont = new OpenGLFont(new Font(FontFamily.GenericSerif, 12, FontStyle.Regular)); myHugeFont = new OpenGLFont(new Font(FontFamily.GenericSerif, 32, FontStyle.Regular)); myLeftAligned = new GlyphRun(myHugeFont, "hello world", float.PositiveInfinity, float.PositiveInfinity, OpenGLTextAlignment.Left, true); myCentered = new GlyphRun(myFont, "centered text", ClientSize.Width, ClientSize.Height, OpenGLTextAlignment.Center, true); myJustified = new GlyphRun(myFont, "this text blob is really long and hopefully it will be justified to the edges as the lines wrap and stuff", ClientSize.Width, ClientSize.Height, OpenGLTextAlignment.Justified, true); myTexture = Texture.LoadStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TestOpenGLES.BitmapBrush.bmp"), false); myLeftAligned.ApplyTextureShader((glyphPos) => { float left = glyphPos.TopLeft.X / myLeftAligned.Width; float top = glyphPos.TopLeft.Y / myLeftAligned.Height; float right = glyphPos.TopRight.X / myLeftAligned.Width; float bottom = glyphPos.BottomLeft.Y / myLeftAligned.Height; return(new GlyphTexCoords(left, top, right, bottom)); } ); myLeftAligned.Texture = myTexture; myJustified.ApplyColorShader((glyphPos) => { GlyphColors colors = new GlyphColors(); colors.TopLeft = new Vector4f(1, 0, 0, 1); colors.BottomLeft = new Vector4f(0, 1, 0, 1); colors.TopRight = new Vector4f(1, 0, 0, 1); colors.BottomRight = new Vector4f(0, 0, 1, 1); return(colors); } ); }
public IFont AllocateFont(Font font) { Monitor.Enter(p_Mutex); //create a hash code for this font. int fontHash = createFontHash(font); //does this font already exist? int fontListLength = p_Fonts.Length; for (int c = 0; c < fontListLength; c++) { if (fontHash == p_Fonts[c].HASH) { //return the font Monitor.Exit(p_Mutex); return(p_Fonts[c]); } } /*select the font on the GDI stack*/ IntPtr hfont = font.ToHfont(); SelectObject(p_DeviceContext, hfont); /*create the internal struct we use to handle this fonts rendering.*/ OpenGLFont buffer = new OpenGLFont { LIST = glGenLists(256), HFONT = hfont, HASH = fontHash, GLYPHINFO = new ABC[256] }; bool success = false; string errorString = ""; /*get the initial gl bitmap lists for the font*/ if (!wglUseFontBitmaps(p_DeviceContext, 0, 256, buffer.LIST)) { errorString = "OpenGL: Unable to create font bitmap! (" + GetLastError() + ")"; } /*get the width information*/ else if (!GetCharABCWidths(p_DeviceContext, 0, 255, buffer.GLYPHINFO)) { errorString = "OpenGL: Cannot retrieve font character width information!"; } /*get the height information*/ else if (!GetTextMetrics(p_DeviceContext, out buffer.METRIC)) { errorString = "OpenGL: Cannot retrieve font metrics"; } else { success = true; } //success? if (!success) { Monitor.Exit(p_Mutex); throw new Exception(errorString); } //add this font to the stack Array.Resize(ref p_Fonts, p_Fonts.Length + 1); p_Fonts[p_Fonts.Length - 1] = buffer; Console.WriteLine("Added font: " + font); Monitor.Exit(p_Mutex); return(buffer); }
public IFont SetFont(Font font) { p_Font = (OpenGLFont)p_Context.AllocateFont(font); return(p_Font); }