[SDL_ttf] disable kerning or provide a func tion

Hi,

I’ve been trying to use SDL_ttf for my project. I’m designing a lightweight
widget tool set for SDL. I’ve run into a problem during implementation of a text
editing widget. The thing is that I’m trying to calculate the position (in the
string) of a character under a mouse pointer (to allow text selection)

Unfortunately, there is no documented functionality to perform this. I could be
rendering all the glyphs solely and put them together in the end (but
"officially" I don’t know how to merge a few surfaces into one - such functions
as SDL_AllocSurface are undocumented, and rendering many surfaces instead of one
would, as I suppose, slow the application down, not to mention the redundancy in
data structures)

I see two solutions to the problem. Either you provide an option to disable
kerning for a font (the quick and dirty solution), or a function that takes the
relative coordinates of mouse pointer and returns the string position of an
element pointed by the pointer. Whadda ya say?

Hello !

Unfortunately, there is no documented functionality to perform this. I
could be
rendering all the glyphs solely and put them together in the end (but
"officially" I don’t know how to merge a few surfaces into one - such
functions
as SDL_AllocSurface are undocumented, and rendering many surfaces instead
of one
would, as I suppose, slow the application down, not to mention the
redundancy in
data structures)

Instead of SDL_AllocSurface you would use SDL_CreateRGBSurface and
then blit all the chars using SDL_BlitSurface.

CU

Instead of SDL_AllocSurface you would use SDL_CreateRGBSurface and
then blit all the chars using SDL_BlitSurface.

OK, thanks a lot!
(but that’s only half of the solution)

Hello !

Instead of SDL_AllocSurface you would use SDL_CreateRGBSurface and
then blit all the chars using SDL_BlitSurface.

OK, thanks a lot!
(but that’s only half of the solution)

Sorry for that. I never used Freetype directly.
Have you looked at the Freetype site for such a function ?

A way would be to check the width and height of each
character you use for each font, size, … and then save
this information in an array or another data structure.

You can then use SDL_ttf to generate the complete string and
use your information to calculate the necessary position.

For example width & height for “a”, width & height for "b"
and then check if width for “a” + width for “b” = width for “ab”

CU

“Torsten Giebl” wrote:

For example width & height for “a”, width & height for "b"
and then check if width for “a” + width for “b” = width for “ab”

maybe it is better to avoid SDL_ttf completly in this case. i dunno if
SDL_ttf does kerning at all but if it does things might be get
complicated as “ab” might have a different width than “ba”. freetype on
the other hand naturally provides you with this information and it
isn’t that hard to use either. as i learnt reacently it even can render
the glyphs outlines, a feature often asked for, but seldom implemented.

best regards …
clemens

Hello !

For example width & height for “a”, width & height for "b"
and then check if width for “a” + width for “b” = width for “ab”

maybe it is better to avoid SDL_ttf completly in this case. i dunno if
SDL_ttf does kerning at all but if it does things might be get
complicated as “ab” might have a different width than “ba”. freetype on
the other hand naturally provides you with this information and it
isn’t that hard to use either. as i learnt reacently it even can render
the glyphs outlines, a feature often asked for, but seldom implemented.

If SDL_ttf has no such function, it should be
added to it, or at least to its TODO list :slight_smile:

CU

William Konopka wrote:

I’ve been trying to use SDL_ttf for my project. I’m designing a lightweight
widget tool set for SDL. I’ve run into a problem during implementation of a text
editing widget. The thing is that I’m trying to calculate the position (in the
string) of a character under a mouse pointer (to allow text selection)

I see two solutions to the problem. Either you provide an option to disable
kerning for a font (the quick and dirty solution), or a function that takes the
relative coordinates of mouse pointer and returns the string position of an
element pointed by the pointer. Whadda ya say?

In my experience it is perfectly doable to implement a text editing
widget using SDL_ttf, even if some things are done less efficiently than
they could be done with direct access to freetype. Kerning is too nice a
feature to turn off!

See
http://pipmak.svn.sourceforge.net/viewvc/pipmak/trunk/pipmak/source/textedit.c?view=markup
for the code. For mapping from mouse coordinates to character offsets
(and vice versa to draw the insertion point) I keep around an array of
the advance widths of each character (always a whole number of pixels, I
don’t know if freetype could even do subpixel kerning). To update this
when the text has changed, taking into account kerning, I use
TTF_SizeUNICODE() over prefixes of the changed substring plus one
character on either end. (Without kerning, TTF_GlyphMetrics() could be
used.)

such functions as SDL_AllocSurface are undocumented

SDL_AllocSurface is #defined as SDL_CreateRGBSurface, which is documented.

-Christian

Really, it sounds more like SDL_ttf should be deprecated.
SDL_pango is a good improvement, if not 100% complete. Problem is, it
depends on Pango, and hence has a TON of dependencies.

If you want complete i18n/l10n, though, it’s your best bet.
(SDL_ttf is pretty useless for many language, sadly.)On Sat, Sep 22, 2007 at 07:35:21PM +0200, Torsten Giebl wrote:

If SDL_ttf has no such function, it should be
added to it, or at least to its TODO list :slight_smile:


-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/