Esempio n. 1
0
        public float DrawText(IFontStashRenderer batch, float x, float y, StringBuilder str, Color[] glyphColors, Vector2 scale, float depth = 0.0f)
        {
            if (str == null || str.Length == 0)
            {
                return(0.0f);
            }

            float ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            float originX = 0.0f;
            float originY = 0.0f;

            originY += ascent;

            FontGlyph prevGlyph = null;
            var       pos       = 0;
            var       q         = new FontGlyphSquad();

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    ++pos;
                    continue;
                }

                var glyph = GetGlyph(codepoint, false);
                if (glyph == null)
                {
                    ++pos;
                    continue;
                }

                GetQuad(glyph, prevGlyph, scale, ref originX, ref originY, ref q);
                if (!glyph.IsEmpty)
                {
                    var destRect   = new Rectangle((int)(x + q.X0), (int)(y + q.Y0), (int)(q.X1 - q.X0), (int)(q.Y1 - q.Y0));
                    var sourceRect = new Rectangle((int)q.S0, (int)q.T0, (int)(q.S1 - q.S0), (int)(q.T1 - q.T0));

                    batch.Draw(glyph.Atlas.Texture,
                               destRect,
                               sourceRect,
                               glyphColors[pos],
                               depth);
                }

                prevGlyph = glyph;
                ++pos;
            }

            return(x);
        }
        /// <summary>
        /// Draws a text
        /// </summary>
        /// <param name="renderer">A renderer.</param>
        /// <param name="text">The text which will be drawn.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="color">A color mask.</param>
        /// <param name="rotation">A rotation of this text in radians.</param>
        /// <param name="origin">Center of the rotation.</param>
        /// <param name="scale">A scaling of this text.</param>
        /// <param name="layerDepth">A depth of the layer of this string.</param>
        public float DrawText(IFontStashRenderer renderer, StringBuilder text, Vector2 position, Color color,
                              Vector2 scale, float rotation, Vector2 origin, float layerDepth = 0.0f)
        {
            if (text == null || text.Length == 0)
            {
                return(0.0f);
            }

            float ascent, lineHeight;

            PreDraw(text, out ascent, out lineHeight);

            float originX = 0.0f;
            float originY = 0.0f;

            originY += ascent;

            FontGlyph prevGlyph = null;
            var       q         = new FontGlyphSquad();

            for (int i = 0; i < text.Length; i += StringBuilderIsSurrogatePair(text, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(text, i);

                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(codepoint, false);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, scale, ref originX, ref originY, ref q);
                if (!glyph.IsEmpty)
                {
                    var sourceRect = new Rectangle((int)q.S0, (int)q.T0, (int)(q.S1 - q.S0), (int)(q.T1 - q.T0));

                    renderer.Draw(glyph.Texture,
                                  position,
                                  sourceRect,
                                  color,
                                  rotation,
                                  origin - q.Offset,
                                  scale,
                                  layerDepth);
                }

                prevGlyph = glyph;
            }

            return(position.X);
        }
Esempio n. 3
0
        public List <Rectangle> GetGlyphRects(StringBuilder str, Vector2 position, Vector2 scale)
        {
            List <Rectangle> Rects = new List <Rectangle>();

            if (str == null || str.Length == 0)
            {
                return(Rects);
            }

            int ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            var x = position.X;
            var y = position.Y;

            y += ascent;

            float minx, maxx, miny, maxy;

            minx = maxx = x;
            miny = maxy = y;
            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, ref x, y, ref q);

                var rect = ApplyScale(new Rectangle((int)q.X0, (int)q.Y0, (int)(q.X1 - q.X0), (int)(q.Y1 - q.Y0)), scale);
                Rects.Add(rect);
                prevGlyph = glyph;
            }

            return(Rects);
        }
Esempio n. 4
0
        public List <Rectangle> GetGlyphRects(string str, Vector2 position, Vector2 scale)
        {
            List <Rectangle> Rects = new List <Rectangle>();

            if (string.IsNullOrEmpty(str))
            {
                return(Rects);
            }

            scale /= RenderFontSizeMultiplicator;

            int ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            var x = position.X;
            var y = position.Y;

            y += ascent * scale.Y;

            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, ref x, y, ref q);

                var rect = ApplyScale(new Rectangle((int)q.X0, (int)q.Y0, (int)(q.X1 - q.X0), (int)(q.Y1 - q.Y0)), scale);
                Rects.Add(rect);
                prevGlyph = glyph;
            }

            return(Rects);
        }
