Example #1
0
        public Vector2 MeasureString(string text)
        {
           	Vector2 v = Vector2.Zero;
            float xoffset=0;
            float yoffset=0;

            foreach (char c in text)
            {
                if (c == '\n')
                {
                    yoffset += LineSpacing;
                    xoffset = 0;
                    continue;
                }
                if (characterData.ContainsKey(c) == false) continue;
                GlyphData g = characterData[c];				
                xoffset += g.Kerning.Y + g.Kerning.Z + Spacing;
                float newHeight = g.Glyph.Height + g.Cropping.Top + yoffset;
				if ( newHeight > v.Y)
                {
                    v.Y = newHeight;
                }
                if (xoffset > v.X) v.X = xoffset;
            }
            return v;
        }
Example #2
0
        public Vector2 MeasureString(StringBuilder text)
        {
            Vector2 v         = Vector2.Zero;
            float   xoffset   = 0;
            float   yoffset   = 0;
            float   newHeight = 0;

            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                if (c == '\n')
                {
                    yoffset += LineSpacing;
                    xoffset  = 0;
                    continue;
                }
                if (characterData.ContainsKey(c) == false)
                {
                    continue;
                }
                GlyphData g = characterData[c];
                xoffset  += g.Kerning.Y + g.Kerning.Z + Spacing;
                newHeight = g.Cropping.Height + yoffset;
                if (newHeight > v.Y)
                {
                    v.Y = newHeight;
                }
                if (xoffset > v.X)
                {
                    v.X = xoffset;
                }
            }
            return(v);
        }
Example #3
0
 public SpriteFont(Texture2D texture, List<Rectangle>glyphs, List<Rectangle>cropping, List<char>charMap, int lineSpacing, float spacing, List<Vector3>kerning, char? defaultCharacter)
 {
     _texture = texture;
     LineSpacing = lineSpacing;
     Spacing = spacing;
     _defaultCharacter = defaultCharacter;
     for (int i = 0; i < charMap.Count; i++)
     {
         GlyphData g = new GlyphData();
         g.Glyph = glyphs[i];
         g.Cropping = cropping[i];
         g.Kerning = kerning[i];
         g.CharacterIndex = charMap[i];
         characterData.Add(g.CharacterIndex, g);
     }
 }
Example #4
0
        public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color)
        {
            if (spriteFont == null)
            {
                throw new ArgumentException("spriteFont");
            }

            Vector2 p = position;

            foreach (char c in text)
            {
                if (c == '\n')
                {
                    p.Y += spriteFont.LineSpacing;
                    p.X  = position.X;
                    continue;
                }
                if (spriteFont.characterData.ContainsKey(c) == false)
                {
                    continue;
                }
                GlyphData g = spriteFont.characterData[c];

                SpriteBatchItem item = _batcher.CreateBatchItem();

                item.Depth     = 0.0f;
                item.TextureID = (int)spriteFont._texture.ID;

                texCoordTL.X = spriteFont._texture.Image.GetTextureCoordX(g.Glyph.X);
                texCoordTL.Y = spriteFont._texture.Image.GetTextureCoordY(g.Glyph.Y);
                texCoordBR.X = spriteFont._texture.Image.GetTextureCoordX(g.Glyph.X + g.Glyph.Width);
                texCoordBR.Y = spriteFont._texture.Image.GetTextureCoordY(g.Glyph.Y + g.Glyph.Height);

                item.Set
                (
                    p.X,
                    p.Y + g.Cropping.Y,
                    g.Glyph.Width,
                    g.Glyph.Height,
                    color,
                    texCoordTL,
                    texCoordBR
                );

                p.X += (g.Kerning.Y + g.Kerning.Z + spriteFont.Spacing);
            }
        }
