tdfont.go 14 KB

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