123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using FontStashSharp.Interfaces;
- using System.Text;
- using System;
- #if MONOGAME || FNA
- using Microsoft.Xna.Framework;
- #elif STRIDE
- using Stride.Core.Mathematics;
- using Stride.Graphics;
- #else
- using System.Numerics;
- using Matrix = System.Numerics.Matrix3x2;
- using Color = Veldrid.RgbaFloat;
- #endif
- namespace FontStashSharp
- {
- public partial class SpriteFontBase
- {
- private void RenderStyle(IFontStashRenderer renderer, TextStyle textStyle, Vector2 pos,
- int lineHeight, int ascent, Color color, ref Matrix transformation, float rotation, Vector2 scale, float layerDepth)
- {
- if (textStyle == TextStyle.None || pos.X == 0)
- {
- return;
- }
- #if MONOGAME || FNA || STRIDE
- var white = GetWhite(renderer.GraphicsDevice);
- #else
- var white = GetWhite(renderer.TextureManager);
- #endif
- var start = Vector2.Zero;
- if (textStyle == TextStyle.Strikethrough)
- {
- start.Y = pos.Y - ascent - FontSystemDefaults.TextStyleLineHeight / 2 + lineHeight / 2;
- }
- else
- {
- start.Y = pos.Y + 1;
- }
- start = start.Transform(ref transformation);
- scale.X *= pos.X;
- scale.Y *= FontSystemDefaults.TextStyleLineHeight;
- renderer.Draw(white, start, null, color, rotation, scale, layerDepth);
- }
- private float DrawText(IFontStashRenderer renderer, TextColorSource source, Vector2 position,
- Vector2? sourceScale, float rotation, Vector2 origin,
- float layerDepth, float characterSpacing, float lineSpacing, TextStyle textStyle)
- {
- if (renderer == null)
- {
- throw new ArgumentNullException(nameof(renderer));
- }
- #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 (source.IsNull) return 0.0f;
- Matrix transformation;
- var scale = sourceScale ?? Utility.DefaultScale;
- Prepare(position, ref scale, rotation, origin, out transformation);
- int ascent, lineHeight;
- PreDraw(source.TextSource, out ascent, out lineHeight);
- var pos = new Vector2(0, ascent);
- FontGlyph prevGlyph = null;
- Color? firstColor = null;
- while (true)
- {
- int codepoint;
- Color color;
- if (!source.GetNextCodepoint(out codepoint))
- break;
- if (codepoint == '\n')
- {
- if (textStyle != TextStyle.None && firstColor != null)
- {
- RenderStyle(renderer, textStyle, pos,
- lineHeight, ascent, firstColor.Value, ref transformation,
- rotation, scale, layerDepth);
- }
- pos.X = 0.0f;
- pos.Y += lineHeight + lineSpacing;
- 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;
- }
- if (prevGlyph != null)
- {
- pos.X += characterSpacing;
- pos.X += GetKerning(glyph, prevGlyph);
- }
- if (!glyph.IsEmpty)
- {
- color = source.GetNextColor();
- firstColor = color;
- var p = pos + new Vector2(glyph.RenderOffset.X, glyph.RenderOffset.Y);
- p = p.Transform(ref transformation);
- renderer.Draw(glyph.Texture,
- p,
- glyph.TextureRectangle,
- color,
- rotation,
- scale,
- layerDepth);
- }
- pos.X += glyph.XAdvance;
- prevGlyph = glyph;
- }
- if (textStyle != TextStyle.None && firstColor != null)
- {
- RenderStyle(renderer, textStyle, pos,
- lineHeight, ascent, firstColor.Value, ref transformation,
- rotation, scale, layerDepth);
- }
- return position.X + pos.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. Null means the scaling is (1, 1)</param>
- /// <param name="layerDepth">A depth of the layer of this string</param>
- /// <param name="characterSpacing">A character spacing</param>
- /// <param name="lineSpacing">A line spacing</param>
- public float DrawText(IFontStashRenderer renderer, string text, Vector2 position, Color color,
- Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
- float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
- TextStyle textStyle = TextStyle.None) =>
- DrawText(renderer, new TextColorSource(text, color), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
- /// <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="colors">Colors of glyphs</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. Null means the scaling is (1, 1)</param>
- /// <param name="layerDepth">A depth of the layer of this string</param>
- /// <param name="characterSpacing">A character spacing</param>
- /// <param name="lineSpacing">A line spacing</param>
- public float DrawText(IFontStashRenderer renderer, string text, Vector2 position, Color[] colors,
- Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
- float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
- TextStyle textStyle = TextStyle.None) =>
- DrawText(renderer, new TextColorSource(text, colors), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
- /// <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. Null means the scaling is (1, 1)</param>
- /// <param name="layerDepth">A depth of the layer of this string</param>
- /// <param name="characterSpacing">A character spacing</param>
- /// <param name="lineSpacing">A line spacing</param>
- public float DrawText(IFontStashRenderer renderer, StringSegment text, Vector2 position, Color color,
- Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
- float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
- TextStyle textStyle = TextStyle.None) =>
- DrawText(renderer, new TextColorSource(text, color), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
- /// <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="colors">Colors of glyphs</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. Null means the scaling is (1, 1)</param>
- /// <param name="layerDepth">A depth of the layer of this string</param>
- /// <param name="characterSpacing">A character spacing</param>
- /// <param name="lineSpacing">A line spacing</param>
- public float DrawText(IFontStashRenderer renderer, StringSegment text, Vector2 position, Color[] colors,
- Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
- float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
- TextStyle textStyle = TextStyle.None) =>
- DrawText(renderer, new TextColorSource(text, colors), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
- /// <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. Null means the scaling is (1, 1)</param>
- /// <param name="layerDepth">A depth of the layer of this string</param>
- /// <param name="characterSpacing">A character spacing</param>
- /// <param name="lineSpacing">A line spacing</param>
- public float DrawText(IFontStashRenderer renderer, StringBuilder text, Vector2 position, Color color,
- Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
- float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
- TextStyle textStyle = TextStyle.None) =>
- DrawText(renderer, new TextColorSource(text, color), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
- /// <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="colors">Colors of glyphs</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. Null means the scaling is (1, 1)</param>
- /// <param name="layerDepth">A depth of the layer of this string</param>
- /// <param name="characterSpacing">A character spacing</param>
- /// <param name="lineSpacing">A line spacing</param>
- public float DrawText(IFontStashRenderer renderer, StringBuilder text, Vector2 position, Color[] colors,
- Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
- float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
- TextStyle textStyle = TextStyle.None) =>
- DrawText(renderer, new TextColorSource(text, colors), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
- }
- }
|