Explorar el Código

Cleanup code.

Steve Thielemann hace 3 años
padre
commit
713a322196
Se han modificado 1 ficheros con 152 adiciones y 95 borrados
  1. 152 95
      font-out.go

+ 152 - 95
font-out.go

@@ -10,7 +10,11 @@ import (
 	"strings"
 )
 
+// TODO:
+// Add ability to load font into struct and render
+
 func ListFonts(filename string) {
+
 	f, err := os.Open(filename)
 	if err != nil {
 		fmt.Printf("Open(%s): %s\n", filename, err)
@@ -92,77 +96,104 @@ func text_to_hextext(line string) string {
 	return output
 }
 
-// Attempt to fix broken fonts.
-// This verifies that the character offsets are proceeded
-// by a null character.
-func FontFixup(offsets []uint16, data *[]byte) bool {
-	fixed := false
-	for _, offset := range offsets {
-		if offset == 65535 {
-			continue
-		}
-		if offset == 0 {
-			continue
-		}
-		if (*data)[offset-1] != 0 {
-			(*data)[offset-1] = 0
-			fixed = true
-		}
-	}
-	return fixed
-}
-
 func ExtractColor(name string, offsets []uint16, data []byte) {
 	fmt.Printf("Extract Color Font: %s\n", name)
 	var indexes []int
 	var blocks [][][]byte
 	var current [][]byte
-	w := -1
-	h := -1
-	ch := -1
+	// w := -1
+	// h := -1
+	// ch := -1
+	// color := -1
 	var line []byte
 
-	for idx, b := range data {
-		if w == -1 {
-			indexes = append(indexes, idx)
-			current = make([][]byte, 0)
-			line = make([]byte, 0)
-			w = int(b)
-			continue
-		}
-		if h == -1 {
-			h = int(b)
-			continue
-		}
-		if ch == -1 {
-			ch = int(b)
-			if ch == 0x0d {
-				ch = -1
+	pos := 0
+	for pos < len(data) {
+		indexes = append(indexes, pos)
+		current = make([][]byte, 0)
+		line = make([]byte, 0)
+
+		// We don't use these.
+		// w = data[pos]
+		// h = data[pos+1]
+
+		pos += 2
+
+		// process this character
+		for pos < len(data) {
+			ch := data[pos]
+			pos++
+			if ch == 0x00 {
+				// end of character
 				current = append(current, line)
+				blocks = append(blocks, current)
+				current = make([][]byte, 0)
 				line = make([]byte, 0)
-				continue
+				break
 			}
-			if ch == 0 {
+			if ch == 0x0d {
+				// end of this character line
 				current = append(current, line)
-				blocks = append(blocks, current)
-				current = make([][]byte, 0)
 				line = make([]byte, 0)
-				w = -1
-				h = -1
-				ch = -1
 				continue
 			}
 			if ch == 0x26 {
 				// & descender mark
-				ch = -1
 				continue
 			}
-			// line += string(rune(ch))
-			line = append(line, byte(ch))
-			ch = -1
+			line = append(line, ch)
+			color := data[pos]
+			pos++
+			line = append(line, color)
 		}
 	}
 
+	// the old, sloppy way
+	/*
+		for idx, b := range data {
+			if w == -1 {
+				indexes = append(indexes, idx)
+				current = make([][]byte, 0)
+				line = make([]byte, 0)
+				w = int(b)
+				continue
+			}
+			if h == -1 {
+				h = int(b)
+				continue
+			}
+			if ch == -1 {
+				ch = int(b)
+				if ch == 0x0d {
+					ch = -1
+					current = append(current, line)
+					line = make([]byte, 0)
+					continue
+				}
+				if ch == 0 {
+					current = append(current, line)
+					blocks = append(blocks, current)
+					current = make([][]byte, 0)
+					line = make([]byte, 0)
+					w = -1
+					h = -1
+					ch = -1
+					continue
+				}
+				if ch == 0x26 {
+					// & descender mark
+					ch = -1
+					continue
+				}
+			} else {
+				color = int(b)
+				line = append(line, byte(ch))
+				line = append(line, byte(color))
+				ch = -1
+			}
+		}
+	*/
+
 	// offset optimization:
 	var single []int
 	for _, o := range offsets {
@@ -170,18 +201,12 @@ func ExtractColor(name string, offsets []uint16, data []byte) {
 			single = append(single, -1)
 			continue
 		}
-		found := false
 		for idx, i := range indexes {
 			if o == uint16(i) {
 				single = append(single, idx)
-				found = true
 				break
 			}
 		}
-		if !found {
-			fmt.Printf("Unable to locate index %d / %x (font corrupted)", o, o)
-			return
-		}
 	}
 
 	// Handle Names with spaces
@@ -239,51 +264,90 @@ func ExtractBlock(name string, offsets []uint16, data []byte) {
 	var indexes []int
 	var blocks [][][]byte
 	var current [][]byte
-	w := -1
-	h := -1
-	ch := -1
+	// w := -1
+	// h := -1
+	// ch := -1
 	var line []byte
 
-	for idx, b := range data {
-		if w == -1 {
-			indexes = append(indexes, idx)
-			current = make([][]byte, 0)
-			line = make([]byte, 0)
-			w = int(b)
-			continue
-		}
-		if h == -1 {
-			h = int(b)
-			continue
-		}
-		if ch == -1 {
-			ch = int(b)
-			if ch == 0x0d {
-				ch = -1
+	pos := 0
+	for pos < len(data) {
+		indexes = append(indexes, pos)
+		current = make([][]byte, 0)
+		line = make([]byte, 0)
+
+		// We don't use these
+		// w = data[pos]
+		// h = data[pos+1]
+		pos += 2
+
+		// process this character
+		for pos < len(data) {
+			ch := data[pos]
+			pos++
+			if ch == 0x00 {
+				// end of character
 				current = append(current, line)
+				blocks = append(blocks, current)
+				current = make([][]byte, 0)
 				line = make([]byte, 0)
-				continue
+				break
 			}
-			if ch == 0 {
+			if ch == 0x0d {
+				// end of this character line
 				current = append(current, line)
-				blocks = append(blocks, current)
-				current = make([][]byte, 0)
 				line = make([]byte, 0)
-				w = -1
-				h = -1
-				ch = -1
 				continue
 			}
 			if ch == 0x26 {
 				// & descender mark
-				ch = -1
 				continue
 			}
-			line = append(line, byte(ch))
-			ch = -1
+			line = append(line, ch)
 		}
 	}
 
+	/*
+		for idx, b := range data {
+			if w == -1 {
+				indexes = append(indexes, idx)
+				current = make([][]byte, 0)
+				line = make([]byte, 0)
+				w = int(b)
+				continue
+			}
+			if h == -1 {
+				h = int(b)
+				continue
+			}
+			if ch == -1 {
+				ch = int(b)
+				if ch == 0x0d {
+					ch = -1
+					current = append(current, line)
+					line = make([]byte, 0)
+					continue
+				}
+				if ch == 0 {
+					current = append(current, line)
+					blocks = append(blocks, current)
+					current = make([][]byte, 0)
+					line = make([]byte, 0)
+					w = -1
+					h = -1
+					ch = -1
+					continue
+				}
+				if ch == 0x26 {
+					// & descender mark
+					ch = -1
+					continue
+				}
+				line = append(line, byte(ch))
+				ch = -1
+			}
+		}
+	*/
+
 	// offset optimization:
 	var single []int
 	for _, o := range offsets {
@@ -291,18 +355,12 @@ func ExtractBlock(name string, offsets []uint16, data []byte) {
 			single = append(single, -1)
 			continue
 		}
-		found := false
 		for idx, i := range indexes {
 			if o == uint16(i) {
 				single = append(single, idx)
-				found = true
 				break
 			}
 		}
-		if !found {
-			fmt.Printf("Unable to locate index %d / %x (font corrupted)", o, o)
-			return
-		}
 	}
 
 	// Handle Names with spaces
@@ -317,6 +375,9 @@ func ExtractBlock(name string, offsets []uint16, data []byte) {
 	defer fp.Close()
 	writer := bufio.NewWriter(fp)
 
+	// Should this output routine be part of the BlockFont?
+	// I think so!
+
 	// writer.WriteString("package main\n")
 	writer.WriteString("// " + name + "\n\n")
 
@@ -404,10 +465,6 @@ func ExtractFonts(filename string, fonts []string) {
 		data := make([]byte, BlockSize)
 		binary.Read(f, binary.LittleEndian, &data)
 
-		if FontFixup(letterOffsets, &data) {
-			fmt.Printf("Attempting to *FIX* Font %s\n", Name)
-		}
-
 		// Special case where they are asking for all fonts
 		if len(fonts) == 1 && fonts[0] == "*" {
 			switch FontType {