-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtitle.go
168 lines (154 loc) · 4.28 KB
/
title.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
package charts
import (
"fmt"
"strings"
)
type TitleOption struct {
// Show specifies if the title should be rendered, set this to *false (through Ptr(false)) to hide the title.
Show *bool
// Theme specifies the colors used for the title.
Theme ColorPalette
// Text specifies the title text, supporting '\n' for new lines.
Text string
// Subtext to the title, supporting '\n' for new lines.
Subtext string
// Offset allows you to specify the position of the title component relative to the left and top side.
Offset OffsetStr
// FontStyle specifies the font, size, and style for rendering the title.
FontStyle FontStyle
// SubtextFontStyle specifies the font, size, and style for rendering the subtext.
SubtextFontStyle FontStyle
// BorderWidth can be set to a non-zero value to render a box around the title.
BorderWidth float64
}
type titleMeasureOption struct {
width int
height int
text string
style FontStyle
}
func splitTitleText(text string) []string {
arr := strings.Split(text, "\n")
result := make([]string, 0, len(arr))
for _, v := range arr {
v = strings.TrimSpace(v)
if v == "" {
continue
}
result = append(result, v)
}
return result
}
type titlePainter struct {
p *Painter
opt *TitleOption
}
// newTitlePainter returns a title renderer
func newTitlePainter(p *Painter, opt TitleOption) *titlePainter {
return &titlePainter{
p: p,
opt: &opt,
}
}
func (t *titlePainter) Render() (Box, error) {
opt := t.opt
p := t.p
if flagIs(false, opt.Show) {
return BoxZero, nil
} else if opt.Text == "" && opt.Subtext == "" {
return BoxZero, nil
}
theme := opt.Theme
if theme == nil {
theme = getPreferredTheme(p.theme)
}
fontStyle := fillFontStyleDefaults(opt.FontStyle, defaultFontSize, theme.GetTitleTextColor())
subtextFontStyle := fillFontStyleDefaults(opt.SubtextFontStyle,
fontStyle.FontSize, fontStyle.FontColor, fontStyle.Font)
mainSplit := splitTitleText(opt.Text)
subSplit := splitTitleText(opt.Subtext)
measureOptions := make([]titleMeasureOption, 0, len(mainSplit)+len(subSplit))
for _, v := range mainSplit {
measureOptions = append(measureOptions, titleMeasureOption{
text: v,
style: fontStyle,
})
}
for _, v := range subSplit {
measureOptions = append(measureOptions, titleMeasureOption{
text: v,
style: subtextFontStyle,
})
}
var textMaxWidth, textMaxHeight, textTotalHeight int
for index, item := range measureOptions {
textBox := p.MeasureText(item.text, 0, item.style)
w := textBox.Width()
h := textBox.Height()
if w > textMaxWidth {
textMaxWidth = w
}
if h > textMaxHeight {
textMaxHeight = h
}
textTotalHeight += h
measureOptions[index].height = h
measureOptions[index].width = w
}
width := textMaxWidth
offset := opt.Offset
var titleX int
switch offset.Left {
case "", PositionLeft:
// no-op
case PositionRight:
titleX = p.Width() - textMaxWidth
case PositionCenter:
titleX = p.Width()>>1 - (textMaxWidth >> 1)
default:
if v, err := parseFlexibleValue(offset.Left, float64(p.Width())); err != nil {
return BoxZero, fmt.Errorf("error parsing title position: %w", err)
} else {
titleX = int(v)
}
}
var titleY int
switch offset.Top {
case "", PositionTop:
// leave default of zero
case PositionBottom:
titleY = p.Height() - textTotalHeight
default:
if v, err := parseFlexibleValue(offset.Top, float64(p.Height())); err != nil {
return BoxZero, fmt.Errorf("error parsing title position: %w", err)
} else {
titleY = int(v)
}
}
startY := titleY
for _, item := range measureOptions {
x := titleX + (textMaxWidth-item.width)>>1
y := titleY + item.height
p.Text(item.text, x, y, 0, item.style)
titleY = y
}
result := Box{
Top: startY,
Bottom: titleY,
Left: titleX,
Right: titleX + width,
IsSet: true,
}
if opt.BorderWidth > 0 {
boxPad := 10 // built in adjustment for possible measure vs render variations
boxPoints := []Point{
{X: result.Left - boxPad, Y: result.Bottom + boxPad},
{X: result.Left - boxPad, Y: result.Top - boxPad},
{X: result.Left + result.Width() + boxPad, Y: result.Top - boxPad},
{X: result.Left + result.Width() + boxPad, Y: result.Bottom + boxPad},
{X: result.Left - boxPad, Y: result.Bottom + boxPad},
}
p.LineStroke(boxPoints, theme.GetTitleBorderColor(), opt.BorderWidth)
}
return result, nil
}