예제 #1
0
        public void SetFixedWidthGlyphs(string glyphs)
        {
            BitmapFontData data       = Data;
            int            maxAdvance = 0;

            for (int index = 0, end = glyphs.Length; index < end; index++)
            {
                Glyph g = data[glyphs[index]];
                if (g != null && g.XAdvance > maxAdvance)
                {
                    maxAdvance = g.XAdvance;
                }
            }

            for (int index = 0, end = glyphs.Length; index < end; index++)
            {
                Glyph g = data[glyphs[index]];
                if (g == null)
                {
                    continue;
                }

                g.XOffset += (maxAdvance - g.XAdvance) / 2;
                g.XAdvance = maxAdvance;
                g.Kerning  = null;
            }
        }
예제 #2
0
        public BitmapFont(GraphicsDevice device, BitmapFontData data, TextureRegion region, bool integer)
        {
            if (region != null)
            {
                _region = region;
            }
            else
            {
                if (data.FontFile == null)
                {
                    _region = new TextureRegion(new TextureContext(device, data.ImagePath, true));
                    // this.region = new TextureRegion(new Texture(Gdx.files.internal(data.imagePath), false));
                    throw new NotImplementedException();
                }
                else
                {
                    _region = new TextureRegion(new TextureContext(device, data.ImagePath, true));
                    // this.region = new TextureRegion(new Texture(Gdx.files.getFileHandle(data.imagePath, data.fontFile.type()), false));
                    //throw new NotImplementedException();
                }
            }

            _cache = new BitmapFontCache(this)
            {
                UsesIntegerPositions = integer,
            };

            _flipped            = data.IsFlipped;
            _data               = data;
            UsesIntegerPostions = integer;

            Load(_data);
            //OwnsTexture = (_region == null);
        }
예제 #3
0
        public int ComputeVisibleGlyphs(CharSequence str, int start, int end, float availableWidth)
        {
            BitmapFontData data      = Data;
            int            index     = start;
            float          width     = 0;
            Glyph          lastGlyph = null;

            availableWidth /= data.ScaleX;

            for (; index < end; index++)
            {
                char  ch = str[index];
                Glyph g  = data[ch];

                if (g != null)
                {
                    if (lastGlyph != null)
                    {
                        width += lastGlyph.GetKerning(ch);
                    }
                    if ((width + g.XAdvance) - availableWidth > .001f)
                    {
                        break;
                    }

                    width    += g.XAdvance;
                    lastGlyph = g;
                }
            }

            return(index - start);
        }
예제 #4
0
        public TextBounds GetBounds(CharSequence str, int start, int end)
        {
            BitmapFontData data      = Data;
            int            width     = 0;
            Glyph          lastGlyph = null;

            while (start < end)
            {
                lastGlyph = data[str[start++]];
                if (lastGlyph != null)
                {
                    width = lastGlyph.XAdvance;
                    break;
                }
            }

            while (start < end)
            {
                char  ch = str[start++];
                Glyph g  = data[ch];
                if (g != null)
                {
                    width    += lastGlyph.GetKerning(ch);
                    lastGlyph = g;
                    width    += g.XAdvance;
                }
            }

            return(new TextBounds()
            {
                Width = width * data.ScaleX,
                Height = data.CapHeight,
            });
        }
예제 #5
0
        public void SetScale(float scaleX, float scaleY)
        {
            BitmapFontData data = Data;
            float          x    = scaleX / data.ScaleX;
            float          y    = scaleY / data.ScaleY;

            data.LineHeight = data.LineHeight * y;
            data.SpaceWidth = data.SpaceWidth * x;
            data.XHeight    = data.XHeight * y;
            data.CapHeight  = data.CapHeight * y;
            data.Ascent     = data.Ascent * y;
            data.Descent    = data.Descent * y;
            data.Down       = data.Down * y;
            data.ScaleX     = scaleX;
            data.ScaleY     = scaleY;
        }
예제 #6
0
        private float AddToCache(CharSequence str, float x, float y, int start, int end)
        {
            float          startX    = x;
            BitmapFont     font      = Font;
            Glyph          lastGlyph = null;
            BitmapFontData data      = font.Data;

            if (data.ScaleX == 1 && data.ScaleY == 1)
            {
                while (start < end)
                {
                    lastGlyph = data[str[start++]];
                    if (lastGlyph != null)
                    {
                        AddGlyph(lastGlyph, x + lastGlyph.XOffset, y + lastGlyph.YOffset, lastGlyph.Width, lastGlyph.Height);
                        x += lastGlyph.XAdvance;
                        break;
                    }
                }

                while (start < end)
                {
                    char  ch = str[start++];
                    Glyph g  = data[ch];
                    if (g != null)
                    {
                        x        += lastGlyph.GetKerning(ch);
                        lastGlyph = g;
                        AddGlyph(lastGlyph, x + g.XOffset, y + g.YOffset, g.Width, g.Height);
                        x += g.XAdvance;
                    }
                }
            }
            else
            {
                float scaleX = data.ScaleX;
                float scaleY = data.ScaleY;

                while (start < end)
                {
                    lastGlyph = data[str[start++]];
                    if (lastGlyph != null)
                    {
                        AddGlyph(lastGlyph, x + lastGlyph.XOffset * scaleX, y + lastGlyph.YOffset * scaleY,
                                 lastGlyph.Width * scaleX, lastGlyph.Height * scaleY);
                        x += lastGlyph.XAdvance * scaleX;
                    }
                }

                while (start < end)
                {
                    char  ch = str[start++];
                    Glyph g  = data[ch];
                    if (g != null)
                    {
                        x        += lastGlyph.GetKerning(ch) * scaleX;
                        lastGlyph = g;
                        AddGlyph(lastGlyph, x + g.XOffset * scaleX, y + g.YOffset * scaleY,
                                 lastGlyph.Width * scaleX, lastGlyph.Height * scaleY);
                        x += g.XAdvance * scaleX;
                    }
                }
            }

            return(x - startX);
        }