Esempio n. 5
0
        public List <Rectangle> GetGlyphRects(float x, float y, StringBuilder str)
        {
            List <Rectangle> Rects = new List <Rectangle>();

            if (str == null || str.Length == 0)
            {
                return(Rects);
            }

            Dictionary <int, FontGlyph> glyphs;
            float ascent, lineHeight;

            PreDraw(str, out glyphs, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            y += ascent;

            float minx, maxx, miny, maxy;

            minx = maxx = x;
            miny = maxy = y;
            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, glyphs, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);

                Rects.Add(new Rectangle((int)q.X0, (int)q.Y0, (int)(q.X1 - q.X0), (int)(q.Y1 - q.Y0)));
                prevGlyph = glyph;
            }

            return(Rects);
        }
Esempio n. 6
0
        public List <Rectangle> GetGlyphRects(float x, float y, string str, Vector2 scale)
        {
            List <Rectangle> Rects = new List <Rectangle>();

            if (string.IsNullOrEmpty(str))
            {
                return(Rects);
            }

            float ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            y += ascent;

            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(codepoint, true);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, scale, ref x, ref y, ref q);

                Rects.Add(new Rectangle((int)q.X0, (int)q.Y0, (int)(q.X1 - q.X0), (int)(q.Y1 - q.Y0)));
                prevGlyph = glyph;
            }

            return(Rects);
        }
Esempio n. 7
0
        public float TextBounds(float x, float y, string str, ref Bounds bounds)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0.0f);
            }

            var glyphs = GetGlyphsCollection(FontSize);

            // Determine ascent and lineHeight from first character
            float ascent = 0, lineHeight = 0;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                var glyph = GetGlyph(null, glyphs, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                ascent     = glyph.Font.Ascent;
                lineHeight = glyph.Font.LineHeight;
                break;
            }


            var   q       = new FontGlyphSquad();
            float startx  = 0;
            float advance = 0;

            y += ascent;

            float minx, maxx, miny, maxy;

            minx   = maxx = x;
            miny   = maxy = y;
            startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, glyphs, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);
                if (q.X0 < minx)
                {
                    minx = q.X0;
                }
                if (x > maxx)
                {
                    maxx = x;
                }
                if (q.Y0 < miny)
                {
                    miny = q.Y0;
                }
                if (q.Y1 > maxy)
                {
                    maxy = q.Y1;
                }

                prevGlyph = glyph;
            }

            advance = x - startx;

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }
Esempio n. 8
0
        public float DrawText(SpriteBatch batch, float x, float y, string str, float depth)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0.0f);
            }

            var glyphs = GetGlyphsCollection(FontSize);

            // Determine ascent and lineHeight from first character
            float ascent = 0, lineHeight = 0;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                var glyph = GetGlyph(batch.GraphicsDevice, glyphs, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                ascent     = glyph.Font.Ascent;
                lineHeight = glyph.Font.LineHeight;
                break;
            }

            var q = new FontGlyphSquad();

            float originX = 0.0f;
            float originY = 0.0f;

            originY += ascent;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(batch.GraphicsDevice, glyphs, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, Spacing, ref originX, ref originY, ref q);

                q.X0 = (int)(q.X0 * Scale.X);
                q.X1 = (int)(q.X1 * Scale.X);
                q.Y0 = (int)(q.Y0 * Scale.Y);
                q.Y1 = (int)(q.Y1 * Scale.Y);

                var destRect = new Rectangle((int)(x + q.X0),
                                             (int)(y + q.Y0),
                                             (int)(q.X1 - q.X0),
                                             (int)(q.Y1 - q.Y0));

                var sourceRect = new Rectangle((int)(q.S0 * _size.X),
                                               (int)(q.T0 * _size.Y),
                                               (int)((q.S1 - q.S0) * _size.X),
                                               (int)((q.T1 - q.T0) * _size.Y));

                batch.Draw(glyph.Atlas.Texture,
                           destRect,
                           sourceRect,
                           Color,
                           0f,
                           Vector2.Zero,
                           SpriteEffects.None,
                           depth);

                prevGlyph = glyph;
            }

            return(x);
        }
        internal virtual void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, Vector2 scale, ref float x, ref float y, ref FontGlyphSquad q)
        {
            float rx = x + glyph.XOffset;
            float ry = y + glyph.YOffset;

            q.X0     = rx * scale.X;
            q.Y0     = ry * scale.Y;
            q.X1     = (rx + glyph.Bounds.Width) * scale.X;
            q.Y1     = (ry + glyph.Bounds.Height) * scale.Y;
            q.S0     = glyph.Bounds.X;
            q.T0     = glyph.Bounds.Y;
            q.S1     = glyph.Bounds.Right;
            q.T1     = glyph.Bounds.Bottom;
            q.Offset = new Vector2(rx, ry);

            x += glyph.XAdvance;
        }
