Пример #1
0
        public void DrawText(Point anchor, Drawing.Text.TextAlignment alignment, IList <TextRun> textRuns)
        {
            double totalWidth  = 0d;
            double totalHeight = 0d;

            foreach (TextRun run in textRuns)
            {
                FormattedText ft = new FormattedText(run.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(RenderHelper.CircuitFont()), run.Formatting.Size, Brushes.Black);
                if (run.Formatting.FormattingType == TextRunFormattingType.Subscript)
                {
                    ft = new FormattedText(run.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(RenderHelper.CircuitFont()), run.Formatting.Size / 1.5, Brushes.Black);
                }
                totalWidth += ft.Width;
                if (ft.Height > totalHeight)
                {
                    totalHeight = ft.Height;
                }
            }

            var renderLocation = anchor.ToWinPoint();

            if (alignment == TextAlignment.TopCentre || alignment == TextAlignment.CentreCentre || alignment == TextAlignment.BottomCentre)
            {
                renderLocation.X -= totalWidth / 2;
            }
            else if (alignment == TextAlignment.TopRight || alignment == TextAlignment.CentreRight || alignment == TextAlignment.BottomRight)
            {
                renderLocation.X -= totalWidth;
            }
            if (alignment == TextAlignment.CentreLeft || alignment == TextAlignment.CentreCentre || alignment == TextAlignment.CentreRight)
            {
                renderLocation.Y -= totalHeight / 2;
            }
            else if (alignment == TextAlignment.BottomLeft || alignment == TextAlignment.BottomCentre || alignment == TextAlignment.BottomRight)
            {
                renderLocation.Y -= totalHeight;
            }

            double horizontalOffsetCounter = 0;

            foreach (TextRun run in textRuns)
            {
                if (run.Formatting.FormattingType == TextRunFormattingType.Normal)
                {
                    FormattedText formattedText = new FormattedText(run.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(RenderHelper.CircuitFont()), run.Formatting.Size, Brushes.Black);
                    Context.DrawText(formattedText, System.Windows.Point.Add(renderLocation, new Vector(horizontalOffsetCounter, 0d)));
                    horizontalOffsetCounter += formattedText.Width;
                }
                else if (run.Formatting.FormattingType == TextRunFormattingType.Subscript)
                {
                    FormattedText formattedText = new FormattedText(run.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(RenderHelper.CircuitFont()), run.Formatting.Size / 1.5, Brushes.Black);
                    Context.DrawText(formattedText, System.Windows.Point.Add(renderLocation, new Vector(horizontalOffsetCounter, totalHeight - formattedText.Height)));
                    horizontalOffsetCounter += formattedText.Width;
                }
                else if (run.Formatting.FormattingType == TextRunFormattingType.Superscript)
                {
                    FormattedText formattedText = new FormattedText(run.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(RenderHelper.CircuitFont()), run.Formatting.Size / 1.5, Brushes.Black);
                    Context.DrawText(formattedText, System.Windows.Point.Add(renderLocation, new Vector(horizontalOffsetCounter, -3d)));
                    horizontalOffsetCounter += formattedText.Width;
                }
            }
        }