~ubuntu-branches/ubuntu/dapper/groff/dapper

« back to all changes in this revision

Viewing changes to src/xditview/XFontName.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2002-03-17 04:11:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020317041150-wkgfawjc3gxlk0o5
Tags: upstream-1.17.2
ImportĀ upstreamĀ versionĀ 1.17.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * XFontName.c
 
3
 *
 
4
 * build/parse X Font name strings
 
5
 */
 
6
 
 
7
#include <X11/Xlib.h>
 
8
#include <X11/Xos.h>
 
9
#include "XFontName.h"
 
10
#include <ctype.h>
 
11
 
 
12
static char *
 
13
extractStringField (name, buffer, size, attrp, bit)
 
14
        char            *name;
 
15
        char            *buffer;
 
16
        int             size;
 
17
        unsigned int    *attrp;
 
18
        unsigned int    bit;
 
19
{
 
20
        char    *buf = buffer;
 
21
 
 
22
        if (!*name)
 
23
                return 0;
 
24
        while (*name && *name != '-' && size > 0) {
 
25
                *buf++ = *name++;
 
26
                --size;
 
27
        }
 
28
        if (size <= 0)
 
29
                return 0;
 
30
        *buf = '\0';
 
31
        if (buffer[0] != '*' || buffer[1] != '\0')
 
32
                *attrp |= bit;
 
33
        if (*name == '-')
 
34
                return name+1;
 
35
        return name;
 
36
}
 
37
 
 
38
static char *
 
39
extractUnsignedField (name, result, attrp, bit)
 
40
        char            *name;
 
41
        unsigned int    *result;
 
42
        unsigned int    *attrp;
 
43
        unsigned int    bit;
 
44
{
 
45
        char            buf[256];
 
46
        char            *c;
 
47
        unsigned int    i;
 
48
 
 
49
        name = extractStringField (name, buf, sizeof (buf), attrp, bit);
 
50
        if (!name)
 
51
                return 0;
 
52
        if (!(*attrp & bit))
 
53
                return name;
 
54
        i = 0;
 
55
        for (c = buf; *c; c++) {
 
56
                if (!isdigit (*c))
 
57
                        return 0;
 
58
                i = i * 10 + (*c - '0');
 
59
        }
 
60
        *result = i;
 
61
        return name;
 
62
}
 
63
 
 
64
Bool
 
65
XParseFontName (fontNameString, fontName, fontNameAttributes)
 
66
        XFontNameString fontNameString;
 
67
        XFontName       *fontName;
 
68
        unsigned int    *fontNameAttributes;
 
69
{
 
70
        char            *name = fontNameString;
 
71
        XFontName       temp;
 
72
        unsigned int    attributes = 0;
 
73
 
 
74
#define GetString(field,bit)\
 
75
        if (!(name = extractStringField \
 
76
                (name, temp.field, sizeof (temp.field),\
 
77
                &attributes, bit))) \
 
78
                return False;
 
79
 
 
80
#define GetUnsigned(field,bit)\
 
81
        if (!(name = extractUnsignedField \
 
82
                (name, &temp.field, \
 
83
                &attributes, bit))) \
 
84
                return False;
 
85
 
 
86
        GetString (Registry, FontNameRegistry)
 
87
        GetString (Foundry, FontNameFoundry)
 
88
        GetString (FamilyName, FontNameFamilyName)
 
89
        GetString (WeightName, FontNameWeightName)
 
90
        GetString (Slant, FontNameSlant)
 
91
        GetString (SetwidthName, FontNameSetwidthName)
 
92
        GetString (AddStyleName, FontNameAddStyleName)
 
93
        GetUnsigned (PixelSize, FontNamePixelSize)
 
94
        GetUnsigned (PointSize, FontNamePointSize)
 
95
        GetUnsigned (ResolutionX, FontNameResolutionX)
 
96
        GetUnsigned (ResolutionY, FontNameResolutionY)
 
97
        GetString (Spacing, FontNameSpacing)
 
98
        GetUnsigned (AverageWidth, FontNameAverageWidth)
 
99
        GetString (CharSetRegistry, FontNameCharSetRegistry)
 
100
        if (!*name) {
 
101
                temp.CharSetEncoding[0] = '\0';
 
102
                attributes |= FontNameCharSetEncoding;
 
103
        } else {
 
104
                GetString (CharSetEncoding, FontNameCharSetEncoding)
 
105
        }
 
106
        *fontName = temp;
 
107
        *fontNameAttributes = attributes;
 
108
        return True;
 
109
}
 
110
 
 
111
static char *
 
112
utoa (u, s, size)
 
113
        unsigned int    u;
 
114
        char            *s;
 
115
        int             size;
 
116
{
 
117
        char    *t;
 
118
 
 
119
        t = s + size;
 
120
        *--t = '\0';
 
121
        do
 
122
                *--t = (u % 10) + '0';
 
123
        while (u /= 10);
 
124
        return t;
 
125
}
 
126
 
 
127
Bool
 
128
XFormatFontName (fontName, fontNameAttributes, fontNameString)
 
129
        XFontName       *fontName;
 
130
        unsigned int    fontNameAttributes;
 
131
        XFontNameString fontNameString;
 
