| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
type TextHorizontalAlign int |
| 9 |
|
| 10 |
const ( |
| 11 |
|
| 12 |
TextHorizontalAlignUnset TextHorizontalAlign = 0 |
| 13 |
|
| 14 |
TextHorizontalAlignLeft TextHorizontalAlign = 1 |
| 15 |
|
| 16 |
|
| 17 |
TextHorizontalAlignCenter TextHorizontalAlign = 2 |
| 18 |
|
| 19 |
|
| 20 |
TextHorizontalAlignRight TextHorizontalAlign = 3 |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
type TextWrap int |
| 25 |
|
| 26 |
const ( |
| 27 |
|
| 28 |
TextWrapUnset TextWrap = 0 |
| 29 |
|
| 30 |
TextWrapNone TextWrap = 1 |
| 31 |
|
| 32 |
TextWrapWord TextWrap = 2 |
| 33 |
|
| 34 |
TextWrapRune TextWrap = 3 |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
type TextVerticalAlign int |
| 39 |
|
| 40 |
const ( |
| 41 |
|
| 42 |
TextVerticalAlignUnset TextVerticalAlign = 0 |
| 43 |
|
| 44 |
TextVerticalAlignBaseline TextVerticalAlign = 1 |
| 45 |
|
| 46 |
TextVerticalAlignBottom TextVerticalAlign = 2 |
| 47 |
|
| 48 |
TextVerticalAlignMiddle TextVerticalAlign = 3 |
| 49 |
|
| 50 |
TextVerticalAlignMiddleBaseline TextVerticalAlign = 4 |
| 51 |
|
| 52 |
TextVerticalAlignTop TextVerticalAlign = 5 |
| 53 |
) |
| 54 |
|
| 55 |
var ( |
| 56 |
|
| 57 |
Text = &text{} |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
type TextStyle struct { |
| 62 |
HorizontalAlign TextHorizontalAlign |
| 63 |
VerticalAlign TextVerticalAlign |
| 64 |
Wrap TextWrap |
| 65 |
} |
| 66 |
|
| 67 |
type text struct{} |
| 68 |
|
| 69 |
func (t text) WrapFit(r Renderer, value string, width int, style Style) []string { |
| 70 |
switch style.TextWrap { |
| 71 |
case TextWrapRune: |
| 72 |
return t.WrapFitRune(r, value, width, style) |
| 73 |
case TextWrapWord: |
| 74 |
return t.WrapFitWord(r, value, width, style) |
| 75 |
} |
| 76 |
return []string{value} |
| 77 |
} |
| 78 |
|
| 79 |
func (t text) WrapFitWord(r Renderer, value string, width int, style Style) []string { |
| 80 |
style.WriteToRenderer(r) |
| 81 |
|
| 82 |
var output []string |
| 83 |
var line string |
| 84 |
var word string |
| 85 |
|
| 86 |
var textBox Box |
| 87 |
|
| 88 |
for _, c := range value { |
| 89 |
if c == rune('\n') { |
| 90 |
output = append(output, t.Trim(line+word)) |
| 91 |
line = "" |
| 92 |
word = "" |
| 93 |
continue |
| 94 |
} |
| 95 |
|
| 96 |
textBox = r.MeasureText(line + word + string(c)) |
| 97 |
|
| 98 |
if textBox.Width() >= width { |
| 99 |
output = append(output, t.Trim(line)) |
| 100 |
line = word |
| 101 |
word = string(c) |
| 102 |
continue |
| 103 |
} |
| 104 |
|
| 105 |
if c == rune(' ') || c == rune('\t') { |
| 106 |
line = line + word + string(c) |
| 107 |
word = "" |
| 108 |
continue |
| 109 |
} |
| 110 |
word = word + string(c) |
| 111 |
} |
| 112 |
|
| 113 |
return append(output, t.Trim(line+word)) |
| 114 |
} |
| 115 |
|
| 116 |
func (t text) WrapFitRune(r Renderer, value string, width int, style Style) []string { |
| 117 |
style.WriteToRenderer(r) |
| 118 |
|
| 119 |
var output []string |
| 120 |
var line string |
| 121 |
var textBox Box |
| 122 |
for _, c := range value { |
| 123 |
if c == rune('\n') { |
| 124 |
output = append(output, line) |
| 125 |
line = "" |
| 126 |
continue |
| 127 |
} |
| 128 |
|
| 129 |
textBox = r.MeasureText(line + string(c)) |
| 130 |
|
| 131 |
if textBox.Width() >= width { |
| 132 |
output = append(output, line) |
| 133 |
line = string(c) |
| 134 |
continue |
| 135 |
} |
| 136 |
line = line + string(c) |
| 137 |
} |
| 138 |
return t.appendLast(output, line) |
| 139 |
} |
| 140 |
|
| 141 |
func (t text) Trim(value string) string { |
| 142 |
return strings.Trim(value, " \t\n\r") |
| 143 |
} |
| 144 |
|
| 145 |
func (t text) MeasureLines(r Renderer, lines []string, style Style) Box { |
| 146 |
style.WriteTextOptionsToRenderer(r) |
| 147 |
var output Box |
| 148 |
for index, line := range lines { |
| 149 |
lineBox := r.MeasureText(line) |
| 150 |
output.Right = MaxInt(lineBox.Right, output.Right) |
| 151 |
output.Bottom += lineBox.Height() |
| 152 |
if index < len(lines)-1 { |
| 153 |
output.Bottom += +style.GetTextLineSpacing() |
| 154 |
} |
| 155 |
} |
| 156 |
return output |
| 157 |
} |
| 158 |
|
| 159 |
func (t text) appendLast(lines []string, text string) []string { |
| 160 |
if len(lines) == 0 { |
| 161 |
return []string{text} |
| 162 |
} |
| 163 |
lastLine := lines[len(lines)-1] |
| 164 |
lines[len(lines)-1] = lastLine + text |
| 165 |
return lines |
| 166 |
} |