Browse Source

Update font-out so it can modify the font's colors.

Steve Thielemann 3 years ago
parent
commit
fe72085858
4 changed files with 141 additions and 6 deletions
  1. 1 0
      .gitignore
  2. 5 1
      Makefile
  3. 127 0
      font-out.go
  4. 8 5
      testdoor/testdoor.go

+ 1 - 0
.gitignore

@@ -1,4 +1,5 @@
 testdoor/art.go
+testdoor/rgfont.go
 testdoor/testdoor
 testdoor/fonts.go
 door32

+ 5 - 1
Makefile

@@ -18,6 +18,10 @@ testdoor/fonts.go: font-out
 	# cat *_font.go >> testdoor/fonts.go    
 	# rm *_font.go
 
+testdoor/rgfont.go: font-out
+	./font-out -f "Armageddon" -c 7,1 -c 4,2 TDFONTS2.TDF > testdoor/rgfont.go
+	sed -i 's/Armageddon/RedGreen/g' testdoor/rgfont.go
+
 ansi-to-go/ansi-to-go: ansi-to-go/ansi-to-go.go
 	cd ansi-to-go; go build
 
@@ -27,7 +31,7 @@ testdoor/art.go: testdoor/growl.ans ansi-to-go/ansi-to-go
 space-ace/space.go: space.ans
 	./ansi-to-go.py main space.ans > space-ace/space.go
 
