Skip to content

System.Drawing.Common and RTL support #25573

@VahidN

Description

@VahidN

Currently System.Drawing.Common version "4.5.0-preview1-26216-02" doesn't support RTL on operating systems other than Windows. Here is a sample to demonstrate this issue:

SystemDrawingCommonRtlTest.csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="4.5.0-preview1-26216-02" />
  </ItemGroup>
</Project>

Program.cs file

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;

namespace SystemDrawingCommonRtlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var text = "آزمايش"; // = Test
            var tahomaFont = new Font("Tahoma", 40, FontStyle.Regular, GraphicsUnit.Pixel);

            var (height, width) = getImageSize(text, tahomaFont);

            using (var pic = new Bitmap(width, height))
            {
                using (var graphics = Graphics.FromImage(pic))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode = InterpolationMode.High;
                    graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

                    using (var font = tahomaFont)
                    {
                        using (var format = new StringFormat())
                        {
                            format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                            graphics.DrawString(
                                text,
                                font,
                                new SolidBrush(Color.Black),
                                new Rectangle(0, 0, width, height),
                                format);

                            pic.Save($"rtl-text-{RuntimeInformation.OSDescription}.png", ImageFormat.Png);
                        }
                    }
                }
            }
        }

        private static (int Height, int Width) getImageSize(string text, Font font)
        {
            SizeF size;
            using (var bmp = new Bitmap(1, 1))
            {
                using (var g = Graphics.FromImage(bmp))
                {
                    size = g.MeasureString(text, font);
                }
            }

            const int margin = 8;
            return ((int)size.Height + margin, (int)size.Width + margin);
        }
    }
}

And here are its output
On Windows 10: It works correctly
rtl-text-microsoft windows 10 0 16299

On Linux (Ubuntu): Characters are separated and rotated
rtl-text-linux 4 4 0-31-generic 50-ubuntu smp wed jul 13 00 07 12 utc 2016

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions