tdfont.go 14 KB

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