Search code examples
c#stringmatrixpathright-to-left

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


I am trying to generate a text which is RTL (Right to Left Layout). It would've been easy if I use drawString. I'll just save all characters of the string in an array and then print the array in reverse. But what I'm using is a matrix that transforms a path then draws the path. How do I do RTL using may text drawing approach?

here's the my code for your reference:

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.


Solution

  • You could simply invert the string, using string.ToCharArray.Reverse()

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

    The InvertedSample string prints:

    txeT elpmaS a si sihT