Преглед изворни кода

Updated testdoor font demo.

Steve Thielemann пре 3 година
родитељ
комит
45a1754594
3 измењених фајлова са 116 додато и 21 уклоњено
  1. 11 1
      Makefile
  2. 55 13
      testdoor/tdfont.go
  3. 50 7
      testdoor/testdoor.go

+ 11 - 1
Makefile

@@ -2,16 +2,26 @@
 all: door32 testdoor/testdoor testdoor/art.go space-ace/space-ace
 
 
+font-out: font-out.go
+	go build font-out.go
+
+
 door32: door32.c
 	gcc -o door32 door32.c
 
+testdoor/fonts.go: font-out
+	echo "package main" > testdoor/fonts.go
+	./font-out -f "Amazon Cyan,Medieval,Anarchy Blue" -tdf TDFONTS.TDF 
+	./font-out -f Unchained,Asylum,ArmageonRed,BrainDmgBlu -tdf TDFONTS2.TDF
+	cat *_font.go >> testdoor/fonts.go    
+
 testdoor/art.go: space.ans
 	./ansi-to-go.py main space.ans > testdoor/art.go
 
 space-ace/space.go: space.ans
 	./ansi-to-go.py main space.ans > space-ace/space.go
 
-testdoor/testdoor: testdoor/art.go testdoor/*.go door/*.go
+testdoor/testdoor: testdoor/art.go testdoor/fonts.go testdoor/*.go door/*.go
 	cd testdoor; go build
 
 space-ace/space-ace: space-ace/*.go door/*.go space-ace/space.go

+ 55 - 13
testdoor/tdfont.go

@@ -12,6 +12,22 @@ type BlockFont struct {
 	data       [][][]byte
 }
 
+/*
+func BytesHexed(output []byte) {
+	fmt.Printf("BH: ")
+	for _, ch := range output {
+		fmt.Printf("%02x ", ch)
+	}
+	fmt.Println("")
+}
+
+func BytesArrayHexed(output *[][]byte) {
+	for _, out := range *output {
+		BytesHexed(out)
+	}
+}
+*/
+
 // Make []strings all the same length
 func normalizeBlock(block *[][]byte) {
 	var max int
@@ -77,14 +93,16 @@ func expandBlock(block *[][]byte) {
 }
 
 // Given text, translate to thedraw font.
-func (bf *BlockFont) Output(input string) [][]byte {
+func (bf *BlockFont) Output(input string) ([][]byte, int) {
 	var out [][]byte
+	var max int
 
 	for _, ch := range input {
 		// fmt.Printf("%d %x\n", ch, ch)
 		block := bf.GetCharacter(int(ch))
 		// fmt.Println("ok")
 		l := len(block)
+		max += l
 		if l != 0 {
 			if len(out) == 0 {
 				// First time
@@ -118,7 +136,7 @@ func (bf *BlockFont) Output(input string) [][]byte {
 		normalizeBlock(&out)
 		// fmt.Println("normalizeBlock done")
 	}
-	return out
+	return out, len(out[0])
 }
 
 type ColorFont struct {
@@ -156,9 +174,13 @@ func normalizeColor(block *[][]byte) int {
 		// l := len(line)
 		blank := []byte{0x20, 0x0f}
 		for len((*block)[idx]) < max {
-			for _, b := range blank {
-				(*block)[idx] = append((*block)[idx], b)
-			}
+			(*block)[idx] = append((*block)[idx], blank...)
+
+			/*
+				for _, b := range blank {
+					(*block)[idx] = append((*block)[idx], b)
+				}
+			*/
 			// (*block)[idx] += strings.Repeat(" \x0f", max-l/2)
 		}
 	}
@@ -171,7 +193,7 @@ func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
 	if c == 32 {
 		blank := []byte{0x20, 0x0f, 0x20, 0x0f}
 		result = append(result, blank) // " \x0f \x0f")
-		return result, len(result[0])
+		return result, len(result[0]) / 2
 	}
 
 	if c >= 33 && c <= 126 {
@@ -181,7 +203,11 @@ func (cf *ColorFont) GetCharacter(c int) ([][]byte, int) {
 			return result, 0
 		}
 		result = cf.data[cf.characters[c]]
+		// fmt.Println("Character:")
+		// BytesArrayHexed(&result)
+		// fmt.Println("normalizing...")
 		max := normalizeColor(&result)
+		// BytesArrayHexed(&result)
 		// StringHexO(&result)
 		return result, max
 	} else {
@@ -271,12 +297,12 @@ func Colorize(input []byte) []byte {
 	// runes := []rune(input)
 	var previous int
 
-	// StringHexed(input)
+	// BytesHexed(input)
 
 	for pos := 0; pos < len(input); pos += 2 {
 		ch := input[pos]
 		color := int(input[pos+1])
-		// fmt.Printf("%d : %d / %x, %d\n", pos, ch, ch, color)
+		// fmt.Printf("%d : CH %d / %x, Color %d / %x\n", pos, ch, ch, color, color)
 		colorstring := ColorOutput(previous, color)
 		for _, c := range []byte(colorstring) {
 			result = append(result, c)
@@ -304,13 +330,17 @@ func expandColor(block *[][]byte, need int) {
 	*block = append(*block, bytes.Repeat(blank, need))
 }
 
-func (bf *ColorFont) Output(input string) [][]byte {
+func (bf *ColorFont) Output(input string) ([][]byte, int) {
 	var out [][]byte
 	var max int
 
 	for _, ch := range input {
 		// fmt.Printf("%d %x\n", ch, ch)
 		block, blklen := bf.GetCharacter(int(ch))
+		if blklen == 0 {
+			continue
+		}
+
 		l := len(block)
 		max += blklen
 		if l != 0 {
@@ -323,14 +353,21 @@ func (bf *ColorFont) Output(input string) [][]byte {
 				if len(out) != 0 {
 					for l > len(out) {
 						// We need to expand the out
-						expandColor(&out, max-blklen)
+						expandColor(&out, len(out[0])/2)
 						// ExpandColor(&out)
 					}
 				}
 
-				for l > len(block) {
+				for len(out) > len(block) {
 					// We need to expand the block out
-					expandColor(&block, len(block[0])/2)
+					expandColor(&block, len(block)/2)
+				}
+
+				// Normalizing the character blocks
+				normalizeColor(&block)
+
+				if len(out) != len(block) {
+					panic(fmt.Sprintf("len(out) %d != len(block) %d", len(out), len(block)))
 				}
 
 				// Ok, we have something!
@@ -350,12 +387,17 @@ func (bf *ColorFont) Output(input string) [][]byte {
 
 	}
 
+	if len(out) == 0 {
+		return out, 0
+	}
+
 	// StringHexO(&out)
+	max = len(out[0]) / 2
 
 	for idx, _ := range out {
 		out[idx] = Colorize(out[idx])
 	}
-	return out
+	return out, max
 }
 
 func CP437Bytes_to_Unicode(cp437 []byte) string {

+ 50 - 7
testdoor/testdoor.go

@@ -99,25 +99,68 @@ func display_ansi(d *door.Door) {
 
 func font_demo(d *door.Door) {
 	var output [][]byte
+	var l int
+	var centering string
 
-	d.Write(door.Clrscr + door.CRNL + door.CRNL)
+	d.Write(door.Clrscr) //  + door.CRNL + door.CRNL)
 	fac := FontAmazonCyan()
-	output = fac.Output("ABCDEFGHIJKL")
+	output, l = fac.Output("ABCDEFGHIJKL")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+
 	for _, o := range output {
 		if door.Unicode {
-			d.Write(fmt.Sprintf("%s%s%s", door.Reset, CP437Bytes_to_Unicode(o), door.Reset) + door.CRNL)
+			d.Write(fmt.Sprintf("%s%s%s", centering, CP437Bytes_to_Unicode(o), door.Reset) + door.CRNL)
 		} else {
-			d.Write(fmt.Sprintf("%s%s%s", door.Reset, string(o), door.Reset) + door.CRNL)
+			d.Write(fmt.Sprintf("%s%s%s", centering, string(o), door.Reset) + door.CRNL)
 		}
 	}
 
 	fab := FontAnarchyBlue()
-	output = fab.Output("Shazam!")
+	output, l = fab.Output("Bugz is Here!")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+
+	for _, o := range output {
+		if door.Unicode {
+			d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
+		} else {
+			d.Write(centering + string(o) + door.Reset + door.CRNL)
+		}
+	}
+
+	unchain := FontUnchained()
+	output, l = unchain.Output("Hi There!")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+
+	for _, o := range output {
+		if door.Unicode {
+			d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
+		} else {
+			d.Write(centering + string(o) + door.Reset + door.CRNL)
+		}
+	}
+
+	asylum := FontAsylum()
+	output, l = asylum.Output("Bugz ROCKS")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+	// centering = ""
+
+	for _, o := range output {
+		if door.Unicode {
+			d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
+		} else {
+			d.Write(centering + string(o) + door.Reset + door.CRNL)
+		}
+	}
+
+	brain := FontBrainDmgBlu()
+	output, l = brain.Output("I'm so BLUE")
+	centering = strings.Repeat(" ", (door.Width-l)/2)
+
 	for _, o := range output {
 		if door.Unicode {
-			d.Write(door.Reset + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
+			d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
 		} else {
-			d.Write(door.Reset + string(o) + door.Reset + door.CRNL)
+			d.Write(centering + string(o) + door.Reset + door.CRNL)
 		}
 	}