From 71c8a58917c517a1a26c1899fb62174df4c7e554 Mon Sep 17 00:00:00 2001 From: probandula Date: Fri, 28 Oct 2016 13:54:57 +0200 Subject: [PATCH] various small changes --- char.go | 23 ++++++----------------- font.go | 33 +++++++++++++++++++++++++-------- fontmanager.go | 14 +++++--------- render.go | 4 ++-- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/char.go b/char.go index e4b850e..abf1c18 100644 --- a/char.go +++ b/char.go @@ -3,11 +3,10 @@ package figlet4go import ( "errors" "github.com/fatih/color" - "strings" ) // Represents a single ascii character -type AsciiChar struct { +type asciiChar struct { // Slice with the lines of the Char Lines []string // Color of the char @@ -15,30 +14,20 @@ type AsciiChar struct { } // Creates a new ascii character -func NewAsciiChar(font *font, char rune) (*AsciiChar, error) { +func NewAsciiChar(font *font, char rune) (*asciiChar, error) { // If not ascii, throw an error if char < 0 || char > 127 { return nil, errors.New("Not Ascii") } - height := font.height - beginRow := (int(char) - 32) * height + // Get the font's representation of the char + lines := font.getCharSlice(char) - lines := make([]string, height) - - // Get the char lines of the char - for i := 0; i < height; i++ { - row := font.fontSlice[beginRow+i] - row = strings.Replace(row, "@", "", -1) - row = strings.Replace(row, font.hardblank, " ", -1) - lines[i] = row - } - - return &AsciiChar{Lines: lines}, nil + return &asciiChar{Lines: lines}, nil } // Return a line of the char as string with color if set -func (char *AsciiChar) GetLine(index int) string { +func (char *asciiChar) GetLine(index int) string { if char.Color != 0 { colorFunc := color.New(char.Color).SprintFunc() return colorFunc(char.Lines[index]) diff --git a/font.go b/font.go index 7b23ce3..7b5d7dd 100644 --- a/font.go +++ b/font.go @@ -1,9 +1,9 @@ // Explanation of the .flf file header // THE HEADER LINE -// +// // The header line gives information about the FIGfont. Here is an example // showing the names of all parameters: -// +// // flf2a$ 6 5 20 15 3 0 143 229 NOTE: The first five characters in // | | | | | | | | | | the entire file must be "flf2a". // / / | | | | | | | \ @@ -12,24 +12,41 @@ // Height / | | \ Print_Direction // Baseline / \ Comment_Lines // Max_Length Old_Layout* -// +// // * The two layout parameters are closely related and fairly complex. // (See "INTERPRETATION OF LAYOUT PARAMETERS".) // package figlet4go +import ( + "strings" +) // Represents a single font type font struct { // Hardblank symbol hardblank string // Height of one char - height int + height int // fontSlice []string } -func (f *font) getCharSlice() []string { - // TODO here will be the logic of NewAsciiChar - return []string{} -} \ No newline at end of file +// Get a slice of strings containing the chars lines +func (f *font) getCharSlice(char rune) []string { + + height := f.height + beginRow := (int(char) - 32) * height + + lines := make([]string, height) + + // Get the char lines of the char + for i := 0; i < height; i++ { + row := f.fontSlice[beginRow+i] + row = strings.Replace(row, "@", "", -1) + row = strings.Replace(row, f.hardblank, " ", -1) + lines[i] = row + } + + return lines +} diff --git a/fontmanager.go b/fontmanager.go index ba0ab82..0da63ab 100644 --- a/fontmanager.go +++ b/fontmanager.go @@ -29,7 +29,7 @@ type fontManager struct { fontList map[string]string } -// Create a new fontmanagerM +// Create a new fontmanager func newFontManager() *fontManager { fm := &fontManager{} fm.fontLib = make(map[string]*font) @@ -52,11 +52,10 @@ func (fm *fontManager) getFont(fontName string) *font { fontName = defaultFont } } - + return fm.fontLib[fontName] } - // Loads all .flf files recursively in the fontPath path // Saves the found font files in a map with the name as the key // and the path as the value. Doesn't load them at this point @@ -69,11 +68,11 @@ func (fm *fontManager) loadFontList(fontPath string) error { return err } // If the current item is a directory or has not the correct suffix - if info.IsDir() || !strings.HasSuffix(info.Name(), "." + extension) { + if info.IsDir() || !strings.HasSuffix(info.Name(), "."+extension) { return nil } // Extract the font name - fontName := strings.TrimSuffix(info.Name(), "." + extension) + fontName := strings.TrimSuffix(info.Name(), "."+extension) // Save the font to the list fm.fontList[fontName] = path @@ -132,8 +131,6 @@ func (fm *fontManager) loadBuildInFont() error { return nil } - - // Parse a font from a content string // Used to load fonts from disk and the builtin fonts func parseFontContent(cont string) (*font, error) { @@ -156,10 +153,9 @@ func parseFontContent(cont string) (*font, error) { // Initialize the font font := &font{ hardblank: header[0][len(header[0])-1:], - height: height, + height: height, fontSlice: lines[commentEndLine+1:], } return font, nil } - diff --git a/render.go b/render.go index 65bae44..e60bfed 100644 --- a/render.go +++ b/render.go @@ -54,9 +54,9 @@ func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error // Load the font font := ar.fontMgr.getFont(opt.FontName) - + // Slice holding the chars - chars := []*AsciiChar{} + chars := []*asciiChar{} // Index of the current color curColorIndex := 0