-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
169 lines (152 loc) · 4.76 KB
/
encoding.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
// 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 encoding
import (
"math/bits"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"seehuhn.de/go/pdf/font/charcode"
"seehuhn.de/go/pdf/font/pdfenc"
"seehuhn.de/go/postscript/type1/names"
"seehuhn.de/go/sfnt/glyph"
)
// SimpleEncoder constructs and stores mappings from one-byte character codes
// to GID values and from one-byte character codes to unicode strings.
type SimpleEncoder struct {
Encoding []glyph.ID
code map[key]byte
key map[byte]key
}
type key struct {
gid glyph.ID
rr string
}
// NewSimpleEncoder allocates a new SimpleEncoder.
func NewSimpleEncoder() *SimpleEncoder {
res := &SimpleEncoder{
Encoding: make([]glyph.ID, 256),
code: make(map[key]byte),
key: make(map[byte]key),
}
return res
}
// WritingMode implements the [font.NewFont] interface.
func (e *SimpleEncoder) WritingMode() int {
return 0 // simple fonts are always horizontal
}
// GIDToCode returns the character code for the given glyph ID (allocating new
// codes as needed). It also records the fact that the character code
// corresponds to the given unicode string.
func (e *SimpleEncoder) GIDToCode(gid glyph.ID, rr []rune) byte {
k := key{gid, string(rr)}
// Rules for choosing the code:
// 1. If the combination of `gid` and `rr` has previously been used,
// then use the same code as before.
code, seen := e.code[k]
if seen {
return code
}
// 2. Allocate a new code based on the last rune in rr.
var r rune
if len(rr) > 0 {
r = rr[len(rr)-1]
}
code = e.allocateCode(r)
e.Encoding[code] = gid
e.code[k] = code
e.key[code] = k
return code
}
func (e *SimpleEncoder) allocateCode(r rune) byte {
if len(e.code) >= 256 {
// Once all codes are used up, simply return 0 for everything.
return 0
}
bestScore := -1
bestCode := byte(0)
for codeInt := 0; codeInt < 256; codeInt++ {
code := byte(codeInt)
if _, alreadyUsed := e.key[code]; alreadyUsed {
continue
}
var score int
q := rune(code)
stdName := pdfenc.StandardEncoding[code]
if stdName == ".notdef" {
// fill up the unused slots first
score += 100
} else {
q = names.ToUnicode(stdName, false)[0]
if q == r {
// If r is in the standard encoding, and the corresponding
// code is still free, then use it.
bestCode = code
break
} else if !(code == 32 && r != ' ') {
// Try to keep code 32 for the space character,
// in order to not break the PDF word spacing parameter.
score += 10
}
}
score += bits.TrailingZeros16(uint16(r) ^ uint16(q))
if score > bestScore {
bestScore = score
bestCode = code
}
}
return bestCode
}
// CodeIsUsed returns true if the given code has already been allocated.
// This can be used to distinguish between codes which have
// explicitly been mapped to GID 0 and codes which are not used.
func (e *SimpleEncoder) CodeIsUsed(code byte) bool {
_, used := e.key[code]
return used
}
// Overflow returns true if the encoder has run out of codes.
func (e *SimpleEncoder) Overflow() bool {
return len(e.code) > 256
}
// Subset returns the subset of glyph IDs which are used by this encoder.
// The result is sorted and always include the glyph ID 0.
func (e *SimpleEncoder) Subset() []glyph.ID {
gidUsed := make(map[glyph.ID]bool, len(e.code)+1)
gidUsed[0] = true
for key := range e.code {
gidUsed[key.gid] = true
}
subset := maps.Keys(gidUsed)
slices.Sort(subset)
return subset
}
// ToUnicode returns the mapping from character codes to unicode strings.
// This can be used to construct a PDF ToUnicode CMap.
func (e *SimpleEncoder) ToUnicode() map[charcode.CharCode][]rune {
toUnicode := make(map[charcode.CharCode][]rune)
for k, v := range e.code {
toUnicode[charcode.CharCode(v)] = []rune(k.rr)
}
return toUnicode
}
// ToUnicodeNew returns the mapping from character codes to unicode strings.
// This can be used to construct a PDF ToUnicode CMap.
func (e *SimpleEncoder) ToUnicodeNew() map[string][]rune {
toUnicode := make(map[string][]rune, len(e.code))
for k, c := range e.code {
toUnicode[string([]byte{c})] = []rune(k.rr)
}
return toUnicode
}