tdfont.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package door
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. type BlockFont struct {
  9. Characters []int
  10. Data [][][]byte
  11. }
  12. /*
  13. func BytesHexed(output []byte) {
  14. fmt.Printf("BH: ")
  15. for _, ch := range output {
  16. fmt.Printf("%02x ", ch)
  17. }
  18. fmt.Println("")
  19. }
  20. func BytesArrayHexed(output *[][]byte) {
  21. for _, out := range *output {
  22. BytesHexed(out)
  23. }
  24. }
  25. */
  26. // Make []strings all the same length
  27. func normalizeBlock(block *[][]byte) {
  28. var max int
  29. // StringO(block)
  30. for _, line := range *block {
  31. var l int = len(line)
  32. if max < l {
  33. max = l
  34. }
  35. }
  36. // fmt.Printf("max = %d\n", max)
  37. for idx := range *block {
  38. // l := len(line)
  39. for len((*block)[idx]) < max {
  40. //(*block)[idx] += byte(0x20) //append((*block)[idx], byte{0x20})
  41. (*block)[idx] = append((*block)[idx], byte(0x20))
  42. }
  43. }
  44. // StringO(block)
  45. // fmt.Println("== normalized ==")
  46. }
  47. // Get Character information from BlockFont return Normalize
  48. func (bf *BlockFont) GetCharacter(c int) [][]byte {
  49. var result [][]byte
  50. if c == 32 {
  51. // Space character
  52. result = append(result, []byte{0x20, 0x20})
  53. return result
  54. }
  55. // 33-126 are the possible characters.
  56. if c >= 33 && c <= 126 {
  57. c -= 33
  58. var idx int = bf.Characters[c]
  59. if idx == -1 {
  60. // This character is not supported by this font.
  61. return result
  62. }
  63. result = bf.Data[bf.Characters[c]]
  64. // fmt.Println("normalizeCharBlock")
  65. normalizeBlock(&result)
  66. // fmt.Println("normalizeChar done")
  67. return result
  68. } else {
  69. return result
  70. }
  71. }
  72. func expandBlock(block *[][]byte) {
  73. // l := len((*block)[0])
  74. var l int = len((*block)[0])
  75. var blank []byte = []byte{0x20}
  76. *block = append(*block, bytes.Repeat(blank, l))
  77. }
  78. // Given text, translate to thedraw font.
  79. func (bf *BlockFont) Output(input string) ([]string, int) {
  80. var out [][]byte
  81. var max int
  82. for _, ch := range input {
  83. // fmt.Printf("%d %x\n", ch, ch)
  84. block := bf.GetCharacter(int(ch))
  85. // fmt.Println("ok")
  86. var l int = len(block)
  87. max += l
  88. if l != 0 {
  89. if len(out) == 0 {
  90. // First time
  91. out = append(out, block...)
  92. /*
  93. for _, b := range block {
  94. out = append(out, b)
  95. }
  96. */
  97. } else {
  98. if len(out) != 0 {
  99. for l > len(out) {
  100. // We need to expand the out
  101. // fmt.Println("expandBlock")
  102. expandBlock(&out)
  103. }
  104. }
  105. // fmt.Println("Append block to out")
  106. // Ok, we have something!
  107. for idx, b := range block {
  108. out[idx] = append(out[idx], byte(0x20))
  109. out[idx] = append(out[idx], b...)
  110. /*
  111. for _, inner := range b {
  112. out[idx] = append(out[idx], inner)
  113. }
  114. */
  115. // out[idx] += byte(0x20) + b
  116. // fmt.Printf("%s\n", CP437_to_Unicode(b))
  117. }
  118. }
  119. }
  120. // fmt.Println("normalizeBlock")
  121. normalizeBlock(&out)
  122. // fmt.Println("normalizeBlock done")
  123. }
  124. var output []string
  125. for idx := range out {
  126. if Unicode {
  127. output = append(output, CP437_to_Unicode(string(out[idx])))
  128. } else {
  129. output = append(output, string(out[idx]))
  130. }
  131. }
  132. return output, len(out[0])
  133. }
  134. type ColorFont struct {
  135. Characters []int
  136. Data [][][]byte
  137. }
  138. func normalizeColor(block *[][]byte) int {
  139. var max int
  140. for _, line := range *block {
  141. l := len(line)
  142. if max < l {
  143. max = l
  144. }
  145. }
  146. for idx := range *block {
  147. // l := len(line)
  148. var blank []byte = []byte{0x20, 0x0f}
  149. for len((*block)[idx]) < max {
  150. (*block)[idx] = append((*block)[idx], blank...)
  151. /*
  152. for _, b := range blank {
  153. (*block)[idx] = append((*block)[idx], b)
  154. }
  155. */
  156. // (*block)[idx] += strings.Repeat(" \x0f", max-l/2)
  157. }
  158. }
  159. return max
  160. }
  161. func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
  162. var result [][]byte
  163. if c == 32 {
  164. var blank []byte = []byte{0x20, 0x0f, 0x20, 0x0f}
  165. result = append(result, blank) // " \x0f \x0f")
  166. return result, len(result[0]) / 2
  167. }
  168. if c >= 33 && c <= 126 {
  169. c -= 33
  170. var idx int = cf.Characters[c]
  171. if idx == -1 {
  172. return result, 0
  173. }
  174. result = cf.Data[cf.Characters[c]]
  175. // fmt.Println("Character:")
  176. // BytesArrayHexed(&result)
  177. // fmt.Println("normalizing...")
  178. var max int = normalizeColor(&result)
  179. // BytesArrayHexed(&result)
  180. // StringHexO(&result)
  181. return result, max
  182. } else {
  183. return result, 0
  184. }
  185. }
  186. func thedraw_to_ansi(c int) int {
  187. var trans []int = []int{0, 4, 2, 6, 1, 5, 3, 7}
  188. // 0, 1, 2, 3, 4, 5, 6, 7
  189. return trans[c]
  190. }
  191. /*
  192. // Before color output was optimized
  193. func colorout(c int) string {
  194. bg := thedraw_to_ansi(c / 16)
  195. fg := c % 16
  196. bold := (fg & 0x8) == 0x8
  197. fg = thedraw_to_ansi(fg & 0x7)
  198. if bold {
  199. return fmt.Sprintf("\x1b[0;1;%d;%dm", fg+30, bg+40)
  200. } else {
  201. return fmt.Sprintf("\x1b[0;%d;%dm", fg+30, bg+40)
  202. }
  203. }
  204. */
  205. type ColorParts struct {
  206. Background int
  207. Foreground int
  208. Bold bool
  209. }
  210. func ColorSplit(color int) ColorParts {
  211. return ColorParts{Background: thedraw_to_ansi(color / 16), Foreground: thedraw_to_ansi(color & 7), Bold: (color%16)&0x08 == 0x08}
  212. }
  213. func ColorOutput(previous int, color int) string {
  214. var prev ColorParts = ColorSplit(previous)
  215. var c ColorParts = ColorSplit(color)
  216. var codes []int8
  217. if c.Bold {
  218. if !prev.Bold {
  219. codes = append(codes, 1)
  220. }
  221. } else {
  222. if prev.Bold {
  223. // bold was set previously, there's only one way to reset it
  224. codes = append(codes, 0)
  225. prev.Background = 0
  226. prev.Foreground = 7
  227. }
  228. }
  229. if prev.Background == 0 && prev.Foreground == 0 {
  230. // output everything
  231. codes = append(codes, int8(c.Foreground)+30)
  232. codes = append(codes, int8(c.Background)+40)
  233. } else {
  234. if c.Foreground != prev.Foreground {
  235. codes = append(codes, int8(c.Foreground)+30)
  236. }
  237. if c.Background != prev.Background {
  238. codes = append(codes, int8(c.Background)+40)
  239. }
  240. }
  241. if len(codes) == 0 {
  242. // Everything matched
  243. return ""
  244. }
  245. var text []string
  246. for _, code := range codes {
  247. text = append(text, strconv.Itoa(int(code)))
  248. }
  249. return "\x1b[" + strings.Join(text, ";") + "m"
  250. }
  251. func Colorize(input []byte) []byte {
  252. var result []byte
  253. // runes := []rune(input)
  254. var previous int
  255. // BytesHexed(input)
  256. for pos := 0; pos < len(input); pos += 2 {
  257. var ch byte = input[pos]
  258. var color int = int(input[pos+1])
  259. // fmt.Printf("%d : CH %d / %x, Color %d / %x\n", pos, ch, ch, color, color)
  260. var colorstring string = ColorOutput(previous, color)
  261. result = append(result, []byte(colorstring)...)
  262. /*
  263. for _, c := range []byte(colorstring) {
  264. result = append(result, c)
  265. }
  266. */
  267. // result = append(result, []byte(colorstring))
  268. // result += []byte(ColorOutput(previous, color))
  269. // result += ColorOutput(previous, color) + string(ch)
  270. result = append(result, ch)
  271. // result += string(ch)
  272. previous = color
  273. // result += colorout(color) + string(ch)
  274. }
  275. return result
  276. }
  277. /*
  278. func __expandColor(block *[]string, need int) {
  279. *block = append(*block, strings.Repeat(" \x0f", need))
  280. }
  281. */
  282. func expandColor(block *[][]byte, need int) {
  283. var blank []byte = []byte{0x20, 0x0f}
  284. // *block = append(*block, strings.Repeat(" \x0f", need))
  285. *block = append(*block, bytes.Repeat(blank, need))
  286. }
  287. func (bf *ColorFont) Output(input string) ([]string, int) {
  288. var out [][]byte
  289. var max int
  290. for _, ch := range input {
  291. // fmt.Printf("%d %x\n", ch, ch)
  292. var block [][]byte
  293. var blklen int
  294. block, blklen = bf.GetCharacter(int(ch))
  295. if blklen == 0 {
  296. continue
  297. }
  298. var l int = len(block)
  299. max += blklen
  300. if l != 0 {
  301. if len(out) == 0 {
  302. // First time
  303. out = append(out, block...)
  304. /*
  305. for _, b := range block {
  306. out = append(out, b)
  307. }
  308. */
  309. } else {
  310. if len(out) != 0 {
  311. for l > len(out) {
  312. // We need to expand the out
  313. expandColor(&out, len(out[0])/2)
  314. // ExpandColor(&out)
  315. }
  316. }
  317. for len(out) > len(block) {
  318. // We need to expand the block out
  319. expandColor(&block, len(block)/2)
  320. }
  321. // Normalizing the character blocks
  322. normalizeColor(&block)
  323. if len(out) != len(block) {
  324. panic(fmt.Sprintf("len(out) %d != len(block) %d", len(out), len(block)))
  325. }
  326. var blank []byte = []byte{0x20, 0x0f}
  327. // Ok, we have something!
  328. for idx, b := range block {
  329. /*
  330. out[idx] = append(out[idx], byte(0x20))
  331. out[idx] = append(out[idx], byte(0x0f))
  332. */
  333. out[idx] = append(out[idx], blank...)
  334. out[idx] = append(out[idx], b...)
  335. /*
  336. for _, inner := range b {
  337. out[idx] = append(out[idx], inner)
  338. }
  339. */
  340. //out[idx] += " \x0f" + b
  341. // fmt.Printf("%s\n", CP437_to_Unicode(b))
  342. }
  343. }
  344. // NormalizeColor(&out)
  345. }
  346. }
  347. if len(out) == 0 {
  348. return []string{}, 0
  349. }
  350. // StringHexO(&out)
  351. max = len(out[0]) / 2
  352. for idx := range out {
  353. out[idx] = Colorize(out[idx])
  354. }
  355. var output []string
  356. for idx := range out {
  357. if Unicode {
  358. output = append(output, CP437_to_Unicode(string(out[idx])))
  359. } else {
  360. output = append(output, string(out[idx]))
  361. }
  362. }
  363. return output, max
  364. }
  365. // Given a color to look for, see if it is in the color byte.
  366. // 0x01 = Upper match
  367. // 0x02 = Lower match
  368. func MatchStyle(color byte, look byte) int {
  369. var match int = 0
  370. if ((color >> 4) & 0x07) == look {
  371. // Top
  372. match |= 1
  373. }
  374. if (color & 0x07) == look {
  375. // Bottom
  376. match |= 2
  377. }
  378. return match
  379. }
  380. // Update a color byte with the new color information.
  381. // Style 0x01 = Upper, 0x02 = Lower, 0x03 = Both.
  382. func PatchColor(color byte, new_color byte, style int) byte {
  383. var c byte = color
  384. if style&1 == 1 {
  385. c = (c & 0x8f) | new_color<<4
  386. }
  387. if style&2 == 2 {
  388. c = (c & 0xf8) | new_color
  389. }
  390. return c
  391. }
  392. type ColorMap map[[2]int][][2]int
  393. // Scan a ColorFont for a specific color.
  394. // This returns a map key of character Index + line Index
  395. // with an array of [2]int (Index, Style) to change.
  396. func (cf *ColorFont) Scan(find_color int) ColorMap {
  397. var Targets ColorMap = make(ColorMap, 0)
  398. // Scan the font looking for the given color FG/BG
  399. // Covert color code to TheDraw Color
  400. var actual byte = byte(thedraw_to_ansi(find_color))
  401. for charIndex := range cf.Data {
  402. for lineIndex := range cf.Data[charIndex] {
  403. var found bool = false
  404. var patches [][2]int = make([][2]int, 0)
  405. for offset := 1; offset < len(cf.Data[charIndex][lineIndex]); offset += 2 {
  406. var color byte = cf.Data[charIndex][lineIndex][offset]
  407. var style int = MatchStyle(color, actual)
  408. if style != 0 {
  409. // log.Printf("color: %x actual %x style: %d\n", color, actual, style)
  410. patches = append(patches, [2]int{offset, style})
  411. found = true
  412. }
  413. }
  414. if found {
  415. var pos [2]int = [2]int{charIndex, lineIndex}
  416. Targets[pos] = make([][2]int, len(patches))
  417. copy(Targets[pos], patches)
  418. /*
  419. for i := range patches {
  420. Targets[pos][i] = patches[i]
  421. }
  422. */
  423. // Targets[pos] = patches
  424. }
  425. }
  426. }
  427. return Targets
  428. }
  429. // This modifies the font color, using the Targets found with
  430. // Scan.
  431. func (cf *ColorFont) Modify(new_color int, Targets ColorMap) {
  432. // Covert color code to TheDraw Color
  433. var actual byte = byte(thedraw_to_ansi(new_color))
  434. for pos, patch := range Targets {
  435. for _, p := range patch {
  436. cf.Data[pos[0]][pos[1]][p[0]] = PatchColor(cf.Data[pos[0]][pos[1]][p[0]], actual, p[1])
  437. }
  438. }
  439. }