Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Arabic Controls in Window phone 7

0.00/5 (No votes)
12 May 2012 1  
Make your Application windows phone 7 support for Bi-Directional right to left

Download Source code

Introduction 

When Microsoft Lunched Windows CE first came befroe 10 years ago, the software market was opened widely for a new type of applications and we all wanted to write new applications or to port our application already written for Win32 to Windows CE. Unfortunately, for the Arabic market, there is no Arabic language support in WinCE 2.11 and WinCE 3.0. also in windows pohone 7. 

and  As i know Microsoft will saying about new Device with Arabic Support ,but i dont think so what we can do about that.

we can claim about him or we can do work-round to ignore this case . 

personaly i preferred approach number 2 (do somthing or do work around). but sill we got problem in windows phone 7 :The main problem the windows phone 7 do not have support for BiDi (Bi-Directional)  and RTL (Right To Left) window controls. I don’t know why. and we can't Inject our code inside system Like messiging or Inbox or Word ...etc 

Background

Windows Mobile 7 development is done using the .NET framework. The .NET framework is a software framework created by Microsoft for use in creating Windows applications. Programmers write applications using one of the several languages supported by the .NET framework, like C#, and the applications then execute inside of a runtime environment called the Common Language Runtime. For Windows Phone 7, there are two distinct development approaches you can take when creating your application.

The first approach is to use Silverlight for Windows Phone. Silverlight was originally envisioned as a way for developers to create rich internet applications. It has seen a sharp increase in market adoption in recent years, driven mostly by the fact that Netflix uses Silverlight to stream videos and NBC used Silverlight for its online broadcast of the Olympic games. A Silverlight application combines declarative markup (called XAML) to construct the user interface and code written in a .NET framework language to control an application’s behavior. If you’re developing a data driven application for Windows Phone 7, you should probably use Silverlight.

 Silver Light 

a Silverlight is platfrom to build rich control and buissnes application for Mobile and Web And Desktop is generally a mix of code and XAML. Most often, you’ll use XAML for defining the layout of the visuals of your application, and you’ll use code for event handling, including all user-input events and all events generated by controls as a result of processing user-input events.

Much of the object creation and initialization performed in XAML would traditionally be done in the constructor of a page or window class. This might make XAML seem just a tiny part of the application, but it turns out to be much more than that. As the name suggests, XAML is totally compliant XML, so it’s instantly toolable—machine writable and machine readable as well as human writable and human readable.

Although XAML is usually concerned with object creation and initialization, certain features of Silverlight provide much more than object initialization would seem to imply. One of these features is data binding, which involves connections between controls, or between controls and underlying data, so that properties are automatically updated without the need for explicit event handlers. Entire animations can also be defined in XAML.

Although XAML is sometimes referred to as a “declarative language,” it is certainly not a complete programming language. You can’t perform arithmetic in any generalized manner in XAML, and you can’t dynamically create objects in XAML.

Experienced programmers encountering XAML for the first time are sometimes resistant to it. I know I was. Everything that we value in a programming language such as C#—required declarations, strong typing, array-bounds checking, tracing abilities for debugging—largely goes away when everything is reduced to XML text strings. Over the years, however, I’ve gotten very comfortable with XAML, and I find it very liberating in using XAML for the visuals of the application. In particular I like how the parent-child relationship of controls on the surface of a window is mimicked by the parent-child structure inherent in XML. I also like the ability to experiment with XAML—even just in the Visual Studio designer.

Everything you need to do in Silverlight can be allocated among these three categories: 

•Stuff you can do in either code or XAML

•Stuff you can do only in code (e.g., event handling and methods)

•Stuff you can do only in XAML (e.g., templates) . 

You Will Benefit from this Article: 

1-You’re interested in Windows Phone 7 (WP7) development

2-You would like to see how to use existing Silverlight knowledge and tools to create and deploy a WP7 app.

3-Make your Application support for Arabic.

The Solution

First of all you don't worry about Arabic font in windows 7 ,it'a already coming with Arabic font not like Windows mobile and pocket PC. you have to copy 2 dll into your project and enjoy with Arabic Support Right to left and Arabic keyboard

Arabization involves reversing the string, considering that numbers shouldn’t be reversed and perform glyph substitution manually.

Using the code

Now Let us discuss some of part in our code i wanna share some of code about Right to Left so The second step is a bit more tricky so be ready. We know that the Arabic string in the memory contains the Unicode characters that take the same form of the isolated glyphs of the letters. We have to make mapping from the character to the right glyphs to be displayed, considering all states of the letter. 

I some how could construct an array of all Arabic letters' state. It is not sorted alphabetically. The first row in the table is the first piece of data I could collect about a letter and it was the letter ‘thal’. 

As far as I know .. the rules of glyph substitution are somewhere in the TTF file but I found it very complex. To help constructing the rules for glyph substitution, I made two sets of characters according to whether it may need a link to the previous or the next character. 

Character Map Table 

 

