0

I have an Arabic string, which is result of concatenation of Arabic string and " : 100". This string is measured and drawn incorrectly. Why?

public partial class Form1 : Form {
    string strIncorrectMeasure = "مەھسۇلات باھاسى" + " : " + "100";//"مەھسۇلات باھاسى : 100";
    string strCorrectMeasure = "100 : مەھسۇلات باھاسى";
    Font font = new Font("Oybab tuz", 18);

    public Form1() {
        InitializeComponent();
    }

    void button1_Click(object sender, EventArgs e) {
        var bitmap = new Bitmap(100, 100);
        var graphics = Graphics.FromImage(bitmap);
        StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft | StringFormatFlags.NoFontFallback | StringFormatFlags.NoClip);
        SizeF measuredIcorrectSize = graphics.MeasureString(strIncorrectMeasure, font, 0, format);
        SizeF measuredCorrectSize = graphics.MeasureString(strCorrectMeasure, font);
        MessageBox.Show(string.Format("FirstString : {0}\nSecondString: {1}", measuredIcorrectSize, measuredCorrectSize));
    }
    void Form1_Paint(object sender, PaintEventArgs e) {
        var font = new Font("Oybab tuz", 18);           
        StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
        e.Graphics.DrawString(this.strIncorrectMeasure, font, Brushes.Black, new PointF(300, 10), format);
        e.Graphics.DrawString(this.strCorrectMeasure, font, Brushes.Black, new PointF(10, 50));
    }
}

Is is it possible that this problem is caused by this specific font?

Eugene Maksimov
  • 1,504
  • 17
  • 36

2 Answers2

0

I have not found a solution. I think, that problem in the font itself. Other fonts works fine.

Eugene Maksimov
  • 1,504
  • 17
  • 36
-1

I couldn't find the Oybab tuz font. However, using SystemFonts.MenuFont and SystemFonts.DefaultFont, both sizes come out the same.

Using Graphics.MeasureString and the MenuFont returned a value of 162.1289. However, taking a screenshot and measuring the true width in a bitmap editor results in a width of 155 pixels. If you need the true width then you will need to draw the text to a bitmap and then find the bounding rectangle by looking at the pixel values.

Also, you don't need to create a Bitmap to get a Graphics object. Simply call CreateGraphics(). Also, you should be disposing the objects by wrapping them in using statements.

Loathing
  • 5,109
  • 3
  • 24
  • 35
  • I don't have any problem with fonts which are pre-installed on my Windows 8. – Eugene Maksimov Aug 20 '15 at 09:29
  • Possibly the font is not setup to handle right-to-left. If the other fonts work correctly, then it points to an issue with that font. – Loathing Aug 20 '15 at 09:37
  • Loathing, how to check it? Also I found that TextRender.DrawText() works correctly. (But we use GDI+ in our application) – Eugene Maksimov Aug 20 '15 at 15:04
  • A good place to start would be doing some research into how fonts support Arabic text. Possibly certain metrics need to be included in the Font data. Possibly contact the designer of the Font you are using, especially if you have paid for it. Google for { "how to" create font arabic } came up with this link: http://www.khtt.net/attachment/31760 – Loathing Aug 20 '15 at 20:40