tdfont.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. type BlockFont struct {
  9. characters []int
  10. data [][][]byte
  11. }
  12. // Make []strings all the same length
  13. func normalizeBlock(block *[][]byte) {
  14. var max int
  15. // StringO(block)
  16. for _, line := range *block {
  17. l := len(line)
  18. if max < l {
  19. max = l
  20. }
  21. }
  22. // fmt.Printf("max = %d\n", max)
  23. for idx, _ := range *block {
  24. // l := len(line)
  25. for len((*block)[idx]) < max {
  26. //(*block)[idx] += byte(0x20) //append((*block)[idx], byte{0x20})
  27. (*block)[idx] = append((*block)[idx], byte(0x20))
  28. }
  29. }
  30. // StringO(block)
  31. // fmt.Println("== normalized ==")
  32. }
  33. // Get Character information from BlockFont return Normalize
  34. func (bf *BlockFont) GetCharacter(c int) [][]byte {
  35. var result [][]byte
  36. if c == 32 {
  37. // Space character
  38. result = append(result, []byte{0x20, 0x20})
  39. return result
  40. }
  41. // 33-126 are the possible characters.
  42. if c >= 33 && c <= 126 {
  43. c -= 33
  44. idx := bf.characters[c]
  45. if idx == -1 {
  46. // This character is not supported by this font.
  47. return result
  48. }
  49. result = bf.data[bf.characters[c]]
  50. // fmt.Println("normalizeCharBlock")
  51. normalizeBlock(&result)
  52. // fmt.Println("normalizeChar done")
  53. return result
  54. } else {
  55. return result
  56. }
  57. }
  58. // expandBlock the number of strings, and make it match the length of [0].
  59. func __expandBlock(block *[]string) {
  60. // l := len((*block)[0])
  61. l := len([]rune((*block)[0]))
  62. *block = append(*block, strings.Repeat(" ", l))
  63. }
  64. func expandBlock(block *[][]byte) {
  65. // l := len((*block)[0])
  66. l := len((*block)[0])
  67. blank := []byte{0x20}
  68. *block = append(*block, bytes.Repeat(blank, l))
  69. }
  70. // Given text, translate to thedraw font.
  71. func (bf *BlockFont) Output(input string) [][]byte {
  72. var out [][]byte
  73. for _, ch := range input {
  74. // fmt.Printf("%d %x\n", ch, ch)
  75. block := bf.GetCharacter(int(ch))
  76. // fmt.Println("ok")
  77. l := len(block)
  78. if l != 0 {
  79. if len(out) == 0 {
  80. // First time
  81. for _, b := range block {
  82. out = append(out, b)
  83. }
  84. } else {
  85. if len(out) != 0 {
  86. for l > len(out) {
  87. // We need to expand the out
  88. // fmt.Println("expandBlock")
  89. expandBlock(&out)
  90. }
  91. }
  92. // fmt.Println("Append block to out")
  93. // Ok, we have something!
  94. for idx, b := range block {
  95. out[idx] = append(out[idx], byte(0x20))
  96. // out[idx] = append(out[idx], b)
  97. for _, inner := range b {
  98. out[idx] = append(out[idx], inner)
  99. }
  100. // out[idx] += byte(0x20) + b
  101. // fmt.Printf("%s\n", CP437_to_Unicode(b))
  102. }
  103. }
  104. }
  105. // fmt.Println("normalizeBlock")
  106. normalizeBlock(&out)
  107. // fmt.Println("normalizeBlock done")
  108. }
  109. return out
  110. }
  111. type ColorFont struct {
  112. characters []int
  113. data [][][]byte
  114. }
  115. // Make []strings all the same length
  116. func ___normalizeColor(block *[]string) int {
  117. var max int
  118. for _, line := range *block {
  119. l := len([]rune(line))
  120. if max < l {
  121. max = l
  122. }
  123. }
  124. for idx, line := range *block {
  125. l := len([]rune(line))
  126. if l < max {
  127. (*block)[idx] += strings.Repeat(" \x0f", max-l/2)
  128. }
  129. }
  130. return max
  131. }
  132. func normalizeColor(block *[][]byte) int {
  133. var max int
  134. for _, line := range *block {
  135. l := len(line)
  136. if max < l {
  137. max = l
  138. }
  139. }
  140. for idx, _ := range *block {
  141. // l := len(line)
  142. blank := []byte{0x20, 0x0f}
  143. for len((*block)[idx]) < max {
  144. for _, b := range blank {
  145. (*block)[idx] = append((*block)[idx], b)
  146. }
  147. // (*block)[idx] += strings.Repeat(" \x0f", max-l/2)
  148. }
  149. }
  150. return max
  151. }
  152. func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
  153. var result [][]byte
  154. if c == 32 {
  155. blank := []byte{0x20, 0x0f, 0x20, 0x0f}
  156. result = append(result, blank) // " \x0f \x0f")
  157. return result, len(result[0])
  158. }
  159. if c >= 33 && c <= 126 {
  160. c -= 33
  161. idx := cf.characters[c]
  162. if idx == -1 {
  163. return result, 0
  164. }
  165. result = cf.data[cf.characters[c]]
  166. max := normalizeColor(&result)
  167. // StringHexO(&result)
  168. return result, max
  169. } else {
  170. return result, 0
  171. }
  172. }
  173. /*
  174. func expandColor(block *[]string) {
  175. l := len((*block)[0])
  176. *block = append(*block, strings.Repeat(" \x0f", l/2))
  177. }
  178. */
  179. func thedraw_to_ansi(c int) int {
  180. trans := []int{0, 4, 2, 6, 1, 5, 3, 7}
  181. return trans[c]
  182. }
  183. func colorout(c int) string {
  184. bg := thedraw_to_ansi(c / 16)
  185. fg := c % 16
  186. bold := (fg & 0x8) == 0x8
  187. fg = thedraw_to_ansi(fg & 0x7)
  188. if bold {
  189. return fmt.Sprintf("\x1b[0;1;%d;%dm", fg+30, bg+40)
  190. } else {
  191. return fmt.Sprintf("\x1b[0;%d;%dm", fg+30, bg+40)
  192. }
  193. }
  194. type ColorParts struct {
  195. Background int
  196. Foreground int
  197. Bold bool
  198. }
  199. func ColorSplit(color int) ColorParts {
  200. return ColorParts{Background: thedraw_to_ansi(color / 16), Foreground: thedraw_to_ansi(color & 7), Bold: (color%16)&0x08 == 0x08}
  201. }
  202. func ColorOutput(previous int, color int) string {
  203. prev := ColorSplit(previous)
  204. c := ColorSplit(color)
  205. var codes []int8
  206. if c.Bold {
  207. if !prev.Bold {
  208. codes = append(codes, 1)
  209. }
  210. } else {
  211. if prev.Bold {
  212. // bold was set previously, there's only one way to reset it
  213. codes = append(codes, 0)
  214. prev.Background = 0
  215. prev.Foreground = 7
  216. }
  217. }
  218. if prev.Background == 0 && prev.Foreground == 0 {
  219. // output everything
  220. codes = append(codes, int8(c.Foreground)+30)
  221. codes = append(codes, int8(c.Background)+40)
  222. } else {
  223. if c.Foreground != prev.Foreground {
  224. codes = append(codes, int8(c.Foreground)+30)
  225. }
  226. if c.Background != prev.Background {
  227. codes = append(codes, int8(c.Background)+40)
  228. }
  229. }
  230. if len(codes) == 0 {
  231. // Everything matched
  232. return ""
  233. }
  234. var text []string
  235. for _, code := range codes {
  236. text = append(text, strconv.Itoa(int(code)))
  237. }
  238. return "\x1b[" + strings.Join(text, ";") + "m"
  239. }
  240. func Colorize(input []byte) []byte {
  241. var result []byte
  242. // runes := []rune(input)
  243. var previous int
  244. // StringHexed(input)
  245. for pos := 0; pos < len(input); pos += 2 {
  246. ch := input[pos]
  247. color := int(input[pos+1])
  248. // fmt.Printf("%d : %d / %x, %d\n", pos, ch, ch, color)
  249. colorstring := ColorOutput(previous, color)
  250. for _, c := range []byte(colorstring) {
  251. result = append(result, c)
  252. }
  253. // result = append(result, []byte(colorstring))
  254. // result += []byte(ColorOutput(previous, color))
  255. // result += ColorOutput(previous, color) + string(ch)
  256. result = append(result, ch)
  257. // result += string(ch)
  258. previous = color
  259. // result += colorout(color) + string(ch)
  260. }
  261. return result
  262. }
  263. func __expandColor(block *[]string, need int) {
  264. *block = append(*block, strings.Repeat(" \x0f", need))
  265. }
  266. func expandColor(block *[][]byte, need int) {
  267. blank := []byte{0x20, 0x0f}
  268. // *block = append(*block, strings.Repeat(" \x0f", need))
  269. *block = append(*block, bytes.Repeat(blank, need))
  270. }
  271. func (bf *ColorFont) Output(input string) [][]byte {
  272. var out [][]byte
  273. var max int
  274. for _, ch := range input {
  275. // fmt.Printf("%d %x\n", ch, ch)
  276. block, blklen := bf.GetCharacter(int(ch))
  277. l := len(block)
  278. max += blklen
  279. if l != 0 {
  280. if len(out) == 0 {
  281. // First time
  282. for _, b := range block {
  283. out = append(out, b)
  284. }
  285. } else {
  286. if len(out) != 0 {
  287. for l > len(out) {
  288. // We need to expand the out
  289. expandColor(&out, max-blklen)
  290. // ExpandColor(&out)
  291. }
  292. }
  293. for l > len(block) {
  294. // We need to expand the block out
  295. expandColor(&block, len(block[0])/2)
  296. }
  297. // Ok, we have something!
  298. for idx, b := range block {
  299. // blank := []byte{0x20, 0x0f}
  300. out[idx] = append(out[idx], byte(0x20))
  301. out[idx] = append(out[idx], byte(0x0f))
  302. for _, inner := range b {
  303. out[idx] = append(out[idx], inner)
  304. }
  305. //out[idx] += " \x0f" + b
  306. // fmt.Printf("%s\n", CP437_to_Unicode(b))
  307. }
  308. }
  309. // NormalizeColor(&out)
  310. }
  311. }
  312. // StringHexO(&out)
  313. for idx, _ := range out {
  314. out[idx] = Colorize(out[idx])
  315. }
  316. return out
  317. }
  318. func CP437Bytes_to_Unicode(cp437 []byte) string {
  319. var result string
  320. for _, char := range cp437 {
  321. // fmt.Printf("%d, %x ", char, char)
  322. switch int(char) {
  323. case 0x01:
  324. result += "\u263A"
  325. case 0x02:
  326. result += "\u263B"
  327. case 0x03:
  328. result += "\u2665"
  329. case 0x04:
  330. result += "\u2666"
  331. case 0x05:
  332. result += "\u2663"
  333. case 0x06:
  334. result += "\u2660"
  335. case 0x09:
  336. result += "\u25CB"
  337. case 0x0b:
  338. result += "\u2642"
  339. case 0x0c:
  340. result += "\u2640"
  341. case 0x0e:
  342. result += "\u266B"
  343. case 0x0f:
  344. result += "\u263C"
  345. case 0x10:
  346. result += "\u25BA"
  347. case 0x11:
  348. result += "\u25C4"
  349. case 0x12:
  350. result += "\u2195"
  351. case 0x13:
  352. result += "\u203C"
  353. case 0x14:
  354. result += "\xc2\xb6"
  355. case 0x15:
  356. result += "\xc2\xa7"
  357. case 0x16:
  358. result += "\u25AC"
  359. case 0x17:
  360. result += "\u21A8"
  361. case 0x18:
  362. result += "\u2191"
  363. case 0x19:
  364. result += "\u2193"
  365. case 0x1a:
  366. result += "\u2192"
  367. case 0x1c:
  368. result += "\u221F"
  369. case 0x1d:
  370. result += "\u2194"
  371. case 0x1e:
  372. result += "\u25B2"
  373. case 0x1f:
  374. result += "\u25BC"
  375. case 0x7f:
  376. result += "\u2302"
  377. case 0x80:
  378. result += "\xc3\x87"
  379. case 0x81:
  380. result += "\xc3\xbc"
  381. case 0x82:
  382. result += "\xc3\xa9"
  383. case 0x83:
  384. result += "\xc3\xa2"
  385. case 0x84:
  386. result += "\xc3\xa4"
  387. case 0x85:
  388. result += "\xc3\xa0"
  389. case 0x86:
  390. result += "\xc3\xa5"
  391. case 0x87:
  392. result += "\xc3\xa7"
  393. case 0x88:
  394. result += "\xc3\xaa"
  395. case 0x89:
  396. result += "\xc3\xab"
  397. case 0x8a:
  398. result += "\xc3\xa8"
  399. case 0x8b:
  400. result += "\xc3\xaf"
  401. case 0x8c:
  402. result += "\xc3\xae"
  403. case 0x8d:
  404. result += "\xc3\xac"
  405. case 0x8e:
  406. result += "\xc3\x84"
  407. case 0x8f:
  408. result += "\xc3\x85"
  409. case 0x90:
  410. result += "\xc3\x89"
  411. case 0x91:
  412. result += "\xc3\xa6"
  413. case 0x92:
  414. result += "\xc3\x86"
  415. case 0x93:
  416. result += "\xc3\xb4"
  417. case 0x94:
  418. result += "\xc3\xb6"
  419. case 0x95:
  420. result += "\xc3\xb2"
  421. case 0x96:
  422. result += "\xc3\xbb"
  423. case 0x97:
  424. result += "\xc3\xb9"
  425. case 0x98:
  426. result += "\xc3\xbf"
  427. case 0x99:
  428. result += "\xc3\x96"
  429. case 0x9a:
  430. result += "\xc3\x9c"
  431. case 0x9b:
  432. result += "\xc2\xa2"
  433. case 0x9c:
  434. result += "\xc2\xa3"
  435. case 0x9d:
  436. result += "\xc2\xa5"
  437. case 0x9e:
  438. result += "\u20A7"
  439. case 0x9f:
  440. result += "\u0192"
  441. case 0xa0:
  442. result += "\xc3\xa1"
  443. case 0xa1:
  444. result += "\xc3\xad"
  445. case 0xa2:
  446. result += "\xc3\xb3"
  447. case 0xa3:
  448. result += "\xc3\xba"
  449. case 0xa4:
  450. result += "\xc3\xb1"
  451. case 0xa5:
  452. result += "\xc3\x91"
  453. case 0xa6:
  454. result += "\xc2\xaa"
  455. case 0xa7:
  456. result += "\xc2\xba"
  457. case 0xa8:
  458. result += "\xc2\xbf"
  459. case 0xa9:
  460. result += "\u2310"
  461. case 0xaa:
  462. result += "\xc2\xac"
  463. case 0xab:
  464. result += "\xc2\xbd"
  465. case 0xac:
  466. result += "\xc2\xbc"
  467. case 0xad:
  468. result += "\xc2\xa1"
  469. case 0xae:
  470. result += "\xc2\xab"
  471. case 0xaf:
  472. result += "\xc2\xbb"
  473. case 0xb0:
  474. result += "\u2591"
  475. case 0xb1:
  476. result += "\u2592"
  477. case 0xb2:
  478. result += "\u2593"
  479. case 0xb3:
  480. result += "\u2502"
  481. case 0xb4:
  482. result += "\u2524"
  483. case 0xb5:
  484. result += "\u2561"
  485. case 0xb6:
  486. result += "\u2562"
  487. case 0xb7:
  488. result += "\u2556"
  489. case 0xb8:
  490. result += "\u2555"
  491. case 0xb9:
  492. result += "\u2563"
  493. case 0xba:
  494. result += "\u2551"
  495. case 0xbb:
  496. result += "\u2557"
  497. case 0xbc:
  498. result += "\u255D"
  499. case 0xbd:
  500. result += "\u255C"
  501. case 0xbe:
  502. result += "\u255B"
  503. case 0xbf:
  504. result += "\u2510"
  505. case 0xc0:
  506. result += "\u2514"
  507. case 0xc1:
  508. result += "\u2534"
  509. case 0xc2:
  510. result += "\u252C"
  511. case 0xc3:
  512. result += "\u251C"
  513. case 0xc4:
  514. result += "\u2500"
  515. case 0xc5:
  516. result += "\u253C"
  517. case 0xc6:
  518. result += "\u255E"
  519. case 0xc7:
  520. result += "\u255F"
  521. case 0xc8:
  522. result += "\u255A"
  523. case 0xc9:
  524. result += "\u2554"
  525. case 0xca:
  526. result += "\u2569"
  527. case 0xcb:
  528. result += "\u2566"
  529. case 0xcc:
  530. result += "\u2560"
  531. case 0xcd:
  532. result += "\u2550"
  533. case 0xce:
  534. result += "\u256C"
  535. case 0xcf:
  536. result += "\u2567"
  537. case 0xd0:
  538. result += "\u2568"
  539. case 0xd1:
  540. result += "\u2564"
  541. case 0xd2:
  542. result += "\u2565"
  543. case 0xd3:
  544. result += "\u2559"
  545. case 0xd4:
  546. result += "\u2558"
  547. case 0xd5:
  548. result += "\u2552"
  549. case 0xd6:
  550. result += "\u2553"
  551. case 0xd7:
  552. result += "\u256B"
  553. case 0xd8:
  554. result += "\u256A"
  555. case 0xd9:
  556. result += "\u2518"
  557. case 0xda:
  558. result += "\u250C"
  559. case 0xdb:
  560. result += "\u2588"
  561. case 0xdc:
  562. result += "\u2584"
  563. case 0xdd:
  564. result += "\u258C"
  565. case 0xde:
  566. result += "\u2590"
  567. case 0xdf:
  568. result += "\u2580"
  569. case 0xe0:
  570. result += "\u03B1"
  571. case 0xe1:
  572. result += "\xc3\x9f"
  573. case 0xe2:
  574. result += "\u0393"
  575. case 0xe3:
  576. result += "\u03C0"
  577. case 0xe4:
  578. result += "\u03A3"
  579. case 0xe5:
  580. result += "\u03C3"
  581. case 0xe6:
  582. result += "\xc2\xb5"
  583. case 0xe7:
  584. result += "\u03C4"
  585. case 0xe8:
  586. result += "\u03A6"
  587. case 0xe9:
  588. result += "\u0398"
  589. case 0xea:
  590. result += "\u03A9"
  591. case 0xeb:
  592. result += "\u03B4"
  593. case 0xec:
  594. result += "\u221E"
  595. case 0xed:
  596. result += "\u03C6"
  597. case 0xee:
  598. result += "\u03B5"
  599. case 0xef:
  600. result += "\u2229"
  601. case 0xf0:
  602. result += "\u2261"
  603. case 0xf1:
  604. result += "\xc2\xb1"
  605. case 0xf2:
  606. result += "\u2265"
  607. case 0xf3:
  608. result += "\u2264"
  609. case 0xf4:
  610. result += "\u2320"
  611. case 0xf5:
  612. result += "\u2321"
  613. case 0xf6:
  614. result += "\xc3\xb7"
  615. case 0xf7:
  616. result += "\u2248"
  617. case 0xf8:
  618. result += "\xc2\xb0"
  619. case 0xf9:
  620. result += "\u2219"
  621. case 0xfa:
  622. result += "\xc2\xb7"
  623. case 0xfb:
  624. result += "\u221A"
  625. case 0xfc:
  626. result += "\u207F"
  627. case 0xfd:
  628. result += "\xc2\xb2"
  629. case 0xfe:
  630. result += "\u25A0"
  631. case 0xff:
  632. result += "\xc2\xa0"
  633. default:
  634. result += string(char)
  635. }
  636. }
  637. // fmt.Printf("\n")
  638. return result
  639. }