//
public ArabicShape()
        {
            //
            // TODO: Add constructor logic here

            theSet1 = new char[22]
                {
                (char)0x62c, (char)0x62d, (char)0x62e, (char)0x647, (char)0x639, (char)0x63a, (char)0x641, (char)0x642,
                (char)0x62b, (char)0x635, (char)0x636, (char)0x637, (char)0x643, (char)0x645, (char)0x646, (char)0x62a,
                (char)0x644, (char)0x628, (char)0x64a, (char)0x633, (char)0x634, (char)0x638
                };
            theSet2 = new char[13]
                {
                (char)0x627, (char)0x623, (char)0x625, (char)0x622, (char)0x62f, (char)0x630, (char)0x631, (char)0x632,
                (char)0x648, (char)0x624, (char)0x629, (char)0x649, 
                (char)0x698, 
                };
            a = new char[N_DISTINCT_CHARACTERS, 5]
                {
                {(char)0x630, (char)0xfeac, (char)0xfeab, (char)0xfeac, (char)0xfeab},
                {(char)0x62f, (char)0xfeaa, (char)0xfea9, (char)0xfeaa, (char)0xfea9},
                {(char)0x62c, (char)0xfe9e, (char)0xfe9f, (char)0xfea0, (char)0xfe9d},
                {(char)0x62d, (char)0xfea2, (char)0xfea3, (char)0xfea4, (char)0xfea1},
                {(char)0x62e, (char)0xfea6, (char)0xfea7, (char)0xfea8, (char)0xfea5},
                {(char)0x647, (char)0xfeea, (char)0xfeeb, (char)0xfeec, (char)0xfee9},
                {(char)0x639, (char)0xfeca, (char)0xfecb, (char)0xfecc, (char)0xfec9},
                {(char)0x63a, (char)0xfece, (char)0xfecf, (char)0xfed0, (char)0xfecd},
                {(char)0x641, (char)0xfed2, (char)0xfed3, (char)0xfed4, (char)0xfed1},
                {(char)0x642, (char)0xfed6, (char)0xfed7, (char)0xfed8, (char)0xfed5},
                {(char)0x62b, (char)0xfe9a, (char)0xfe9b, (char)0xfe9c, (char)0xfe99},
                {(char)0x635, (char)0xfeba, (char)0xfebb, (char)0xfebc, (char)0xfeb9},
                {(char)0x636, (char)0xfebe, (char)0xfebf, (char)0xfec0, (char)0xfebd},
                {(char)0x637, (char)0xfec2, (char)0xfec3, (char)0xfec4, (char)0xfec1},
                {(char)0x643, (char)0xfeda, (char)0xfedb, (char)0xfedc, (char)0xfed9},
                {(char)0x645, (char)0xfee2, (char)0xfee3, (char)0xfee4, (char)0xfee1},
                {(char)0x646, (char)0xfee6, (char)0xfee7, (char)0xfee8, (char)0xfee5},
                {(char)0x62a, (char)0xfe96, (char)0xfe97, (char)0xfe98, (char)0xfe95},
                {(char)0x627, (char)0xfe8e, (char)0xfe8d, (char)0xfe8e, (char)0xfe8d},
                {(char)0x644, (char)0xfede, (char)0xfedf, (char)0xfee0, (char)0xfedd},
                {(char)0x628, (char)0xfe90, (char)0xfe91, (char)0xfe92, (char)0xfe8f},
                {(char)0x64a, (char)0xfef2, (char)0xfef3, (char)0xfef4, (char)0xfef1},
                {(char)0x633, (char)0xfeb2, (char)0xfeb3, (char)0xfeb4, (char)0xfeb1},
                {(char)0x634, (char)0xfeb6, (char)0xfeb7, (char)0xfeb8, (char)0xfeb5},
                {(char)0x638, (char)0xfec6, (char)0xfec7, (char)0xfec8, (char)0xfec5},
                {(char)0x632, (char)0xfeb0, (char)0xfeaf, (char)0xfeb0, (char)0xfeaf},
                {(char)0x648, (char)0xfeee, (char)0xfeed, (char)0xfeee, (char)0xfeed},
                {(char)0x629, (char)0xfe94, (char)0xfe93, (char)0xfe93, (char)0xfe93},
                {(char)0x649, (char)0xfef0, (char)0xfeef, (char)0xfef0, (char)0xfeef},
                {(char)0x631, (char)0xfeae, (char)0xfead, (char)0xfeae, (char)0xfead},
                {(char)0x624, (char)0xfe86, (char)0xfe85, (char)0xfe86, (char)0xfe85},
                {(char)0x621, (char)0xfe80, (char)0xfe80, (char)0xfe80, (char)0xfe7f},
                {(char)0x626, (char)0xfe8a, (char)0xfe8b, (char)0xfe8c, (char)0xfe89},
                {(char)0x623, (char)0xfe84, (char)0xfe83, (char)0xfe84, (char)0xfe83},
                {(char)0x622, (char)0xfe82, (char)0xfe81, (char)0xfe82, (char)0xfe81},
                {(char)0x625, (char)0xfe88, (char)0xfe87, (char)0xfe88, (char)0xfe87},
                
               
                };
            //
        }
        private void ArabiChar(ref string s)
        {
            string out1, rev;
            out1 = "";
            char[] dest = s.ToCharArray();
            System.Array.Reverse(dest, 0, dest.Length);
            s = new string(dest);
            int i = 0;
            while (i < s.Length)
            {
                if ((s[i] >= '0' && s[i] <= '9')) // isDigit(s[i]) ?
                {
                    rev = "";
                    while ((s[i] >= '0' && s[i] <= '9')) // isDigit(s[i]) ?
                    {
                        rev = rev + s[i];
                        ++i;
                    }
                    dest = rev.ToCharArray();
                    System.Array.Reverse(dest, 0, dest.Length);
                    rev = new string(dest);
                    //rev.MakeReverse();
                    out1 = out1 + rev;
                }
                else
                {
                    out1 = out1 + s[i];
                    ++i;
                }
            }
            s = out1;
        }
        public string DisplayArabic(string s)
        {
            str = s;
            bool linkBefore, linkAfter;
            Pstr = str;
            for (int i = 0; i < str.Length; i++)
            {
                char ch = str.ToCharArray()[i];
                if ((ch >= 0x0621 && ch <= 0x064a) || (ch >= 0x067e && ch <= 0x06d5)) // is an Arabic character?
                {
                    int idx = 0;
                    while (idx < N_DISTINCT_CHARACTERS)
                    {
                        if (a[idx, 0] == str.ToCharArray()[i])
                            break;
                        ++idx;
                    }

                    if (i == str.Length - 1)
                        linkAfter = false;
                    else
                        linkAfter = (isFromTheSet1(str.ToCharArray()[i + 1]) ||
                        isFromTheSet2(str.ToCharArray()[i + 1]));
                    if (i == 0)
                        linkBefore = false;
                    else
                        linkBefore = isFromTheSet1(str.ToCharArray()[i - 1]);

                    if (linkBefore && linkAfter)
                    {
                        Pstr = Pstr.Remove(i, 1);
                        Pstr = Pstr.Insert(i, new string(a[idx, 3], 1));//mid
                    }
                    if (linkBefore && !linkAfter)
                    {
                        Pstr = Pstr.Remove(i, 1);
                        Pstr = Pstr.Insert(i, new string(a[idx, 1], 1));//end
                    }
                    if (!linkBefore && linkAfter)
                    {
                        Pstr = Pstr.Remove(i, 1);
                        Pstr = Pstr.Insert(i, new string(a[idx, 2], 1));//init
                    }
                    if (!linkBefore && !linkAfter)
                    {

                        Pstr = Pstr.Remove(i, 1);
                        Pstr = Pstr.Insert(i, new string(a[idx, 4], 1));//iso
                    }
                }
            }
            ArabiChar(ref Pstr);
            return Pstr;
        }
