font-out.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. // Attempt to fix broken fonts.
  83. // This verifies that the character offsets are proceeded
  84. // by a null character.
  85. func FontFixup(offsets []uint16, data *[]byte) bool {
  86. fixed := false
  87. for _, offset := range offsets {
  88. if offset == 65535 {
  89. continue
  90. }
  91. if offset == 0 {
  92. continue
  93. }
  94. if (*data)[offset-1] != 0 {
  95. (*data)[offset-1] = 0
  96. fixed = true
  97. }
  98. }
  99. return fixed
  100. }
  101. func ExtractColor(name string, offsets []uint16, data []byte) {
  102. fmt.Printf("Extract Color Font: %s\n", name)
  103. var indexes []int
  104. var blocks [][][]byte
  105. var current [][]byte
  106. w := -1
  107. h := -1
  108. ch := -1
  109. var line []byte
  110. for idx, b := range data {
  111. if w == -1 {
  112. indexes = append(indexes, idx)
  113. current = make([][]byte, 0)
  114. line = make([]byte, 0)
  115. w = int(b)
  116. continue
  117. }
  118. if h == -1 {
  119. h = int(b)
  120. continue
  121. }
  122. if ch == -1 {
  123. ch = int(b)
  124. if ch == 0x0d {
  125. ch = -1
  126. current = append(current, line)
  127. line = make([]byte, 0)
  128. continue
  129. }
  130. if ch == 0 {
  131. current = append(current, line)
  132. blocks = append(blocks, current)
  133. current = make([][]byte, 0)
  134. line = make([]byte, 0)
  135. w = -1
  136. h = -1
  137. ch = -1
  138. continue
  139. }
  140. if ch == 0x26 {
  141. // & descender mark
  142. ch = -1
  143. continue
  144. }
  145. // line += string(rune(ch))
  146. line = append(line, byte(ch))
  147. ch = -1
  148. }
  149. }
  150. // offset optimization:
  151. var single []int
  152. for _, o := range offsets {
  153. if o == 65535 {
  154. single = append(single, -1)
  155. continue
  156. }
  157. found := false
  158. for idx, i := range indexes {
  159. if o == uint16(i) {
  160. single = append(single, idx)
  161. found = true
  162. break
  163. }
  164. }
  165. if !found {
  166. fmt.Printf("Unable to locate index %d / %x (font corrupted)", o, o)
  167. return
  168. }
  169. }
  170. // Handle Names with spaces
  171. filename := fmt.Sprintf("%s_font.go", strings.Replace(name, " ", "", -1))
  172. fp, err := os.Create(filename)
  173. if err != nil {
  174. panic(err)
  175. }
  176. fmt.Printf("Writing: %s\n", filename)
  177. defer fp.Close()
  178. writer := bufio.NewWriter(fp)
  179. // writer.WriteString("package main\n")
  180. writer.WriteString("// " + name + "\n\n")
  181. // Name := strings.ToUpper(name)
  182. Name := strings.Replace(name, " ", "", -1)
  183. writer.WriteString("func Font" + Name + "() ColorFont {\n")
  184. var output string
  185. output = " return ColorFont{characters: []int{"
  186. for _, s := range single {
  187. output += strconv.Itoa(s) + ", "
  188. }
  189. output = output[:len(output)-2] + "},\n"
  190. writer.WriteString(output)
  191. writer.Flush()
  192. output = " data: [][][]byte{"
  193. for _, blk := range blocks {
  194. output += "{"
  195. if len(blk) == 0 {
  196. output += "{},"
  197. } else {
  198. for _, inner := range blk {
  199. // output += text_to_hextext(b) + ","
  200. output += "{" + byte_to_text(inner) + "},"
  201. }
  202. output = output[:len(output)-1]
  203. }
  204. output += "},\n"
  205. writer.WriteString(output)
  206. output = " "
  207. }
  208. writer.WriteString(" }}\n")
  209. writer.WriteString("}\n")
  210. writer.Flush()
  211. }
  212. func ExtractBlock(name string, offsets []uint16, data []byte) {
  213. fmt.Printf("Extract Block Font: %s\n", name)
  214. var indexes []int
  215. var blocks [][][]byte
  216. var current [][]byte
  217. w := -1
  218. h := -1
  219. ch := -1
  220. var line []byte
  221. for idx, b := range data {
  222. if w == -1 {
  223. indexes = append(indexes, idx)
  224. current = make([][]byte, 0)
  225. line = make([]byte, 0)
  226. w = int(b)
  227. continue
  228. }
  229. if h == -1 {
  230. h = int(b)
  231. continue
  232. }
  233. if ch == -1 {
  234. ch = int(b)
  235. if ch == 0x0d {
  236. ch = -1
  237. current = append(current, line)
  238. line = make([]byte, 0)
  239. continue
  240. }
  241. if ch == 0 {
  242. current = append(current, line)
  243. blocks = append(blocks, current)
  244. current = make([][]byte, 0)
  245. line = make([]byte, 0)
  246. w = -1
  247. h = -1
  248. ch = -1
  249. continue
  250. }
  251. if ch == 0x26 {
  252. // & descender mark
  253. ch = -1
  254. continue
  255. }
  256. line = append(line, byte(ch))
  257. ch = -1
  258. }
  259. }
  260. // offset optimization:
  261. var single []int
  262. for _, o := range offsets {
  263. if o == 65535 {
  264. single = append(single, -1)
  265. continue
  266. }
  267. found := false
  268. for idx, i := range indexes {
  269. if o == uint16(i) {
  270. single = append(single, idx)
  271. found = true
  272. break
  273. }
  274. }
  275. if !found {
  276. fmt.Printf("Unable to locate index %d / %x (font corrupted)", o, o)
  277. return
  278. }
  279. }
  280. // Handle Names with spaces
  281. filename := fmt.Sprintf("%s_font.go", strings.Replace(name, " ", "", -1))
  282. fp, err := os.Create(filename)
  283. if err != nil {
  284. panic(err)
  285. }
  286. fmt.Printf("Writing: %s\n", filename)
  287. defer fp.Close()
  288. writer := bufio.NewWriter(fp)
  289. // writer.WriteString("package main\n")
  290. writer.WriteString("// " + name + "\n\n")
  291. // Name := strings.ToUpper(name)
  292. Name := strings.Replace(name, " ", "", -1)
  293. writer.WriteString("func Font" + Name + "() BlockFont {\n")
  294. var output string
  295. output = " return BlockFont{characters: []int{"
  296. for _, s := range single {
  297. output += strconv.Itoa(s) + ", "
  298. }
  299. output = output[:len(output)-2] + "},\n"
  300. writer.WriteString(output)
  301. writer.Flush()
  302. output = " data: [][][]byte{"
  303. for _, blk := range blocks {
  304. output += "{"
  305. if len(blk) == 0 {
  306. output += "{},"
  307. } else {
  308. for _, inner := range blk {
  309. output += "{" + byte_to_text(inner) + "},"
  310. }
  311. output = output[:len(output)-1]
  312. }
  313. output += "},\n"
  314. // output = output[:len(output)-1]
  315. // output += "},\n"
  316. writer.WriteString(output)
  317. output = " "
  318. }
  319. writer.WriteString(" }}\n")
  320. writer.WriteString("}\n")
  321. writer.Flush()
  322. }
  323. func ExtractFonts(filename string, fonts []string) {
  324. f, err := os.Open(filename)
  325. if err != nil {
  326. fmt.Printf("Open(%s): %s\n", filename, err)
  327. panic(err)
  328. }
  329. defer f.Close()
  330. tdfonts := make([]byte, 20)
  331. f.Read(tdfonts)
  332. for true {
  333. fontdef := make([]byte, 4)
  334. read, _ := f.Read(fontdef)
  335. if read != 4 {
  336. break
  337. }
  338. fontname := make([]byte, 13)
  339. f.Read(fontname)
  340. Name := strings.Trim(string(fontname[1:]), "\x00")
  341. // fmt.Printf("Font: %s\n", Name)
  342. f.Read(fontdef)
  343. single := make([]byte, 1)
  344. var FontType int8
  345. binary.Read(f, binary.LittleEndian, &FontType)
  346. // fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  347. f.Read(single) // Spacing
  348. var BlockSize int16
  349. binary.Read(f, binary.LittleEndian, &BlockSize)
  350. letterOffsets := make([]uint16, 94)
  351. binary.Read(f, binary.LittleEndian, &letterOffsets)
  352. if false {
  353. for idx, i := range letterOffsets {
  354. fmt.Printf(" %04X", i)
  355. if (idx+1)%10 == 0 {
  356. fmt.Println("")
  357. }
  358. }
  359. fmt.Println("")
  360. }
  361. data := make([]byte, BlockSize)
  362. binary.Read(f, binary.LittleEndian, &data)
  363. if FontFixup(letterOffsets, &data) {
  364. fmt.Printf("Attempting to *FIX* Font %s\n", Name)
  365. }
  366. // Special case where they are asking for all fonts
  367. if len(fonts) == 1 && fonts[0] == "*" {
  368. switch FontType {
  369. case 1:
  370. ExtractBlock(Name, letterOffsets, data)
  371. case 2:
  372. ExtractColor(Name, letterOffsets, data)
  373. default:
  374. fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  375. }
  376. } else {
  377. for _, f := range fonts {
  378. if Name == f {
  379. switch FontType {
  380. case 1:
  381. ExtractBlock(Name, letterOffsets, data)
  382. case 2:
  383. ExtractColor(Name, letterOffsets, data)
  384. default:
  385. fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  386. }
  387. break
  388. }
  389. }
  390. }
  391. }
  392. }
  393. func main() {
  394. fmt.Println("Font-Out - A TDF (TheDraw Font) file processor.")
  395. var fontfile string
  396. var fonts string
  397. var defaultPackage string = "main"
  398. var listFonts bool
  399. var allFonts bool
  400. flag.StringVar(&fontfile, "tdf", "", "TheDraw Font File")
  401. flag.StringVar(&fonts, "f", "", "Font(s) to extract")
  402. flag.BoolVar(&allFonts, "a", false, "Extract All Fonts")
  403. flag.StringVar(&defaultPackage, "p", "main", "Package name to use")
  404. flag.BoolVar(&listFonts, "l", false, "List Fonts")
  405. flag.Parse()
  406. if len(fontfile) == 0 {
  407. fmt.Println("I need a TDF filename.")
  408. flag.PrintDefaults()
  409. os.Exit(2)
  410. }
  411. if listFonts {
  412. ListFonts(fontfile)
  413. }
  414. var fontList []string
  415. if len(fonts) > 0 {
  416. fontList = strings.Split(fonts, ",")
  417. }
  418. if allFonts {
  419. fontList = make([]string, 0)
  420. fontList = append(fontList, "*")
  421. }
  422. if len(fontList) > 0 {
  423. ExtractFonts(fontfile, fontList)
  424. }
  425. }