1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-20 12:58:08 +01:00

- rewrote the font conversion routines to avoid the need to allocate memory

for font options while parsing the xlfd. now all processing is done using
  the original xlfd string only.
- removed memleaks introduced by previous commit.
This commit is contained in:
dan
2004-04-13 03:49:38 +00:00
parent 500d569c79
commit 6d02fe98f2
2 changed files with 950 additions and 810 deletions

View File

@@ -104,7 +104,7 @@ static char *theme_options[] = {
/* table of style related fonts */ /* table of style related fonts */
static char *font_styles[] = { static char *font_options[] = {
"ClipTitleFont", "ClipTitleFont",
"WindowTitleFont", "WindowTitleFont",
"MenuTitleFont", "MenuTitleFont",
@@ -115,7 +115,7 @@ static char *font_styles[] = {
NULL NULL
}; };
char *default_font = "sans:pixelsize=12"; #define DEFAULT_FONT "sans-serif:pixelsize=12"
char *ProgName; char *ProgName;
@@ -223,33 +223,6 @@ wgethomedir()
} }
void*
mymalloc(int size)
{
void *tmp;
tmp = malloc(size);
if (!tmp) {
abortar("out of memory");
}
return tmp;
}
char*
mystrdup(char *str)
{
char *tmp;
tmp = mymalloc(strlen(str)+1);
strcpy(tmp, str);
return tmp;
}
static char* static char*
getuserhomedir(char *username) getuserhomedir(char *username)
{ {
@@ -271,8 +244,6 @@ getuserhomedir(char *username)
} }
char* char*
wexpandpath(char *path) wexpandpath(char *path)
{ {
@@ -353,7 +324,7 @@ wexpandpath(char *path)
} }
} }
return mystrdup(buffer); return wstrdup(buffer);
} }
@@ -383,7 +354,7 @@ wfindfileinarray(WMPropList *paths, char *file)
return fullpath; return fullpath;
} }
} else { } else {
return mystrdup(file); return wstrdup(file);
} }
} }
@@ -397,7 +368,7 @@ wfindfileinarray(WMPropList *paths, char *file)
continue; continue;
len = strlen(dir); len = strlen(dir);
path = mymalloc(len+flen+2); path = wmalloc(len+flen+2);
path = memcpy(path, dir, len); path = memcpy(path, dir, len);
path[len]=0; path[len]=0;
strcat(path, "/"); strcat(path, "/");
@@ -416,94 +387,187 @@ wfindfileinarray(WMPropList *paths, char *file)
return NULL; return NULL;
} }
char*
capitalize(char *element) static Bool
isFontOption(char *option)
{ {
unsigned int first = 1; int i;
char *p;
char *b; for (i=0; font_options[i]!=NULL; i++) {
b = element; if (strcasecmp(option, font_options[i])==0) {
for (p = b; *p != 0; p++) return True;
{
if (isalpha(*p) && first) {
first = 0;
*p = toupper(*p);
} }
else if (*p == '-' || *p == ' ') {
first = 1;
}
}
return b;
} }
char* return False;
getElementFromXLFD(const char *xlfd, int index) }
static int
countChar(char *str, char c)
{ {
const char *p = xlfd; int count = 0;
while (*p != 0) {
if (*p == '-' && --index == 0) { if (!str)
const char *end = strchr(p + 1, '-'); return 0;
char *buf;
size_t len; for (; *str!=0; str++) {
if (end == 0) end = p + strlen(p); if (*str == c) {
len = end - (p + 1); count++;
buf = wmalloc(len);
memcpy(buf, p + 1, len);
buf[len] = 0;
return buf;
} }
p++;
} }
return "*";
return count;
} }
typedef struct str {
char *str;
int len;
} str;
#define XLFD_TOKENS 14
static str*
getXLFDTokens(char *xlfd)
{
static str tokens[XLFD_TOKENS];
int i, len, size;
char *ptr;
if (!xlfd || countChar(xlfd, '-')<XLFD_TOKENS)
return NULL;
memset(tokens, 0, sizeof(str)*XLFD_TOKENS);
len = strlen(xlfd);
for (ptr=xlfd, i=0; i<XLFD_TOKENS && len>0; i++) {
size = strspn(ptr, "-");
ptr += size;
len -= size;
if (len <= 0)
break;
size = strcspn(ptr, "-");
if (size==0)
break;
tokens[i].str = ptr;
tokens[i].len = size;
ptr += size;
len -= size;
}
return tokens;
}
static int
strToInt(str *token)
{
int res=0, pos, c;
if (token->len==0 || token->str[0]=='*') {
return -1;
} else {
for (res=0, pos=0; pos<token->len; pos++) {
c = token->str[pos] - '0';
if (c<0 || c>9)
break;
res = res*10 + c;
}
}
return res;
}
static char*
mapSlantToName(str *slant)
{
if (slant->len==0 || slant->str[0]=='*')
return "roman";
switch(slant->str[0]) {
case 'i':
return "italic";
case 'o':
return "oblique";
case 'r':
default:
return "roman";
}
}
char* char*
xlfdToFc(char *xlfd) xlfdToFc(char *xlfd)
{ {
char *Fcname = NULL; str *tokens, *family, *weight, *slant;
char *name, buf[512];
int size, pixelsize;
char *family = getElementFromXLFD(xlfd, 2); tokens = getXLFDTokens(xlfd);
char *size = getElementFromXLFD(xlfd, 7); if (!tokens)
char *weight = getElementFromXLFD(xlfd, 3); return wstrdup(DEFAULT_FONT);
char *slant = getElementFromXLFD(xlfd, 4);
if (strcmp(family, "*") != 0) { family = &(tokens[1]);
Fcname = wstrconcat(Fcname, capitalize(family)); weight = &(tokens[2]);
} slant = &(tokens[3]);
if (strcmp(size, "*") != 0) {
Fcname = wstrconcat(Fcname, ":pixelsize=");
Fcname = wstrconcat(Fcname, size);
}
if (strcmp(weight, "*") != 0) {
Fcname = wstrconcat(Fcname, ":weight=");
Fcname = wstrconcat(Fcname, capitalize(weight));
}
if (strcmp(slant, "*") != 0) {
if (strcmp(slant, "i") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Italic");
} else if (strcmp(slant, "o") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Oblique");
} else if (strcmp(slant, "ri") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Rev Italic");
} else if (strcmp(slant, "ro") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Rev Oblique");
}
}
if (!Fcname)
Fcname = wstrdup(default_font);
wfree(family); if (family->len==0 || family->str[0]=='*')
wfree(size); return wstrdup(DEFAULT_FONT);
wfree(weight);
wfree(slant);
return Fcname; sprintf(buf, "%.*s", family->len, family->str);
name = wstrdup(buf);
pixelsize = strToInt(&tokens[6]);
size = strToInt(&tokens[7]);
if (size<=0 && pixelsize<=0) {
name = wstrappend(name, ":pixelsize=12");
} else if (pixelsize>0) {
/* if pixelsize is present size will be ignored so we skip it */
sprintf(buf, ":pixelsize=%d", pixelsize);
name = wstrappend(name, buf);
} else {
sprintf(buf, "-%d", size/10);
name = wstrappend(name, buf);
} }
if (weight->len>0 && weight->str[0]!='*') {
sprintf(buf, ":weight=%.*s", weight->len, weight->str);
name = wstrappend(name, buf);
}
if (slant->len>0 && slant->str[0]!='*') {
sprintf(buf, ":slant=%s", mapSlantToName(slant));
name = wstrappend(name, buf);
}
name = wstrappend(name, ":xlfd=");
name = wstrappend(name, xlfd);
return name;
}
/* return converted font (if conversion is needed) else the original font */
static char*
convertFont(char *font)
{
if (font[0]=='-') {
if (!strchr(font, ',')) {
return xlfdToFc(font);
} else {
wwarning("fontsets are not supported. replaced "
"with default %s", DEFAULT_FONT);
return wstrdup(DEFAULT_FONT);
}
} else {
return font;
}
}
void void
copyFile(char *dir, char *file) copyFile(char *dir, char *file)
{ {
@@ -542,13 +606,13 @@ makeThemePack(WMPropList *style, char *themeName)
int i; int i;
char *themeDir; char *themeDir;
themeDir = mymalloc(strlen(themeName)+50); themeDir = wmalloc(strlen(themeName)+50);
sprintf(themeDir, "%s.themed", themeName); sprintf(themeDir, "%s.themed", themeName);
ThemePath = themeDir; ThemePath = themeDir;
{ {
char *tmp; char *tmp;
tmp = mymalloc(strlen(themeDir)+20); tmp = wmalloc(strlen(themeDir)+20);
sprintf(tmp, "/bin/mkdir \"%s\"", themeDir); sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
if (system(tmp)!=0) { if (system(tmp)!=0) {
printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir); printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
@@ -588,7 +652,7 @@ makeThemePack(WMPropList *style, char *themeName)
if (p) { if (p) {
copyFile(themeDir, WMGetFromPLString(file)); copyFile(themeDir, WMGetFromPLString(file));
newPath = mystrdup(p+1); newPath = wstrdup(p+1);
WMDeleteFromPLArray(value, 1); WMDeleteFromPLArray(value, 1);
WMInsertInPLArray(value, 1, WMCreatePLString(newPath)); WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
free(newPath); free(newPath);
@@ -606,7 +670,7 @@ makeThemePack(WMPropList *style, char *themeName)
if (p) { if (p) {
copyFile(themeDir, WMGetFromPLString(file)); copyFile(themeDir, WMGetFromPLString(file));
newPath = mystrdup(p+1); newPath = wstrdup(p+1);
WMDeleteFromPLArray(value, 1); WMDeleteFromPLArray(value, 1);
WMInsertInPLArray(value, 1, WMCreatePLString(newPath)); WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
free(newPath); free(newPath);
@@ -621,7 +685,7 @@ makeThemePack(WMPropList *style, char *themeName)
if (p) { if (p) {
copyFile(themeDir, WMGetFromPLString(file)); copyFile(themeDir, WMGetFromPLString(file));
newPath = mystrdup(p+1); newPath = wstrdup(p+1);
WMDeleteFromPLArray(value, 2); WMDeleteFromPLArray(value, 2);
WMInsertInPLArray(value, 2, WMCreatePLString(newPath)); WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
free(newPath); free(newPath);
@@ -641,8 +705,7 @@ main(int argc, char **argv)
{ {
WMPropList *prop, *style, *key, *val; WMPropList *prop, *style, *key, *val;
char *path; char *path;
int i, theme_too=0; int i, theme_too=0, make_pack=0;
int make_pack = 0;
char *style_file = NULL; char *style_file = NULL;
@@ -702,37 +765,28 @@ main(int argc, char **argv)
style = WMCreatePLDictionary(NULL, NULL, NULL); style = WMCreatePLDictionary(NULL, NULL, NULL);
for (i=0; options[i]!=NULL; i++) { for (i=0; options[i]!=NULL; i++) {
key = WMCreatePLString(options[i]); key = WMCreatePLString(options[i]);
val = WMGetFromPLDictionary(prop, key); val = WMGetFromPLDictionary(prop, key);
if (val) { if (val) {
int j; WMRetainPropList(val);
char *str = WMGetFromPLString(key); if (isFontOption(options[i])) {
for (j = 0; font_styles[j]!=NULL; j++) { char *newfont, *oldfont;
if (strcasecmp(str, font_styles[j]) == 0) {
char *oldfont;
oldfont = WMGetFromPLString(val); oldfont = WMGetFromPLString(val);
if (oldfont[0] == '-') { newfont = convertFont(oldfont);
if (!strchr(oldfont, ',')) /* font is a reference to old if conversion is not needed */
{ if (newfont != oldfont) {
char *newfont; WMReleasePropList(val);
newfont = xlfdToFc(oldfont);
val = WMCreatePLString(newfont); val = WMCreatePLString(newfont);
break; wfree(newfont);
} else {
wwarning("fontsets are not supported. replaced with default %s", default_font);
val = WMCreatePLString(default_font);
break;
}
} else {
break;
}
} }
} }
WMPutInPLDictionary(style, key, val); WMPutInPLDictionary(style, key, val);
WMReleasePropList(val);
} }
WMReleasePropList(key);
} }
val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath")); val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
@@ -754,7 +808,7 @@ main(int argc, char **argv)
makeThemePack(style, style_file); makeThemePack(style, style_file);
path = mymalloc(strlen(ThemePath)+32); path = wmalloc(strlen(ThemePath)+32);
strcpy(path, ThemePath); strcpy(path, ThemePath);
strcat(path, "/style"); strcat(path, "/style");
WMWritePropListToFile(style, path, False); WMWritePropListToFile(style, path, False);

View File

@@ -37,7 +37,7 @@
#define MAX_OPTIONS 128 #define MAX_OPTIONS 128
char *default_font = "sans:pixelsize=12"; #define DEFAULT_FONT "sans:pixelsize=12"
char *FontOptions[] = { char *FontOptions[] = {
"IconTitleFont", "IconTitleFont",
@@ -80,92 +80,202 @@ Display *dpy;
WMPropList *readBlackBoxStyle(char *path); WMPropList *readBlackBoxStyle(char *path);
char*
capitalize(char *element) static Bool
isCursorOption(char *option)
{ {
unsigned int first = 1; int i;
char *p;
char *b; for (i=0; CursorOptions[i]!=NULL; i++) {
b = element; if (strcasecmp(option, CursorOptions[i])==0) {
for (p = b; *p != 0; p++) { return True;
if (isalpha(*p) && first) {
first = 0;
*p = toupper(*p);
} else if (*p == '-' || *p == ' ') {
first = 1;
} }
} }
return b;
}
char* return False;
getElementFromXLFD(const char *xlfd, int index) }
static Bool
isFontOption(char *option)
{ {
const char *p = xlfd; int i;
while (*p != 0) {
if (*p == '-' && --index == 0) { for (i=0; FontOptions[i]!=NULL; i++) {
const char *end = strchr(p + 1, '-'); if (strcasecmp(option, FontOptions[i])==0) {
char *buf; return True;
size_t len;
if (end == 0) end = p + strlen(p);
len = end - (p + 1);
buf = wmalloc(len);
memcpy(buf, p + 1, len);
buf[len] = 0;
return buf;
} }
p++;
} }
return "*";
return False;
} }
static int
countChar(char *str, char c)
{
int count = 0;
if (!str)
return 0;
for (; *str!=0; str++) {
if (*str == c) {
count++;
}
}
return count;
}
typedef struct str {
char *str;
int len;
} str;
#define XLFD_TOKENS 14
static str*
getXLFDTokens(char *xlfd)
{
static str tokens[XLFD_TOKENS];
int i, len, size;
char *ptr;
if (!xlfd || countChar(xlfd, '-')<XLFD_TOKENS)
return NULL;
memset(tokens, 0, sizeof(str)*XLFD_TOKENS);
len = strlen(xlfd);
for (ptr=xlfd, i=0; i<XLFD_TOKENS && len>0; i++) {
size = strspn(ptr, "-");
ptr += size;
len -= size;
if (len <= 0)
break;
size = strcspn(ptr, "-");
if (size==0)
break;
tokens[i].str = ptr;
tokens[i].len = size;
ptr += size;
len -= size;
}
return tokens;
}
static int
strToInt(str *token)
{
int res=0, pos, c;
if (token->len==0 || token->str[0]=='*') {
return -1;
} else {
for (res=0, pos=0; pos<token->len; pos++) {
c = token->str[pos] - '0';
if (c<0 || c>9)
break;
res = res*10 + c;
}
}
return res;
}
static char*
mapSlantToName(str *slant)
{
if (slant->len==0 || slant->str[0]=='*')
return "roman";
switch(slant->str[0]) {
case 'i':
return "italic";
case 'o':
return "oblique";
case 'r':
default:
return "roman";
}
}
char* char*
xlfdToFc(char *xlfd) xlfdToFc(char *xlfd)
{ {
char *Fcname = NULL; str *tokens, *family, *weight, *slant;
char *name, buf[512];
int size, pixelsize;
char *family = getElementFromXLFD(xlfd, 2); tokens = getXLFDTokens(xlfd);
char *size = getElementFromXLFD(xlfd, 7); if (!tokens)
char *weight = getElementFromXLFD(xlfd, 3); return wstrdup(DEFAULT_FONT);
char *slant = getElementFromXLFD(xlfd, 4);
if (strcmp(family, "*") != 0) { family = &(tokens[1]);
Fcname = wstrconcat(Fcname, capitalize(family)); weight = &(tokens[2]);
} slant = &(tokens[3]);
if (strcmp(size, "*") != 0) {
Fcname = wstrconcat(Fcname, ":pixelsize=");
Fcname = wstrconcat(Fcname, size);
}
if (strcmp(weight, "*") != 0) {
Fcname = wstrconcat(Fcname, ":weight=");
Fcname = wstrconcat(Fcname, capitalize(weight));
}
if (strcmp(slant, "*") != 0) {
if (strcmp(slant, "i") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Italic");
} else if (strcmp(slant, "o") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Oblique");
} else if (strcmp(slant, "ri") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Rev Italic");
} else if (strcmp(slant, "ro") == 0) {
Fcname = wstrconcat(Fcname, ":slant=");
Fcname = wstrconcat(Fcname, "Rev Oblique");
}
}
if (!Fcname)
Fcname = wstrdup(default_font);
wfree(family); if (family->len==0 || family->str[0]=='*')
wfree(size); return wstrdup(DEFAULT_FONT);
wfree(weight);
wfree(slant);
return Fcname; sprintf(buf, "%.*s", family->len, family->str);
name = wstrdup(buf);
pixelsize = strToInt(&tokens[6]);
size = strToInt(&tokens[7]);
if (size<=0 && pixelsize<=0) {
name = wstrappend(name, ":pixelsize=12");
} else if (pixelsize>0) {
/* if pixelsize is present size will be ignored so we skip it */
sprintf(buf, ":pixelsize=%d", pixelsize);
name = wstrappend(name, buf);
} else {
sprintf(buf, "-%d", size/10);
name = wstrappend(name, buf);
} }
if (weight->len>0 && weight->str[0]!='*') {
sprintf(buf, ":weight=%.*s", weight->len, weight->str);
name = wstrappend(name, buf);
}
if (slant->len>0 && slant->str[0]!='*') {
sprintf(buf, ":slant=%s", mapSlantToName(slant));
name = wstrappend(name, buf);
}
name = wstrappend(name, ":xlfd=");
name = wstrappend(name, xlfd);
return name;
}
/* return converted font (if conversion is needed) else the original font */
static char*
convertFont(char *font)
{
if (font[0]=='-') {
if (!strchr(font, ',')) {
return xlfdToFc(font);
} else {
wwarning("fontsets are not supported. replaced "
"with default %s", DEFAULT_FONT);
return wstrdup(DEFAULT_FONT);
}
} else {
return font;
}
}
char* char*
defaultsPathForDomain(char *domain) defaultsPathForDomain(char *domain)
{ {
@@ -377,54 +487,30 @@ hackStyle(WMPropList *style)
tmp = WMGetFromPLArray(keys, i); tmp = WMGetFromPLArray(keys, i);
str = WMGetFromPLString(tmp); str = WMGetFromPLString(tmp);
if (str) { if (str) {
int j, found; if (ignoreFonts && isFontOption(str)) {
if (ignoreFonts) {
for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
if (strcasecmp(str, FontOptions[j])==0) {
WMRemoveFromPLDictionary(style, tmp); WMRemoveFromPLDictionary(style, tmp);
found = 1;
break;
}
}
if (found)
continue; continue;
} }
if (ignoreCursors) { if (ignoreCursors && isCursorOption(str)) {
for (j = 0, found = 0; CursorOptions[j] != NULL; j++) {
if (strcasecmp(str, CursorOptions[j]) == 0) {
WMRemoveFromPLDictionary(style, tmp); WMRemoveFromPLDictionary(style, tmp);
found = 1;
break;
}
}
if (found)
continue; continue;
} }
for (j = 0; FontOptions[j]!=NULL; j++) { if (isFontOption(str)) {
if (strcasecmp(str, FontOptions[j]) == 0) {
char *oldfont;
WMPropList *value; WMPropList *value;
value = WMGetFromPLDictionary(style, tmp); char *newfont, *oldfont;
oldfont = WMGetFromPLString(value);
if (oldfont[0] == '-') {
if (!strchr(oldfont, ',')) {
char *newfont;
newfont = xlfdToFc(oldfont);
WMPutInPLDictionary(style, tmp, WMCreatePLString(newfont));
break;
} else {
wwarning("fontsets are not supported. replaced with default: %s", default_font);
WMPutInPLDictionary(style, tmp,
WMCreatePLString(default_font));
break;
}
} else {
break;
}
}
}
value = WMGetFromPLDictionary(style, tmp);
if (value) {
oldfont = WMGetFromPLString(value);
newfont = convertFont(oldfont);
if (newfont != oldfont) {
value = WMCreatePLString(newfont);
WMPutInPLDictionary(style, tmp, value);
WMReleasePropList(value);
wfree(newfont);
}
}
}
if (strcasecmp(str, "IconTitleColor")==0 if (strcasecmp(str, "IconTitleColor")==0
|| strcasecmp(str, "IconTitleBack")==0) { || strcasecmp(str, "IconTitleBack")==0) {
foundIconTitle = 1; foundIconTitle = 1;