4

I have two font ttf files that must be applied on a TextView based on languages inside String. So e.g. consider this sample text:

hey what's up ضعيف

I can just apply a typeface span based on language but it requires custom markup in every string that is fetched from our server e.g.

 <ttf1>hey what's up <ttf1><ttf2>ضعيف</ttf2>

And parsing every String at run time will give a performance hit. Is there any other approach to achieve this?

For start lets say I need to do this just for direction of text i.e. RTL and LTR so in above example English is LTR and Arabic is RTL. Will this be any different?

I have tried merging those two font files but there are line height issues and if I fix it for one font file it gets broken for other file.

Community
  • 1
  • 1
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • How do you determine which part of the (raw) string is LTR and which part is RTL? – ozbek Nov 13 '14 at 09:55
  • @shoerat There are ways, e.g. using this method in a loop `Character.getDirectionality()` over `String`. – M-Wajeeh Nov 13 '14 at 12:00
  • Alright. If you can determine and split the string into LTR/RTL parts, then you could 1) stack separate `TextView`s side by side; 2) set text, custom font and direction individually. Would that work? – ozbek Nov 13 '14 at 12:06
  • Yes that would work but creating more than one `TextView` and aligning them vs creating two `TypefaceSpan` and applying on one `TextView` as I mentioned above, which one you think is better? – M-Wajeeh Nov 13 '14 at 12:17
  • Yeah, sorry. I can't tell how much would be the overall difference, but won't make any difference regarding _parsing every String at run time will give a performance hit._ So, probably not a better option. – ozbek Nov 13 '14 at 12:24

1 Answers1

3

I found a more elegant solution than manual markup with help of someone:

String paragraph = "hey what's up ضعيف";
int NO_FLAG = 0;
Bidi bidi = new Bidi(paragraph, NO_FLAG);
int runCount = bidi.getRunCount();
for (int i = 0; i < runCount; i++) {
    String ltrtl = bidi.getRunLevel(i) % 2 == 0 ? "ltr" : "rtl";
    String subString = paragraph.substring(bidi.getRunStart(i), bidi.getRunLimit(i));
    Log.d(">>bidi:" + i,  subString+" is "+ltrtl);
}

prints:

hey what's up is ltr

ضعيف is rtl

So now one can easily build TypefaceSpan or MetricAffectingSpan based on language direction like this:

SpannableString spanString = new SpannableString(paragraph);
for (int i = 0; i < runCount; i++) {
    Object span = bidi.getRunLevel(i) % 2 == 0 ? ltrFontSpan : rtlFontSpan;
    spanString.setSpan(span, bidi.getRunStart(i), bidi.getRunLimit(i), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
}
textView.setText(spanString);
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103