Xft and word wrapping

Lucien Gentis lucien.gentis at univ-lorraine.fr
Mon Dec 2 16:17:39 UTC 2019


Hello Радомир,

First I recommand this reading : 
https://keithp.com/~keithp/talks/xtc2001/paper/

It is somehow out of date (particularly XftFont structure description is 
false), but it permits to understand how Xft works.

In order to be sure to get a correct description of data structures, 
look at the Xft, fontconfig and freetype header files.

*First question* : if you want the upper left corner of your text in  
"0,0" , you have to pass coordinates "0, font->ascent" to XftDrawString 
function.

font is the XftFont structure associated with your font, and ascent is 
the member of this structure corresponding to the difference between the 
top of the higher character and its base line.

*Second question* : in order to determine how many pixels is a white 
space for a given font, I calculate the difference between the lengths 
of strings " e" and "e", the first string beginning with a white space :

XGlyphInfo glyphInfo,gi1,gi2;
XftTextExtentsUtf8 (display,font,(FcChar8 *)(" e"),strlen(" e"),&gi1);
XftTextExtentsUtf8 (display,font,(FcChar8 *)("e"),strlen("e"),&gi2);
spaceWidth=gi1.width-gi2.width;

But be aware that if the last character of your string is a white space, 
it is not seen by XftTextExtentsUtf8 function ; in this case, you have 
to test the last character of your string, and if it's a white space, 
you have to add the spaceWidth calculate upward to the length in pixels 
calculated by XftTextExtentsUtf8.

*Third question* :
These are the members I use :

typedef struct _XftFont {
     int        ascent;  // see first question
     int        descent; // difference between bottom and base line
     int        height;
     int        max_advance_width;
     FcCharSet    *charset;
     FcPattern    *pattern; // characteristics of the font ; it is     
                                 // constructed from a patName string like
         // "Liberation Sans-12:bold:roman" passed
         // to FcNameParse function.
} XftFont;


*Fourth question* : you have to verify if anti-aliasing is enabled for 
your font like this :

fcResult=FcPatternGet(font,FC_ANTIALIAS ,0, &fcValue);

if fcValue.u.i == 1 the anti-aliasing is enabled

I think it is enabled by default ; what is the name of your font ?

Le 28/11/2019 à 22:32, Радомир Хаџић a écrit :
> I'm trying to implement word wrapping using Xlib and Xft. I have a few
> questions and I hope some of you will answer at least some of them.
>
> 1. What to set the coordinates to in XftDrawString* functions? For
> example, if I set them to (0,0), the top of the string is cropped.
>
> 2. By what amount to move the pen when we encounter a whitespace
> (spaces, tabs, etc.) character? Currently, on every space I move the
> cursor to the right by 5 pixels and on every tab four times that.
> However the text looks uneven because of this:
>
>      A a   B b   C c    D d
>      E e  F f    G g      H h
>
> I hope it's clear what I mean.
>
> (I have to draw whitespace strings and non-whitespace strings
> separately because I have to break if the word can't fit on the line.)
>
> 3. What do all the fields in XftFont structure represent?
>
> 4. For some reason the rendered text is "ugly". It is pixelated
> instead of smooth for some reason, even though I know the font is
> alright and it renders just fine in xterm. What can I do about this?
> _______________________________________________
> xorg at lists.x.org: X.Org support
> Archives: http://lists.freedesktop.org/archives/xorg
> Info: https://lists.x.org/mailman/listinfo/xorg
> Your subscription address: %(user_address)s
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.x.org/archives/xorg/attachments/20191202/9d8c4d0e/attachment.html>


More information about the xorg mailing list