-
Notifications
You must be signed in to change notification settings - Fork 0
/
tu-write.go
155 lines (141 loc) · 3.64 KB
/
tu-write.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cmap
import (
"fmt"
"io"
"strings"
"text/template"
"unicode/utf16"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/font/charcode"
)
// Embed adds the ToUnicode cmap to a PDF file.
func (info *ToUnicode) Embed(w pdf.Putter, ref pdf.Reference) error {
stm, err := w.OpenStream(ref, nil, pdf.FilterCompress{})
if err != nil {
return err
}
err = info.Write(stm)
if err != nil {
return fmt.Errorf("embedding ToUnicode cmap: %w", err)
}
err = stm.Close()
if err != nil {
return err
}
return nil
}
func (info *ToUnicode) Write(w io.Writer) error {
return toUnicodeTmpl.Execute(w, info)
}
func tuSingleChunks(x []SingleTUEntry) [][]SingleTUEntry {
var res [][]SingleTUEntry
for len(x) >= chunkSize {
res = append(res, x[:chunkSize])
x = x[chunkSize:]
}
if len(x) > 0 {
res = append(res, x)
}
return res
}
func tuRangeChunks(x []RangeTUEntry) [][]RangeTUEntry {
var res [][]RangeTUEntry
for len(x) >= chunkSize {
res = append(res, x[:chunkSize])
x = x[chunkSize:]
}
if len(x) > 0 {
res = append(res, x)
}
return res
}
func hexRunes(rr []rune) string {
val := utf16.Encode(rr)
if len(val) == 1 {
return fmt.Sprintf("<%04x>", val[0])
}
valStrings := make([]string, len(val))
for i, v := range val {
valStrings[i] = fmt.Sprintf("%04x", v)
}
return "<" + strings.Join(valStrings, "") + ">"
}
// TODO(voss): once https://github.com/pdf-association/pdf-issues/issues/344
// is resoved, reconsider CIDSystemInfo and CMapName.
var toUnicodeTmpl = template.Must(template.New("tounicode").Funcs(template.FuncMap{
"B": func(x []byte) string {
return fmt.Sprintf("<%02x>", x)
},
"SingleChunks": tuSingleChunks,
"Single": func(cs charcode.CodeSpaceRange, s SingleTUEntry) string {
var buf []byte
buf = cs.Append(buf, s.Code)
val := hexRunes(s.Value)
return fmt.Sprintf("<%x> %s", buf, val)
},
"RangeChunks": tuRangeChunks,
"Range": func(cs charcode.CodeSpaceRange, s RangeTUEntry) string {
var first, last []byte
first = cs.Append(first, s.First)
last = cs.Append(last, s.Last)
if len(s.Values) == 1 {
return fmt.Sprintf("<%x> <%x> %s", first, last, hexRunes(s.Values[0]))
}
var repl []string
for _, v := range s.Values {
repl = append(repl, hexRunes(v))
}
return fmt.Sprintf("<%x> <%x> [%s]", first, last, strings.Join(repl, " "))
},
}).Parse(`/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
/CIDSystemInfo <<
/Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
{{with .CS -}}
{{len .}} begincodespacerange
{{range . -}}
{{B .Low}} {{B .High}}
{{end -}}
{{end -}}
endcodespacerange
{{$cs := .CS -}}
{{range SingleChunks .Singles -}}
{{len .}} beginbfchar
{{range . -}}
{{Single $cs .}}
{{end -}}
endbfchar
{{end -}}
{{range RangeChunks .Ranges -}}
{{len .}} beginbfrange
{{range . -}}
{{Range $cs .}}
{{end -}}
endbfrange
{{end -}}
endcmap
CMapName currentdict /CMap defineresource pop
end
end
`))