コード例 #1
0
        public override Dictionary <char, BMPFontCharacter> GetCharacters()
        {
            byte blockId   = r.ReadByte();
            int  blockSize = r.ReadInt16();

            int numCharacters = blockSize / 20;
            Dictionary <char, BMPFontCharacter> chars = new Dictionary <char, BMPFontCharacter>();

            for (int i = 0; i < numCharacters; i++)
            {
                uint charId   = r.ReadUInt32();
                uint x        = r.ReadUInt16();
                uint y        = r.ReadUInt16();
                uint width    = r.ReadUInt16();
                uint height   = r.ReadUInt16();
                int  xOffset  = r.ReadInt16();
                int  yOffset  = r.ReadInt16();
                int  xAdvance = r.ReadInt16();
                uint page     = r.ReadByte();
                byte channel  = r.ReadByte();

                BMPFontCharacter c = new BMPFontCharacter(charId, x, y, width, height,
                                                          xOffset, yOffset, xAdvance, page, channel);
                chars.Add(c.Character, c);
            }

            return(chars);
        }
コード例 #2
0
        public override Dictionary <char, BMPFontCharacter> GetCharacters()
        {
            TextLine line = reader.ReadLine();

            if (line.Id != "chars")
            {
                throw new Exception("Failed to load angel bmp file, unexpected block '" + line.Id + "'");
            }

            uint numChars = line.GetUInt("count") ?? 0;
            Dictionary <char, BMPFontCharacter> chars = new Dictionary <char, BMPFontCharacter>();

            for (int i = 0; i < numChars; i++)
            {
                TextLine cline = reader.ReadLine();
                if (cline.Id != "char")
                {
                    throw new Exception("Failed to load angel bmp file, unexpected block '" + cline.Id + "'");
                }

                uint charId   = cline.GetUInt("id") ?? '\0';
                uint x        = cline.GetUInt("x") ?? 0;
                uint y        = cline.GetUInt("y") ?? 0;
                uint width    = cline.GetUInt("width") ?? 0;
                uint height   = cline.GetUInt("height") ?? 0;
                int  xOffset  = cline.GetInt("xoffset") ?? 0;
                int  yOffset  = cline.GetInt("yoffset") ?? 0;
                int  xAdvance = cline.GetInt("xadvance") ?? 0;
                uint page     = cline.GetUInt("page") ?? 0;
                byte channel  = cline.GetByte("chnl") ?? 0;

                BMPFontCharacter c = new BMPFontCharacter(charId, x, y, width, height,
                                                          xOffset, yOffset, xAdvance, page, channel);

                chars.Add(c.Character, c);
            }

            return(chars);
        }
コード例 #3
0
        public Vector2 MeasureString(string str)
        {
            float xSize = 0;
            float ySize = 0;

            string[] lines = str.Split('\n');

            for (int l = 0; l < lines.Length; l++)
            {
                ySize += LineHeight;
                string line = lines[l];

                int  lineXSize = 0;
                char?lastC     = null;
                for (int i = 0; i < line.Length; i++)
                {
                    char c = line[i];

                    if (c == '\r')
                    {
                        continue;
                    }

                    BMPFontCharacter g = GetCharacter(c);
                    if (g != null)
                    {
                        int kerningOffset = lastC.HasValue ? GetKerning(lastC.Value, c) : 0;
                        lineXSize += g.XAdvance + kerningOffset;
                    }

                    lastC = c;
                }

                xSize = Math.Max(lineXSize, xSize);
            }

            return(new Vector2(xSize, ySize));
        }
コード例 #4
0
        public void DrawString(string str, float x, float y, SpriteBatch sb, TextAlign align, Color color,
                               Color?shadowColor = null)
        {
            float startX = x;
            float startY = y;

            string[] lines = str.Split('\n');

            bool topAlign = align == TextAlign.TopLeft ||
                            align == TextAlign.TopCenter ||
                            align == TextAlign.TopRight;

            bool bottomAlign = align == TextAlign.BottomLeft ||
                               align == TextAlign.BottomCenter ||
                               align == TextAlign.BottomRight;

            bool leftAlign = align == TextAlign.TopLeft ||
                             align == TextAlign.Left ||
                             align == TextAlign.BottomLeft;

            bool rightAlign = align == TextAlign.TopRight ||
                              align == TextAlign.Right ||
                              align == TextAlign.BottomRight;

            for (int l = 0; l < lines.Length; l++)
            {
                string  line     = lines[l];
                Vector2 lineSize = MeasureString(line);

                if (leftAlign)
                {
                    x = startX;
                }
                else if (rightAlign)
                {
                    x = startX - lineSize.X;
                }
                else
                {
                    x = startX - lineSize.X / 2f;
                }

                if (topAlign)
                {
                    y = startY + LineHeight * l;
                }
                else if (bottomAlign)
                {
                    y = startY - LineHeight * (lines.Length - l);
                }
                else
                {
                    y = startY - (LineHeight * (lines.Length / 2f)) + LineHeight * l;
                }

                char?lastC = null;
                for (int i = 0; i < line.Length; i++)
                {
                    char c = line[i];

                    if (c == '\r')
                    {
                        continue;
                    }

                    BMPFontCharacter g = GetCharacter(c);
                    if (g != null)
                    {
                        int       kerningOffset = lastC.HasValue ? GetKerning(lastC.Value, c) : 0;
                        Rectangle rect          = new Rectangle(x + g.XOffset + kerningOffset, y + g.YOffset, g.Width, g.Height);
                        Rectangle clip          = new Rectangle(g.X, g.Y, g.Width, g.Height);

                        if (shadowColor.HasValue)
                        {
                            sb.Draw(Pages[g.PageIndex], rect + new Vector2(1, 1), clip, shadowColor.Value);
                        }

                        sb.Draw(Pages[g.PageIndex], rect, clip, color);

                        x += g.XAdvance + kerningOffset;
                    }

                    lastC = c;
                }
            }
        }