public void DrawText(FontState state, SpriteBatch spriteBatch, Vector2 dest, string text) { state.Location = dest; state.Text = text; FontSurface(state).DrawText(state, spriteBatch); }
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; }
/// <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))); }
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); }
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); }
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); } }
public void OnStyleChanged(FontState fontState) { }
public void OnSizeChanged(FontState fontState) { }
public void OnColorChanged(FontState fontState) { }
public void OnLocationChanged(FontState fontState) { NeedsRefresh = true; }
public void OnDisplayAlignmentChanged(FontState fontState) { NeedsRefresh = true; }
public void OnTextChanged(FontState fontState) { NeedsRefresh = true; }
/// <summary> /// Returns the height of characters in the font. /// </summary> public int FontHeight(FontState state) { return((int)(mCharHeight * state.ScaleHeight)); }
public int FontHeight(FontState state) { var surface = FontSurface(state); return(surface.FontHeight(state)); }
public Size MeasureString(FontState state, string text) { return(FontSurface(state).MeasureString(state, text)); }