-
Notifications
You must be signed in to change notification settings - Fork 8
/
text.go
153 lines (130 loc) · 3.4 KB
/
text.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
141
142
143
144
145
146
147
148
149
150
151
152
153
package rtfdoc
import (
"fmt"
"strings"
)
func (text Text) compose() string {
var res strings.Builder
var emphTextSlice []string
if text.isBold {
emphTextSlice = append(emphTextSlice, "\\b")
}
if text.isItalic {
emphTextSlice = append(emphTextSlice, "\\i")
}
if text.isScaps {
emphTextSlice = append(emphTextSlice, "\\scaps")
}
if text.isStrike {
emphTextSlice = append(emphTextSlice, "\\strike")
}
if text.isSub {
emphTextSlice = append(emphTextSlice, "\\sub")
}
if text.isSuper {
emphTextSlice = append(emphTextSlice, "\\super")
}
if text.isUnderlining {
emphTextSlice = append(emphTextSlice, "\\ul")
}
if text.rotated {
emphTextSlice = append(emphTextSlice, "\\horzvert0")
}
PreparedText := convertNonASCIIToUTF16(text.content)
// If there is no emphasis, remove it from format so that there is no additional space
// sb0 and sa0 remove leading and trailing spaces
if len(emphTextSlice) > 0 {
res.WriteString(fmt.Sprintf("\n\\sb0 \\sa0 \\fs%d\\f%d \\cf%d{%s %s}\\f0", text.fontSize*2, text.fontCode, text.colorCode, strings.Join(emphTextSlice, " "), PreparedText))
} else {
res.WriteString(fmt.Sprintf("\n\\sb0 \\sa0 \\fs%d\\f%d \\cf%d{%s}\\f0", text.fontSize*2, text.fontCode, text.colorCode, PreparedText))
}
return res.String()
}
// AddText returns new text instance
func (p *Paragraph) AddText(textStr string, fontSize int, fontCode string, colorCode string) *Text {
fn := 0
for i, f := range *p.generalSettings.fontColor {
if f.code == fontCode {
fn = i
}
}
fc := 0
for i, c := range *p.generalSettings.colorTable {
if c.name == colorCode {
fc = i + 1
}
}
txt := Text{
fontSize: fontSize,
fontCode: fn,
colorCode: fc,
content: textStr,
generalSettings: generalSettings{
colorTable: p.colorTable,
fontColor: p.fontColor,
},
}
p.content = append(p.content, &txt)
return &txt
}
//AddNewLine adds new line into Paragraph text
func (p *Paragraph) AddNewLine() *Paragraph {
txt := Text{
content: "\\line",
}
p.content = append(p.content, &txt)
return p
}
// SetBold function sets text to Bold
func (text *Text) SetBold() *Text {
text.isBold = true
return text
}
// SetItalic function sets text to Italic
func (text *Text) SetItalic() *Text {
text.isItalic = true
return text
}
// SetUnderlining function sets text to Underlining
func (text *Text) SetUnderlining() *Text {
text.isUnderlining = true
return text
}
// SetSuper function sets text to Super
func (text *Text) SetSuper() *Text {
text.isSuper = true
return text
}
// SetSub function sets text to Sub
func (text *Text) SetSub() *Text {
text.isSub = true
return text
}
// SetScaps function sets text to Scaps
func (text *Text) SetScaps() *Text {
text.isScaps = true
return text
}
// SetStrike function sets text to Strike
func (text *Text) SetStrike() *Text {
text.isStrike = true
return text
}
// SetRotate function rotates Text so it flows in a direction opposite to that of the main document (Horizontal in vertical and vertical in horizontal)
func (text *Text) SetRotate() *Text {
text.rotated = true
return text
}
func (text *Text) getEmphasis() string {
return text.emphasis
}
// SetColor sets text color
func (text *Text) SetColor(colorCode string) *Text {
for i := range *text.colorTable {
if (*text.colorTable)[i].name == colorCode {
// Присваиваем тексту порядковый номер шрифта
text.colorCode = i + 1
}
}
return text
}