-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
78 lines (72 loc) · 2.04 KB
/
simple.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
// 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 (
"seehuhn.de/go/pdf"
)
// Info contains the FirstChar, LastChar and Widths entries of
// a PDF font dictionary, as well as the MissingWidth entry of the
// FontDescriptor dictionary.
type Info struct {
FirstChar pdf.Integer
LastChar pdf.Integer
Widths pdf.Array
MissingWidth float64
}
// EncodeSimple encodes the glyph width information for a simple PDF font.
// The slice ww must have length 256 and is indexed by character code.
// Widths values are given in PDF glyph space units.
func EncodeSimple(ww []float64) *Info {
// find FirstChar and LastChar
cand := make(map[float64]bool)
cand[ww[0]] = true
cand[ww[255]] = true
bestGain := 0
FirstChar := 0
LastChar := 255
var MissingWidth float64
for w := range cand {
b := 255
for b > 0 && ww[b] == w {
b--
}
a := 0
for a < b && ww[a] == w {
a++
}
gain := (255 - b + a) * 4
if w != 0 {
gain -= 15
}
if gain > bestGain {
bestGain = gain
FirstChar = a
LastChar = b
MissingWidth = w
}
}
Widths := make(pdf.Array, LastChar-FirstChar+1)
for i := range Widths {
w := ww[FirstChar+i]
Widths[i] = pdf.Number(w)
}
return &Info{
FirstChar: pdf.Integer(FirstChar),
LastChar: pdf.Integer(LastChar),
Widths: Widths,
MissingWidth: MissingWidth,
}
}