But #widthOfString: is definitely weird... like it's adding some extra
space on the sides or something.

On Tue, Oct 27, 2015 at 12:50 PM, Peter Uhnák <[email protected]> wrote:

> Note that you shouldn't use 2) or 3) for non-monospace fonts because of
> kerning.
>
> font := StandardFonts defaultFont.
> font widthOfString: 'eT'. "16"
> font widthOfString: 'Te'. "15"
>
> 'eT' inject: 0 into: [ :width :c | width + (font getGlyphWidth: c) ].
> "13.416015625"
> 'Te' inject: 0 into: [ :width :c | width + (font getGlyphWidth: c) ].
> "13.416015625"
>
> 4) seems fine
>
> font := StandardFonts defaultFont.
> provider := CairoScaledFont fromFreetypeFont: font realFont.
> width := [ :string |
> converter := CairoUTF8Converter new.
> converter convert: string from: 1 to: string size.
> stringExtents := CairoTextExtents new.
> provider getExtentsOf: converter buffer into: stringExtents.
> stringExtents width ].
>
> width value: 'eT'. "12.463638305664063"
> width value: 'Te'. "12.561752319335938"
>
>
> On Tue, Oct 27, 2015 at 12:27 PM, Yuriy Tymchuk <[email protected]>
> wrote:
>
>> 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
>> 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
>> 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
>> 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