freetype icon indicating copy to clipboard operation
freetype copied to clipboard

How to draw string in one line with different colors

Open RINNE-TAN opened this issue 3 years ago • 2 comments

I try to use the function DrawString draw it one by one and change the color , but it hard to know how much advanceWidth I should move,so I can't set the right pt . Can you give me some help?

RINNE-TAN avatar Jun 15 '21 15:06 RINNE-TAN

I try to use the function DrawString draw it one by one and change the color , but it hard to know how much advanceWidth I should move,so I can't set the right pt . Can you give me some help?

hi, have you resolve this? I have the same request.

mbeoliero avatar Nov 16 '21 03:11 mbeoliero

Everytime that you use DrawString it returns a fixed.Point26_6 which has the coordinates where the next character should go, I recently did this:

for i, l := range lines {
	for j, char := range l {
		c.SetSrc(image.NewUniform(hsl.HSL{H: 360.0 * float64(j) / float64(len(l)), S: 1.0, L: 0.6}))
		if j == 0 {
			pt = freetype.Pt((j+1)*fontSizePx, (i+1)*fontSizePx)
		}
		pt, err = c.DrawString(string(char), pt)
		if err != nil {
			panic(err)
		}
	}
}

Which will produce something like this:

out

andresperezl avatar Jan 14 '22 16:01 andresperezl