Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save snyh/4dade8d474ef2dfe461ad29e525779a4 to your computer and use it in GitHub Desktop.
Save snyh/4dade8d474ef2dfe461ad29e525779a4 to your computer and use it in GitHub Desktop.
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)))
}
@snyh
Copy link
Author

snyh commented Jun 29, 2017

It seems the bug has been fixed in google-chrome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment