Android:找出哪个字体文件适合我要显示的字符

发布于 2024-12-08 18:20:03 字数 695 浏览 0 评论 0原文

我正在维护一个 Android 应用程序,人们用它来显示各种异国语言(如藏语或古希腊语)的字符串。由于Android设备自带的字体很少,用户可以将字体文件放在SD卡上,应用程序将使用它们。

问题:给定一个字符串,我如何自动决定哪个字体文件最合适,以便该字符串出现时不会将字符替换为方块/框?

注意:

  • 每个字符串都是一种语言。
  • 字符串显示在 WebView 中。
  • 自定义字体可以使用,唯一的问题是决定使用哪个字体文件。
  • 它可以提供该字符串可接受的字体列表,而不是单一字体。

<我> 对于好奇的人来说,不必要的上下文:我正在尝试开发此功能: http://code.google.com/p/ankidroid/issues/detail ?id=779


更新:我最终创建了Antisquare 基于 Mostafa 想法的开源库。
它有一个 getSuitableFonts 方法,速度非常快。

I am maintaining an Android app that people use to display strings in various exotic languages like Tibetan or old Greek. Because Android devices come with very few fonts, users can put font files on the SD card, and the app will use them.

QUESTION: Given a string, how can I automatically decide which font file is the most appropriate, so that this string appears without characters being replaced with squares/boxes?

Notes:

  • Each string is in one language.
  • Strings are displayed in a WebView.
  • Custom fonts work, the only problem is deciding which font file to use.
  • Instead of a single font, it could provide a list of fonts that are acceptable for that string.


Unnecessary context, for the curious: I am trying to develop this feature:
http://code.google.com/p/ankidroid/issues/detail?id=779


UPDATE: I ended up creating the Antisquare Open Source library based on Mostafa's idea.
It has a getSuitableFonts method which is blazingly fast.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-12-15 18:20:03

Android 本身并不足以完成这样的任务。 Android 中的加载和渲染字体是在 Skia 中进行的,它是用 C 语言编写的。Skia 会检测是否在字体中找不到某个字符,然后针对此类字符(而不是整个字符串)回退到另一种字体。这就是日语、希伯来语或阿拉伯语文本在 Android 中的显示方式,这正是这些脚本没有粗体的原因! (他们的字体是通过后备选择的,而后备只选择一个字体文件。)

不幸的是,API 中没有提供这种机制,您必须自己构建类似的东西。看起来很复杂,但比看起来容易。您所要做的就是:

  1. 准备每个字体文件中可用的字符列表。
  2. 对于每个字符串,找到具有更多字符串字符的字体。

获取每种字体的字符列表

您不必在 Android 应用程序中即时执行此操作。您可以准备每种字体的字符列表并将这些列表放入您的应用程序中。我这么说是因为使用 Android 中可能不可用的工具会更容易。我会通过字体应用程序中的 Python 脚本来实现这一点(大多数严肃的字体工具都有很棒的 Python 脚本环境),但这些应用程序价格昂贵,而且适合严肃的字体设计师。由于您是 Android 开发人员,我建议使用 sfntly,这是一个 Java 和 C++ 库。使用 sfntly 可以轻松完成您需要的操作(获取字体文件中可用的 Unicode 字符列表)。 此示例适用于 CMap 表(保存字符到字形映射的表),对您来说应该是一个很好的起点。

现在有趣的部分是 snftly 是用 Java 编写的,您可以将其包含在您的 Android 应用程序中并自动执行所有操作。这太棒了,我建议您从熟悉 snftly 开始。

选择字体

在完成上一部分之后,您将获得每种字体的 Unicode 字符列表,并且根据这些列表选择提供每个字符串的大多数字符的字体文件是很简单的。

Android by itself does not provide enough for such a task. Loading and rendering fonts in Android happens in Skia, which is written in C. Skia detects if a character can't be found in a font and falls back to another font for such characters (not the whole string). That's how Japanese, Hebrew, or Arabic text is shown in Android and that's exactly why these scripts don't have bold face! (Their font is selected through fallback and fallback only selects one font file.)

Unfortunately, this mechanism is not provided in APIs and you have to build similar thing on your own. It seems complicated, but is easier than it looks. All you have to do is:

  1. Prepare lists of characters available in each font file.
  2. For every string find the font that has more characters of the string.

Getting list of characters in each font

You don't have to do this on-the-fly in your Android app. You can prepare the list of characters in each font and put these lists in your app. I say that because this is way easier with tools that may not be available in Android. I would do that through Python scripting in a font app (most serious font tools have awesome Python scripting environments), but these apps are expensive and are for serious type designers. Since you're an Android developer, I recommend using sfntly, a library in Java and C++. Doing what you need (getting a list of Unicode characters available in a font file) is easy with sfntly. This sample works with CMap tables (tables that hold character to glyph mapping) and should be a good starting point for you.

Now the interesting part is that snftly is in Java and you may be able to include that in your Android app and do everything automatically. That's awesome by I recommend you start by getting familiar with snftly.

Selecting the font

After the previous part you'll have a list of Unicode character for every font, and based on these lists selecting the font file that provides most characters of every string is trivial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文