123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- package main
- import (
- "bytes"
- "fmt"
- "strconv"
- "strings"
- )
- type BlockFont struct {
- characters []int
- data [][][]byte
- }
- /*
- func BytesHexed(output []byte) {
- fmt.Printf("BH: ")
- for _, ch := range output {
- fmt.Printf("%02x ", ch)
- }
- fmt.Println("")
- }
- func BytesArrayHexed(output *[][]byte) {
- for _, out := range *output {
- BytesHexed(out)
- }
- }
- */
- // Make []strings all the same length
- func normalizeBlock(block *[][]byte) {
- var max int
- // StringO(block)
- for _, line := range *block {
- l := len(line)
- if max < l {
- max = l
- }
- }
- // fmt.Printf("max = %d\n", max)
- for idx, _ := range *block {
- // l := len(line)
- for len((*block)[idx]) < max {
- //(*block)[idx] += byte(0x20) //append((*block)[idx], byte{0x20})
- (*block)[idx] = append((*block)[idx], byte(0x20))
- }
- }
- // StringO(block)
- // fmt.Println("== normalized ==")
- }
- // Get Character information from BlockFont return Normalize
- func (bf *BlockFont) GetCharacter(c int) [][]byte {
- var result [][]byte
- if c == 32 {
- // Space character
- result = append(result, []byte{0x20, 0x20})
- return result
- }
- // 33-126 are the possible characters.
- if c >= 33 && c <= 126 {
- c -= 33
- idx := bf.characters[c]
- if idx == -1 {
- // This character is not supported by this font.
- return result
- }
- result = bf.data[bf.characters[c]]
- // fmt.Println("normalizeCharBlock")
- normalizeBlock(&result)
- // fmt.Println("normalizeChar done")
- return result
- } else {
- return result
- }
- }
- // expandBlock the number of strings, and make it match the length of [0].
- func __expandBlock(block *[]string) {
- // l := len((*block)[0])
- l := len([]rune((*block)[0]))
- *block = append(*block, strings.Repeat(" ", l))
- }
- func expandBlock(block *[][]byte) {
- // l := len((*block)[0])
- l := len((*block)[0])
- blank := []byte{0x20}
- *block = append(*block, bytes.Repeat(blank, l))
- }
- // Given text, translate to thedraw font.
- func (bf *BlockFont) Output(input string) ([][]byte, int) {
- var out [][]byte
- var max int
- for _, ch := range input {
- // fmt.Printf("%d %x\n", ch, ch)
- block := bf.GetCharacter(int(ch))
- // fmt.Println("ok")
- l := len(block)
- max += l
- if l != 0 {
- if len(out) == 0 {
- // First time
- for _, b := range block {
- out = append(out, b)
- }
- } else {
- if len(out) != 0 {
- for l > len(out) {
- // We need to expand the out
- // fmt.Println("expandBlock")
- expandBlock(&out)
- }
- }
- // fmt.Println("Append block to out")
- // Ok, we have something!
- for idx, b := range block {
- out[idx] = append(out[idx], byte(0x20))
- // out[idx] = append(out[idx], b)
- for _, inner := range b {
- out[idx] = append(out[idx], inner)
- }
- // out[idx] += byte(0x20) + b
- // fmt.Printf("%s\n", CP437_to_Unicode(b))
- }
- }
- }
- // fmt.Println("normalizeBlock")
- normalizeBlock(&out)
- // fmt.Println("normalizeBlock done")
- }
- return out, len(out[0])
- }
- type ColorFont struct {
- characters []int
- data [][][]byte
- }
- // Make []strings all the same length
- func ___normalizeColor(block *[]string) int {
- var max int
- for _, line := range *block {
- l := len([]rune(line))
- if max < l {
- max = l
- }
- }
- for idx, line := range *block {
- l := len([]rune(line))
- if l < max {
- (*block)[idx] += strings.Repeat(" \x0f", max-l/2)
- }
- }
- return max
- }
- func normalizeColor(block *[][]byte) int {
- var max int
- for _, line := range *block {
- l := len(line)
- if max < l {
- max = l
- }
- }
- for idx, _ := range *block {
- // l := len(line)
- blank := []byte{0x20, 0x0f}
- for len((*block)[idx]) < max {
- (*block)[idx] = append((*block)[idx], blank...)
- /*
- for _, b := range blank {
- (*block)[idx] = append((*block)[idx], b)
- }
- */
- // (*block)[idx] += strings.Repeat(" \x0f", max-l/2)
- }
- }
- return max
- }
- func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
- var result [][]byte
- if c == 32 {
- blank := []byte{0x20, 0x0f, 0x20, 0x0f}
- result = append(result, blank) // " \x0f \x0f")
- return result, len(result[0]) / 2
- }
- if c >= 33 && c <= 126 {
- c -= 33
- idx := cf.characters[c]
- if idx == -1 {
- return result, 0
- }
- result = cf.data[cf.characters[c]]
- // fmt.Println("Character:")
- // BytesArrayHexed(&result)
- // fmt.Println("normalizing...")
- max := normalizeColor(&result)
- // BytesArrayHexed(&result)
- // StringHexO(&result)
- return result, max
- } else {
- return result, 0
- }
- }
- /*
- func expandColor(block *[]string) {
- l := len((*block)[0])
- *block = append(*block, strings.Repeat(" \x0f", l/2))
- }
- */
- func thedraw_to_ansi(c int) int {
- trans := []int{0, 4, 2, 6, 1, 5, 3, 7}
- return trans[c]
- }
- func colorout(c int) string {
- bg := thedraw_to_ansi(c / 16)
- fg := c % 16
- bold := (fg & 0x8) == 0x8
- fg = thedraw_to_ansi(fg & 0x7)
- if bold {
- return fmt.Sprintf("\x1b[0;1;%d;%dm", fg+30, bg+40)
- } else {
- return fmt.Sprintf("\x1b[0;%d;%dm", fg+30, bg+40)
- }
- }
- type ColorParts struct {
- Background int
- Foreground int
- Bold bool
- }
- func ColorSplit(color int) ColorParts {
- return ColorParts{Background: thedraw_to_ansi(color / 16), Foreground: thedraw_to_ansi(color & 7), Bold: (color%16)&0x08 == 0x08}
- }
- func ColorOutput(previous int, color int) string {
- prev := ColorSplit(previous)
- c := ColorSplit(color)
- var codes []int8
- if c.Bold {
- if !prev.Bold {
- codes = append(codes, 1)
- }
- } else {
- if prev.Bold {
- // bold was set previously, there's only one way to reset it
- codes = append(codes, 0)
- prev.Background = 0
- prev.Foreground = 7
- }
- }
- if prev.Background == 0 && prev.Foreground == 0 {
- // output everything
- codes = append(codes, int8(c.Foreground)+30)
- codes = append(codes, int8(c.Background)+40)
- } else {
- if c.Foreground != prev.Foreground {
- codes = append(codes, int8(c.Foreground)+30)
- }
- if c.Background != prev.Background {
- codes = append(codes, int8(c.Background)+40)
- }
- }
- if len(codes) == 0 {
- // Everything matched
- return ""
- }
- var text []string
- for _, code := range codes {
- text = append(text, strconv.Itoa(int(code)))
- }
- return "\x1b[" + strings.Join(text, ";") + "m"
- }
- func Colorize(input []byte) []byte {
- var result []byte
- // runes := []rune(input)
- var previous int
- // BytesHexed(input)
- for pos := 0; pos < len(input); pos += 2 {
- ch := input[pos]
- color := int(input[pos+1])
- // fmt.Printf("%d : CH %d / %x, Color %d / %x\n", pos, ch, ch, color, color)
- colorstring := ColorOutput(previous, color)
- for _, c := range []byte(colorstring) {
- result = append(result, c)
- }
- // result = append(result, []byte(colorstring))
- // result += []byte(ColorOutput(previous, color))
- // result += ColorOutput(previous, color) + string(ch)
- result = append(result, ch)
- // result += string(ch)
- previous = color
- // result += colorout(color) + string(ch)
- }
- return result
- }
- func __expandColor(block *[]string, need int) {
- *block = append(*block, strings.Repeat(" \x0f", need))
- }
- func expandColor(block *[][]byte, need int) {
- blank := []byte{0x20, 0x0f}
- // *block = append(*block, strings.Repeat(" \x0f", need))
- *block = append(*block, bytes.Repeat(blank, need))
- }
- func (bf *ColorFont) Output(input string) ([][]byte, int) {
- var out [][]byte
- var max int
- for _, ch := range input {
- // fmt.Printf("%d %x\n", ch, ch)
- block, blklen := bf.GetCharacter(int(ch))
- if blklen == 0 {
- continue
- }
- l := len(block)
- max += blklen
- if l != 0 {
- if len(out) == 0 {
- // First time
- for _, b := range block {
- out = append(out, b)
- }
- } else {
- if len(out) != 0 {
- for l > len(out) {
- // We need to expand the out
- expandColor(&out, len(out[0])/2)
- // ExpandColor(&out)
- }
- }
- for len(out) > len(block) {
- // We need to expand the block out
- expandColor(&block, len(block)/2)
- }
- // Normalizing the character blocks
- normalizeColor(&block)
- if len(out) != len(block) {
- panic(fmt.Sprintf("len(out) %d != len(block) %d", len(out), len(block)))
- }
- // Ok, we have something!
- for idx, b := range block {
- // blank := []byte{0x20, 0x0f}
- out[idx] = append(out[idx], byte(0x20))
- out[idx] = append(out[idx], byte(0x0f))
- for _, inner := range b {
- out[idx] = append(out[idx], inner)
- }
- //out[idx] += " \x0f" + b
- // fmt.Printf("%s\n", CP437_to_Unicode(b))
- }
- }
- // NormalizeColor(&out)
- }
- }
- if len(out) == 0 {
- return out, 0
- }
- // StringHexO(&out)
- max = len(out[0]) / 2
- for idx, _ := range out {
- out[idx] = Colorize(out[idx])
- }
- return out, max
- }
- func CP437Bytes_to_Unicode(cp437 []byte) string {
- var result string
- for _, char := range cp437 {
- // fmt.Printf("%d, %x ", char, char)
- switch int(char) {
- case 0x01:
- result += "\u263A"
- case 0x02:
- result += "\u263B"
- case 0x03:
- result += "\u2665"
- case 0x04:
- result += "\u2666"
- case 0x05:
- result += "\u2663"
- case 0x06:
- result += "\u2660"
- case 0x09:
- result += "\u25CB"
- case 0x0b:
- result += "\u2642"
- case 0x0c:
- result += "\u2640"
- case 0x0e:
- result += "\u266B"
- case 0x0f:
- result += "\u263C"
- case 0x10:
- result += "\u25BA"
- case 0x11:
- result += "\u25C4"
- case 0x12:
- result += "\u2195"
- case 0x13:
- result += "\u203C"
- case 0x14:
- result += "\xc2\xb6"
- case 0x15:
- result += "\xc2\xa7"
- case 0x16:
- result += "\u25AC"
- case 0x17:
- result += "\u21A8"
- case 0x18:
- result += "\u2191"
- case 0x19:
- result += "\u2193"
- case 0x1a:
- result += "\u2192"
- case 0x1c:
- result += "\u221F"
- case 0x1d:
- result += "\u2194"
- case 0x1e:
- result += "\u25B2"
- case 0x1f:
- result += "\u25BC"
- case 0x7f:
- result += "\u2302"
- case 0x80:
- result += "\xc3\x87"
- case 0x81:
- result += "\xc3\xbc"
- case 0x82:
- result += "\xc3\xa9"
- case 0x83:
- result += "\xc3\xa2"
- case 0x84:
- result += "\xc3\xa4"
- case 0x85:
- result += "\xc3\xa0"
- case 0x86:
- result += "\xc3\xa5"
- case 0x87:
- result += "\xc3\xa7"
- case 0x88:
- result += "\xc3\xaa"
- case 0x89:
- result += "\xc3\xab"
- case 0x8a:
- result += "\xc3\xa8"
- case 0x8b:
- result += "\xc3\xaf"
- case 0x8c:
- result += "\xc3\xae"
- case 0x8d:
- result += "\xc3\xac"
- case 0x8e:
- result += "\xc3\x84"
- case 0x8f:
- result += "\xc3\x85"
- case 0x90:
- result += "\xc3\x89"
- case 0x91:
- result += "\xc3\xa6"
- case 0x92:
- result += "\xc3\x86"
- case 0x93:
- result += "\xc3\xb4"
- case 0x94:
- result += "\xc3\xb6"
- case 0x95:
- result += "\xc3\xb2"
- case 0x96:
- result += "\xc3\xbb"
- case 0x97:
- result += "\xc3\xb9"
- case 0x98:
- result += "\xc3\xbf"
- case 0x99:
- result += "\xc3\x96"
- case 0x9a:
- result += "\xc3\x9c"
- case 0x9b:
- result += "\xc2\xa2"
- case 0x9c:
- result += "\xc2\xa3"
- case 0x9d:
- result += "\xc2\xa5"
- case 0x9e:
- result += "\u20A7"
- case 0x9f:
- result += "\u0192"
- case 0xa0:
- result += "\xc3\xa1"
- case 0xa1:
- result += "\xc3\xad"
- case 0xa2:
- result += "\xc3\xb3"
- case 0xa3:
- result += "\xc3\xba"
- case 0xa4:
- result += "\xc3\xb1"
- case 0xa5:
- result += "\xc3\x91"
- case 0xa6:
- result += "\xc2\xaa"
- case 0xa7:
- result += "\xc2\xba"
- case 0xa8:
- result += "\xc2\xbf"
- case 0xa9:
- result += "\u2310"
- case 0xaa:
- result += "\xc2\xac"
- case 0xab:
- result += "\xc2\xbd"
- case 0xac:
- result += "\xc2\xbc"
- case 0xad:
- result += "\xc2\xa1"
- case 0xae:
- result += "\xc2\xab"
- case 0xaf:
- result += "\xc2\xbb"
- case 0xb0:
- result += "\u2591"
- case 0xb1:
- result += "\u2592"
- case 0xb2:
- result += "\u2593"
- case 0xb3:
- result += "\u2502"
- case 0xb4:
- result += "\u2524"
- case 0xb5:
- result += "\u2561"
- case 0xb6:
- result += "\u2562"
- case 0xb7:
- result += "\u2556"
- case 0xb8:
- result += "\u2555"
- case 0xb9:
- result += "\u2563"
- case 0xba:
- result += "\u2551"
- case 0xbb:
- result += "\u2557"
- case 0xbc:
- result += "\u255D"
- case 0xbd:
- result += "\u255C"
- case 0xbe:
- result += "\u255B"
- case 0xbf:
- result += "\u2510"
- case 0xc0:
- result += "\u2514"
- case 0xc1:
- result += "\u2534"
- case 0xc2:
- result += "\u252C"
- case 0xc3:
- result += "\u251C"
- case 0xc4:
- result += "\u2500"
- case 0xc5:
- result += "\u253C"
- case 0xc6:
- result += "\u255E"
- case 0xc7:
- result += "\u255F"
- case 0xc8:
- result += "\u255A"
- case 0xc9:
- result += "\u2554"
- case 0xca:
- result += "\u2569"
- case 0xcb:
- result += "\u2566"
- case 0xcc:
- result += "\u2560"
- case 0xcd:
- result += "\u2550"
- case 0xce:
- result += "\u256C"
- case 0xcf:
- result += "\u2567"
- case 0xd0:
- result += "\u2568"
- case 0xd1:
- result += "\u2564"
- case 0xd2:
- result += "\u2565"
- case 0xd3:
- result += "\u2559"
- case 0xd4:
- result += "\u2558"
- case 0xd5:
- result += "\u2552"
- case 0xd6:
- result += "\u2553"
- case 0xd7:
- result += "\u256B"
- case 0xd8:
- result += "\u256A"
- case 0xd9:
- result += "\u2518"
- case 0xda:
- result += "\u250C"
- case 0xdb:
- result += "\u2588"
- case 0xdc:
- result += "\u2584"
- case 0xdd:
- result += "\u258C"
- case 0xde:
- result += "\u2590"
- case 0xdf:
- result += "\u2580"
- case 0xe0:
- result += "\u03B1"
- case 0xe1:
- result += "\xc3\x9f"
- case 0xe2:
- result += "\u0393"
- case 0xe3:
- result += "\u03C0"
- case 0xe4:
- result += "\u03A3"
- case 0xe5:
- result += "\u03C3"
- case 0xe6:
- result += "\xc2\xb5"
- case 0xe7:
- result += "\u03C4"
- case 0xe8:
- result += "\u03A6"
- case 0xe9:
- result += "\u0398"
- case 0xea:
- result += "\u03A9"
- case 0xeb:
- result += "\u03B4"
- case 0xec:
- result += "\u221E"
- case 0xed:
- result += "\u03C6"
- case 0xee:
- result += "\u03B5"
- case 0xef:
- result += "\u2229"
- case 0xf0:
- result += "\u2261"
- case 0xf1:
- result += "\xc2\xb1"
- case 0xf2:
- result += "\u2265"
- case 0xf3:
- result += "\u2264"
- case 0xf4:
- result += "\u2320"
- case 0xf5:
- result += "\u2321"
- case 0xf6:
- result += "\xc3\xb7"
- case 0xf7:
- result += "\u2248"
- case 0xf8:
- result += "\xc2\xb0"
- case 0xf9:
- result += "\u2219"
- case 0xfa:
- result += "\xc2\xb7"
- case 0xfb:
- result += "\u221A"
- case 0xfc:
- result += "\u207F"
- case 0xfd:
- result += "\xc2\xb2"
- case 0xfe:
- result += "\u25A0"
- case 0xff:
- result += "\xc2\xa0"
- default:
- result += string(char)
- }
- }
- // fmt.Printf("\n")
- return result
- }
|