font-out.go 8.9 KB

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