forked from signintech/pdft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl_result_font.go
86 lines (76 loc) · 1.65 KB
/
crawl_result_font.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
package pdft
import (
"bytes"
"fmt"
"strconv"
"strings"
"unicode"
)
type crawlResultFonts []crawlResultFont
// Split a font string like '/F1' or '/TT2' into the prefix and index
func splitFont(s string) (string, int, error) {
prefix := ""
for i, r := range s {
if unicode.IsDigit(r) {
s := strings.ReplaceAll(s, "_", "")
val, err := strconv.Atoi(s[i:])
if err != nil {
return "", 0, err
}
return prefix, val, nil
}
prefix += string(r)
}
return "", 0, fmt.Errorf("No font index")
}
func (c *crawlResultFonts) parse(propVal *[]byte) error {
var props PDFObjPropertiesData
err := readProperties(propVal, &props)
if err != nil {
return err
}
for _, prop := range props {
var crFont crawlResultFont
prefix, fontIndex, err := splitFont(strings.TrimSpace(prop.key))
if err != nil {
return err
}
objID, _, err := prop.asDictionary()
if err != nil {
return err
}
crFont.prefix = prefix
crFont.fontIndex = fontIndex
crFont.fontObjID = objID
*c = append(*c, crFont)
}
return nil
}
func (c *crawlResultFonts) String() string {
var buff bytes.Buffer
for _, f := range *c {
buff.WriteString(fmt.Sprintf("/%s%d %d 0 R\n", f.prefix, f.fontIndex, f.fontObjID))
}
return buff.String()
}
func (c *crawlResultFonts) append(fontIndex int, fontObjID int) {
var crFont crawlResultFont
crFont.fontIndex = fontIndex
crFont.fontObjID = fontObjID
crFont.prefix = "F"
*c = append(*c, crFont)
}
func (c *crawlResultFonts) maxFontIndex() int {
max := 0
for _, f := range *c {
if f.fontIndex > max {
max = f.fontIndex
}
}
return max
}
type crawlResultFont struct {
fontIndex int
fontObjID int
prefix string
}