aewl

diff font.c @ 2:a79188fe4a40

added new stuff
author Anselm R. Garbe <garbeam@wmii.de>
date Mon, 10 Jul 2006 18:35:39 +0200
parents
children d567f430a81d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/font.c	Mon Jul 10 18:35:39 2006 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +/*
     1.5 + * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
     1.6 + * See LICENSE file for license details.
     1.7 + */
     1.8 +
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include <string.h>
    1.12 +#include <locale.h>
    1.13 +
    1.14 +unsigned int
    1.15 +textwidth_l(BlitzFont *font, char *text, unsigned int len)
    1.16 +{
    1.17 +	if(font->set) {
    1.18 +		XRectangle r;
    1.19 +		XmbTextExtents(font->set, text, len, nil, &r);
    1.20 +		return r.width;
    1.21 +	}
    1.22 +	return XTextWidth(font->xfont, text, len);
    1.23 +}
    1.24 +
    1.25 +unsigned int
    1.26 +textwidth(BlitzFont *font, char *text)
    1.27 +{
    1.28 +	return blitz_textwidth_l(font, text, strlen(text));
    1.29 +}
    1.30 +
    1.31 +void
    1.32 +loadfont(Blitz *blitz, BlitzFont *font)
    1.33 +{
    1.34 +	char *fontname = font->fontstr;
    1.35 +	char **missing = nil, *def = "?";
    1.36 +	int n;
    1.37 +
    1.38 +	setlocale(LC_ALL, "");
    1.39 +	if(font->set)
    1.40 +		XFreeFontSet(blitz->dpy, font->set);
    1.41 +	font->set = XCreateFontSet(blitz->dpy, fontname, &missing, &n, &def);
    1.42 +	if(missing) {
    1.43 +		while(n--)
    1.44 +			fprintf(stderr, "liblitz: missing fontset: %s\n", missing[n]);
    1.45 +		XFreeStringList(missing);
    1.46 +		if(font->set) {
    1.47 +			XFreeFontSet(blitz->dpy, font->set);
    1.48 +			font->set = nil;
    1.49 +		}
    1.50 +	}
    1.51 +	if(font->set) {
    1.52 +		XFontSetExtents *font_extents;
    1.53 +		XFontStruct **xfonts;
    1.54 +		char **font_names;
    1.55 +		unsigned int i;
    1.56 +
    1.57 +		font->ascent = font->descent = 0;
    1.58 +		font_extents = XExtentsOfFontSet(font->set);
    1.59 +		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
    1.60 +		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
    1.61 +			if(font->ascent < (*xfonts)->ascent)
    1.62 +				font->ascent = (*xfonts)->ascent;
    1.63 +			if(font->descent < (*xfonts)->descent)
    1.64 +				font->descent = (*xfonts)->descent;
    1.65 +			xfonts++;
    1.66 +		}
    1.67 +	}
    1.68 +	else {
    1.69 +		if(font->xfont)
    1.70 +			XFreeFont(blitz->dpy, font->xfont);
    1.71 +		font->xfont = nil;
    1.72 +		font->xfont = XLoadQueryFont(blitz->dpy, fontname);
    1.73 +		if (!font->xfont) {
    1.74 +			fontname = "fixed";
    1.75 +			font->xfont = XLoadQueryFont(blitz->dpy, fontname);
    1.76 +		}
    1.77 +		if (!font->xfont) {
    1.78 +			fprintf(stderr, "%s", "liblitz: error, cannot load 'fixed' font\n");
    1.79 +			exit(1);
    1.80 +		}
    1.81 +		font->ascent = font->xfont->ascent;
    1.82 +		font->descent = font->xfont->descent;
    1.83 +	}
    1.84 +}