示例#1
0
        public float WidthToNextLine(string text, int start)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(0);
            }

            var currentLineWidth = 0f;

            for (int i = start, j = text.Length; i < j; i++)
            {
                if (text[i] == '\n')
                {
                    break;
                }

                PixelFontCharacter c = null;
                if (Characters.TryGetValue(text[i], out c))
                {
                    currentLineWidth += c.XAdvance;

                    int kerning;
                    if (i < j - 1 && c.Kerning.TryGetValue(text[i + 1], out kerning))
                    {
                        currentLineWidth += kerning;
                    }
                }
            }

            return(currentLineWidth);
        }
示例#2
0
        public PixelFontCharacter Get(int id)
        {
            PixelFontCharacter val = null;

            if (Characters.TryGetValue(id, out val))
            {
                return(val);
            }
            return(null);
        }
示例#3
0
        public Vector2 Measure(char text)
        {
            PixelFontCharacter c = null;

            if (Characters.TryGetValue(text, out c))
            {
                return(new Vector2(c.XAdvance, LineHeight));
            }
            return(Vector2.Zero);
        }
示例#4
0
        public void Draw(char character, Vector2 position, Vector2 justify, Vector2 scale, Color color)
        {
            if (char.IsWhiteSpace(character))
            {
                return;
            }

            PixelFontCharacter c = null;

            if (Characters.TryGetValue(character, out c))
            {
                var measure   = Measure(character);
                var justified = new Vector2(measure.X * justify.X, measure.Y * justify.Y);
                var pos       = position + (new Vector2(c.XOffset, c.YOffset) - justified) * scale;
                c.Texture.Draw(Calc.Floor(pos), Vector2.Zero, color, scale);
            }
        }
示例#5
0
        public Vector2 Measure(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Vector2.Zero);
            }

            var size             = new Vector2(0, LineHeight);
            var currentLineWidth = 0f;

            for (var i = 0; i < text.Length; i++)
            {
                if (text[i] == '\n')
                {
                    size.Y += LineHeight;
                    if (currentLineWidth > size.X)
                    {
                        size.X = currentLineWidth;
                    }
                    currentLineWidth = 0f;
                }
                else
                {
                    PixelFontCharacter c = null;
                    if (Characters.TryGetValue(text[i], out c))
                    {
                        currentLineWidth += c.XAdvance;

                        int kerning;
                        if (i < text.Length - 1 && c.Kerning.TryGetValue(text[i + 1], out kerning))
                        {
                            currentLineWidth += kerning;
                        }
                    }
                }
            }

            if (currentLineWidth > size.X)
            {
                size.X = currentLineWidth;
            }

            return(size);
        }