132
{
 
133
        XFontNameString tmp;
 
134
        char            *name = tmp, *f;
 
135
        int             left = sizeof (tmp) - 1;
 
136
        char            number[32];
 
137
 
 
138
#define PutString(field, bit)\
 
139
        f = (fontNameAttributes & bit) ? \
 
140
                fontName->field \
 
141
                : "*"; \
 
142
        if ((left -= strlen (f)) < 0) \
 
143
                return False; \
 
144
        while (*f) \
 
145
                if ((*name++ = *f++) == '-') \
 
146
                        return False; 
 
147
#define PutHyphen()\
 
148
        if (--left < 0) \
 
149
                return False; \
 
150
        *name++ = '-';
 
151
 
 
152
#define PutUnsigned(field, bit) \
 
153
        f = (fontNameAttributes & bit) ? \
 
154
                utoa (fontName->field, number, sizeof (number)) \
 
155
                : "*"; \
 
156
        if ((left -= strlen (f)) < 0) \
 
157
                return False; \
 
158
        while (*f) \
 
159
                *name++ = *f++;
 
160
 
 
161
        PutString (Registry, FontNameRegistry)
 
162
        PutHyphen ();
 
163
        PutString (Foundry, FontNameFoundry)
 
164
        PutHyphen ();
 
165
        PutString (FamilyName, FontNameFamilyName)
 
166
        PutHyphen ();
 
167
        PutString (WeightName, FontNameWeightName)
 
168
        PutHyphen ();
 
169
        PutString (Slant, FontNameSlant)
 
170
        PutHyphen ();
 
171
        PutString (SetwidthName, FontNameSetwidthName)
 
172
        PutHyphen ();
 
173
        PutString (AddStyleName, FontNameAddStyleName)
 
174
        PutHyphen ();
 
175
        PutUnsigned (PixelSize, FontNamePixelSize)
 
176
        PutHyphen ();
 
177
        PutUnsigned (PointSize, FontNamePointSize)
 
178
        PutHyphen ();
 
179
        PutUnsigned (ResolutionX, FontNameResolutionX)
 
180
        PutHyphen ();
 
181
        PutUnsigned (ResolutionY, FontNameResolutionY)
 
182
        PutHyphen ();
 
183
        PutString (Spacing, FontNameSpacing)
 
184
        PutHyphen ();
 
185
        PutUnsigned (AverageWidth, FontNameAverageWidth)
 
186
        PutHyphen ();
 
187
        PutString (CharSetRegistry, FontNameCharSetRegistry)
 
188
        PutHyphen ();
 
189
        PutString (CharSetEncoding, FontNameCharSetEncoding)
 
190
        *name = '\0';
 
191
        strcpy (fontNameString, tmp);
 
192
        return True;
 
193
}
 
194
 
 
195
Bool
 
196
XCompareFontName (name1, name2, fontNameAttributes)
 
197
        XFontName       *name1, *name2;
 
198
        unsigned int    fontNameAttributes;
 
199
{
 
200
#define CompareString(field,bit) \
 
201
        if (fontNameAttributes & bit) \
 
202
                if (strcmp (name1->field, name2->field)) \
 
203
                        return False;
 
204
 
 
205
#define CompareUnsigned(field,bit) \
 
206
        if (fontNameAttributes & bit) \
 
207
                if (name1->field != name2->field) \
 
208
                        return False;
 
209
 
 
210
        CompareString (Registry, FontNameRegistry)
 
211
        CompareString (Foundry, FontNameFoundry)
 
212
        CompareString (FamilyName, FontNameFamilyName)
 
213
        CompareString (WeightName, FontNameWeightName)
 
214
        CompareString (Slant, FontNameSlant)
 
215
        CompareString (SetwidthName, FontNameSetwidthName)
 
216
        CompareString (AddStyleName, FontNameAddStyleName)
 
217
        CompareUnsigned (PixelSize, FontNamePixelSize)
 
218
        CompareUnsigned (PointSize, FontNamePointSize)
 
219
        CompareUnsigned (ResolutionX, FontNameResolutionX)
 
220
        CompareUnsigned (ResolutionY, FontNameResolutionY)
 
221
        CompareString (Spacing, FontNameSpacing)
 
222
        CompareUnsigned (AverageWidth, FontNameAverageWidth)
 
223
        CompareString (CharSetRegistry, FontNameCharSetRegistry)
 
224
        CompareString (CharSetEncoding, FontNameCharSetEncoding)
 
225
        return True;
 
226
}
 
227
 
 
228
XCopyFontName (name1, name2, fontNameAttributes)
 
229
        XFontName       *name1, *name2;
 
230
        unsigned int    fontNameAttributes;
 
231
{
 
232
#define CopyString(field,bit) \
 
233
        if (fontNameAttributes & bit) \
 
234
                strcpy (name2->field, name1->field);
 
235
 
 
236
#define CopyUnsigned(field,bit) \
 
237
        if (fontNameAttributes & bit) \
 
238
                name2->field = name1->field;
 
239
 
 
240
        CopyString (Registry, FontNameRegistry)
 
241
        CopyString (Foundry, FontNameFoundry)
 
242
        CopyString (FamilyName, FontNameFamilyName)
 
243
        CopyString (WeightName, FontNameWeightName)
 
244
        CopyString (Slant, FontNameSlant)
 
245
        CopyString (SetwidthName, FontNameSetwidthName)
 
246
        CopyString (AddStyleName, FontNameAddStyleName)
 
247
        CopyUnsigned (PixelSize, FontNamePixelSize)
 
248
        CopyUnsigned (PointSize, FontNamePointSize)
 
249
        CopyUnsigned (ResolutionX, FontNameResolutionX)
 
250
        CopyUnsigned (ResolutionY, FontNameResolutionY)
 
251
        CopyString (Spacing, FontNameSpacing)
 
252
        CopyUnsigned (AverageWidth, FontNameAverageWidth)
 
253
        CopyString (CharSetRegistry, FontNameCharSetRegistry)
 
254
        CopyString (CharSetEncoding, FontNameCharSetEncoding)
 
255
        return True;
 
256
}