Beispiel #1
0
// PlaceholderImage returns an solid image with a message and text describing the shape.
func PlaceholderImage(shape DataShape, imageSize Point, message string) (image.Image, error) {
	dx, dy, err := shape.GetSize2D(imageSize)
	if err != nil {
		return nil, err
	}
	size := float64(12)
	spacing := float64(1.5)

	// Initialize the context.
	fg, bg := image.Black, image.White
	ruler := color.NRGBA{0xdd, 0xdd, 0xdd, 0xff}
	// White on black
	// fg, bg = image.White, image.Black
	// ruler = color.NRGBA{0x22, 0x22, 0x22, 0xff}
	rgba := image.NewNRGBA(image.Rect(0, 0, int(dx), int(dy)))
	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
	c := freetype.NewContext()
	c.SetDPI(72)
	c.SetFont(Font)
	c.SetFontSize(size)
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)
	c.SetSrc(fg)

	// Draw the guidelines.
	for x := 10; x < int(dx)-10; x++ {
		rgba.Set(x, 10, ruler)
	}
	for y := 10; y < int(dy)-10; y++ {
		rgba.Set(10, y, ruler)
	}

	// Write axis labels.
	rasterToInt := func(f32 raster.Fix32) int {
		return int(f32 >> 8)
	}
	fontY := c.PointToFix32(size * spacing)
	y := 10 + rasterToInt(c.PointToFix32(size))
	pt := freetype.Pt(int(dx)-10, y)
	_, err = c.DrawString(shape.AxisName(0), pt)
	if err != nil {
		return nil, err
	}
	pt = freetype.Pt(10, int(dy)-rasterToInt(fontY))
	_, err = c.DrawString(shape.AxisName(1), pt)
	if err != nil {
		return nil, err
	}

	// Draw the text.
	pt = freetype.Pt(15, y+rasterToInt(fontY))
	_, err = c.DrawString(message, pt)
	if err != nil {
		return nil, err
	}
	pt.Y += fontY
	sizeStr := fmt.Sprintf("%d x %d pixels", dx, dy)
	_, err = c.DrawString(sizeStr, pt)
	if err != nil {
		return nil, err
	}

	return rgba, nil
}
Beispiel #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+int(c.PointToFix32(*size)>>8))
	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.")
}