From 85fbb77d63874b8ef4eb3124cdca717a7791a9b5 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Mon, 12 Oct 2026 23:39:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=A9=BA=E6=A0=BC=E5=A4=84=E7=90=86):=20?= =?UTF-8?q?=E8=A7=84=E8=8C=83=E7=A9=BA=E6=A0=BC=E8=BD=AC=E4=B9=89=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E5=AD=97=E9=94=99=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_editor.go | 4 ++-- ofdgo_editor_xml.go | 9 +++++++++ ofdgo_text.go | 13 +++++++++++++ ofdgo_writer.go | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ofdgo_editor.go b/ofdgo_editor.go index 3950476..ce42acc 100644 --- a/ofdgo_editor.go +++ b/ofdgo_editor.go @@ -1371,13 +1371,13 @@ func ofdNumber(value float64) string { return strconv.FormatFloat(value, 'f', -1, 64) } -// escapeOFDText 转义控制字符、反斜线和XML不允许的字符 +// escapeOFDText 转义控制字符和反斜线,空格由XML写出层转义 // 入参: value 原文 // 返回: string OFD文字编码 func escapeOFDText(value string) string { var text strings.Builder for _, char := range value { - if char <= ' ' || char == '\\' || char == '\ufffe' || char == '\uffff' { + if char < ' ' || char == '\\' || char == '\ufffe' || char == '\uffff' { fmt.Fprintf(&text, "\\%04X", char) } else { text.WriteRune(char) diff --git a/ofdgo_editor_xml.go b/ofdgo_editor_xml.go index f4ed54e..f2c9fee 100644 --- a/ofdgo_editor_xml.go +++ b/ofdgo_editor_xml.go @@ -239,10 +239,19 @@ func editorXMLTextValue(data []byte, node *editorXML, before, after string) ([]b if before == after { return data[node.start:node.end], nil } + textCode := node.matchesOFD("TextCode") + if textCode { + after = textCodeXMLValue(after) + } var content bytes.Buffer if err := xml.EscapeText(&content, []byte(after)); err != nil { return nil, err } + if textCode && bytes.IndexByte(content.Bytes(), ' ') >= 0 { + escaped := bytes.ReplaceAll(content.Bytes(), []byte(" "), []byte(" ")) + content.Reset() + content.Write(escaped) + } if node.open < node.close { inner := data[node.open:node.close] decoder := xml.NewDecoder(bytes.NewReader(inner)) diff --git a/ofdgo_text.go b/ofdgo_text.go index f386e21..aee14cc 100644 --- a/ofdgo_text.go +++ b/ofdgo_text.go @@ -278,6 +278,19 @@ func textDelta(deltas []float64, index int) (float64, bool) { return deltas[len(deltas)-1], true } +// textCodeXMLValue 将空格转义还原为字符,供XML写出层统一编码 +// 入参: value OFD文字编码 +// 返回: string 待写出的文字编码 +func textCodeXMLValue(value string) string { + if !strings.Contains(value, `\0020`) { + return value + } + if strings.ContainsAny(value, "\r\n") { + return escapeOFDText(string(textCodeRunes(value))) + } + return strings.ReplaceAll(value, `\0020`, " ") +} + // textCodeRunes 获取文本编码字符 // 入参: value 文本编码内容 // 返回: []rune 文本字符 diff --git a/ofdgo_writer.go b/ofdgo_writer.go index 5c12c83..f07f286 100644 --- a/ofdgo_writer.go +++ b/ofdgo_writer.go @@ -1107,6 +1107,7 @@ func (x *ofdXML) textCode(value string, attrs ofdAttrs) { if !x.checkContext() { return } + value = textCodeXMLValue(value) x.escaped.Reset() if x.err = xml.EscapeText(&x.escaped, []byte(value)); x.err != nil { return