{"id":2323,"date":"2013-12-19T07:00:00","date_gmt":"2013-12-19T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/12\/19\/how-do-i-display-an-rtl-string-in-a-notification-balloon-on-an-ltr-system\/"},"modified":"2013-12-19T07:00:00","modified_gmt":"2013-12-19T07:00:00","slug":"how-do-i-display-an-rtl-string-in-a-notification-balloon-on-an-ltr-system","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131219-00\/?p=2323","title":{"rendered":"How do I display an RTL string in a notification balloon on an LTR system?"},"content":{"rendered":"<p>\nSuppose you have a program that is written in Arabic or Hebrew\nand you want to render some text.\nNo problem.\nYou just call\n<code>Ext&shy;Text&shy;Out<\/code> and pass the\n<code>ETO_RTL&shy;READING<\/code> flag to say,\n&#8220;Please render this string in an RTL context.&#8221;\nMany other text-rendering functions have a similar flag,\nsuch as\n<code>DT_RTL&shy;READING<\/code> for\n<code>Draw&shy;Text<\/code>.\n<\/p>\n<p>\nBut what if you don&#8217;t control the call to\n<code>Ext&shy;Text&shy;Out<\/code> or <code>Draw&shy;Text<\/code>\nor whatever other function is being used to render the text.\nIf you don&#8217;t control the call, then you can&#8217;t pass along the\nmagic &#8220;Please render this string in an RTL context&#8221; flag.\n<\/p>\n<p>\nIf you&#8217;re lucky, the component that is doing the rendering has\nsome analogous flag that tells it to render in RTL context.\nIf the component is a control,\nthis flag may be implicit in the\n<code>WS_EX_RTL&shy;READING<\/code> extended style\non the control window itself.\nFor some components, the secret signal is\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms645505(v=vs.85).aspx\">\nthe presence of two RLM characters (U+200F) at the beginning of the string<\/a>.\n<\/p>\n<p>\nIf you&#8217;re not lucky, then the component that is doing the rendering\ngives you no way to convince or cajole it into rendering text\nin an RTL context.\nBut all hope is not lost:\nThe\n(<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/10\/26\/10362864.aspx\">possibly\nnon-intuitive<\/a>)\nUnicode Bidi algorithm comes to the rescue!\n<\/p>\n<p>\nWhat you can do is place the RLE control character (U+202B)\nat the start of the string.\nThe RIGHT-TO-LEFT EMBEDDING control character means\n&#8220;Treat the text that follows in an RTL context until further instructions.&#8221;\n(You cancel the effect of an RLE by a PDF (POP DIRECTIONAL\nFORMATTING, U+202C).)\n<\/p>\n<p>\nLet&#8217;s demonstrate in our\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a>.\n<\/p>\n<pre>\n#define THESTRING L\"\\x0639\\x0644\\x0649 \\x0633\\x0628\\x064a\\x0644 \" \\\n                  L\"\\x0627\\x0644\\x0645\\x062b\\x0627\\x0644: \" \\\n                  L\"Dear \\x0623\\x0634\\x0631\\x0641 \" \\\n                  L\"\\x0645\\x0627\\x0647\\x0631\"\n#define RLE L\"\\x202b\"\nvoid ShowString(HDC hdc, int y, PCWSTR psz, UINT format)\n{\n RECT rc = { 0, y, 500, y+100 };\n DrawTextW(hdc, psz, -1, &amp;rc, format);\n}\nvoid\nPaintContent(HWND hwnd, PAINTSTRUCT *pps)\n{\n ShowString(pps-&gt;hdc, 0, THESTRING, 0);\n ShowString(pps-&gt;hdc, 100, THESTRING, DT_RTLREADING);\n ShowString(pps-&gt;hdc, 200, RLE THESTRING, 0);\n}\n<\/pre>\n<p>\nThis sample program takes\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/05\/30\/10168423.aspx\">\na string in Arabic<\/a>\n(with a little bit of English thrown in just to make the difference\nmore noticeable)\nand renders it three ways:\n<\/p>\n<ul>\n<li>As an LTR string with no special formatting.\n<li>As an RTL string with no special formatting.\n<li>As an LTR string with an RTL context imposed via the RLE control character.\n<\/ul>\n<p>\nObserve that in the first case, the string treats the Arabic\nat the beginning and end of the string as Arabic text embedded\nin an English sentence,\nso it is formatted as\n<\/p>\n<blockquote CLASS=\"m\"><p>\n<span STYLE=\"border: black .75pt solid;padding-left: .2ex;padding-right: .2ex\">\n&#x639;&#x644;&#x649;\n&#x633;&#x628;&#x64a;&#x644;\n&#x627;&#x644;&#x645;&#x62b;&#x627;&#x644;<\/span>:\nDear\n<span STYLE=\"border: black .75pt solid;padding-left: .2ex;padding-right: .2ex\">\n&#x623;&#x634;&#x631;&#x641; &#x645;&#x627;&#x647;&#x631;<\/span>\n<\/p><\/blockquote>\n<p>\nIn the second case, the entire string is treated as an Arabic sentence\nwith an English word stuck inside it.\nTherefore, it comes out as<\/p>\n<blockquote CLASS=\"m\" STYLE=\"text-align: left\"><p>\n&#x639;&#x644;&#x649;\n&#x633;&#x628;&#x64a;&#x644;\n&#x627;&#x644;&#x645;&#x62b;&#x627;&#x644;:\n<span STYLE=\"border: black .75pt solid;padding-left: .2ex;padding-right: .2ex\">\nDear<\/span>\n&#x623;&#x634;&#x631;&#x641; &#x645;&#x627;&#x647;&#x631;\n<\/p><\/blockquote>\n<p>\nIn the third case, we force the string to be treated as an Arabic\nsentence by using the RLE control character.\nThe result matches the second string.\n<\/p>\n<p>\nNote that the formatting is still not ideal because the underlying\ncanvas is still LTR:\nThe text is left-justified instead of right-justified,\nand the caption buttons on the window\nwill still be drawn in the LTR position.\nBut it&#8217;s better than nothing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you have a program that is written in Arabic or Hebrew and you want to render some text. No problem. You just call Ext&shy;Text&shy;Out and pass the ETO_RTL&shy;READING flag to say, &#8220;Please render this string in an RTL context.&#8221; Many other text-rendering functions have a similar flag, such as DT_RTL&shy;READING for Draw&shy;Text. But what [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-2323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Suppose you have a program that is written in Arabic or Hebrew and you want to render some text. No problem. You just call Ext&shy;Text&shy;Out and pass the ETO_RTL&shy;READING flag to say, &#8220;Please render this string in an RTL context.&#8221; Many other text-rendering functions have a similar flag, such as DT_RTL&shy;READING for Draw&shy;Text. But what [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=2323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2323\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=2323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}