123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- package door
- import (
- "bytes"
- "fmt"
- "strconv"
- "strings"
- )
- type BlockFont struct {
- Characters []int
- Data [][][]byte
- }
- func normalizeBlock(block *[][]byte) {
- var max int
-
- for _, line := range *block {
- var l int = len(line)
- if max < l {
- max = l
- }
- }
-
- for idx := range *block {
-
- for len((*block)[idx]) < max {
-
- (*block)[idx] = append((*block)[idx], byte(0x20))
- }
- }
-
-
- }
- func (bf *BlockFont) GetCharacter(c int) [][]byte {
- var result [][]byte
- if c == 32 {
-
- result = append(result, []byte{0x20, 0x20})
- return result
- }
-
- if c >= 33 && c <= 126 {
- c -= 33
- var idx int = bf.Characters[c]
- if idx == -1 {
-
- return result
- }
- result = bf.Data[bf.Characters[c]]
-
- normalizeBlock(&result)
-
- return result
- } else {
- return result
- }
- }
- func expandBlock(block *[][]byte) {
-
- var l int = len((*block)[0])
- var blank []byte = []byte{0x20}
- *block = append(*block, bytes.Repeat(blank, l))
- }
- func (bf *BlockFont) Output(input string) ([]string, int) {
- var out [][]byte
- var max int
- for _, ch := range input {
-
- block := bf.GetCharacter(int(ch))
-
- var l int = len(block)
- max += l
- if l != 0 {
- if len(out) == 0 {
-
- out = append(out, block...)
-
- } else {
- if len(out) != 0 {
- for l > len(out) {
-
-
- expandBlock(&out)
- }
- }
-
-
- for idx, b := range block {
- out[idx] = append(out[idx], byte(0x20))
- out[idx] = append(out[idx], b...)
-
-
-
- }
- }
- }
-
- normalizeBlock(&out)
-
- }
- var output []string
- for idx := range out {
- if Unicode {
- output = append(output, CP437_to_Unicode(string(out[idx])))
- } else {
- output = append(output, string(out[idx]))
- }
- }
- return output, len(out[0])
- }
- type ColorFont struct {
- Characters []int
- Data [][][]byte
- }
- func normalizeColor(block *[][]byte) int {
- var max int
- for _, line := range *block {
- l := len(line)
- if max < l {
- max = l
- }
- }
- for idx := range *block {
-
- var blank []byte = []byte{0x20, 0x0f}
- for len((*block)[idx]) < max {
- (*block)[idx] = append((*block)[idx], blank...)
-
-
- }
- }
- return max
- }
- func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
- var result [][]byte
- if c == 32 {
- var blank []byte = []byte{0x20, 0x0f, 0x20, 0x0f}
- result = append(result, blank)
- return result, len(result[0]) / 2
- }
- if c >= 33 && c <= 126 {
- c -= 33
- var idx int = cf.Characters[c]
- if idx == -1 {
- return result, 0
- }
- result = cf.Data[cf.Characters[c]]
-
-
-
- var max int = normalizeColor(&result)
-
-
- return result, max
- } else {
- return result, 0
- }
- }
- func thedraw_to_ansi(c int) int {
- var trans []int = []int{0, 4, 2, 6, 1, 5, 3, 7}
-
- return trans[c]
- }
- 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 {
- var prev ColorParts = ColorSplit(previous)
- var c ColorParts = ColorSplit(color)
- var codes []int8
- if c.Bold {
- if !prev.Bold {
- codes = append(codes, 1)
- }
- } else {
- if prev.Bold {
-
- codes = append(codes, 0)
- prev.Background = 0
- prev.Foreground = 7
- }
- }
- if prev.Background == 0 && prev.Foreground == 0 {
-
- 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 {
-
- 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
-
- var previous int
-
- for pos := 0; pos < len(input); pos += 2 {
- var ch byte = input[pos]
- var color int = int(input[pos+1])
-
- var colorstring string = ColorOutput(previous, color)
- result = append(result, []byte(colorstring)...)
-
-
-
-
- result = append(result, ch)
-
- previous = color
-
- }
- return result
- }
- func expandColor(block *[][]byte, need int) {
- var blank []byte = []byte{0x20, 0x0f}
-
- *block = append(*block, bytes.Repeat(blank, need))
- }
- func (bf *ColorFont) Output(input string) ([]string, int) {
- var out [][]byte
- var max int
- for _, ch := range input {
-
- var block [][]byte
- var blklen int
- block, blklen = bf.GetCharacter(int(ch))
- if blklen == 0 {
- continue
- }
- var l int = len(block)
- max += blklen
- if l != 0 {
- if len(out) == 0 {
-
- out = append(out, block...)
-
- } else {
- if len(out) != 0 {
- for l > len(out) {
-
- expandColor(&out, len(out[0])/2)
-
- }
- }
- for len(out) > len(block) {
-
- expandColor(&block, len(block)/2)
- }
-
- normalizeColor(&block)
- if len(out) != len(block) {
- panic(fmt.Sprintf("len(out) %d != len(block) %d", len(out), len(block)))
- }
- var blank []byte = []byte{0x20, 0x0f}
-
- for idx, b := range block {
-
- out[idx] = append(out[idx], blank...)
- out[idx] = append(out[idx], b...)
-
-
-
- }
- }
-
- }
- }
- if len(out) == 0 {
- return []string{}, 0
- }
-
- max = len(out[0]) / 2
- for idx := range out {
- out[idx] = Colorize(out[idx])
- }
- var output []string
- for idx := range out {
- if Unicode {
- output = append(output, CP437_to_Unicode(string(out[idx])))
- } else {
- output = append(output, string(out[idx]))
- }
- }
- return output, max
- }
- func MatchStyle(color byte, look byte) int {
- var match int = 0
- if ((color >> 4) & 0x07) == look {
-
- match |= 1
- }
- if (color & 0x07) == look {
-
- match |= 2
- }
- return match
- }
- func PatchColor(color byte, new_color byte, style int) byte {
- var c byte = color
- if style&1 == 1 {
- c = (c & 0x8f) | new_color<<4
- }
- if style&2 == 2 {
- c = (c & 0xf8) | new_color
- }
- return c
- }
- type ColorMap map[[2]int][][2]int
- func (cf *ColorFont) Scan(find_color int) ColorMap {
- var Targets ColorMap = make(ColorMap, 0)
-
-
- var actual byte = byte(thedraw_to_ansi(find_color))
- for charIndex := range cf.Data {
- for lineIndex := range cf.Data[charIndex] {
- var found bool = false
- var patches [][2]int = make([][2]int, 0)
- for offset := 1; offset < len(cf.Data[charIndex][lineIndex]); offset += 2 {
- var color byte = cf.Data[charIndex][lineIndex][offset]
- var style int = MatchStyle(color, actual)
- if style != 0 {
-
- patches = append(patches, [2]int{offset, style})
- found = true
- }
- }
- if found {
- var pos [2]int = [2]int{charIndex, lineIndex}
- Targets[pos] = make([][2]int, len(patches))
- copy(Targets[pos], patches)
-
-
- }
- }
- }
- return Targets
- }
- func (cf *ColorFont) Modify(new_color int, Targets ColorMap) {
-
- var actual byte = byte(thedraw_to_ansi(new_color))
- for pos, patch := range Targets {
- for _, p := range patch {
- cf.Data[pos[0]][pos[1]][p[0]] = PatchColor(cf.Data[pos[0]][pos[1]][p[0]], actual, p[1])
- }
- }
- }
|