-
Notifications
You must be signed in to change notification settings - Fork 0
/
type1.go
163 lines (150 loc) · 4.12 KB
/
type1.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
// 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 (
"bytes"
"errors"
"fmt"
"math"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/font/pdfenc"
)
// DescribeEncodingType1 returns the /Encoding entry for the font dictionary
// of a Type 1 font. The arguments are the encoding used by the client,
// and the font's builtin encoding.
//
// See section 9.6.1 and 9.6.5 of ISO 32000-2:2020.
func DescribeEncodingType1(encoding, builtin []string) pdf.Object {
type cand struct {
name pdf.Object
enc []string
}
candidates := []cand{
{nil, builtin},
{pdf.Name("WinAnsiEncoding"), pdfenc.WinAnsiEncoding[:]},
{pdf.Name("MacRomanEncoding"), pdfenc.MacRomanEncoding[:]},
{pdf.Name("MacExpertEncoding"), pdfenc.MacExpertEncoding[:]},
}
type D struct {
code int
newName pdf.Name
}
var diff []D
var desc pdf.Dict
descLen := math.MaxInt
for _, cand := range candidates {
diff = diff[:0]
for code, name := range encoding {
if name != ".notdef" && name != cand.enc[code] {
diff = append(diff, D{code, pdf.Name(name)})
}
}
if len(diff) == 0 {
return cand.name
}
newDesc := pdf.Dict{}
if cand.name != nil {
newDesc["BaseEncoding"] = cand.name
}
var a pdf.Array
prev := 256
for _, d := range diff {
if d.code != prev+1 {
a = append(a, pdf.Integer(d.code))
}
a = append(a, d.newName)
prev = d.code
}
newDesc["Differences"] = a
b := &bytes.Buffer{}
newDesc.PDF(b)
if b.Len() < descLen {
desc = newDesc
descLen = b.Len()
}
}
return desc
}
// UndescribeEncodingType1 returns the encoding used by the client, given
// the /Encoding entry for the font dictionary of a Type 1 font and the
// font's builtin encoding.
//
// This function is nearly the inverse of [DescribeEncodingType1]: if
// the name assigned to a code is not `.notdef`, then [DescribeEncodingType1]
// followed by [UndescribeEncodingType1] will return the same name.
func UndescribeEncodingType1(r pdf.Getter, desc pdf.Object, builtin []string) ([]string, error) {
desc, err := pdf.Resolve(r, desc)
if err != nil {
return nil, err
}
switch desc := desc.(type) {
case nil:
return builtin, nil
case pdf.Name:
return getNamedEncoding(desc)
case pdf.Dict:
base, err := pdf.GetName(r, desc["BaseEncoding"])
if err != nil {
return nil, err
}
res := make([]string, 256)
baseEnc := builtin
if base != "" {
baseEnc, err = getNamedEncoding(base)
if err != nil {
return nil, err
}
}
if baseEnc == nil {
return nil, errors.New("encoding: invalid base encoding")
}
copy(res, baseEnc)
a, err := pdf.GetArray(r, desc["Differences"])
if err != nil {
return nil, err
}
code := -1
for _, x := range a {
switch x := x.(type) {
case pdf.Integer:
code = int(x)
case pdf.Name:
if code < 0 || code >= 256 {
return nil, fmt.Errorf("encoding: invalid code %d", code)
}
res[code] = string(x)
code++
default:
return nil, fmt.Errorf("encoding: expected Integer or Name, got %T", x)
}
}
return res, nil
default:
return nil, fmt.Errorf("encoding: expected Name or Dict, got %T", desc)
}
}
func getNamedEncoding(name pdf.Name) ([]string, error) {
switch name {
case "WinAnsiEncoding":
return pdfenc.WinAnsiEncoding[:], nil
case "MacRomanEncoding":
return pdfenc.MacRomanEncoding[:], nil
case "MacExpertEncoding":
return pdfenc.MacExpertEncoding[:], nil
default:
return nil, fmt.Errorf("unknown encoding %q", name)
}
}