Esempio n. 10
0
        private void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, Vector2 scale, ref float x, ref float y, ref FontGlyphSquad q)
        {
            if (prevGlyph != null)
            {
                float adv = 0;
                if (FontSystem.UseKernings && glyph.Font == prevGlyph.Font)
                {
                    adv = prevGlyph.Font.GetGlyphKernAdvance(prevGlyph.Id, glyph.Id, glyph.Size);
                }

                x += (int)(adv + FontSystem.CharacterSpacing + 0.5f);
            }

            float rx = x + glyph.XOffset;
            float ry = y + glyph.YOffset;

            q.X0 = rx * scale.X;
            q.Y0 = ry * scale.Y;
            q.X1 = (rx + glyph.Bounds.Width) * scale.X;
            q.Y1 = (ry + glyph.Bounds.Height) * scale.Y;
            q.S0 = glyph.Bounds.X;
            q.T0 = glyph.Bounds.Y;
            q.S1 = glyph.Bounds.Right;
            q.T1 = glyph.Bounds.Bottom;

            x += glyph.XAdvance;
        }
Esempio n. 11
0
        public float TextBounds(float x, float y, StringSegment str, ref Bounds bounds)
        {
            var       q              = new FontGlyphSquad();
            FontGlyph glyph          = null;
            var       prevGlyphIndex = -1;
            var       isize          = (int)(Size * 10.0f);
            var       iblur          = (int)BlurValue;
            float     scale          = 0;
            Font      font;
            float     startx  = 0;
            float     advance = 0;
            float     minx    = 0;
            float     miny    = 0;
            float     maxx    = 0;
            float     maxy    = 0;

            if (FontId < 0 || FontId >= _fonts.Count)
            {
                return(0);
            }
            font = _fonts[FontId];
            if (font.Data == null)
            {
                return(0);
            }
            scale  = font._font.fons__tt_getPixelHeightScale(isize / 10.0f);
            y     += GetVertAlign(font, Alignment, isize);
            minx   = maxx = x;
            miny   = maxy = y;
            startx = x;
            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str.String, i + str.Location);
                glyph = GetGlyph(font, codepoint, isize, iblur, false);
                if (glyph != null)
                {
                    GetQuad(font, prevGlyphIndex, glyph, scale, Spacing, ref x, ref y, &q);
                    if (q.X0 < minx)
                    {
                        minx = q.X0;
                    }
                    if (x > maxx)
                    {
                        maxx = x;
                    }
                    if (_params_.IsAlignmentTopLeft)
                    {
                        if (q.Y0 < miny)
                        {
                            miny = q.Y0;
                        }
                        if (q.Y1 > maxy)
                        {
                            maxy = q.Y1;
                        }
                    }
                    else
                    {
                        if (q.Y1 < miny)
                        {
                            miny = q.Y1;
                        }
                        if (q.Y0 > maxy)
                        {
                            maxy = q.Y0;
                        }
                    }
                }

                prevGlyphIndex = glyph != null ? glyph.Index : -1;
            }

            advance = x - startx;
            if ((Alignment & Alignment.Left) != 0)
            {
            }
            else if ((Alignment & Alignment.Right) != 0)
            {
                minx -= advance;
                maxx -= advance;
            }
            else if ((Alignment & Alignment.Center) != 0)
            {
                minx -= advance * 0.5f;
                maxx -= advance * 0.5f;
            }

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }
Esempio n. 12
0
        internal override void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, ref float x, float y, ref FontGlyphSquad q)
        {
            if (prevGlyph != null)
            {
                if (UseKernings)
                {
                    x += GetGlyphKernAdvance(prevGlyph.Codepoint, glyph.Codepoint);
                }

                x += CharacterSpacing;
            }

            base.GetQuad(glyph, prevGlyph, ref x, y, ref q);
        }
