-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.go
172 lines (154 loc) · 4.13 KB
/
mapping.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
// 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 (
"bytes"
"sort"
"seehuhn.de/go/dag"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/font/charcode"
"seehuhn.de/go/postscript/cid"
)
// GetMapping returns the mapping information from info.
func (info *Info) GetMapping() map[charcode.CharCode]cid.CID {
res := make(map[charcode.CharCode]cid.CID)
for _, s := range info.Singles {
res[s.Code] = s.Value
}
for _, r := range info.Ranges {
val := r.Value
for code := r.First; code <= r.Last; code++ {
res[code] = val
val++
}
}
return res
}
// SetMapping replaces the mapping information in info with the given mapping.
//
// To make efficient use of range entries, the generated mapping may be a
// superset of the original mapping, i.e. it may contain entries for charcodes
// which were not mapped in the original mapping.
func (info *Info) SetMapping(m map[charcode.CharCode]cid.CID) {
entries := make([]entry, 0, len(m))
for code, val := range m {
entries = append(entries, entry{code, val})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].code < entries[j].code
})
g := &encoder{
cs: info.CodeSpaceRange,
mm: entries,
}
ee, err := dag.ShortestPath[int16, uint32](g, len(entries))
if err != nil {
panic(err)
}
info.Singles = info.Singles[:0]
info.Ranges = info.Ranges[:0]
v := 0
for _, e := range ee {
if e == 0 {
info.Singles = append(info.Singles, SingleEntry{
Code: entries[v].code,
Value: entries[v].value,
})
} else {
info.Ranges = append(info.Ranges, RangeEntry{
First: entries[v].code,
Last: entries[v+int(e)-1].code,
Value: entries[v].value,
})
}
v = g.To(v, e)
}
}
type entry struct {
code charcode.CharCode
value cid.CID
}
type encoder struct {
cs charcode.CodeSpaceRange
mm []entry
buf0 pdf.String
buf1 pdf.String
}
func (g *encoder) AppendEdges(ee []int16, v int) []int16 {
if v < 0 || v >= len(g.mm) {
return ee
}
m0 := g.mm[v]
g.buf0 = g.cs.Append(g.buf0[:0], m0.code)
// Find the largest l such that entries v, ..., v+l-1 have codes which only
// differ in the last byte, and such that the difference between values and
// codes is constant.
l := 1
for v+l < len(g.mm) {
m1 := g.mm[v+l]
g.buf1 = g.cs.Append(g.buf1[:0], m1.code)
if !bytes.Equal(g.buf0[:len(g.buf0)-1], g.buf1[:len(g.buf1)-1]) {
break
}
if m1.code-m0.code != charcode.CharCode(m1.value-m0.value) {
break
}
l++
}
if l > 1 {
// We can encode the entries v, ..., v+l-1 as a range of
// l consecutive codes/values. We use l to indicate this
// kind of range.
ee = append(ee, int16(l))
} else {
// We use 0 to indicate an entry in the Singles list
ee = append(ee, 0)
}
return ee
}
func (g *encoder) Length(v int, e int16) uint32 {
// For simplicity we ignore the cost of the "begin...end" operators.
cost := uint32(0)
if e == 0 {
g.buf0 = g.cs.Append(g.buf0[:0], g.mm[v].code)
cost += 2*uint32(len(g.buf0)) + 3 // "<xx> "
cost += ndigit(g.mm[v].value) + 1 // "xxx\n"
} else {
g.buf0 = g.cs.Append(g.buf0[:0], g.mm[v].code)
cost += 4*uint32(len(g.buf0)) + 6 // "<xx> <xx> "
cost += ndigit(g.mm[v].value) + 1 // "xxx\n"
}
return cost
}
func ndigit(cid cid.CID) uint32 {
if cid < 10 {
return 1
} else if cid < 100 {
return 2
} else if cid < 1000 {
return 3
} else if cid < 10000 {
return 4
} else {
return 5
}
}
func (g *encoder) To(v int, e int16) int {
if e == 0 {
return v + 1
}
return v + int(e)
}