使用路径绘制时如何生成从右到左的文本

How to generate a Right to Left text when it is drawn using a path

我正在尝试生成 RTL(从右到左布局)的文本。如果我使用 drawString 会很容易。我只是将字符串的所有字符保存在一个数组中,然后反向打印该数组。但是我正在使用的是一个矩阵,它可以转换路径然后绘制路径。如何使用文本绘制方法进行 RTL?

这是我的代码供您参考:

using (StringFormat string_format = new StringFormat())
        {
            SizeF stringSize = e.Graphics.MeasureString(text, _fontStyle);
            rect.Location = new PointF(Shape.center.X - (rect.Width / 2), Shape.center.Y - (rect.Height / 2));
            GraphicsContainer gc = e.Graphics.BeginContainer();
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            //e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(rect));

            RectangleF r = new RectangleF(rect.Location, rect.Size);
            GraphicsPath path = new GraphicsPath();
            if (text == "" || text == " ")
                path.Dispose(); //Disposes the path to prevent OutOfMemoryExcetption
            else
            {
                path.AddString(text, _fontStyle.FontFamily, Convert.ToInt32(_fontStyle.Style), rect.Height, rect.Location, string_format);
                RectangleF text_rectf = path.GetBounds();
                PointF[] target_pts = {
                            new PointF(r.Left, r.Top),
                            new PointF(r.Right, r.Top),
                            new PointF(r.Left, r.Bottom)};
                //e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(r));
                using (Matrix m = new Matrix(text_rectf, target_pts))
                using (Matrix rotate = new Matrix(1, 0, 0, 1, Offset.X, Offset.Y)) 
                using (Matrix move = new Matrix())
                using (Matrix FlipXMatrix = new Matrix(-1, 0, 0, 1, 500, 0)) 
                using (Matrix FlipYMatrix = new Matrix(1, 0, 0, -1, 0, 500))
                using (Matrix TransformMatrix = new Matrix())
                {
                    TransformMatrix.Multiply(move);
                    rotate.RotateAt(angle, new PointF(250, 250 - Offset.Y));
                    TransformMatrix.Multiply(rotate);
                    if (flipped)
                        TransformMatrix.Multiply(FlipXMatrix);
                    TransformMatrix.Multiply(m); 
                    path.Transform(TransformMatrix);
                    //Checks if the user wants the text filled or outlined
                    if (!isOutlined)
                        e.Graphics.FillPath(Brushes.Red, path);
                    else
                        e.Graphics.DrawPath(Pens.Red, path);
                } 
            }
        e.Graphics.EndContainer(gc);
        }

PS. I use Boolean to check whether the user wants to RTL or not.

您可以简单地反转字符串,使用 string.ToCharArray.Reverse()

string SampleText = "This is a Sample Text";
string InvertedSample = string.Join("", SampleText.ToCharArray().Reverse());

InvertedSample 字符串打印:

txeT elpmaS a si sihT