Esempio n. 13
0
        public float TextBounds(float x, float y, StringSegment str, ref Bounds bounds)
        {
            var       q              = new FontGlyphSquad();
            FontGlyph glyph          = null;
            var       prevGlyphIndex = -1;
            var       isize          = (int)(Size * 10.0f);
            var       iblur          = (int)BlurValue;
            float     startx         = 0;
            float     advance        = 0;
            float     minx           = 0;
            float     miny           = 0;
            float     maxx           = 0;
            float     maxy           = 0;

            y += GetVertAlign(str, Alignment, isize);

            minx   = maxx = x;
            miny   = maxy = y;
            startx = x;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str.String, i + str.Location);
                var font      = FindFontWithCodepoint(codepoint);

                if (font == null)
                {
                    // Skip this character. TODO: draw a box or something.
                    prevGlyphIndex = -1; // Important so GetQuad() below doesn't use an invalid value next iteration.
                    continue;
                }

                var scale = font._font.fons__tt_getPixelHeightScale(isize / 10.0f);

                glyph = GetGlyph(font, codepoint, isize, iblur, false);

                if (glyph != null)
                {
                    GetQuad(font, prevGlyphIndex, glyph, scale, Spacing, ref x, ref y, &q);
                    if (q.X0 < minx)
                    {
                        minx = q.X0;
                    }
                    if (x > maxx)
                    {
                        maxx = x;
                    }
                    if (_params_.IsAlignmentTopLeft)
                    {
                        if (q.Y0 < miny)
                        {
                            miny = q.Y0;
                        }
                        if (q.Y1 > maxy)
                        {
                            maxy = q.Y1;
                        }
                    }
                    else
                    {
                        if (q.Y1 < miny)
                        {
                            miny = q.Y1;
                        }
                        if (q.Y0 > maxy)
                        {
                            maxy = q.Y0;
                        }
                    }
                }

                prevGlyphIndex = glyph != null ? glyph.Index : -1;
            }

            advance = x - startx;
            if ((Alignment & Alignment.Left) != 0)
            {
            }
            else if ((Alignment & Alignment.Right) != 0)
            {
                minx -= advance;
                maxx -= advance;
            }
            else if ((Alignment & Alignment.Center) != 0)
            {
                minx -= advance * 0.5f;
                maxx -= advance * 0.5f;
            }

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }
Esempio n. 14
0
        public float DrawText(SpriteBatch batch, float x, float y, StringSegment str, float depth)
        {
            if (str.IsNullOrEmpty)
            {
                return(0.0f);
            }

            FontGlyph glyph          = null;
            var       q              = new FontGlyphSquad();
            var       prevGlyphIndex = -1;
            var       isize          = (int)(Size * 10.0f);
            var       iblur          = (int)BlurValue;
            float     width          = 0;

            if ((Alignment & Alignment.Left) != 0)
            {
            }
            else if ((Alignment & Alignment.Right) != 0)
            {
                var bounds = new Bounds();
                width = TextBounds(x, y, str, ref bounds);
                x    -= width;
            }
            else if ((Alignment & Alignment.Center) != 0)
            {
                var bounds = new Bounds();
                width = TextBounds(x, y, str, ref bounds);
                x    -= width * 0.5f;
            }

            float originX = 0.0f;
            float originY = 0.0f;

            originY += GetVertAlign(str, Alignment, isize);
            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str.String, i + str.Location);
                var font      = FindFontWithCodepoint(codepoint);

                if (font == null)
                {
                    // Skip this character. TODO: draw a box or something.
                    prevGlyphIndex = -1; // Important so GetQuad() below doesn't use an invalid value next iteration.
                    continue;
                }

                var scale = font._font.fons__tt_getPixelHeightScale(isize / 10.0f);

                glyph = GetGlyph(font, codepoint, isize, iblur, true);

                if (glyph != null)
                {
                    GetQuad(font, prevGlyphIndex, glyph, scale, Spacing, ref originX, ref originY, &q);
                    if (_vertsNumber + 6 > 1024)
                    {
                        Flush(batch, depth);
                    }

                    q.X0 = (int)(q.X0 * Scale.X);
                    q.X1 = (int)(q.X1 * Scale.X);
                    q.Y0 = (int)(q.Y0 * Scale.Y);
                    q.Y1 = (int)(q.Y1 * Scale.Y);

                    AddVertex(new Rectangle((int)(x + q.X0),
                                            (int)(y + q.Y0),
                                            (int)(q.X1 - q.X0),
                                            (int)(q.Y1 - q.Y0)),
                              new Rectangle((int)(q.S0 * _params_.Width),
                                            (int)(q.T0 * _params_.Height),
                                            (int)((q.S1 - q.S0) * _params_.Width),
                                            (int)((q.T1 - q.T0) * _params_.Height)),
                              Color);
                }

                prevGlyphIndex = glyph != null ? glyph.Index : -1;
            }

            Flush(batch, depth);
            return(x);
        }
