-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyph.go
140 lines (123 loc) · 3.72 KB
/
glyph.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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2021 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 type3
import (
"bytes"
"errors"
"fmt"
"math"
"regexp"
"strconv"
"seehuhn.de/go/pdf"
"seehuhn.de/go/postscript/funit"
"seehuhn.de/go/pdf/graphics"
)
// AddGlyph adds a new glyph to the type 3 font.
//
// If shapeOnly is true, a call to the "d1" operator is added at the start of
// the glyph description. In this case, the glyph description may only specify
// the shape of the glyph, but not its color. Otherwise, a call to the "d0"
// operator is added at the start of the glyph description. In this case, the
// glyph description may specify both the shape and the color of the glyph.
func (f *Font) AddGlyph(name string, widthX funit.Int16, bbox funit.Rect16, shapeOnly bool) (*GlyphBuilder, error) {
if f.NumOpen < 0 {
return nil, errors.New("font already embedded")
}
f.NumOpen++
if _, exists := f.Glyphs[name]; exists {
return nil, fmt.Errorf("glyph %q already present", name)
} else if name == "" {
return nil, errors.New("empty glyph name")
}
f.Glyphs[name] = nil // reserve the name
buf := &bytes.Buffer{}
page := graphics.NewWriter(buf, pdf.V1_7) // TODO(voss): what to use as the PDF version here?
// TODO(voss): consider the discussion at
// https://pdf-issues.pdfa.org/32000-2-2020/clause07.html#H7.8.3
page.Resources = f.Resources
if shapeOnly {
fmt.Fprintf(page.Content,
"%d 0 %d %d %d %d d1\n",
widthX, bbox.LLx, bbox.LLy, bbox.URx, bbox.URy)
page.Set = graphics.StateStrokeColor | graphics.StateFillColor
} else {
fmt.Fprintf(page.Content, "%d 0 d0\n", widthX)
page.Set = 0
}
g := &GlyphBuilder{
Writer: page,
name: name,
widthX: widthX,
bbox: bbox,
f: f,
}
return g, nil
}
// GlyphBuilder is used to write a glyph description as described in section
// 9.6.5 of PDF 32000-1:2008. The .Close() method must be called after
// the description has been written.
type GlyphBuilder struct {
*graphics.Writer
name string
widthX funit.Int16
bbox funit.Rect16
f *Font
}
// Close most be called after the glyph description has been written.
func (g *GlyphBuilder) Close() error {
if g.Writer.Err != nil {
return g.Writer.Err
}
buf := g.Content.(*bytes.Buffer)
data := &Glyph{
WidthX: g.widthX,
BBox: g.bbox,
Data: buf.Bytes(),
}
g.f.Glyphs[g.name] = data
g.f.NumOpen--
return nil
}
func setGlyphGeometry(g *Glyph, data []byte) {
m := type3StartRegexp.FindSubmatch(data)
if len(m) != 9 {
return
}
if m[1] != nil {
x, _ := strconv.ParseFloat(string(m[1]), 64)
g.WidthX = funit.Int16(math.Round(x))
} else if m[3] != nil {
var xx [6]funit.Int16
for i := range xx {
x, _ := strconv.ParseFloat(string(m[3+i]), 64)
xx[i] = funit.Int16(math.Round(x))
}
g.WidthX = xx[0]
g.BBox = funit.Rect16{
LLx: xx[2],
LLy: xx[3],
URx: xx[4],
URy: xx[5],
}
}
}
var (
spc = `[\t\n\f\r ]+`
num = `([+-]?[0-9.]+)` + spc
d0 = num + num + "d0"
d1 = num + num + num + num + num + num + "d1"
type3StartRegexp = regexp.MustCompile(`^[\t\n\f\r ]*(?:` + d0 + "|" + d1 + ")" + spc)
)