1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-20 04:48:06 +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 */
static char *font_styles[] = {
static char *font_options[] = {
"ClipTitleFont",
"WindowTitleFont",
"MenuTitleFont",
@@ -115,7 +115,7 @@ static char *font_styles[] = {
NULL
};
char *default_font = "sans:pixelsize=12";
#define DEFAULT_FONT "sans-serif:pixelsize=12"
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*
getuserhomedir(char *username)
{
@@ -271,8 +244,6 @@ getuserhomedir(char *username)
}
char*
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;
}
} else {
return mystrdup(file);
return wstrdup(file);
}
}
@@ -397,7 +368,7 @@ wfindfileinarray(WMPropList *paths, char *file)
continue;
len = strlen(dir);
path = mymalloc(len+flen+2);
path = wmalloc(len+flen+2);
path = memcpy(path, dir, len);
path[len]=0;
strcat(path, "/");
@@ -416,94 +387,187 @@ wfindfileinarray(WMPropList *paths, char *file)
return NULL;
}
char*
capitalize(char *element)
static Bool
isFontOption(char *option)
{
unsigned int first = 1;
char *p;
char *b;
b = element;
for (p = b; *p != 0; p++)
{
if (isalpha(*p) && first) {
first = 0;
*p = toupper(*p);
}
else if (*p == '-' || *p == ' ') {
first = 1;
int i;
for (i=0; font_options[i]!=NULL; i++) {
if (strcasecmp(option, font_options[i])==0) {
return True;
}
}
return b;
return False;
}
char*
getElementFromXLFD(const char *xlfd, int index)
static int
countChar(char *str, char c)
{
const char *p = xlfd;
while (*p != 0) {
if (*p == '-' && --index == 0) {
const char *end = strchr(p + 1, '-');
char *buf;
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;
int count = 0;
if (!str)
return 0;
for (; *str!=0; str++) {
if (*str == c) {
count++;
}
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*
xlfdToFc(char *xlfd)
{
char *Fcname = NULL;
str *tokens, *family, *weight, *slant;
char *name, buf[512];
int size, pixelsize;
char *family = getElementFromXLFD(xlfd, 2);
char *size = getElementFromXLFD(xlfd, 7);
char *weight = getElementFromXLFD(xlfd, 3);
char *slant = getElementFromXLFD(xlfd, 4);
tokens = getXLFDTokens(xlfd);
if (!tokens)
return wstrdup(DEFAULT_FONT);
if (strcmp(family, "*") != 0) {
Fcname = wstrconcat(Fcname, capitalize(family));
}
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);
family = &(tokens[1]);
weight = &(tokens[2]);
slant = &(tokens[3]);
wfree(family);
wfree(size);
wfree(weight);
wfree(slant);
if (family->len==0 || family->str[0]=='*')
return wstrdup(DEFAULT_FONT);
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
copyFile(char *dir, char *file)
{
@@ -542,13 +606,13 @@ makeThemePack(WMPropList *style, char *themeName)
int i;
char *themeDir;
themeDir = mymalloc(strlen(themeName)+50);
themeDir = wmalloc(strlen(themeName)+50);
sprintf(themeDir, "%s.themed", themeName);
ThemePath = themeDir;
{
char *tmp;
tmp = mymalloc(strlen(themeDir)+20);
tmp = wmalloc(strlen(themeDir)+20);
sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
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);
@@ -588,7 +652,7 @@ makeThemePack(WMPropList *style, char *themeName)
if (p) {
copyFile(themeDir, WMGetFromPLString(file));
newPath = mystrdup(p+1);
newPath = wstrdup(p+1);
WMDeleteFromPLArray(value, 1);
WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
free(newPath);
@@ -606,7 +670,7 @@ makeThemePack(WMPropList *style, char *themeName)
if (p) {
copyFile(themeDir, WMGetFromPLString(file));
newPath = mystrdup(p+1);
newPath = wstrdup(p+1);
WMDeleteFromPLArray(value, 1);
WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
free(newPath);
@@ -621,7 +685,7 @@ makeThemePack(WMPropList *style, char *themeName)
if (p) {
copyFile(themeDir, WMGetFromPLString(file));
newPath = mystrdup(p+1);
newPath = wstrdup(p+1);
WMDeleteFromPLArray(value, 2);
WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
free(newPath);
@@ -641,8 +705,7 @@ main(int argc, char **argv)
{
WMPropList *prop, *style, *key, *val;
char *path;
int i, theme_too=0;
int make_pack = 0;
int i, theme_too=0, make_pack=0;
char *style_file = NULL;
@@ -702,37 +765,28 @@ main(int argc, char **argv)
style = WMCreatePLDictionary(NULL, NULL, NULL);
for (i=0; options[i]!=NULL; i++) {
key = WMCreatePLString(options[i]);
val = WMGetFromPLDictionary(prop, key);
if (val) {
int j;
char *str = WMGetFromPLString(key);
for (j = 0; font_styles[j]!=NULL; j++) {
if (strcasecmp(str, font_styles[j]) == 0) {
char *oldfont;
WMRetainPropList(val);
if (isFontOption(options[i])) {
char *newfont, *oldfont;
oldfont = WMGetFromPLString(val);
if (oldfont[0] == '-') {
if (!strchr(oldfont, ','))
{
char *newfont;
newfont = xlfdToFc(oldfont);
newfont = convertFont(oldfont);
/* font is a reference to old if conversion is not needed */
if (newfont != oldfont) {
WMReleasePropList(val);
val = WMCreatePLString(newfont);
break;
} else {
wwarning("fontsets are not supported. replaced with default %s", default_font);
val = WMCreatePLString(default_font);
break;
}
} else {
break;
}
wfree(newfont);
}
}
WMPutInPLDictionary(style, key, val);
WMReleasePropList(val);
}
WMReleasePropList(key);
}
val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
@@ -754,7 +808,7 @@ main(int argc, char **argv)
makeThemePack(style, style_file);
path = mymalloc(strlen(ThemePath)+32);
path = wmalloc(strlen(ThemePath)+32);
strcpy(path, ThemePath);
strcat(path, "/style");
WMWritePropListToFile(style, path, False);

View File

@@ -37,7 +37,7 @@
#define MAX_OPTIONS 128
char *default_font = "sans:pixelsize=12";
#define DEFAULT_FONT "sans:pixelsize=12"
char *FontOptions[] = {
"IconTitleFont",
@@ -80,92 +80,202 @@ Display *dpy;
WMPropList *readBlackBoxStyle(char *path);
char*
capitalize(char *element)
static Bool
isCursorOption(char *option)
{
unsigned int first = 1;
char *p;
char *b;
b = element;
for (p = b; *p != 0; p++) {
if (isalpha(*p) && first) {
first = 0;
*p = toupper(*p);
} else if (*p == '-' || *p == ' ') {
first = 1;
int i;
for (i=0; CursorOptions[i]!=NULL; i++) {
if (strcasecmp(option, CursorOptions[i])==0) {
return True;
}
}
return b;
return False;
}
char*
getElementFromXLFD(const char *xlfd, int index)
static Bool
isFontOption(char *option)
{
const char *p = xlfd;
while (*p != 0) {
if (*p == '-' && --index == 0) {
const char *end = strchr(p + 1, '-');
char *buf;
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;
int i;
for (i=0; FontOptions[i]!=NULL; i++) {
if (strcasecmp(option, FontOptions[i])==0) {
return True;
}
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*
xlfdToFc(char *xlfd)
{
char *Fcname = NULL;
str *tokens, *family, *weight, *slant;
char *name, buf[512];
int size, pixelsize;
char *family = getElementFromXLFD(xlfd, 2);
char *size = getElementFromXLFD(xlfd, 7);
char *weight = getElementFromXLFD(xlfd, 3);
char *slant = getElementFromXLFD(xlfd, 4);
tokens = getXLFDTokens(xlfd);
if (!tokens)
return wstrdup(DEFAULT_FONT);
if (strcmp(family, "*") != 0) {
Fcname = wstrconcat(Fcname, capitalize(family));
}
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);
family = &(tokens[1]);
weight = &(tokens[2]);
slant = &(tokens[3]);
wfree(family);
wfree(size);
wfree(weight);
wfree(slant);
if (family->len==0 || family->str[0]=='*')
return wstrdup(DEFAULT_FONT);
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*
defaultsPathForDomain(char *domain)
{
@@ -377,54 +487,30 @@ hackStyle(WMPropList *style)
tmp = WMGetFromPLArray(keys, i);
str = WMGetFromPLString(tmp);
if (str) {
int j, found;
if (ignoreFonts) {
for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
if (strcasecmp(str, FontOptions[j])==0) {
if (ignoreFonts && isFontOption(str)) {
WMRemoveFromPLDictionary(style, tmp);
found = 1;
break;
}
}
if (found)
continue;
}
if (ignoreCursors) {
for (j = 0, found = 0; CursorOptions[j] != NULL; j++) {
if (strcasecmp(str, CursorOptions[j]) == 0) {
if (ignoreCursors && isCursorOption(str)) {
WMRemoveFromPLDictionary(style, tmp);
found = 1;
break;
}
}
if (found)
continue;
}
for (j = 0; FontOptions[j]!=NULL; j++) {
if (strcasecmp(str, FontOptions[j]) == 0) {
char *oldfont;
if (isFontOption(str)) {
WMPropList *value;
value = WMGetFromPLDictionary(style, tmp);
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;
}
}
}
char *newfont, *oldfont;
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
|| strcasecmp(str, "IconTitleBack")==0) {
foundIconTitle = 1;