Esempio n. 15
0
        /// <summary>
        /// Draws a text
        /// </summary>
        /// <param name="renderer">A renderer.</param>
        /// <param name="text">The text which will be drawn.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="color">A color mask.</param>
        /// <param name="rotation">A rotation of this text in radians.</param>
        /// <param name="origin">Center of the rotation.</param>
        /// <param name="scale">A scaling of this text.</param>
        /// <param name="layerDepth">A depth of the layer of this string.</param>
        public float DrawText(IFontStashRenderer renderer, string text, Vector2 position, Color color,
                              Vector2 scale, float rotation, Vector2 origin, float layerDepth = 0.0f)
        {
#if MONOGAME || FNA || STRIDE
            if (renderer.GraphicsDevice == null)
            {
                throw new ArgumentNullException("renderer.GraphicsDevice can't be null.");
            }
#else
            if (renderer.TextureManager == null)
            {
                throw new ArgumentNullException("renderer.TextureManager can't be null.");
            }
#endif

            if (string.IsNullOrEmpty(text))
            {
                return(0.0f);
            }

            scale /= RenderFontSizeMultiplicator;

            int ascent, lineHeight;
            PreDraw(text, out ascent, out lineHeight);

            float originX = 0.0f;
            float originY = 0.0f;

            originY += ascent;

            FontGlyph prevGlyph = null;
            var       q         = new FontGlyphSquad();
            for (int i = 0; i < text.Length; i += char.IsSurrogatePair(text, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(text, i);
                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    continue;
                }

#if MONOGAME || FNA || STRIDE
                var glyph = GetGlyph(renderer.GraphicsDevice, codepoint);
#else
                var glyph = GetGlyph(renderer.TextureManager, codepoint);
#endif
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, ref originX, originY, ref q);
                if (!glyph.IsEmpty)
                {
                    var sourceRect = new Rectangle((int)q.S0, (int)q.T0, (int)(q.S1 - q.S0), (int)(q.T1 - q.T0));

                    renderer.Draw(glyph.Texture,
                                  position,
                                  sourceRect,
                                  color,
                                  rotation,
                                  origin * (float)RenderFontSizeMultiplicator - q.Offset,
                                  scale,
                                  layerDepth);
                }

                prevGlyph = glyph;
            }

            return(position.X);
        }
Esempio n. 16
0
        protected virtual void InternalTextBounds(StringBuilder str, Vector2 position, ref Bounds bounds)
        {
            if (str == null || str.Length == 0)
            {
                return;
            }

            int ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            var x = position.X;
            var y = position.Y;

            y += ascent;

            float minx, maxx, miny, maxy;

            minx = maxx = x;
            miny = maxy = y;
            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, ref x, y, ref q);
                if (q.X0 < minx)
                {
                    minx = q.X0;
                }
                if (x > maxx)
                {
                    maxx = x;
                }

                if (q.Y0 < miny)
                {
                    miny = q.Y0;
                }
                if (q.Y1 > maxy)
                {
                    maxy = q.Y1;
                }

                prevGlyph = glyph;
            }

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;
        }
Esempio n. 17
0
        internal override void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, ref float x, float y, ref FontGlyphSquad q)
        {
            if (prevGlyph != null)
            {
                var adv = 0;

                var dynamicGlyph     = (DynamicFontGlyph)glyph;
                var dynamicPrevGlyph = (DynamicFontGlyph)prevGlyph;
                if (FontSystem.UseKernings && dynamicGlyph.FontSourceIndex == dynamicPrevGlyph.FontSourceIndex)
                {
                    var key = GetKerningsKey(prevGlyph.Id, dynamicGlyph.Id);
                    if (!Kernings.TryGetValue(key, out adv))
                    {
                        var fontSource = FontSystem.FontSources[dynamicGlyph.FontSourceIndex];
                        adv           = fontSource.GetGlyphKernAdvance(prevGlyph.Id, dynamicGlyph.Id, dynamicGlyph.Size);
                        Kernings[key] = adv;
                    }
                }

                x += adv + FontSystem.CharacterSpacing;
            }

            base.GetQuad(glyph, prevGlyph, ref x, y, ref q);
        }
