Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux, C++, xft : how to use it?

I try to use Xft, the tutorial, well let them calling that a tutorial... looks like it was written in a north korean camp... I also found this one. So let me try to do a step-by-step :

// g++ XftTest.cc -lX11 -lXft `pkg-config --cflags freetype2`
#include<unistd.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<X11/Xft/Xft.h>
int main()
{
  Display *display;
  XftFont      *font;
  XftDraw      *xftdraw;
  XRenderColor xrcolor;
  XftColor     xftcolor;

  display = XOpenDisplay(0);
  Window XP = XCreateSimpleWindow(display,DefaultRootWindow(display),0,0,360,90,0,0,0);
  XMapWindow(display,XP);

  font = NULL; /* added 6/16 */

  //font = XftFontOpenName(display,0,"NorthKorea-50"); // how to check if this is good ?
  font = XftFontOpenName(display,0,"") /* added 6/16 */
  if (!font) return 1;

  xftdraw = XftDrawCreate(display,XP,DefaultVisual(display,0),DefaultColormap(display,0));

  xrcolor.red  =65535;
  xrcolor.green=0;
  xrcolor.blue =0;
  xrcolor.alpha=65535;
  XftColorAllocValue(display,DefaultVisual(display,0),DefaultColormap(display,0),&xrcolor,&xftcolor);

  XftDrawString8(xftdraw, &xftcolor, font, 20,70 , (XftChar8 *)"Joe Dalton", 10);

  XFlush(display);

  sleep(2);

  XftDrawDestroy(xftdraw);
  XftColorFree(display,DefaultVisual(display,0),DefaultColormap(display,0),&xftcolor);

  return 0;
}

as you can see, in the XftFontOpenName : XftFontOpenName(display,0,"NorthKorea-50") if you write a bullshit, like I did, there is no error or something to tell me that I don't have a font called NorthKorea. That make sense since XftFontOpenName returns the nearest font from the string.

So, how can I check if my font match or not ? how can I use a font like arial? I tried XftFontOpenName(display,0,"arial-50"), I got nothing different.

On my system, I have these folders in my fonts folder :

$ls /usr/share/fonts/
100dpi  75dpi  cyrillic  encodings  misc  OTF  TTF  Type1  util

can I use any font in these folders by the same way ?

if you guys have some nice internet links, it would be a pleasure for me to take a look.

n.b. : there is no xft tag...

update 6/16

I'm still stuck with dat stuff... I don't understand, if I replace

font = XftFontOpenName(display,0,"NorthKorea-50");

by

font = XftFontOpenName(display,0,"");

it still loading a font...

xlsfonts return a lot of stuff, like dat one :

-monotype-courier new-bold-r-normal--0-0-0-0-m-0-iso8859-10

but passing this as argument to XftFontOpenName changes nothing; I try to load a font with XftFontOpenXlfd too, it returns always NULL...

like image 938
The Unholy Metal Machine Avatar asked Jun 13 '13 02:06

The Unholy Metal Machine


1 Answers

xft uses fontconfig for font discovery. xlsfonts is a way to query the old X core fonts system fontconfig replaced. xlsfonts results won't map in any reliable way way to the fontconfig state.

So:

  1. forget about any shortcut where xft work sort-of like the X core font system documented in last centuries tech books. It is completely different. It is not X core fonts with antialiasing and truetype support. It is something that can use modern fonts and do antialiasing, but definitely not X core fonts in any form

  2. Read the fontconfig documentation, the fontconfig configuration files on your system, and use fontconfig utilities (fc-query, fc-list, etc) to query the fontconfig state

  3. as the people who maintain xorg (both x core fonts and fontconfig) already told you, do not try to use xft directly. xft is the product of the first attempt to write a new font stack. Its capabilities are not sufficient to manage modern text and modern fonts. The part dealing with modern font format complexities has been spun out in pango a long time ago (because it was proving complex enough it deserved a separate project). If you try to use xft alone, you'll hit the deficiencies in pre-pango code sooner or later, and no one will be interested in fixing your problems because the same code has already been subjected to a decade of fixes pango-side. Avoiding pango is not a shortcut. Avoiding pango is trying to rely of an half-finished, abandonned for years project.

  4. forget about matching a specific font file. fontconfig will actively fight any attempt to work this way. In the post-core-fonts world, you give the font system a font pattern, and it will build a matching font for you. If the result is not the font file you expected that's because the file was evaluated and found lacking. If you want fontconfig to use this file you will have to fix the font file (add the missing glyphs, the missing font styles, etc), not write code to try to force it anyway. Fontconfig considers that it's more important to render a text your users can read, than to only render the parts your pet font can do. And yes that's now how you're used to think about it on other systems. fontconfig is different. Nothing short of a rewrite will make it behave like you expect. No one is interested in this rewrite because by and large fontconfig has been quite successful those past years.

fonts opentype text-rendering fontconfig linux

like image 116
nim Avatar answered Sep 19 '22 06:09

nim