/// <summary> /// Prepares text for drawing. /// </summary> /// <param name="text">The string to draw.</param> /// <param name="font">The font to use for drawing.</param> /// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param> /// <param name="width">Not implemented.</param> /// <param name="wordWarp">Not implemented.</param> /// <see cref="TextPrinter.Draw()"/> public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp) { this.Prepare(text, font, out handle, width, wordWarp, StringAlignment.Near, false); }
public void InitFonts() { //Init Fonts to defaults LabelFont = new TextureFont(new Font(FontFamily.GenericSansSerif, 10.0f)); LabelFont.RebuildTextures(); TitleFont = new TextureFont(new Font(FontFamily.GenericSansSerif, 18.0f)); AxesFont = new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f, FontStyle.Bold)); }
/// <summary> /// Draws dynamic text without caching. Not implemented yet! /// </summary> /// <param name="text">The System.String to draw.</param> /// <param name="font">The OpenTK.Fonts.TextureFont to draw the text in.</param> public void Draw(string text, TextureFont font) { //printer.Draw(text); }
// Performs layout on the given string. void PerformLayout(string text, TextureFont font, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft, ref Vector2[] vertices, ref ushort[] indices, out int num_indices) { if (text == null) throw new ArgumentNullException("Parameter cannot be null.", "text"); if (text.Length > 8192) throw new ArgumentOutOfRangeException("text", text.Length, "Text length must be between 1 and 8192 characters"); if (wordWarp || rightToLeft || alignment != StringAlignment.Near) throw new NotImplementedException(); while (8 * text.Length > vertices.Length) vertices = new Vector2[vertices.Length << 1]; while (6 * text.Length > indices.Length) indices = new ushort[indices.Length << 1]; num_indices = 6 * text.Length; //Vector2[] vertices = new Vector2[8 * text.Length]; // Interleaved, vertex, texcoord, vertex, etc... //ushort[] indices = new ushort[6 * text.Length]; float x_pos = 0, y_pos = 0; ushort i = 0, index_count = 0, vertex_count = 0, last_break_point = 0; Box2 rect = new Box2(); float char_width, char_height, measured_width, measured_height; int texture; font.LoadGlyphs(text); // Every character comprises of 4 vertices, forming two triangles. We generate an index array which // indexes vertices in a triangle-strip fashion. To create a single strip for the whole string, we // need to add a degenerate triangle (0 height) to connect the last vertex of the previous line with // the first vertex of the next line. // This algorithm works for left-to-right scripts. if (alignment == StringAlignment.Near && !rightToLeft || alignment == StringAlignment.Far && rightToLeft) { foreach (char c in text) { if (Char.IsSeparator(c)) last_break_point = index_count; if (c != '\n' && c != '\r') { font.GlyphData(c, out char_width, out char_height, out rect, out texture); vertices[vertex_count].X = x_pos; // Vertex vertices[vertex_count++].Y = y_pos; vertices[vertex_count].X = rect.Left; // Texcoord vertices[vertex_count++].Y = rect.Top; vertices[vertex_count].X = x_pos; // Vertex vertices[vertex_count++].Y = y_pos + char_height; vertices[vertex_count].X = rect.Left; // Texcoord vertices[vertex_count++].Y = rect.Bottom; vertices[vertex_count].X = x_pos + char_width; // Vertex vertices[vertex_count++].Y = y_pos + char_height; vertices[vertex_count].X = rect.Right; // Texcoord vertices[vertex_count++].Y = rect.Bottom; vertices[vertex_count].X = x_pos + char_width; // Vertex vertices[vertex_count++].Y = y_pos; vertices[vertex_count].X = rect.Right; // Texcoord vertices[vertex_count++].Y = rect.Top; indices[index_count++] = (ushort)(vertex_count - 8); indices[index_count++] = (ushort)(vertex_count - 6); indices[index_count++] = (ushort)(vertex_count - 4); indices[index_count++] = (ushort)(vertex_count - 4); indices[index_count++] = (ushort)(vertex_count - 2); indices[index_count++] = (ushort)(vertex_count - 8); font.MeasureString(text.Substring(i, 1), out measured_width, out measured_height); x_pos += measured_width; } else if (c == '\n') { //x_pos = layoutRect.Left; x_pos = 0; y_pos += font.Height; } ++i; } } else if (alignment != StringAlignment.Center) { throw new NotImplementedException("This feature is not yet implemented. Sorry for the inconvenience."); } else { throw new NotImplementedException("This feature is not yet implemented. Sorry for the inconvenience."); } }
/// <summary> /// Prepares text for drawing. /// </summary> /// <param name="text">The string to draw.</param> /// <param name="font">The font to use for drawing.</param> /// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param> /// <param name="width">Not implemented.</param> /// <param name="wordWarp">Not implemented.</param> /// <param name="alignment">Not implemented.</param> /// <param name="rightToLeft">Not implemented.</param> /// <see cref="TextPrinter.Draw()"/> /// <exception cref="NotSupportedException">Occurs when OpenGL 1.1 is not supported.</exception> public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft) { if (!functionality_checked) CheckNeededFunctionality(); int num_indices; PerformLayout(text, font, width, wordWarp, alignment, rightToLeft, ref vertices, ref indices, out num_indices); handle = printer.Load(vertices, indices, num_indices); handle.font = font; }
private TextHandle GetTextHandle(string text, TextureFont font) { if (!cachedHandles.ContainsKey(text)) { TextHandle handle; printer.Prepare(text, font, out handle); cachedHandles[text] = handle; } return cachedHandles[text]; }
/// <summary> /// Prepares text for drawing. /// </summary> /// <param name="text">The string to draw.</param> /// <param name="font">The font to use for drawing.</param> /// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param> /// <see cref="TextPrinter.Draw()"/> public void Prepare(string text, TextureFont font, out TextHandle handle) { this.Prepare(text, font, out handle, 0, false, StringAlignment.Near, false); }