This commit is contained in:
honggengwei
2014-10-16 10:12:28 +08:00
parent 876cbb5f6b
commit 03062a956d
3 changed files with 85 additions and 71 deletions

View File

@@ -1,7 +1,7 @@
package figlet package figlet
// Build in source font // Build in source font
func BuildInFont() string { func buildInFont() string {
// from starwars.flf // from starwars.flf
var str = `flf2a$ 7 6 22 15 4` + "\n" + var str = `flf2a$ 7 6 22 15 4` + "\n" +
`starwars.flf by Ryan Youck (youck@cs.uregina.ca) Dec 25/1994` + "\n" + `starwars.flf by Ryan Youck (youck@cs.uregina.ca) Dec 25/1994` + "\n" +

View File

@@ -9,24 +9,24 @@ import (
"strings" "strings"
) )
type Font struct { type font struct {
Hardblank string hardblank string
Height int height int
FontSlice []string fontSlice []string
} }
type FontManager struct { type fontManager struct {
// font library // font library
fontLib map[string]*Font fontLib map[string]*font
// font name to path // font name to path
fontList map[string]string fontList map[string]string
} }
func NewFontManager() *FontManager { func newFontManager() *fontManager {
this := &FontManager{} this := &fontManager{}
this.fontLib = make(map[string]*Font) this.fontLib = make(map[string]*font)
this.fontList = make(map[string]string) this.fontList = make(map[string]string)
this.loadBuildInFont() this.loadBuildInFont()
@@ -34,7 +34,7 @@ func NewFontManager() *FontManager {
} }
// walk through the path, load all the *.flf font file // walk through the path, load all the *.flf font file
func (this *FontManager) LoadFont(fontPath string) error { func (this *fontManager) loadFont(fontPath string) error {
return filepath.Walk(fontPath, func(path string, info os.FileInfo, err error) error { return filepath.Walk(fontPath, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@@ -51,8 +51,8 @@ func (this *FontManager) LoadFont(fontPath string) error {
}) })
} }
func (this *FontManager) loadBuildInFont() error { func (this *fontManager) loadBuildInFont() error {
font, err := this.parseFontContent(BuildInFont()) font, err := this.parseFontContent(buildInFont())
if err != nil { if err != nil {
return err return err
} }
@@ -60,7 +60,7 @@ func (this *FontManager) loadBuildInFont() error {
return nil return nil
} }
func (this *FontManager) loadDiskFont(fontName string) error { func (this *fontManager) loadDiskFont(fontName string) error {
fontFilePath, ok := this.fontList[fontName] fontFilePath, ok := this.fontList[fontName]
if !ok { if !ok {
@@ -82,7 +82,7 @@ func (this *FontManager) loadDiskFont(fontName string) error {
return nil return nil
} }
func (this *FontManager) parseFontContent(cont string) (*Font, error) { func (this *fontManager) parseFontContent(cont string) (*font, error) {
lines := strings.Split(cont, "\n") lines := strings.Split(cont, "\n")
if len(lines) < 1 { if len(lines) < 1 {
return nil, errors.New("font content error") return nil, errors.New("font content error")
@@ -103,17 +103,17 @@ func (this *FontManager) parseFontContent(cont string) (*Font, error) {
header := strings.Split(lines[0], " ") header := strings.Split(lines[0], " ")
font := &Font{} font := &font{}
font.Hardblank = header[0][len(header)-1:] font.hardblank = header[0][len(header)-1:]
font.Height, _ = strconv.Atoi(header[1]) font.height, _ = strconv.Atoi(header[1])
commentEndLine, _ := strconv.Atoi(header[5]) commentEndLine, _ := strconv.Atoi(header[5])
font.FontSlice = lines[commentEndLine+1:] font.fontSlice = lines[commentEndLine+1:]
return font, nil return font, nil
} }
func (this *FontManager) getFont(fontName string) (*Font, error) { func (this *fontManager) getFont(fontName string) (*font, error) {
font, ok := this.fontLib[fontName] font, ok := this.fontLib[fontName]
if !ok { if !ok {
err := this.loadDiskFont(fontName) err := this.loadDiskFont(fontName)
@@ -125,48 +125,3 @@ func (this *FontManager) getFont(fontName string) (*Font, error) {
font, _ = this.fontLib[fontName] font, _ = this.fontLib[fontName]
return font, nil 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
}

View File

@@ -1,22 +1,81 @@
package figlet package figlet
import () import (
//"github.com/fatih/color"
"errors"
"strings"
)
type RenderOptions struct {
FontName string
}
func NewRenderOptions() *RenderOptions {
opt := &RenderOptions{}
opt.FontName = "default"
return opt
}
type AsciiRender struct { type AsciiRender struct {
fontMgr *FontManager fontMgr *fontManager
} }
func NewAsciiRender() *AsciiRender { func NewAsciiRender() *AsciiRender {
this := &AsciiRender{} this := &AsciiRender{}
this.fontMgr = NewFontManager() this.fontMgr = newFontManager()
return this return this
} }
func (this *AsciiRender) LoadFont(fontPath string) error { func (this *AsciiRender) LoadFont(fontPath string) error {
return this.fontMgr.LoadFont(fontPath) return this.fontMgr.loadFont(fontPath)
} }
func (this *AsciiRender) Render(asciiStr string, options interface{}) error { func (this *AsciiRender) Render(asciiStr string) (string, error) {
return nil return this.render(asciiStr, NewRenderOptions())
}
func (this *AsciiRender) 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 *AsciiRender) render(asciiStr string, opt *RenderOptions) (string, error) {
font, _ := this.fontMgr.getFont(opt.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
} }