mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 14:53:44 +08:00
1 parent
f8263f798f
commit
85fbb77d63
4 files changed
+25
-2
No files matched your search
+2
-2
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 文本字符
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user