/// <summary>
            /// Creates a new <see cref="GlyphRenderState"/> structure from the parameters to the <see cref="DrawStringInternal{TSource}"/> method.
            /// </summary>
            public static GlyphRenderState FromDrawStringParameters(UltravioletFontFace fontFace,
                                                                    Vector2 position, Vector2 origin, Vector2 scale, Single rotation, SpriteEffects effects, Size2 measure)
            {
                // Determine whether the text is flipped.
                var isFlippedHorizontally = (effects & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;
                var isFlippedVertically   = (effects & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;

                // Calculate the text area from its measurement.
                var areaTL = new Vector2(position.X - origin.X, position.Y - origin.Y);
                var areaBR = new Vector2(position.X - origin.X + measure.Width, position.Y - origin.Y + measure.Height);

                // Calculate the transformation matrix.
                var transformRotation = Matrix.CreateRotationZ(rotation);
                var transformScale    = Matrix.CreateScale(scale.X, scale.Y, 1f);

                Matrix.Multiply(ref transformRotation, ref transformScale, out var transform);

                // Transform the text area.
                Vector2.Transform(ref areaTL, ref transform, out var areaTransformedTL);
                Vector2.Transform(ref areaBR, ref transform, out var areaTransformedBR);
                var areaSize            = new Size2F(areaBR.X - areaTL.X, areaBR.Y - areaTL.Y);
                var areaTransformedSize = new Size2F(
                    areaTransformedBR.X - areaTransformedTL.X,
                    areaTransformedBR.Y - areaTransformedTL.Y);
                var textStartPosition = new Vector2(
                    isFlippedHorizontally ? areaTransformedBR.X : areaTransformedTL.X,
                    isFlippedVertically ? areaTransformedBR.Y : areaTransformedTL.Y);

                // Create the new render state object.
                return(new GlyphRenderState()
                {
                    FontFace = fontFace,
                    TextStartPosition = textStartPosition,
                    TextRenderPosition = textStartPosition,
                    TextPosition = position,
                    TextOrigin = origin,
                    TextScale = scale,
                    TextIsFlippedHorizontally = isFlippedHorizontally,
                    TextIsFlippedVertically = isFlippedVertically,
                    TextDirection = new Vector2(isFlippedHorizontally ? -1 : 1, isFlippedVertically ? -1 : 1),
                    TextTransform = transform,
                    TextArea = new RectangleF(areaTL, areaSize),
                    TextAreaTransformed = new RectangleF(areaTransformedTL, areaTransformedSize),
                });
            }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UltravioletFont{TFontFace}"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="regular">The font's regular font face.</param>
        /// <param name="bold">The font's bold font face.</param>
        /// <param name="italic">The font's italic font face.</param>
        /// <param name="boldItalic">The font's bold italic font face.</param>
        protected UltravioletFont(UltravioletContext uv, UltravioletFontFace regular, UltravioletFontFace bold, UltravioletFontFace italic, UltravioletFontFace boldItalic)
            : base(uv)
        {
            uv.ValidateResource(regular);
            uv.ValidateResource(bold);
            uv.ValidateResource(italic);
            uv.ValidateResource(boldItalic);

            if (regular == null && bold == null && italic == null && boldItalic == null)
            {
                throw new ArgumentException(UltravioletStrings.InvalidFontFaces);
            }

            this.Regular    = regular;
            this.Bold       = bold;
            this.Italic     = italic;
            this.BoldItalic = boldItalic;
        }