Skip to content

Commit

Permalink
unescape numeric code in DA string
Browse files Browse the repository at this point in the history
Maybe this should be implemented in `types.Dict` or `types.StringLiteral`.
  • Loading branch information
ka2n committed Jan 20, 2024
1 parent b89d7b1 commit 86a81ff
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/pdfcpu/primitives/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package primitives

import (
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -392,6 +393,17 @@ func extractFormFontDetails(
return fontID, fName, fLang, fontIndRef, err
}

func unescapeNumericCodes(input string) string {
re := regexp.MustCompile(`\\(\d{3})`)
return re.ReplaceAllStringFunc(input, func(match string) string {
code, err := strconv.Atoi(match[1:])
if err != nil {
return match
}
return string(rune(code))
})
}

func fontFromDA(s string) (string, FormFont, error) {

da := strings.Fields(s)
Expand All @@ -405,7 +417,9 @@ func fontFromDA(s string) (string, FormFont, error) {

for i := 0; i < len(da); i++ {
if da[i] == "Tf" {
fontID = da[i-2][1:]
// replace ASCII escaped fontID. e.g. `\057F2F1` to `/F2F1`
rawFontID := unescapeNumericCodes(da[i-2])
fontID = rawFontID[1:]
//tf.SetFontID(fontID)
fl, err := strconv.ParseFloat(da[i-1], 64)
if err != nil {
Expand Down

0 comments on commit 86a81ff

Please sign in to comment.