Esempio n. 1
0
        /// <summary>
        /// Creates the text line metrics.
        /// </summary>
        /// <param name="textRuns">The text runs.</param>
        /// <param name="paragraphWidth">The paragraph width.</param>
        /// <param name="textAlignment">The text alignment.</param>
        /// <returns></returns>
        public static TextLineMetrics Create(IEnumerable <TextRun> textRuns, double paragraphWidth, TextAlignment textAlignment)
        {
            var lineWidth = 0.0;
            var ascent    = 0.0;
            var descent   = 0.0;
            var lineGap   = 0.0;

            foreach (var textRun in textRuns)
            {
                var shapedRun = (ShapedTextRun)textRun;

                lineWidth += shapedRun.Bounds.Width;

                var textFormat = textRun.Style.TextFormat;

                if (ascent > textRun.Style.TextFormat.FontMetrics.Ascent)
                {
                    ascent = textFormat.FontMetrics.Ascent;
                }

                if (descent < textFormat.FontMetrics.Descent)
                {
                    descent = textFormat.FontMetrics.Descent;
                }

                if (lineGap < textFormat.FontMetrics.LineGap)
                {
                    lineGap = textFormat.FontMetrics.LineGap;
                }
            }

            var xOrigin = TextLine.GetParagraphOffsetX(lineWidth, paragraphWidth, textAlignment);

            return(new TextLineMetrics(lineWidth, xOrigin, ascent, descent, lineGap));
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the text line metrics.
        /// </summary>
        /// <param name="textRuns">The text runs.</param>
        /// <param name="textRange">The text range that is covered by the text line.</param>
        /// <param name="paragraphWidth">The paragraph width.</param>
        /// <param name="paragraphProperties">The text alignment.</param>
        /// <returns></returns>
        public static TextLineMetrics Create(IEnumerable <TextRun> textRuns, TextRange textRange, double paragraphWidth,
                                             TextParagraphProperties paragraphProperties)
        {
            var lineWidth = 0.0;
            var ascent    = 0.0;
            var descent   = 0.0;
            var lineGap   = 0.0;

            foreach (var textRun in textRuns)
            {
                var shapedRun = (ShapedTextCharacters)textRun;

                var fontMetrics =
                    new FontMetrics(shapedRun.Properties.Typeface, shapedRun.Properties.FontRenderingEmSize);

                lineWidth += shapedRun.Bounds.Width;

                if (ascent > fontMetrics.Ascent)
                {
                    ascent = fontMetrics.Ascent;
                }

                if (descent < fontMetrics.Descent)
                {
                    descent = fontMetrics.Descent;
                }

                if (lineGap < fontMetrics.LineGap)
                {
                    lineGap = fontMetrics.LineGap;
                }
            }

            var xOrigin = TextLine.GetParagraphOffsetX(lineWidth, paragraphWidth, paragraphProperties.TextAlignment);

            var baselineOrigin = new Point(xOrigin, -ascent);

            var size = new Size(lineWidth,
                                double.IsNaN(paragraphProperties.LineHeight) || MathUtilities.IsZero(paragraphProperties.LineHeight) ?
                                descent - ascent + lineGap :
                                paragraphProperties.LineHeight);

            return(new TextLineMetrics(size, baselineOrigin, textRange));
        }
Esempio n. 3
0
        /// <summary>
        /// Draws the text layout.
        /// </summary>
        /// <param name="context">The drawing context.</param>
        /// <param name="origin">The origin.</param>
        public void Draw(DrawingContext context, Point origin)
        {
            if (!TextLines.Any())
            {
                return;
            }

            var currentY = origin.Y;

            foreach (var textLine in TextLines)
            {
                var offsetX = TextLine.GetParagraphOffsetX(textLine.LineMetrics.Size.Width, Size.Width,
                                                           _paragraphProperties.TextAlignment);

                textLine.Draw(context, new Point(origin.X + offsetX, currentY));

                currentY += textLine.LineMetrics.Size.Height;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Draws the text layout.
        /// </summary>
        /// <param name="context">The drawing context.</param>
        public void Draw(DrawingContext context)
        {
            if (!TextLines.Any())
            {
                return;
            }

            var currentY = 0.0;

            foreach (var textLine in TextLines)
            {
                var offsetX = TextLine.GetParagraphOffsetX(textLine.LineMetrics.Size.Width, Size.Width,
                                                           _paragraphProperties.TextAlignment);

                using (context.PushPostTransform(Matrix.CreateTranslation(offsetX, currentY)))
                {
                    textLine.Draw(context);
                }

                currentY += textLine.LineMetrics.Size.Height;
            }
        }