font-show.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. package main
  2. import (
  3. "encoding/binary"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "red-green/door"
  8. "regexp"
  9. "strings"
  10. )
  11. // Given a TheDrawFont file, display the fonts contained within.
  12. func ListFonts(filename string) {
  13. f, err := os.Open(filename)
  14. if err != nil {
  15. fmt.Printf("Open(%s): %s\n", filename, err)
  16. panic(err)
  17. }
  18. defer f.Close()
  19. tdfonts := make([]byte, 20)
  20. f.Read(tdfonts)
  21. for true {
  22. fontdef := make([]byte, 4)
  23. read, _ := f.Read(fontdef)
  24. if read != 4 {
  25. fmt.Println("*END*")
  26. break
  27. }
  28. fontname := make([]byte, 13)
  29. f.Read(fontname)
  30. Name := strings.Trim(string(fontname[1:]), "\x00")
  31. // fmt.Printf("Font: %s\n", Name)
  32. f.Read(fontdef)
  33. single := make([]byte, 1)
  34. var FontType int8
  35. binary.Read(f, binary.LittleEndian, &FontType)
  36. fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  37. f.Read(single) // Spacing
  38. var BlockSize int16
  39. binary.Read(f, binary.LittleEndian, &BlockSize)
  40. // fmt.Printf("Size: %d / %x\n", BlockSize, BlockSize)
  41. letterOffsets := make([]int16, 94)
  42. binary.Read(f, binary.LittleEndian, &letterOffsets)
  43. if false {
  44. for idx, i := range letterOffsets {
  45. fmt.Printf(" %04X", i)
  46. if (idx+1)%10 == 0 {
  47. fmt.Println("")
  48. }
  49. }
  50. fmt.Println("")
  51. }
  52. // Or possibly, seek past the character data
  53. data := make([]byte, BlockSize)
  54. binary.Read(f, binary.LittleEndian, &data)
  55. }
  56. }
  57. // Properly convert bytes to string
  58. // This is easy to get wrong, and string([]byte) might not work.
  59. // (Work right, that is.)
  60. func byte_to_text(line []byte) string {
  61. var output string
  62. for _, ch := range line {
  63. output += fmt.Sprintf("0x%02x,", ch)
  64. }
  65. if len(output) > 0 {
  66. output = output[:len(output)-1]
  67. }
  68. return output
  69. }
  70. // Convert string to hex values for initializing []byte{}
  71. func text_to_hextext(line string) string {
  72. var output string
  73. // output = "\""
  74. for _, ch := range []byte(line) {
  75. output += fmt.Sprintf("0x%02x,", ch)
  76. }
  77. if len(output) > 1 {
  78. output = output[:len(output)-1]
  79. }
  80. // output += "\""
  81. return output
  82. }
  83. // Attempt to fix broken fonts.
  84. // This verifies that the character offsets are proceeded
  85. // by a null character.
  86. func FontFixup(offsets []uint16, data *[]byte) bool {
  87. fixed := false
  88. for _, offset := range offsets {
  89. if offset == 65535 {
  90. continue
  91. }
  92. if offset == 0 {
  93. continue
  94. }
  95. if (*data)[offset-1] != 0 {
  96. (*data)[offset-1] = 0
  97. fixed = true
  98. }
  99. }
  100. return fixed
  101. }
  102. func ExtractColor(name string, offsets []uint16, data []byte) (Font door.ColorFont) {
  103. defer func() {
  104. if r := recover(); r != nil {
  105. // Ok, this failed
  106. Font = door.ColorFont{}
  107. }
  108. }()
  109. var indexes []int
  110. var blocks [][][]byte
  111. var current [][]byte
  112. var line []byte
  113. pos := 0
  114. for pos < len(data) {
  115. indexes = append(indexes, pos)
  116. current = make([][]byte, 0)
  117. line = make([]byte, 0)
  118. // We don't use these.
  119. // w = data[pos]
  120. // h = data[pos+1]
  121. pos += 2
  122. // process this character
  123. for pos < len(data) {
  124. ch := data[pos]
  125. pos++
  126. if ch == 0x00 {
  127. // end of character
  128. current = append(current, line)
  129. blocks = append(blocks, current)
  130. current = make([][]byte, 0)
  131. line = make([]byte, 0)
  132. break
  133. }
  134. if ch == 0x0d {
  135. // end of this character line
  136. current = append(current, line)
  137. line = make([]byte, 0)
  138. continue
  139. }
  140. if ch == 0x26 {
  141. // & descender mark
  142. continue
  143. }
  144. line = append(line, ch)
  145. color := data[pos]
  146. pos++
  147. line = append(line, color)
  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. panic(fmt.Sprintf("Unable to locate index %d / %x (font appears corrupted)", o, o))
  167. }
  168. }
  169. font := door.ColorFont{}
  170. font.Characters = single
  171. font.Data = blocks
  172. return font
  173. }
  174. func ExtractBlock(name string, offsets []uint16, data []byte) (Font door.BlockFont) {
  175. defer func() {
  176. if r := recover(); r != nil {
  177. // Ok, this failed
  178. Font = door.BlockFont{}
  179. }
  180. }()
  181. // fmt.Printf("Extract Block Font: %s\n", name)
  182. var indexes []int
  183. var blocks [][][]byte
  184. var current [][]byte
  185. var line []byte
  186. pos := 0
  187. for pos < len(data) {
  188. indexes = append(indexes, pos)
  189. current = make([][]byte, 0)
  190. line = make([]byte, 0)
  191. // We don't use these
  192. // w = data[pos]
  193. // h = data[pos+1]
  194. pos += 2
  195. // process this character
  196. for pos < len(data) {
  197. ch := data[pos]
  198. pos++
  199. if ch == 0x00 {
  200. // end of character
  201. current = append(current, line)
  202. blocks = append(blocks, current)
  203. current = make([][]byte, 0)
  204. line = make([]byte, 0)
  205. break
  206. }
  207. if ch == 0x0d {
  208. // end of this character line
  209. current = append(current, line)
  210. line = make([]byte, 0)
  211. continue
  212. }
  213. if ch == 0x26 {
  214. // & descender mark
  215. continue
  216. }
  217. line = append(line, ch)
  218. }
  219. }
  220. // offset optimization:
  221. var single []int
  222. for _, o := range offsets {
  223. if o == 65535 {
  224. single = append(single, -1)
  225. continue
  226. }
  227. found := false
  228. for idx, i := range indexes {
  229. if o == uint16(i) {
  230. single = append(single, idx)
  231. found = true
  232. break
  233. }
  234. }
  235. if !found {
  236. panic(fmt.Sprintf("Unable to locate index %d / %x (font appears corrupted)", o, o))
  237. }
  238. }
  239. font := door.BlockFont{}
  240. font.Characters = single
  241. font.Data = blocks
  242. return font
  243. }
  244. func Show(parts []string) {
  245. reset := "\x1b[0m"
  246. if len(parts) > 0 {
  247. for _, line := range parts {
  248. // fmt.Printf("%s%s\n", door.CP437_to_Unicode(string(line)), reset)
  249. fmt.Printf("%s%s\n", line, reset)
  250. }
  251. fmt.Println("")
  252. }
  253. }
  254. // Examine character indexes, are there lower case letter? are they unique?
  255. // What are the available characters in this font?
  256. func FontInfo(characters []int) (lower bool, lowerUnique bool, available string) {
  257. // Does it have lowercase?
  258. // Is lowercase unique?
  259. // Available characters in font
  260. // 33 -126
  261. lower = characters[int(rune('a')-33)] != -1
  262. lowerUnique = characters[int(rune('A')-33)] != characters[int(rune('a')-33)]
  263. for idx, offset := range characters {
  264. char := idx + 33
  265. if offset != -1 {
  266. available += string(char)
  267. }
  268. }
  269. return
  270. }
  271. func ShowBlockFont(name string, bf *door.BlockFont) {
  272. low, uniq, avail := FontInfo(bf.Characters)
  273. fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
  274. output, _ := bf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  275. Show(output)
  276. if low && uniq {
  277. output, _ := bf.Output("abcdefghijklmnopqrstuvwxyz")
  278. Show(output)
  279. }
  280. leftovers := avail
  281. reg, _ := regexp.Compile("[a-zA-Z]+")
  282. left := reg.ReplaceAllString(leftovers, "")
  283. // output, _ = bf.Output("abcdef")
  284. // Show(output)
  285. if len(left) > 0 {
  286. output, _ = bf.Output(left)
  287. Show(output)
  288. }
  289. }
  290. func ShowColorFont(name string, cf *door.ColorFont) {
  291. low, uniq, avail := FontInfo(cf.Characters)
  292. fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
  293. // fmt.Printf("Font: %s\n", name)
  294. output, _ := cf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  295. Show(output)
  296. if low && uniq {
  297. output, _ := cf.Output("abcdefghijklmnopqrstuvwxyz")
  298. Show(output)
  299. }
  300. leftovers := avail
  301. reg, _ := regexp.Compile("[a-zA-Z]+")
  302. left := reg.ReplaceAllString(leftovers, "")
  303. // output, _ = bf.Output("abcdef")
  304. // Show(output)
  305. if len(left) > 0 {
  306. output, _ = cf.Output(left)
  307. Show(output)
  308. }
  309. }
  310. // Example using FontOutput interface
  311. /*
  312. func ShowFont(name string, f FontOutput) {
  313. var low, uniq bool
  314. var avail string
  315. v, ok := f.(BlockFont)
  316. if ok {
  317. low, uniq, avail := FontInfo(v.characters)
  318. }
  319. v, ok = f.(ColorFont)
  320. if ok {
  321. low, uniq, avail := FontInfo(v.characters)
  322. }
  323. // low, uniq, avail := FontInfo((*f).characters)
  324. fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
  325. // fmt.Printf("Font: %s\n", name)
  326. output, _ := f.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  327. Show(output)
  328. if low && uniq {
  329. output, _ = f.Output("abcdefghijklmnopqrstuvwxyz")
  330. Show(output)
  331. }
  332. leftovers := avail
  333. reg, _ := regexp.Compile("[a-zA-Z]+")
  334. left := reg.ReplaceAllString(leftovers, "")
  335. // output, _ = bf.Output("abcdef")
  336. // Show(output)
  337. // left := "0123456789!@#$%^&*()-=_+[]{}~"
  338. if len(left) > 0 {
  339. output, _ = f.Output(left)
  340. Show(output)
  341. }
  342. }
  343. */
  344. func ExtractFonts(filename string, fonts []string) {
  345. f, err := os.Open(filename)
  346. if err != nil {
  347. fmt.Printf("Open(%s): %s\n", filename, err)
  348. panic(err)
  349. }
  350. defer f.Close()
  351. tdfonts := make([]byte, 20)
  352. f.Read(tdfonts)
  353. for true {
  354. fontdef := make([]byte, 4)
  355. read, _ := f.Read(fontdef)
  356. if read != 4 {
  357. break
  358. }
  359. fontname := make([]byte, 13)
  360. f.Read(fontname)
  361. Name := strings.Trim(string(fontname[1:]), "\x00")
  362. // fmt.Printf("Font: %s\n", Name)
  363. f.Read(fontdef)
  364. single := make([]byte, 1)
  365. var FontType int8
  366. binary.Read(f, binary.LittleEndian, &FontType)
  367. // fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  368. f.Read(single) // Spacing
  369. var BlockSize int16
  370. binary.Read(f, binary.LittleEndian, &BlockSize)
  371. letterOffsets := make([]uint16, 94)
  372. binary.Read(f, binary.LittleEndian, &letterOffsets)
  373. if false {
  374. for idx, i := range letterOffsets {
  375. fmt.Printf(" %04X", i)
  376. if (idx+1)%10 == 0 {
  377. fmt.Println("")
  378. }
  379. }
  380. fmt.Println("")
  381. }
  382. data := make([]byte, BlockSize)
  383. binary.Read(f, binary.LittleEndian, &data)
  384. // The problem isn't that the offsets are > BlockSize
  385. // Detect "truncated" fonts...
  386. broken := false
  387. for idx, i := range letterOffsets {
  388. if i != 65535 {
  389. if i >= uint16(BlockSize) {
  390. broken = true
  391. // Mark character offset as not used
  392. letterOffsets[idx] = 65535
  393. // fmt.Printf("offset %d / %x is out of range %d / %x\n", i, i, BlockSize, BlockSize)
  394. }
  395. }
  396. }
  397. if broken {
  398. fmt.Println("FONT is corrupted/truncated. FIX attempted.")
  399. }
  400. if FontFixup(letterOffsets, &data) {
  401. fmt.Printf("Attempting to *FIX* Font %s\n", Name)
  402. }
  403. // Special case where they are asking for all fonts
  404. if len(fonts) == 1 && fonts[0] == "*" {
  405. switch FontType {
  406. case 1:
  407. bf := ExtractBlock(Name, letterOffsets, data)
  408. if len(bf.Characters) == 0 {
  409. fmt.Printf("%s : BLOCK FONT FAIL\n", Name)
  410. } else {
  411. // ShowFont(Name, &bf)
  412. ShowBlockFont(Name, &bf)
  413. }
  414. case 2:
  415. cf := ExtractColor(Name, letterOffsets, data)
  416. if len(cf.Characters) == 0 {
  417. fmt.Printf("%s : COLOR FONT FAIL\n", Name)
  418. } else {
  419. // ShowFont(Name, &cf)
  420. ShowColorFont(Name, &cf)
  421. }
  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. bf := ExtractBlock(Name, letterOffsets, data)
  431. if len(bf.Characters) == 0 {
  432. fmt.Printf("%s : BLOCK FONT FAIL\n", Name)
  433. } else {
  434. ShowBlockFont(Name, &bf)
  435. }
  436. case 2:
  437. cf := ExtractColor(Name, letterOffsets, data)
  438. if len(cf.Characters) == 0 {
  439. fmt.Printf("%s : COLOR FONT FAIL\n", Name)
  440. } else {
  441. ShowColorFont(Name, &cf)
  442. }
  443. default:
  444. fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  445. }
  446. break
  447. }
  448. }
  449. }
  450. }
  451. }
  452. func main() {
  453. fmt.Println("Font-Show - A TDF (TheDraw Font) Viewer.")
  454. var fonts string
  455. var listFonts bool
  456. var allFonts bool
  457. door.Unicode = true
  458. flag.StringVar(&fonts, "f", "", "Font(s) to show")
  459. flag.BoolVar(&allFonts, "a", false, "Show All Fonts")
  460. flag.BoolVar(&listFonts, "l", false, "List Fonts")
  461. flag.Parse()
  462. if flag.NArg() == 0 {
  463. fmt.Println("I need a TDF filename.")
  464. flag.PrintDefaults()
  465. os.Exit(2)
  466. }
  467. var fontList []string
  468. if len(fonts) > 0 {
  469. fontList = strings.Split(fonts, ",")
  470. }
  471. if allFonts {
  472. fontList = make([]string, 0)
  473. fontList = append(fontList, "*")
  474. }
  475. for _, fontfile := range flag.Args() {
  476. if listFonts {
  477. ListFonts(fontfile)
  478. }
  479. if len(fontList) > 0 {
  480. ExtractFonts(fontfile, fontList)
  481. }
  482. }
  483. }