File: mknfonts.m

package info (click to toggle)
mknfonts.tool 0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 128 kB
  • sloc: objc: 540; sh: 44; makefile: 41
file content (327 lines) | stat: -rw-r--r-- 7,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
copyright 2002 Alexander Malmberg <alexander@malmberg.org>
*/

#include <Foundation/NSString.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSSet.h>
#include <Foundation/NSFileManager.h>

#include <ft2build.h>
#include FT_FREETYPE_H


static FT_Library ftlib;


/*
from GNUstep's core/back/Source/art/ftfont.m
GNU LGPL:ed
*/
#define NSBoldFontMask 0
#define NSItalicFontMask 0
#define NSCondensedFontMask 0
static int traits_from_string(NSString *s, unsigned int *traits, unsigned int *weight)
{
static struct
{
  NSString *str;
  unsigned int trait;
  int weight;
} suffix[] = {
/* TODO */
{@"Ultralight"     ,0                         , 1},
{@"Thin"           ,0                         , 2},
{@"Light"          ,0                         , 3},
{@"Extralight"     ,0                         , 3},
{@"Book"           ,0                         , 4},
{@"Regular"        ,0                         , 5},
{@"Plain"          ,0                         , 5},
{@"Display"        ,0                         , 5},
{@"Roman"          ,0                         , 5},
{@"Semilight"      ,0                         , 5},
{@"Medium"         ,0                         , 6},
{@"Demi"           ,0                         , 7},
{@"Demibold"       ,0                         , 7},
{@"Semi"           ,0                         , 8},
{@"Semibold"       ,0                         , 8},
{@"Bold"           ,NSBoldFontMask            , 9},
{@"Extra"          ,NSBoldFontMask            ,10},
{@"Extrabold"      ,NSBoldFontMask            ,10},
{@"Heavy"          ,NSBoldFontMask            ,11},
{@"Heavyface"      ,NSBoldFontMask            ,11},
{@"Ultrabold"      ,NSBoldFontMask            ,12},
{@"Black"          ,NSBoldFontMask            ,12},
{@"Ultra"          ,NSBoldFontMask            ,13},
{@"Ultrablack"     ,NSBoldFontMask            ,13},
{@"Fat"            ,NSBoldFontMask            ,13},
{@"Extrablack"     ,NSBoldFontMask            ,14},
{@"Obese"          ,NSBoldFontMask            ,14},
{@"Nord"           ,NSBoldFontMask            ,14},

{@"Italic"         ,NSItalicFontMask          ,-1},
{@"Oblique"        ,NSItalicFontMask          ,-1},

{@"Cond"           ,NSCondensedFontMask       ,-1},
{@"Condensed"      ,NSCondensedFontMask       ,-1},
{nil,0,-1}
};
  int i;

  *traits = 0;
//  printf("do '%@'\n", s);
  while ([s length] > 0)
    {
//      printf("  got '%@'\n", s);
      if ([s hasSuffix: @"-"] || [s hasSuffix: @" "])
	{
//	  printf("  do -\n");
	  s = [s substringToIndex: [s length] - 1];
	  continue;
	}
      for (i = 0; suffix[i].str; i++)
	{
	  if (![s hasSuffix: suffix[i].str])
	    continue;
//	  printf("  found '%@'\n", suffix[i].str);
	  if (suffix[i].weight != -1)
	    *weight = suffix[i].weight;
	  (*traits) |= suffix[i].trait;
	  s = [s substringToIndex: [s length] - [suffix[i].str length]];
	  break;
	}
      if (!suffix[i].str)
	break;
    }
//  printf("end up with '%@'\n", s);
  return [s length];
}



@interface FaceInfo : NSObject
{
@public
	NSString *postScriptName;
	NSString *familyName;
	NSString *faceName;

	NSMutableArray *files;
}
-(BOOL) collectFaceInfo;
-(NSDictionary *) faceInfoDictionary;
@end

@implementation FaceInfo
- init
{
	self=[super init];
	files=[[NSMutableArray alloc] init];
	return self;
}

-(void) dealloc
{
	DESTROY(files);
	[super dealloc];
}

-(NSString *) description
{
	return [NSString stringWithFormat: @"<FaceInfo: psname='%@' family='%@' face='%@' files=%@>",
		postScriptName,familyName,faceName,files];
}


