如何检测的语言字体由设备支持? [英] how to detect which language fonts are supported by device?

查看:152
本文介绍了如何检测的语言字体由设备支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我想显示10种语言的本地脚本列表,如果手机不支持的字体将回落到英语脚本。有哪些字体,通过特定的设备支持的任何方式,我们可以检测到? 换句话说,我只是想检查是否,比方说,旁遮普语可以显示为ਪੰਜਾਬੀ

say, I want to display a list of 10 languages in native scripts, and if the phone doesn't support the font it will fall back to english script. Is there any way we can detect which fonts are supported by particular device? In other words, I just want to check whether, say, "punjabi" can be displayed as "ਪੰਜਾਬੀ"

推荐答案

为了安全起见,我会用英文和本地名称(这是我实际做)。请怜惜这个可怜的家伙看到所有这些فارسی赣语ગુજરાતીՀայերենहिन्दीবিষ্ণুপ্রিয়ামণিপুরীעבריתಕನ್ನಡქართულიລາວمصرىनेपालभाषाОлыкмарийភាសាខ្មែរСрпскиதமிழ்文言ייִדיש中文的原生脚本而已!

To be on the safe side, I would use both the English and the native name (and this is what I actually did). Please take pity on the poor guy seeing all these فارسی 贛語 ગુજરાતી Հայերեն हिन्दी বিষ্ণুপ্রিয়া মণিপুরী עברית ಕನ್ನಡ ქართული ລາວ مصرى नेपाल भाषा Олык марий ភាសាខ្មែរ Српски தமிழ் 文言 ייִדיש 中文 in native scripts only!

您也可以尝试绘制文本转换成位图并检查所生成位图是否是空白。 例如。 http://www.skoumal.net/en/android-how-draw -text位图 和 <一href="http://stackoverflow.com/questions/28767466/how-to-check-if-a-bitmap-is-empty-blank-on-android">How检查一个位图是在Android 空(空白) (当然,假设不支持的字母不显示为黑色钻石或类似的东西)。

You also can try to draw your text into a bitmap and check whether or not the resulting bitmap is blank. E.g. http://www.skoumal.net/en/android-how-draw-text-bitmap and How to check if a Bitmap is empty (blank) on Android (of course, assuming that unsupported letters are not shown as black diamonds or something like that).

class Util {
    private static final int WIDTH_PX = 200;
    private static final int HEIGHT_PX = 80;

    public static boolean isSupported(Context context, String text) {
        int w = WIDTH_PX, h = HEIGHT_PX;
        Resources resources = context.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
        Bitmap orig = bitmap.copy(conf, false);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.rgb(0, 0, 0));
        paint.setTextSize((int) (14 * scale));

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int x = (bitmap.getWidth() - bounds.width()) / 2;
        int y = (bitmap.getHeight() + bounds.height()) / 2;

        canvas.drawText(text, x, y, paint);
        boolean res = !orig.sameAs(bitmap);
        orig.recycle();
        bitmap.recycle();
        return res;
    }
}

测试code:

Testing code:

    String s;
    s="";Log.d("~~~","=="+s+"=="+Util.isSupported(this, s));
    s="中文";Log.d("~~~","=="+s+"=="+Util.isSupported(this, s));
    s="ਪੰਜਾਬੀ";Log.d("~~~","=="+s+"=="+Util.isSupported(this, s));
    s="Հայերեն";Log.d("~~~","=="+s+"=="+Util.isSupported(this, s));

输出:

 ====false
 ==中文==true
 ==ਪੰਜਾਬੀ==false
 ==Հայերեն==true

PS。如果您想在相同的字符串显示英文和母语的名字,你可以检查本机的名称可能会显示组成一个字符串,这两个名字之前,或删除拉丁字符:

PS If you want to show both English and native name in the same string, you can either check that the native name may be displayed before composing a string with both names, or remove the Latin characters:

public static String stripLatin(String s) {
    String res = "";
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) > 127) {
            res += s.charAt(i);
        }
    }
    return res;
}

测试code:

Testing code:

s="Punjabi(ਪੰਜਾਬੀ)";Log.d("~~~","=="+s+"=="+Util.isSupported(this, Util.stripLatin(s)));

这篇关于如何检测的语言字体由设备支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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