aewl

annotate font.c @ 20:4560e0882c1d

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