示例#1
0
        public GLText(
            System.Windows.Forms.AnchorStyles anchor, System.Windows.Forms.Padding margin,
            System.Drawing.Size size, int zNear, int zFar, FontResource fontResource = null, int maxCharCount = 100)
            : base(null, anchor, margin, size, zNear, zFar)
        {
            if (fontResource == null)
            {
                this.fontResource = FontResource.Default;
            }
            else
            {
                this.fontResource = fontResource;
            }

            this.Name = "GLText";
            var shaderCodes = new ShaderCode[2];

            shaderCodes[0] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
                                                @"Resources.GLText.vert"), ShaderType.VertexShader);
            shaderCodes[1] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
                                                @"Resources.GLText.frag"), ShaderType.FragmentShader);
            var map = new PropertyNameMap();

            map.Add("position", "position");
            map.Add("uv", "uv");
            var      model    = new TextModel(maxCharCount);
            Renderer renderer = new Renderer(model, shaderCodes, map);

            this.model    = model;
            this.Renderer = renderer;
        }
示例#2
0
        public unsafe void SetText(string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                if (this.model != null && this.model.indexBufferPtr != null)
                {
                    this.model.indexBufferPtr.VertexCount = 0;
                }
                this.content = string.Empty;
                return;
            }

            this.content = content;

            int count = content.Length;

            if (count > this.model.maxCharCount)
            {
                throw new ArgumentException();
            }
            //{ count = this.model.maxCharCount; }

            FontResource fontResource = this.fontResource;

            SetupGlyphPositions(content, fontResource);
            SetupGlyphTexCoord(content, fontResource);
            this.model.indexBufferPtr.VertexCount = count * 4;
        }
示例#3
0
文件: UIText.cs 项目: xzoth/CSharpGL
        public UIText(
            System.Windows.Forms.AnchorStyles anchor, System.Windows.Forms.Padding margin,
            System.Drawing.Size size, int zNear, int zFar, FontResource fontResource = null, int maxCharCount = 100)
            : base(anchor, margin, size, zNear, zFar)
        {
            if (fontResource == null)
            { this.fontResource = FontResource.Default; }
            else
            { this.fontResource = fontResource; }

            this.Name = this.GetType().Name;
            var shaderCodes = new ShaderCode[2];
            shaderCodes[0] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
            @"Resources.UIText.vert"), ShaderType.VertexShader);
            shaderCodes[1] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
            @"Resources.UIText.frag"), ShaderType.FragmentShader);
            var map = new PropertyNameMap();
            map.Add("position", "position");
            map.Add("uv", "uv");
            var model = new TextModel(maxCharCount);
            Renderer renderer = new Renderer(model, shaderCodes, map);

            this.model = model;
            this.Renderer = renderer;
        }
        public static FontResource Load(Stream stream, IEnumerable<char> targets, int pixelSize)
        {
            InitStandardWidths();

            int count = targets.Count();
            int maxWidth = GetMaxWidth(pixelSize, count);

            var dict = new FullDictionary<char, CharacterInfo>(CharacterInfo.Default);
            Bitmap finalBitmap;
            using (var bitmap = new Bitmap(maxWidth, maxWidth, PixelFormat.Format24bppRgb))
            {
                int currentX = 0, currentY = 0;
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    var typeface = new FontFace(stream);

                    foreach (char c in targets)
                    {
                        BlitCharacter(pixelSize, maxWidth, dict, ref currentX, ref currentY, graphics, typeface, c);
                    }
                }

                finalBitmap = ShortenBitmap(bitmap, maxWidth, currentY + yInterval + pixelSize + (pixelSize / 10 > 1 ? pixelSize / 10 : 1));
            }

            var fontResource = new FontResource();
            fontResource.FontHeight = pixelSize + yInterval;
            fontResource.CharInfoDict = dict;
            fontResource.InitTexture(finalBitmap);
            finalBitmap.Dispose();
            return fontResource;
        }
