Java国际化自动换行 [英] Internationalized word wrapping in Java

查看:232
本文介绍了Java国际化自动换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对Java进行良好的自动换行处理.并非难事,除了一个皱纹:因为我正在开发一个国际化应用程序,所以它需要正确处理中文,日文和韩文文本.在那些语言中,由于字符本身是单词并且没有空格,因此在字符之间会出现自动换行.不仅如此,而且由于该文本可能包含以拉丁字符表示的外来词,因此必须特别对待这些那些词,并且不要像其他文本一样在字符之间打断它们.文本和图形上下文都需要支持包装(以字符或像素为单位表示的坐标).

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).

是否存在现有的软件包?我还没看过如果没有,谁能给我看一个很好的算法来处理这种情况?如果需要,代码可以访问与要包装的文本的语言相对应的Locale对象.贪婪算法(每行需要尽可能多的文本)是可以的.

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.

推荐答案

ICU项目看看它是否有更好的选择(有些的Java实现来自那里).图形处理将取决于您的GUI库,但是AWT/Swing

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.)

这篇关于Java国际化自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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