Esempio n. 18
0
        public float DrawText(SpriteBatch batch, float x, float y, StringBuilder str, Color[] glyphColors, float depth)
        {
            if (str == null || str.Length == 0)
            {
                return(0.0f);
            }

            Dictionary <int, FontGlyph> glyphs;
            float ascent, lineHeight;

            PreDraw(str, out glyphs, out ascent, out lineHeight);

            float originX = 0.0f;
            float originY = 0.0f;

            originY += ascent;

            FontGlyph prevGlyph = null;
            var       pos       = 0;
            var       q         = new FontGlyphSquad();

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    ++pos;
                    continue;
                }

                var glyph = GetGlyph(batch.GraphicsDevice, glyphs, codepoint);
                if (glyph == null)
                {
                    ++pos;
                    continue;
                }

                GetQuad(glyph, prevGlyph, Spacing, ref originX, ref originY, ref q);
                if (!glyph.IsEmpty)
                {
                    q.X0 = (int)(q.X0 * Scale.X);
                    q.X1 = (int)(q.X1 * Scale.X);
                    q.Y0 = (int)(q.Y0 * Scale.Y);
                    q.Y1 = (int)(q.Y1 * Scale.Y);

                    var destRect = new Rectangle((int)(x + q.X0),
                                                 (int)(y + q.Y0),
                                                 (int)(q.X1 - q.X0),
                                                 (int)(q.Y1 - q.Y0));

                    var sourceRect = new Rectangle((int)(q.S0 * _size.X),
                                                   (int)(q.T0 * _size.Y),
                                                   (int)((q.S1 - q.S0) * _size.X),
                                                   (int)((q.T1 - q.T0) * _size.Y));

                    batch.Draw(glyph.Atlas.Texture,
                               destRect,
                               sourceRect,
                               glyphColors[pos],
                               0f,
                               Vector2.Zero,
                               SpriteEffects.None,
                               depth);
                }

                prevGlyph = glyph;
                ++pos;
            }

            return(x);
        }
Esempio n. 19
0
        public float TextBounds(float x, float y, StringBuilder str, ref Bounds bounds)
        {
            if (str == null || str.Length == 0)
            {
                return(0.0f);
            }

            Dictionary <int, FontGlyph> glyphs;
            float ascent, lineHeight;

            PreDraw(str, out glyphs, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            y += ascent;

            float minx, maxx, miny, maxy;

            minx = maxx = x;
            miny = maxy = y;
            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, glyphs, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);
                if (q.X0 < minx)
                {
                    minx = q.X0;
                }
                if (x > maxx)
                {
                    maxx = x;
                }
                if (q.Y0 < miny)
                {
                    miny = q.Y0;
                }
                if (q.Y1 > maxy)
                {
                    maxy = q.Y1;
                }

                prevGlyph = glyph;
            }

            float advance = x - startx;

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }
Esempio n. 20
0
        public float DrawText(SpriteBatch batch, float x, float y, StringSegment str)
        {
            if (str.IsNullOrEmpty)
            {
                return(0.0f);
            }

            FontGlyph glyph          = null;
            var       q              = new FontGlyphSquad();
            var       prevGlyphIndex = -1;
            var       isize          = (int)(Size * 10.0f);
            var       iblur          = (int)BlurValue;
            float     scale          = 0;
            Font      font;
            float     width = 0;

            if (FontId < 0 || FontId >= _fontsNumber)
            {
                return(x);
            }
            font = _fonts[FontId];
            if (font.Data == null)
            {
                return(x);
            }
            scale = font._font.fons__tt_getPixelHeightScale(isize / 10.0f);

            if ((Alignment & FONS_ALIGN_LEFT) != 0)
            {
            }
            else if ((Alignment & FONS_ALIGN_RIGHT) != 0)
            {
                var bounds = new Bounds();
                width = TextBounds(x, y, str, ref bounds);
                x    -= width;
            }
            else if ((Alignment & FONS_ALIGN_CENTER) != 0)
            {
                var bounds = new Bounds();
                width = TextBounds(x, y, str, ref bounds);
                x    -= width * 0.5f;
            }

            float originX = 0.0f;
            float originY = 0.0f;

            originY += GetVertAlign(font, Alignment, isize);
            for (int i = 0; i < str.Length; i += Char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1)
            {
                var codepoint = Char.ConvertToUtf32(str.String, i + str.Location);
                glyph = GetGlyph(font, codepoint, isize, iblur, FONS_GLYPH_BITMAP_REQUIRED);
                if (glyph != null)
                {
                    GetQuad(font, prevGlyphIndex, glyph, scale, Spacing, ref originX, ref originY, &q);
                    if (_vertsNumber + 6 > 1024)
                    {
                        Flush(batch);
                    }

                    q.X0 = (int)(q.X0 * Scale.X);
                    q.X1 = (int)(q.X1 * Scale.X);
                    q.Y0 = (int)(q.Y0 * Scale.Y);
                    q.Y1 = (int)(q.Y1 * Scale.Y);

                    AddVertex(new Rectangle((int)(x + q.X0),
                                            (int)(y + q.Y0),
                                            (int)(q.X1 - q.X0),
                                            (int)(q.Y1 - q.Y0)),
                              new Rectangle((int)(q.S0 * _params_.Width),
                                            (int)(q.T0 * _params_.Height),
                                            (int)((q.S1 - q.S0) * _params_.Width),
                                            (int)((q.T1 - q.T0) * _params_.Height)),
                              Color);
                }

                prevGlyphIndex = glyph != null ? glyph.Index : -1;
            }

            Flush(batch);
            return(x);
        }
