forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicode_map.go
135 lines (115 loc) · 3.08 KB
/
unicode_map.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
package gopdf
import (
"fmt"
"io"
)
//UnicodeMap unicode map
type UnicodeMap struct {
PtrToSubsetFontObj *SubsetFontObj
//getRoot func() *GoPdf
pdfProtection *PDFProtection
}
func (u *UnicodeMap) init(funcGetRoot func() *GoPdf) {
//u.getRoot = funcGetRoot
}
func (u *UnicodeMap) setProtection(p *PDFProtection) {
u.pdfProtection = p
}
func (u *UnicodeMap) protection() *PDFProtection {
return u.pdfProtection
}
//SetPtrToSubsetFontObj set pointer to SubsetFontObj
func (u *UnicodeMap) SetPtrToSubsetFontObj(ptr *SubsetFontObj) {
u.PtrToSubsetFontObj = ptr
}
func (u *UnicodeMap) getType() string {
return "Unicode"
}
func (u *UnicodeMap) write(w io.Writer, objID int) error {
//stream
//characterToGlyphIndex := u.PtrToSubsetFontObj.CharacterToGlyphIndex
prefix :=
"/CIDInit /ProcSet findresource begin\n" +
"12 dict begin\n" +
"begincmap\n" +
"/CIDSystemInfo << /Registry (Adobe)/Ordering (UCS)/Supplement 0>> def\n" +
"/CMapName /Adobe-Identity-UCS def /CMapType 2 def\n"
suffix := "endcmap CMapName currentdict /CMap defineresource pop end end"
glyphIndexToCharacter := newMapGlyphIndexToCharacter() //make(map[int]rune)
lowIndex := 65536
hiIndex := -1
keys := u.PtrToSubsetFontObj.CharacterToGlyphIndex.AllKeys()
for _, k := range keys {
v, _ := u.PtrToSubsetFontObj.CharacterToGlyphIndex.Val(k)
index := int(v)
if index < lowIndex {
lowIndex = index
}
if index > hiIndex {
hiIndex = index
}
//glyphIndexToCharacter[index] = k
glyphIndexToCharacter.set(index, k)
}
buff := GetBuffer()
defer PutBuffer(buff)
buff.WriteString(prefix)
buff.WriteString("1 begincodespacerange\n")
fmt.Fprintf(buff, "<%04X><%04X>\n", lowIndex, hiIndex)
buff.WriteString("endcodespacerange\n")
fmt.Fprintf(buff, "%d beginbfrange\n", glyphIndexToCharacter.size())
indexs := glyphIndexToCharacter.allIndexs()
for _, k := range indexs {
v, _ := glyphIndexToCharacter.runeByIndex(k)
fmt.Fprintf(buff, "<%04X><%04X><%04X>\n", k, k, v)
}
buff.WriteString("endbfrange\n")
buff.WriteString(suffix)
buff.WriteString("\n")
io.WriteString(w, "<<\n")
fmt.Fprintf(w, "/Length %d\n", buff.Len())
io.WriteString(w, ">>\n")
io.WriteString(w, "stream\n")
if u.protection() != nil {
tmp, err := rc4Cip(u.protection().objectkey(objID), buff.Bytes())
if err != nil {
return err
}
w.Write(tmp)
//streambuff.WriteString("\n")
} else {
buff.WriteTo(w)
}
io.WriteString(w, "endstream\n")
return nil
}
type mapGlyphIndexToCharacter struct {
runes []rune
indexs []int
}
func newMapGlyphIndexToCharacter() *mapGlyphIndexToCharacter {
var m mapGlyphIndexToCharacter
return &m
}
func (m *mapGlyphIndexToCharacter) set(index int, r rune) {
m.runes = append(m.runes, r)
m.indexs = append(m.indexs, index)
}
func (m *mapGlyphIndexToCharacter) size() int {
return len(m.indexs)
}
func (m *mapGlyphIndexToCharacter) allIndexs() []int {
return m.indexs
}
func (m *mapGlyphIndexToCharacter) runeByIndex(index int) (rune, bool) {
var r rune
ok := false
for i, idx := range m.indexs {
if idx == index {
r = m.runes[i]
ok = true
break
}
}
return r, ok
}