From 84d4777a360b5cc8aa1a4cf08219123831bc77e4 Mon Sep 17 00:00:00 2001 From: honggengwei Date: Wed, 15 Oct 2014 23:53:21 +0800 Subject: [PATCH] fontmanager done --- figlet/font.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/figlet/font.go b/figlet/font.go index 678e140..ac1fa69 100644 --- a/figlet/font.go +++ b/figlet/font.go @@ -60,7 +60,13 @@ func (this *FontManager) loadBuildInFont() error { return nil } -func (this *FontManager) loadDiskFont(fontName, fontFilePath string) error { +func (this *FontManager) loadDiskFont(fontName string) error { + + fontFilePath, ok := this.fontList[fontName] + if !ok { + return errors.New("FontName Not Found.") + } + // read full file content fileBuf, err := ioutil.ReadFile(fontFilePath) if err != nil { @@ -106,3 +112,61 @@ func (this *FontManager) parseFontContent(cont string) (*Font, error) { return font, nil } + +func (this *FontManager) getFont(fontName string) (*Font, error) { + font, ok := this.fontLib[fontName] + if !ok { + err := this.loadDiskFont(fontName) + if err != nil { + font, _ := this.fontLib["default"] + return font, nil + } + } + font, _ = this.fontLib[fontName] + return font, nil +} + +func (this *FontManager) convertChar(font *Font, char rune) ([]string, error) { + + if char < 0 || char > 127 { + return nil, errors.New("Not Ascii") + } + + height := font.Height + begintRow := (int(char) - 32) * height + + word := make([]string, height, height) + + for i := 0; i < height; i++ { + row := font.FontSlice[begintRow+i] + row = strings.Replace(row, "@", "", -1) + row = strings.Replace(row, font.Hardblank, " ", -1) + word[i] = row + } + + return word, nil +} + +func (this *FontManager) ConvertString(fontName, asciiStr string) (string, error) { + + font, _ := this.getFont(fontName) + + wordlist := make([][]string, 0) + for _, char := range asciiStr { + word, err := this.convertChar(font, char) + if err != nil { + return "", err + } + wordlist = append(wordlist, word) + } + + result := "" + + for i := 0; i < font.Height; i++ { + for j := 0; j < len(wordlist); j++ { + result += wordlist[j][i] + } + result += "\n" + } + return result, nil +}