font.c
changeset 2 a79188fe4a40
child 9 d567f430a81d
equal deleted inserted replaced
1:f10194d4b76d 2:a79188fe4a40
       
     1 /*
       
     2  * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
       
     3  * See LICENSE file for license details.
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <string.h>
       
     9 #include <locale.h>
       
    10 
       
    11 unsigned int
       
    12 textwidth_l(BlitzFont *font, char *text, unsigned int len)
       
    13 {
       
    14 	if(font->set) {
       
    15 		XRectangle r;
       
    16 		XmbTextExtents(font->set, text, len, nil, &r);
       
    17 		return r.width;
       
    18 	}
       
    19 	return XTextWidth(font->xfont, text, len);
       
    20 }
       
    21 
       
    22 unsigned int
       
    23 textwidth(BlitzFont *font, char *text)
       
    24 {
       
    25 	return blitz_textwidth_l(font, text, strlen(text));
       
    26 }
       
    27 
       
    28 void
       
    29 loadfont(Blitz *blitz, BlitzFont *font)
       
    30 {
       
    31 	char *fontname = font->fontstr;
       
    32 	char **missing = nil, *def = "?";
       
    33 	int n;
       
    34 
       
    35 	setlocale(LC_ALL, "");
       
    36 	if(font->set)
       
    37 		XFreeFontSet(blitz->dpy, font->set);
       
    38 	font->set = XCreateFontSet(blitz->dpy, fontname, &missing, &n, &def);
       
    39 	if(missing) {
       
    40 		while(n--)
       
    41 			fprintf(stderr, "liblitz: missing fontset: %s\n", missing[n]);
       
    42 		XFreeStringList(missing);
       
    43 		if(font->set) {
       
    44 			XFreeFontSet(blitz->dpy, font->set);
       
    45 			font->set = nil;
       
    46 		}
       
    47 	}
       
    48 	if(font->set) {
       
    49 		XFontSetExtents *font_extents;
       
    50 		XFontStruct **xfonts;
       
    51 		char **font_names;
       
    52 		unsigned int i;
       
    53 
       
    54 		font->ascent = font->descent = 0;
       
    55 		font_extents = XExtentsOfFontSet(font->set);
       
    56 		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
       
    57 		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
       
    58 			if(font->ascent < (*xfonts)->ascent)
       
    59 				font->ascent = (*xfonts)->ascent;
       
    60 			if(font->descent < (*xfonts)->descent)
       
    61 				font->descent = (*xfonts)->descent;
       
    62 			xfonts++;
       
    63 		}
       
    64 	}
       
    65 	else {
       
    66 		if(font->xfont)
       
    67 			XFreeFont(blitz->dpy, font->xfont);
       
    68 		font->xfont = nil;
       
    69 		font->xfont = XLoadQueryFont(blitz->dpy, fontname);
       
    70 		if (!font->xfont) {
       
    71 			fontname = "fixed";
       
    72 			font->xfont = XLoadQueryFont(blitz->dpy, fontname);
       
    73 		}
       
    74 		if (!font->xfont) {
       
    75 			fprintf(stderr, "%s", "liblitz: error, cannot load 'fixed' font\n");
       
    76 			exit(1);
       
    77 		}
       
    78 		font->ascent = font->xfont->ascent;
       
    79 		font->descent = font->xfont->descent;
       
    80 	}
       
    81 }