-testdoor/testdoor: testdoor/art.go testdoor/fonts.go testdoor/*.go door/*.go
+testdoor/testdoor: testdoor/art.go testdoor/fonts.go testdoor/rgfont.go testdoor/*.go door/*.go
 	cd testdoor; go build
 
 space-ace/space-ace: space-ace/*.go door/*.go space-ace/space.go

+ 127 - 0
font-out.go

@@ -10,6 +10,82 @@ import (
 	"strings"
 )
 
+var Conversion [][2]int
+
+// copied from tdfont.go, and modified
+
+func thedraw_to_ansi(c int) int {
+	trans := []int{0, 4, 2, 6, 1, 5, 3, 7}
+	//             0, 1, 2, 3, 4, 5, 6, 7
+	return trans[c]
+}
+
+func MatchStyle(color byte, look byte) int {
+	var match int = 0
+	if ((color >> 4) & 0x07) == look {
+		// Top
+		match |= 1
+	}
+	if (color & 0x07) == look {
+		// Bottom
+		match |= 2
+	}
+	return match
+}
+
+func PatchColor(color byte, new_color byte, style int) byte {
+	var c byte = color
+	if style&1 == 1 {
+		c = (c & 0x8f) | new_color<<4
+	}
+	if style&2 == 2 {
+		c = (c & 0xf8) | new_color
+	}
+	return c
+}
+
+func Scan(block [][][]byte, find_color int) map[[2]int][][2]int {
+	var Targets map[[2]int][][2]int = make(map[[2]int][][2]int, 0)
+	// Scan the font looking for the given color FG/BG
+	// Covert color code to TheDraw Color
+	actual := byte(thedraw_to_ansi(find_color))
+
+	for charIndex := range block {
+		for lineIndex := range block[charIndex] {
+			var found bool = false
+			var patches [][2]int = make([][2]int, 0)
+			for offset := 1; offset < len(block[charIndex][lineIndex]); offset += 2 {
+				color := block[charIndex][lineIndex][offset]
+				style := MatchStyle(color, actual)
+				if style != 0 {
+					// log.Printf("color: %x actual %x style: %d\n", color, actual, style)
+					patches = append(patches, [2]int{offset, style})
+					found = true
+				}
+			}
+			if found {
+				pos := [2]int{charIndex, lineIndex}
+				Targets[pos] = make([][2]int, len(patches))
+				for i := range patches {
+					Targets[pos][i] = patches[i]
+				}
+				// Targets[pos] = patches
+			}
+		}
+	}
+	return Targets
+}
+
+func Modify(block [][][]byte, new_color int, Targets map[[2]int][][2]int) {
+	// Covert color code to TheDraw Color
+	actual := byte(thedraw_to_ansi(new_color))
+	for pos, patch := range Targets {
+		for _, p := range patch {
+			block[pos[0]][pos[1]][p[0]] = PatchColor(block[pos[0]][pos[1]][p[0]], actual, p[1])
+		}
+	}
+}
+
 func ListFonts(filename string) {
 
 	f, err := os.Open(filename)
@@ -185,6 +261,17 @@ func ExtractColor(name string, offsets []uint16, data []byte) {
 	writer.WriteString(output)
 	writer.Flush()
 
+	if len(Conversion) > 0 {
+		// Color Convert time!
+		var Maps []map[[2]int][][2]int = make([]map[[2]int][][2]int, len(Conversion))
+		for idx, codes := range Conversion {
+			Maps[idx] = Scan(blocks, codes[0])
+		}
+		for idx, codes := range Conversion {
+			Modify(blocks, codes[1], Maps[idx])
+		}
+	}
+
 	output = "    Data: [][][]byte{"
 	for _, blk := range blocks {
 		output += "{"
@@ -399,19 +486,59 @@ func ExtractFonts(filename string, fonts []string) {
 	}
 }
 
+// Created so that multiple inputs can be accecpted
+type arrayFlags []string
+
+func (i *arrayFlags) String() string {
+	// change this, this is just can example to satisfy the interface
+	result := ""
+	for _, str := range *i {
+		if result != "" {
+			result += ", "
+		}
+		result += str
+	}
+	return result
+}
+
+func (i *arrayFlags) Set(value string) error {
+	*i = append(*i, strings.TrimSpace(value))
+	return nil
+}
+
+func ParseColorConvert(convert arrayFlags) {
+	Conversion = make([][2]int, 0)
+	if len(convert) > 0 {
+		// Something to do
+		for _, color := range convert {
+			split := strings.Split(color, ",")
+			v1, _ := strconv.Atoi(split[0])
+			v2, _ := strconv.Atoi(split[1])
+			Conversion = append(Conversion, [2]int{v1, v2})
+		}
+	}
+}
+
 func main() {
 
 	var fonts string
 	var defaultPackage string = "main"
 	var listFonts bool
 	var allFonts bool
+	var convert arrayFlags
 
 	flag.StringVar(&fonts, "f", "", "Font(s) to extract")
 	flag.BoolVar(&allFonts, "a", false, "Extract All Fonts")
 	flag.StringVar(&defaultPackage, "p", "main", "Package name to use")
 	flag.BoolVar(&listFonts, "l", false, "List Fonts")
+	flag.Var(&convert, "c", "Convert Color to Color n,n")
 	flag.Parse()
 
+	ParseColorConvert(convert)
+
+	// fmt.Printf("Convert: %#v\n", convert)
+	// fmt.Printf("Conversion: %#v\n", Conversion)
+
 	if flag.NArg() == 0 {
 		fmt.Println("Font-Out - A TDF (TheDraw Font) file processor.")
 		fmt.Println("No TDF filenames given.")

+ 8 - 5
testdoor/testdoor.go

@@ -259,11 +259,14 @@ func font_demo(d *door.Door) {
 		d.Write(door.CRNL)
 	}
 
-	redgreen := FontArmageddon()
-	white := redgreen.Scan(7)
-	blue := redgreen.Scan(4)
-	redgreen.Modify(1, white)
-	redgreen.Modify(2, blue)
+	/*
+		redgreen := FontArmageddon()
+		white := redgreen.Scan(7)
+		blue := redgreen.Scan(4)
+		redgreen.Modify(1, white)
+		redgreen.Modify(2, blue)
+	*/
+	redgreen := FontRedGreen()
 	output, l = redgreen.Output("Red-Green")
 
 	if l < door.Width {