chatdrom's picture

OpenTK draw text in Unicode

Project:OpenTK.Text
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:open
Description

I want to show code where print text in Unicode

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Runtime.InteropServices;
 
namespace OpenTKBitmapTexture
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
 
		Color textColor=Color.Green;
		Color backgroundColor=Color.White;
		Font font;
              Bitmap bmp;
        bool loadGL = false;
 
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			font=new Font(new FontFamily("Times New Roman"),14,FontStyle.Regular);
			//Support Unicode
 
			pictureChooseTextColor.BackColor=textColor;
			pictureChooseColorBackground.BackColor=backgroundColor;
            pictureGDI.BackColor = backgroundColor;
			lblFont.Text=FontString(font);
 
 
            pictureGL.Load += new EventHandler(pictureGL_Load);
            pictureGL.Paint += new PaintEventHandler(pictureGL_Paint);
            pictureGL.Resize += new EventHandler(pictureGL_Resize);
 
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
 
		#region Commands
		//Run Project		
		void BtnRunClick(object sender, EventArgs e)
		{
			//MessageBox.Show("Run project",this.Text);
            //loadGL = false;
            DrawGDI();
            DrawGL();
 
		}
 
		//Font Text
		void BtnFontClick(object sender, EventArgs e)
		{
			if(fontDialog.ShowDialog()==DialogResult.OK)
			{
				font=fontDialog.Font;
				lblFont.Text=FontString(font);
                BtnRunClick(sender, e);
			}
		}
 
 
		void PictureChooseTextColorClick(object sender, EventArgs e)
		{
            if (colorTextColorDialog.ShowDialog() == DialogResult.OK)
            {
                textColor = colorTextColorDialog.Color;
                pictureChooseTextColor.BackColor = textColor;
                BtnRunClick(sender, e); 
            } 
 
        }
 
		void PictureChooseColorBackgroundClick(object sender, EventArgs e)
        {
            if (colorBackgroundColorDialog.ShowDialog() == DialogResult.OK)
            {
                backgroundColor = colorBackgroundColorDialog.Color;
                pictureChooseColorBackground.BackColor = backgroundColor;
                BtnRunClick(sender, e); 
            }
 
		}
		#endregion
 
		#region Methods
		String FontString(Font fonts)
		{
			return String.Format("Font: {0}, {1}, {2}.",
			                     fonts.FontFamily.Name,fonts.Size,
			                     fonts.Style.ToString());
		}
 
        void DrawGDI()
        {
            Bitmap tmpFont = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            SizeF size=Graphics.FromImage(tmpFont).MeasureString(txtBox.Text, font);
            textureHeight = (int)size.Height; textureWidth = (int)size.Width;
 
            if (textureWidth == 0) { textureHeight = 1; textureWidth = 1; }
            bmp = new Bitmap(textureWidth, textureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);       
            Graphics e = Graphics.FromImage(bmp);
            e.Clear(backgroundColor);
            SolidBrush brush=new SolidBrush(textColor);
            e.SmoothingMode = SmoothingMode.HighSpeed;
            e.DrawString(txtBox.Text, font, brush, new PointF(0,0));
 
            pictureGDI.Image = bmp;
            pictureGDI.BackColor = backgroundColor;            
            pictureGDI.Invalidate();
            tmpFont.Dispose();
            e.Dispose();
 
 
        }
 
 
        #region GL Graphics
        int textureWidth = 100, textureHeight = 100;
        void DrawGL()
        {
            loadGL = false;
            pictureGL_Load(new object(), new EventArgs());
            pictureGL.Invalidate();
        }
        void pictureGL_Resize(object sender, EventArgs e)
        {
            SetupViewPort();
            pictureGL.Invalidate();
        }
 
        void pictureGL_Paint(object sender, PaintEventArgs e)
        {
            if (!loadGL) return;
            GL.ClearColor(backgroundColor);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            DrawString();
            pictureGL.SwapBuffers();
        }
 
        int res;
        void pictureGL_Load(object sender, EventArgs e)
        {
            loadGL = true;
            res = loadBitmap(bmp);
            SetupViewPort();
        }
 
        int w, h;
        void SetupViewPort()
        {
            w = pictureGL.Width;
            h = pictureGL.Height;
            if (h == 0) h = 1;
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Viewport(0, 0, w, h);
            GL.Ortho(0f, w, h, 0f, 0f, 1f);
 
 
        }
 
        void DrawString()
        {
            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, loadBitmap(bmp));
                 DrawQuad();
            GL.Disable(EnableCap.Texture2D);
        }
 
        void DrawQuad()
        {
            GL.Begin(BeginMode.Quads);
                GL.TexCoord2(0f, 0f); GL.Vertex2(0, 0);
                GL.TexCoord2(0f, 1f); GL.Vertex2(0,(float)textureHeight);
                GL.TexCoord2(1f, 1f); GL.Vertex2((float)textureWidth,(float)textureHeight);
                GL.TexCoord2(1f, 0f); GL.Vertex2((float)textureWidth,0);                
            GL.End();
        }
 
        int loadBitmap(Bitmap image)
        {
            try
            {
                short width = (short)(image.Width);
                short height = (short)(image.Height);
 
                 int bpp = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb ? 32 : 24;
 
                BitmapData bData = image.LockBits(new Rectangle(new Point(), image.Size),
                ImageLockMode.ReadOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                image.UnlockBits(bData);
 
                int result = GL.GenTexture();
                GL.BindTexture(TextureTarget.Texture2D, result);
 
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
                    (int)TextureWrapMode.ClampToEdge);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
                    (int)TextureWrapMode.ClampToEdge);
 
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                    (int)TextureMagFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, 
                    (int)TextureMinFilter.Linear);
 
                GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, 
                    (int)TextureEnvMode.Modulate);
 
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0,
                    OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bData.Scan0);
 
                return result;
            }
            catch { return -1;  }
        }
		#endregion
 
 
        #endregion
 
 
    }
}

Example Unicode
Code this app
What do you think about it?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chatdrom's picture

#1