示例#1
1
文件: draw.go 项目: jnjackins/slice
func drawPerimeterNumber(dst draw.Image, pt image.Point, number string) {
	dot := fixed.P(pt.X, pt.Y)
	d := font.Drawer{
		Dst:  dst,
		Src:  image.NewUniform(color.RGBA{B: 0xFF, A: 0xFF}),
		Face: basicfont.Face7x13,
		Dot:  dot,
	}
	d.DrawString(number)
}
示例#2
0
文件: main.go 项目: jnjackins/slice
func drawLayerNumber(dst draw.Image, n int) {
	d := font.Drawer{
		Dst:  dst,
		Src:  image.Black,
		Face: basicfont.Face7x13,
		Dot:  fixed.P(2, 13),
	}
	d.DrawString(fmt.Sprintf("Layer %03d", n))
}
示例#3
0
func (atlas *FontAtlas) Draw(text string, b Bounds) {
	m := atlas.Face.Metrics()
	w := pow2(font.MeasureString(atlas.Face, text).Ceil())
	h := pow2(m.Ascent.Ceil() + m.Descent.Ceil())
	if w > 2048 {
		w = 2048
	}
	if h > 2048 {
		h = 2048
	}
	b.Max.X = b.Min.X + float32(w)
	b.Max.Y = b.Min.Y + float32(h)

	rendered := image.NewRGBA(image.Rect(0, 0, w, h))
	drawer := font.Drawer{
		Dst:  rendered,
		Src:  image.Black,
		Face: atlas.Face,
	}
	drawer.Dot = fixed.P(0, m.Ascent.Ceil())
	drawer.DrawString(text)

	atlas.draw(rendered, b)
}
示例#4
0
func createImages(dir, mode string) {

	os.Mkdir(dir, 0777)

	//Load font file
	b, err := ioutil.ReadFile("Arial.ttf")
	if err != nil {
		log.Println(err)
		return
	}

	//Parse font file
	ttf, err := truetype.Parse(b)
	if err != nil {
		log.Println(err)
		return
	}

	//Create Font.Face from font
	face := truetype.NewFace(ttf, &truetype.Options{
		Size:    size,
		DPI:     dpi,
		Hinting: font.HintingNone,
	})

	//Create initial matrix
	m := make([]Bit, 0)
	for y := 17; y <= height; y = y + int(size) {
		for x := 4; x <= width; x = x + int(size) {
			val := "1"
			rand.Seed(time.Now().UnixNano())
			if rand.Intn(2) == 1 {
				val = "0"
			}

			m = append(m, Bit{X: x, Y: y, Val: val})
		}
	}

	//Create next matrix
	mm := make([][]Bit, 0)
	mm = append(mm, m)

	//Get a random list
	rand.Seed(time.Now().UnixNano())
	randBit := rand.Perm(len(m))

	//Create each next matrix
	for i := 0; i < len(randBit); i++ {

		tmpM := make([]Bit, len(m))

		//Copy last matrix
		copy(tmpM, mm[len(mm)-1])

		//Update one bit
		tmpBit := tmpM[randBit[i]]
		if tmpBit.Val == "0" {
			tmpBit.Val = "1"
		} else {
			tmpBit.Val = "0"
		}

		//Save bit in matrix
		tmpM[randBit[i]] = tmpBit

		//Append new matrix
		mm = append(mm, tmpM)

	}

	//Get a random list
	rand.Seed(time.Now().UnixNano())
	randBit = rand.Perm(len(m))

	//Create each next matrix
	for i := 0; i < len(randBit); i++ {

		tmpM := make([]Bit, len(m))

		//Copy last matrix
		copy(tmpM, mm[len(mm)-1])

		//Update one bit
		tmpBit := tmpM[randBit[i]]
		if tmpBit.Val == "0" {
			tmpBit.Val = "1"
		} else {
			tmpBit.Val = "0"
		}

		//Save bit in matrix
		tmpM[randBit[i]] = tmpBit

		//Append new matrix
		mm = append(mm, tmpM)

	}

	//Create all matrix images
	for i := 0; i < len(mm); i++ {

		if i%100 == 0 {
			log.Printf("i:%d/%d", i, len(mm))
		}

		//Create template images
		src := image.NewNRGBA(image.Rect(0, 0, width, height))
		dst := image.NewNRGBA(image.Rect(0, 0, width, height))
		for y := 0; y < height; y++ {
			for x := 0; x < width; x++ {
				if mode == "black" {
					src.Set(x, y, color.NRGBA{uint8(255), uint8(255), uint8(255), 255})
					dst.Set(x, y, color.NRGBA{uint8(0), uint8(0), uint8(0), 255})
				} else {
					src.Set(x, y, color.NRGBA{uint8(0), uint8(0), uint8(0), 255})
					dst.Set(x, y, color.NRGBA{uint8(255), uint8(255), uint8(255), 255})
				}

			}
		}

		//Create file
		f, err := os.OpenFile(fmt.Sprintf("%s/%d.png", dir, i), os.O_CREATE|os.O_WRONLY, 0666)
		if err != nil {
			log.Fatalf("OpenFile - Err:%s", err)
		}

		//Draw the bit
		for _, val := range mm[i] {
			p := fixed.P(val.X, val.Y)
			d := font.Drawer{Dst: dst, Src: src, Face: face, Dot: p}
			d.DrawString(val.Val)
		}

		//Save the file
		if err = png.Encode(f, dst); err != nil {
			log.Fatalf("Encode0 - Err:%s", err)
		}

	}
}
示例#5
-1
func drawRGBA(m *image.RGBA, tp image.Point) {
	draw.Draw(m, m.Bounds(), image.White, image.Point{}, draw.Src)
	for _, p := range crossPoints {
		m.SetRGBA(p.X, p.Y, crossColor)
	}
	d := font.Drawer{
		Dst:  m,
		Src:  image.Black,
		Face: basicfont.Face7x13,
		Dot:  fixed.P(0, 12),
	}
	d.DrawString(fmt.Sprint(tp))
}