-(BOOL) collectFaceInfo
{
	FT_Face face;
	int i;
	const char *psname;
	unsigned int foo,bar;

	for (i=1;i<[files count];i++)
	{
		NSString *f;
		f=[files objectAtIndex: i];
		if ([[f pathExtension] isEqualToString: @"pfa"] ||
			 [[f pathExtension] isEqualToString: @"pfb"])
		{
			[f retain];
			[files removeObject: f];
			[files insertObject: f atIndex: 0];
			[f release];
		}
	}

	if (FT_New_Face(ftlib,[[files objectAtIndex: 0] cString],0,&face))
	{
		GSPrintf(stderr,@"unable to open %@, ignoring\n",[files objectAtIndex: 0]);
		return NO;
	}
	for (i=1;i<[files count];i++)
	{
		if (FT_Attach_File(face,[[files objectAtIndex: i] cString]))
		{
			GSPrintf(stderr,@"warning: unable to attach %@\n",[files objectAtIndex: i]);
		}
	}

	psname=FT_Get_Postscript_Name(face);
	if (!psname)
	{
		GSPrintf(stderr,@"couldn't get postscript name for %@, ignoring\n",[files objectAtIndex: 0]);
		return NO;
	}
	postScriptName=[[NSString alloc] initWithCString: psname];

	familyName=[[NSString alloc] initWithCString: face->family_name];
	faceName=[[NSString alloc] initWithCString: face->style_name];

	if (traits_from_string(faceName,&foo,&bar))
	{
		GSPrintf(stderr,@"warning: couldn't fully parse '%@' (%@)\n",faceName,postScriptName);
	}

	FT_Done_Face(face);

	return YES;
}

-(NSDictionary *) faceInfoDictionary
{
	return [NSDictionary dictionaryWithObjectsAndKeys:
		files,@"Files",
		postScriptName,@"PostScriptName",
		faceName,@"Name",
		nil];
}

@end


static NSArray *group_files(NSArray *files)
{
	NSSet *combine_set=[NSSet setWithObjects: @"pfa",@"pfb",@"afm",@"pfm",nil];
	NSMutableDictionary *groups;
	NSArray *a;
	int i;

	groups=[[NSMutableDictionary alloc] init];
	for (i=0;i<[files count];i++)
	{
		NSString *f=[files objectAtIndex: i];
		NSString *key;
		FaceInfo *fa;

		if ([combine_set containsObject: [f pathExtension]])
			key=[f stringByDeletingPathExtension];
		else
			key=f;

		fa=[groups objectForKey: key];
		if (!fa)
		{
			fa=[[[FaceInfo alloc] init] autorelease];
			[groups setObject: fa  forKey: key];
		}
		[fa->files addObject: f];
	}

	a=[groups allValues];
	DESTROY(groups);
	return a;
}


int main(int argc, char **argv)
{
	CREATE_AUTORELEASE_POOL(arp);

	NSMutableArray *files;
	NSArray *groups;
	int i;

	NSMutableDictionary *families;


	files=[[NSMutableArray alloc] init];
	for (i=1;i<argc;i++)
	{
		[files addObject: [NSString stringWithCString: argv[i]]];
	}

	groups=[group_files(files) retain];
	DESTROY(files);

	if (FT_Init_FreeType(&ftlib))
	{
		fprintf(stderr,"failed to initialize freetype\n");
		return 1;
	}

	families=[[NSMutableDictionary alloc] init];
	for (i=0;i<[groups count];i++)
	{
		NSMutableArray *ma;
		FaceInfo *fi=[groups objectAtIndex: i];
		if ([fi collectFaceInfo])
		{
			ma=[families objectForKey: fi->familyName];
			if (!ma)
			{
				ma=[[[NSMutableArray alloc] init] autorelease];
				[families setObject: ma forKey: fi->familyName];
			}
			[ma addObject: fi];
		}
	}
	DESTROY(groups);

	{
		NSFileManager *fm=[NSFileManager defaultManager];
		NSEnumerator *e;
		NSString *family;
		int i,c;

		NSMutableDictionary *family_info;
		NSMutableArray *faces;
		NSArray *faceinfos;
		FaceInfo *fi;
		NSString *path;


		e=[families keyEnumerator];
		while ((family=[e nextObject]))
		{
			family_info=[[NSMutableDictionary alloc] init];
			faces=[[NSMutableArray alloc] init];
			[family_info setObject: faces  forKey: @"Faces"];

			path=[family stringByAppendingPathExtension: @"nfont"];

			[fm createDirectoryAtPath: path
				attributes: nil];

			faceinfos=[families objectForKey: family];
			for (c=[faceinfos count],i=0;i<c;i++)
			{
				fi=[faceinfos objectAtIndex: i];
				[faces addObject: [fi faceInfoDictionary]];
			}

			[family_info
				writeToFile: [path stringByAppendingPathComponent: @"FontInfo.plist"]
				atomically: NO];
			DESTROY(faces);
			DESTROY(family_info);
		}
	}
	DESTROY(families);

	DESTROY(arp);
	return 0;
}