~profzoom/ubuntu/quantal/wmaker/bug-1079925

« back to all changes in this revision

Viewing changes to util/fontconv.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2004-11-10 14:05:30 UTC
  • Revision ID: james.westby@ubuntu.com-20041110140530-qpd66b5lm38x7apk
Tags: upstream-0.91.0
ImportĀ upstreamĀ versionĀ 0.91.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <string.h>
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
#include <WINGs/WUtil.h>
 
6
 
 
7
#include "../src/wconfig.h"
 
8
 
 
9
#define DEFAULT_FONT "sans serif:pixelsize=12"
 
10
 
 
11
static int
 
12
countChar(char *str, char c)
 
13
{
 
14
    int count = 0;
 
15
 
 
16
    if (!str)
 
17
        return 0;
 
18
 
 
19
    for (; *str!=0; str++) {
 
20
        if (*str == c) {
 
21
            count++;
 
22
        }
 
23
    }
 
24
 
 
25
    return count;
 
26
}
 
27
 
 
28
 
 
29
typedef struct str {
 
30
    char *str;
 
31
    int len;
 
32
} str;
 
33
 
 
34
#define XLFD_TOKENS 14
 
35
 
 
36
 
 
37
static str*
 
38
getXLFDTokens(char *xlfd)
 
39
{
 
40
    static str tokens[XLFD_TOKENS];
 
41
    int i, len, size;
 
42
    char *ptr;
 
43
 
 
44
    if (!xlfd || *xlfd!='-' || countChar(xlfd, '-')<XLFD_TOKENS)
 
45
        return NULL;
 
46
 
 
47
    memset(tokens, 0, sizeof(str)*XLFD_TOKENS);
 
48
 
 
49
    len  = strlen(xlfd);
 
50
 
 
51
    for (ptr=xlfd, i=0; i<XLFD_TOKENS && len>0; i++) {
 
52
        /* skip one '-' */
 
53
        ptr++;
 
54
        len--;
 
55
        if (len <= 0)
 
56
            break;
 
57
        size = strcspn(ptr, "-,");
 
58
        tokens[i].str = ptr;
 
59
        tokens[i].len = size;
 
60
        ptr += size;
 
61
        len -= size;
 
62
    }
 
63
 
 
64
    return tokens;
 
65
}
 
66
 
 
67
 
 
68
static int
 
69
strToInt(str *token)
 
70
{
 
71
    int res=0, pos, c;
 
72
 
 
73
    if (token->len==0 || token->str[0]=='*') {
 
74
        return -1;
 
75
    } else {
 
76
        for (res=0, pos=0; pos<token->len; pos++) {
 
77
            c = token->str[pos] - '0';
 
78
            if (c<0 || c>9)
 
79
                break;
 
80
            res = res*10 + c;
 
81
        }
 
82
    }
 
83
    return res;
 
84
}
 
85
 
 
86
 
 
87
static char*
 
88
mapWeightToName(str *weight)
 
89
{
 
90
    char *normalNames[] = {"medium", "normal", "regular"};
 
91
    static char buf[32];
 
92
    int i;
 
93
 
 
94
    if (weight->len==0)
 
95
        return "";
 
96
 
 
97
    for (i=0; i<sizeof(normalNames)/sizeof(char*); i++) {
 
98
        if (strlen(normalNames[i])==weight->len &&
 
99
            strncmp(normalNames[i], weight->str, weight->len))
 
100
        {
 
101
            return "";
 
102
        }
 
103
    }
 
104
 
 
105
    snprintf(buf, sizeof(buf), ":%.*s", weight->len, weight->str);
 
106
 
 
107
    return buf;
 
108
}
 
109
 
 
110
 
 
111
static char*
 
112
mapSlantToName(str *slant)
 
113
{
 
114
    if (slant->len==0)
 
115
        return "";
 
116
 
 
117
    switch(slant->str[0]) {
 
118
    case 'i':
 
119
        return ":italic";
 
120
    case 'o':
 
121
        return ":oblique";
 
122
    case 'r':
 
123
    default:
 
124
        return "";
 
125
    }
 
126
}
 
127
 
 
128
 
 
129
char*
 
130
xlfdToFc(char *xlfd, char *useFamily, Bool keepXLFD)
 
131
{
 
132
    str *tokens, *family, *weight, *slant;
 
133
    char *name, buf[64], *slt;
 
134
    int size, pixelsize;
 
135
 
 
136
    tokens = getXLFDTokens(xlfd);
 
137
    if (!tokens)
 
138
        return wstrdup(DEFAULT_FONT);
 
139
 
 
140
    family = &(tokens[1]);
 
141
    weight = &(tokens[2]);
 
142
    slant  = &(tokens[3]);
 
143
    pixelsize = strToInt(&tokens[6]);
 
144
    size = strToInt(&tokens[7]);
 
145
 
 
146
    if (useFamily) {
 
147
        name = wstrdup(useFamily);
 
148
    } else {
 
149
        if (family->len==0 || family->str[0]=='*')
 
150
            return wstrdup(DEFAULT_FONT);
 
151
 
 
152
        snprintf(buf, sizeof(buf), "%.*s", family->len, family->str);
 
153
        name = wstrdup(buf);
 
154
    }
 
155
 
 
156
    if (size>0 && pixelsize<=0) {
 
157
        snprintf(buf, sizeof(buf), "-%d", size/10);
 
158
        name = wstrappend(name, buf);
 
159
    }
 
160
 
 
161
    name = wstrappend(name, mapWeightToName(weight));
 
162
    name = wstrappend(name, mapSlantToName(slant));
 
163
 
 
164
    if (size<=0 && pixelsize<=0) {
 
165
        name = wstrappend(name, ":pixelsize=12");
 
166
    } else if (pixelsize>0) {
 
167
        /* if pixelsize is present size will be ignored so we skip it */
 
168
        snprintf(buf, sizeof(buf), ":pixelsize=%d", pixelsize);
 
169
        name = wstrappend(name, buf);
 
170
    }
 
171
 
 
172
    if (keepXLFD) {
 
173
        name = wstrappend(name, ":xlfd=");
 
174
        name = wstrappend(name, xlfd);
 
175
    }
 
176
 
 
177
    return name;
 
178
}
 
179
 
 
180
 
 
181
/* return converted font (if conversion is needed) else the original font */
 
182
char*
 
183
convertFont(char *font, Bool keepXLFD)
 
184
{
 
185
    if (font[0]=='-') {
 
186
        if (MB_CUR_MAX < 2) {
 
187
            return xlfdToFc(font, NULL, keepXLFD);
 
188
        } else {
 
189
            return xlfdToFc(font, "sans serif", keepXLFD);
 
190
        }
 
191
    } else {
 
192
        return font;
 
193
    }
 
194
}
 
195