Tibetan Font Rendering on Android, iPad, and iPhone 4

June 14th, 2010 § 3

Thanks to Chris Fynn and the Dzongkha Development Commission, there is now a Tibetan font that can display correctly on Android, iPad, and iPhone 4, with some conversion and munging of the Unicode data.

In a previous post, I described how to render Tibetan on a rooted Android phone, through replacing the built-in fallback font with one (Jomolhari ) that supported the Chinese Tibetan Unicode standard GB/T20524-2006, which resides in the 0xF300 to 0xF8FF space.  This technique had a severe limitation, in that it wouldn’t work on an unrooted phone, and also removed support for Japanese, Chinese, and Korean (quite important languages in their own right).

I mentioned in that previous post that it would be possible to render Tibetan without having to root your Android phone, if you could use the technique described here, which embeds the fonts in the application and sets it on TextView objects. Unfortunately, it didn’t work with Jomolhari when I tried it, showing lots of beautiful boxes instead of Tibetan letters.  Evidently, some fonts just “don’t work” with this technique, and it was unclear why that happened; additionally this technique would have required a 2.3MB font to be contained in every Tibetan-supporting Android app.  It was large because this font was optimized for printing, so was specified in a lot more detail than cell phone screens needed.

A while back, Chris Fynn sent me his new font, “DDC Uchen,” which was designed to be used for web sites, and is only 860k, distributed under the Open Font License.   This font and Jomolhari are the only two fonts I’m aware of that have support for the Precomposed Tibetan standard.  I’ve placed it for download here.  (The DDC stands for Dzongkha Development Commission, which paid for Chris Fynn to develop the font, and is making it freely available to everyone. Dzongkha is the language spoken in the country of Bhutan.  It is very closely related to Tibetan and uses an identical alphabet.)  Just for grins, I thought I’d try it out by placing the font in my Android project under assets/fonts/DDC_Uchen.ttf, with this fairly simple code:

        TextView tv=(TextView)findViewById(R.id.output);
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/DDC_Uchen.ttf");
        // Precomposed Tibetan version of a supplication to Tara
        String tibetanText = "\u0F04\u0F05\u0F0D \u0F0D\u0F60\u0F55\u0F42\u0F66\u0F0B\u0F58\u0F0B" +
        "\uF397\u0F63\u0F0B\u0F58\u0F0B\u0F63\u0F0B\u0F56\uF43A\u0F51\u0F0B\u0F54\u0F0B\n" +
        "\uF4BA\u0F42\u0F0B\u0F60\u0F5A\u0F63\u0F0B\u0F56\u0F0B" +
        "\uF3ED\u0F0B\uF5BE\u0F0B\uF53F\u0F0B\u0F42\uF3B7\u0F42\u0F0B\u0F54" +
        "\uF591\u0F0B\uF5AA\u0F58\u0F0B\u0F54\u0F0B\uF473\u0F0D\u0F0D";
        tv.setText(tibetanText);
        tv.setTypeface(face);

It rendered perfectly on my Android phone:

android_tibetan_cropped

To convert text from Tibetan Unicode, you either have to use the libraries I’ve uploaded at the GyutrulDrawa project, hosted on Google Code, or use Andrew West’s BabelPad (Windows only).  To convert it using BabelPad, use the menu option Convert -> Tibetan -> Unicode to Precomposed Tibetan (Set A).  For these program examples, to paste it into Java code, I then used “Convert to Universal Character Name”, which generated Hex codes in \u format, and pasted it into the Android Java code.

This technique will only work with widgets which are derived from TextView, which means that this can’t be used to easily create a Tibetan Web browser.  However, other widgets such as EditText, Button, CheckedTextView, CheckBox, CompoundButton, RadioButton, and ToggleButton, as well as Chronometer, DigitalClock, AutoCompleteTextView, ExtractEditText, and MultiAutoCompleteTextView, all inherit from TextView, and so should work with this technique (though I haven’t tried it out with them).

Getting it to work on iPad 3.2 and iPhone 4

Now that we have working Tibetan font support on Android, what about the Other Phone, the iPhone?  Well, it turns out that 3G and earlier iPhones don’t support setting embedded fonts easily, but 3.2 for the iPad supports it, as described in this post.  As on Android, the iPhone doesn’t have the complex script support necessary to render Tibetan properly (in fact, the default rendering mode looks worse than on Android), but the exact same technique we used on Android works on iOS as well.

I’d worked through some basic tutorials on iPhone development, but this was my first excursion into writing my own program in Objective C, so it took me a while to get everything set right.  There are a few steps to this:

  1. Put the DDC_Uchen.ttf font in the Resources directory of your Xcode project.
  2. In your projects plist file, you have to add a Key named “UIAppFonts”, change the Value Type to Array, and then set the first (only) value to “DDC_Uchen.ttf”.
  3. Make sure you refer to the font by its actual name, not the file name, in this case “DDC Uchen,” when you use it.

Here’s some code to get it to work:

	textView = [ [ UITextView alloc] initWithFrame: rect ];
	UIFont* tibFont = [UIFont fontWithName: @"DDC Uchen" size: 22];

	if (tibFont != nil) {

		[ textView setFont:tibFont];

		textView.text = @"\u0F04\u0F05\u0F0D \u0F0D\u0F60\u0F55\u0F42\u0F66\u0F0B\u0F58\u0F0B\uF397\u0F63\u0F0B\u0F58\u0F0B\u0F63\u0F0B\u0F56\uF43A\u0F51\u0F0B\u0F54\u0F0B\n\uF4BA\u0F42\u0F0B\u0F60\u0F5A\u0F63\u0F0B\u0F56\u0F0B\uF3ED\u0F0B\uF5BE\u0F0B\uF53F\u0F0B\u0F42\uF3B7\u0F42\u0F0B\u0F54\uF591\u0F0B\uF5AA\u0F58\u0F0B\u0F54\u0F0B\uF473\u0F0D\u0F0D";

	} else {

		textView.text = @"Failed.";

	}

This results in the following screen shot, which I took from the 3.2 simulator:

screen-shot-2010-06-13-at-43756-pm

This will definitely not work on any iPhone 3GS or earlier, but I presume that it will work with the iPhone 4 (though I haven’t tested it).

The conversion routines which Andrew West offered to this project are written in C++ (here and here).  I haven’t yet ported them to Objective C, so if anyone is inspired to do this, it should allow for dynamic display of any Unicode Tibetan text by converting it into Precomposed Tibetan.  Otherwise, you’ll have to use the Windows-based BabelPad to convert each of your strings into Precomposed Tibetan, or use the C++ code and convert the strings between CString and NSString.

The next steps will be to firm this code up.  There are a few Tibetan projects (TBRC, the Trace Foundation, and Sonam Rinchen‘s iPhone apps) that are looking to use this code, so it would be best if everyone involved can keep this in the Open Source spirit by building on this bare foundation to create a common display, text entry, and reading environment for Mobile Tibetan.  Enjoy.  Gelek Pel and  Sarva Mangalam!

Tagged: , , , ,

§ 3 Responses to “Tibetan Font Rendering on Android, iPad, and iPhone 4”

What's this?

You are currently reading Tibetan Font Rendering on Android, iPad, and iPhone 4 at tom at tom dot to.

meta