コード例 #1
0
ファイル: GLDraw.cs プロジェクト: rainrcn/GLGUI
        public static void Text(GLFontText processedText, ref Rectangle rect, ref Color4 color)
        {
            if (color.A == 0.0f)
            {
                return;
            }

            int w = Math.Min(rect.Width, ScissorRect.Width - rect.X);
            int h = Math.Min(rect.Height, ScissorRect.Height - rect.Y);

            if (w > 0 && h > 0)
            {
                GL.Scissor(ScissorRect.X + rect.X, CurrentGui.Outer.Height - (ScissorRect.Y + rect.Y + h), w, h);
                GL.PushMatrix();
                switch (processedText.alignment)
                {
                case GLFontAlignment.Left:
                case GLFontAlignment.Justify:
                    GL.Translate(ControlRect.X + rect.X, ControlRect.Y + rect.Y, 0.0f);
                    break;

                case GLFontAlignment.Centre:
                    GL.Translate(ControlRect.X + rect.X + rect.Width / 2, ControlRect.Y + rect.Y, 0.0f);
                    break;

                case GLFontAlignment.Right:
                    GL.Translate(ControlRect.X + rect.X + rect.Width, ControlRect.Y + rect.Y, 0.0f);
                    break;
                }
                GL.Color4(color);
                for (int i = 0; i < processedText.VertexBuffers.Length; i++)
                {
                    processedText.VertexBuffers[i].Draw();
                }
                GL.PopMatrix();
            }
        }
コード例 #2
0
        public SizeF ProcessText(GLFontText processedText, string text, SizeF maxSize, GLFontAlignment alignment = GLFontAlignment.Left)
        {
            if (processedText == null)
            {
                throw new ArgumentNullException("processedText");
            }

            var nodeList = new GLFontTextNodeList(text);

            nodeList.MeasureNodes(fontData, Options);

            if (!Options.WordWrap)
            {
                //we "crumble" words that are two long so that that can be split up
                var nodesToCrumble = new List <GLFontTextNode>();
                foreach (GLFontTextNode node in nodeList)
                {
                    if (node.Length >= maxSize.Width && node.Type == GLFontTextNodeType.Word)
                    {
                        nodesToCrumble.Add(node);
                    }
                }

                if (nodesToCrumble.Count > 0)
                {
                    foreach (var node in nodesToCrumble)
                    {
                        nodeList.Crumble(node, 1);
                    }

                    //need to measure crumbled words
                    nodeList.MeasureNodes(fontData, Options);
                }
            }

            if (processedText.VertexBuffers == null)
            {
                processedText.VertexBuffers = new GLFontVertexBuffer[fontData.Pages.Length];
                for (int i = 0; i < processedText.VertexBuffers.Length; i++)
                {
                    processedText.VertexBuffers[i] = new GLFontVertexBuffer(fontData.Pages[i].TextureID);
                }
            }
            if (processedText.VertexBuffers[0].TextureID != fontData.Pages[0].TextureID)
            {
                for (int i = 0; i < processedText.VertexBuffers.Length; i++)
                {
                    processedText.VertexBuffers[i].TextureID = fontData.Pages[i].TextureID;
                }
            }
            processedText.textNodeList = nodeList;
            processedText.maxSize      = maxSize;
            processedText.alignment    = alignment;

            foreach (var buffer in processedText.VertexBuffers)
            {
                buffer.Reset();
            }
            SizeF size = PrintOrMeasure(processedText.VertexBuffers, processedText, false);

            foreach (var buffer in processedText.VertexBuffers)
            {
                buffer.Load();
            }
            return(size);
        }
コード例 #3
0
 public SizeF ProcessText(GLFontText processedText, string text, GLFontAlignment alignment = GLFontAlignment.Left)
 {
     return(ProcessText(processedText, text, new SizeF(float.MaxValue, float.MaxValue), alignment));
 }
