[hackers] [libdraw] improved no-locale support || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Tue, 10 Aug 2010 18:47:49 +0000 (UTC)

changeset: 12:bc4fcb6b1e4b
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Tue Aug 10 19:47:42 2010 +0100
files: draw.h initfont.c
description:
improved no-locale support

diff -r 671b9e7831f7 -r bc4fcb6b1e4b draw.h
--- a/draw.h Tue Aug 10 18:43:18 2010 +0100
+++ b/draw.h Tue Aug 10 19:47:42 2010 +0100
@@ -36,7 +36,7 @@
 void freedraw(DC *dc);
 unsigned long getcolor(DC *dc, const char *colstr);
 DC *initdraw(void);
-void initfont(DC *dc, const char *s);
+void initfont(DC *dc, const char *fontstr);
 void setcanvas(DC *dc, unsigned int w, unsigned int h);
 int textnw(DC *dc, const char *text, size_t len);
 int textw(DC *dc, const char *text);
diff -r 671b9e7831f7 -r bc4fcb6b1e4b initfont.c
--- a/initfont.c Tue Aug 10 18:43:18 2010 +0100
+++ b/initfont.c Tue Aug 10 19:47:42 2010 +0100
@@ -5,37 +5,45 @@
 #define DEFAULT "fixed"
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
+static Bool loadfont(DC *dc, const char *fontstr);
+
 void
-initfont(DC *dc, const char *s) {
- int i, n;
-
- dc->font.set = NULL;
- if(s == NULL || *s) {
- char *def, **missing;
-
- dc->font.set = XCreateFontSet(dc->dpy, s ? s : DEFAULT, &missing, &n, &def);
- if(missing)
- XFreeStringList(missing);
- }
- if(dc->font.set) {
- char **names;
- XFontStruct **xfonts;
-
- n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
- for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
- dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
- dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
- }
- }
- else if(s != NULL) {
- weprintf("cannot load font '%s'\n", s);
- initfont(dc, NULL);
- }
- else {
- if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, DEFAULT)))
- eprintf("cannot load fallback font\n");
- dc->font.ascent = dc->font.xfont->ascent;
- dc->font.descent = dc->font.xfont->descent;
+initfont(DC *dc, const char *fontstr) {
+ if(!loadfont(dc, fontstr ? fontstr : DEFAULT)) {
+ if(fontstr != NULL)
+ weprintf("cannot load font '%s'\n", fontstr);
+ if(fontstr == NULL || !loadfont(dc, DEFAULT))
+ eprintf("cannot load font '%s'\n", DEFAULT);
         }
         dc->font.height = dc->font.ascent + dc->font.descent;
 }
+
+Bool
+loadfont(DC *dc, const char *fontstr) {
+ if(!*fontstr)
+ return False;
+ if(XSupportsLocale()) {
+ char *def, **missing, **names;
+ int i, n;
+ XFontStruct **xfonts;
+
+ if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
+ n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
+ for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
+ dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
+ dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
+ }
+ }
+ if(missing)
+ XFreeStringList(missing);
+ return dc->font.set != NULL;
+ }
+ else {
+ dc->font.set = NULL;
+ if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
+ dc->font.ascent = dc->font.xfont->ascent;
+ dc->font.descent = dc->font.xfont->descent;
+ }
+ return dc->font.xfont != NULL;
+ }
+}
Received on Tue Aug 10 2010 - 20:47:49 CEST

This archive was generated by hypermail 2.2.0 : Tue Aug 10 2010 - 20:48:03 CEST