Esempio n. 1
0
        public void DrawText(FontState state, SpriteBatch spriteBatch, Vector2 dest, string text)
        {
            state.Location = dest;
            state.Text     = text;

            FontSurface(state).DrawText(state, spriteBatch);
        }
Esempio n. 2
0
        private void RefreshCache(FontState state, BitmapFontCache cache)
        {
            if (cache.NeedsRefresh == false)
            {
                return;
            }

            // this variable counts the number of rectangles actually used to display text.
            // It may be less then text.Length because carriage return characters
            // don't need any rects.
            GetRects(cache.SrcRects, cache.DestRects, out cache.DisplayTextLength,
                     state.Text, state.ScaleHeight, state.ScaleWidth);

            Vector2 dest = state.Location;

            if (state.TextAlignment != OriginAlignment.TopLeft)
            {
                Point value = Origin.Calc(state.TextAlignment,
                                          MeasureString(state, state.Text));

                dest.X -= value.X;
                dest.Y -= value.Y;
            }

            for (int i = 0; i < cache.DisplayTextLength; i++)
            {
                cache.DestRects[i].X += (int)dest.X;
                cache.DestRects[i].Y += (int)dest.Y;
            }

            cache.NeedsRefresh = false;
        }
Esempio n. 3
0
        /// <summary>
        /// Measures the string based on how it would be drawn with the
        /// specified FontState object.
        /// </summary>
        /// <param name="state"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public Size MeasureString(FontState state, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            int    carriageReturnCount = 0;
            int    i = 0;
            double highestLineWidth = 0;

            // measure width
            string[] lines = text.Split('\n');
            for (i = 0; i < lines.Length; i++)
            {
                string line      = lines[i];
                double lineWidth = 0;

                for (int j = 0; j < line.Length; j++)
                {
                    if (mFontMetrics.ContainsKey(line[j]) == false)
                    {
                        continue;
                    }

                    lineWidth += mFontMetrics[line[j]].LayoutWidth;
                }

                if (lineWidth > highestLineWidth)
                {
                    highestLineWidth = lineWidth;
                }
            }

            // measure height
            i = 0;
            do
            {
                i = text.IndexOf('\n', i + 1);

                if (i == -1)
                {
                    break;
                }

                carriageReturnCount++;
            } while (i != -1);

            if (text[text.Length - 1] == '\n')
            {
                carriageReturnCount--;
            }

            return(new Size((int)Math.Ceiling(highestLineWidth * state.ScaleWidth),
                            (int)(mCharHeight * (carriageReturnCount + 1) * state.ScaleHeight)));
        }
Esempio n. 4
0
        public IFontTexture FontSurface(FontState state)
        {
            var settings = GetClosestFontSettings(state.Settings);
            var result   = fontTextures[settings];

            var ratio = state.Settings.Size / (double)settings.Size;

            state.ScaleHeight = ratio;
            state.ScaleWidth  = ratio;

            return(result);
        }
Esempio n. 5
0
        private static BitmapFontCache GetCache(FontState state)
        {
            BitmapFontCache cache = state.Cache as BitmapFontCache;

            if (cache == null)
            {
                cache       = new BitmapFontCache();
                state.Cache = cache;
            }

            if (cache.SrcRects == null ||
                cache.SrcRects.Length < state.Text.Length)
            {
                cache.SrcRects  = new Rectangle[state.Text.Length];
                cache.DestRects = new Rectangle[state.Text.Length];
            }

            return(cache);
        }
Esempio n. 6
0
        public void DrawText(FontState state, SpriteBatch spriteBatch)
        {
            if (string.IsNullOrEmpty(state.Text))
            {
                return;
            }

            BitmapFontCache cache = GetCache(state);

            RefreshCache(state, cache);

            for (int i = 0; i < cache.DisplayTextLength; i++)
            {
                spriteBatch.Draw(
                    texture,
                    cache.DestRects[i],
                    cache.SrcRects[i],
                    state.Color);
            }
        }
Esempio n. 7
0
 public void OnStyleChanged(FontState fontState)
 {
 }
Esempio n. 8
0
 public void OnSizeChanged(FontState fontState)
 {
 }
Esempio n. 9
0
 public void OnColorChanged(FontState fontState)
 {
 }
Esempio n. 10
0
 public void OnLocationChanged(FontState fontState)
 {
     NeedsRefresh = true;
 }
Esempio n. 11
0
 public void OnDisplayAlignmentChanged(FontState fontState)
 {
     NeedsRefresh = true;
 }
Esempio n. 12
0
 public void OnTextChanged(FontState fontState)
 {
     NeedsRefresh = true;
 }
Esempio n. 13
0
 /// <summary>
 /// Returns the height of characters in the font.
 /// </summary>
 public int FontHeight(FontState state)
 {
     return((int)(mCharHeight * state.ScaleHeight));
 }
Esempio n. 14
0
        public int FontHeight(FontState state)
        {
            var surface = FontSurface(state);

            return(surface.FontHeight(state));
        }
Esempio n. 15
0
 public Size MeasureString(FontState state, string text)
 {
     return(FontSurface(state).MeasureString(state, text));
 }