-
Notifications
You must be signed in to change notification settings - Fork 0
/
runes.go
457 lines (418 loc) · 12.1 KB
/
runes.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// ntcharts - Copyright (c) 2024 Neomantra Corp.
// Package runes contains commonly used runes and functions to obtain runes.
package runes
// https://en.wikipedia.org/wiki/Box-drawing_character
// https://en.wikipedia.org/wiki/Braille_Patterns
const (
Null = '\u0000'
LineHorizontal = '\u2500' // ─
LineVertical = '\u2502' // │
LineDownRight = '\u250C' // ┌
LineDownLeft = '\u2510' // ┐
LineUpRight = '\u2514' // └
LineUpLeft = '\u2518' // ┘
LineVerticalRight = '\u251C' // ├
LineVerticalLeft = '\u2524' // ┤
LineHorizontalUp = '\u2534' // ┴
LineHorizontalDown = '\u252C' // ┬
LineHorizontalVertical = '\u253C' // ┼
LineLeft = '\u2574' // ╴
LineUp = '\u2575' // ╵
LineRight = '\u2576' // ╶
LineDown = '\u2577' // ╷
ArcDownRight = '\u256D' // ╭
ArcDownLeft = '\u256E' // ╮
ArcUpLeft = '\u256F' // ╯
ArcUpRight = '\u2570' // ╰
LowerBlockOne = '\u2581' // ▁
LowerBlockTwo = '\u2582' // ▂
LowerBlockThree = '\u2583' // ▃
LowerBlockFour = '\u2584' // ▄
LowerBlockFive = '\u2585' // ▅
LowerBlockSix = '\u2586' // ▆
LowerBlockSeven = '\u2587' // ▇
FullBlock = '\u2588' // █
LeftBlockSeven = '\u2589' // ▉
LeftBlockSix = '\u258A' // ▊
LeftBlockFive = '\u258B' // ▋
LeftBlockFour = '\u258C' // ▌
LeftBlockThree = '\u258D' // ▍
LeftBlockTwo = '\u258E' // ▎
LeftBlockOne = '\u258F' // ▏
)
/*
Braille dot number offsets
Unicode Braille Patterns can be computed by
adding hex values to the beginning block offset
[0][3] = [0x0001][0x0008]
[1][4] [0x0002][0x0010]
[2][5] [0x0004][0x0020]
[6][7] [0x0040][0x0080]
*/
const BrailleBlockOffset = 0x2800 // beginning of Unicode Braille Patterns (empty Braille Pattern)
var brailleDotNumberOffsets = [8]int32{0x0001, 0x0002, 0x0004, 0x0008, 0x00010, 0x0020, 0x0040, 0x0080}
// PatternDots indicates whether a dot in a Braille Pattern is displayed.
type PatternDots [8]bool
// PatternDotsGrid is a 2D array where each row and column indicates whether
// a dot in a sequence of Braille Patterns runes should be displayed.
// Example:
//
// width = 4, height = 4 will give 2 Braille Pattern runes
// [0][3][0][3]
// [1][4][1][4]
// [2][5][2][5]
// [6][7][6][7]
//
// setting (0,0) will set Dot 0 of first braille rune
// setting (0,3) will set Dot 6 of first braille rune
// setting (3,0) will set Dot 3 of second braille rune
// setting (3,3) will set Dot 7 of second braille rune
type PatternDotsGrid struct {
w int // grid width
h int // grid height
g [][]bool // each index indicates whether to display Braille Pattern dot
}
// NewPatternDotsGrid returns new initialized *PatternDotsGrid
func NewPatternDotsGrid(w, h int) *PatternDotsGrid {
g := PatternDotsGrid{
w: w,
h: h,
}
g.Reset()
return &g
}
// Reset will reset the internal grid
func (g *PatternDotsGrid) Reset() {
g.g = make([][]bool, g.h, g.h)
for i := range g.g {
g.g[i] = make([]bool, g.w)
}
}
// Set will set value in grid at given column and row
func (g *PatternDotsGrid) Set(x int, y int) {
if (x < 0) || (x >= g.w) || (y < 0) || (y >= g.h) {
return
}
g.g[y][x] = true
}
// Unset will unset value in grid at given column and row
func (g *PatternDotsGrid) Unset(x int, y int) {
if (x < 0) || (x >= g.w) || (y < 0) || (y >= g.h) {
return
}
g.g[y][x] = false
}
// BraillePatterns returns a [][]rune containing Braille Pattern
// runes based on internal grid values.
func (g *PatternDotsGrid) BraillePatterns() (p [][]rune) {
for y := 0; y < g.h; {
xb := []rune{}
for x := 0; x < g.w; {
xb = append(xb, g.getBraillePattern(x, y))
x += 2 // each braille pattern rune has a width of 2
}
p = append(p, xb)
y += 4 // each braille pattern rune has a height of 4
}
return
}
// getBraillePattern returns Braille Pattern rune
// starting at internal grid column and row.
func (g *PatternDotsGrid) getBraillePattern(x int, y int) (b rune) {
if (x < 0) || (x >= g.w) || (y < 0) || (y >= g.h) {
return
}
b = BrailleBlockOffset
// set left side of braille pattern
if g.g[y][x] {
b |= brailleDotNumberOffsets[0]
}
if (y+1 < g.h) && (g.g[y+1][x]) {
b |= brailleDotNumberOffsets[1]
}
if (y+2 < g.h) && (g.g[y+2][x]) {
b |= brailleDotNumberOffsets[2]
}
if (y+3 < g.h) && (g.g[y+3][x]) {
b |= brailleDotNumberOffsets[6]
}
// set right side of braille pattern
if (x+1 < g.w) && (g.g[y][x+1]) {
b |= brailleDotNumberOffsets[3]
}
if (y+1 < g.h) && (x+1 < g.w) && (g.g[y+1][x+1]) {
b |= brailleDotNumberOffsets[4]
}
if (y+2 < g.h) && (x+1 < g.w) && (g.g[y+2][x+1]) {
b |= brailleDotNumberOffsets[5]
}
if (y+3 < g.h) && (x+1 < g.w) && (g.g[y+3][x+1]) {
b |= brailleDotNumberOffsets[7]
}
return
}
// IsBraillePattern returns whether a given rune is
// considered a Braile Pattern rune.
func IsBraillePattern(r rune) bool {
if r >= 0x2800 && r <= 0x28FF {
return true
}
return false
}
// BraillePatternFromPatternDots returns a Braille Pattern rune using given PatternDots.
// Each index in PatternDots corresponds to Braille dot number
// and whether the dot should be displayed.
func BraillePatternFromPatternDots(p PatternDots) (r rune) {
r = BrailleBlockOffset
for i, b := range p {
if b {
r |= brailleDotNumberOffsets[i]
}
}
return
}
// SetPatternDots sets given PatternDots dots based on given rune.
func SetPatternDots(r rune, p *PatternDots) {
if !IsBraillePattern(r) {
return
}
for i, b := range brailleDotNumberOffsets {
if (b & r) != Null {
p[i] = true
}
}
return
}
// CombineBraillePatterns returns a rune
// that is a combination of two braille pattern runes.
// Any invalid braille pattern rune combinations will return r2.
func CombineBraillePatterns(r1 rune, r2 rune) rune {
if !IsBraillePattern(r1) || !IsBraillePattern(r2) {
return r2
}
return (r1 | r2)
}
var lowerBlockElements = [9]rune{
Null,
LowerBlockOne,
LowerBlockTwo,
LowerBlockThree,
LowerBlockFour,
LowerBlockFive,
LowerBlockSix,
LowerBlockSeven,
FullBlock,
}
// IsLowerBlockElement returns whether a given rune is
// considered a lower block or full block element.
func IsLowerBlockElement(r rune) bool {
if r >= 0x2581 && r <= 0x2588 {
return true
}
return false
}
// LowerBlockElementFromFloat64 returns either an empty rune
// or a lower Block Element rune using given float64.
// A float64 < 1.0 will return the nearest one eights lower block element
// corresponding to the float value. An empty rune will be returned if
// float64 does not round to lowest 1/8 lower block.
func LowerBlockElementFromFloat64(f float64) rune {
if f >= 1 {
return lowerBlockElements[8]
} else if f <= 0 {
return lowerBlockElements[0]
}
e := int(f / .125) // number of 1/8s blocks to show
// round remaining fraction smaller than 1/8 to nearest 1/16
if n := f - (float64(e) * .125); n >= 0.0625 {
e++
}
return lowerBlockElements[e]
}
var leftBlockElements = [9]rune{
Null,
LeftBlockOne,
LeftBlockTwo,
LeftBlockThree,
LeftBlockFour,
LeftBlockFive,
LeftBlockSix,
LeftBlockSeven,
FullBlock,
}
// IsLeftBlockElement returns whether a given rune is
// considered a left block or full block element.
func IsLeftBlockElement(r rune) bool {
if r >= 0x2588 && r <= 0x258F {
return true
}
return false
}
// LeftBlockElementFromFloat64 returns either an empty rune
// or a left Block Element rune using given float64.
// A float64 < 1.0 will return the nearest one eights left block element
// corresponding to the float value. An empty rune will be returned if
// float64 does not round to lowest 1/8 left block.
func LeftBlockElementFromFloat64(f float64) rune {
if f >= 1 {
return leftBlockElements[8]
} else if f <= 0 {
return leftBlockElements[0]
}
e := int(f / .125) // number of 1/8s blocks to show
// round remaining fraction smaller than 1/8 to nearest 1/16
if n := f - (float64(e) * .125); n >= 0.0625 {
e++
}
return leftBlockElements[e]
}
// LineStyle enumerates the different style of line runes to display.
type LineStyle int
const (
ThinLineStyle LineStyle = iota
ArcLineStyle
)
// LineSegments indicates whether a line segment
// going up, down, left, or right is displayed.
type LineSegments struct {
Up bool
Down bool
Left bool
Right bool
}
var arcLineSegmentsMap = map[LineSegments]rune{
{false, false, false, false}: Null,
{false, false, false, true}: LineRight,
{false, false, true, false}: LineLeft,
{false, false, true, true}: LineHorizontal,
{false, true, false, false}: LineDown,
{false, true, false, true}: ArcDownRight,
{false, true, true, false}: ArcDownLeft,
{false, true, true, true}: LineHorizontalDown,
{true, false, false, false}: LineUp,
{true, false, false, true}: ArcUpRight,
{true, false, true, false}: ArcUpLeft,
{true, false, true, true}: LineHorizontalUp,
{true, true, false, false}: LineVertical,
{true, true, false, true}: LineVerticalRight,
{true, true, true, false}: LineVerticalLeft,
{true, true, true, true}: LineHorizontalVertical,
}
var thinLineSegmentsMap = map[LineSegments]rune{
{false, false, false, false}: Null,
{false, false, false, true}: LineRight,
{false, false, true, false}: LineLeft,
{false, false, true, true}: LineHorizontal,
{false, true, false, false}: LineDown,
{false, true, false, true}: LineDownRight,
{false, true, true, false}: LineDownLeft,
{false, true, true, true}: LineHorizontalDown,
{true, false, false, false}: LineUp,
{true, false, false, true}: LineUpRight,
{true, false, true, false}: LineUpLeft,
{true, false, true, true}: LineHorizontalUp,
{true, true, false, false}: LineVertical,
{true, true, false, true}: LineVerticalRight,
{true, true, true, false}: LineVerticalLeft,
{true, true, true, true}: LineHorizontalVertical,
}
// SetLineSegments sets given LineSegments directions based on given rune.
func SetLineSegments(r rune, l *LineSegments) {
switch r {
case ArcDownRight, LineDownRight:
l.Down = true
l.Right = true
case ArcDownLeft, LineDownLeft:
l.Down = true
l.Left = true
case ArcUpLeft, LineUpLeft:
l.Up = true
l.Left = true
case ArcUpRight, LineUpRight:
l.Up = true
l.Right = true
case LineHorizontal:
l.Left = true
l.Right = true
case LineVertical:
l.Up = true
l.Down = true
case LineHorizontalUp:
l.Up = true
l.Left = true
l.Right = true
case LineHorizontalDown:
l.Down = true
l.Left = true
l.Right = true
case LineVerticalRight:
l.Up = true
l.Down = true
l.Right = true
case LineVerticalLeft:
l.Up = true
l.Down = true
l.Left = true
case LineHorizontalVertical:
l.Up = true
l.Down = true
l.Left = true
l.Right = true
case LineUp:
l.Up = true
case LineDown:
l.Down = true
case LineLeft:
l.Left = true
case LineRight:
l.Right = true
}
return
}
// IsLine returns whether a given rune is considered a line rune.
func IsLine(r rune) bool {
if (r >= 0x2500 && r <= 0x253C) || (r >= 0x256D && r <= 0x2570) || (r >= 0x2574 && r <= 0x2577) {
return true
}
return false
}
// ArcLineFromLineSegments returns either an empty rune
// or a line rune using given LineSegments.
// LineSegments contain whether or not the returned rune
// should display arc lines going up, down, left or right.
func ArcLineFromLineSegments(l LineSegments) rune {
return arcLineSegmentsMap[l]
}
// ThinLineFromLineSegments returns either an empty rune
// or a line rune using given LineSegments.
// LineSegments contain whether or not the returned rune
// should display thin lines going up, down, left or right.
func ThinLineFromLineSegments(l LineSegments) rune {
return thinLineSegmentsMap[l]
}
// CombineLines returns a rune that is a combination of two line runes.
// Any invalid line rune combinations or invalid LineStyle will return r2.
// The Linestyle determines the output line rune, even if
// the two input line runes are not of that style.
func CombineLines(r1 rune, r2 rune, ls LineStyle) (r rune) {
r = r2
r1ok := IsLine(r1)
r2ok := IsLine(r2)
if !r1ok && !r2ok {
return
}
var l LineSegments
if r1ok {
SetLineSegments(r1, &l)
}
if r2ok {
SetLineSegments(r2, &l)
}
switch ls {
case ThinLineStyle:
r = ThinLineFromLineSegments(l)
case ArcLineStyle:
r = ArcLineFromLineSegments(l)
}
return
}