Пример #1
0
        private Vector2 CalculateTextSize(SpriteFont.StringProxy textToMeasure)
        {
            if (Font == null)
            {
                return(Vector2.Zero);
            }

            var sizeRatio       = LayoutingContext?.RealVirtualResolutionRatio ?? Vector2.One;
            var measureFontSize = new Vector2(sizeRatio.Y * ActualTextSize); // we don't want letters non-uniform ratio
            var realSize        = Font.MeasureString(ref textToMeasure, ref measureFontSize);

            // force pre-generation if synchronous generation is required
            if (SynchronousCharacterGeneration)
            {
                Font.PreGenerateGlyphs(ref textToMeasure, ref measureFontSize);
            }

            if (Font.FontType == SpriteFontType.Dynamic)
            {
                // rescale the real size to the virtual size
                realSize.X /= sizeRatio.X;
                realSize.Y /= sizeRatio.Y;
            }
            else if (Font.Size > 0f)
            {
                var scaleRatio = ActualTextSize / Font.Size;
                realSize.X *= scaleRatio;
                realSize.Y *= scaleRatio;
            }

            return(realSize);
        }
Пример #2
0
        private Vector2 CalculateTextSize(SpriteFont.StringProxy textToMeasure)
        {
            if (Font == null)
            {
                return(Vector2.Zero);
            }

            var sizeRatio       = RealSizeVirtualResolutionRatio;
            var measureFontSize = TextSize * sizeRatio;
            var realSize        = Font.MeasureString(ref textToMeasure, ref measureFontSize);

            // force pre-generation if synchronous generation is required
            if (SynchronousCharacterGeneration)
            {
                Font.PreGenerateGlyphs(ref textToMeasure, ref measureFontSize);
            }

            if (Font.IsDynamic)
            {
                // rescale the real size to the virtual size
                realSize.X /= sizeRatio.X;
                realSize.Y /= sizeRatio.Y;
            }

            return(realSize);
        }
Пример #3
0
        private void DrawString(SpriteFont spriteFont, ref SpriteFont.StringProxy text, float fontSize, ref Vector2 position, ref Color4 color, float rotation, ref Vector2 origin, ref Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
        {
            if (spriteFont == null)
            {
                throw new ArgumentNullException("spriteFont");
            }
            if (text.IsNull)
            {
                throw new ArgumentNullException("text");
            }
            if (fontSize < 0)
            {
                fontSize = spriteFont.Size;
            }

            // calculate the resolution ratio between the screen real size and the virtual resolution
            var commandList       = GraphicsContext.CommandList;
            var viewportSize      = commandList.Viewport;
            var virtualResolution = GetCurrentResolution(commandList);
            var resolutionRatio   = new Vector2(viewportSize.Width / virtualResolution.X, viewportSize.Height / virtualResolution.Y);

            scale.X = scale.X / resolutionRatio.X;
            scale.Y = scale.Y / resolutionRatio.Y;

            var fontSize2   = fontSize * ((spriteFont.FontType == SpriteFontType.Dynamic) ? resolutionRatio : Vector2.One);
            var drawCommand = new SpriteFont.InternalDrawCommand(this, in fontSize2, in position, in color, rotation, in origin, in scale, effects, layerDepth);

            // snap the position the closest 'real' pixel
            Vector2.Modulate(ref drawCommand.Position, ref resolutionRatio, out drawCommand.Position);
            drawCommand.Position.X  = (float)Math.Round(drawCommand.Position.X);
            drawCommand.Position.Y  = (float)Math.Round(drawCommand.Position.Y);
            drawCommand.Position.X /= resolutionRatio.X;
            drawCommand.Position.Y /= resolutionRatio.Y;

            spriteFont.InternalDraw(commandList, ref text, ref drawCommand, alignment);
        }
Пример #4
0
 private void DrawString(SpriteFont spriteFont, ref SpriteFont.StringProxy text, float fontSize, ref Vector2 position, ref Color4 color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
 {
     DrawString(spriteFont, ref text, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
 }
Пример #5
0
        /// <summary>Adds a string to a batch of sprites for rendering using the specified font, text, position, color, rotation, origin, scale, effects and layer.</summary>
        /// <param name="spriteFont">A font for displaying text.</param>
        /// <param name="text">Text string.</param>
        /// <param name="fontSize">The font size in pixels (ignored in the case of static fonts)</param>
        /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
        /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
        /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
        /// <param name="origin">The sprite origin in virtual pixels; the default is (0,0) which represents the upper-left corner.</param>
        /// <param name="scale">Scale factor.</param>
        /// <param name="effects">Effects to apply.</param>
        /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
        /// <param name="alignment">Describes how to align the text to draw</param>
        public void DrawString(SpriteFont spriteFont, StringBuilder text, float fontSize, Vector2 position, Color4 color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
        {
            var proxy = new SpriteFont.StringProxy(text);

            DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
        }
Пример #6
0
        /// <summary>Adds a string to a batch of sprites for rendering using the specified font, text, position, and color.</summary>
        /// <param name="spriteFont">A font for displaying text.</param>
        /// <param name="text">Text string.</param>
        /// <param name="fontSize">The font size in pixels (ignored in the case of static fonts)</param>
        /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
        /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
        /// <param name="alignment">Describes how to align the text to draw</param>
        public void DrawString(SpriteFont spriteFont, StringBuilder text, float fontSize, Vector2 position, Color4 color, TextAlignment alignment = TextAlignment.Left)
        {
            var proxy = new SpriteFont.StringProxy(text);

            DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f, alignment);
        }