[cairo] pango+cairo how to get position of each character?

Theo Veenker theo.veenker at beexy.nl
Sat May 8 13:59:57 UTC 2021


On 08/05/2021 10:32, mhao1999 at live.cn wrote:
> For example, I have a text string "hello" and a truetype font file, and 
> I want to apply different color to different character, currently I 
> cannot find a way to implement it.
> 
> My thought is:
> first get each position of each character from pango/cairo, then if no 
> api to render one by one, I can use freetype library to render each 
> glyph, does it work?
> 
> Thanks for any idea.
> 

I think you need to create a pango layout for the text. Then use a pango 
iterator to iterate over the lines and each line's content. This should 
give you the position and size of each glyph. Then you can render each 
character separately in the desired color at the calculated position. 
Maybe it can be done smarter than that.

If your goal is just to display fancy colored text rather than use a 
specific color for each character, I would simply create a rainbow 
gradient or whatever and use that as the paint source for your text. 
That's much easier and may also look better.

HTH.

Theo


-- 
Theo Veenker  |  Beexy - Behavioral Experiment Software
+31(0)524-541531  |  +31(0)6-42525777 mobile
theo.veenker at beexy.nl  |  www.beexy.nl


More information about the cairo mailing list