Added +[NSFont rbl_fontWithName:size:fallbackNames:]

This commit is contained in:
Justin Spahr-Summers 2012-12-09 00:21:48 -08:00
Родитель dd666fee0d
Коммит b71a5b10f3
5 изменённых файлов: 122 добавлений и 2 удалений

Просмотреть файл

@ -39,6 +39,9 @@
D0723FE315C616AF004DBDC7 /* RBLResizableImage.h in Headers */ = {isa = PBXBuildFile; fileRef = D0723FE115C616AF004DBDC7 /* RBLResizableImage.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0723FE415C616AF004DBDC7 /* RBLResizableImage.m in Sources */ = {isa = PBXBuildFile; fileRef = D0723FE215C616AF004DBDC7 /* RBLResizableImage.m */; };
D078B7DE15F6DB420063DDD5 /* NSView+RBLAnimationAdditionsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = D078B7DD15F6DB420063DDD5 /* NSView+RBLAnimationAdditionsSpec.m */; };
D088BEF916747D8F004B2665 /* NSFont+RBLFallbackAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = D088BEF716747D8F004B2665 /* NSFont+RBLFallbackAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
D088BEFA16747D8F004B2665 /* NSFont+RBLFallbackAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = D088BEF816747D8F004B2665 /* NSFont+RBLFallbackAdditions.m */; };
D088BEFC16747EE1004B2665 /* NSFont+RBLFallbackAdditionsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = D088BEFB16747EE1004B2665 /* NSFont+RBLFallbackAdditionsSpec.m */; };
D09AE4E315C5F45200ECAD10 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D09AE4E215C5F45200ECAD10 /* Cocoa.framework */; };
D09AE4ED15C5F45200ECAD10 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = D09AE4EB15C5F45200ECAD10 /* InfoPlist.strings */; };
D09AE4F915C5F45300ECAD10 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D09AE4F815C5F45300ECAD10 /* SenTestingKit.framework */; };
@ -162,6 +165,9 @@
D0723FE115C616AF004DBDC7 /* RBLResizableImage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RBLResizableImage.h; sourceTree = "<group>"; };
D0723FE215C616AF004DBDC7 /* RBLResizableImage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLResizableImage.m; sourceTree = "<group>"; };
D078B7DD15F6DB420063DDD5 /* NSView+RBLAnimationAdditionsSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSView+RBLAnimationAdditionsSpec.m"; sourceTree = "<group>"; };
D088BEF716747D8F004B2665 /* NSFont+RBLFallbackAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFont+RBLFallbackAdditions.h"; sourceTree = "<group>"; };
D088BEF816747D8F004B2665 /* NSFont+RBLFallbackAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFont+RBLFallbackAdditions.m"; sourceTree = "<group>"; };
D088BEFB16747EE1004B2665 /* NSFont+RBLFallbackAdditionsSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFont+RBLFallbackAdditionsSpec.m"; sourceTree = "<group>"; };
D08BB2DB15CB307200CC9868 /* Common.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Common.xcconfig; sourceTree = "<group>"; };
D08BB2DD15CB307200CC9868 /* Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
D08BB2DE15CB307200CC9868 /* Profile.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Profile.xcconfig; sourceTree = "<group>"; };
@ -278,6 +284,8 @@
children = (
D0723FC415C5FB68004DBDC7 /* NSColor+RBLCGColorAdditions.h */,
D0723FC515C5FB68004DBDC7 /* NSColor+RBLCGColorAdditions.m */,
D088BEF716747D8F004B2665 /* NSFont+RBLFallbackAdditions.h */,
D088BEF816747D8F004B2665 /* NSFont+RBLFallbackAdditions.m */,
D011FD5C16239A8600A27946 /* NSImage+RBLResizableImageAdditions.h */,
D011FD5D16239A8600A27946 /* NSImage+RBLResizableImageAdditions.m */,
D0410EA915C61895003A3203 /* NSTextView+RBLAntialiasingAdditions.h */,
@ -295,11 +303,12 @@
D0723FDA15C60815004DBDC7 /* Classes */ = {
isa = PBXGroup;
children = (
D0723FDB15C60846004DBDC7 /* NSColor+RBLCGColorAdditionsSpec.m */,
306A81EA1601EAE700BF45F3 /* CAAnimation+RBLBlockAdditionsSpec.m */,
D0723FDB15C60846004DBDC7 /* NSColor+RBLCGColorAdditionsSpec.m */,
D088BEFB16747EE1004B2665 /* NSFont+RBLFallbackAdditionsSpec.m */,
D078B7DD15F6DB420063DDD5 /* NSView+RBLAnimationAdditionsSpec.m */,
D0723FDF15C60E94004DBDC7 /* RBLViewSpec.m */,
AB10DB52166EADD000AB7AB1 /* RBLScrollViewSpec.m */,
D0723FDF15C60E94004DBDC7 /* RBLViewSpec.m */,
);
name = Classes;
sourceTree = "<group>";
@ -473,6 +482,7 @@
AB61F63616373FE000DD5D2C /* RBLTableCellView.h in Headers */,
AB61F6D41638370900DD5D2C /* NSApplication+RBLBlockAdditions.h in Headers */,
AB10DB4D166EA9CA00AB7AB1 /* RBLScrollView.h in Headers */,
D088BEF916747D8F004B2665 /* NSFont+RBLFallbackAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -667,6 +677,7 @@
AB61F63716373FE000DD5D2C /* RBLTableCellView.m in Sources */,
AB61F6D51638370900DD5D2C /* NSApplication+RBLBlockAdditions.m in Sources */,
AB10DB4E166EA9CA00AB7AB1 /* RBLScrollView.m in Sources */,
D088BEFA16747D8F004B2665 /* NSFont+RBLFallbackAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -679,6 +690,7 @@
D078B7DE15F6DB420063DDD5 /* NSView+RBLAnimationAdditionsSpec.m in Sources */,
306A81EB1601EAE700BF45F3 /* CAAnimation+RBLBlockAdditionsSpec.m in Sources */,
AB10DB53166EADD000AB7AB1 /* RBLScrollViewSpec.m in Sources */,
D088BEFC16747EE1004B2665 /* NSFont+RBLFallbackAdditionsSpec.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

Просмотреть файл

@ -0,0 +1,26 @@
//
// NSFont+RBLFallbackAdditions.h
// Rebel
//
// Created by Justin Spahr-Summers on 2012-12-09.
// Copyright (c) 2012 GitHub. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSFont (RBLFallbackAdditions)
// Looks up a font with the given name and size.
//
// If text rendered in the returned font uses a glyph not present in
// `fontName`, the fonts specified by `fallbackNames` are searched in order, and
// the glyph is rendered in the first font that provides it.
//
// If `fontName` does not exist on the system, `fallbackNames` is searched in
// order for a replacement font.
//
// Returns a font that renders in `fontName` by default, and `fallbackNames`
// only if necessary, or `nil` if no fonts by the given names could be found.
+ (NSFont *)rbl_fontWithName:(NSString *)fontName size:(CGFloat)fontSize fallbackNames:(NSArray *)fallbackNames;
@end

Просмотреть файл

@ -0,0 +1,39 @@
//
// NSFont+RBLFallbackAdditions.m
// Rebel
//
// Created by Justin Spahr-Summers on 2012-12-09.
// Copyright (c) 2012 GitHub. All rights reserved.
//
#import "NSFont+RBLFallbackAdditions.h"
@implementation NSFont (RBLFallbackAdditions)
+ (NSFont *)rbl_fontWithName:(NSString *)fontName size:(CGFloat)fontSize fallbackNames:(NSArray *)fallbackNames {
NSMutableArray *fallbackDescriptors = [NSMutableArray arrayWithCapacity:fallbackNames.count];
for (NSString *fallbackName in fallbackNames) {
[fallbackDescriptors addObject:[NSFontDescriptor fontDescriptorWithName:fallbackName size:fontSize]];
}
NSMutableArray *remainingFontNames = [fallbackNames mutableCopy];
NSAssert(fallbackDescriptors.count == remainingFontNames.count, @"Should have the same number of fallback font descriptors (%lu) as names to try (%lu)", (unsigned long)fallbackDescriptors.count, remainingFontNames.count);
while (YES) {
NSDictionary *attributes = @{ NSFontNameAttribute: fontName, NSFontCascadeListAttribute: fallbackDescriptors };
NSFont *font = [NSFont fontWithDescriptor:[NSFontDescriptor fontDescriptorWithFontAttributes:attributes] size:fontSize];
if (font != nil) return font;
if (remainingFontNames.count == 0) break;
// Try the next font in the list.
fontName = remainingFontNames[0];
[remainingFontNames removeObjectAtIndex:0];
[fallbackDescriptors removeObjectAtIndex:0];
}
return nil;
}
@end

Просмотреть файл

@ -8,6 +8,7 @@
#import <Rebel/NSApplication+RBLBlockAdditions.h>
#import <Rebel/NSColor+RBLCGColorAdditions.h>
#import <Rebel/NSFont+RBLFallbackAdditions.h>
#import <Rebel/NSImage+RBLResizableImageAdditions.h>
#import <Rebel/NSView+RBLAnimationAdditions.h>
#import <Rebel/RBLClipView.h>

Просмотреть файл

@ -0,0 +1,42 @@
//
// NSFont+RBLFallbackAdditionsSpec.m
// Rebel
//
// Created by Justin Spahr-Summers on 2012-12-09.
// Copyright (c) 2012 GitHub. All rights reserved.
//
SpecBegin(NSFontRBLFallbackAdditions)
describe(@"+rbl_fontWithName:size:fallbackNames:", ^{
__block NSFont *font;
afterEach(^{
// All of the below tests should return Helvetica size 12.
expect(font).notTo.beNil();
expect(font.familyName).to.equal(@"Helvetica");
expect(font.pointSize).to.equal(12);
});
it(@"should return a valid font without any fallback names", ^{
font = [NSFont rbl_fontWithName:@"Helvetica" size:12 fallbackNames:@[]];
});
it(@"should return a valid font with fallback names", ^{
font = [NSFont rbl_fontWithName:@"Helvetica" size:12 fallbackNames:@[ @"Lucida Grande" ]];
});
it(@"should return a fallback font if the desired font couldn't be found", ^{
font = [NSFont rbl_fontWithName:@"somemadeupfontname" size:12 fallbackNames:@[ @"Helvetica" ]];
});
it(@"should return the second fallback font if the first two desired fonts couldn't be found", ^{
font = [NSFont rbl_fontWithName:@"somemadeupfontname" size:12 fallbackNames:@[ @"anothermadeupfontname", @"Helvetica" ]];
});
it(@"should search the fallback font list in order", ^{
font = [NSFont rbl_fontWithName:@"somemadeupfontname" size:12 fallbackNames:@[ @"Helvetica", @"Lucida Grande" ]];
});
});
SpecEnd