linux, C++, xft : how to use it?
Asked Answered
A

2

7

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

Arciniega answered 13/6, 2013 at 2:27 Comment(1)
xlsfonts will show you the fonts on the systemHymie
F
6

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.

Fruitcake answered 18/6, 2013 at 18:44 Comment(3)
This answer is way too aggressive.Anadem
Yes it is) But it's better than digging things that dead.Allness
"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." If Xft was a "first attempt" , it's well to remember it's credited to Keith Packard, who's only been working on X for 35 years. The claim that it's "abandonned" [sic] is belied by the fact that Pango uses Xft. To be taken seriously, these claims need support, otherwise they're just strong opinions.Teide
E
0

You should not pass an empty string to XftFontOpenName and expect to match a font. Xft provides support for core fonts and the structure nested within XftFont is a union of XFontStruct and XftFontStruct. X11 fills in the default font in the XFontStruct when creating new instances, so in the case of empty string you are likely getting the default core font.

In the simplest approach, one can hard code a font name like this:

const char *font_name = "Arial-20";
XftFont *font = XftFontOpenName (display, DefaultScreen(display), font_name);

Or you could use XftFontMatch and check the "results" before passing the pattern to XftFontOpenPattern. See http://keithp.com/~keithp/talks/xtc2001/paper/xft.html#sec-editing

Note that XCloseDisplay is missing.

Evocator answered 18/12, 2014 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.