Sfoglia il codice sorgente

Added width to show.

See ShowColorFontSize and ShowBlockFontSize for text wrapping that
works.
In Windows (to get ANSI color to work):
[HKEY_CURRENT_USER\Console]
"VirtualTerminalLevel"=dword:00000001
Steve Thielemann 2 anni fa
parent
commit
3d32f99507
2 ha cambiato i file con 149 aggiunte e 32 eliminazioni
  1. 3 1
      font-util/font-out.go
  2. 146 31
      font-util/font-show.go

+ 3 - 1
font-util/font-out.go

@@ -470,11 +470,13 @@ func main() {
 	var allFonts bool
 	var convert arrayFlags
 	var output string
+	var width int
 
 	var listCommand *flag.FlagSet = flag.NewFlagSet("list", flag.ExitOnError)
 	var showCommand *flag.FlagSet = flag.NewFlagSet("show", flag.ExitOnError)
 	showCommand.BoolVar(&allFonts, "a", false, "Show All Fonts")
 	showCommand.StringVar(&fonts, "f", "", "Fonts to Show font1,font2,font3")
+	showCommand.IntVar(&width, "w", 0, "Width to Show fonts")
 
 	var extractCommand *flag.FlagSet = flag.NewFlagSet("extract", flag.ExitOnError)
 	extractCommand.BoolVar(&allFonts, "a", false, "Extract All Fonts")
@@ -537,7 +539,7 @@ func main() {
 
 		for _, fontfile := range showCommand.Args() {
 			fmt.Println("FILE:", fontfile)
-			DisplayFonts(fontfile, fontList)
+			DisplayFonts(fontfile, fontList, width)
 		}
 	}
 

+ 146 - 31
font-util/font-show.go

@@ -31,8 +31,15 @@ func FontFixup(offsets []uint16, data *[]byte) bool {
 	return fixed
 }
 
+func StripANSIColors(text string) string {
+	var re *regexp.Regexp
+
+	re, _ = regexp.Compile("\x1b\\[[0-9;]*m")
+	return re.ReplaceAllString(text, "")
+}
+
 func Show(parts []string) {
-	reset := "\x1b[0m"
+	var reset string = "\x1b[0m"
 
 	if len(parts) > 0 {
 		for _, line := range parts {
@@ -63,50 +70,158 @@ func FontInfo(characters []int) (lower bool, lowerUnique bool, available string)
 	return
 }
 
-func ShowBlockFont(name string, bf *door.BlockFont) {
-	low, uniq, avail := FontInfo(bf.Characters)
-	fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
+func ShowBlockFontSize(bf *door.BlockFont, text string, width int) {
+	var output []string
+	output, _ = bf.Output(text)
+	if width == 0 || len(output[0]) < width {
+		Show(output)
+		return
+	}
 
-	output, _ := bf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
-	Show(output)
+	var pos int = 1
 
-	if low && uniq {
-		output, _ := bf.Output("abcdefghijklmnopqrstuvwxyz")
+	for text != "" {
+	nextPart:
+		for pos = 1; pos < len(text); pos++ {
+			output, _ = bf.Output(text[:pos])
+
+			if len(output[0]) >= width {
+				// use previous
+				output, _ = bf.Output(text[:pos-1])
+				Show(output)
+				text = text[pos-1:]
+				goto nextPart
+			}
+		}
+		output, _ = bf.Output(text)
 		Show(output)
+		text = ""
 	}
+}
 
-	leftovers := avail
-	reg, _ := regexp.Compile("[a-zA-Z]+")
-	left := reg.ReplaceAllString(leftovers, "")
+func ShowBlockFont(name string, bf *door.BlockFont, width int) {
+	var low, uniq bool
+	var avail string
+	low, uniq, avail = FontInfo(bf.Characters)
+	fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
+
+	ShowBlockFontSize(bf, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", width)
+	/*
+		var output []string
+		output, _ = bf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
+		Show(output)
+	*/
+	if low && uniq {
+		ShowBlockFontSize(bf, "abcdefghijklmnopqrstuvwxyz", width)
+		/*
+			output, _ = bf.Output("abcdefghijklmnopqrstuvwxyz")
+			Show(output)
+		*/
+	}
+
+	var leftovers string = avail
+	var reg *regexp.Regexp
+	reg, _ = regexp.Compile("[a-zA-Z]+")
+	var left string = reg.ReplaceAllString(leftovers, "")
 	// output, _ = bf.Output("abcdef")
 	// Show(output)
 	if len(left) > 0 {
-		output, _ = bf.Output(left)
-		Show(output)
+		ShowBlockFontSize(bf, left, width)
+		/*
+			output, _ = bf.Output(left)
+			Show(output)
+		*/
 	}
 }
 
-func ShowColorFont(name string, cf *door.ColorFont) {
-	low, uniq, avail := FontInfo(cf.Characters)
-	fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
+func ShowColorFontSize(bf *door.ColorFont, text string, width int) {
+	var output []string
+	output, _ = bf.Output(text)
+	if width == 0 || len(output[0]) < width {
+		Show(output)
+		return
+	}
 
-	// fmt.Printf("Font: %s\n", name)
-	output, _ := cf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
-	Show(output)
+	// Guessing doesn't work very well
+	var pos int = 1
+
+	for text != "" {
+	nextPart:
+		// fmt.Printf("TEXT: [%s]\n", text)
+		for pos = 1; pos < len(text); pos++ {
+			output, _ = bf.Output(text[:pos])
+			// fmt.Println(pos, len(StripANSIColors(output[0])))
+
+			if len(StripANSIColors(output[0])) >= width {
+				// use previous
+				output, _ = bf.Output(text[:pos-1])
+				// fmt.Println("OUT:", text[:pos-1])
+				Show(output)
+				text = text[pos-1:]
+				// fmt.Println("TEXT:", text)
+				goto nextPart
+			}
+		}
+		output, _ = bf.Output(text)
+		Show(output)
+		text = ""
+	}
 
-	if low && uniq {
-		output, _ := cf.Output("abcdefghijklmnopqrstuvwxyz")
+	/*
+		var guess int = len(StripANSIColors(output[0])) / len(text)
+		guess = (width / guess)
+
+		for text != "" {
+			var part string
+			if guess >= len(text) {
+				part = text
+			} else {
+				part = text[0:guess]
+			}
+			output, _ = bf.Output(part)
+			Show(output)
+			if guess >= len(text) {
+				text = ""
+			} else {
+				text = text[guess:]
+			}
+		}
+	*/
+
+}
+func ShowColorFont(name string, cf *door.ColorFont, width int) {
+	var low, uniq bool
+	var avail string
+	low, uniq, avail = FontInfo(cf.Characters)
+	fmt.Printf("Font: %s (LowerCase %t, Unique Lower %t, [%s]\n", name, low, uniq, avail)
+
+	ShowColorFontSize(cf, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", width)
+	/*
+		// fmt.Printf("Font: %s\n", name)
+		var output []string
+		output, _ = cf.Output("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
 		Show(output)
+	*/
+	if low && uniq {
+		ShowColorFontSize(cf, "abcdefghijklmnopqrstuvwxyz", width)
+		/*
+			output, _ = cf.Output("abcdefghijklmnopqrstuvwxyz")
+			Show(output)
+		*/
 	}
 
-	leftovers := avail
-	reg, _ := regexp.Compile("[a-zA-Z]+")
-	left := reg.ReplaceAllString(leftovers, "")
+	var leftovers string = avail
+	var reg *regexp.Regexp
+	reg, _ = regexp.Compile("[a-zA-Z]+")
+	var left string = reg.ReplaceAllString(leftovers, "")
 	// output, _ = bf.Output("abcdef")
 	// Show(output)
 	if len(left) > 0 {
-		output, _ = cf.Output(left)
-		Show(output)
+		ShowColorFontSize(cf, left, width)
+		/*
+			output, _ = cf.Output(left)
+			Show(output)
+		*/
 	}
 }
 
@@ -154,7 +269,7 @@ func ShowFont(name string, f FontOutput) {
 */
 
 // displays fonts
-func DisplayFonts(filename string, fonts []string) {
+func DisplayFonts(filename string, fonts []string, width int) {
 	f, err := os.Open(filename)
 	if err != nil {
 		fmt.Printf("Open(%s): %s\n", filename, err)
@@ -233,7 +348,7 @@ func DisplayFonts(filename string, fonts []string) {
 					fmt.Printf("%s : BLOCK FONT FAIL\n", Name)
 				} else {
 					// ShowFont(Name, &bf)
-					ShowBlockFont(Name, &bf)
+					ShowBlockFont(Name, &bf, width)
 				}
 			case 2:
 				cf := ExtractColor(Name, letterOffsets, data)
@@ -241,7 +356,7 @@ func DisplayFonts(filename string, fonts []string) {
 					fmt.Printf("%s : COLOR FONT FAIL\n", Name)
 				} else {
 					// ShowFont(Name, &cf)
-					ShowColorFont(Name, &cf)
+					ShowColorFont(Name, &cf, width)
 				}
 			default:
 				fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)
@@ -255,14 +370,14 @@ func DisplayFonts(filename string, fonts []string) {
 						if len(bf.Characters) == 0 {
 							fmt.Printf("%s : BLOCK FONT FAIL\n", Name)
 						} else {
-							ShowBlockFont(Name, &bf)
+							ShowBlockFont(Name, &bf, width)
 						}
 					case 2:
 						cf := ExtractColor(Name, letterOffsets, data)
 						if len(cf.Characters) == 0 {
 							fmt.Printf("%s : COLOR FONT FAIL\n", Name)
 						} else {
-							ShowColorFont(Name, &cf)
+							ShowColorFont(Name, &cf, width)
 						}
 					default:
 						fmt.Printf("Sorry, I can't handle Font: %s Type %d!\n", Name, FontType)