Make system.mk logic consistent
[notion/jeffpc.git] / de / fontset.c
blobf8864b3efe99d881dbce70d9e860c6e696557663
1 /*
2 * ion/de/fontset.c
3 *
4 * This file contains routines to attempt to add fonts to a font pattern
5 * so that XCreateFontSet will not fail because the given font(s) do not
6 * contain all the characters required by the locale.
8 * It is based on code by Tomohiro Kubota; see
9 * <http://www.debian.org/doc/manuals/intro-i18n/ch-examples.en.html#s13.4.5>.
11 * Copyright © 1999-2001 Tomohiro KUBOTA.
13 * This manual is free software; you may redistribute it and/or modify it under
14 * the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2, or (at your option) any later
16 * version.
18 * This is distributed in the hope that it will be useful, but without any
19 * warranty; without even the implied warranty of merchantability or fitness
20 * for a particular purpose. See the GNU General Public License for more
21 * details.
23 * You should have received a copy of the GNU General Public License along with
24 * this program; if not, write to the Free Software Foundation, Inc., 51
25 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <string.h>
29 #include <ctype.h>
30 #include <locale.h>
32 #include <ioncore/common.h>
33 #include <ioncore/global.h>
35 #ifndef CF_FONT_ELEMENT_SIZE
36 #define CF_FONT_ELEMENT_SIZE 50
37 #endif
39 #define FNT_D(X) /*X*/
42 static const char *get_font_element(const char *pattern, char *buf,
43 int bufsiz, ...)
45 const char *p, *v;
46 char *p2;
47 va_list va;
49 va_start(va, bufsiz);
50 buf[bufsiz-1]=0;
51 buf[bufsiz-2]='*';
52 while((v=va_arg(va, char *))!=NULL){
53 p=libtu_strcasestr(pattern, v);
54 if(p){
55 strncpy(buf, p+1, bufsiz-2);
56 p2=strchr(buf, '-');
57 if(p2) *p2=0;
58 va_end(va);
59 return p;
62 va_end(va);
63 strncpy(buf, "*", bufsiz);
64 return NULL;
68 static const char *get_font_size(const char *pattern, int *size)
70 const char *p;
71 const char *p2=NULL;
72 int n=0;
74 for(p=pattern; 1; p++){
75 if(!*p){
76 if(p2!=NULL && n>1 && n<72){
77 *size=n; return p2+1;
78 }else{
79 *size=16; return NULL;
81 }else if(*p=='-'){
82 if(n>1 && n<72 && p2!=NULL){
83 *size=n;
84 return p2+1;
86 p2=p; n=0;
87 }else if(*p>='0' && *p<='9' && p2!=NULL){
88 n*=10;
89 n+=*p-'0';
90 }else{
91 p2=NULL; n=0;
97 XFontSet de_create_font_set(const char *fontname)
99 XFontSet fs;
100 char **missing=NULL, *def="-";
101 int nmissing, pixel_size=0;
102 char weight[CF_FONT_ELEMENT_SIZE], slant[CF_FONT_ELEMENT_SIZE];
103 const char *nfontname=fontname;
104 char *pattern2=NULL;
105 int i;
107 FNT_D(fprintf(stderr, "FNTRQ: %s\n", fontname));
109 fs=XCreateFontSet(ioncore_g.dpy, fontname, &missing, &nmissing, &def);
111 if(fs && nmissing==0){
112 if(missing!=NULL)
113 XFreeStringList(missing);
114 return fs;
117 /* Not a warning, nothing serious */
118 FNT_D(fprintf(stderr, "Failed to load fontset.\n"));
120 if(!fs){
121 char *lcc=NULL;
122 const char *lc;
123 if(missing!=NULL)
124 XFreeStringList(missing);
126 lc=setlocale(LC_CTYPE, NULL);
127 if(lc!=NULL && strcmp(lc, "POSIX")!=0 && strcmp(lc, "C")!=0)
128 lcc=scopy(lc);
130 setlocale(LC_CTYPE, "C");
132 fs=XCreateFontSet(ioncore_g.dpy, fontname, &missing, &nmissing, &def);
134 if(lcc!=NULL){
135 setlocale(LC_CTYPE, lcc);
136 free(lcc);
140 #ifndef CF_NO_FONTSET_KLUDGE
142 if(fs){
143 XFontStruct **fontstructs;
144 char **fontnames;
145 XFontsOfFontSet(fs, &fontstructs, &fontnames);
146 nfontname=fontnames[0];
149 get_font_element(nfontname, weight, CF_FONT_ELEMENT_SIZE,
150 "-medium-", "-bold-", "-demibold-", "-regular-", NULL);
151 get_font_element(nfontname, slant, CF_FONT_ELEMENT_SIZE,
152 "-r-", "-i-", "-o-", "-ri-", "-ro-", NULL);
153 get_font_size(nfontname, &pixel_size);
155 if(!strcmp(weight, "*"))
156 strncpy(weight, "medium", CF_FONT_ELEMENT_SIZE);
157 if(!strcmp(slant, "*"))
158 strncpy(slant, "r", CF_FONT_ELEMENT_SIZE);
159 if(pixel_size<3)
160 pixel_size=3;
161 else if(pixel_size>97)
162 pixel_size=97;
164 if(ioncore_g.enc_utf8){
165 libtu_asprintf(&pattern2,
166 "%s,"
167 "-misc-fixed-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
168 "-misc-fixed-*-*-*-*-%d-*-*-*-*-*-*-*",
169 fontname, weight, slant, pixel_size, pixel_size);
170 }else{
171 libtu_asprintf(&pattern2,
172 "%s,"
173 "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
174 "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*",
175 fontname, weight, slant, pixel_size, pixel_size);
178 if(pattern2==NULL)
179 return NULL;
181 FNT_D(fprintf(stderr, "NRQ: %s\n", pattern2));
183 nfontname=pattern2;
185 if(nmissing)
186 XFreeStringList(missing);
187 if(fs)
188 XFreeFontSet(ioncore_g.dpy, fs);
190 FNT_D(if(fs) fprintf(stderr, "Trying '%s'.\n", nfontname));
192 fs=XCreateFontSet(ioncore_g.dpy, nfontname, &missing, &nmissing, &def);
194 free(pattern2);
196 #endif
198 if(missing!=NULL)
199 XFreeStringList(missing);
201 return fs;