...
1 package text
2
3 import "strings"
4
5
6 type VAlign int
7
8
9 const (
10 VAlignDefault VAlign = iota
11 VAlignTop
12 VAlignMiddle
13 VAlignBottom
14 )
15
16
17
18
19
20
21
22
23 func (va VAlign) Apply(lines []string, maxLines int) []string {
24 if len(lines) == maxLines {
25 return lines
26 } else if len(lines) > maxLines {
27 maxLines = len(lines)
28 }
29
30 insertIdx := 0
31 if va == VAlignMiddle {
32 insertIdx = int(maxLines-len(lines)) / 2
33 } else if va == VAlignBottom {
34 insertIdx = maxLines - len(lines)
35 }
36
37 linesOut := strings.Split(strings.Repeat("\n", maxLines-1), "\n")
38 for idx, line := range lines {
39 linesOut[idx+insertIdx] = line
40 }
41 return linesOut
42 }
43
44
45
46
47
48
49
50
51 func (va VAlign) ApplyStr(text string, maxLines int) []string {
52 return va.Apply(strings.Split(text, "\n"), maxLines)
53 }
54
55
56 func (va VAlign) HTMLProperty() string {
57 switch va {
58 case VAlignTop:
59 return "valign=\"top\""
60 case VAlignMiddle:
61 return "valign=\"middle\""
62 case VAlignBottom:
63 return "valign=\"bottom\""
64 default:
65 return ""
66 }
67 }
68
View as plain text