Hi Alex,

This is indeed a very useful information. You can also put it on StackOverflow, 
then it will be Googlable.

Cheers.
Uko


> On 27 Oct 2015, at 12:02, Aliaksei Syrel <[email protected]> wrote:
> 
> Hi
> 
> I'm sending it here so that it doesn't get lost.
> 
> There are multiple ways to measure string width. In the following examples 
> performance will be tested measuring 10`000 times the width of the following 
> string:
> 
> string := 'abcdefghijklmnopqrstuvwxyz 0123456789!@#$%^&*()_+'.
> 
> 1) The most straightforward way is to send #widthOfString: to the font. Time 
> to run gives around 250ms.
> http://ws.stfx.eu/2Q5YA9DFTRDR <http://ws.stfx.eu/2Q5YA9DFTRDR>
> Resulting value is rounded to integer and seems to be not absolutely correct 
> and precise.
> 
> 2) More complex way is to go deeper on the level of glyphs and manually 
> summarise the width of each character in the string sending #getGlyphWidth: 
> character to the font. Time to run gives around 750ms.
> http://ws.stfx.eu/ETBEW1EHAAZ8 <http://ws.stfx.eu/ETBEW1EHAAZ8>
> Resulting value is float and looks like correct and precise value.
> 
> 3) Even more complex is to use CairoFontMetricsProvider instead of font's 
> methods. The same as in 2nd case we measure each character. Time to run 
> around 350ms.
> http://ws.stfx.eu/7I89DMD0ZLM3 <http://ws.stfx.eu/7I89DMD0ZLM3>
> Resulting value is exactly the same as in the 2nd case. With almost equal 
> performance to 1st it is nice alternative.
> 
> 4) One more way is to let native cairo to calculate everything for us. Calls 
> happen through nativeboost. Time to run around 120ms.
> http://ws.stfx.eu/HYD76OMIOM7L  <http://ws.stfx.eu/HYD76OMIOM7L>
> As result it returns CairoTextExtents which allows to calculate width and 
> height with one call.
> 
> Cheers,
> Alex

Reply via email to