private void IterateString(string text, STRVector position, bool draw, float scale, STRColor color, CoordinateType coordinateType, out StringMetrics metrics)
        {
            metrics = new StringMetrics();
            STRVector startPosition = position;
            float     scalY         = coordinateType == SpriteTextRenderer.CoordinateType.SNorm ? -1 : 1;

            string visualText = NBidi.NBidi.LogicalToVisual(text);

            int[] codePoints = Helpers.ConvertToCodePointArray(visualText);

            foreach (int c in codePoints)
            {
                var charDesc    = GetCharDescription(c);
                var charMetrics = charDesc.ToStringMetrics(position, scale, scale * scalY);
                if (draw)
                {
                    if (charMetrics.FullRectSize.X != 0 && charMetrics.FullRectSize.Y != 0)
                    {
                        float posY = position.Y - scalY * charMetrics.OverhangTop;
                        float posX = position.X - charMetrics.OverhangLeft;
                        Sprite.Draw(charDesc.TableDescription.SRV, new STRVector(posX, posY), charMetrics.FullRectSize, STRVector.Zero, 0, charDesc.TexCoordsStart, charDesc.TexCoordsSize, color, coordinateType);
                    }
                }

                metrics.Merge(charMetrics);

                position.X += charMetrics.Size.X;

                //Break newlines
                if (c == '\r')
                {
                    position.X = metrics.TopLeft.X;
                }

                if (c == '\n')
                {
                    position.Y = metrics.BottomRight.Y - charMetrics.Size.Y / 2;
                }
            }
        }
Beispiel #2
0
        private void IterateString(string text, Vector2 position, bool draw, float scale, Color4 color, CoordinateType coordinateType, out StringMetrics metrics)
        {
            metrics = new StringMetrics();
            Vector2 startPosition = position;
            float   scalY         = coordinateType == SpriteTextRenderer.CoordinateType.SNorm ? -1 : 1;

            foreach (char c in text)
            {
                var charDesc    = GetCharDescription(c);
                var charMetrics = charDesc.ToStringMetrics(position, scale, scale * scalY);
                if (draw)
                {
                    if (charMetrics.FullRectSize.X != 0 && charMetrics.FullRectSize.Y != 0)
                    {
                        float posY = position.Y - scalY * charMetrics.OverhangTop;
                        float posX = position.X - charMetrics.OverhangLeft;
                        Sprite.Draw(charDesc.TableDescription.SRV, new Vector2(posX, posY), charMetrics.FullRectSize, charDesc.TexCoordsStart, charDesc.TexCoordsSize, color, coordinateType);
                    }
                }

                metrics.Merge(charMetrics);

                position.X += charMetrics.Size.X;

                //Break newlines
                if (c == '\r')
                {
                    position.X = metrics.TopLeft.X;
                }

                if (c == '\n')
                {
                    position.Y = metrics.BottomRight.Y - charMetrics.Size.Y / 2;
                }
            }
        }
Beispiel #3
0
        public StringMetrics DrawString(string text, RectangleF rect, TextAlignment align, float realFontSize, Color4 color, CoordinateType coordinateType)
        {
            //If text is aligned top and left, no adjustment has to be made
            if (align.HasFlag(TextAlignment.Top) && align.HasFlag(TextAlignment.Left))
            {
                return(DrawString(text, new Vector2(rect.X, rect.Y), realFontSize, color, coordinateType));
            }

            text = text.Replace("\r", "");
            var   rawTextMetrics = MeasureString(text, realFontSize, coordinateType);
            var   mMetrics       = MeasureString("m", realFontSize, coordinateType);
            float startY;

            if (align.HasFlag(TextAlignment.Top))
            {
                startY = rect.Top;
            }
            else if (align.HasFlag(TextAlignment.VerticalCenter))
            {
                startY = rect.Top + rect.Height / 2 - rawTextMetrics.Size.Y / 2;
            }
            else             //Bottom
            {
                startY = rect.Bottom - rawTextMetrics.Size.Y;
            }

            var totalMetrics = new StringMetrics();

            //break text into lines
            var lines = text.Split('\n');

            foreach (var line in lines)
            {
                float startX;
                if (align.HasFlag(TextAlignment.Left))
                {
                    startX = rect.X;
                }
                else
                {
                    var lineMetrics = MeasureString(line, realFontSize, coordinateType);
                    if (align.HasFlag(TextAlignment.HorizontalCenter))
                    {
                        startX = rect.X + rect.Width / 2 - lineMetrics.Size.X / 2;
                    }
                    else                     //Right
                    {
                        startX = rect.Right - lineMetrics.Size.X;
                    }
                }

                var   lineMetrics2 = DrawString(line, new Vector2(startX, startY), realFontSize, color, coordinateType);
                float lineHeight;
                if (mMetrics.Size.Y < 0)
                {
                    lineHeight = Math.Min(lineMetrics2.Size.Y, mMetrics.Size.Y);
                }
                else
                {
                    lineHeight = Math.Max(lineMetrics2.Size.Y, mMetrics.Size.Y);
                }
                startY += lineHeight;
                totalMetrics.Merge(lineMetrics2);
            }

            return(totalMetrics);
        }