/
text.go
274 lines (236 loc) · 7.25 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) 2021, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vector
import (
"cogentcore.org/core/core"
"cogentcore.org/core/styles"
"cogentcore.org/core/svg"
"cogentcore.org/core/units"
"cogentcore.org/core/views"
)
// TextStyle is text styling info -- using StructView to do text editor
type TextStyle struct {
// current text to edit
Text string
// font family
Font core.FontName `xml:"font-family"`
// font size
Size units.Value `xml:"font-size"`
// prop: font-style = style -- normal, italic, etc
Style styles.FontStyles `xml:"font-style" inherit:"true"`
// prop: font-weight = weight: normal, bold, etc
Weight styles.FontWeights `xml:"font-weight" inherit:"true"`
// prop: font-stretch = font stretch / condense options
Stretch styles.FontStretch `xml:"font-stretch" inherit:"true"`
// prop: font-variant = normal or small caps
Variant styles.FontVariants `xml:"font-variant" inherit:"true"`
// prop: text-decoration = underline, line-through, etc -- not inherited
Deco styles.TextDecorations `xml:"text-decoration"`
// prop: baseline-shift = super / sub script -- not inherited
Shift styles.BaselineShifts `xml:"baseline-shift"`
// prop: text-align (inherited) = how to align text, horizontally. This *only* applies to the text within its containing element, and is typically relevant only for multi-line text: for single-line text, if element does not have a specified size that is different from the text size, then this has *no effect*.
Align styles.Aligns `xml:"text-align" inherit:"true"`
// font value view for font toolbar
FontValue views.FontValue `view:"-"`
// the parent vectorview
VectorView *VectorView `copier:"-" json:"-" xml:"-" view:"-"`
}
func (ts *TextStyle) Update() {
// this is called augtomatically when edited
if ts.VectorView != nil {
ts.VectorView.SetTextProperties(ts.TextProperties())
ts.VectorView.SetText(ts.Text)
}
}
func (ts *TextStyle) Defaults() {
ts.Text = ""
ts.Font = "Arial"
ts.Size.Dp(16)
ts.Style = 0
ts.Weight = 0
ts.Stretch = 0
ts.Variant = 0
ts.Deco = 0
ts.Shift = 0
ts.Align = 0
// ts.SetFromFontStyle(&Settings.TextStyle.FontStyle)
}
// SetFromFontStyle sets from standard styles.Font style
func (ts *TextStyle) SetFromFontStyle(fs *styles.Font) {
ts.Font = core.FontName(fs.Family)
ts.Size = fs.Size
ts.Weight = fs.Weight
ts.Stretch = fs.Stretch
ts.Variant = fs.Variant
ts.Deco = fs.Decoration
ts.Shift = fs.Shift
}
// SetFromNode sets text style info from given svg.Text node
func (ts *TextStyle) SetFromNode(txt *svg.Text) {
ts.Defaults() // always start fresh
if txt.Text == "" && txt.HasChildren() { // todo: multi-line text..
tspan := txt.Kids[0].(*svg.Text)
ts.Text = tspan.Text
}
// ts.SetFromFontStyle(&txt.Paint.FontStyle)
ts.Align = txt.Paint.TextStyle.Align
}
// SetTextPropertiesNode sets the text properties of given Text node
func (gv *VectorView) SetTextPropertiesNode(sii svg.Node, tps map[string]string) {
if gp, isgp := sii.(*svg.Group); isgp {
for _, kid := range gp.Kids {
gv.SetTextPropertiesNode(kid.(svg.Node), tps)
}
return
}
_, istxt := sii.(*svg.Text)
if !istxt {
return
}
g := sii.AsNodeBase()
for k, v := range tps {
if v == "" {
g.DeleteProperty(k)
} else {
g.SetProperty(k, v)
}
}
}
// SetTextProperties sets the text properties of selected Text nodes
func (gv *VectorView) SetTextProperties(tps map[string]string) {
es := &gv.EditState
sv := gv.SVG()
sv.UndoSave("SetTextProperties", "")
// sv.SetFullReRender()
for itm := range es.Selected {
gv.SetTextPropertiesNode(itm.(svg.Node), tps)
}
sv.NeedsRender()
gv.ChangeMade()
}
// TextProperties returns non-default text properties to set
func (ts *TextStyle) TextProperties() map[string]string {
tps := make(map[string]string)
tps["font-family"] = string(ts.Font)
tps["font-size"] = ts.Size.String()
if int(ts.Weight) != 0 {
tps["font-weight"] = ts.Weight.String()
} else {
tps["font-weight"] = ""
}
if int(ts.Stretch) != 0 {
tps["font-stretch"] = ts.Stretch.String()
} else {
tps["font-stretch"] = ""
}
if int(ts.Variant) != 0 {
tps["font-variant"] = ts.Variant.String()
} else {
tps["font-variant"] = ""
}
if int(ts.Deco) != 0 {
tps["text-decoration"] = ts.Deco.String()
} else {
tps["text-decoration"] = ""
}
if int(ts.Shift) != 0 {
tps["baseline-shift"] = ts.Shift.String()
} else {
tps["baseline-shift"] = ""
}
tps["text-align"] = ts.Align.String()
return tps
}
// SetTextNode sets the text of given Text node
func (gv *VectorView) SetTextNode(sii svg.Node, txt string) bool {
if sii.HasChildren() {
for _, kid := range *sii.Children() {
if gv.SetTextNode(kid.(svg.Node), txt) {
return true
}
}
return false
}
tn, istxt := sii.(*svg.Text)
if !istxt {
return false
}
tn.Text = txt // todo: actually need to deal with multi-line here..
return true
}
// SetText sets the text of selected Text node
func (gv *VectorView) SetText(txt string) {
es := &gv.EditState
if len(es.Selected) != 1 { // only if exactly one selected
return
}
sv := gv.SVG()
sv.UndoSave("SetText", "")
// sv.SetFullReRender()
for itm := range es.Selected {
if gv.SetTextNode(itm.(svg.Node), txt) {
break // only set first..
}
}
sv.UpdateView(true) // needs full update
gv.ChangeMade()
}
///////////////////////////////////////////////////////////////////////
// Toolbar
func (gv *VectorView) TextToolbar() *core.Toolbar {
tbs := gv.ModalToolbarStack()
tb := tbs.ChildByName("text-tb", 2).(*core.Toolbar)
return tb
}
// ConfigTextToolbar configures the text modal toolbar
func (gv *VectorView) ConfigTextToolbar() {
tb := gv.TextToolbar()
if tb.HasChildren() {
return
}
es := &gv.EditState
ts := &es.Text
ts.VectorView = gv
txt := core.NewTextField(tb, "text")
txt.Tooltip = "current text string"
txt.SetText(ts.Text)
// txt.SetProp("width", units.NewCh(50))
// txt.TextFieldSig.Connect(gv.This(), func(recv, send tree.Node, sig int64, data any) {
// if sig == int64(core.TextFieldDone) {
// ts.Text = txt.Text()
// ts.Update()
// }
// })
// tree.InitNode(&ts.FontVal)
// ts.FontVal.SetSoloValue(reflect.ValueOf(&ts.Font))
// fw := tb.AddNewChild(ts.FontVal.WidgetType(), "font").(core.Node2D)
// ts.FontVal.Config(fw)
// fsz := core.NewSpinner(tb, "size")
// fsz.SetValue(ts.Size.Val)
// fsz.SpinnerSig.Connect(gv.This(), func(recv, send tree.Node, sig int64, data any) {
// ts.Size.Val = fsz.Value
// ts.Update()
// })
// fzu := core.NewChooser(tb, "size-units")
// fzu.ItemsFromEnum(units.KiT_Units, true, 0)
// fzu.SetCurIndex(int(ts.Size.Un))
// fzu.ComboSig.Connect(gv.This(), func(recv, send tree.Node, sig int64, data any) {
// ts.Size.Un = units.Units(fzu.CurIndex)
// ts.Update()
// })
}
// UpdateTextToolbar updates the select toolbar based on current selection
func (gv *VectorView) UpdateTextToolbar() {
tb := gv.TextToolbar()
es := &gv.EditState
ts := &es.Text
txt := tb.ChildByName("text", 0).(*core.TextField)
txt.SetText(ts.Text)
// fw := tb.ChildByName("font", 0).(core.Node2D)
// ts.FontVal.UpdateWidget()
// fsz := tb.ChildByName("size", 0).(*core.Spinner)
// fsz.SetValue(ts.Size.Val)
// fzu := tb.ChildByName("size-units", 0).(*core.Chooser)
// fzu.SetCurrentIndex(int(ts.Size.Un))
}