-
Notifications
You must be signed in to change notification settings - Fork 0
/
composite.go
233 lines (209 loc) · 5.09 KB
/
composite.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2024 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 widths
import (
"errors"
"sort"
"seehuhn.de/go/dag"
"seehuhn.de/go/pdf"
"seehuhn.de/go/postscript/cid"
)
// EncodeComposite constructs the W and DW entries for a CIDFont dictionary.
func EncodeComposite(widths map[cid.CID]float64, v pdf.Version) (float64, pdf.Array) {
var ww []cidWidth
for cid, w := range widths {
ww = append(ww, cidWidth{cid, w})
}
sort.Slice(ww, func(i, j int) bool {
return ww[i].CID < ww[j].CID
})
// In pdf versions up to 1.7, "DW" is defined to be an integer.
// From 2.0 onwards, it is defined to be a "number".
// TODO(voss): be more clever here
// TODO(voss): do we need to use the widths[0] here?
dw := 1000.0
if v >= pdf.V2_0 {
dw = mostFrequent(ww)
}
g := wwGraph{ww, dw}
ee, err := dag.ShortestPath(g, len(ww))
if err != nil {
panic(err)
}
var res pdf.Array
pos := 0
for _, e := range ee {
switch {
case e > 0:
wiScaled := pdf.Number(ww[pos].GlyphWidth)
res = append(res,
pdf.Integer(ww[pos].CID),
pdf.Integer(ww[pos+int(e)-1].CID),
wiScaled)
case e < 0:
var wi pdf.Array
for i := pos; i < pos+int(-e); i++ {
wi = append(wi, pdf.Number(ww[i].GlyphWidth))
}
res = append(res,
pdf.Integer(ww[pos].CID),
wi)
}
pos = g.To(pos, e)
}
return dw, res
}
type cidWidth struct {
CID cid.CID
GlyphWidth float64
}
type wwGraph struct {
ww []cidWidth
dw float64
}
// An Edge encodes how the next CID width is encoded.
// The edge values have the following meaning:
//
// e=0: the width of the next CID is the default width, so no entry is needed
// e>0: the next e CIDs have the same width, encode as a range
// e<0: the next -e entries have consecutive CIDs, encode as an array
type wwEdge int16
func (g wwGraph) AppendEdges(ee []wwEdge, v int) []wwEdge {
ww := g.ww
if ww[v].GlyphWidth == g.dw {
return append(ee, 0)
}
n := len(ww)
// positive edges: sequences of CIDS with the same width
i := v + 1
for i < n && ww[i].GlyphWidth == ww[v].GlyphWidth {
i++
}
if i > v+1 {
ee = append(ee, wwEdge(i-v))
}
// negative edges: sequences of consecutive CIDs
i = v
for i < n && int(ww[i].CID)-int(ww[v].CID) == i-v {
i++
ee = append(ee, wwEdge(v-i))
}
return ee
}
func (g wwGraph) Length(v int, e wwEdge) int {
// for simplicity we assume that all integers in the output have 3 digits
if e == 0 {
return 0
} else if e > 0 {
// "%d %d %d\n"
return 12
} else {
// "%d [%d ... %d]\n"
return 6 + 4*int(-e)
}
}
func (g wwGraph) To(v int, e wwEdge) int {
if e == 0 {
return v + 1
}
step := int(e)
if step < 0 {
step = -step
}
return v + step
}
func mostFrequent(ww []cidWidth) float64 {
hist := make(map[float64]int)
for _, wi := range ww {
hist[wi.GlyphWidth]++
}
bestCount := 0
bestVal := 0.0
for wi, count := range hist {
if count > bestCount || (count == bestCount && wi < bestVal) {
bestCount = count
bestVal = wi
}
}
return bestVal
}
// DecodeComposite decodes the W and DW entries of a CIDFont dictionary.
func DecodeComposite(r pdf.Getter, ref pdf.Object, dw float64) (map[cid.CID]float64, error) {
w, err := pdf.GetArray(r, ref)
if err != nil {
return nil, err
}
res := make(map[cid.CID]float64)
for len(w) > 1 {
c0, err := pdf.GetInteger(r, w[0])
if err != nil {
return nil, err
}
obj1, err := pdf.Resolve(r, w[1])
if err != nil {
return nil, err
}
if c1, ok := obj1.(pdf.Integer); ok {
if len(w) < 3 {
break
}
wi, err := pdf.GetNumber(r, w[2])
if err != nil {
return nil, err
}
for c := c0; c <= c1; c++ {
cid := cid.CID(c)
if pdf.Integer(cid) != c {
return nil, &pdf.MalformedFileError{
Err: errors.New("invalid W entry in CIDFont dictionary"),
}
}
if float64(wi) != dw {
res[cid] = float64(wi)
}
}
w = w[3:]
} else {
wi, err := pdf.GetArray(r, w[1])
if err != nil {
return nil, err
}
for _, wiObj := range wi {
wi, err := pdf.GetNumber(r, wiObj)
if err != nil {
return nil, err
}
cid := cid.CID(c0)
if pdf.Integer(cid) != c0 {
return nil, &pdf.MalformedFileError{
Err: errors.New("invalid W entry in CIDFont dictionary"),
}
}
if float64(wi) != dw {
res[cid] = float64(wi)
}
c0++
}
w = w[2:]
}
}
if len(w) != 0 {
return nil, &pdf.MalformedFileError{
Err: errors.New("invalid W entry in CIDFont dictionary"),
}
}
return res, nil
}