示例#5
0
        unsafe private void SetupGlyphPositions(string content, FontResource fontResource)
        {
            FullDictionary <char, CharacterInfo> charInfoDict = fontResource.CharInfoDict;

            OpenGL.BindBuffer(BufferTarget.ArrayBuffer, this.model.positionBufferPtr.BufferId);
            IntPtr pointer = OpenGL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.ReadWrite);
            var    array = (GlyphPosition *)pointer.ToPointer();
            int    currentWidth = 0; int currentHeight = 0;

            /*
             * 0     3  4     7 8     11 12   15
             * -------  ------- -------  -------
             * |     |  |     | |     |  |     |
             * |     |  |     | |     |  |     |
             * |     |  |     | |     |  |     |
             * -------  ------- -------  -------
             * 1     2  5     6 9     10 13   14
             */
            for (int i = 0; i < content.Length; i++)
            {
                char          ch   = content[i];
                CharacterInfo info = charInfoDict[ch];
                array[i] = new GlyphPosition(
                    new vec2(currentWidth, currentHeight + fontResource.FontHeight),
                    new vec2(currentWidth, currentHeight),
                    new vec2(currentWidth + info.width, currentHeight),
                    new vec2(currentWidth + info.width, currentHeight + fontResource.FontHeight));
                currentWidth += info.width + fontResource.FontHeight / 10;
            }
            // move to center
            for (int i = 0; i < content.Length; i++)
            {
                GlyphPosition position = array[i];

                position.leftUp.x -= currentWidth / 2.0f;
                //position.leftUp.x /= currentWidth / factor;
                position.leftDown.x -= currentWidth / 2.0f;
                //position.leftDown.x /= currentWidth / factor;
                position.rightUp.x -= currentWidth / 2.0f;
                //position.rightUp.x /= currentWidth / factor;
                position.rightDown.x -= currentWidth / 2.0f;
                //position.rightDown.x /= currentWidth / factor;
                position.leftUp.y    -= (currentHeight + fontResource.FontHeight) / 2.0f;
                position.leftDown.y  -= (currentHeight + fontResource.FontHeight) / 2.0f;
                position.rightUp.y   -= (currentHeight + fontResource.FontHeight) / 2.0f;
                position.rightDown.y -= (currentHeight + fontResource.FontHeight) / 2.0f;

                position.leftUp.x    /= (currentHeight + fontResource.FontHeight);
                position.leftDown.x  /= (currentHeight + fontResource.FontHeight);
                position.rightUp.x   /= (currentHeight + fontResource.FontHeight);
                position.rightDown.x /= (currentHeight + fontResource.FontHeight);
                position.leftUp.y    /= (currentHeight + fontResource.FontHeight);
                position.leftDown.y  /= (currentHeight + fontResource.FontHeight);
                position.rightUp.y   /= (currentHeight + fontResource.FontHeight);
                position.rightDown.y /= (currentHeight + fontResource.FontHeight);
                array[i]              = position;
            }
            OpenGL.UnmapBuffer(BufferTarget.ArrayBuffer);
            OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
示例#6
0
        private unsafe void SetupGlyphPositions(string content, FontResource fontResource)
        {
            FullDictionary<char, CharacterInfo> charInfoDict = fontResource.CharInfoDict;
            OpenGL.BindBuffer(BufferTarget.ArrayBuffer, this.model.positionBufferPtr.BufferId);
            IntPtr pointer = OpenGL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.ReadWrite);
            var array = (TextModel.GlyphPosition*)pointer.ToPointer();
            int currentWidth = 0; int currentHeight = 0;
            /*
             * 0     3  4     7 8     11 12   15
             * -------  ------- -------  -------
             * |     |  |     | |     |  |     |
             * |     |  |     | |     |  |     |
             * |     |  |     | |     |  |     |
             * -------  ------- -------  -------
             * 1     2  5     6 9     10 13   14
             */
            for (int i = 0; i < content.Length; i++)
            {
                char ch = content[i];
                CharacterInfo info = charInfoDict[ch];
                array[i] = new TextModel.GlyphPosition(
                    new vec2(currentWidth, currentHeight + fontResource.FontHeight),
                    new vec2(currentWidth, currentHeight),
                    new vec2(currentWidth + info.width, currentHeight),
                    new vec2(currentWidth + info.width, currentHeight + fontResource.FontHeight));
                currentWidth += info.width + fontResource.FontHeight / 10;
            }
            // move to center
            for (int i = 0; i < content.Length; i++)
            {
                TextModel.GlyphPosition position = array[i];

                position.leftUp.x -= currentWidth / 2.0f;
                //position.leftUp.x /= currentWidth / factor;
                position.leftDown.x -= currentWidth / 2.0f;
                //position.leftDown.x /= currentWidth / factor;
                position.rightUp.x -= currentWidth / 2.0f;
                //position.rightUp.x /= currentWidth / factor;
                position.rightDown.x -= currentWidth / 2.0f;
                //position.rightDown.x /= currentWidth / factor;
                position.leftUp.y -= (currentHeight + fontResource.FontHeight) / 2.0f;
                position.leftDown.y -= (currentHeight + fontResource.FontHeight) / 2.0f;
                position.rightUp.y -= (currentHeight + fontResource.FontHeight) / 2.0f;
                position.rightDown.y -= (currentHeight + fontResource.FontHeight) / 2.0f;

                position.leftUp.x /= (currentHeight + fontResource.FontHeight);
                position.leftDown.x /= (currentHeight + fontResource.FontHeight);
                position.rightUp.x /= (currentHeight + fontResource.FontHeight);
                position.rightDown.x /= (currentHeight + fontResource.FontHeight);
                position.leftUp.y /= (currentHeight + fontResource.FontHeight);
                position.leftDown.y /= (currentHeight + fontResource.FontHeight);
                position.rightUp.y /= (currentHeight + fontResource.FontHeight);
                position.rightDown.y /= (currentHeight + fontResource.FontHeight);
                array[i] = position;
            }
            OpenGL.UnmapBuffer(BufferTarget.ArrayBuffer);
            OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