コード例 #4
0
        private SizeF PrintOrMeasure(GLFontVertexBuffer[] vbos, GLFontText processedText, bool measureOnly)
        {
            // init values we'll return
            float maxMeasuredWidth = 0f;

            float xOffset = 0f;
            float yOffset = 0f;

            lineSpacingCache         = LineSpacing;
            isMonospacingActiveCache = IsMonospacingActive;
            monoSpaceWidthCache      = MonoSpaceWidth;
            float maxWidth  = processedText.maxSize.Width;
            var   alignment = processedText.alignment;
            var   nodeList  = processedText.textNodeList;

            for (GLFontTextNode node = nodeList.Head; node != null; node = node.Next)
            {
                node.LengthTweak = 0f;  //reset tweaks
            }
            if (alignment == GLFontAlignment.Right)
            {
                xOffset -= (float)Math.Ceiling(TextNodeLineLength(nodeList.Head, maxWidth) - maxWidth);
            }
            else if (alignment == GLFontAlignment.Centre)
            {
                xOffset -= (float)Math.Ceiling(0.5f * TextNodeLineLength(nodeList.Head, maxWidth));
            }
            else if (alignment == GLFontAlignment.Justify)
            {
                JustifyLine(nodeList.Head, maxWidth);
            }

            bool  atLeastOneNodeCosumedOnLine = false;
            float length = 0f;

            for (GLFontTextNode node = nodeList.Head; node != null; node = node.Next)
            {
                bool newLine = false;

                if (node.Type == GLFontTextNodeType.LineBreak)
                {
                    newLine = true;
                }
                else
                {
                    if (Options.WordWrap && SkipTrailingSpace(node, length, maxWidth) && atLeastOneNodeCosumedOnLine)
                    {
                        newLine = true;
                    }
                    else if (length + node.ModifiedLength <= maxWidth || !atLeastOneNodeCosumedOnLine)
                    {
                        atLeastOneNodeCosumedOnLine = true;

                        if (!measureOnly)
                        {
                            RenderWord(vbos, xOffset + length, yOffset, node);
                        }
                        length += node.ModifiedLength;

                        maxMeasuredWidth = Math.Max(length, maxMeasuredWidth);
                    }
                    else if (Options.WordWrap)
                    {
                        newLine = true;
                        if (node.Previous != null)
                        {
                            node = node.Previous;
                        }
                    }
                    else
                    {
                        continue; // continue so we still read line breaks even if reached max width
                    }
                }

                if (newLine)
                {
                    if (yOffset + lineSpacingCache >= processedText.maxSize.Height)
                    {
                        break;
                    }

                    yOffset += lineSpacingCache;
                    xOffset  = 0f;
                    length   = 0f;
                    atLeastOneNodeCosumedOnLine = false;

                    if (node.Next != null)
                    {
                        if (alignment == GLFontAlignment.Right)
                        {
                            xOffset -= (float)Math.Ceiling(TextNodeLineLength(node.Next, maxWidth) - maxWidth);
                        }
                        else if (alignment == GLFontAlignment.Centre)
                        {
                            xOffset -= (float)Math.Ceiling(0.5f * TextNodeLineLength(node.Next, maxWidth));
                        }
                        else if (alignment == GLFontAlignment.Justify)
                        {
                            JustifyLine(node.Next, maxWidth);
                        }
                    }
                }
            }

            return(new SizeF(maxMeasuredWidth, yOffset + (nodeList.Head == null ? 0 : lineSpacingCache)));
        }
