-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtitle.go
174 lines (160 loc) · 3.92 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
169
170
171
172
173
174
package charts
import (
"fmt"
"strings"
)
type TitleOption struct {
// Show specifies if the title should be rendered, set this to *false (through 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
}
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)
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
}
theme := opt.Theme
if theme == nil {
theme = getPreferredTheme(p.theme)
}
if opt.Text == "" && opt.Subtext == "" {
return BoxZero, nil
}
measureOptions := make([]titleMeasureOption, 0)
fontStyle := opt.FontStyle
if fontStyle.Font == nil {
fontStyle.Font = GetDefaultFont()
}
if fontStyle.FontColor.IsZero() {
fontStyle.FontColor = theme.GetTextColor()
}
if fontStyle.FontSize == 0 {
fontStyle.FontSize = defaultFontSize
}
subtextFontStyle := opt.SubtextFontStyle
if subtextFontStyle.Font == nil {
subtextFontStyle.Font = fontStyle.Font
}
if subtextFontStyle.FontColor.IsZero() {
subtextFontStyle.FontColor = fontStyle.FontColor
}
if subtextFontStyle.FontSize == 0 {
subtextFontStyle.FontSize = fontStyle.FontSize
}
// main title
for _, v := range splitTitleText(opt.Text) {
measureOptions = append(measureOptions, titleMeasureOption{
text: v,
style: fontStyle,
})
}
// subtitle
for _, v := range splitTitleText(opt.Subtext) {
measureOptions = append(measureOptions, titleMeasureOption{
text: v,
style: subtextFontStyle,
})
}
textMaxWidth := 0
textMaxHeight := 0
textTotalHeight := 0
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
titleX := 0
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)
}
}
titleY := 0
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
}
return Box{
Top: startY,
Bottom: titleY,
Left: titleX,
Right: titleX + width,
IsSet: true,
}, nil
}