Esempio n. 21
0
        private void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, float spacing, ref float x, ref float y, ref FontGlyphSquad q)
        {
            if (prevGlyph != null)
            {
                float adv = 0;
                if (UseKernings && glyph.Font == prevGlyph.Font)
                {
                    adv = prevGlyph.GetKerning(glyph) * glyph.Font.Scale;
                }

                x += (int)(adv + spacing + 0.5f);
            }

            float rx = x + glyph.XOffset;
            float ry = y + glyph.YOffset;

            q.X0 = rx;
            q.Y0 = ry;
            q.X1 = rx + glyph.Bounds.Width;
            q.Y1 = ry + glyph.Bounds.Height;
            q.S0 = glyph.Bounds.X * _itw;
            q.T0 = glyph.Bounds.Y * _ith;
            q.S1 = glyph.Bounds.Right * _itw;
            q.T1 = glyph.Bounds.Bottom * _ith;

            x += (int)(glyph.XAdvance / 10.0f + 0.5f);
        }
Esempio n. 22
0
        internal override void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, Vector2 scale, ref float x, ref float y, ref FontGlyphSquad q)
        {
            if (prevGlyph != null)
            {
                float adv = 0;

                var dynamicGlyph     = (DynamicFontGlyph)glyph;
                var dynamicPrevGlyph = (DynamicFontGlyph)prevGlyph;
                if (FontSystem.UseKernings && dynamicGlyph.Font == dynamicPrevGlyph.Font)
                {
                    adv = dynamicPrevGlyph.Font.GetGlyphKernAdvance(prevGlyph.Id, dynamicGlyph.Id, dynamicGlyph.Size);
                }

                x += (int)(adv + FontSystem.CharacterSpacing + 0.5f);
            }

            base.GetQuad(glyph, prevGlyph, scale, ref x, ref y, ref q);
        }
Esempio n. 23
0
        public float DrawText(SpriteBatch batch, float x, float y, StringSegment str, float depth)
        {
            if (str.IsNullOrEmpty)
            {
                return(0.0f);
            }

            FontGlyph glyph          = null;
            var       q              = new FontGlyphSquad();
            var       prevGlyphIndex = -1;
            var       isize          = (int)(Size * 10.0f);
            var       iblur          = (int)BlurValue;
            float     scale          = 0;
            Font      font;
            float     width = 0;

            if (FontId < 0 || FontId >= _fonts.Count)
            {
                return(x);
            }
            font = _fonts[FontId];
            if (font.Data == null)
            {
                return(x);
            }
            scale = font._font.fons__tt_getPixelHeightScale(isize / 10.0f);

            if ((Alignment & Alignment.Left) != 0)
            {
            }
            else if ((Alignment & Alignment.Right) != 0)
            {
                var bounds = new Bounds();
                width = TextBounds(x, y, str, ref bounds);
                x    -= width;
            }
            else if ((Alignment & Alignment.Center) != 0)
            {
                var bounds = new Bounds();
                width = TextBounds(x, y, str, ref bounds);
                x    -= width * 0.5f;
            }

            float originX = 0.0f;
            float originY = 0.0f;

            originY += GetVertAlign(font, Alignment, isize);
            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str.String, i + str.Location);
                glyph = GetGlyph(font, codepoint, isize, iblur, true);
                if (glyph != null)
                {
                    GetQuad(font, prevGlyphIndex, glyph, scale, Spacing, ref originX, ref originY, &q);

                    q.X0 = (int)(q.X0 * Scale.X);
                    q.X1 = (int)(q.X1 * Scale.X);
                    q.Y0 = (int)(q.Y0 * Scale.Y);
                    q.Y1 = (int)(q.Y1 * Scale.Y);

                    var renderItem = new RenderItem
                    {
                        Atlas  = _atlases[glyph.AtlasIndex],
                        _verts = new Rectangle((int)(x + q.X0),
                                               (int)(y + q.Y0),
                                               (int)(q.X1 - q.X0),
                                               (int)(q.Y1 - q.Y0)),
                        _textureCoords = new Rectangle((int)(q.S0 * _params_.Width),
                                                       (int)(q.T0 * _params_.Height),
                                                       (int)((q.S1 - q.S0) * _params_.Width),
                                                       (int)((q.T1 - q.T0) * _params_.Height)),
                        _colors = Color
                    };

                    _renderItems.Add(renderItem);
                }

                prevGlyphIndex = glyph != null ? glyph.Index : -1;
            }

            Flush(batch, depth);
            return(x);
        }
