您的位置:首页 > 移动开发 > Android开发

在Android2.3上完善阿拉伯语的显示以及添加多语言支持

2014-12-10 17:41 429 查看
做某个项目,周期超长,到现在还没有升级平台,还是Android2.3,我个人觉得挺崩溃的,连wifi miracast都要往上移植,我一直觉得不是正常的做项目的逻辑,但现实就是这样无奈。

进入正题,这个产品要卖给阿拉伯语国家,是整系统,不是应用。

因此需要在系统设置里支持切换到阿拉伯语,看了Android2.3,已经带了阿拉伯语字体,也能支持切换到阿拉伯语,但就是显示各种不正常,没有支持联系,从右到左排序也不对,特别是在edittext里。

怎么办?

网上有很多方法,针对应用,那是自己装载一个字体库,并且调用第三方的farsi类来转换文字显示,但我们是整系统,应该需要更好的方案,网上第三方的farsi类对阿拉伯文的转换支持存在一些缺陷。

Android4.3以上已经很完善的支持阿拉伯语的显示了,包括自动镜像。

我们能把JB,甚至KK上的东西完全移植过来么?看看framework的代码库,已经面目全非,显然直接换是不现实的,那么看看git log,一个一个patch的打呢?也不现实,从2010年到2013年期间,关于RTL,Arabic的patch,就有100多个,期间layout、textview这些基础类全都大变样了,怎么弄?

从目前来看,不可能把GB弄得能像JB支持阿拉伯文那么好了,只能一点一点来了。

先是解决bidi的问题,这个是关系到RTL(也就是从右到左排序)的算法。

在主线上找到这个patch:

commit 9f7a4442b89cc06cb8cae6992484e7ae795323ab

Author: Doug Felt <dougfelt@google.com>

Date: Mon Mar 1 12:45:56 2010 -0800

Enable nested bidi levels in a paragraph.

Changes the internal representation of direction information in the Directions object to be a visually-ordered list of start/length+direction pairs instead of a list of directionality inversion offsets.

Rewrite Layout.getOffsetToLeft/RightOf to use run information instead of width metrics.

Remove java Bidi, use native. Switch bidi tests to test native, expect levels instead of dirs.

Add test of directionality. Leave in switch to turn new code off and restore previous behavior for now.

Change-Id: Iea8bb46c678a18820e237c90f76007a084c83051

可以直接放进去,没有冲突。

接下来是连写显示的问题,找到了下面的patch:

commit e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7a

Author: Doug Felt <dougfelt@google.com>

Date: Mon Mar 29 14:58:40 2010 -0700

Refactor Styled utility functions into reusable objects.

This takes utility functions from Styled and a few other classes and

incorporates them into two new utility classes, TextLine and

MeasuredText. The main point of this is to support shaping by skia,

to experiment with how this will look, this also introduces

character-based Arabic shaping.

MeasuredText is used by code that determines line breaks by generating

and examining character widths in logical order. Factoring the code

in this way makes it usable by the ellipsize functions in TextUtils as

well as by StaticLayout. This class takes over the caching of widths

and chars arrays that was previously performed by StyledText. A small

number of MeasuredText objects are themselves cached by the class and

accesed using static obtain and recycle methods. Generally only these

few cached instances are ever created.

TextLine is used by code that draws or measures text on a line. This

unifies the line measuring and rendering code, and pushes assumptions

about how rtl text is treated closer to the points where skia code is

invoked. TextLine implements the functions that were previously

provided by Styled, working on member arrays rather than

explicitly-passed arguments. It implements the same kind of static

cache as MeasuredText.

TextLine and MeasureText simulate arabic glyph generation and shaping

by using ArabicShaping, ported with very minor changes from ICU4J's

ArabicShaping. This class generates shaped Arabic glyphs and Lam-Alef

ligatures using Unicode presentation forms. ArabicShaping is not

intended to be permanent, but to be replaced by real shaping from the

skia layer. It is introduced in order to emulate the behavior of real

shaping so that higher level code dealing with rendering shaped text

and cursor movement over ligatures can be developed and tested; it

also provides basic-level support for Arabic.

