示例#1
0
        private void SetupSpaceGlyphs(MSDFFontData fontData)
        {
            var space = fontData.GetGlyphById(SPACE_ID);

            if (null == space)
            {
                space = fontData.GetMGlyph();
            }

            if (null == space)
            {
                space = fontData.GetGlyphByIndex(0);
            }

            var tabWidth = _tabSize * space.xadvance;

            _fallbackSpaceGlyph = space;

            _fallbackTabGlyph = new Glyph()
            {
                x         = 0,
                y         = 0,
                xadvance  = tabWidth,
                id        = TAB_ID,
                xoffset   = 0,
                yoffset   = 0,
                width     = 0,
                height    = 0,
                page      = space.page,
                channel   = space.channel,
                character = space.character,
                index     = space.index,
            };
        }
示例#2
0
        public static List <ProcessedGlyph> GetVisibleGlyphs(string text, MSDFFontData fontData, float tabSize = 4f, int width = 0, float lineHeight = 0f, float letterSpacing = 0f, Align alignType = Align.LEFT)
        {
            // setup space and tab
            var space = fontData.GetGlyphById(SPACE_ID);

            if (null == space)
            {
                space = fontData.GetMGlyph();
            }

            if (null == space)
            {
                space = fontData.GetGlyphByIndex(0);
            }

            var fallbackSpaceGlyph = space;

            var fallbackTabGlyph = new Glyph()
            {
                x         = 0,
                y         = 0,
                xadvance  = tabSize * space.xadvance,
                id        = TextLayout.TAB_ID,
                xoffset   = 0,
                yoffset   = 0,
                width     = 0,
                height    = 0,
                page      = space.page,
                channel   = space.channel,
                character = space.character,
                index     = space.index,
            };

            // calculate layouts
            var glyphs = new List <ProcessedGlyph>();

            var lines    = WordWrapper.GetLines(text, fontData);
            var minWidth = width;

            glyphs.Clear();

            var maxLineWidth = lines.Max(metrics => metrics.width);

            var x         = 0f;
            var y         = 0f;
            var lh        = 0f == lineHeight ? fontData.common.lineHeight : lineHeight;
            var descender = lh - fontData.common.baseLine;
            var height    = lh * lines.Count() - descender;

            y -= height;

            int lineIndex = 0;

            foreach (var line in lines)
            {
                var   start     = line.start;
                var   end       = line.end;
                var   lineWidth = line.width;
                Glyph lastGlyph = null;

                for (var i = start; i < end; ++i)
                {
                    var id = MSDFFontData.GetCharCode(text[i]);

                    Glyph glyph;

                    if (id == TAB_ID)
                    {
                        glyph = fallbackTabGlyph;
                    }
                    else if (id == SPACE_ID)
                    {
                        glyph = fallbackSpaceGlyph;
                    }
                    else
                    {
                        glyph = fontData.GetGlyphById(id);
                    }

                    if (null != glyph)
                    {
                        if (null != lastGlyph)
                        {
                            x += fontData.GetKerningAmount(lastGlyph.id, glyph.id);
                        }

                        var tx = x;

                        if (Align.CENTER == alignType)
                        {
                            tx += (maxLineWidth - lineWidth) * 0.5f;
                        }
                        else if (Align.RIGHT == alignType)
                        {
                            tx += (maxLineWidth - lineWidth);
                        }

                        glyphs.Add(new ProcessedGlyph()
                        {
                            position = new Vector2(tx, y),
                            glyph    = glyph,
                            index    = i,
                            line     = lineIndex
                        });

                        x        += glyph.xadvance + letterSpacing;
                        lastGlyph = glyph;
                    }
                }

                y += lineHeight;
                x  = 0;

                ++lineIndex;
            }

            return(glyphs.Where(g => g.glyph.width * g.glyph.height > 0).ToList());
        }