Created
April 9, 2017 09:04
-
-
Save snyh/4dade8d474ef2dfe461ad29e525779a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"github.com/disintegration/imaging" | |
"golang.org/x/image/font" | |
"golang.org/x/image/font/basicfont" | |
"golang.org/x/image/math/fixed" | |
"image" | |
"image/color" | |
"math" | |
"reflect" | |
) | |
func drawChar(img *image.Gray, row int, c byte) { | |
col := color.Gray{255} | |
point := fixed.Point26_6{fixed.Int26_6(0), fixed.Int26_6(10 * (row + 1) * 64)} | |
d := &font.Drawer{ | |
Dst: img, | |
Src: image.NewUniform(col), | |
Face: basicfont.Face7x13, | |
Dot: point, | |
} | |
d.DrawString(string(c)) | |
} | |
func build(str string, maxSize int) image.Image { | |
w := 7 | |
h := 13 * (len(str)) | |
img := image.NewGray(image.Rect(0, 0, w, h)) | |
for i := 0; i < len(str); i++ { | |
drawChar(img, i, byte(str[i])) | |
} | |
s := math.Sqrt(float64(maxSize) / (float64(w) * float64(h))) | |
s = 3 | |
sw := int(s * float64(w)) | |
sh := int(s * float64(h)) | |
r := imaging.Resize(img, sw, sh, imaging.Lanczos) | |
r = imaging.Rotate270(r) | |
return r | |
} | |
func Draw(img image.Image) []byte { | |
w := img.Bounds().Size().X | |
h := img.Bounds().Size().Y | |
var Table = []rune{ | |
'่', | |
'ํ', | |
'๋', | |
// '๎', | |
// '็', | |
// '๊', | |
} | |
T2 := []rune{ | |
'็', | |
'ฺ', | |
'้', | |
} | |
buf := new(bytes.Buffer) | |
for i := 0; i < 100; i++ { | |
buf.WriteRune(T2[0]) | |
} | |
for i := 0; i < h; i++ { | |
for j := 0; j < w; j++ { | |
g := color.GrayModel.Convert(img.At(j, i)) | |
y := reflect.ValueOf(g).FieldByName("Y").Uint() | |
pos := int(y * uint64(len(Table)-1) / 255) | |
buf.WriteRune(Table[pos]) | |
} | |
_ = buf.WriteByte(' ') | |
} | |
for i := 0; i < 100; i++ { | |
buf.WriteRune(T2[2]) | |
} | |
return buf.Bytes() | |
} | |
func main() { | |
img := build("Deepin", 1000) | |
fmt.Print(string(Draw(img))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems the bug has been fixed in google-chrome