Browse Source

Added ColorFont.Scan and Modify.

We can now replace colors in ColorFonts.
Steve Thielemann 3 years ago
parent
commit
6e7d4baee8
3 changed files with 126 additions and 1 deletions
  1. 2 1
      Makefile
  2. 79 0
      door/tdfont.go
  3. 45 0
      testdoor/testdoor.go

+ 2 - 1
Makefile

@@ -13,7 +13,8 @@ door32: door32.c
 
 testdoor/fonts.go: font-out
 	# echo "package main\n\nimport \"red-green/door\"\n\n" > testdoor/fonts.go
-	./font-out -f "Amazon Cyan,Medieval,Anarchy Blue,Unchained,Asylum,ArmageonRed,BrainDmgBlu,Boner,Descent,Remorse,Dungeon" TDFONTS.TDF TDFONTS2.TDF TDFONTS9.TDF > testdoor/fonts.go
+	# ./font-out -f "Amazon Cyan,Medieval,Anarchy Blue,Unchained,Asylum,ArmageonRed,BrainDmgBlu,Boner,Descent,Remorse,Dungeon" TDFONTS.TDF TDFONTS2.TDF TDFONTS9.TDF > testdoor/fonts.go
+	./font-out -f "Amazon Cyan,Medieval,Anarchy Blue,Unchained,Asylum,Armageddon,ArmageonRed,BrainDmgBlu,Boner,Descent,Remorse,Dungeon" TDFONTS.TDF TDFONTS2.TDF TDFONTS9.TDF > testdoor/fonts.go
 	# cat *_font.go >> testdoor/fonts.go    
 	# rm *_font.go
 

+ 79 - 0
door/tdfont.go

@@ -207,6 +207,7 @@ func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
 
 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]
 }
 
@@ -411,6 +412,83 @@ func (bf *ColorFont) Output(input string) ([]string, int) {
 	return output, max
 }
 
+// Given a color to look for, see if it is in the color byte.
+// 0x01 = Upper match
+// 0x02 = Lower match
+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
+}
+
+// Update a color byte with the new color information.
+// Style 0x01 = Upper, 0x02 = Lower, 0x03 = Both.
+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
+}
+
+// Scan a ColorFont for a specific color.
+// This returns a map key of character Index + line Index
+// with an array of [2]int (Index, Style) to change.
+func (cf *ColorFont) Scan(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 cf.Data {
+		for lineIndex := range cf.Data[charIndex] {
+			var found bool = false
+			var patches [][2]int = make([][2]int, 0)
+			for offset := 1; offset < len(cf.Data[charIndex][lineIndex]); offset += 2 {
+				color := cf.Data[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
+}
+
+// This modifies the font color, using the Targets found with
+// Scan.
+func (cf *ColorFont) Modify(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 {
+			cf.Data[pos[0]][pos[1]][p[0]] = PatchColor(cf.Data[pos[0]][pos[1]][p[0]], actual, p[1])
+		}
+	}
+}
+
+/*
 func CP437Bytes_to_Unicode(cp437 []byte) string {
 	var result string
 
@@ -734,3 +812,4 @@ func CP437Bytes_to_Unicode(cp437 []byte) string {
 	// fmt.Printf("\n")
 	return result
 }
+*/

+ 45 - 0
testdoor/testdoor.go

@@ -122,6 +122,27 @@ func font_demo(d *door.Door) {
 		d.Write(door.CRNL)
 	}
 
+	patch := fac.Scan(6)
+	// log.Printf("Patch: %#v\n", patch)
+
+	fac.Modify(4, patch)
+
+	output, l = fac.Output("Blue")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+	for _, o := range output {
+		d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
+	}
+	d.Write(door.CRNL)
+
+	fac.Modify(1, patch)
+	output, l = fac.Output("Red")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+	for _, o := range output {
+		d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
+	}
+	d.Write(door.CRNL)
+	press_a_key(d)
+
 	fab := FontAnarchyBlue()
 	output, l = fab.Output("Bugz is Here!")
 	if l > door.Width {
@@ -238,6 +259,30 @@ 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)
+	output, l = redgreen.Output("Red-Green")
+
+	if l < door.Width {
+		press_a_key(d)
+		centering = strings.Repeat(" ", (door.Width-l)/2)
+
+		for _, o := range output {
+			d.Write(centering + o + door.Reset + door.CRNL)
+		}
+		d.Write(door.CRNL)
+		output, l = redgreen.Output("Software")
+		centering = strings.Repeat(" ", (door.Width-l)/2)
+
+		for _, o := range output {
+			d.Write(centering + o + door.Reset + door.CRNL)
+		}
+		d.Write(door.CRNL)
+	}
+
 }
 
 func input_demo(d *door.Door) {