написал маленькую прогу на чистом X Window "Привет мир" программа именно этот текст и выводит.. но рякозяблами.
Писал на кодировке UTF-8. Как заставить ИКСЫ понимать UTF-8 руcский
Кириллица В X (или кодировка UTF-8)
Модератор: Модераторы разделов
-
sash-kan
- Администратор
- Сообщения: 13939
- Статус: oel ngati kameie
- ОС: GNU
Re: Кириллица В X
шрифт
Писать безграмотно - значит посягать на время людей, к которым мы адресуемся, а потому совершенно недопустимо в правильно организованном обществе. © Щерба Л. В., 1957
при сбоях форума см.блог
при сбоях форума см.блог
-
v04bvs
- Сообщения: 636
- ОС: Debian GNU/Linux
Re: Кириллица В X
Как вы выводите текст? Вообще то нынче принято текст рисовать через freetype, там проблем с юникодом быть не должно.
-
Aleman
- Сообщения: 109
Re: Кириллица В X
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Display *display;
int screen;
void drawtext(Window win, GC gc, XFontStruct *fontinfo,
unsigned int wwidth, unsigned int wheight)
{
setlocale();
char *str1 = "Привет World!";
char *str2 = "Press any key to exit";
int len1, width1, len2, width2;
len1 = strlen(str1);
len2 = strlen(str2);
/* compute string width for centering */
width1 = XTextWidth(fontinfo, str1, len1);
width2 = XTextWidth(fontinfo, str2, len2);
XDrawString(display, win, gc, (wwidth-width1)/2, 20, str1, len1);
XDrawString(display, win, gc, (wwidth-width2)/2, wheight/2 , str2, len2);
}
void drawrect(Window win, GC gc, unsigned int wwidth, unsigned int wheight)
{
unsigned int w, h;
int x, y;
w = 3*wwidth/4;
h = 3*wheight/4;
x = (wwidth - w)/2;
y = (wheight - h)/2;
XDrawRectangle(display, win, gc, x, y, w, h);
}
int main(int argc, char *argv[])
{
char *displayname = NULL; /* if NULL, consults the $DISPLAY env. var. */
unsigned int width, height;
int x, y;
XSizeHints size_hints;
XFontStruct *fontinfo;
GC gc;
Window win;
XEvent report;
/* connect to the X server */
if ((display=XOpenDisplay(displayname))==NULL) {
fprintf(stderr, "cannot connect to the X server %s\n",
XDisplayName(displayname));
exit(1);
}
/* extract screen number from the display structure */
screen = DefaultScreen(display);
/* create opaque window */
x = 100; y=100;
width = 400; height = 300;
win = XCreateSimpleWindow(display, RootWindow(display, screen),
x, y, width, height, 0,
BlackPixel(display,screen), /* border color */
WhitePixel(display,screen) /* background color */
);
/* set properties for the window manager */
size_hints.flags = PPosition | PSize | PMinSize;
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
size_hints.min_width = width/2;
size_hints.min_height = height/2;
XSetStandardProperties(display, win, "Basic Window Program",
"", 0, argv, argc, &size_hints);
/* select event types wanted */
XSelectInput(display, win,
ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
/* set font */
if ((fontinfo=XLoadQueryFont(display, "9x15"))==NULL) {
fprintf(stderr, "Cannot open font 9x15\n");
exit(1);
}
/* create GC for text and drawing */
gc = XCreateGC(display, win, 0, NULL); /* use default GC */
XSetForeground(display, gc, BlackPixel(display,screen));
XSetFont(display, gc, fontinfo->fid);
XSetLineAttributes(display, gc, 4, LineSolid, CapButt, JoinMiter);
/* display window */
XMapWindow(display, win);
/* process events */
while (1) {
XNextEvent(display, &report);
switch(report.type) {
case Expose:
/* get rid of all other expose events in the queue */
while (XCheckTypedEvent(display, Expose, &report))
;
drawtext(win, gc, fontinfo, width, height);
drawrect(win, gc, width, height);
break;
case ConfigureNotify:
/* window has been resized */
width = report.xconfigure.width;
height = report.xconfigure.height;
break;
case ButtonPress:
/* fall through */
case KeyPress:
XUnloadFont(display, fontinfo->fid);
XFreeGC(display, gc);
XCloseDisplay(display);
exit(0);
default:
break;
} /* switch */
} /* while */
return 0;
}
понял что дело в XLoadQueryFont как мне туда TTF шрифт запихать
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Display *display;
int screen;
void drawtext(Window win, GC gc, XFontStruct *fontinfo,
unsigned int wwidth, unsigned int wheight)
{
setlocale();
char *str1 = "Привет World!";
char *str2 = "Press any key to exit";
int len1, width1, len2, width2;
len1 = strlen(str1);
len2 = strlen(str2);
/* compute string width for centering */
width1 = XTextWidth(fontinfo, str1, len1);
width2 = XTextWidth(fontinfo, str2, len2);
XDrawString(display, win, gc, (wwidth-width1)/2, 20, str1, len1);
XDrawString(display, win, gc, (wwidth-width2)/2, wheight/2 , str2, len2);
}
void drawrect(Window win, GC gc, unsigned int wwidth, unsigned int wheight)
{
unsigned int w, h;
int x, y;
w = 3*wwidth/4;
h = 3*wheight/4;
x = (wwidth - w)/2;
y = (wheight - h)/2;
XDrawRectangle(display, win, gc, x, y, w, h);
}
int main(int argc, char *argv[])
{
char *displayname = NULL; /* if NULL, consults the $DISPLAY env. var. */
unsigned int width, height;
int x, y;
XSizeHints size_hints;
XFontStruct *fontinfo;
GC gc;
Window win;
XEvent report;
/* connect to the X server */
if ((display=XOpenDisplay(displayname))==NULL) {
fprintf(stderr, "cannot connect to the X server %s\n",
XDisplayName(displayname));
exit(1);
}
/* extract screen number from the display structure */
screen = DefaultScreen(display);
/* create opaque window */
x = 100; y=100;
width = 400; height = 300;
win = XCreateSimpleWindow(display, RootWindow(display, screen),
x, y, width, height, 0,
BlackPixel(display,screen), /* border color */
WhitePixel(display,screen) /* background color */
);
/* set properties for the window manager */
size_hints.flags = PPosition | PSize | PMinSize;
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
size_hints.min_width = width/2;
size_hints.min_height = height/2;
XSetStandardProperties(display, win, "Basic Window Program",
"", 0, argv, argc, &size_hints);
/* select event types wanted */
XSelectInput(display, win,
ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
/* set font */
if ((fontinfo=XLoadQueryFont(display, "9x15"))==NULL) {
fprintf(stderr, "Cannot open font 9x15\n");
exit(1);
}
/* create GC for text and drawing */
gc = XCreateGC(display, win, 0, NULL); /* use default GC */
XSetForeground(display, gc, BlackPixel(display,screen));
XSetFont(display, gc, fontinfo->fid);
XSetLineAttributes(display, gc, 4, LineSolid, CapButt, JoinMiter);
/* display window */
XMapWindow(display, win);
/* process events */
while (1) {
XNextEvent(display, &report);
switch(report.type) {
case Expose:
/* get rid of all other expose events in the queue */
while (XCheckTypedEvent(display, Expose, &report))
;
drawtext(win, gc, fontinfo, width, height);
drawrect(win, gc, width, height);
break;
case ConfigureNotify:
/* window has been resized */
width = report.xconfigure.width;
height = report.xconfigure.height;
break;
case ButtonPress:
/* fall through */
case KeyPress:
XUnloadFont(display, fontinfo->fid);
XFreeGC(display, gc);
XCloseDisplay(display);
exit(0);
default:
break;
} /* switch */
} /* while */
return 0;
}
понял что дело в XLoadQueryFont как мне туда TTF шрифт запихать