Java 中的国际化自动换行
我需要 Java 良好的自动换行处理。不是太困难,除了一个问题:由于我正在开发一个国际化应用程序,它需要正确处理中文、日文和韩文文本。在这些语言中,文字换行发生在字符之间,因为字符本身就是单词并且没有空格。不仅如此,由于该文本可能包含用拉丁字符呈现的外来单词,因此必须对这些单词进行特殊处理,并且不能像文本的其余部分一样在字符之间断开。文本和图形上下文(以字符或像素单位表示的坐标)需要支持换行。
是否有现有的软件包可以做到这一点?我还没见过。如果没有,谁能告诉我一个处理这种情况的好算法?如果需要,代码可以访问与要换行的文本语言相对应的 Locale
对象。贪心算法(每行占用尽可能多的文本)就可以了。
I need good word-wrapping handling for Java. Not too difficult, except for one wrinkle: since I'm working on an internationalized application, it needs to handle Chinese, Japanese and Korean text properly. In those languages, word wrapping occurs between characters, since the characters themselves are words and there are no spaces. Not only that, but since that text may include foreign words rendered with Latin characters, those words must be treated specially and not broken between characters like the rest of the text. Wrapping needs to be supported for both text and graphics context (coordinates expressed in either character or pixel units).
Is there an existing package that does this? I haven't seen one. If not, can anyone show me a good algorithm for handling this scenario? The code would have access to a Locale
object corresponding to the language of the text to be wrapped, if needed. A greedy algorithm (each line takes as much text as possible) is fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BreakIterator 应该有助于打破字符序列变成文字。如果这还不够,我会检查 ICU 项目,看看它是否有更好的东西(一些Java 实现的部分内容来自那里)。图形处理将取决于您的 GUI 库,但 AWT/Swing Font API 支持确定行指标。 (如果您没有“Locale”实例,您可能可以使用 Unicode 块。)
BreakIterator should help here with breaking character sequences into words. If this is insufficient, I'd check the ICU project to see if it had something better (some of the Java implementation comes from there). Graphics handling is going to be dependent on your GUI library, but the AWT/Swing Font API has support for determining line metrics. (If you didn't have 'Locale' instances, you could probably do something heuristically using Unicode blocks.)
看来 ICU4J 库可以满足您的需要。请参阅边界分析。给出的示例适用于 ICU4C,因此是 C/C++ 语言,但也应该在 Java 包中工作。
It appears the the ICU4J library may do what you need. See boundary analysis. The examples given are for ICU4C, and are therefore in C/C++, but should work from the Java package as well.