public float DrawText(IFontStashRenderer batch, float x, float y, StringBuilder str, Color[] glyphColors, Vector2 scale, float depth = 0.0f) { if (str == null || str.Length == 0) { return(0.0f); } float ascent, lineHeight; PreDraw(str, out ascent, out lineHeight); float originX = 0.0f; float originY = 0.0f; originY += ascent; FontGlyph prevGlyph = null; var pos = 0; var q = new FontGlyphSquad(); for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1) { var codepoint = StringBuilderConvertToUtf32(str, i); if (codepoint == '\n') { originX = 0.0f; originY += lineHeight; prevGlyph = null; ++pos; continue; } var glyph = GetGlyph(codepoint, false); if (glyph == null) { ++pos; continue; } GetQuad(glyph, prevGlyph, scale, ref originX, ref originY, ref q); if (!glyph.IsEmpty) { var destRect = new Rectangle((int)(x + q.X0), (int)(y + q.Y0), (int)(q.X1 - q.X0), (int)(q.Y1 - q.Y0)); var sourceRect = new Rectangle((int)q.S0, (int)q.T0, (int)(q.S1 - q.S0), (int)(q.T1 - q.T0)); batch.Draw(glyph.Atlas.Texture, destRect, sourceRect, glyphColors[pos], depth); } prevGlyph = glyph; ++pos; } return(x); }
/// <summary> /// Draws a text /// </summary> /// <param name="renderer">A renderer.</param> /// <param name="text">The text which will be drawn.</param> /// <param name="position">The drawing location on screen.</param> /// <param name="color">A color mask.</param> /// <param name="rotation">A rotation of this text in radians.</param> /// <param name="origin">Center of the rotation.</param> /// <param name="scale">A scaling of this text.</param> /// <param name="layerDepth">A depth of the layer of this string.</param> public float DrawText(IFontStashRenderer renderer, StringBuilder text, Vector2 position, Color color, Vector2 scale, float rotation, Vector2 origin, float layerDepth = 0.0f) { if (text == null || text.Length == 0) { return(0.0f); } float ascent, lineHeight; PreDraw(text, out ascent, out lineHeight); float originX = 0.0f; float originY = 0.0f; originY += ascent; FontGlyph prevGlyph = null; var q = new FontGlyphSquad(); for (int i = 0; i < text.Length; i += StringBuilderIsSurrogatePair(text, i) ? 2 : 1) { var codepoint = StringBuilderConvertToUtf32(text, i); if (codepoint == '\n') { originX = 0.0f; originY += lineHeight; prevGlyph = null; continue; } var glyph = GetGlyph(codepoint, false); if (glyph == null) { continue; } GetQuad(glyph, prevGlyph, scale, ref originX, ref originY, ref q); if (!glyph.IsEmpty) { var sourceRect = new Rectangle((int)q.S0, (int)q.T0, (int)(q.S1 - q.S0), (int)(q.T1 - q.T0)); renderer.Draw(glyph.Texture, position, sourceRect, color, rotation, origin - q.Offset, scale, layerDepth); } prevGlyph = glyph; } return(position.X); }
/// <summary> /// Draws a text /// </summary> /// <param name="renderer">A renderer.</param> /// <param name="text">The text which will be drawn.</param> /// <param name="position">The drawing location on screen.</param> /// <param name="color">A color mask.</param> /// <param name="rotation">A rotation of this text in radians.</param> /// <param name="origin">Center of the rotation.</param> /// <param name="scale">A scaling of this text.</param> /// <param name="layerDepth">A depth of the layer of this string.</param> public float DrawText(IFontStashRenderer renderer, string text, Vector2 position, Color color, Vector2 scale, float rotation, Vector2 origin, float layerDepth = 0.0f) { #if MONOGAME || FNA || STRIDE if (renderer.GraphicsDevice == null) { throw new ArgumentNullException("renderer.GraphicsDevice can't be null."); } #else if (renderer.TextureManager == null) { throw new ArgumentNullException("renderer.TextureManager can't be null."); } #endif if (string.IsNullOrEmpty(text)) { return(0.0f); } scale /= RenderFontSizeMultiplicator; int ascent, lineHeight; PreDraw(text, out ascent, out lineHeight); float originX = 0.0f; float originY = 0.0f; originY += ascent; FontGlyph prevGlyph = null; var q = new FontGlyphSquad(); for (int i = 0; i < text.Length; i += char.IsSurrogatePair(text, i) ? 2 : 1) { var codepoint = char.ConvertToUtf32(text, i); if (codepoint == '\n') { originX = 0.0f; originY += lineHeight; prevGlyph = null; continue; } #if MONOGAME || FNA || STRIDE var glyph = GetGlyph(renderer.GraphicsDevice, codepoint); #else var glyph = GetGlyph(renderer.TextureManager, codepoint); #endif if (glyph == null) { continue; } GetQuad(glyph, prevGlyph, ref originX, originY, ref q); if (!glyph.IsEmpty) { var sourceRect = new Rectangle((int)q.S0, (int)q.T0, (int)(q.S1 - q.S0), (int)(q.T1 - q.T0)); renderer.Draw(glyph.Texture, position, sourceRect, color, rotation, origin * (float)RenderFontSizeMultiplicator - q.Offset, scale, layerDepth); } prevGlyph = glyph; } return(position.X); }