B4J Question BCTextEngine's WordWrap Does not Work for Chinese

xulihang

Well-Known Member
Licensed User
Longtime User
There are no spaces in Chinese and Japanese.

I modified `HandleTextRun` to make the word wrapping work.


B4X:
Private Sub HandleTextRun (Run As BCTextRun, Unbreakables As List, style As BCParagraphStyle)
    Dim fm As BCFontMetrics = GetFontMetrics(Run.TextFont, Run.TextColor)
    Dim i1 As Int
    For i = 0 To Run.TextChars.Length - 1
        Dim c As String = Run.TextChars.Buffer(Run.TextChars.StartIndex + i)
        If WordBoundaries.Contains(c) Or Utils.isChinese(c)  Or Utils.isJapanese(c) Then
......

Here is the isChinese and isJapanese sub:


B4X:
Sub isChinese(text As String) As Boolean
    Dim jo As JavaObject
    jo=Me
    Return jo.RunMethod("isChinese",Array As String(text))
End Sub

Sub isJapanese(text As String) As Boolean
    Dim jo As JavaObject
    jo=Me
    Return jo.RunMethod("isJapanese",Array As String(text))
End Sub

#If JAVA
import java.util.regex.Pattern;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.text.Font;
import javafx.scene.text.TextBoundsType;
// 日语字符的正则表达式模式
private static final Pattern JAPANESE_PATTERN = Pattern.compile(
    "[\\u3040-\\u309F\\u30A0-\\u30FF\\u4E00-\\u9FFF\\uFF00-\\uFFEF]+"
);

/**
 * 使用正则表达式检测是否包含日语字符
 */
public static boolean isJapanese(String text) {
    if (text == null || text.trim().isEmpty()) {
        return false;
    }
    return JAPANESE_PATTERN.matcher(text).find();
}

private static boolean isChinese(char c) {

    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B

            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS

            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {

        return true;

    }

    return false;

}



// 完整的判断中文汉字和符号

public static boolean isChinese(String strName) {

    char[] ch = strName.toCharArray();

    for (int i = 0; i < ch.length; i++) {

        char c = ch[i];

        if (isChinese(c)) {

            return true;

        }

    }

    return false;

}
#End If
 
Last edited:

xulihang

Well-Known Member
Licensed User
Longtime User
No. Chinese and Japanese can break at any characters. I think adding word boundaries is not enough.

The expected result is like this:

 
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
I see. We can set the entire sentence as word boundaries for Chinese and Japanese:

B4X:
TextEngine.WordBoundaries = "你好,请问有什么能够帮你的吗?我是您的贴心小助手,爱你哟"

But it is more like a workaround.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…