font-show.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package main
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "os"
  6. "red-green/door"
  7. "regexp"
  8. "strings"
  9. )
  10. // Best used with | less -RS
  11. // Attempt to fix broken fonts.
  12. // This verifies that the character offsets are proceeded
  13. // by a null character.
  14. func FontFixup(offsets []uint16, data *[]byte) bool {
  15. fixed := false
  16. for _, offset := range offsets {
  17. if offset == 65535 {
  18. continue
  19. }
  20. if offset == 0 {
  21. continue
  22. }
  23. if (*data)[offset-1] != 0 {
  24. (*data)[offset-1] = 0
  25. fixed = true
  26. }
  27. }
  28. return fixed
  29. }
  30. func StripANSIColors(text string) string {
  31. var re *regexp.Regexp
  32. re, _ = regexp.Compile("\x1b\\[[0-9;]*m")
  33. return re.ReplaceAllString(text, "")
  34. }
  35. func Show(parts []string) {
  36. var reset string = "\x1b[0m"
  37. if len(parts) > 0 {
  38. for _, line := range parts {
  39. fmt.Printf("%s%s\n", door.CP437_to_Unicode(string(line)), reset)
  40. // fmt.Printf("%s%s\n", line, reset)
  41. }
  42. fmt.Println("")
  43. }
  44. }
  45. // Examine character indexes, are there lower case letter? are they unique?
  46. // What are the available characters in this font?
  47. func FontInfo(characters []int) (lower bool, lowerUnique bool, available string) {
  48. // Does it have lowercase?
  49. // Is lowercase unique?
  50. // Available characters in font
  51. // 33 -126
  52. lower = characters[int(rune('a')-33)] != -1
  53. lowerUnique = characters[int(rune('A')-33)] != characters[int(rune('a')-33)]
  54. for idx, offset := range characters {
  55. char := idx + 33
  56. if offset != -1 {
  57. available += string(rune(char))
  58. }
  59. }
  60. return
  61. }
  62. func ShowBlockFontSize(bf *door.BlockFont, text string, width int) {
  63. var output []string
  64. output, _ = bf.Output(text)
  65. if width == 0 || len(output[0]) < width {
  66. Show(output)
  67. return
  68. }
  69. for text != "" {
  70. nextPart:
  71. for pos := 1; pos < len(text); pos++ {
  72. output, _ = bf.Output(text[:pos])
  73. if len(output[0]) >= width {
  74. // use previous
  75. output, _ = bf.Output(text[:pos-1])
  76. Show(output)
  77. text = text[pos-1:]
  78. goto nextPart
  79. }
  80. }
  81. output, _ = bf.Output(text)
  82. Show(output)
  83. text = ""
  84. }
  85. }
  86. func ShowBlockFont(name string, bf *door.BlockFont, width int) {
  87. var low, uniq bool
  88. var avail string
  89. low, uniq, avail = FontInfo(bf.Characters)
  90. fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
  91. ShowBlockFontSize(bf, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", width)
  92. /*
  93. var output []string
  94. output, _ = bf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  95. Show(output)
  96. */
  97. if low && uniq {
  98. ShowBlockFontSize(bf, "abcdefghijklmnopqrstuvwxyz", width)
  99. /*
  100. output, _ = bf.Output("abcdefghijklmnopqrstuvwxyz")
  101. Show(output)
  102. */
  103. }
  104. var leftovers string = avail
  105. var reg *regexp.Regexp
  106. reg, _ = regexp.Compile("[a-zA-Z]+")
  107. var left string = reg.ReplaceAllString(leftovers, "")
  108. // output, _ = bf.Output("abcdef")
  109. // Show(output)
  110. if len(left) > 0 {
  111. ShowBlockFontSize(bf, left, width)
  112. /*
  113. output, _ = bf.Output(left)
  114. Show(output)
  115. */
  116. }
  117. }
  118. func ShowColorFontSize(bf *door.ColorFont, text string, width int) {
  119. var output []string
  120. output, _ = bf.Output(text)
  121. if width == 0 || len(output[0]) < width {
  122. Show(output)
  123. return
  124. }
  125. // Guessing doesn't work very well
  126. for text != "" {
  127. nextPart:
  128. // fmt.Printf("TEXT: [%s]\n", text)
  129. for pos := 1; pos < len(text); pos++ {
  130. output, _ = bf.Output(text[:pos])
  131. // fmt.Println(pos, len(StripANSIColors(output[0])))
  132. if len(StripANSIColors(output[0])) >= width {
  133. // use previous
  134. output, _ = bf.Output(text[:pos-1])
  135. // fmt.Println("OUT:", text[:pos-1])
  136. Show(output)
  137. text = text[pos-1:]
  138. // fmt.Println("TEXT:", text)
  139. goto nextPart
  140. }
  141. }
  142. output, _ = bf.Output(text)
  143. Show(output)
  144. text = ""
  145. }
  146. /*
  147. var guess int = len(StripANSIColors(output[0])) / len(text)
  148. guess = (width / guess)
  149. for text != "" {
  150. var part string
  151. if guess >= len(text) {
  152. part = text
  153. } else {
  154. part = text[0:guess]
  155. }
  156. output, _ = bf.Output(part)
  157. Show(output)
  158. if guess >= len(text) {
  159. text = ""
  160. } else {
  161. text = text[guess:]
  162. }
  163. }
  164. */
  165. }
  166. func ShowColorFont(name string, cf *door.ColorFont, width int) {
  167. var low, uniq bool
  168. var avail string
  169. low, uniq, avail = FontInfo(cf.Characters)
  170. fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
  171. ShowColorFontSize(cf, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", width)
  172. /*
  173. // fmt.Printf("Font: %s\n", name)
  174. var output []string
  175. output, _ = cf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  176. Show(output)
  177. */
  178. if low && uniq {
  179. ShowColorFontSize(cf, "abcdefghijklmnopqrstuvwxyz", width)
  180. /*
  181. output, _ = cf.Output("abcdefghijklmnopqrstuvwxyz")
  182. Show(output)
  183. */
  184. }
  185. var leftovers string = avail
  186. var reg *regexp.Regexp
  187. reg, _ = regexp.Compile("[a-zA-Z]+")
  188. var left string = reg.ReplaceAllString(leftovers, "")
  189. // output, _ = bf.Output("abcdef")
  190. // Show(output)
  191. if len(left) > 0 {
  192. ShowColorFontSize(cf, left, width)
  193. /*
  194. output, _ = cf.Output(left)
  195. Show(output)
  196. */
  197. }
  198. }
  199. // Example using FontOutput interface
  200. /*
  201. func ShowFont(name string, f FontOutput) {
  202. var low, uniq bool
  203. var avail string
  204. v, ok := f.(BlockFont)
  205. if ok {
  206. low, uniq, avail := FontInfo(v.characters)
  207. }
  208. v, ok = f.(ColorFont)
  209. if ok {
  210. low, uniq, avail := FontInfo(v.characters)
  211. }
  212. // low, uniq, avail := FontInfo((*f).characters)
  213. fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
  214. // fmt.Printf("Font: %s\n", name)
  215. output, _ := f.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  216. Show(output)
  217. if low && uniq {
  218. output, _ = f.Output("abcdefghijklmnopqrstuvwxyz")
  219. Show(output)
  220. }
  221. leftovers := avail
  222. reg, _ := regexp.Compile("[a-zA-Z]+")
  223. left := reg.ReplaceAllString(leftovers, "")
  224. // output, _ = bf.Output("abcdef")
  225. // Show(output)
  226. // left := "0123456789!@#$%^&*()-=_+[]{}~"
  227. if len(left) > 0 {
  228. output, _ = f.Output(left)
  229. Show(output)
  230. }
  231. }
  232. */
  233. // displays fonts
  234. func DisplayFonts(filename string, fonts []string, width int) {
  235. f, err := os.Open(filename)
  236. if err != nil {
  237. fmt.Printf("Open(%s): %s\n", filename, err)
  238. panic(err)
  239. }
  240. defer f.Close()
  241. tdfonts := make([]byte, 20)
  242. f.Read(tdfonts)
  243. for {
  244. fontdef := make([]byte, 4)
  245. read, _ := f.Read(fontdef)
  246. if read != 4 {
  247. break
  248. }
  249. fontname := make([]byte, 13)
  250. f.Read(fontname)
  251. Name := strings.Trim(string(fontname[1:]), "\x00")
  252. // fmt.Printf("Font: %s\n", Name)
  253. f.Read(fontdef)
  254. single := make([]byte, 1)
  255. var FontType int8
  256. binary.Read(f, binary.LittleEndian, &FontType)
  257. // fmt.Printf("Font: %s (type %d)\n", Name, FontType)
  258. f.Read(single) // Spacing
  259. var BlockSize int16
  260. binary.Read(f, binary.LittleEndian, &BlockSize)
  261. letterOffsets := make([]uint16, 94)
  262. binary.Read(f, binary.LittleEndian, &letterOffsets)
  263. if false {
  264. for idx, i := range letterOffsets {
  265. fmt.Printf(" %04X", i)
  266. if (idx+1)%10 == 0 {
  267. fmt.Println("")
  268. }
  269. }
  270. fmt.Println("")
  271. }
  272. data := make([]byte, BlockSize)
  273. binary.Read(f, binary.LittleEndian, &data)
  274. // The problem isn't that the offsets are > BlockSize
  275. // Detect "truncated" fonts...
  276. broken := false
  277. for idx, i := range letterOffsets {
  278. if i != 65535 {
  279. if i >= uint16(BlockSize) {
  280. broken = true
  281. // Mark character offset as not used
  282. letterOffsets[idx] = 65535
  283. // fmt.Printf("offset %d / %x is out of range %d / %x\n", i, i, BlockSize, BlockSize)
  284. }
  285. }
  286. }
  287. if broken {
  288. fmt.Println("FONT is corrupted/truncated. FIX attempted.")
  289. }
  290. if FontFixup(letterOffsets, &data) {
  291. fmt.Printf("Attempting to *FIX* Font %s\n", Name)
  292. }
  293. // Special case where they are asking for all fonts
  294. if len(fonts) == 1 && fonts[0] == "*" {
  295. switch FontType {
  296. case 1:
  297. bf := ExtractBlock(Name, letterOffsets, data)
  298. if len(bf.Characters) == 0 {
  299. fmt.Printf("%s : BLOCK FONT FAIL\n", Name)
  300. } else {
  301. // ShowFont(Name, &bf)
  302. ShowBlockFont(Name, &bf, width)
  303. }
  304. case 2:
  305. cf := ExtractColor(Name, letterOffsets, data)
  306. if len(cf.Characters) == 0 {
  307. fmt.Printf("%s : COLOR FONT FAIL\n", Name)
  308. } else {
  309. // ShowFont(Name, &cf)
  310. ShowColorFont(Name, &cf, width)
  311. }
  312. default:
  313. fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  314. }
  315. } else {
  316. for _, f := range fonts {
  317. if Name == f {
  318. switch FontType {
  319. case 1:
  320. bf := ExtractBlock(Name, letterOffsets, data)
  321. if len(bf.Characters) == 0 {
  322. fmt.Printf("%s : BLOCK FONT FAIL\n", Name)
  323. } else {
  324. ShowBlockFont(Name, &bf, width)
  325. }
  326. case 2:
  327. cf := ExtractColor(Name, letterOffsets, data)
  328. if len(cf.Characters) == 0 {
  329. fmt.Printf("%s : COLOR FONT FAIL\n", Name)
  330. } else {
  331. ShowColorFont(Name, &cf, width)
  332. }
  333. default:
  334. fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
  335. }
  336. break
  337. }
  338. }
  339. }
  340. }
  341. }
  342. /*
  343. func main() {
  344. fmt.Println("Font-Show - A TDF (TheDraw Font) Viewer.")
  345. var fonts string
  346. var listFonts bool
  347. var allFonts bool
  348. door.Unicode = true
  349. flag.StringVar(&fonts, "f", "", "Font(s) to show")
  350. flag.BoolVar(&allFonts, "a", false, "Show All Fonts")
  351. flag.BoolVar(&listFonts, "l", false, "List Fonts")
  352. flag.Parse()
  353. if flag.NArg() == 0 {
  354. fmt.Println("I need a TDF filename.")
  355. flag.PrintDefaults()
  356. os.Exit(2)
  357. }
  358. var fontList []string
  359. if len(fonts) > 0 {
  360. fontList = strings.Split(fonts, ",")
  361. }
  362. if allFonts {
  363. fontList = make([]string, 0)
  364. fontList = append(fontList, "*")
  365. }
  366. for _, fontfile := range flag.Args() {
  367. if listFonts {
  368. ListFonts(fontfile)
  369. }
  370. if len(fontList) > 0 {
  371. DisplayFonts(fontfile, fontList)
  372. }
  373. }
  374. }
  375. */