font-out.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/binary"
  5. "flag"
  6. "fmt"
  7. "os"
  8. "strconv"
  9. "strings"
  10. )
  11. func ListFonts(filename string) {
  12. f, err := os.Open(filename)
  13. if err != nil {
  14. fmt.Printf("Open(%s): %s\n", filename, err)
  15. panic(err)
  16. }
  17. defer f.Close()
  18. tdfonts := make([]byte, 20)
  19. f.Read(tdfonts)
  20. for true {
  21. fontdef := make([]byte, 4)
  22. read, _ := f.Read(fontdef)
  23. if read != 4 {
  24. fmt.Println("*END*")
  25. break
  26. }
  27. fontname := make([]byte, 13)
  28. f.Read(fontname)
  29. Name := strings.Trim(string(fontname[1:]), "\x00")
  30. // fmt.Printf("Font: %s\n", Name)
  31. f.Read(fontdef)
  32. single := make([]byte, 1)
  33. var FontType int8
  34. binary.Read(f, binary.LittleEndian, &FontType)
  35. // f.Read(single) // FontType
  36. // FontType := int(single[0])
  37. fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  38. f.Read(single) // Spacing
  39. // blocksize := make([]byte, 2)
  40. // f.Read(blocksize)
  41. var BlockSize int16
  42. binary.Read(f, binary.LittleEndian, &BlockSize)
  43. // fmt.Printf("Size: %d / %x\n", BlockSize, BlockSize)
  44. letterOffsets := make([]int16, 94)
  45. binary.Read(f, binary.LittleEndian, &letterOffsets)
  46. if false {
  47. for idx, i := range letterOffsets {
  48. fmt.Printf(" %04X", i)
  49. if (idx+1)%10 == 0 {
  50. fmt.Println("")
  51. }
  52. }
  53. fmt.Println("")
  54. }
  55. data := make([]byte, BlockSize)
  56. binary.Read(f, binary.LittleEndian, &data)
  57. }
  58. }
  59. func byte_to_text(line []byte) string {
  60. var output string
  61. for _, ch := range line {
  62. output += fmt.Sprintf("0x%02x,", ch)
  63. }
  64. if len(output) > 0 {
  65. output = output[:len(output)-1]
  66. }
  67. return output
  68. }
  69. func text_to_hextext(line string) string {
  70. var output string
  71. // output = "\""
  72. for _, ch := range []byte(line) {
  73. // output += fmt.Sprintf("\\x%02x", ch)
  74. output += fmt.Sprintf("0x%02x,", ch)
  75. }
  76. if len(output) > 1 {
  77. output = output[:len(output)-1]
  78. }
  79. // output += "\""
  80. return output
  81. }
  82. func ExtractColor(name string, offsets []uint16, data []byte) {
  83. // fmt.Printf("Extract Color Font: %s\n", name)
  84. var indexes []int
  85. var blocks [][][]byte
  86. var current [][]byte
  87. var line []byte
  88. pos := 0
  89. for pos < len(data) {
  90. indexes = append(indexes, pos)
  91. current = make([][]byte, 0)
  92. line = make([]byte, 0)
  93. // We don't use these.
  94. // w = data[pos]
  95. // h = data[pos+1]
  96. pos += 2
  97. // process this character
  98. for pos < len(data) {
  99. ch := data[pos]
  100. pos++
  101. if ch == 0x00 {
  102. // end of character
  103. current = append(current, line)
  104. blocks = append(blocks, current)
  105. current = make([][]byte, 0)
  106. line = make([]byte, 0)
  107. break
  108. }
  109. if ch == 0x0d {
  110. // end of this character line
  111. current = append(current, line)
  112. line = make([]byte, 0)
  113. continue
  114. }
  115. if ch == 0x26 {
  116. // & descender mark
  117. continue
  118. }
  119. line = append(line, ch)
  120. color := data[pos]
  121. pos++
  122. line = append(line, color)
  123. }
  124. }
  125. // offset optimization:
  126. var single []int
  127. for _, o := range offsets {
  128. if o == 65535 {
  129. single = append(single, -1)
  130. continue
  131. }
  132. for idx, i := range indexes {
  133. if o == uint16(i) {
  134. single = append(single, idx)
  135. break
  136. }
  137. }
  138. }
  139. /*
  140. // Handle Names with spaces
  141. filename := fmt.Sprintf("%s_font.go", strings.Replace(name, " ", "", -1))
  142. fp, err := os.Create(filename)
  143. if err != nil {
  144. panic(err)
  145. }
  146. fmt.Printf("Writing: %s\n", filename)
  147. defer fp.Close()
  148. writer := bufio.NewWriter(fp)
  149. */
  150. writer := bufio.NewWriter(os.Stdout)
  151. // writer.WriteString("package main\n")
  152. writer.WriteString("// " + name + "\n\n")
  153. // Name := strings.ToUpper(name)
  154. Name := strings.Replace(name, " ", "", -1)
  155. writer.WriteString("func Font" + Name + "() door.ColorFont {\n")
  156. var output string
  157. output = " return door.ColorFont{Characters: []int{"
  158. for _, s := range single {
  159. output += strconv.Itoa(s) + ", "
  160. }
  161. output = output[:len(output)-2] + "},\n"
  162. writer.WriteString(output)
  163. writer.Flush()
  164. output = " Data: [][][]byte{"
  165. for _, blk := range blocks {
  166. output += "{"
  167. if len(blk) == 0 {
  168. output += "{},"
  169. } else {
  170. for _, inner := range blk {
  171. // output += text_to_hextext(b) + ","
  172. output += "{" + byte_to_text(inner) + "},"
  173. }
  174. output = output[:len(output)-1]
  175. }
  176. output += "},\n"
  177. writer.WriteString(output)
  178. output = " "
  179. }
  180. writer.WriteString(" }}\n")
  181. writer.WriteString("}\n")
  182. writer.Flush()
  183. }
  184. func ExtractBlock(name string, offsets []uint16, data []byte) {
  185. // fmt.Printf("Extract Block Font: %s\n", name)
  186. var indexes []int
  187. var blocks [][][]byte
  188. var current [][]byte
  189. var line []byte
  190. pos := 0
  191. for pos < len(data) {
  192. indexes = append(indexes, pos)
  193. current = make([][]byte, 0)
  194. line = make([]byte, 0)
  195. // We don't use these
  196. // w = data[pos]
  197. // h = data[pos+1]
  198. pos += 2
  199. // process this character
  200. for pos < len(data) {
  201. ch := data[pos]
  202. pos++
  203. if ch == 0x00 {
  204. // end of character
  205. current = append(current, line)
  206. blocks = append(blocks, current)
  207. current = make([][]byte, 0)
  208. line = make([]byte, 0)
  209. break
  210. }
  211. if ch == 0x0d {
  212. // end of this character line
  213. current = append(current, line)
  214. line = make([]byte, 0)
  215. continue
  216. }
  217. if ch == 0x26 {
  218. // & descender mark
  219. continue
  220. }
  221. line = append(line, ch)
  222. }
  223. }
  224. // offset optimization:
  225. var single []int
  226. for _, o := range offsets {
  227. if o == 65535 {
  228. single = append(single, -1)
  229. continue
  230. }
  231. for idx, i := range indexes {
  232. if o == uint16(i) {
  233. single = append(single, idx)
  234. break
  235. }
  236. }
  237. }
  238. /*
  239. // Handle Names with spaces
  240. filename := fmt.Sprintf("%s_font.go", strings.Replace(name, " ", "", -1))
  241. fp, err := os.Create(filename)
  242. if err != nil {
  243. panic(err)
  244. }
  245. fmt.Printf("Writing: %s\n", filename)
  246. defer fp.Close()
  247. writer := bufio.NewWriter(fp)
  248. */
  249. writer := bufio.NewWriter(os.Stdout)
  250. // Should this output routine be part of the BlockFont?
  251. // I think so!
  252. // writer.WriteString("package main\n")
  253. writer.WriteString("// " + name + "\n\n")
  254. // Name := strings.ToUpper(name)
  255. Name := strings.Replace(name, " ", "", -1)
  256. writer.WriteString("func Font" + Name + "() door.BlockFont {\n")
  257. var output string
  258. output = " return door.BlockFont{Characters: []int{"
  259. for _, s := range single {
  260. output += strconv.Itoa(s) + ", "
  261. }
  262. output = output[:len(output)-2] + "},\n"
  263. writer.WriteString(output)
  264. writer.Flush()
  265. output = " Data: [][][]byte{"
  266. for _, blk := range blocks {
  267. output += "{"
  268. if len(blk) == 0 {
  269. output += "{},"
  270. } else {
  271. for _, inner := range blk {
  272. output += "{" + byte_to_text(inner) + "},"
  273. }
  274. output = output[:len(output)-1]
  275. }
  276. output += "},\n"
  277. // output = output[:len(output)-1]
  278. // output += "},\n"
  279. writer.WriteString(output)
  280. output = " "
  281. }
  282. writer.WriteString(" }}\n")
  283. writer.WriteString("}\n")
  284. writer.Flush()
  285. }
  286. func ExtractFonts(filename string, fonts []string) {
  287. f, err := os.Open(filename)
  288. if err != nil {
  289. fmt.Printf("Open(%s): %s\n", filename, err)
  290. panic(err)
  291. }
  292. defer f.Close()
  293. tdfonts := make([]byte, 20)
  294. f.Read(tdfonts)
  295. for true {
  296. fontdef := make([]byte, 4)
  297. read, _ := f.Read(fontdef)
  298. if read != 4 {
  299. break
  300. }
  301. fontname := make([]byte, 13)
  302. f.Read(fontname)
  303. Name := strings.Trim(string(fontname[1:]), "\x00")
  304. // fmt.Printf("Font: %s\n", Name)
  305. f.Read(fontdef)
  306. single := make([]byte, 1)
  307. var FontType int8
  308. binary.Read(f, binary.LittleEndian, &FontType)
  309. // fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  310. f.Read(single) // Spacing
  311. var BlockSize int16
  312. binary.Read(f, binary.LittleEndian, &BlockSize)
  313. letterOffsets := make([]uint16, 94)
  314. binary.Read(f, binary.LittleEndian, &letterOffsets)
  315. if false {
  316. for idx, i := range letterOffsets {
  317. fmt.Printf(" %04X", i)
  318. if (idx+1)%10 == 0 {
  319. fmt.Println("")
  320. }
  321. }
  322. fmt.Println("")
  323. }
  324. data := make([]byte, BlockSize)
  325. binary.Read(f, binary.LittleEndian, &data)
  326. // Special case where they are asking for all fonts
  327. if len(fonts) == 1 && fonts[0] == "*" {
  328. switch FontType {
  329. case 1:
  330. ExtractBlock(Name, letterOffsets, data)
  331. case 2:
  332. ExtractColor(Name, letterOffsets, data)
  333. default:
  334. fmt.Printf("// Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  335. }
  336. } else {
  337. for _, f := range fonts {
  338. if Name == f {
  339. switch FontType {
  340. case 1:
  341. ExtractBlock(Name, letterOffsets, data)
  342. case 2:
  343. ExtractColor(Name, letterOffsets, data)
  344. default:
  345. fmt.Printf("// Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  346. }
  347. break
  348. }
  349. }
  350. }
  351. }
  352. }
  353. func main() {
  354. var fonts string
  355. var defaultPackage string = "main"
  356. var listFonts bool
  357. var allFonts bool
  358. flag.StringVar(&fonts, "f", "", "Font(s) to extract")
  359. flag.BoolVar(&allFonts, "a", false, "Extract All Fonts")
  360. flag.StringVar(&defaultPackage, "p", "main", "Package name to use")
  361. flag.BoolVar(&listFonts, "l", false, "List Fonts")
  362. flag.Parse()
  363. if flag.NArg() == 0 {
  364. fmt.Println("Font-Out - A TDF (TheDraw Font) file processor.")
  365. fmt.Println("No TDF filenames given.")
  366. flag.PrintDefaults()
  367. os.Exit(2)
  368. }
  369. if !listFonts {
  370. fmt.Printf("package %s\n\n", defaultPackage)
  371. fmt.Println("import (")
  372. fmt.Println(" \"red-green/door\"")
  373. fmt.Println(")")
  374. fmt.Println("")
  375. } else {
  376. fmt.Println("Font-Out - A TDF (TheDraw Font) file processor.")
  377. }
  378. var fontList []string
  379. if len(fonts) > 0 {
  380. fontList = strings.Split(fonts, ",")
  381. }
  382. if allFonts {
  383. fontList = make([]string, 0)
  384. fontList = append(fontList, "*")
  385. }
  386. if !listFonts && len(fontList) == 0 {
  387. fmt.Println("Default to -l (List Fonts)")
  388. listFonts = true
  389. }
  390. for _, fontfile := range flag.Args() {
  391. if listFonts {
  392. ListFonts(fontfile)
  393. } else {
  394. if len(fontList) > 0 {
  395. ExtractFonts(fontfile, fontList)
  396. }
  397. }
  398. }
  399. }