[xft] xft 如何自動找字?


[xft] xft 如何自動找字?

文章訪客 » 週日 2月 11, 2007 3:48 pm

小弟想在程式裡使用 xft ,可是不會自動找字。

以下面的範例來說,如果指定字體裡沒有全部的「龍龙竜」三字,就會顯示成空白或方格…(例如顯示成「龍龙 」或「龍 竜」)

代碼: 選擇全部

  With parts from lysavis.c by
  Heine Larsen

  With parts from HelloX.C by
  (c) Copyright Paul Griffiths 1999
  Email: paulgriffiths@cwcom.net
  "Hello, World!", X Window System style.

  Other parts about doublebuffering from
  Linux/X11 Game Writers Page
  Maintained by Paul Coates at


#include <X11/Xft/Xft.h>

int main()
   Display      *display;
          display = XOpenDisplay(NULL);

   int      screen_num = DefaultScreen(display);
   Window      win;
   int      x = 0;
   int      y = 0;
   int      msg_x_pos = 0;
   int      width = 500;
   int      height = 70;
   int      border_width = 0;
   unsigned int   depth = DefaultDepth(display, screen_num);
   Colormap   cmap = DefaultColormap(display, screen_num);
   XGCValues   values;
   GC      gc;
   GC      copygc;
   Pixmap      buffer;
   Window      rootwin = RootWindow(display, screen_num);
   XftFont      *xftfont;
   Status      retval;
   XftColor   color_fg;
   XColor      dummyc;
   XColor      fg;
   XftDraw      *xftdraw;
   char      *msg = "龍龙竜";

   win = XCreateSimpleWindow(display, rootwin, x, y, width, height, border_width, BlackPixel(display, screen_num), BlackPixel(display, screen_num));

   buffer = XCreatePixmap(display, win, width, height, depth);

   xftfont = XftFontOpen(display, screen_num, XFT_FAMILY, XftTypeString, "Sazanami Mincho", XFT_SIZE, XftTypeInteger, 40, 0, NULL);

   gc = XCreateGC(display, win, 0, &values);
   copygc = XCreateGC(display, win, 0, &values);

   retval = XAllocNamedColor(display, cmap, "white", &fg, &dummyc);
   color_fg.color.red = dummyc.red;
   color_fg.color.green = dummyc.green;
   color_fg.color.blue = dummyc.blue;

   xftdraw = XftDrawCreate(display, (Drawable) buffer, DefaultVisual(display, screen_num), cmap);

   XMapWindow(display, win);

   while (1) {
      XFillRectangle(display, buffer, gc, 0, 0, width, height);
      XftDrawStringUtf8(xftdraw, &color_fg, xftfont, msg_x_pos, xftfont->ascent, (unsigned char *) msg, strlen(msg));
      XSetFillStyle(display, copygc, FillTiled);
      XSetTile(display, copygc, buffer);
      XFillRectangle(display, win, copygc, 0, 0, width, height);
   return 0;

回到 talk


正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客