Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

xlib xft font color

327 views
Skip to first unread message

Yoshi Rokuko

unread,
Nov 15, 2008, 4:59:30 PM11/15/08
to
hey!

is there a good howto with examples on using xft antialiased and colored
fonts in xlib with c?

i have http://keithp.com/~keithp/render/Xft.tutorial, but some examples
would be nice too ...

the following gives me black font:

#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
[...]

struct {
GC blue;
GC gray;
} gc; /* grafical contexts */
XftFont *xftfont;
XftDraw *draw;
XftColor color;
char *msg = "789,34 £ |@~½¾²³";
[...]

xftfont = XftFontOpen(dpy, scr,
XFT_FAMILY, XftTypeString, "dejavuserifcondensed",
XFT_SIZE, XftTypeDouble, 98.0,
NULL);

/* Set the color to black */
color.color.red = 0;
color.color.green = 0;
color.color.blue = 0;
color.color.alpha = 0xffff; //USHRT_MAX;

draw = XftDrawCreate(dpy, win, DefaultVisual(dpy, scr), cmap);

but how do i get i.e. something like

XAllocNamedColor(dpy, cmap, "#4c7899", &blue, &blue)

for the xft fonts?

thank you in advance
y0shi

--
psyc://nemesis.psyc.biz/~y0shi
http://psyc.biz/y0shi_public.ke

arahne

unread,
Nov 24, 2008, 3:29:02 AM11/24/08
to
Yoshi Rokuko wrote:
> hey!
>
> is there a good howto with examples on using xft antialiased and colored
> fonts in xlib with c?

That should be easy.

> /* Set the color to black */
> color.color.red = 0;
> color.color.green = 0;
> color.color.blue = 0;

Here you should put in your r,g b, value of the desired color.

> but how do i get i.e. something like
>
> XAllocNamedColor(dpy, cmap, "#4c7899", &blue, &blue)
>
> for the xft fonts?

You do not need to use XColor for XFT, just set the
correct RGB while creating XftColor.

The color #4c7899 is already hex of
red 0x4c, green 0x78, blue 0x99.
You can find the complete list here:
http://en.wikipedia.org/wiki/X11_color_names
If you wish to use color name "Blue" in color
specification, you can still XAllocNamedColor(),
get the RGB values, and use this to initialize
the XftColor.

Hope this helps,

Dušan Peterc
http://www.arahne.si

Yoshi Rokuko

unread,
Dec 2, 2008, 5:31:44 PM12/2/08
to
arahne <ara...@arahne.si> schrieb:

> Yoshi Rokuko wrote:
>> is there a good howto with examples on using xft antialiased and colored
>> fonts in xlib with c?
>
> That should be easy.
>
>> /* Set the color to black */
>> color.color.red = 0;
>> color.color.green = 0;
>> color.color.blue = 0;
> Here you should put in your r,g b, value of the desired color.

hm, I still don't get what I want do you no what else is wrong?
I will post the complete sample code:

#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
Display *dpy;
Window win;
Colormap cmap;
XEvent e;
XftFont *xfthuge;
XftColor color;
XftDraw *draw;
char *msg = "Hello, World!";
int scr;

/* open connection with the server */
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}

scr = DefaultScreen(dpy);
cmap = DefaultColormap(dpy, scr);

/* create window */
win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 100, 100, 700,
300, 1, BlackPixel(dpy, scr), WhitePixel(dpy, scr));

/* select kind of events we are interested in */
XSelectInput(dpy, win, ExposureMask | KeyPressMask);

/* map (show) the window */
XMapWindow(dpy, win);

xfthuge = XftFontOpen(dpy, scr,
XFT_FAMILY, XftTypeString, "dejavuserifcondensed",
XFT_SIZE, XftTypeDouble, 100.0,
NULL);

color.color.red = 0x4c;
color.color.green = 0x78;
color.color.blue = 0x99;
color.color.alpha = 0x8080; // I tried 0x8080 and I get gray if I try
// 0xffff I get black, but somehow no
// color ...

draw = XftDrawCreate(dpy, win, DefaultVisual(dpy, scr), cmap);

/* event loop */
while (1) {
XNextEvent(dpy, &e);

/* draw or redraw the window */
if (e.type == Expose) {
XftDrawStringUtf8(draw, &color, xfthuge, 10, 100,
(FcChar8*)msg, strlen(msg));
/* XFillRectangle(dpy, win, DefaultGC(dpy, scr), 20,
* 20, 10, 10);
XDrawString(dpy, win, DefaultGC(dpy, scr), 50, 50,
msg, strlen(msg)); */
}
/* exit on key press */
if (e.type == KeyPress)
break;
}

XftDrawDestroy(draw);

/* close connection to server */
XCloseDisplay(dpy);

return 0;
}

I made a screenshot - http://psyc.biz/hello_world.png ;-)

Many thanks so far!

Regards, y0shi

Yoshi Rokuko

unread,
Dec 3, 2008, 12:18:08 PM12/3/08
to
Yoshi Rokuko <yo.rhe...@web.de> schrieb:

> arahne <ara...@arahne.si> schrieb:
>> Yoshi Rokuko wrote:
>>> is there a good howto with examples on using xft antialiased and colored
>>> fonts in xlib with c?
>>
>> That should be easy.
>>
>>> /* Set the color to black */
>>> color.color.red = 0;
>>> color.color.green = 0;
>>> color.color.blue = 0;
>> Here you should put in your r,g b, value of the desired color.
>
> hm, I still don't get what I want do you know what else is wrong?

allright now I found my mistake ...
... I need to allocate the color.

here comes a working sample code:

#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
Display *dpy;
Window win;
Colormap cmap;
XEvent e;
XftFont *xfthuge;
XftColor color;
XftDraw *draw;

XRenderColor kindofblue = { 0x4c00, 0x7800, 0x9900, 0xffff};


char *msg = "Hello, World!";
int scr;

/* open connection with the server */
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}

scr = DefaultScreen(dpy);
cmap = DefaultColormap(dpy, scr);

/* create window */
win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 100, 100, 700,
300, 1, BlackPixel(dpy, scr), WhitePixel(dpy, scr));

/* select kind of events we are interested in */
XSelectInput(dpy, win, ExposureMask | KeyPressMask);

/* map (show) the window */
XMapWindow(dpy, win);

xfthuge = XftFontOpen(dpy, scr,
XFT_FAMILY, XftTypeString, "dejavuserifcondensed",
XFT_SIZE, XftTypeDouble, 100.0,
NULL);

XftColorAllocValue(dpy, DefaultVisual(dpy, scr), cmap,
&kindofblue, &color);

draw = XftDrawCreate(dpy, win, DefaultVisual(dpy, scr), cmap);

/* event loop */
while (1) {
XNextEvent(dpy, &e);

/* draw or redraw the window */
if (e.type == Expose)
XftDrawStringUtf8(draw, &color, xfthuge, 10, 100,
(FcChar8*)msg, strlen(msg));

/* exit on key press */


if (e.type == KeyPress)
break;
}

XftDrawDestroy(draw);

/* close connection to server */
XCloseDisplay(dpy);

return 0;
}

thanks, have fun - y0shi

0 new messages