font-out.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/binary"
  5. "flag"
  6. "fmt"
  7. "os"
  8. "strconv"
  9. "strings"
  10. )
  11. var Conversion [][2]int
  12. // copied from tdfont.go, and modified
  13. func thedraw_to_ansi(c int) int {
  14. trans := []int{0, 4, 2, 6, 1, 5, 3, 7}
  15. // 0, 1, 2, 3, 4, 5, 6, 7
  16. return trans[c]
  17. }
  18. func MatchStyle(color byte, look byte) int {
  19. var match int = 0
  20. if ((color >> 4) & 0x07) == look {
  21. // Top
  22. match |= 1
  23. }
  24. if (color & 0x07) == look {
  25. // Bottom
  26. match |= 2
  27. }
  28. return match
  29. }
  30. func PatchColor(color byte, new_color byte, style int) byte {
  31. var c byte = color
  32. if style&1 == 1 {
  33. c = (c & 0x8f) | new_color<<4
  34. }
  35. if style&2 == 2 {
  36. c = (c & 0xf8) | new_color
  37. }
  38. return c
  39. }
  40. func Scan(block [][][]byte, find_color int) map[[2]int][][2]int {
  41. var Targets map[[2]int][][2]int = make(map[[2]int][][2]int, 0)
  42. // Scan the font looking for the given color FG/BG
  43. // Covert color code to TheDraw Color
  44. actual := byte(thedraw_to_ansi(find_color))
  45. for charIndex := range block {
  46. for lineIndex := range block[charIndex] {
  47. var found bool = false
  48. var patches [][2]int = make([][2]int, 0)
  49. for offset := 1; offset < len(block[charIndex][lineIndex]); offset += 2 {
  50. color := block[charIndex][lineIndex][offset]
  51. style := MatchStyle(color, actual)
  52. if style != 0 {
  53. // log.Printf("color: %x actual %x style: %d\n", color, actual, style)
  54. patches = append(patches, [2]int{offset, style})
  55. found = true
  56. }
  57. }
  58. if found {
  59. pos := [2]int{charIndex, lineIndex}
  60. Targets[pos] = make([][2]int, len(patches))
  61. for i := range patches {
  62. Targets[pos][i] = patches[i]
  63. }
  64. // Targets[pos] = patches
  65. }
  66. }
  67. }
  68. return Targets
  69. }
  70. func Modify(block [][][]byte, new_color int, Targets map[[2]int][][2]int) {
  71. // Covert color code to TheDraw Color
  72. actual := byte(thedraw_to_ansi(new_color))
  73. for pos, patch := range Targets {
  74. for _, p := range patch {
  75. block[pos[0]][pos[1]][p[0]] = PatchColor(block[pos[0]][pos[1]][p[0]], actual, p[1])
  76. }
  77. }
  78. }
  79. func ListFonts(filename string) {
  80. f, err := os.Open(filename)
  81. if err != nil {
  82. fmt.Printf("Open(%s): %s\n", filename, err)
  83. panic(err)
  84. }
  85. defer f.Close()
  86. tdfonts := make([]byte, 20)
  87. f.Read(tdfonts)
  88. for true {
  89. fontdef := make([]byte, 4)
  90. read, _ := f.Read(fontdef)
  91. if read != 4 {
  92. fmt.Println("*END*")
  93. break
  94. }
  95. fontname := make([]byte, 13)
  96. f.Read(fontname)
  97. Name := strings.Trim(string(fontname[1:]), "\x00")
  98. // fmt.Printf("Font: %s\n", Name)
  99. f.Read(fontdef)
  100. single := make([]byte, 1)
  101. var FontType int8
  102. binary.Read(f, binary.LittleEndian, &FontType)
  103. // f.Read(single) // FontType
  104. // FontType := int(single[0])
  105. fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  106. f.Read(single) // Spacing
  107. // blocksize := make([]byte, 2)
  108. // f.Read(blocksize)
  109. var BlockSize int16
  110. binary.Read(f, binary.LittleEndian, &BlockSize)
  111. // fmt.Printf("Size: %d / %x\n", BlockSize, BlockSize)
  112. letterOffsets := make([]int16, 94)
  113. binary.Read(f, binary.LittleEndian, &letterOffsets)
  114. if false {
  115. for idx, i := range letterOffsets {
  116. fmt.Printf(" %04X", i)
  117. if (idx+1)%10 == 0 {
  118. fmt.Println("")
  119. }
  120. }
  121. fmt.Println("")
  122. }
  123. data := make([]byte, BlockSize)
  124. binary.Read(f, binary.LittleEndian, &data)
  125. }
  126. }
  127. func byte_to_text(line []byte) string {
  128. var output string
  129. for _, ch := range line {
  130. output += fmt.Sprintf("0x%02x,", ch)
  131. }
  132. if len(output) > 0 {
  133. output = output[:len(output)-1]
  134. }
  135. return output
  136. }
  137. func text_to_hextext(line string) string {
  138. var output string
  139. // output = "\""
  140. for _, ch := range []byte(line) {
  141. // output += fmt.Sprintf("\\x%02x", ch)
  142. output += fmt.Sprintf("0x%02x,", ch)
  143. }
  144. if len(output) > 1 {
  145. output = output[:len(output)-1]
  146. }
  147. // output += "\""
  148. return output
  149. }
  150. func ExtractColor(name string, offsets []uint16, data []byte) {
  151. // fmt.Printf("Extract Color Font: %s\n", name)
  152. var indexes []int
  153. var blocks [][][]byte
  154. var current [][]byte
  155. var line []byte
  156. pos := 0
  157. for pos < len(data) {
  158. indexes = append(indexes, pos)
  159. current = make([][]byte, 0)
  160. line = make([]byte, 0)
  161. // We don't use these.
  162. // w = data[pos]
  163. // h = data[pos+1]
  164. pos += 2
  165. // process this character
  166. for pos < len(data) {
  167. ch := data[pos]
  168. pos++
  169. if ch == 0x00 {
  170. // end of character
  171. current = append(current, line)
  172. blocks = append(blocks, current)
  173. current = make([][]byte, 0)
  174. line = make([]byte, 0)
  175. break
  176. }
  177. if ch == 0x0d {
  178. // end of this character line
  179. current = append(current, line)
  180. line = make([]byte, 0)
  181. continue
  182. }
  183. if ch == 0x26 {
  184. // & descender mark
  185. continue
  186. }
  187. line = append(line, ch)
  188. color := data[pos]
  189. pos++
  190. line = append(line, color)
  191. }
  192. }
  193. // offset optimization:
  194. var single []int
  195. for _, o := range offsets {
  196. if o == 65535 {
  197. single = append(single, -1)
  198. continue
  199. }
  200. for idx, i := range indexes {
  201. if o == uint16(i) {
  202. single = append(single, idx)
  203. break
  204. }
  205. }
  206. }
  207. /*
  208. // Handle Names with spaces
  209. filename := fmt.Sprintf("%s_font.go", strings.Replace(name, " ", "", -1))
  210. fp, err := os.Create(filename)
  211. if err != nil {
  212. panic(err)
  213. }
  214. fmt.Printf("Writing: %s\n", filename)
  215. defer fp.Close()
  216. writer := bufio.NewWriter(fp)
  217. */
  218. writer := bufio.NewWriter(os.Stdout)
  219. // writer.WriteString("package main\n")
  220. writer.WriteString("// " + name + "\n\n")
  221. // Name := strings.ToUpper(name)
  222. Name := strings.Replace(name, " ", "", -1)
  223. writer.WriteString("func Font" + Name + "() door.ColorFont {\n")
  224. var output string
  225. output = " return door.ColorFont{Characters: []int{"
  226. for _, s := range single {
  227. output += strconv.Itoa(s) + ", "
  228. }
  229. output = output[:len(output)-2] + "},\n"
  230. writer.WriteString(output)
  231. writer.Flush()
  232. if len(Conversion) > 0 {
  233. // Color Convert time!
  234. var Maps []map[[2]int][][2]int = make([]map[[2]int][][2]int, len(Conversion))
  235. for idx, codes := range Conversion {
  236. Maps[idx] = Scan(blocks, codes[0])
  237. }
  238. for idx, codes := range Conversion {
  239. Modify(blocks, codes[1], Maps[idx])
  240. }
  241. }
  242. output = " Data: [][][]byte{"
  243. for _, blk := range blocks {
  244. output += "{"
  245. if len(blk) == 0 {
  246. output += "{},"
  247. } else {
  248. for _, inner := range blk {
  249. // output += text_to_hextext(b) + ","
  250. output += "{" + byte_to_text(inner) + "},"
  251. }
  252. output = output[:len(output)-1]
  253. }
  254. output += "},\n"
  255. writer.WriteString(output)
  256. output = " "
  257. }
  258. writer.WriteString(" }}\n")
  259. writer.WriteString("}\n")
  260. writer.Flush()
  261. }
  262. func ExtractBlock(name string, offsets []uint16, data []byte) {
  263. // fmt.Printf("Extract Block Font: %s\n", name)
  264. var indexes []int
  265. var blocks [][][]byte
  266. var current [][]byte
  267. var line []byte
  268. pos := 0
  269. for pos < len(data) {
  270. indexes = append(indexes, pos)
  271. current = make([][]byte, 0)
  272. line = make([]byte, 0)
  273. // We don't use these
  274. // w = data[pos]
  275. // h = data[pos+1]
  276. pos += 2
  277. // process this character
  278. for pos < len(data) {
  279. ch := data[pos]
  280. pos++
  281. if ch == 0x00 {
  282. // end of character
  283. current = append(current, line)
  284. blocks = append(blocks, current)
  285. current = make([][]byte, 0)
  286. line = make([]byte, 0)
  287. break
  288. }
  289. if ch == 0x0d {
  290. // end of this character line
  291. current = append(current, line)
  292. line = make([]byte, 0)
  293. continue
  294. }
  295. if ch == 0x26 {
  296. // & descender mark
  297. continue
  298. }
  299. line = append(line, ch)
  300. }
  301. }
  302. // offset optimization:
  303. var single []int
  304. for _, o := range offsets {
  305. if o == 65535 {
  306. single = append(single, -1)
  307. continue
  308. }
  309. for idx, i := range indexes {
  310. if o == uint16(i) {
  311. single = append(single, idx)
  312. break
  313. }
  314. }
  315. }
  316. /*
  317. // Handle Names with spaces
  318. filename := fmt.Sprintf("%s_font.go", strings.Replace(name, " ", "", -1))
  319. fp, err := os.Create(filename)
  320. if err != nil {
  321. panic(err)
  322. }
  323. fmt.Printf("Writing: %s\n", filename)
  324. defer fp.Close()
  325. writer := bufio.NewWriter(fp)
  326. */
  327. writer := bufio.NewWriter(os.Stdout)
  328. // Should this output routine be part of the BlockFont?
  329. // I think so!
  330. // writer.WriteString("package main\n")
  331. writer.WriteString("// " + name + "\n\n")
  332. // Name := strings.ToUpper(name)
  333. Name := strings.Replace(name, " ", "", -1)
  334. writer.WriteString("func Font" + Name + "() door.BlockFont {\n")
  335. var output string
  336. output = " return door.BlockFont{Characters: []int{"
  337. for _, s := range single {
  338. output += strconv.Itoa(s) + ", "
  339. }
  340. output = output[:len(output)-2] + "},\n"
  341. writer.WriteString(output)
  342. writer.Flush()
  343. output = " Data: [][][]byte{"
  344. for _, blk := range blocks {
  345. output += "{"
  346. if len(blk) == 0 {
  347. output += "{},"
  348. } else {
  349. for _, inner := range blk {
  350. output += "{" + byte_to_text(inner) + "},"
  351. }
  352. output = output[:len(output)-1]
  353. }
  354. output += "},\n"
  355. // output = output[:len(output)-1]
  356. // output += "},\n"
  357. writer.WriteString(output)
  358. output = " "
  359. }
  360. writer.WriteString(" }}\n")
  361. writer.WriteString("}\n")
  362. writer.Flush()
  363. }
  364. func ExtractFonts(filename string, fonts []string) {
  365. f, err := os.Open(filename)
  366. if err != nil {
  367. fmt.Printf("Open(%s): %s\n", filename, err)
  368. panic(err)
  369. }
  370. defer f.Close()
  371. tdfonts := make([]byte, 20)
  372. f.Read(tdfonts)
  373. for true {
  374. fontdef := make([]byte, 4)
  375. read, _ := f.Read(fontdef)
  376. if read != 4 {
  377. break
  378. }
  379. fontname := make([]byte, 13)
  380. f.Read(fontname)
  381. Name := strings.Trim(string(fontname[1:]), "\x00")
  382. // fmt.Printf("Font: %s\n", Name)
  383. f.Read(fontdef)
  384. single := make([]byte, 1)
  385. var FontType int8
  386. binary.Read(f, binary.LittleEndian, &FontType)
  387. // fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  388. f.Read(single) // Spacing
  389. var BlockSize int16
  390. binary.Read(f, binary.LittleEndian, &BlockSize)
  391. letterOffsets := make([]uint16, 94)
  392. binary.Read(f, binary.LittleEndian, &letterOffsets)
  393. if false {
  394. for idx, i := range letterOffsets {
  395. fmt.Printf(" %04X", i)
  396. if (idx+1)%10 == 0 {
  397. fmt.Println("")
  398. }
  399. }
  400. fmt.Println("")
  401. }
  402. data := make([]byte, BlockSize)
  403. binary.Read(f, binary.LittleEndian, &data)
  404. // Special case where they are asking for all fonts
  405. if len(fonts) == 1 && fonts[0] == "*" {
  406. switch FontType {
  407. case 1:
  408. ExtractBlock(Name, letterOffsets, data)
  409. case 2:
  410. ExtractColor(Name, letterOffsets, data)
  411. default:
  412. fmt.Printf("// Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  413. }
  414. } else {
  415. for _, f := range fonts {
  416. if Name == f {
  417. switch FontType {
  418. case 1:
  419. ExtractBlock(Name, letterOffsets, data)
  420. case 2:
  421. ExtractColor(Name, letterOffsets, data)
  422. default:
  423. fmt.Printf("// Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  424. }
  425. break
  426. }
  427. }
  428. }
  429. }
  430. }
  431. // Created so that multiple inputs can be accecpted
  432. type arrayFlags []string
  433. func (i *arrayFlags) String() string {
  434. // change this, this is just can example to satisfy the interface
  435. result := ""
  436. for _, str := range *i {
  437. if result != "" {
  438. result += ", "
  439. }
  440. result += str
  441. }
  442. return result
  443. }
  444. func (i *arrayFlags) Set(value string) error {
  445. *i = append(*i, strings.TrimSpace(value))
  446. return nil
  447. }
  448. func ParseColorConvert(convert arrayFlags) {
  449. Conversion = make([][2]int, 0)
  450. if len(convert) > 0 {
  451. // Something to do
  452. for _, color := range convert {
  453. split := strings.Split(color, ",")
  454. v1, _ := strconv.Atoi(split[0])
  455. v2, _ := strconv.Atoi(split[1])
  456. Conversion = append(Conversion, [2]int{v1, v2})
  457. }
  458. }
  459. }
  460. func main() {
  461. var fonts string
  462. var defaultPackage string = "main"
  463. var listFonts bool
  464. var allFonts bool
  465. var convert arrayFlags
  466. flag.StringVar(&fonts, "f", "", "Font(s) to extract")
  467. flag.BoolVar(&allFonts, "a", false, "Extract All Fonts")
  468. flag.StringVar(&defaultPackage, "p", "main", "Package name to use")
  469. flag.BoolVar(&listFonts, "l", false, "List Fonts")
  470. flag.Var(&convert, "c", "Convert Color to Color n,n")
  471. flag.Parse()
  472. ParseColorConvert(convert)
  473. // fmt.Printf("Convert: %#v\n", convert)
  474. // fmt.Printf("Conversion: %#v\n", Conversion)
  475. if flag.NArg() == 0 {
  476. fmt.Println("Font-Out - A TDF (TheDraw Font) file processor.")
  477. fmt.Println("No TDF filenames given.")
  478. flag.PrintDefaults()
  479. os.Exit(2)
  480. }
  481. if !listFonts {
  482. fmt.Printf("package %s\n\n", defaultPackage)
  483. fmt.Println("import (")
  484. fmt.Println(" \"red-green/door\"")
  485. fmt.Println(")")
  486. fmt.Println("")
  487. } else {
  488. fmt.Println("Font-Out - A TDF (TheDraw Font) file processor.")
  489. }
  490. var fontList []string
  491. if len(fonts) > 0 {
  492. fontList = strings.Split(fonts, ",")
  493. }
  494. if allFonts {
  495. fontList = make([]string, 0)
  496. fontList = append(fontList, "*")
  497. }
  498. if !listFonts && len(fontList) == 0 {
  499. fmt.Println("Default to -l (List Fonts)")
  500. listFonts = true
  501. }
  502. for _, fontfile := range flag.Args() {
  503. if listFonts {
  504. ListFonts(fontfile)
  505. } else {
  506. if len(fontList) > 0 {
  507. ExtractFonts(fontfile, fontList)
  508. }
  509. }
  510. }
  511. }