2006-04-23 22:00:47 +04:00
|
|
|
/*
|
|
|
|
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-04-25 15:33:30 +04:00
|
|
|
#include <string.h>
|
2006-05-26 19:22:34 +04:00
|
|
|
#include <locale.h>
|
2006-04-23 22:39:58 +04:00
|
|
|
#include <cext.h>
|
2006-04-23 22:00:47 +04:00
|
|
|
|
|
|
|
#include "blitz.h"
|
|
|
|
|
2006-04-25 15:33:30 +04:00
|
|
|
unsigned int
|
2006-06-12 17:20:03 +04:00
|
|
|
blitz_textwidth(BlitzFont *font, char *text)
|
2006-04-25 15:33:30 +04:00
|
|
|
{
|
2006-04-27 16:18:54 +04:00
|
|
|
if(font->set) {
|
2006-05-02 01:32:04 +04:00
|
|
|
XRectangle r;
|
2006-06-12 14:32:35 +04:00
|
|
|
XmbTextExtents(font->set, text, strlen(text), nil, &r);
|
2006-04-25 15:33:30 +04:00
|
|
|
return r.width;
|
|
|
|
}
|
|
|
|
return XTextWidth(font->xfont, text, strlen(text));
|
|
|
|
}
|
|
|
|
|
2006-04-23 22:00:47 +04:00
|
|
|
void
|
2006-06-22 13:03:42 +04:00
|
|
|
blitz_loadfont(Blitz *blitz, BlitzFont *font)
|
2006-04-23 22:00:47 +04:00
|
|
|
{
|
2006-06-19 12:28:37 +04:00
|
|
|
char *fontname = font->fontstr;
|
2006-04-24 03:01:49 +04:00
|
|
|
char **missing = nil, *def = "?";
|
2006-05-02 01:32:04 +04:00
|
|
|
int n;
|
|
|
|
|
2006-05-26 19:22:34 +04:00
|
|
|
setlocale(LC_ALL, "");
|
2006-04-24 03:01:49 +04:00
|
|
|
if(font->set)
|
2006-06-22 13:03:42 +04:00
|
|
|
XFreeFontSet(blitz->display, font->set);
|
|
|
|
font->set = XCreateFontSet(blitz->display, fontname, &missing, &n, &def);
|
2006-05-26 17:51:14 +04:00
|
|
|
if(missing) {
|
|
|
|
while(n--)
|
|
|
|
fprintf(stderr, "liblitz: missing fontset: %s\n", missing[n]);
|
|
|
|
XFreeStringList(missing);
|
2006-05-29 14:00:55 +04:00
|
|
|
if(font->set) {
|
2006-06-22 13:03:42 +04:00
|
|
|
XFreeFontSet(blitz->display, font->set);
|
2006-05-29 14:00:55 +04:00
|
|
|
font->set = nil;
|
|
|
|
}
|
2006-04-24 01:32:36 +04:00
|
|
|
}
|
2006-05-02 01:32:04 +04:00
|
|
|
if(font->set) {
|
|
|
|
XFontSetExtents *font_extents;
|
|
|
|
XFontStruct **xfonts;
|
|
|
|
char **font_names;
|
|
|
|
unsigned int i;
|
2006-04-24 03:01:49 +04:00
|
|
|
|
2006-05-02 01:32:04 +04:00
|
|
|
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;
|
2006-06-22 14:20:22 +04:00
|
|
|
if(font->rbearing < (*xfonts)->max_bounds.rbearing)
|
|
|
|
font->rbearing = (*xfonts)->max_bounds.rbearing;
|
|
|
|
if(font->lbearing < (*xfonts)->min_bounds.lbearing)
|
|
|
|
font->lbearing = (*xfonts)->min_bounds.lbearing;
|
2006-05-02 01:32:04 +04:00
|
|
|
xfonts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(font->xfont)
|
2006-06-22 13:03:42 +04:00
|
|
|
XFreeFont(blitz->display, font->xfont);
|
2006-05-02 01:32:04 +04:00
|
|
|
font->xfont = nil;
|
2006-06-22 13:03:42 +04:00
|
|
|
font->xfont = XLoadQueryFont(blitz->display, fontname);
|
2006-05-02 01:32:04 +04:00
|
|
|
if (!font->xfont) {
|
|
|
|
fontname = "fixed";
|
2006-06-22 13:03:42 +04:00
|
|
|
font->xfont = XLoadQueryFont(blitz->display, fontname);
|
2006-05-02 01:32:04 +04:00
|
|
|
}
|
|
|
|
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;
|
2006-06-22 14:20:22 +04:00
|
|
|
font->rbearing = font->xfont->max_bounds.rbearing;
|
|
|
|
font->lbearing = font->xfont->min_bounds.lbearing;
|
2006-04-25 21:19:42 +04:00
|
|
|
}
|
2006-04-23 22:00:47 +04:00
|
|
|
}
|