Пример #1
0
func TexToImg(text string) string {

	m := image.NewRGBA(image.Rect(0, 0, 100, 100))
	//blue := color.RGBA{0, 255, 0, 255}

	n := rand.Intn(len(clolrs))
	blue := clolrs[n]

	draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)

	flag.Parse()
	fontBytes, err := ioutil.ReadFile(*fontfile)
	if err != nil {
		log.Println(err)
		return ""
	}
	font, err := freetype.ParseFont(fontBytes)
	if err != nil {
		log.Println(err)
		return ""
	}

	fg := image.Black

	c := freetype.NewContext()
	c.SetDPI(*dpi)
	c.SetFont(font)
	c.SetFontSize(*size)
	c.SetClip(m.Bounds())
	c.SetDst(m)
	c.SetSrc(fg)

	pt := freetype.Pt(10, 0+c.FUnitToPixelRU(font.UnitsPerEm()))
	//转化为数组处理
	texts := []string{text}
	for _, s := range texts {
		_, err = c.DrawString(s, pt)
		if err != nil {
			log.Println(err)
			return ""
		}
		pt.Y += c.PointToFix32(*size * *spacing)
	}

	var buf bytes.Buffer
	png.Encode(&buf, m)
	return base64.StdEncoding.EncodeToString(buf.Bytes())
}
Пример #2
0
func main() {
	flag.Parse()

	// Read the font data.
	fontBytes, err := ioutil.ReadFile(*fontfile)
	if err != nil {
		log.Println(err)
		return
	}
	font, err := freetype.ParseFont(fontBytes)
	if err != nil {
		log.Println(err)
		return
	}

	// Initialize the context.
	fg, bg := image.Black, image.White
	ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
	if *wonb {
		fg, bg = image.White, image.Black
		ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
	}
	rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
	c := freetype.NewContext()
	c.SetDPI(*dpi)
	c.SetFont(font)
	c.SetFontSize(*size)
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)
	c.SetSrc(fg)

	// Draw the guidelines.
	for i := 0; i < 200; i++ {
		rgba.Set(10, 10+i, ruler)
		rgba.Set(10+i, 10, ruler)
	}

	// Draw the text.
	pt := freetype.Pt(10, 10+c.FUnitToPixelRU(font.UnitsPerEm()))
	for _, s := range text {
		_, err = c.DrawString(s, pt)
		if err != nil {
			log.Println(err)
			return
		}
		pt.Y += c.PointToFix32(*size * *spacing)
	}

	// Save that RGBA image to disk.
	f, err := os.Create("out.png")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer f.Close()
	b := bufio.NewWriter(f)
	err = png.Encode(b, rgba)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	err = b.Flush()
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	fmt.Println("Wrote out.png OK.")
}
Пример #3
0
func Draw(text string, dotFunc Drawer) {
	//	初期設定
	dpi := 72
	//	size := int(8)
	size := int(16)
	spacing := 1.0
	texts := []string{text}

	once.Do(func() {
		////	フォント読み込み
		//fontBytes, err := ioutil.ReadFile(*fontfile)
		//if err != nil {
		//	log.Println(err)
		//	return
		//}
		//font, err := freetype.ParseFont(fontBytes)
		//if err != nil {
		//	log.Println(err)
		//	return
		//}
		//	フォント読み込み
		fontBytes, err := Asset("data/" + fontfile)
		if err != nil {
			log.Println(err)
			return
		}

		font, err = freetype.ParseFont(fontBytes)
		if err != nil {
			log.Println(err)
			return
		}
	})

	//	描画設定
	fg, bg := image.Black, image.White
	rgba := image.NewRGBA(image.Rect(0, 0, size*utf8.RuneCountInString(text), size))
	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
	c := freetype.NewContext()
	c.SetDPI(dpi)
	c.SetFont(font)
	c.SetFontSize(float64(size))
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)
	c.SetSrc(fg)

	// Draw the text.
	pt := freetype.Pt(0, 0+c.FUnitToPixelRU(font.UnitsPerEm()))
	for _, s := range texts {
		_, err := c.DrawString(s, pt)
		if err != nil {
			log.Println(err)
			return
		}
		pt.Y += c.PointToFix32(float64(size) * spacing)
	}

	//	画像から文字表示
	for jj, text := range texts {
		for j := jj * size; j < (jj+1)*size; j++ {
			for ii := 0; ii < utf8.RuneCountInString(text); ii++ {
				for i := ii * size; i < (ii+1)*size; i++ {
					r, _, _, _ := rgba.At(i, j).RGBA()
					fmt.Print(dotFunc(ii, jj, i, j, r != 65535))
				}
			}
			fmt.Println()
		}
	}

	//	画像保存
	f, err := os.Create("out.png")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer f.Close()
	b := bufio.NewWriter(f)
	err = png.Encode(b, rgba)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	err = b.Flush()
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	//fmt.Println("Wrote out.png OK.")
}