-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
139 lines (109 loc) · 2.82 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package podofo
import (
"bytes"
"github.com/denisss025/go-podofo/internal/pdf"
)
type StringState uint8
const (
// StringStateRawBuffer is for an unvaluated
// raw buffer string.
StringStateRawBuffer StringState = iota
// StringStateASCII is for both an ASCII
// and PDFDocEncoding charsets.
StringStateASCII
// StringStateDocEncoding is for strings
// that use the whole PDFDocEncoding charset.
StringStateDocEncoding
// StringStateUnicode is for strings that
// use the whole Unicode charset.
StringStateUnicode
)
type StringEncoding uint8
const (
StringEncodingUTF8 StringEncoding = iota
StringEncodingUTF16BE
StringEncodingUTF16LE
StringEncodingPDFDoc
)
type stringData struct {
State StringState
Chars []byte
}
type String struct {
pdf.BaseObject
data *stringData
isHex bool
}
func (s *String) isValidText() (bool, error) {
switch s.data.State {
case StringStateASCII, StringStateDocEncoding, StringStateUnicode:
return true, nil
case StringStateRawBuffer:
return false, nil
default:
return false, ErrInvalidEnumValue
}
}
func (s *String) Kind() ObjectKind { return pdf.ObjectKindString }
func (s *String) UnmarshalBinary(data []byte) error {
panic("not implemented") // TODO: Implement
}
func (s *String) unmarshalUTF8(text []byte) error {
if len(text) == 0 {
s.data.Chars = s.data.Chars[:0]
s.data.State = StringStateASCII
return nil
}
panic("not implemented") // TODO: implement me
}
func (s *String) MarshalPDF(w *pdf.Writer) error {
panic("not implemented") // TODO: implement me
}
func (s *String) IsHex() bool { return s.isHex }
func (s *String) State() StringState {
return s.data.State
}
func (s *String) IsZero() bool { return s.data.Chars == nil }
func (s *String) String() string {
panic("not implemented") // TODO: implement me
}
func (s *String) evaluateString() error {
panic("not implemented") // TODO: implement me
}
func (s *String) Copy() (Object, error) {
panic("not implemented") // TODO: implement me
}
func (s *String) canPerformComparison(rhs *String) (bool, error) {
if s.State() == rhs.State() {
return true, nil
}
if ok, err := s.isValidText(); ok || err != nil {
return ok, err
}
// TODO? check if this is the same to
// if ok, err := rhs.isValidText(); ok || err != nil {
// return ok, err
// }
// return false, nil
return rhs.isValidText()
}
func (s *String) RawData() []byte {
return s.data.Chars
}
func getStringEncoding(text []byte) (enc StringEncoding) {
const (
utf16BEMarker = "\xEE\xFF"
utf16LEMarker = "\xFF\xFE"
utf8Marker = "\xEF\xBB\xBF"
)
enc = StringEncodingPDFDoc
switch {
case bytes.HasPrefix(text, []byte(utf16BEMarker)):
enc = StringEncodingUTF16BE
case bytes.HasPrefix(text, []byte(utf16LEMarker)):
enc = StringEncodingUTF16LE
case bytes.HasPrefix(text, []byte(utf8Marker)):
enc = StringEncodingUTF8
}
return enc
}