Since cursor movement depends on conjuncts whose formation is

font-dependent, cursor movement code that was formerly in Layout and

StaticLayout was moved into TextLine so that it can work on the shaped

text.

Other than these changes, the other major change is a rework of the

ellipsize utility functions to combine multiple branches into fewer

branches with additional state.

Updated copyright notices on new files.

Change-Id: I492cb58b51f5aaf6f14cb1419bdbed49eac5ba29

git am的时候,最后一个文件最后一行冲突,看了一下,实际那部分啥也没改,只是+-了一个空行,直接从patch里删掉那部分,am进去之后,连写是行了,可是有些文字后面出现大段的空白。

这里还有一个插曲,这个patch单独加进去编译会报错,需要在build/core/pathmap.xml加上icu4j,也就是这个commit:

commit 05d275837c5f16784a23c9ccc9095f14b9ad02fd

Author: Doug Felt <dougfelt@google.com>

Date: Mon Apr 5 14:00:48 2010 -0700

Add subdir under frameworks/base for code ported from icu4j.

Change-Id: I6c9fa6dac1f80bbaf8c80b9e0900f4e628ff596f

编译才会顺利通过。

那文字后面有一堆空白的问题怎么办?我记得我之前看到过一个commit有提到相关的问题的,一翻,就是:

Author: Doug Felt <dougfelt@google.com>

Date:   Tue May 25 11:51:40 2010 -0700    Fix alignment issues with RTL paragraphs.    Also remove unused debugging code that depends on junit.
Remove trailing whitespace in changed code.    Change-Id: Ie02d1b8220c599a672ee6e91af0fba634e0f620c


git am,有冲突,其中,touch.java,看了看,是因为Android2.3.7上单独merge了一个改动,在mainline上没有的,那只能单独手工处理这个文件,好在一共只有11行的改动。

StaticLayout.java,textutil.java等文件,先后合入下列commit:

commit 4e0c5e55e171532760d5f51e0165563827129d4e

Author: Doug Felt <dougfelt@google.com>

Date: Mon Mar 15 16:56:02 2010 -0700

Don't measure for a secondary caret unless we have one.

Also, clean up imports and trailing whitespace.

Change-Id: I5ebaaceb756600a1ca7370ab3ef078ce200545ef

commit 4c8ad6eb6241a0f689e49237ecadb65e8ffa4b6c

Author: Brian Muramatsu <btmura@google.com>

Date: Thu Jan 27 18:13:39 2011 -0800

Fix TextUtils#commaEllipsize

Bug 3400770

TextUtils#commaEllipsize creates a MeasuredText "mt" object with the

text to be ellipsized. It calls setPara which initializes mt's mPos

member to be 0. It then calls addStyleRun which moves mPos to the end

of the string. The loop back in commaEllipsize then calls mt addStyleRun

again and this causes IndexOutOfBoundsException, because the paint

object is trying to measure text past the text's length.

It seems this was a typo and that the tempMt variable should be used,

because the code is trying to measure the format string...not the

the string to be ellipsized. This makes the saner parts of CTS test

for this method pass now.

Change-Id: Ib6aa6e4bbd6afff4c95ad4c4d51a384cc1389875

commit 23241887515ed77687c23e29a4a3ffff671666bd

Author: Doug Felt <dougfelt@google.com>

Date: Wed Jun 2 14:41:06 2010 -0700

Fix IndexOutOfBoundsException when measuring text.

Some line wrapping conditions caused the same style run to be measured

twice, incorrectly advancing the position in the width buffer and

occasionally causing measurement to run out of room. The fix is to

not remeasure the same style run.

Change-Id: Iceb29729c1c27bb602df20fdf83af1da28c82e11

之后,am fix alignment...那个commit就能成功,再看看显示,那些不应该出现的空白消失了,现在阿拉伯文看上去顺眼多了,拿Android4.X的平板进行对比,基本上差不多。

对了,我还顺便从Android 代码库里下载来DroidNaskh-Regular.ttf,替换了2.3下的 DroidSansArabic.ttf。

后面还有自动镜像的支持,留待以后继续研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: