Esempio n. 1
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. 2
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)
                {
                    continue;
                }

                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);
        }