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

Theo Veenker theo.veenker at beexy.nl
Mon May 10 14:59:57 UTC 2021

On 5/10/21 4:10 PM, 猛 郝 wrote:
> Hi Theo, thank you, that's great. what I want is first: get each glyph; then get contour 
> and outline points of each glyph; then use cairo bezier related funtions to draw those 
> glyphs. because I want to do some transform based on those outline and points information.

So you want to convert each character in a text to path and then operate on that. I'm 
afraid that's far beyond my expertise. I think I would study Inkscape (the source code) 
and learn from that.

> Now my question is if I can use some built-in cairo/pango function to get and render those 
> outline and points without freetype library? I am not familiar with how cairo draws each 
> glyph in detail (bitmap or bezier/line?)

I don't know font rendering in depth, sorry. If you can do the text to path stuff you can 
of course draw or fill the path as you please. But seems a bit of an overkill if all you 
want is to color the individual characters.

> ------------------------------------------------------------------------------------------
> *From:* cairo <cairo-bounces at cairographics.org> on behalf of Theo Veenker 
> <theo.veenker at beexy.nl>
> *Sent:* Saturday, May 8, 2021 9:59 PM
> *To:* cairo at cairographics.org <cairo at cairographics.org>
> *Subject:* Re: [cairo] pango+cairo how to get position of each character?
> 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 <http://www.beexy.nl>
> -- 
> cairo mailing list
> cairo at cairographics.org
> https://lists.cairographics.org/mailman/listinfo/cairo 
> <https://lists.cairographics.org/mailman/listinfo/cairo>

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