如何在Android中制作传统蒙文TextView [英] How to make a traditional Mongolian script TextView in Android

查看:21
本文介绍了如何在Android中制作传统蒙文TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 Android 应用制作垂直(从左到右换行)蒙古文脚本 TextView?

背景

Android 对世界上的许多语言都有相当好的支持,甚至包括阿拉伯语和希伯来语等 RTL 语言.但是,没有内置支持自上而下的语言,如 甚至没有一个好的答案.) 传统蒙古语的应用程序开发者有很多,但无论是出于商业原因还是其他原因,他们似乎都没有做出他们的代码开源.

由于这些困难,我想提出一系列 StackOverflow 问题,这些问题可以作为收集与传统蒙古语应用程序开发相关的一些更困难的编程问题的答案的中心位置.即使您不懂蒙古语,您也可以帮助查看代码、发表评论和问题、给出答案甚至对问题进行投票,我们将不胜感激.

蒙古文垂直TextView

蒙古文TextView需要具备以下要求:

  • 支持传统的蒙古字体
  • 从上到下垂直显示文本
  • 换行从左到右.
  • 在空格处出现换行符(与英文相同)

(TextView支持蒙古字体不是绝对的要求,TypeFace可以在后面设置,但是在使用很多TextView的时候会很方便.)

我的答案如下,但我欢迎其他解决此问题的方法.

本系列其他相关问题:

  • 以下解决方案的问题是镜像字体中未包含的任何字符(尤其是中文)都会向后显示.

    旧答案

    蒙古字体都是按照字形的方向与英文相同的方向制作的,即从左到右.这允许将蒙古语单词添加到英语、汉语或西里尔语文本中(唯一的问题是这些单词是放下"而不是站立",因为它们应该是).

    将 TextView 顺时针旋转 90 度将使其垂直,但换行方向错误(旋转后从右到左而不是从左到右).换行方向问题可以通过水平翻转或镜像TextView来解决,但是随后所有的字形都被镜像了.最后一个问题可以通过从垂直镜像字体开始来解决(可以通过使用像 获取 Unicode 蒙古语渲染引擎示例.

How do you make vertical (with left-to-right line wrapping) Mongolian script TextViews for Android apps?

Background

Android has fairly good support for many of the world's languages, even RTL languages like Arabic and Hebrew. However, there is no built in support for top-to-bottom languages like traditional Mongolian (which is still very much alive in Inner Mongolia and not to be confused with Cyrillic Mongolian). The following graphic shows the text direction with English added for clarity.

Since this functionality is not built into Android, it makes almost every single aspect of app development extremely difficult. There is also very, very little information available online. (One of the few related SO questions does not even have a good answer.) There are a number of app developers for traditional Mongolian, but whether it is for commercial reasons or otherwise, they do not seem to make their code open source.

Because of these difficulties I would like to make a series of StackOverflow questions that can serve as a central place to collect answers for some of the more difficult programming problems related to traditional Mongolian app development. Even if you are not literate in Mongolian, your help reviewing the code, making comments and questions, giving answers or even up-voting the question would be appreciated.

Mongolian Vertical TextView

A Mongolian TextView needs to have the following requirements:

  • Supports a traditional Mongolian font
  • Displays text vertically from top to bottom
  • Line wrapping goes from left to right.
  • Line breaks occur at a space (same as English)

(Having the TextView support the Mongolian font is not an absolute requirement since the TypeFace can be set later, but it is a convenience when many TextViews are being used.)

My answer is below but I welcome other ways of solving this problem.

Other related questions in this series:

iOS:

解决方案

Update

I ended up developing a vertical script MongolTextView from scratch. It is available as a part of mongol-library.

The problem with the solution below is that any characters not included in the mirrored font (Chinese notably) will appear backward.

Old answer

Mongolian fonts are all made with the the orientation of the glyphs in the same direction as English, that is, left to right. This allows Mongolian words to be added to English, Chinese, or Cyrillic text (the only problem being that the words are "laying down" rather than "standing up" as they should).

Rotating the TextView 90 degrees clockwise will make it vertical, but the line-wrap goes the wrong direction (right-to-left instead of left-to-right after the rotation). The line-wrap direction problem can be solved by horizontally flipping or mirroring the TextView, but then all of the glyphs are mirrored. This final problem can be solved by starting with a vertically mirrored font (something that is possible to make by editing an existing font with open source software like FontForge). The following graphic illustrates the process:

The rotating and flipping can be done by extending TextView and overriding the onDraw() and onMeasure() methods:

public class MongolTextView extends TextView {

    private TextPaint textPaint;

    // Constructors
    public MongolTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MongolTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MongolTextView(Context context) {
        super(context);
        init();
    }

    // This class requires the mirrored Mongolian font to be in the assets/fonts folder
    private void init() {

        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/MongolFontMirrored.ttf");
        setTypeface(tf);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // swap the height and width
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();

        canvas.save();

        // flip and rotate the canvas
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
        canvas.translate(0, getWidth());
        canvas.scale(1, -1);
        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

        getLayout().draw(canvas);

        canvas.restore();
    }
}

In your xml layout use the full name of your extended TextView:

<com.example.MongolTextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="@string/title_string" />

Known issues:

  • layout_margin and layout_gravity work ok if you keep the rotation in mind, but padding and gravity act strangely. So it seems to work best to use wrap_content and avoid using padding and gravity. The same effect can be achieved by putting the MongolTextView in a FrameLayout and using layout_margin and layout_gravity.
  • This solution does not deal with rendering the Unicode text. Either you need to use non-Unicode text (discouraged) or you need to include a rendering engine in your app. (Android does not support OpenType smartfont rendering at this time. Hopefully this will change in the future. iOS, by comparison does support complex text rendering fonts.) See this link for a Unicode Mongolian rendering engine example.

这篇关于如何在Android中制作传统蒙文TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