PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/groff/src/libs/libxutil/XFontName.c

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