@@ -24,8 +24,8 @@ import (
2424)
2525
2626// KeywordsList returns a list of keywords as recorded in the document info dict.
27- func KeywordsList (xRefTable * model.XRefTable ) ([]string , error ) {
28- ss := strings .FieldsFunc (xRefTable .Keywords , func (c rune ) bool { return c == ',' || c == ';' || c == '\r' })
27+ func KeywordsList (ctx * model.Context ) ([]string , error ) {
28+ ss := strings .FieldsFunc (ctx .Keywords , func (c rune ) bool { return c == ',' || c == ';' || c == '\r' })
2929 for i , s := range ss {
3030 ss [i ] = strings .TrimSpace (s )
3131 }
@@ -34,34 +34,49 @@ func KeywordsList(xRefTable *model.XRefTable) ([]string, error) {
3434
3535// KeywordsAdd adds keywords to the document info dict.
3636// Returns true if at least one keyword was added.
37- func KeywordsAdd (xRefTable * model.XRefTable , keywords []string ) error {
37+ func KeywordsAdd (ctx * model.Context , keywords []string ) error {
38+ if err := ensureInfoDictAndFileID (ctx ); err != nil {
39+ return err
40+ }
3841
39- list , err := KeywordsList (xRefTable )
42+ list , err := KeywordsList (ctx )
4043 if err != nil {
4144 return err
4245 }
4346
44- for _ , s := range keywords {
45- if ! types .MemberOf (s , list ) {
46- xRefTable .Keywords += ", " + types .UTF8ToCP1252 (s )
47+ for _ , kw := range keywords {
48+ if ! types .MemberOf (kw , list ) {
49+ if len (ctx .Keywords ) == 0 {
50+ ctx .Keywords = kw
51+ } else {
52+ ctx .Keywords += ", " + kw
53+ }
4754 }
4855 }
4956
50- d , err := xRefTable .DereferenceDict (* xRefTable .Info )
57+ d , err := ctx .DereferenceDict (* ctx .Info )
5158 if err != nil || d == nil {
5259 return err
5360 }
5461
55- d ["Keywords" ] = types .StringLiteral (xRefTable .Keywords )
62+ s , err := types .EscapeUTF16String (ctx .Keywords )
63+ if err != nil {
64+ return err
65+ }
66+
67+ d ["Keywords" ] = types .StringLiteral (* s )
5668
5769 return nil
5870}
5971
6072// KeywordsRemove deletes keywords from the document info dict.
6173// Returns true if at least one keyword was removed.
62- func KeywordsRemove (xRefTable * model.XRefTable , keywords []string ) (bool , error ) {
63- // TODO Handle missing info dict.
64- d , err := xRefTable .DereferenceDict (* xRefTable .Info )
74+ func KeywordsRemove (ctx * model.Context , keywords []string ) (bool , error ) {
75+ if ctx .Info == nil {
76+ return false , nil
77+ }
78+
79+ d , err := ctx .DereferenceDict (* ctx .Info )
6580 if err != nil || d == nil {
6681 return false , err
6782 }
@@ -72,34 +87,36 @@ func KeywordsRemove(xRefTable *model.XRefTable, keywords []string) (bool, error)
7287 return true , nil
7388 }
7489
75- kw := make ([]string , len (keywords ))
76- for i , s := range keywords {
77- kw [i ] = types .UTF8ToCP1252 (s )
78- }
79-
80- // Distil document keywords.
81- ss := strings .FieldsFunc (xRefTable .Keywords , func (c rune ) bool { return c == ',' || c == ';' || c == '\r' })
90+ ss := strings .FieldsFunc (ctx .Keywords , func (c rune ) bool { return c == ',' || c == ';' || c == '\r' })
8291
83- xRefTable .Keywords = ""
92+ ctx .Keywords = ""
8493 var removed bool
8594 first := true
8695
8796 for _ , s := range ss {
8897 s = strings .TrimSpace (s )
89- if types .MemberOf (s , kw ) {
98+ if types .MemberOf (s , keywords ) {
9099 removed = true
91100 continue
92101 }
102+
93103 if first {
94- xRefTable .Keywords = s
104+ ctx .Keywords = s
95105 first = false
96106 continue
97107 }
98- xRefTable .Keywords += ", " + s
108+
109+ ctx .Keywords += ", " + s
99110 }
100111
101112 if removed {
102- d ["Keywords" ] = types .StringLiteral (xRefTable .Keywords )
113+
114+ s , err := types .EscapeUTF16String (ctx .Keywords )
115+ if err != nil {
116+ return false , err
117+ }
118+
119+ d ["Keywords" ] = types .StringLiteral (* s )
103120 }
104121
105122 return removed , nil
0 commit comments