コード例 #5
0
        public GLFontTextPosition GetTextPosition(GLFontText processedText, GLFontTextPosition position)
        {
            float maxMeasuredWidth = 0f;

            float xOffset = 0f;
            float yOffset = 0f;

            int character = 0;

            lineSpacingCache         = LineSpacing;
            isMonospacingActiveCache = IsMonospacingActive;
            monoSpaceWidthCache      = MonoSpaceWidth;
            float maxWidth  = processedText.maxSize.Width;
            var   alignment = processedText.alignment;
            var   nodeList  = processedText.textNodeList;

            for (GLFontTextNode node = nodeList.Head; node != null; node = node.Next)
            {
                node.LengthTweak = 0f;  //reset tweaks
            }
            if (alignment == GLFontAlignment.Right)
            {
                xOffset -= (float)Math.Ceiling(TextNodeLineLength(nodeList.Head, maxWidth) - maxWidth);
            }
            else if (alignment == GLFontAlignment.Centre)
            {
                xOffset -= (float)Math.Ceiling(0.5f * TextNodeLineLength(nodeList.Head, maxWidth));
            }
            else if (alignment == GLFontAlignment.Justify)
            {
                JustifyLine(nodeList.Head, maxWidth);
            }

            bool  atLeastOneNodeCosumedOnLine = false;
            float length = 0f;

            for (GLFontTextNode node = nodeList.Head; node != null; node = node.Next)
            {
                bool newLine = false;

                if (node.Type == GLFontTextNodeType.LineBreak)
                {
                    newLine = true;
                    if (character == position.Index)
                    {
                        return new GLFontTextPosition()
                               {
                                   Index = character, Position = new Vector2(xOffset + length, yOffset)
                               }
                    }
                    ;
                    character++;
                }
                else
                {
                    if (Options.WordWrap && SkipTrailingSpace(node, length, maxWidth) && atLeastOneNodeCosumedOnLine)
                    {
                        newLine = true;

                        if (character == position.Index)
                        {
                            return new GLFontTextPosition()
                                   {
                                       Index = character, Position = new Vector2(xOffset + length, yOffset)
                                   }
                        }
                        ;
                        character++;
                    }
                    else if (length + node.ModifiedLength <= maxWidth || !atLeastOneNodeCosumedOnLine)
                    {
                        atLeastOneNodeCosumedOnLine = true;

                        Vector2 p;
                        if (GetWordPosition(xOffset + length, yOffset, node, position, ref character, out p))
                        {
                            return new GLFontTextPosition()
                                   {
                                       Index = character, Position = p
                                   }
                        }
                        ;

                        length += node.ModifiedLength;

                        maxMeasuredWidth = Math.Max(length, maxMeasuredWidth);
                    }
                    else if (Options.WordWrap)
                    {
                        newLine = true;
                        if (node.Previous != null)
                        {
                            node = node.Previous;
                        }
                    }
                    else
                    {
                        continue; // continue so we still read line breaks even if reached max width
                    }
                }

                if (newLine)
                {
                    if (yOffset + lineSpacingCache >= processedText.maxSize.Height)
                    {
                        break;
                    }

                    if ((long)(yOffset / lineSpacingCache) == (long)(position.Position.Y / lineSpacingCache))
                    {
                        return new GLFontTextPosition()
                               {
                                   Index = character - 1, Position = new Vector2(xOffset + length, yOffset)
                               }
                    }
                    ;

                    yOffset += lineSpacingCache;
                    xOffset  = 0f;
                    length   = 0f;
                    atLeastOneNodeCosumedOnLine = false;

                    if (node.Next != null)
                    {
                        if (alignment == GLFontAlignment.Right)
                        {
                            xOffset -= (float)Math.Ceiling(TextNodeLineLength(node.Next, maxWidth) - maxWidth);
                        }
                        else if (alignment == GLFontAlignment.Centre)
                        {
                            xOffset -= (float)Math.Ceiling(0.5f * TextNodeLineLength(node.Next, maxWidth));
                        }
                        else if (alignment == GLFontAlignment.Justify)
                        {
                            JustifyLine(node.Next, maxWidth);
                        }
                    }
                }
            }

            return(new GLFontTextPosition()
            {
                Index = character, Position = new Vector2(xOffset + length, yOffset)
            });
        }
コード例 #6
0
ファイル: GLDraw.cs プロジェクト: rainrcn/GLGUI
 public static void Text(GLFontText processedText, Rectangle rect, ref Color4 color)
 {
     Text(processedText, ref rect, ref color);
 }