Beispiel #1
0
func (te *TextElement) updateImage(size mgl32.Vec2) {
	// Initialize the context.
	bg := image.Transparent
	c := te.getContext()

	text := te.GetHiddenText()
	if len(text) == 0 {
		text = te.props.placeholder
		r, g, b, _ := te.props.textColor.RGBA()
		placeholderColor := color.RGBA{uint8(r), uint8(g), uint8(b), 80}
		c.SetSrc(image.NewUniform(placeholderColor))
	}

	// Establish image dimensions and do word wrap
	textHeight := c.PointToFixed(float64(te.props.textSize))
	var width int
	var height int = int(textHeight >> 6)
	words := strings.Split(text, " ")
	lines := []string{""}
	lineNb := 0
	for _, word := range words {
		wordWithSpace := fmt.Sprintf("%v ", word)
		dimensions, _ := c.StringDimensions(wordWithSpace)
		width += int(dimensions.X >> 6)
		if width > int(size.X()) {
			width = int(dimensions.X >> 6)
			height += int(dimensions.Y>>6) + 1
			lines = append(lines, "")
			lineNb += 1
		}
		lines[lineNb] = fmt.Sprintf("%v%v", lines[lineNb], wordWithSpace)
	}
	if te.props.height > 0 {
		height = int(te.props.height)
	}

	rgba := image.NewRGBA(image.Rect(0, 0, int(size.X()), height+int(textHeight>>6)/3))
	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)

	// Draw the text.
	pt := freetype.Pt(0, int(textHeight>>6))
	for _, line := range lines {
		_, err := c.DrawString(line, pt)
		if err != nil {
			log.Printf("Error drawing string: %v\n", err)
			return
		}
		pt.Y += textHeight
	}

	te.img.SetImage(imaging.FlipV(rgba))
	te.img.SetWidth(float32(rgba.Bounds().Size().X))
	te.img.SetHeight(float32(rgba.Bounds().Size().Y))
}
Beispiel #2
0
func main() {
	flag.Parse()

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

	// Initialize the context.
	fg, bg := image.Black, image.White
	if *wonb {
		fg, bg = image.White, image.Black
	}
	c := freetype.NewContext()
	c.SetDPI(*dpi)
	c.SetFont(f)
	c.SetFontSize(*size)
	c.SetSrc(fg)
	switch *hinting {
	default:
		c.SetHinting(font.HintingNone)
	case "full":
		c.SetHinting(font.HintingFull)
	}

	var width fixed.Int26_6
	var height fixed.Int26_6
	for _, s := range text {
		dimensions, _ := c.StringDimensions(s)
		height = height + dimensions.Y
		if dimensions.X > width {
			width = dimensions.X
		}
	}
	imgWidth := int(width >> 6)
	imgHeight := int(height >> 6)
	fmt.Println(imgWidth, imgHeight)
	rgba := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)

	// Draw the text.
	pt := freetype.Pt(0, int(c.PointToFixed(*size)>>6))
	for _, s := range text {
		_, err = c.DrawString(s, pt)
		if err != nil {
			log.Println(err)
			return
		}
		pt.Y += c.PointToFixed(*size * *spacing)
	}

	// Save that RGBA image to disk.
	outFile, err := os.Create("out.png")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()
	b := bufio.NewWriter(outFile)
	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.")
}