-
Notifications
You must be signed in to change notification settings - Fork 280
/
strhelper.go
55 lines (48 loc) · 1.29 KB
/
strhelper.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
package gopdf
import (
"math/big"
"strings"
)
// StrHelperGetStringWidth get string width
func StrHelperGetStringWidth(str string, fontSize int, ifont IFont) float64 {
return StrHelperGetStringWidthPrecise(str, float64(fontSize), ifont)
}
// StrHelperGetStringWidthPrecise get string width with real number fontSize
func StrHelperGetStringWidthPrecise(str string, fontSize float64, ifont IFont) float64 {
w := 0
bs := []byte(str)
i := 0
max := len(bs)
for i < max {
w += ifont.GetCw()[bs[i]]
i++
}
return float64(w) * (float64(fontSize) / 1000.0)
}
// CreateEmbeddedFontSubsetName create Embedded font (subset font) name
func CreateEmbeddedFontSubsetName(name string) string {
name = strings.Replace(name, " ", "+", -1)
name = strings.Replace(name, "/", "+", -1)
return name
}
// ReadShortFromByte read short from byte array
func ReadShortFromByte(data []byte, offset int) (int64, int) {
buff := data[offset : offset+2]
num := big.NewInt(0)
num.SetBytes(buff)
u := num.Uint64()
var v int64
if u >= 0x8000 {
v = int64(u) - 65536
} else {
v = int64(u)
}
return v, 2
}
// ReadUShortFromByte read ushort from byte array
func ReadUShortFromByte(data []byte, offset int) (uint64, int) {
buff := data[offset : offset+2]
num := big.NewInt(0)
num.SetBytes(buff)
return num.Uint64(), 2
}