[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freedesktop-xorg
Subject:    Re: Xft and word wrapping
From:       Lucien Gentis <lucien.gentis () univ-lorraine ! fr>
Date:       2019-12-02 16:17:39
Message-ID: 12edf867-ace6-3122-c1ee-8d7fe37d9b73 () univ-lorraine ! fr
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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@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

[Attachment #5 (text/html)]

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hello Радомир,</p>
    <p>First I recommand this reading : <a
        href="https://keithp.com/~keithp/talks/xtc2001/paper/">https://keithp.com/~keithp/talks/xtc2001/paper/</a></p>
  <p>It is somehow out of date (particularly XftFont structure
      description is false), but it permits to understand how Xft works.</p>
    <p>In order to be sure to get a correct description of data
      structures, look at the Xft, fontconfig and freetype header files.<br>
    </p>
    <p><b>First question</b> : if you want the upper left corner of your
      text in  "0,0" , you have to pass coordinates "0, font-&gt;ascent"
      to XftDrawString function.</p>
    <p>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.</p>
    <p><b>Second question</b> : 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 :</p>
    <font size="+2"><tt>XGlyphInfo glyphInfo,gi1,gi2;</tt><tt><br>
      </tt><tt>XftTextExtentsUtf8 (display,font,(FcChar8 *)("
        e"),strlen(" e"),&amp;gi1);</tt><tt><br>
      </tt><tt>XftTextExtentsUtf8 (display,font,(FcChar8
        *)("e"),strlen("e"),&amp;gi2);</tt><tt><br>
      </tt><tt>spaceWidth=gi1.width-gi2.width;</tt></font><br>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">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.</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix"><b>Third question</b> : <br>
    </div>
    <div class="moz-cite-prefix">These are the members I use :</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix"><font size="+2"><tt>typedef struct
          _XftFont {</tt><tt><br>
        </tt><tt>    int        ascent;  // see first question</tt><tt><br>
        </tt><tt>    int        descent; // difference between bottom
          and base line</tt><tt><br>
        </tt><tt>    int        height;</tt><tt><br>
        </tt><tt>    int        max_advance_width;</tt><tt><br>
        </tt><tt>    FcCharSet    *charset;</tt><tt><br>
        </tt><tt>    FcPattern    *pattern; // characteristics of the
          font ; it is                                     //
          constructed from a patName string like <br>
        </tt></font></div>
    <div class="moz-cite-prefix"><font size="+2"><tt>                   
                  // "Liberation Sans-12:bold:roman" passed</tt></font></div>
    <div class="moz-cite-prefix"><font size="+2"><tt>                   
                  // to FcNameParse function.</tt><tt><br>
        </tt><tt>} XftFont;</tt></font></div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix"><b>Fourth question</b> : you have to
      verify if anti-aliasing is enabled for your font like this :</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">fcResult=FcPatternGet(font,FC_ANTIALIAS
      <style type="text/css">td p { background: transparent none repeat scroll 0% 0%; \
}p { margin-bottom: 0.25cm; line-height: 115%; background: transparent none repeat \
scroll 0% 0%; }a:link { color: rgb(0, 0, 128); text-decoration: underline; \
}</style>,0,  <style type="text/css">td p { background: transparent none repeat \
scroll 0% 0%; }p { margin-bottom: 0.25cm; line-height: 115%; background: transparent \
none repeat scroll 0% 0%; }a:link { color: rgb(0, 0, 128); text-decoration: \
underline; }</style>&amp;fcValue);</div>  <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">if fcValue.u.i == 1 the anti-aliasing
      is enabled</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">I think it is enabled by default ; what
      is the name of your font ?</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">Le 28/11/2019 à 22:32, Радомир Хаџић a
      écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:CAPV9uWvA6G1g0A8OyVLXkvyehf2No_Y-VA-6pTJMs82ChdPdrg@mail.gmail.com">
      <pre class="moz-quote-pre" wrap="">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?
_______________________________________________
<a class="moz-txt-link-abbreviated" \
                href="mailto:xorg@lists.x.org">xorg@lists.x.org</a>: X.Org support
Archives: <a class="moz-txt-link-freetext" \
href="http://lists.freedesktop.org/archives/xorg">http://lists.freedesktop.org/archives/xorg</a>
                
Info: <a class="moz-txt-link-freetext" \
href="https://lists.x.org/mailman/listinfo/xorg">https://lists.x.org/mailman/listinfo/xorg</a>
 Your subscription address: %(user_address)s</pre>
    </blockquote>
  </body>
</html>


[Attachment #6 (text/plain)]

_______________________________________________
xorg@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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic