1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* ion/de/fontset.c
*
* This file contains routines to attempt to add fonts to a font pattern
* so that XCreateFontSet will not fail because the given font(s) do not
* contain all the characters required by the locale. The original code
* was apparently written by Tomohiro Kubota; see
* <http://www.debian.org/doc/manuals/intro-i18n/ch-examples.en.html#s13.4.5>.
*
*/
#include <string.h>
#include <ctype.h>
#include <locale.h>
#include <ioncore/common.h>
#include <ioncore/global.h>
#ifndef CF_FONT_ELEMENT_SIZE
#define CF_FONT_ELEMENT_SIZE 50
#endif
#define FNT_D(X) /*X*/
static const char *mystrcasestr(const char *str, const char *ptn)
{
const char *s2, *p2;
for(; *str; str++) {
for(s2=str,p2=ptn; ; s2++,p2++) {
if (!*p2) return str;
if (toupper(*s2) != toupper(*p2)) break;
}
}
return NULL;
}
static const char *get_font_element(const char *pattern, char *buf,
int bufsiz, ...)
{
const char *p, *v;
char *p2;
va_list va;
va_start(va, bufsiz);
buf[bufsiz-1] = 0;
buf[bufsiz-2] = '*';
while((v = va_arg(va, char *)) != NULL) {
p = mystrcasestr(pattern, v);
if (p) {
strncpy(buf, p+1, bufsiz-2);
p2 = strchr(buf, '-');
if (p2) *p2=0;
va_end(va);
return p;
}
}
va_end(va);
strncpy(buf, "*", bufsiz);
return NULL;
}
static const char *get_font_size(const char *pattern, int *size)
{
const char *p;
const char *p2=NULL;
int n=0;
for (p=pattern; 1; p++) {
if (!*p) {
if (p2!=NULL && n>1 && n<72) {
*size = n; return p2+1;
} else {
*size = 16; return NULL;
}
} else if (*p=='-') {
if (n>1 && n<72 && p2!=NULL) {
*size = n;
return p2+1;
}
p2=p; n=0;
} else if (*p>='0' && *p<='9' && p2!=NULL) {
n *= 10;
n += *p-'0';
} else {
p2=NULL; n=0;
}
}
}
XFontSet de_create_font_set(const char *fontname)
{
XFontSet fs;
char **missing, *def = "-";
int nmissing, pixel_size = 0;
char weight[CF_FONT_ELEMENT_SIZE], slant[CF_FONT_ELEMENT_SIZE];
const char *nfontname = fontname;
char *pattern2 = NULL;
FNT_D(fprintf(stderr, "FNTRQ: %s\n", fontname));
fs = XCreateFontSet(wglobal.dpy, fontname, &missing, &nmissing, &def);
if (fs && (! nmissing))
return fs;
/* Not a warning, nothing serious */
FNT_D(fprintf(stderr, "Failed to load fonset.\n"));
if (! fs) {
char *lcc=NULL;
const char *lc;
if (nmissing)
XFreeStringList(missing);
lc=setlocale(LC_CTYPE, NULL);
if(lc!=NULL && strcmp(lc, "POSIX")!=0 && strcmp(lc, "C")!=0)
lcc=scopy(lc);
setlocale(LC_CTYPE, "C");
fs = XCreateFontSet(wglobal.dpy, fontname, &missing, &nmissing, &def);
if(lcc!=NULL){
setlocale(LC_CTYPE, lcc);
free(lcc);
}
}
if (fs) {
XFontStruct **fontstructs;
char **fontnames;
XFontsOfFontSet(fs, &fontstructs, &fontnames);
nfontname = fontnames[0];
}
/*if(fs){
XFontStruct **fontstructs;
char **fontnames;
int i, n=XFontsOfFontSet(fs, &fontstructs, &fontnames);
for(i=0; fontnames[i] && i<n; i++)
fprintf(stderr, "%s\n", fontnames[i]);
}*/
get_font_element(nfontname, weight, CF_FONT_ELEMENT_SIZE,
"-medium-", "-bold-", "-demibold-", "-regular-", NULL);
get_font_element(nfontname, slant, CF_FONT_ELEMENT_SIZE,
"-r-", "-i-", "-o-", "-ri-", "-ro-", NULL);
get_font_size(nfontname, &pixel_size);
if (! strcmp(weight, "*"))
strncpy(weight, "medium", CF_FONT_ELEMENT_SIZE);
if (! strcmp(slant, "*"))
strncpy(slant, "r", CF_FONT_ELEMENT_SIZE);
if (pixel_size < 3)
pixel_size = 3;
else if (pixel_size > 97)
pixel_size = 97;
libtu_asprintf(&pattern2,
"%s,"
"-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
"-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*",
fontname, weight, slant, pixel_size, pixel_size);
if(pattern2==NULL)
return NULL;
FNT_D(fprintf(stderr, "NRQ: %s\n", pattern2));
nfontname = pattern2;
if (nmissing)
XFreeStringList(missing);
if (fs)
XFreeFontSet(wglobal.dpy, fs);
FNT_D(if(fs) fprintf(stderr, "Trying '%s'.\n", nfontname));
fs = XCreateFontSet(wglobal.dpy, nfontname, &missing, &nmissing, &def);
free(pattern2);
/*if(fs){
XFontStruct **fontstructs;
char **fontnames;
int i, n=XFontsOfFontSet(fs, &fontstructs, &fontnames);
for(i=0; fontnames[i] && i<n; i++)
fprintf(stderr, "%s\n", fontnames[i]);
}*/
return fs;
}
|