Debian Codegraph @

text.go

raw symbols SCIP
1 package chart
2
3 import (
4 "strings"
5 )
6
7 // TextHorizontalAlign is an enum for the horizontal alignment options.
8 type TextHorizontalAlign int
9
10 const (
11 // TextHorizontalAlignUnset is the unset state for text horizontal alignment.
12 TextHorizontalAlignUnset TextHorizontalAlign = 0
13 // TextHorizontalAlignLeft aligns a string horizontally so that it's left ligature starts at horizontal pixel 0.
14 TextHorizontalAlignLeft TextHorizontalAlign = 1
15 // TextHorizontalAlignCenter left aligns a string horizontally so that there are equal pixels
16 // to the left and to the right of a string within a box.
17 TextHorizontalAlignCenter TextHorizontalAlign = 2
18 // TextHorizontalAlignRight right aligns a string horizontally so that the right ligature ends at the right-most pixel
19 // of a box.
20 TextHorizontalAlignRight TextHorizontalAlign = 3
21 )
22
23 // TextWrap is an enum for the word wrap options.
24 type TextWrap int
25
26 const (
27 // TextWrapUnset is the unset state for text wrap options.
28 TextWrapUnset TextWrap = 0
29 // TextWrapNone will spill text past horizontal boundaries.
30 TextWrapNone TextWrap = 1
31 // TextWrapWord will split a string on words (i.e. spaces) to fit within a horizontal boundary.
32 TextWrapWord TextWrap = 2
33 // TextWrapRune will split a string on a rune (i.e. utf-8 codepage) to fit within a horizontal boundary.
34 TextWrapRune TextWrap = 3
35 )
36
37 // TextVerticalAlign is an enum for the vertical alignment options.
38 type TextVerticalAlign int
39
40 const (
41 // TextVerticalAlignUnset is the unset state for vertical alignment options.
42 TextVerticalAlignUnset TextVerticalAlign = 0
43 // TextVerticalAlignBaseline aligns text according to the "baseline" of the string, or where a normal ascender begins.
44 TextVerticalAlignBaseline TextVerticalAlign = 1
45 // TextVerticalAlignBottom aligns the text according to the lowers pixel of any of the ligatures (ex. g or q both extend below the baseline).
46 TextVerticalAlignBottom TextVerticalAlign = 2
47 // TextVerticalAlignMiddle aligns the text so that there is an equal amount of space above and below the top and bottom of the ligatures.
48 TextVerticalAlignMiddle TextVerticalAlign = 3
49 // TextVerticalAlignMiddleBaseline aligns the text vertically so that there is an equal number of pixels above and below the baseline of the string.
50 TextVerticalAlignMiddleBaseline TextVerticalAlign = 4
51 // TextVerticalAlignTop alignts the text so that the top of the ligatures are at y-pixel 0 in the container.
52 TextVerticalAlignTop TextVerticalAlign = 5
53 )
54
55 var (
56 // Text contains utilities for text.
57 Text = &text{}
58 )
59
60 // TextStyle encapsulates text style options.
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') { // commit the line to output
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 }