Skip to content

Commit

Permalink
Merge pull request rkusa#252 from severi/master
Browse files Browse the repository at this point in the history
Fix PDF 1.2 name  # escape logic
  • Loading branch information
rkusa authored Apr 22, 2021
2 parents ae3a260 + 968ee55 commit a41d74b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/object/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ class PDFName {
throw new Error('A Name mustn\'t contain the null characters')
}

// delimiter characters are not allowed
if (name.match(/[\(\)<>\[\]\{\}\/\%]/)) {
throw new Error('A Name mustn\'t contain delimiter characters')
}

name = name.toString()

// Beginning with PDF 1.2, any character except null (character code 0)
Expand All @@ -37,7 +32,23 @@ class PDFName {
if (code > 0xff) {
code = 0x5f
}
return '#' + code
return '#' + Number(code).toString(16)
})

// Add # in front of delimiter characters
// 25 %
// 28 (
// 29 )
// 2f /
// 3c <
// 3e >
// 5b [
// 5d ]
// 7b {
// 7d }
name = name.replace(/[\x25\x28\x29\x2f\x3c\x3e\x5b\x5d\x7b\x7d]/g, function(c) {
let code = c.charCodeAt(0)
return '#' + Number(code).toString(16)
})

this.name = name
Expand Down

0 comments on commit a41d74b

Please sign in to comment.