font-out.go 11 KB

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