From dd0fb9758305c68031a5f7acf8e9c38440fb2ebe Mon Sep 17 00:00:00 2001 From: qiudejia Date: Mon, 29 Sep 2025 16:18:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=AD=E6=96=87=E4=B9=B1?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qelmt/dynamictext.rs | 2 ++ src/qelmt/mod.rs | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/qelmt/dynamictext.rs b/src/qelmt/dynamictext.rs index 3cd8b2b..26eac9b 100644 --- a/src/qelmt/dynamictext.rs +++ b/src/qelmt/dynamictext.rs @@ -433,6 +433,8 @@ impl<'a> DTextBuilder<'a> { /*dbg!(&value); dbg!(&y); dbg!(&self.text);*/ + let value = super::strip_mtext_control_sequences(&value); + DynamicText { //x: x - (calc_width as f64/2.0), x, diff --git a/src/qelmt/mod.rs b/src/qelmt/mod.rs index ab19b02..ba75776 100644 --- a/src/qelmt/mod.rs +++ b/src/qelmt/mod.rs @@ -1879,5 +1879,64 @@ fn decode_unicode_escapes(text: &str) -> String { } } + result +} + +/// 移除常见的 MTEXT 控制序列(如 `\fSimSun|b0|i0;`)并做基础替换 +pub(crate) fn strip_mtext_control_sequences(input: &str) -> String { + let mut result = String::with_capacity(input.len()); + let mut chars = input.chars().peekable(); + + while let Some(ch) = chars.next() { + if ch == '\\' { + let mut handled = false; + + if let Some(&cmd) = chars.peek() { + match cmd { + 'P' => { + chars.next(); // consume command + result.push('\n'); + handled = true; + } + 'f' | 'F' | 'H' | 'h' | 'C' | 'c' | 'W' | 'w' | 'A' | 'a' | 'p' | 'q' => { + chars.next(); // consume command + while let Some(next_ch) = chars.next() { + if next_ch == ';' { + break; + } + } + handled = true; + } + 'L' | 'l' | 'O' | 'o' | 'K' | 'k' => { + chars.next(); // toggle style command, no payload + handled = true; + } + '~' => { + chars.next(); + result.push(' '); + handled = true; + } + '\\' => { + chars.next(); + result.push('\\'); + handled = true; + } + '{' | '}' => { + chars.next(); + result.push(cmd); + handled = true; + } + _ => {} + } + } + + if handled { + continue; + } + } + + result.push(ch); + } + result } \ No newline at end of file