checking locale now in blitz_loadfont (prevents ugly artefacts)

This commit is contained in:
Anselm R. Garbe 2006-05-01 23:32:04 +02:00
parent 70f3dfff77
commit d18d7c66f8
5 changed files with 62 additions and 25 deletions

View File

@ -65,7 +65,7 @@ unsigned int
height_of_bar()
{
enum { BAR_PADDING = 4 };
return blitzfont.xfont->ascent + blitzfont.xfont->descent + BAR_PADDING;
return blitzfont.ascent + blitzfont.descent + BAR_PADDING;
}
void

View File

@ -358,7 +358,7 @@ main(int argc, char *argv[])
| SubstructureRedirectMask | SubstructureNotifyMask;
mrect.width = DisplayWidth(dpy, screen);
mrect.height = draw.font.xfont->ascent + draw.font.xfont->descent + 4;
mrect.height = draw.font.ascent + draw.font.descent + 4;
mrect.y = DisplayHeight(dpy, screen) - mrect.height;
mrect.x = 0;

View File

@ -4,6 +4,7 @@
*/
#include <X11/Xlib.h>
#include <X11/Xlocale.h>
#define BLITZ_FONT "fixed"
#define BLITZ_SELCOLORS "#ffffff #285577 #4c7899"
@ -23,6 +24,8 @@ typedef struct {
typedef struct {
XFontStruct *xfont;
XFontSet set;
int ascent;
int descent;
} BlitzFont;
typedef struct {

View File

@ -60,18 +60,27 @@ xdrawtext(Display *dpy, BlitzDraw *d)
unsigned int x = 0, y = 0, w = 1, h = 1, shortened = 0;
unsigned int len = 0;
static char text[2048];
XGCValues gcv;
if (!d->data)
return;
len = strlen(d->data);
cext_strlcpy(text, d->data, sizeof(text));
XSetFont(dpy, d->gc, d->font.xfont->fid);
h = d->font.xfont->ascent + d->font.xfont->descent;
y = d->rect.y + d->rect.height / 2 - h / 2 + d->font.xfont->ascent;
gcv.foreground = d->color.fg;
gcv.background = d->color.bg;
if(d->font.set)
XChangeGC(dpy, d->gc, GCForeground | GCBackground, &gcv);
else {
gcv.font = d->font.xfont->fid;
XChangeGC(dpy, d->gc, GCForeground | GCBackground | GCFont, &gcv);
}
h = d->font.ascent + d->font.descent;
y = d->rect.y + d->rect.height / 2 - h / 2 + d->font.ascent;
/* shorten text if necessary */
while (len && (w = XTextWidth(d->font.xfont, text, len)) > d->rect.width) {
while (len && (w = blitz_textwidth(dpy, &d->font, text)) > d->rect.width) {
text[len - 1] = 0;
len--;
shortened = 1;
@ -100,8 +109,6 @@ xdrawtext(Display *dpy, BlitzDraw *d)
x = d->rect.x + h / 2;
break;
}
XSetBackground(dpy, d->gc, d->color.bg);
XSetForeground(dpy, d->gc, d->color.fg);
if(d->font.set)
XmbDrawString(dpy, d->drawable, d->font.set, d->gc, x, y, text, len);
else

View File

@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <cext.h>
#include "blitz.h"
@ -13,8 +14,8 @@
unsigned int
blitz_textwidth(Display *dpy, BlitzFont *font, char *text)
{
XRectangle r;
if(font->set) {
XRectangle r;
XmbTextExtents(font->set, text, strlen(text), nil, &r);
return r.width;
}
@ -26,25 +27,51 @@ blitz_loadfont(Display *dpy, BlitzFont *font, char *fontstr)
{
char *fontname = fontstr;
char **missing = nil, *def = "?";
int nmissing;
char *loc = setlocale(LC_ALL, "");
int n;
if(font->set)
XFreeFontSet(dpy, font->set);
if(font->xfont)
XFreeFont(dpy, font->xfont);
font->xfont = XLoadQueryFont(dpy, fontname);
if (!font->xfont) {
fontname = "fixed";
font->xfont = XLoadQueryFont(dpy, fontname);
font->set = nil;
if(!loc || !strcmp(loc, "C") || !strcmp(loc, "POSIX")|| !XSupportsLocale()) {
font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def);
if(missing) {
while(n--)
fprintf(stderr, "liblitz: missing fontset: %s\n", missing[n]);
XFreeStringList(missing);
}
}
if (!font->xfont) {
fprintf(stderr, "%s", "liblitz: error, cannot load 'fixed' font\n");
exit(1);
}
font->set = XCreateFontSet(dpy, fontname, &missing, &nmissing, &def);
if(font->set) {
XFontSetExtents *font_extents;
XFontStruct **xfonts;
char **font_names;
unsigned int i;
if(missing) {
while(nmissing--)
fprintf(stderr, "liblitz: missing fontset: %s\n", missing[nmissing]);
XFreeStringList(missing);
font->ascent = font->descent = 0;
font_extents = XExtentsOfFontSet(font->set);
n = XFontsOfFontSet(font->set, &xfonts, &font_names);
for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
if(font->ascent < (*xfonts)->ascent)
font->ascent = (*xfonts)->ascent;
if(font->descent < (*xfonts)->descent)
font->descent = (*xfonts)->descent;
xfonts++;
}
}
else {
if(font->xfont)
XFreeFont(dpy, font->xfont);
font->xfont = nil;
font->xfont = XLoadQueryFont(dpy, fontname);
if (!font->xfont) {
fontname = "fixed";
font->xfont = XLoadQueryFont(dpy, fontname);
}
if (!font->xfont) {
fprintf(stderr, "%s", "liblitz: error, cannot load 'fixed' font\n");
exit(1);
}
font->ascent = font->xfont->ascent;
font->descent = font->xfont->descent;
}
}