//

In This Part of code i check if the Char can connect from start or can connect from End or if the char  can never connect you can check Arabic Unicode from  Character map in windows system Tools 

private bool isFromTheSet1(char ch)
        {
            int i = 0;
            while (i < 22)
            {
                if (ch == theSet1[i])
                    return true;
                ++i;
            }
            return false;
        }
private bool isFromTheSet2(char ch)
        {
            int i = 0;
            while (i < 13)
            {
                if (ch == theSet2[i])
                    return true;
                ++i;
            }
            return false;
        }  

 

Here our code we put it inside private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 

 /*
             * do some extra initialize like keybd Coordinate and button size
             * and some brushes info
             */
            this.keyboard.InitializeKeyboard();
            /*
             * defined handle for key bosrd ,it's fired when keybd show or hide
             */
            this.keyboard.KeyboardHide += new EventHandler(keyboard_KeyboardHide);
            this.keyboard.KeyboardShow += new EventHandler(keyboard_KeyboardShow);

            /*
             * defined handler for edit control ,it's fired when we
             * put the fniger inside edit (WM_SETFOCUS)
             */ 
            this.textBlock.GotFocus += new RoutedEventHandler(textBlock1);
            this.rTLTextBox1.GotFocus += new RoutedEventHandler(textBlock2); 

 Here peace of code to describe how to initialization ArabicShape Class and we can see how add the Arabic string its too easy  

//Intilize  Arabic Shape Class
            crAr = new Arabic.Controls.ArabicShape();

            listBox1.Items.Add("إختبار عربي");
            /*Here to make Control Display Arabic items corectly*/
            listBox1.Items.Add(crAr.DisplayArabic("إختبار عربي")); //look to Display Arabic Function
            btnClose.Content = crAr.DisplayArabic("دخول");  

 We override BackKey Function to handle some of issue related to sip you know this keybd devloped by us so may be when click back to exit from application may be still appear ,so sure will be worry bout that i suggest when user clik to back button i want to Hide Sip and if SIP already show i wanna call orginal procedure 

if (SIP.Instance.Visible)
            {
                e.Cancel = true;
                this.keyboard.Dismiss();
            }
            else
            {
                NavigationService.GoBack(); }   

The History  : 

I will keep update on This Article , so i will make some change for windows phone 7.1 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here