[HarfBuzz] HarfBuzz glyph offsets

Khaled Hosny khaledhosny at eglug.org
Wed Dec 23 22:16:05 PST 2015


On Wed, Dec 23, 2015 at 02:37:38PM -0800, Jonathan Blow wrote:
> >
> >
> >
> > FreeType does not give you any offsets. Unless you mean the side
> > bearings, but they are very different things and shouldn’t be used for
> > glyph placement at all. You are getting 0 offsets because either the
> > font does not do any special positioning here (not uncommon for old
> > Chinese fonts), or something is wrong in the way the text is shaped.
> >
> 
> ​Okay, this is confusing. Maybe this has to do with different cultural
> assumptions or different ideas about what the software looks like, but
> bearings are just offsets to me. When I look at this diagram from the
> FreeType documentation:
> 
> http://www.freetype.org/freetype2/docs/tutorial/step2.html
> 
> It says that wherever the cursor is, add bearingX as an offset and draw the
> thing in the box. And in fact this is how I draw things when they show up
> in the proper places. So I am not sure what you mean here; my guess is that
> you are thinking of there being a little bit of a software stack living
> below you that itself adds bearingX to draw the thing in the box, so that
> it would be incorrect for you to do so?
>
> So then my interpretation is that the 'offsets' from HB are additional and
> are not meant to include bearings, and are only for kerning or for like
> things, and I should actually add both offsets together I guess.

HarfBuzz gives you information about the placement of each glyph (the
origin in that point). How to render individual glyphs at this origin
point is up to the software stack you use, conflating glyph positioning
with the way FreeType API works will just lead to trouble, as you are
experiencing yourself.

> > For Arabic, it looks like you are applying the kerning on the wrong
> > direction, see how the ا in لطراز is moving closer to the ز instead of
> > the ر as I’d expect it.
> >
> 
> ​That's quite possible, I will investigate. My old code, pre-HB, would
> switch to drawing glyphs right-to-left for RTL languages, but upon starting
> to use HB I was pretty confused because it looks to me like HB is always
> giving me glyphs in left-to-right order, even for RTL languages (if there
> is a setting that changes this, I have no idea). So at some point I just
> simplified all the rendering code to always be LTR, though it is possible I
> made a mistake or else for some reason kerning is RTL while everything else
> is LTR which would be really weird??
>
> Oh wait ... I remember something about kerning in otf/ttf always having to
> do with the logical order of glyphs and not the display order; does HB keep
> this convention? I had interpreted offsets as being screen positions, not
> deltas in some abstract kerning space, so that could be the source of the
> issue.
>
The OpenType model positions glyphs from left to right always, HarfBuzz
should give you the glyphs and positions in their visual order, so after
shaping you don’t need to care whether the text was LTR or RTL (until
you start doing line breaking, because it need to be done in logical
order).

> > If you are not setting the correct text script, then you are unlikely to
> > get correct output. You need to analyse the text and pass the correct
> > script to HarfBuzz (resolving characters with common and inhere tied
> > script properties, etc).
> >
> 
> ​In this case we have a fixed set of languages (currently 15) that we are
> just swapping in and out, so I can set this stuff on a per-language basis,
> so I guess for each one I need to know:
> 
> * what to pass to hb_buffer_set_script​
> 
> ​* what to pass to hb_buffer_set_language (I am not sure how this latter
> affects results in different ways than the former)
> 
> ... Anything else?​

Each Unicode character has a script property, so you don’t need to hard
code it for the text. The only complication is inherited or common
characters, but there is a simple heuristic to handle them, see for
example:
https://github.com/HOST-Oman/libraqm/blob/master/raqm.c#L289

But if you are sure your text is always single script and language (I
see the Arabic has English words, so doesn’t seem to be the case), then
you can hard code the script values.

Regards,
Khaled


More information about the HarfBuzz mailing list