tdfont.go 14 KB

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