Friday, March 6, 2015

Adding new font (TTF) in Android

Following are the steps that we have to follow in order to integrate and use a new language into android.
      1.      copy the font file (true type) into the below mentioned path
/android_src/mydroid/frameworks/base/data/fonts/DroidSansArabic.ttf.
2.      Edit the xml file under the following path :
/android_src/mydroid/frameworks/base/data/fonts/fonts.xml.
add the following entry towards the end of the file
<font ttf="DroidSansFallback" /><font ttf="DroidSansArabic" /></fonts>
3.      Edit the Andriod.mk file in the following path so that the new font file is copied into the resource folder during build.
/android_src/mydroid/frameworks/base/data/fonts/Android.mk
DroidSerif-BoldItalic.ttf \
DroidSansMono.ttf \
DroidSansArabic.ttf

4.      Edit the following code file to add our font-file
/android_src/mydroid/external/skia/src/ports/SkFontHost_android.cpp
static const FontInitRec gSystemFonts[] =
{
{ "DroidSans.ttf", gSansNames },
{ "DroidSans-Bold.ttf", NULL },
{ "DroidSerif-Regular.ttf", gSerifNames },
{ "DroidSerif-Bold.ttf", NULL },
{ "DroidSerif-Italic.ttf", NULL },
{ "DroidSerif-BoldItalic.ttf", NULL },
{ "DroidSansMono.ttf", gMonoNames },
//this will cause Arabic font file to be added to final image.
{ "DroidSansArabic.ttf", NULL },

5.      Modify the following files to enable the android framework to choose appropriate resource folder in each application when user selects some Locale.

a.       /android_src/mydroid/external/icu4c/common/unicode/locid.h
/** Useful constant for this country/region. @stable ICU 2.0 */
static const Locale &U_EXPORT2 getCanadaFrench(void);
// Adding new font for arabic
/** Useful constant for this country/region. @stable ICU 2.0 */
static const Locale &U_EXPORT2 getEgyptArabic(void);

b. /android_src/mydroid/external/icu4c/common/locid.cpp (Two changes needed in this file)
const Locale & U_EXPORT2
Locale::getCanadaFrench(void)
{
return getLocale(eCANADA_FRENCH);
}
// Adding new font for arabic
const Locale & U_EXPORT2
Locale::getEgyptArabic(void)
{
return getLocale(eEGYPT_ARABIC);
}
and
in the Locale * Locale::getLocaleCache(void) method, add a new entry as shown below :
tLocaleCache[eCANADA_FRENCH] = Locale("fr", "CA");
tLocaleCache[eEGYPT_ARABIC] = Locale("ar", "EG");


  1. /android_src/mydroid/external/icu4c/common/locid.cpp
struct FamilyRec {
    FamilyRec*  fNext;
    SkTypeface* fFaces[6];   <=== increment whenever we are adding a new font file.
  1. In this locid.cpp file, we need to increment the for loop count also accordingly. Otherwise it will not read the newly added font file
 for (int i = 0; i < 6; i++) {  <== increment

  1. In the locid.cpp file, Add the enum value for each language:
      enum ELocalePos {
…..
….
                   eHINDI, 
                   eARABIC, 

These are all for adding a new language. If we need to add a new country, we need to do similar things for Country Enums, Get methods etc.,

9.      uild the entire source code
a.       Make
b.      Make sdk

0 comments:

Post a Comment