Esempio n. 24
0
        public float TextBounds(float x, float y, string str, ref Bounds bounds, Vector2 scale)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0.0f);
            }

            float ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            y += ascent;

            float minx, maxx, miny, maxy;

            minx = maxx = x;
            miny = maxy = y;
            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);
                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(codepoint, true);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, scale, ref x, ref y, ref q);
                if (q.X0 < minx)
                {
                    minx = q.X0;
                }
                var scaledX = x * scale.X;
                if (scaledX > maxx)
                {
                    maxx = scaledX;
                }

                if (q.Y0 < miny)
                {
                    miny = q.Y0;
                }
                if (q.Y1 > maxy)
                {
                    maxy = q.Y1;
                }

                prevGlyph = glyph;
            }

            maxx += FontSystem.StrokeAmount * 2;

            float advance = x - startx;

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }
Esempio n. 25
0
        public float TextBounds(float x, float y, StringSegment str, ref Bounds bounds)
        {
            var        state          = GetState();
            var        q              = new FontGlyphSquad();
            FontGlyph *glyph          = null;
            var        prevGlyphIndex = -1;
            var        isize          = (short)(state.Size * 10.0f);
            var        iblur          = (short)state.Blur;
            float      scale          = 0;
            Font       font;
            float      startx  = 0;
            float      advance = 0;
            float      minx    = 0;
            float      miny    = 0;
            float      maxx    = 0;
            float      maxy    = 0;

            if (state.Font < 0 || state.Font >= _fontsNumber)
            {
                return(0);
            }
            font = _fonts[state.Font];
            if (font.Data == null)
            {
                return(0);
            }
            scale  = font.FontInfo.__tt_getPixelHeightScale(isize / 10.0f);
            y     += GetVertAlign(font, state.Align, isize);
            minx   = maxx = x;
            miny   = maxy = y;
            startx = x;
            for (int i = 0; i < str.Length; i += Char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1)
            {
                var codepoint = Char.ConvertToUtf32(str.String, i + str.Location);
                glyph = GetGlyph(font, codepoint, isize, iblur, FONS_GLYPH_BITMAP_OPTIONAL);
                if (glyph != null)
                {
                    GetQuad(font, prevGlyphIndex, glyph, scale, state.Spacing, ref x, ref y, &q);
                    if (q.X0 < minx)
                    {
                        minx = q.X0;
                    }
                    if (q.X1 > maxx)
                    {
                        maxx = q.X1;
                    }
                    if ((_params_.Flags & FONS_ZERO_TOPLEFT) != 0)
                    {
                        if (q.Y0 < miny)
                        {
                            miny = q.Y0;
                        }
                        if (q.Y1 > maxy)
                        {
                            maxy = q.Y1;
                        }
                    }
                    else
                    {
                        if (q.Y1 < miny)
                        {
                            miny = q.Y1;
                        }
                        if (q.Y0 > maxy)
                        {
                            maxy = q.Y0;
                        }
                    }
                }

                prevGlyphIndex = glyph != null ? glyph->Index : -1;
            }

            advance = x - startx;
            if ((state.Align & Alignment.Left) != 0)
            {
            }
            else if ((state.Align & Alignment.Right) != 0)
            {
                minx -= advance;
                maxx -= advance;
            }
            else if ((state.Align & Alignment.Center) != 0)
            {
                minx -= advance * 0.5f;
                maxx -= advance * 0.5f;
            }

            bounds.b1 = minx;
            bounds.b2 = miny;
            bounds.b3 = maxx;
            bounds.b4 = maxy;

            return(advance);
        }
Esempio n. 26
0
        public virtual float TextBounds(StringBuilder str, Vector2 position, ref Bounds bounds, Vector2 scale)
        {
            if (str == null || str.Length == 0)
            {
                return(0.0f);
            }

            float ascent, lineHeight;

            PreDraw(str, out ascent, out lineHeight);

            var q = new FontGlyphSquad();

            var x = position.X;
            var y = position.Y;

            y += ascent;

            float minx, maxx, miny, maxy;

            minx = maxx = x;
            miny = maxy = y;
            float startx = x;

            FontGlyph prevGlyph = null;

            for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(codepoint, true);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, scale, ref x, ref y, ref q);
                if (q.X0 < minx)
                {
                    minx = q.X0;
                }
                var scaledX = x * scale.X;
                if (scaledX > maxx)
                {
                    maxx = scaledX;
                }

                if (q.Y0 < miny)
                {
                    miny = q.Y0;
                }
                if (q.Y1 > maxy)
                {
                    maxy = q.Y1;
                }

                prevGlyph = glyph;
            }

            float advance = x - startx;

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }