检查自定义字体可以显示的字符 [英] Check if custom font can display character

查看:143
本文介绍了检查自定义字体可以显示的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的字体是显示框字符。我使用的是不支持的所有语言显然字体。我要检查,如果字符串我将显示可以通过我的自定义的字体显示。如果不能那么我想使用标准的Andr​​oid字体(我知道可以显示的字符)。我无法找到一个方法来检查,如果我的字样,虽然可以显示特定的字符串。我相信,我已经看到了周围的方法做这个的地方。任何人都知道?

I have a custom font which is displaying the box character. The font I am using does not support all languages apparently. I want to check if the String I am about to display can be displayed by my custom font. If it cannot then I want to use the standard Android font (which I know can display the characters). I can't find a method to check if my Typeface can display a particular String though. I am sure I have seen a method around that does this somewhere. Anyone know?

推荐答案

。但是,如果你有过要检查什么字符串/字符一定的控制,它实际上是可以用一个稍微更有创意的方式做到这一点。

As you mentioned in your own answer there is no built in methods available for checking this. However if you have some control over what string/characters you want to check, it is actually possible to do this with a slightly more creative approach.

您可以借鉴,你知道丢失在您要检查,然后绘制你想知道,如果它存在一个字符,最后进行比较,看看他们看起来是一样的字体字符。

You could draw a character that you know are missing in the font you want to check and then draw a character that you want to know if it exists, and then finally compare them and see if they look the same.

下面是一个code示例,说明我是如何实现这个检查:

Here is a code sample that illustrates how I implemented this check:

public Bitmap drawBitmap(String text){
    Bitmap b = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(b);
    c.drawText(text, 0, BITMAP_HEIGHT / 2, paint);
    return b;
}

public byte[] getPixels(Bitmap b) {
    ByteBuffer buffer = ByteBuffer.allocate(b.getByteCount());
    b.copyPixelsToBuffer(buffer);
    return buffer.array();
}
public boolean isCharacterMissingInFont(String ch) {
    String missingCharacter = "\u0978"; // reserved code point in the devanagari block (should not exist).
    byte[] b1 = getPixels(drawBitmap(ch));
    byte[] b2 = getPixels(drawBitmap(missingCharacter));
    return Arrays.equals(b1, b2);
}

有要记住这种方法的一些重要的限制:

There are some important limitations to keep in mind with this method:

  • 的字符,你检查(参数 isCharacterMissing )必须是无空格(可以使用的 Character.isWhitespace())。在某些字体缺字呈现为空白,因此,如果你比较一下存在,它会错误地报告为缺少的字符在这样的字体的空白字符。
  • missingCharacter 成员必须是已知的字体丢失进行检查字符。在code点 U + 0978 我现在用的就是在梵文统一code座,因此当前应在所有的Uni code兼容字体缺少的中间保留code点,但在未来,当新的字符添加到统一$ C C本code $点可能会被分配到一个真正的性格。因此,这种做法并不面向未来的,但如果你提供的字体自己,你可以确保无论何时更改应用程序的字体您使用的是缺少的字符。
  • 由于此检查是通过绘制位图,并比较它们是不是很有效率,因此应该只用来检查几个字符,而不是所有字符串在应用程序中完成的。
  • The character you check (argument to isCharacterMissing) must be non whitespace (can be detected by using Character.isWhitespace()). In some fonts missing characters are rendered as whitespace, so if you would compare a whitespace character that exists it would incorrectly be reported as a missing character in fonts like these.
  • The missingCharacter member must be a character that is known to be missing in the font to be checked. The code point U+0978 that I am using is a reserved code point in the middle of the Devanagari Unicode block so it should currently be missing in all Unicode compatible fonts, however in the future when new characters are added to Unicode this code point might be assigned to a real character. So this approach is not future proof, but if you supply the font yourself you can make sure you are using a missing character whenever you change the font of your application.
  • Since this check is done by drawing bitmaps and comparing them it's not very efficient so it should only be used to check a few characters and not all strings in your application.

这篇关于检查自定义字体可以显示的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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