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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#ifndef lint
static char *rcsid = "$Id: fontset.c,v 1.9 1994/06/02 04:59:23 ishisone Rel $";
#endif
/*
* Copyright (c) 1991 Software Research Associates, Inc.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Software Research Associates not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. Software Research
* Associates makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied
* warranty.
*
* Author: Makoto Ishisone, Software Research Associates, Inc., Japan
*
* a FontSet handler for kinput2.
*/
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>
#include <X11/Xmu/CharSet.h>
#include "CachedAtom.h"
#include "CachedFont.h"
#include "KIFontSet.h"
#define DEBUG_VAR debug_fontset
#include "DebugPrint.h"
typedef struct {
String name;
String xlfdname;
} FSFontName;
static char noname[] = "no";
static int match();
static Cardinal parseFontSet();
static Boolean tryCharSet();
static XFontStruct *csSuppliedMatchFont();
static XFontStruct *exactMatchFont();
static XFontStruct *csReplacedMatchFont();
static String getXLFDName();
/*- match: returns 1 if the specified string matches the pattern -*/
static int
match(s, p)
register char *s; /* string */
register char *p; /* pattern */
{
register int tmp;
while ((tmp = *p++) != '\0') {
switch (tmp) {
case '?':
if (*s++ == '\0') return 0;
continue;
case '*':
while ((tmp = match(s, p)) == 0) {
if (*s++ == '\0') return -1;
}
return tmp;
default:
if (*s++ != tmp) return 0;
continue;
}
}
return (*s == '\0');
}
/*- countCommas: count number of commas in the string -*/
static Cardinal
countCommas(s)
String s;
{
int n = 0;
while (*s != '\0') {
if (*s++ == ',') n++;
}
return n;
}
/*- parseFontSet: separate each font in a font name list -*/
static Cardinal
parseFontSet(spec, basenames)
String spec; /* IN */
FSFontName *basenames; /* OUT */
{
register char *p, *q;
Cardinal nnames;
int c;
nnames = 0;
p = spec;
for (;;) {
/* skip leading blanks */
while ((c = *p) != '\0' && (c == ' ' || c == '\t' || c == '\n')) p++;
if (c == '\0') break;
basenames[nnames++].name = p;
/* find comma or NUL char */
for (q = p; (c = *q) != '\0' && c != ','; q++) ;
/* omit trailing blanks */
p = q - 1;
while ((c = *p) == ' ' || c == '\t' || c == '\n') *p-- = '\0';
if (*q == '\0') break;
*q = '\0';
p = q + 1;
}
#ifdef DEBUG
if (DDEBUG_CONDITION(10)) {
if (nnames == 0) {
printf("\tparse error\n");
} else {
int i;
printf("\t%d elements\n", nnames);
for (i = 0; i < nnames; i++) printf("\t\t%s\n", basenames[i].name);
}
}
#endif
return nnames;
}
/*- tryCharSet: apply specified function to the fontnames for getting appropriate font for given character set -*/
static Boolean
tryCharSet(dpy, cset, extfont, namelist, numlist, func)
Display *dpy;
KICharSet *cset;
KICharSetFont *extfont;
FSFontName *namelist;
Cardinal numlist;
XFontStruct *(*func)();
{
KICharSetSpec *specs;
Cardinal i;
XFontStruct *font;
for (i = 0, specs = cset->specs; i < cset->num_specs; i++, specs++) {
font = (*func)(dpy, specs->pattern, namelist, numlist);
if (font != NULL) {
extfont->charset = specs->pattern;
extfont->cldata = specs->cldata;
extfont->font = font;
return True;
}
}
return False;
}
static XFontStruct *
exactMatchFont(dpy, csetstr, namelist, numlist)
Display *dpy;
String csetstr;
FSFontName *namelist;
Cardinal numlist;
{
XFontStruct *font;
while (numlist-- > 0) {
String name = (namelist++)->name;
String p;
int namelen;
int hyphen;
if (*name != '-' && *name != '+') continue; /* not an XLFD name */
namelen = strlen(name);
for (p = name + namelen - 1, hyphen = 0; p > name; p--) {
if (*p == '-') hyphen++;
if (hyphen == 2) goto found;
}
continue; /* doesn't contain charset part */
found:
p++; /* now p points the charset part of the fontname */
if (match(p, csetstr) > 0 &&
(font = CachedLoadQueryFontByName(dpy, name)) != NULL) {
TRACE(("\tmatched in exactMatchFont()\n"));
TRACE(("\t\tcset=%s, font=%s\n", csetstr, name));
return font;
}
}
return NULL;
}
static XFontStruct *
csSuppliedMatchFont(dpy, csetstr, namelist, numlist)
Display *dpy;
String csetstr;
FSFontName *namelist;
Cardinal numlist;
{
XFontStruct *font;
while (numlist-- > 0) {
String name = (namelist++)->name;
char namebuf[512];
if (*name != '-' && *name != '+') continue; /* not an XLFD name */
(void)strcpy(namebuf, name);
(void)strcat(namebuf, "-");
(void)strcat(namebuf, csetstr);
if ((font = CachedLoadQueryFontByName(dpy, namebuf)) != NULL) {
TRACE(("\tmatched in csSuppliedMatchFont()\n"));
TRACE(("\t\tcset=%s, font=%s\n", csetstr, namebuf));
return font;
}
}
return NULL;
}
static XFontStruct *
csReplacedMatchFont(dpy, csetstr, namelist, numlist)
Display *dpy;
String csetstr;
FSFontName *namelist;
Cardinal numlist;
{
XFontStruct *font;
while (numlist-- > 0) {
String name = namelist->name;
char namebuf[512];
String p;
int namelen;
int hyphen;
if (*name != '-' && *name != '+') { /* non XLFD name */
if (namelist->xlfdname == NULL ||
(namelist->xlfdname == noname &&
(namelist->xlfdname = getXLFDName(dpy, name)) == NULL)) {
/* this font doesn't have XLFD name */
namelist++;
continue;
}
name = namelist->xlfdname;
}
namelist++;
(void)strcpy(namebuf, name);
namelen = strlen(namebuf);
/* find charset part of the font name */
for (p = namebuf + namelen - 1, hyphen = 0; p > namebuf; p--) {
if (*p == '-') hyphen++;
if (hyphen == 2) goto found;
}
continue; /* doesn't contain charset part */
found:
p++; /* now p points the charset part of the fontname */
/* replace charset part */
(void)strcpy(p, csetstr);
if ((font = CachedLoadQueryFontByName(dpy, namebuf)) != NULL) {
TRACE(("\tmatched in csReplacedMatchFont()\n"));
TRACE(("\t\tcset=%s, font=%s\n", csetstr, namebuf));
return font;
}
}
return NULL;
}
/*- getXLFDName: obtain XLFD font name from a non XLFD font name -*/
static String
getXLFDName(dpy, nonxlfdname)
Display *dpy;
String nonxlfdname;
{
XFontStruct *font;
Atom fontprop;
String name;
TRACE(("getXLFDName(%s)\n", nonxlfdname));
font = CachedLoadQueryFontByName(dpy, nonxlfdname);
if (font == NULL) {
DPRINT(("getXLFDName(%s):CachedLoadQueryFontByName() failed\n",
nonxlfdname));
return NULL;
}
if (!XGetFontProperty(font, XA_FONT, (unsigned long *)&fontprop)) {
/* can't get 'FONT' property. so XLFD name cannot be obtained */
DPRINT(("getXLFDName(%s): can't get FONT property\n",
nonxlfdname));
CachedFreeFont(dpy, font);
return NULL;
}
CachedFreeFont(dpy, font);
name = CachedGetAtomName(dpy, fontprop);
TRACE(("\tgot %s\n", name));
return (*name == '-' || *name == '+') ? name : NULL;
}
/*
* public functions
*/
int
ExtractFontsFromFontSet(dpy, fontset, charsets, exfonts, numcsets)
Display *dpy;
String fontset;
KICharSet *charsets;
KICharSetFont *exfonts;
Cardinal numcsets;
{
Cardinal nfonts;
String fsp;
char fsbuf[1024];
FSFontName *fnp;
FSFontName fnbuf[20];
int nfound;
int i;
TRACE(("ExtractFontsFromFontSet(fontset=%s)\n", fontset));
for (i = 0; i < numcsets; i++) {
exfonts[i].font = NULL;
exfonts[i].charset = NULL;
exfonts[i].cldata = NULL;
}
if (strlen(fontset) >= sizeof(fsbuf)) {
fsp = XtMalloc((unsigned int)(strlen(fontset) + 1));
} else {
fsp = fsbuf;
}
XmuCopyISOLatin1Lowered(fsp, fontset);
if ((nfonts = countCommas(fsp) + 1) >= XtNumber(fnbuf)) {
fnp = (FSFontName *)XtMalloc(nfonts * sizeof(FSFontName));
} else {
fnp = fnbuf;
}
for (i = 0; i < nfonts; i++) fnp[i].xlfdname = noname;
if ((nfonts = parseFontSet(fsp, fnp)) == 0) {
if (fsp != fsbuf) XtFree(fsp);
if (fnp != fnbuf) XtFree((char *)fnp);
return 0;
}
nfound = 0;
for (i = 0; i < numcsets; i++) {
KICharSet *cp = charsets + i;
KICharSetFont *fp = exfonts + i;
TRACE(("\tfor charset #%d\n", i));
if (tryCharSet(dpy, cp, fp, fnp, nfonts, exactMatchFont) ||
tryCharSet(dpy, cp, fp, fnp, nfonts, csSuppliedMatchFont) ||
tryCharSet(dpy, cp, fp, fnp, nfonts, csReplacedMatchFont)) {
nfound++;
}
}
if (fsp != fsbuf) XtFree(fsp);
if (fnp != fnbuf) XtFree((char *)fnp);
return nfound;
}
|