private FontOutlineEntry CreateFontOutlineEntry(OpenGL gl, string faceName, float deviation, float extrusion, FontOutlineFormat fontOutlineFormat) { // Make the OpenGL instance current. gl.MakeCurrent(); // Create the font based on the face name. var hFont = Win32.CreateFont(12, 0, 0, 0, Win32.FW_DONTCARE, 0, 0, 0, Win32.DEFAULT_CHARSET, Win32.OUT_OUTLINE_PRECIS, Win32.CLIP_DEFAULT_PRECIS, Win32.CLEARTYPE_QUALITY, Win32.VARIABLE_PITCH, faceName); // Select the font handle. var hOldObject = Win32.SelectObject(gl.RenderContextProvider.DeviceContextHandle, hFont); // Create the list base. var listBase = gl.GenLists(1); // Create space for the glyph metrics. var glyphMetrics = new GLYPHMETRICSFLOAT[255]; // Create the font bitmaps. bool result = Win32.wglUseFontOutlines(gl.RenderContextProvider.DeviceContextHandle, 0, 255, listBase, deviation, extrusion, (int)fontOutlineFormat, glyphMetrics); // Reselect the old font. Win32.SelectObject(gl.RenderContextProvider.DeviceContextHandle, hOldObject); // Free the font. Win32.DeleteObject(hFont); // If we failed to create the font outlines, bail out now. if (result == false) { return(null); } // Create the font bitmap entry. var foe = new FontOutlineEntry() { HDC = gl.RenderContextProvider.DeviceContextHandle, HRC = gl.RenderContextProvider.RenderContextHandle, FaceName = faceName, ListBase = listBase, ListCount = 255, Deviation = deviation, Extrusion = extrusion, FontOutlineFormat = fontOutlineFormat, GlyphMetrics = glyphMetrics }; // Add the font bitmap entry to the internal list. fontOutlineEntries.Add(foe); return(foe); }
/// <summary> /// Draws the text. /// </summary> /// <param name="gl">The gl.</param> /// <param name="faceName">Name of the face.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="deviation">The deviation.</param> /// <param name="extrusion">The extrusion.</param> /// <param name="text">The text.</param> /// <param name="glyphMetrics"> </param> public void DrawText(OpenGL gl, string faceName, float fontSize, float deviation, float extrusion, string text, out GLYPHMETRICSFLOAT[] glyphMetrics) { // Get the font size in pixels. var fontHeight = (int)(fontSize * (16.0f / 12.0f)); // Do we have a font bitmap entry for this OpenGL instance and face name? var result = (from fbe in fontOutlineEntries where fbe.HDC == gl.RenderContextProvider.DeviceContextHandle && fbe.HRC == gl.RenderContextProvider.RenderContextHandle && String.Compare(fbe.FaceName, faceName, StringComparison.OrdinalIgnoreCase) == 0 && fbe.Height == fontHeight && fbe.Deviation == deviation && fbe.Extrusion == extrusion select fbe).ToList(); // Get the FBE or null. var fontOutlineEntry = result.FirstOrDefault(); // If we don't have the FBE, we must create it. if (fontOutlineEntry == null) fontOutlineEntry = CreateFontOutlineEntry(gl, faceName, fontHeight, deviation, extrusion, FontOutlineFormat.Polygons); // Set the list base. gl.ListBase(fontOutlineEntry.ListBase); // Create an array of lists for the glyphs. var lists = text.Select(c => (byte) c).ToArray(); // Call the lists for the string. gl.CallLists(lists.Length, lists); gl.Flush(); // Return the glyph metrics used. glyphMetrics = fontOutlineEntry.GlyphMetrics; }
private FontOutlineEntry CreateFontOutlineEntry(OpenGL gl, string faceName, int height, float deviation, float extrusion, FontOutlineFormat fontOutlineFormat) { // Make the OpenGL instance current. gl.MakeCurrent(); // Create the font based on the face name. var hFont = Win32.CreateFont(height, 0, 0, 0, Win32.FW_DONTCARE, 0, 0, 0, Win32.DEFAULT_CHARSET, Win32.OUT_OUTLINE_PRECIS, Win32.CLIP_DEFAULT_PRECIS, Win32.CLEARTYPE_QUALITY, Win32.VARIABLE_PITCH, faceName); // Select the font handle. var hOldObject = Win32.SelectObject(gl.RenderContextProvider.DeviceContextHandle, hFont); // Create the list base. var listBase = gl.GenLists(1); // Create space for the glyph metrics. var glyphMetrics = new GLYPHMETRICSFLOAT[255]; // Create the font bitmaps. bool result = Win32.wglUseFontOutlines(gl.RenderContextProvider.DeviceContextHandle, 0, 255, listBase, deviation, extrusion, (int)fontOutlineFormat, glyphMetrics); // Reselect the old font. Win32.SelectObject(gl.RenderContextProvider.DeviceContextHandle, hOldObject); // Free the font. Win32.DeleteObject(hFont); // Create the font bitmap entry. var foe = new FontOutlineEntry() { HDC = gl.RenderContextProvider.DeviceContextHandle, HRC = gl.RenderContextProvider.RenderContextHandle, FaceName = faceName, Height = height, ListBase = listBase, ListCount = 255, Deviation = deviation, Extrusion = extrusion, FontOutlineFormat = fontOutlineFormat, GlyphMetrics = glyphMetrics }; // Add the font bitmap entry to the internal list. fontOutlineEntries.Add(foe); return foe; }