コード例 #1
0
        private bool GetWordPosition(float x, float y, GLFontTextNode node, GLFontTextPosition position, ref int character, out Vector2 p)
        {
            bool sameLine = (long)(y / lineSpacingCache) == (long)(position.Position.Y / lineSpacingCache);

            if (node.Type == GLFontTextNodeType.Space || node.Type == GLFontTextNodeType.Tab)
            {
                p = new Vector2(x, y);
                if (position.Index == character || (sameLine && x + node.ModifiedLength * 0.5f > position.Position.X))
                {
                    return(true);
                }
                character++;
                return(false);
            }

            int  charGaps      = node.Text.Length - 1;
            bool isCrumbleWord = CrumbledWord(node);

            if (isCrumbleWord)
            {
                charGaps++;
            }

            int pixelsPerGap   = 0;
            int leftOverPixels = 0;

            if (charGaps != 0)
            {
                pixelsPerGap   = (int)node.LengthTweak / charGaps;
                leftOverPixels = (int)node.LengthTweak - pixelsPerGap * charGaps;
            }

            for (int i = 0; i < node.Text.Length; i++)
            {
                char c = node.Text[i];
                if (fontData.CharSetMapping.ContainsKey(c))
                {
                    var glyph = fontData.CharSetMapping[c];

                    float oldX = x;

                    if (isMonospacingActiveCache)
                    {
                        x += monoSpaceWidthCache;
                    }
                    else
                    {
                        x += (int)Math.Ceiling(glyph.Rect.Width + fontData.meanGlyphWidth * Options.CharacterSpacing + fontData.GetKerningPairCorrection(i, node.Text, node));
                    }

                    x += pixelsPerGap;
                    if (leftOverPixels > 0)
                    {
                        x += 1.0f;
                        leftOverPixels--;
                    }
                    else if (leftOverPixels < 0)
                    {
                        x -= 1.0f;
                        leftOverPixels++;
                    }

                    if (position.Index == character || (sameLine && (oldX + x) * 0.5f > position.Position.X))
                    {
                        p = new Vector2(oldX, y);
                        return(true);
                    }
                }
                character++;
            }
            p = new Vector2();
            return(false);
        }
コード例 #2
0
        public void MeasureNodes(GLFontData fontData, GLFontRenderOptions options)
        {
            bool  monospaced     = fontData.IsMonospacingActive(options);
            float monospaceWidth = fontData.GetMonoSpaceWidth(options);

            foreach (GLFontTextNode node in this)
            {
                if (node.Length == 0f)
                {
                    if (node.Type == GLFontTextNodeType.Space)
                    {
                        if (monospaced)
                        {
                            node.Length = monospaceWidth;
                            continue;
                        }
                        node.Length = (float)Math.Ceiling(fontData.meanGlyphWidth * options.WordSpacing);
                        continue;
                    }

                    if (node.Type == GLFontTextNodeType.Tab)
                    {
                        if (monospaced)
                        {
                            node.Length = monospaceWidth * 4;
                            continue;
                        }
                        node.Length = (float)Math.Ceiling(4 * fontData.meanGlyphWidth * options.WordSpacing);
                        continue;
                    }

                    if (node.Type == GLFontTextNodeType.Word)
                    {
                        for (int i = 0; i < node.Text.Length; i++)
                        {
                            char        c = node.Text[i];
                            GLFontGlyph glyph;
                            if (fontData.CharSetMapping.TryGetValue(c, out glyph))
                            {
                                if (monospaced)
                                {
                                    node.Length += monospaceWidth;
                                }
                                else
                                {
                                    node.Length += (float)Math.Ceiling(glyph.Rect.Width + fontData.meanGlyphWidth * options.CharacterSpacing + fontData.GetKerningPairCorrection(i, node.Text, node));
                                }
                            }
                        }
                    }
                }
            }
        }