Support fontconfig 2.11's new 'postscriptname' attribute.

GNUstep has been bending over backwards to deal with fontconfig's baffling
lack of support for PostScript-style font names, just as it bent over
backwards to deal with XLFD's lack of same. Luckily, the fontconfig guys
have done us (and let's be honest, ghostscript and every pdf reader in
creation) a solid, and now that it's here it would be downright criminal
to not use it.

Implement it. :)
This commit is contained in:
Jeff Teunissen 2013-11-26 21:24:22 -05:00
parent 016f5eda25
commit 233e44dc4c

View file

@ -80,12 +80,15 @@ static NSArray *faFromFc(FcPattern *pat)
{
int weight, slant, spacing, width, nsweight;
unsigned int nstraits = 0;
char *family, *fcstyle;
NSMutableString *name, *style;
char *fcfamily, *fcstyle;
NSMutableString *name, *family, *style;
#ifdef FC_POSTSCRIPT_NAME
char *fcname;
#endif
if (FcPatternGetInteger(pat, FC_WEIGHT, 0, &weight) != FcResultMatch
|| FcPatternGetInteger(pat, FC_SLANT, 0, &slant) != FcResultMatch
|| FcPatternGetString(pat, FC_FAMILY, 0, (FcChar8 **)&family)
|| FcPatternGetString(pat, FC_FAMILY, 0, (FcChar8 **)&fcfamily)
!= FcResultMatch)
return nil;
@ -94,8 +97,13 @@ static NSArray *faFromFc(FcPattern *pat)
nstraits |= NSFixedPitchFontMask;
name = [NSMutableString stringWithCapacity: 100];
#ifdef FC_POSTSCRIPT_NAME
if (FcPatternGetString(pat, FC_POSTSCRIPT_NAME, 0, (FcChar8 **)&fcname) == FcResultMatch)
[name appendString: [NSMutableString stringWithUTF8String: fcname]];
#endif
family = [NSMutableString stringWithUTF8String: fcfamily];
style = [NSMutableString stringWithCapacity: 100];
[name appendString: [NSString stringWithUTF8String: family]];
switch (weight)
{
@ -205,12 +213,18 @@ static NSArray *faFromFc(FcPattern *pat)
break;
}
if (![name length]) // no psname
{
NSDebugLLog(@"NSFont", @"Warning: synthesizing PSName for '%@ %@'", family, style);
[name appendString: family];
if ([style length] > 0)
{
[name appendString: @"-"];
[name appendString: style];
}
else
}
if (![style length])
{
[style setString: @"Regular"];
}
@ -236,6 +250,9 @@ static NSArray *faFromFc(FcPattern *pat)
FcPattern *pat = FcPatternCreate();
FcObjectSet *os = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_FULLNAME,
#ifdef FC_POSTSCRIPT_NAME
FC_POSTSCRIPT_NAME,
#endif
FC_SLANT, FC_WEIGHT, FC_WIDTH,
FC_SPACING, NULL);
FcFontSet *fs = FcFontList(NULL, pat, os);
@ -261,14 +278,17 @@ static NSArray *faFromFc(FcPattern *pat)
familyString = [NSString stringWithUTF8String: family];
familyArray = [fcxft_allFontFamilies objectForKey: familyString];
if (familyArray == nil)
{
NSDebugLLog(@"NSFont", @"Found font family %@", familyString);
familyArray = [[NSMutableArray alloc] init];
familyArray = [NSMutableArray array];
[fcxft_allFontFamilies setObject: familyArray
forKey: familyString];
RELEASE(familyArray);
}
if (![fcxft_allFontNames containsObject: name])
{
NSDebugLLog(@"NSFont", @"fc enumerator: adding font: %@", name);
[familyArray addObject: fontArray];
[fcxft_allFontNames addObject: name];
@ -281,6 +301,7 @@ static NSArray *faFromFc(FcPattern *pat)
}
}
}
}
FcFontSetDestroy (fs);
allFontNames = fcxft_allFontNames;
@ -290,10 +311,6 @@ static NSArray *faFromFc(FcPattern *pat)
- (NSString *) defaultSystemFontName
{
if ([allFontNames containsObject: @"DejaVu Sans-Book"])
{
return @"DejaVu Sans-Book";
}
if ([allFontNames containsObject: @"DejaVuSans"])
{
return @"DejaVuSans";
@ -302,10 +319,6 @@ static NSArray *faFromFc(FcPattern *pat)
{
return @"BitstreamVeraSans-Roman";
}
if ([allFontNames containsObject: @"Bitstream Vera Sans"])
{
return @"Bitstream Vera Sans";
}
if ([allFontNames containsObject: @"FreeSans"])
{
return @"FreeSans";
@ -314,9 +327,9 @@ static NSArray *faFromFc(FcPattern *pat)
{
return @"Tahoma";
}
if ([allFontNames containsObject: @"Arial"])
if ([allFontNames containsObject: @"ArialMT"])
{
return @"Arial";
return @"ArialMT";
}
return @"Helvetica";
}
@ -339,9 +352,9 @@ static NSArray *faFromFc(FcPattern *pat)
{
return @"Tahoma-Bold";
}
if ([allFontNames containsObject: @"Arial-Bold"])
if ([allFontNames containsObject: @"Arial-BoldMT"])
{
return @"Arial-Bold";
return @"Arial-BoldMT";
}
return @"Helvetica-Bold";
}
@ -352,7 +365,7 @@ static NSArray *faFromFc(FcPattern *pat)
{
return @"DejaVuSansMono";
}
if ([allFontNames containsObject: @"Bitstream Vera Sans Mono"])
if ([allFontNames containsObject: @"BitstreamVeraSansMono-Roman"])
{
return @"Bitstream Vera Sans Mono";
}
@ -360,9 +373,9 @@ static NSArray *faFromFc(FcPattern *pat)
{
return @"FreeMono";
}
if ([allFontNames containsObject: @"Courier New"])
if ([allFontNames containsObject: @"CourierNewPSMT"])
{
return @"Courier New";
return @"CourierNewPSMT";
}
return @"Courier";
}
@ -432,13 +445,15 @@ static NSArray *faFromFc(FcPattern *pat)
- (void)addName: (NSString*)name
{
#ifdef FC_POSTSCRIPT_NAME
FcPatternAddString(_pat, FC_POSTSCRIPT_NAME, (const FcChar8 *)[name UTF8String]);
#else
// FIXME: Fontconfig ignores PostScript names of fonts; we need
// https://bugs.freedesktop.org/show_bug.cgi?id=18095 fixed.
// This is a heuristic to try to 'parse' a PostScript font name,
// however, since they are just unique identifiers for fonts and
// don't need to follow any naming convention, this may fail
NSRange dash = [name rangeOfString: @"-"];
if (dash.location == NSNotFound)
{
@ -490,6 +505,7 @@ static NSArray *faFromFc(FcPattern *pat)
FcPatternAddInteger(_pat, FC_WIDTH, FC_WIDTH_EXPANDED);
}
}
#endif
}
- (void)addVisibleName: (NSString*)name