You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PC开源游戏客户端移植Android:NPC聊天文本换行解析异常

NPC聊天文本解析换行异常问题

问题场景

将开源游戏客户端从PC移植到Android平台时,处理NPC聊天内容遇到文本解析异常:第二行“How do I move?”未正常换行,其余以\r\n结尾的行均能正确换行。

服务器返回的原始文本

formatted_text_ = "Now...ask me any questions you may have on traveling!!\r\n#L0##bHow do I move?#l\r\n#L1#How do I take down the monsters?#l\r\n#L2#How can I pick up an item?#l\r\n#L3#What happens when I die?#l\r\n#L4#When can I choose a job?#l\r\n#L5#Tell me more about this island!#l\r\n#L6#What should I do to become a Warrior?#l\r\n#L7#What should I do to become a Bowman?#l\r\n#L8#What should I do to become a Magician?#l\r\n#L9#What should I do to become a Thief?#l\r\n#L10#How do I raise the character stats? (S)#l\r\n#L11#How do I check the items that I just picked up?#l\r\n#L12#How do I put on an item?#l\r\n#L13#How do I check out the items that I'm wearing?#l\r\n#L14#What are skills? (K)#l\r\n#L15#How do I get to Victoria Island?#l\r\n#L16#What are mesos?#l#k"

\r和\n的处理逻辑

switch (text[first]) {
    case '\\':
        if (first + 1 < last) {
            switch (text[first + 1]) {
                case 'n':
                    linebreak = true;
                    break;
                case 'r':
                    linebreak = ax_ > 0;
                    break;
            }

            skip++;
        }

        skip++;
        break;

换行触发逻辑

当linebreak为true或文本长度超过最大宽度时,应添加新行:

bool newword = skip > 0;
bool newline = linebreak || ax_ + wordwidth > max_width_;

if (newword || newline) {
    add_word(prev, first, last_font, last_color);
}

if (newline) {
    add_line();

    endy_ = ay_;
    ax_ = 0;
    ay_ += font_.linespace();

    if (!lines_.empty()) {
        ay_ -= line_adj_;
    }
}

异常效果

异常效果

相关处理代码位置

  • UINpcTalk.cpp 第141行
  • GraphicsGL.cpp 第617行
  • GraphicsGL.cpp 第657行

内容的提问来源于stack exchange,提问作者k0ala Xing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 22:52:46