/
runproperties.go
238 lines (209 loc) · 5.77 KB
/
runproperties.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package document
import (
"github.com/danangsigit/gooxml"
"github.com/danangsigit/gooxml/color"
"github.com/danangsigit/gooxml/measurement"
"github.com/danangsigit/gooxml/schema/soo/ofc/sharedTypes"
"github.com/danangsigit/gooxml/schema/soo/wml"
)
// RunProperties controls run styling properties
type RunProperties struct {
x *wml.CT_RPr
}
// X returns the inner wrapped XML type.
func (r RunProperties) X() *wml.CT_RPr {
return r.x
}
// SetStyle sets the font size.
func (r RunProperties) SetStyle(style string) {
if style == "" {
r.x.RStyle = nil
} else {
r.x.RStyle = wml.NewCT_String()
r.x.RStyle.ValAttr = style
}
}
// SetFontFamily sets the Ascii & HAnsi fonly family for a run.
func (r RunProperties) SetFontFamily(family string) {
if r.x.RFonts == nil {
r.x.RFonts = wml.NewCT_Fonts()
}
r.x.RFonts.AsciiAttr = gooxml.String(family)
r.x.RFonts.HAnsiAttr = gooxml.String(family)
}
// SetSize sets the font size for a run.
func (r RunProperties) SetSize(size measurement.Distance) {
r.x.Sz = wml.NewCT_HpsMeasure()
r.x.Sz.ValAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(size / measurement.HalfPoint))
r.x.SzCs = wml.NewCT_HpsMeasure()
r.x.SzCs.ValAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(size / measurement.HalfPoint))
}
// SetKerning sets the run's font kerning.
func (r RunProperties) SetKerning(size measurement.Distance) {
r.x.Kern = wml.NewCT_HpsMeasure()
r.x.Kern.ValAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(size / measurement.HalfPoint))
}
// SetCharacterSpacing sets the run's Character Spacing Adjustment.
func (r RunProperties) SetCharacterSpacing(size measurement.Distance) {
r.x.Spacing = wml.NewCT_SignedTwipsMeasure()
r.x.Spacing.ValAttr.Int64 = gooxml.Int64(int64(size / measurement.Twips))
}
// Fonts returns the style's Fonts.
func (r RunProperties) Fonts() Fonts {
if r.x.RFonts == nil {
r.x.RFonts = wml.NewCT_Fonts()
}
return Fonts{r.x.RFonts}
}
// Color returns the style's Color.
func (r RunProperties) Color() Color {
if r.x.Color == nil {
r.x.Color = wml.NewCT_Color()
}
return Color{r.x.Color}
}
// SetUnderline controls underline for a run style.
func (r RunProperties) SetUnderline(style wml.ST_Underline, c color.Color) {
if style == wml.ST_UnderlineUnset {
r.x.U = nil
} else {
r.x.U = wml.NewCT_Underline()
r.x.U.ColorAttr = &wml.ST_HexColor{}
r.x.U.ColorAttr.ST_HexColorRGB = c.AsRGBString()
r.x.U.ValAttr = style
}
}
// IsBold returns true if the run has been set to bold.
func (r RunProperties) IsBold() bool {
return r.x.B != nil
}
// SetBold sets the run to bold.
func (r RunProperties) SetBold(b bool) {
if !b {
r.x.B = nil
r.x.BCs = nil
} else {
r.x.B = wml.NewCT_OnOff()
r.x.BCs = wml.NewCT_OnOff()
}
}
// IsItalic returns true if the run was set to bold.
func (r RunProperties) IsItalic() bool {
if r.x == nil {
return false
}
return r.x.I != nil
}
// SetItalic sets the run to italic.
func (r RunProperties) SetItalic(b bool) {
if !b {
r.x.I = nil
r.x.ICs = nil
} else {
r.x.I = wml.NewCT_OnOff()
r.x.ICs = wml.NewCT_OnOff()
}
}
// SetAllCaps sets the run to all caps.
func (r RunProperties) SetAllCaps(b bool) {
if !b {
r.x.Caps = nil
} else {
r.x.Caps = wml.NewCT_OnOff()
}
}
// SetSmallCaps sets the run to small caps.
func (r RunProperties) SetSmallCaps(b bool) {
if !b {
r.x.SmallCaps = nil
} else {
r.x.SmallCaps = wml.NewCT_OnOff()
}
}
// SetStrikeThrough sets the run to strike-through.
func (r RunProperties) SetStrikeThrough(b bool) {
if !b {
r.x.Strike = nil
} else {
r.x.Strike = wml.NewCT_OnOff()
}
}
// SetDoubleStrikeThrough sets the run to double strike-through.
func (r RunProperties) SetDoubleStrikeThrough(b bool) {
if !b {
r.x.Dstrike = nil
} else {
r.x.Dstrike = wml.NewCT_OnOff()
}
}
// SetOutline sets the run to outlined text.
func (r RunProperties) SetOutline(b bool) {
if !b {
r.x.Outline = nil
} else {
r.x.Outline = wml.NewCT_OnOff()
}
}
// SetShadow sets the run to shadowed text.
func (r RunProperties) SetShadow(b bool) {
if !b {
r.x.Shadow = nil
} else {
r.x.Shadow = wml.NewCT_OnOff()
}
}
// SetEmboss sets the run to embossed text.
func (r RunProperties) SetEmboss(b bool) {
if !b {
r.x.Emboss = nil
} else {
r.x.Emboss = wml.NewCT_OnOff()
}
}
// SetImprint sets the run to imprinted text.
func (r RunProperties) SetImprint(b bool) {
if !b {
r.x.Imprint = nil
} else {
r.x.Imprint = wml.NewCT_OnOff()
}
}
// ClearColor clears the text color.
func (r RunProperties) ClearColor() {
r.x.Color = nil
}
// SetColor sets the text color.
func (r RunProperties) SetColor(c color.Color) {
r.x.Color = wml.NewCT_Color()
r.x.Color.ValAttr.ST_HexColorRGB = c.AsRGBString()
}
// SetHighlight highlights text in a specified color.
func (r RunProperties) SetHighlight(c wml.ST_HighlightColor) {
r.x.Highlight = wml.NewCT_Highlight()
r.x.Highlight.ValAttr = c
}
// SetEffect sets a text effect on the run.
func (r RunProperties) SetEffect(e wml.ST_TextEffect) {
if e == wml.ST_TextEffectUnset {
r.x.Effect = nil
} else {
r.x.Effect = wml.NewCT_TextEffect()
r.x.Effect.ValAttr = wml.ST_TextEffectShimmer
}
}
// SetVerticalAlignment controls the vertical alignment of the run, this is used
// to control if text is superscript/subscript.
func (r RunProperties) SetVerticalAlignment(v sharedTypes.ST_VerticalAlignRun) {
if v == sharedTypes.ST_VerticalAlignRunUnset {
r.x.VertAlign = nil
} else {
r.x.VertAlign = wml.NewCT_VerticalAlignRun()
r.x.VertAlign.ValAttr = v
}
}