示例#7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="ttfFilename">".ttf", or ".otf"</param>
 /// <param name="pixelSize">The desired size of the font, in pixels.</param>
 /// <returns></returns>
 public static FontResource Load(string ttfFilename,
                                 char firstChar, char lastChar, int pixelSize = 32)
 {
     using (FileStream stream = File.OpenRead(ttfFilename))
     {
         FontResource fontResource = Load(stream, firstChar, lastChar, pixelSize);
         return(fontResource);
     }
 }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ttfFilename">".ttf", or ".otf"</param>
        /// <param name="pixelSize">The desired size of the font, in pixels.</param>
        /// <returns></returns>
        public static FontResource Load(Stream stream,
                                        string content, int pixelSize = 32)
        {
            InitStandardWidths();

            var targets = (from item in content select item).Distinct();

            FontResource fontResource = LoadFromSomeChars(stream, pixelSize, targets);

            return(fontResource);
        }
示例#9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ttfFilename">".ttf", or ".otf"</param>
        /// <param name="pixelSize">The desired size of the font, in pixels.</param>
        /// <returns></returns>
        public static FontResource Load(string ttfFilename,
                                        string content, int pixelSize = 32)
        {
            InitStandardWidths();

            var targets = (from item in content select item).Distinct();

            using (FileStream stream = File.OpenRead(ttfFilename))
            {
                FontResource fontResource = LoadFromSomeChars(stream, pixelSize, targets);

                return(fontResource);
            }
        }
示例#10
0
        public FontResource Load(string filename, string config)
        {
            var result = new FontResource();

            var bitmap = new Bitmap(filename);

            result.InitTexture(bitmap);
            bitmap.Dispose();

            XElement xElement = XElement.Load(config, LoadOptions.None);

            result.InitConfig(xElement);

            return(result);
        }
示例#11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream">".ttf", or ".otf"</param>
        /// <param name="pixelSize">The desired size of the font, in pixels.</param>
        /// <returns></returns>
        private static FontResource Load(Stream stream,
                                         char firstChar, char lastChar, int pixelSize = 32)
        {
            InitStandardWidths();

            int count    = lastChar - firstChar + 1;
            int maxWidth = GetMaxWidth(pixelSize, count);

            var fontResource = new FontResource();

            fontResource.FontHeight = pixelSize + yInterval;
            var dict = new FullDictionary <char, CharacterInfo>(CharacterInfo.Default);

            fontResource.CharInfoDict = dict;
            var      bitmap = new Bitmap(maxWidth, maxWidth, PixelFormat.Format24bppRgb);
            int      currentX = 0, currentY = 0;
            Graphics g = Graphics.FromImage(bitmap);

            /*
             * this.FontHeight = int.Parse(config.Attribute(strFontHeight).Value);
             * this.CharInfoDict = CharacterInfoDictHelper.Parse(
             *  config.Element(CharacterInfoDictHelper.strCharacterInfoDict));
             */
            //using (var file = File.OpenRead(ttfFilename))
            {
                var typeface = new FontFace(stream);

                for (char c = firstChar; c <= lastChar; c++)
                {
                    BlitCharacter(pixelSize, maxWidth, dict, ref currentX, ref currentY, g, typeface, c);

                    if (c == char.MaxValue)
                    {
                        break;
                    }
                }
            }

            g.Dispose();
            Bitmap finalBitmap = ShortenBitmap(bitmap, maxWidth, currentY + yInterval + pixelSize + (pixelSize / 10 > 1 ? pixelSize / 10 : 1));

            bitmap.Dispose();
            fontResource.InitTexture(finalBitmap);
            finalBitmap.Dispose();

            return(fontResource);
        }