예제 #7
0
        public void ComputeGlyphAdvancesAndPositions(CharSequence str, IList <float> glyphAdvances, IList <float> glyphPositions)
        {
            glyphAdvances.Clear();
            glyphPositions.Clear();

            int            index     = 0;
            int            end       = str.Length;
            float          width     = 0;
            Glyph          lastGlyph = null;
            BitmapFontData data      = Data;

            if (data.ScaleX == 1)
            {
                for (; index < end; index++)
                {
                    char  ch = str[index];
                    Glyph g  = data[ch];

                    if (g != null)
                    {
                        if (lastGlyph != null)
                        {
                            width += lastGlyph.GetKerning(ch);
                        }

                        lastGlyph = g;
                        glyphAdvances.Add(g.XAdvance);
                        glyphPositions.Add(width);
                        width += g.XAdvance;
                    }
                }

                glyphAdvances.Add(0);
                glyphPositions.Add(width);
            }
            else
            {
                float scaleX = data.ScaleX;
                for (; index < end; index++)
                {
                    char  ch = str[index];
                    Glyph g  = data[ch];

                    if (g != null)
                    {
                        if (lastGlyph != null)
                        {
                            width += lastGlyph.GetKerning(ch) * scaleX;
                        }

                        lastGlyph = g;
                        float xAdvance = g.XAdvance * scaleX;
                        glyphAdvances.Add(xAdvance);
                        glyphPositions.Add(width);
                        width += xAdvance;
                    }
                }

                glyphAdvances.Add(0);
                glyphPositions.Add(width);
            }
        }
예제 #8
0
        private void Load(BitmapFontData data)
        {
            float invTexWidth  = 1f / _region.Texture.Width;
            float invTexHeight = 1f / _region.Texture.Height;
            float u            = _region.U;
            float v            = _region.V;

            float offsetX      = 0;
            float offsetY      = 0;
            float regionWidth  = _region.RegionWidth;
            float regionHeight = _region.RegionHeight;

            if (_region is TextureAtlas.AtlasRegion)
            {
                // Compensate for whitespace stripped from left and top edges.
                TextureAtlas.AtlasRegion atlasRegion = _region as TextureAtlas.AtlasRegion;
                offsetX = atlasRegion.OffsetX;
                offsetY = atlasRegion.OriginalHeight - atlasRegion.PackedHeight - atlasRegion.OffsetY;
            }

            foreach (Glyph[] page in data.Glyphs)
            {
                if (page == null)
                {
                    continue;
                }

                foreach (Glyph glyph in page)
                {
                    if (glyph == null)
                    {
                        continue;
                    }

                    float x  = glyph.SrcX;
                    float x2 = glyph.SrcX + glyph.Width;
                    float y  = glyph.SrcY;
                    float y2 = glyph.SrcY + glyph.Height;

                    // Shift glyph for left and top edge stripped whitespace.
                    // Clip glyph for right and bottom edge stripped whitespace.
                    if (offsetX > 0)
                    {
                        x -= offsetX;
                        if (x < 0)
                        {
                            glyph.Width   += (int)x;
                            glyph.XOffset -= (int)x;
                            x              = 0;
                        }

                        x2 -= offsetX;
                        if (x2 > regionWidth)
                        {
                            glyph.Width -= (int)(x2 - regionWidth);
                            x2           = regionWidth;
                        }
                    }

                    if (offsetY > 0)
                    {
                        y -= offsetY;
                        if (y < 0)
                        {
                            glyph.Height += (int)y;
                            y             = 0;
                        }

                        y2 -= offsetY;
                        if (y2 > regionHeight)
                        {
                            float amount = y2 - regionWidth;
                            glyph.Height  -= (int)amount;
                            glyph.YOffset += (int)amount;
                            y2             = regionHeight;
                        }
                    }

                    glyph.U  = u + x * invTexWidth;
                    glyph.U2 = u + x2 * invTexWidth;

                    if (data.IsFlipped)
                    {
                        glyph.V  = v + y * invTexHeight;
                        glyph.V2 = v + y2 * invTexHeight;
                    }
                    else
                    {
                        glyph.V2 = v + y * invTexHeight;
                        glyph.V  = v + y2 * invTexHeight;
                    }
                }
            }
        }