Beispiel #1
0
        public int GetKerning(char previous, char current)
        {
            int result;

            var kerning = new Kerning(previous, current, 0);

            if (!Kernings.TryGetValue(kerning, out result))
            {
                result = 0;
            }

            return(result);
        }
Beispiel #2
0
        public Vector2 MeasureText(string text, int start, int end)
#endif
        {
            Vector2 size = Vector2.Zero;

            if (start >= end)
            {
                return(size);
            }
            if (end > text.Length)
            {
                end = text.Length;
            }
            if (start < 0)
            {
                start = 0;
            }

            float x = 0;
            float y = 0;

            int key = 0;

            for (int i = start; i < end; i++)
            {
                char c = text[i];
                switch (c)
                {
                case '\r':
                {
                    key |= c;
                    continue;
                }

                case '\n':
                {
                    key = 0;
                    x   = 0;
                    y  += LineSpacing;
                }
                break;

                default:
                {
                    if (!_glyphs.TryGetValue(c, out Glyph glyph))
                    {
                        if (IgnoreUnknownCharacters)
                        {
                            continue;
                        }

                        glyph = _defaultGlyph;
                    }
                    key |= c;

                    float dx = glyph.OffsetX;
                    if (Kernings.TryGetValue(key, out Kerning kerning))
                    {
                        dx += kerning.Offset;
                    }

                    float nextX = x + glyph.XAdvance + SpacingX;

                    float h = y + LineSpacing;
                    if (nextX + dx > size.X)
                    {
                        size.X = nextX;
                    }
                    if (h > size.Y)
                    {
                        size.Y = h;
                    }

                    x = nextX;
                }
                break;
                }

                key <<= 16;
            }

            return(size);
        }
Beispiel #3
0
        public int HitTest(string text, int start, int end, float xPos, float yPos)
#endif
        {
            if (start >= end)
            {
                return(end);
            }
            if (end > text.Length)
            {
                end = text.Length;
            }
            if (start < 0)
            {
                start = 0;
            }

            if (xPos < 0)
            {
                return(-1);
            }
            if (yPos < 0)
            {
                return(-1);
            }

            float x = 0;
            float y = 0;

            int key = 0;

            for (int i = start; i < end; i++)
            {
                char c = text[i];
                switch (c)
                {
                case '\r':
                {
                    key |= c;
                    continue;
                }

                case '\n':
                {
                    key = 0;
                    x   = 0;
                    y  += LineSpacing;
                }
                break;

                default:
                {
                    if (!_glyphs.TryGetValue(c, out Glyph glyph))
                    {
                        if (IgnoreUnknownCharacters)
                        {
                            continue;
                        }
                        glyph = _defaultGlyph;
                    }
                    key |= c;

                    float dx = glyph.OffsetX;
                    if (Kernings.TryGetValue(key, out Kerning kerning))
                    {
                        dx += kerning.Offset;
                    }

                    float nextX = x + glyph.XAdvance + SpacingX;
                    float h     = y + LineSpacing;

                    if (xPos >= x && xPos <= nextX + dx && yPos <= h && yPos >= y)
                    {
                        if (xPos < (x + nextX + dx) * 0.5f)
                        {
                            return(i);
                        }
                        return(i + 1);
                    }
                    x = nextX;
                }
                break;
                }
                key <<= 16;
            }

            return(end);
        }