Example #5
0
        internal void InternalDrawString(SpriteBatch sb, string text, Vector2 position,
                                         Color color, float rotation, Vector2 origin, Vector2 scale,
                                         SpriteEffects effects, float layerDepth)
        {
            if (effects != SpriteEffects.None)
            {
                throw new NotImplementedException("Flipped text is not implemented");
            }

            Vector2 offset    = origin / internalScale;
            bool    lineStart = true;

            foreach (char c in text)
            {
                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.Y -= LineSpacing;
                    offset.X  = origin.X;
                    lineStart = true;
                    continue;
                }

                if (characterData.ContainsKey(c) == false)
                {
                    continue;                     // TODO: Make this match XNA behaviour
                }
                GlyphData g = characterData[c];

                if (!lineStart)
                {
                    offset.X -= g.Kerning.X;                     // add A spacing
                }
                sb.InternalDraw(texture, position, g.Glyph,
                                color, rotation, offset - new Vector2(g.Cropping.X, g.Cropping.Y),
                                scale * internalScale, SpriteEffects.None, layerDepth);

                offset.X -= (g.Kerning.Y + g.Kerning.Z + Spacing);                 // add B, C and user spacing

                lineStart = false;
            }
        }
Example #6
0
        SpriteFont
#endif
        (Texture2D texture, Stream metricsDataStream, float internalScale)
        {
            this.texture       = texture;
            this.internalScale = internalScale;

            using (BinaryReader br = new BinaryReader(metricsDataStream, Encoding.Unicode))
            {
                // Check for signature "ExEnFont"
                if (br.ReadInt32() != 0x6E457845 || br.ReadInt32() != 0x746E6F46)
                {
                    throw new ContentLoadException("Invalid ExEn font metrics file");
                }
                if (br.ReadInt32() != 0)
                {
                    throw new ContentLoadException("Invalid version of ExEn font metrics file");
                }

                // Read common properties:
                LineSpacing = br.ReadInt32();
                Spacing     = br.ReadInt32();
                if (br.ReadBoolean())
                {
                    DefaultCharacter = br.ReadChar();
                }
                else
                {
                    DefaultCharacter = null;
                }

                // Read glyph list:
                int count = br.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    char      c = br.ReadChar();
                    GlyphData g = new GlyphData();
                    g.Glyph    = br.ReadRectangle();
                    g.Cropping = br.ReadRectangle();
                    g.Kerning  = br.ReadVector3();
                    characterData.Add(c, g);
                }
            }
        }
Example #7
0
        public void DrawString
        (
            SpriteFont spriteFont,
            string text,
            Vector2 position,
            Color color,
            float rotation,
            Vector2 origin,
            Vector2 scale,
            SpriteEffects effects,
            float depth
        )
        {
            if (spriteFont == null)
            {
                throw new ArgumentException("spriteFont");
            }

            Vector2 p = new Vector2(-origin.X, -origin.Y);

            float sin = (float)Math.Sin(rotation);
            float cos = (float)Math.Cos(rotation);

            foreach (char c in text)
            {
                if (c == '\n')
                {
                    p.Y += spriteFont.LineSpacing;
                    p.X  = -origin.X;
                    continue;
                }
                if (spriteFont.characterData.ContainsKey(c) == false)
                {
                    continue;
                }
                GlyphData g = spriteFont.characterData[c];

                SpriteBatchItem item = _batcher.CreateBatchItem();

                item.Depth     = depth;
                item.TextureID = (int)spriteFont._texture.ID;

                Vector2 texCoordTL = spriteFont._texture.Image.GetTextureCoord(g.Glyph.X, g.Glyph.Y);
                Vector2 texCoordBR = spriteFont._texture.Image.GetTextureCoord(g.Glyph.X + g.Glyph.Width, g.Glyph.Y + g.Glyph.Height);

                if (effects == SpriteEffects.FlipVertically)
                {
                    float temp = texCoordBR.Y;
                    texCoordBR.Y = texCoordTL.Y;
                    texCoordTL.Y = temp;
                }
                else if (effects == SpriteEffects.FlipHorizontally)
                {
                    float temp = texCoordBR.X;
                    texCoordBR.X = texCoordTL.X;
                    texCoordTL.X = temp;
                }

                item.Set
                (
                    position.X,
                    position.Y,
                    p.X * scale.X,
                    (p.Y + g.Cropping.Y) * scale.Y,
                    g.Glyph.Width * scale.X,
                    g.Glyph.Height * scale.Y,
                    sin,
                    cos,
                    color,
                    texCoordTL,
                    texCoordBR
                );

                p.X += (g.Kerning.Y + g.Kerning.Z + spriteFont.Spacing);
            }
        }