Esempio n. 1
0
        public static void DrawString(Color clr, string s, float size, Point p)
        {
            var lineHeight  = 1.0f;
            var spacesInTab = 4;

            var xOff = 0f;
            var yOff = 0f;

            foreach (var c in s)
            {
                var gl   = _ttf.Glyfs[(byte)c];
                var mesh = GlyfCache.GetGlyfMesh(gl);
                //we need to scale things down
                var maxWidth  = _ttf.Header.Xmax + 0.0000000001f;
                var maxHeight = _ttf.Header.Ymax + 0.0000000001f;

                if (char.IsWhiteSpace(c))
                {
                    if (c == '\n')
                    {
                        yOff += (size - (size * (_ttf.HorizontalHeaderTable.lineGap / maxHeight)) * lineHeight);
                        xOff  = 0;
                    }
                    else if (c == '\t')
                    {
                        xOff += size * spacesInTab;
                    }
                    else
                    {
                        xOff += size;
                    }

                    continue;
                }


                var scaleFactorX = 1f / maxWidth;
                var scaleFactorY = 1f / maxHeight;


                var scaleX = size * scaleFactorX;
                var scaleY = size * scaleFactorY;

                var trans = new Matrix4f().InitIdentity().InitTranslation(xOff + p.X, yOff + p.Y + (size), 0);
                var scale = new Matrix4f().InitIdentity().InitScale(scaleX, -scaleY, 0);


                _rectShader.Apply();
                _rectShader.SetUniform("mvp", _orthMat * trans * scale);
                _rectShader.SetUniform("uColor", clr);


                xOff += size * (_ttf.longHorMetrics[0].advanceWidth / maxWidth);

                mesh.Draw();
            }
        }
Esempio n. 2
0
        public static Size MesureString(string s, float size)
        {
            var lineHeight  = 1.0f;
            var spacesInTab = 4;

            var xOff = 0f;
            var yOff = 0f;

            foreach (var c in s)
            {
                var gl   = _ttf.Glyfs[(byte)c];
                var mesh = GlyfCache.GetGlyfMesh(gl);
                //we need to scale things down
                var maxWidth  = _ttf.Header.Xmax + 0.0000000001f;
                var maxHeight = _ttf.Header.Ymax + 0.0000000001f;

                if (char.IsWhiteSpace(c))
                {
                    if (c == '\n')
                    {
                        yOff += (size - (size * (_ttf.HorizontalHeaderTable.lineGap / maxHeight)) * lineHeight);
                        xOff  = 0;
                    }
                    else if (c == '\t')
                    {
                        xOff += size * spacesInTab;
                    }
                    else
                    {
                        xOff += size;
                    }

                    continue;
                }

                xOff += size * (_ttf.longHorMetrics[0].advanceWidth / maxWidth);
            }

            return(new Size(xOff, yOff));
        }