示例#6
0
        public new void Draw(string text, Vector2 position, Vector2 justify, Vector2 scale, Color color, float edgeDepth, Color edgeColor, float stroke, Color strokeColor)
        {
            text = Emoji.Apply(text);

            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            Vector2 offset      = Vector2.Zero;
            Vector2 justifyOffs = new Vector2(
                ((justify.X != 0f) ? WidthToNextLine(text, 0) : 0f) * justify.X,
                HeightOf(text) * justify.Y
                );

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '\n')
                {
                    offset.X  = 0f;
                    offset.Y += LineHeight;
                    if (justify.X != 0f)
                    {
                        justifyOffs.X = WidthToNextLine(text, i + 1) * justify.X;
                    }
                    continue;
                }

                PixelFontCharacter c = null;
                if (!Characters.TryGetValue(text[i], out c))
                {
                    continue;
                }

                Vector2 pos = position + (offset + new Vector2(c.XOffset, c.YOffset) - justifyOffs) * scale;
                if (stroke > 0f && !Outline)
                {
                    if (edgeDepth > 0f)
                    {
                        c.Texture.Draw(pos + new Vector2(0f, -stroke), Vector2.Zero, strokeColor, scale);
                        for (float num2 = -stroke; num2 < edgeDepth + stroke; num2 += stroke)
                        {
                            c.Texture.Draw(pos + new Vector2(-stroke, num2), Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(stroke, num2), Vector2.Zero, strokeColor, scale);
                        }
                        c.Texture.Draw(pos + new Vector2(-stroke, edgeDepth + stroke), Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(0f, edgeDepth + stroke), Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(stroke, edgeDepth + stroke), Vector2.Zero, strokeColor, scale);
                    }
                    else
                    {
                        c.Texture.Draw(pos + new Vector2(-1f, -1f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(0f, -1f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(1f, -1f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(-1f, 0f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(1f, 0f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(-1f, 1f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(0f, 1f) * stroke, Vector2.Zero, strokeColor, scale);
                        c.Texture.Draw(pos + new Vector2(1f, 1f) * stroke, Vector2.Zero, strokeColor, scale);
                    }
                }

                if (edgeDepth > 0f)
                {
                    c.Texture.Draw(pos + Vector2.UnitY * edgeDepth, Vector2.Zero, edgeColor, scale);
                }

                Color cColor = color;
                if (Emoji.Start <= c.Character &&
                    c.Character <= Emoji.Last &&
                    !Emoji.IsMonochrome((char)c.Character))
                {
                    cColor = new Color(color.A, color.A, color.A, color.A);
                }
                c.Texture.Draw(pos, Vector2.Zero, cColor, scale);

                offset.X += c.XAdvance;

                if (i < text.Length - 1 && c.Kerning.TryGetValue(text[i + 1], out int kerning))
                {
                    offset.X += kerning;
                }
            }
        }
示例#7
0
        public PixelFontSize AddFontSize(string path, XmlElement data, Atlas atlas = null, bool outline = false)
        {
            // check if size already exists
            var size = data["info"].AttrFloat("size");

            foreach (var fs in Sizes)
            {
                if (fs.Size == size)
                {
                    return(fs);
                }
            }

            // get textures
            Textures = new List <MTexture>();
            var pages = data["pages"];

            foreach (XmlElement page in pages)
            {
                var file      = page.Attr("file");
                var atlasPath = Path.GetFileNameWithoutExtension(file);

                if (atlas != null && atlas.Has(atlasPath))
                {
                    Textures.Add(atlas[atlasPath]);
                }
                else
                {
                    var dir = Path.GetDirectoryName(path);
                    dir = dir.Substring(Engine.ContentDirectory.Length + 1);
                    Textures.Add(MTexture.FromFile(Path.Combine(dir, file)));
                }
            }

            // create font size
            var fontSize = new PixelFontSize()
            {
                Textures   = Textures,
                Characters = new Dictionary <int, PixelFontCharacter>(),
                LineHeight = data["common"].AttrInt("lineHeight"),
                Size       = size,
                Outline    = outline
            };

            // get characters
            foreach (XmlElement character in data["chars"])
            {
                int id   = character.AttrInt("id");
                int page = character.AttrInt("page", 0);
                fontSize.Characters.Add(id, new PixelFontCharacter(id, Textures[page], character));
            }

            // get kerning
            if (data["kernings"] != null)
            {
                foreach (XmlElement kerning in data["kernings"])
                {
                    var from = kerning.AttrInt("first");
                    var to   = kerning.AttrInt("second");
                    var push = kerning.AttrInt("amount");

                    PixelFontCharacter c = null;
                    if (fontSize.Characters.TryGetValue(from, out c))
                    {
                        c.Kerning.Add(to, push);
                    }
                }
            }

            // add font size
            Sizes.Add(fontSize);
            Sizes.Sort((a, b) => { return(Math.Sign(a.Size - b.Size)); });

            return(fontSize);
        }
示例#8
0
        public void Draw(string text, Vector2 position, Vector2 justify, Vector2 scale, Color color, float edgeDepth, Color edgeColor, float stroke, Color strokeColor)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            var offset    = Vector2.Zero;
            var lineWidth = (justify.X != 0 ? WidthToNextLine(text, 0) : 0);
            var justified = new Vector2(lineWidth * justify.X, HeightOf(text) * justify.Y);

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '\n')
                {
                    offset.X  = 0;
                    offset.Y += LineHeight;
                    if (justify.X != 0)
                    {
                        justified.X = WidthToNextLine(text, i + 1) * justify.X;
                    }
                    continue;
                }

                PixelFontCharacter c = null;
                if (Characters.TryGetValue(text[i], out c))
                {
                    var pos = (position + (offset + new Vector2(c.XOffset, c.YOffset) - justified) * scale);

                    // draw stroke
                    if (stroke > 0 && !Outline)
                    {
                        if (edgeDepth > 0)
                        {
                            c.Texture.Draw(pos + new Vector2(0, -stroke), Vector2.Zero, strokeColor, scale);
                            for (var j = -stroke; j < edgeDepth + stroke; j += stroke)
                            {
                                c.Texture.Draw(pos + new Vector2(-stroke, j), Vector2.Zero, strokeColor, scale);
                                c.Texture.Draw(pos + new Vector2(stroke, j), Vector2.Zero, strokeColor, scale);
                            }
                            c.Texture.Draw(pos + new Vector2(-stroke, edgeDepth + stroke), Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(0, edgeDepth + stroke), Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(stroke, edgeDepth + stroke), Vector2.Zero, strokeColor, scale);
                        }
                        else
                        {
                            c.Texture.Draw(pos + new Vector2(-1, -1) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(0, -1) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(1, -1) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(-1, 0) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(1, 0) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(-1, 1) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(0, 1) * stroke, Vector2.Zero, strokeColor, scale);
                            c.Texture.Draw(pos + new Vector2(1, 1) * stroke, Vector2.Zero, strokeColor, scale);
                        }
                    }

                    // draw edge
                    if (edgeDepth > 0)
                    {
                        c.Texture.Draw(pos + Vector2.UnitY * edgeDepth, Vector2.Zero, edgeColor, scale);
                    }

                    // draw normal character
                    c.Texture.Draw(pos, Vector2.Zero, color, scale);

                    offset.X += c.XAdvance;

                    int kerning;
                    if (i < text.Length - 1 && c.Kerning.TryGetValue(text[i + 1], out kerning))
                    {
                        offset.X += kerning;
                    }
                }
            }
        }