示例#12
0
        unsafe private void SetupGlyphTexCoord(string content, FontResource fontResource)
        {
            FullDictionary <char, CharacterInfo> charInfoDict = fontResource.CharInfoDict;

            OpenGL.BindBuffer(BufferTarget.ArrayBuffer, this.model.uvBufferPtr.BufferId);
            IntPtr pointer = OpenGL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.WriteOnly);
            var    array   = (GlyphTexCoord *)pointer.ToPointer();
            int    width   = fontResource.TextureSize.Width;
            int    height  = fontResource.TextureSize.Height;

            /*
             * 0     3  4     6 8     11 12   15
             * -------  ------- -------  -------
             * |     |  |     | |     |  |     |
             * |     |  |     | |     |  |     |
             * |     |  |     | |     |  |     |
             * -------  ------- -------  -------
             * 1     2  5     6 9     10 13   14
             */
            for (int i = 0; i < content.Length; i++)
            {
                char          ch     = content[i];
                CharacterInfo info   = fontResource.CharInfoDict[ch];
                const int     shrimp = 0;
                array[i] = new GlyphTexCoord(
                    //new vec2(0, 0),
                    //new vec2(0, 1),
                    //new vec2(1, 1),
                    //new vec2(1, 0)
                    new vec2((float)(info.xoffset + shrimp) / (float)width, (float)(info.yoffset) / (float)height),
                    new vec2((float)(info.xoffset + shrimp) / (float)width, (float)(info.yoffset + info.height) / (float)height),
                    new vec2((float)(info.xoffset - shrimp + info.width) / (float)width, (float)(info.yoffset + info.height) / (float)height),
                    new vec2((float)(info.xoffset - shrimp + info.width) / (float)width, (float)(info.yoffset) / (float)height)
                    );
            }
            OpenGL.UnmapBuffer(BufferTarget.ArrayBuffer);
            OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
示例#13
0
        private static FontResource LoadFromSomeChars(Stream stream, int pixelSize, IEnumerable <char> targets)
        {
            FontResource fontResource;

            int count    = targets.Count();
            int maxWidth = GetMaxWidth(pixelSize, count);

            fontResource            = new FontResource();
            fontResource.FontHeight = pixelSize + yInterval;
            var dict = new FullDictionary <char, CharacterInfo>(CharacterInfo.Default);

            fontResource.CharInfoDict = dict;
            var      bitmap = new Bitmap(maxWidth, maxWidth, PixelFormat.Format24bppRgb);
            int      currentX = 0, currentY = 0;
            Graphics g = Graphics.FromImage(bitmap);

            /*
             * this.FontHeight = int.Parse(config.Attribute(strFontHeight).Value);
             * this.CharInfoDict = CharacterInfoDictHelper.Parse(
             *  config.Element(CharacterInfoDictHelper.strCharacterInfoDict));
             */
            {
                var typeface = new FontFace(stream);

                foreach (char c in targets)
                {
                    BlitCharacter(pixelSize, maxWidth, dict, ref currentX, ref currentY, g, typeface, c);
                }
            }

            g.Dispose();
            Bitmap finalBitmap = ShortenBitmap(bitmap, maxWidth, currentY + yInterval + pixelSize + (pixelSize / 10 > 1 ? pixelSize / 10 : 1));

            bitmap.Dispose();
            fontResource.InitTexture(finalBitmap);
            finalBitmap.Dispose();
            return(fontResource);
        }
示例#14
0
 private unsafe void SetupGlyphTexCoord(string content, FontResource fontResource)
 {
     FullDictionary<char, CharacterInfo> charInfoDict = fontResource.CharInfoDict;
     OpenGL.BindBuffer(BufferTarget.ArrayBuffer, this.model.uvBufferPtr.BufferId);
     IntPtr pointer = OpenGL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.WriteOnly);
     var array = (TextModel.GlyphTexCoord*)pointer.ToPointer();
     int width = fontResource.TextureSize.Width;
     int height = fontResource.TextureSize.Height;
     /*
      * 0     3  4     6 8     11 12   15
      * -------  ------- -------  -------
      * |     |  |     | |     |  |     |
      * |     |  |     | |     |  |     |
      * |     |  |     | |     |  |     |
      * -------  ------- -------  -------
      * 1     2  5     6 9     10 13   14
      */
     for (int i = 0; i < content.Length; i++)
     {
         char ch = content[i];
         CharacterInfo info = fontResource.CharInfoDict[ch];
         const int shrimp = 0;
         array[i] = new TextModel.GlyphTexCoord(
             //new vec2(0, 0),
             //new vec2(0, 1),
             //new vec2(1, 1),
             //new vec2(1, 0)
             new vec2((float)(info.xoffset + shrimp) / (float)width, (float)(info.yoffset) / (float)height),
             new vec2((float)(info.xoffset + shrimp) / (float)width, (float)(info.yoffset + info.height) / (float)height),
             new vec2((float)(info.xoffset - shrimp + info.width) / (float)width, (float)(info.yoffset + info.height) / (float)height),
             new vec2((float)(info.xoffset - shrimp + info.width) / (float)width, (float)(info.yoffset) / (float)height)
             );
     }
     OpenGL.UnmapBuffer(BufferTarget.ArrayBuffer);
     OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0);
 }