Never in my life thinking of leaning another language (human communication language). But while exploring an API in Android, leads me to learn something about the Arabic language.

What is canvas.drawTextRun(…) for?

It all begins trying to understand what is this function drawTextRun(...) provided by Android’s canvas. It look the same as drawText with the exception of having 2 more parameters of startContext and endContext index.

The description from the Google Document is not very helpful.

Draw a run of text, all in a single direction, with optional context for complex text shaping.

What is complex text shaping?

Answer in a Chinese Blog

Searching high and low, found nothing. Then I came across this Chinese blog

My Chinese reading skill is limited, but enough to get me through can capture the gist of it.

The Arabic letters

Apparently in Arabic, the letters would be written differently depends on the context around it (i.e. if there’s letter surrounding it)

Given the below 4 letters word (عربى) read from Right to Left (Arabic)

If written individually, it looks like this

Look at the shape. They are different from them written together.

Check out the Arabic Unicode, and notice they are written differently depending on the context.

The application of canvas.drawTextRun(…)

With this understanding, let’s explore a little on this drawTextRun function

private val TEXT = "عربى"



canvas.drawTextRun(TEXT,

1, TEXT.length - 1,

1, TEXT.length - 1,

x, y,

true, projectResources.paint)

In this case, we are showing the second and third letter of TEXT , and have the context matching the letter (e.g. assuming no surrounding letters). It is then displayed as below (complete letters by itself).

However if we to change the context as below.

private val TEXT = "عربى"



canvas.drawTextRun(TEXT,

1, TEXT.length - 1,

0, TEXT.length,

x, y,

true, projectResources.paint)

Make the context larger, indicating the shown second and third letters are actually having other letters surrounding them. Hence it will be displayed as below (like a hanging letters that is left unconnected)

Interesting indeed, same letters but different way of writing.