forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTextNodeWordKernerTests.mm
149 lines (124 loc) · 7.61 KB
/
ASTextNodeWordKernerTests.mm
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
//
// ASTextNodeWordKernerTests.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/ASTextKitComponents.h>
#import <AsyncDisplayKit/ASTextNodeTypes.h>
#import <AsyncDisplayKit/ASTextNodeWordKerner.h>
#pragma mark - Tests
@interface ASTextNodeWordKernerTests : XCTestCase
@property (nonatomic) ASTextNodeWordKerner *layoutManagerDelegate;
@property (nonatomic) ASTextKitComponents *components;
@property (nonatomic, copy) NSAttributedString *attributedString;
@end
@implementation ASTextNodeWordKernerTests
- (void)setUp
{
[super setUp];
_layoutManagerDelegate = [[ASTextNodeWordKerner alloc] init];
_components.layoutManager.delegate = _layoutManagerDelegate;
}
- (void)setupTextKitComponentsWithoutWordKerning
{
CGSize size = CGSizeMake(200, 200);
NSDictionary *attributes = nil;
NSString *seedString = @"Hello world";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:seedString attributes:attributes];
_components = [ASTextKitComponents componentsWithAttributedSeedString:attributedString textContainerSize:size];
}
- (void)setupTextKitComponentsWithWordKerning
{
CGSize size = CGSizeMake(200, 200);
NSDictionary *attributes = @{ASTextNodeWordKerningAttributeName: @".5"};
NSString *seedString = @"Hello world";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:seedString attributes:attributes];
_components = [ASTextKitComponents componentsWithAttributedSeedString:attributedString textContainerSize:size];
}
- (void)setupTextKitComponentsWithWordKerningDifferentFontSizes
{
CGSize size = CGSizeMake(200, 200);
NSDictionary *attributes = @{ASTextNodeWordKerningAttributeName: @".5"};
NSString *seedString = @" ";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:seedString attributes:attributes];
UIFont *bigFont = [UIFont systemFontOfSize:36];
UIFont *normalFont = [UIFont systemFontOfSize:12];
[attributedString addAttribute:NSFontAttributeName value:bigFont range:NSMakeRange(0, 1)];
[attributedString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(1, 1)];
_components = [ASTextKitComponents componentsWithAttributedSeedString:attributedString textContainerSize:size];
}
- (void)testSomeGlyphsToChangeIfWordKerning
{
[self setupTextKitComponentsWithWordKerning];
NSInteger glyphsToChange = [self _layoutManagerShouldGenerateGlyphs];
XCTAssertTrue(glyphsToChange > 0, @"Should have changed the properties on some glyphs");
}
- (void)testSpaceBoundingBoxForNoWordKerning
{
CGSize size = CGSizeMake(200, 200);
UIFont *font = [UIFont systemFontOfSize:12.0];
NSDictionary *attributes = @{NSFontAttributeName : font};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@" " attributes:attributes];
_components = [ASTextKitComponents componentsWithAttributedSeedString:attributedString textContainerSize:size];
CGFloat expectedWidth = [@" " sizeWithAttributes:@{ NSFontAttributeName : font }].width;
CGRect boundingBox = [_layoutManagerDelegate layoutManager:_components.layoutManager boundingBoxForControlGlyphAtIndex:0 forTextContainer:_components.textContainer proposedLineFragment:CGRectZero glyphPosition:CGPointZero characterIndex:0];
XCTAssertEqualWithAccuracy(boundingBox.size.width, expectedWidth, FLT_EPSILON, @"Word kerning shouldn't alter the default width of %f. Encountered space width was %f", expectedWidth, boundingBox.size.width);
}
- (void)testSpaceBoundingBoxForWordKerning
{
CGSize size = CGSizeMake(200, 200);
UIFont *font = [UIFont systemFontOfSize:12];
CGFloat kernValue = 0.5;
NSDictionary *attributes = @{ASTextNodeWordKerningAttributeName: @(kernValue),
NSFontAttributeName : font};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@" " attributes:attributes];
_components = [ASTextKitComponents componentsWithAttributedSeedString:attributedString textContainerSize:size];
CGFloat expectedWidth = [@" " sizeWithAttributes:@{ NSFontAttributeName : font }].width + kernValue;
CGRect boundingBox = [_layoutManagerDelegate layoutManager:_components.layoutManager boundingBoxForControlGlyphAtIndex:0 forTextContainer:_components.textContainer proposedLineFragment:CGRectZero glyphPosition:CGPointZero characterIndex:0];
XCTAssertEqualWithAccuracy(boundingBox.size.width, expectedWidth, FLT_EPSILON, @"Word kerning shouldn't alter the default width of %f. Encountered space width was %f", expectedWidth, boundingBox.size.width);
}
- (NSInteger)_layoutManagerShouldGenerateGlyphs
{
NSRange stringRange = NSMakeRange(0, _components.textStorage.length);
NSRange glyphRange = [_components.layoutManager glyphRangeForCharacterRange:stringRange actualCharacterRange:NULL];
NSInteger glyphCount = glyphRange.length;
NSUInteger *characterIndexes = (NSUInteger *)malloc(sizeof(NSUInteger) * glyphCount);
for (NSUInteger i=0; i < stringRange.length; i++) {
characterIndexes[i] = i;
}
NSGlyphProperty *glyphProperties = (NSGlyphProperty *)malloc(sizeof(NSGlyphProperty) * glyphCount);
CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * glyphCount);
NSInteger glyphsToChange = [_layoutManagerDelegate layoutManager:_components.layoutManager shouldGenerateGlyphs:glyphs properties:glyphProperties characterIndexes:characterIndexes font:[UIFont systemFontOfSize:12.0] forGlyphRange:stringRange];
free(characterIndexes);
free(glyphProperties);
free(glyphs);
return glyphsToChange;
}
- (void)testPerCharacterWordKerning
{
[self setupTextKitComponentsWithWordKerningDifferentFontSizes];
CGPoint glyphPosition = CGPointZero;
NSUInteger bigSpaceIndex = 0;
NSUInteger normalSpaceIndex = 1;
CGRect bigBoundingBox = [_layoutManagerDelegate layoutManager:_components.layoutManager boundingBoxForControlGlyphAtIndex:bigSpaceIndex forTextContainer:_components.textContainer proposedLineFragment:CGRectZero glyphPosition:glyphPosition characterIndex:bigSpaceIndex];
CGRect normalBoundingBox = [_layoutManagerDelegate layoutManager:_components.layoutManager boundingBoxForControlGlyphAtIndex:normalSpaceIndex forTextContainer:_components.textContainer proposedLineFragment:CGRectZero glyphPosition:glyphPosition characterIndex:normalSpaceIndex];
XCTAssertTrue(bigBoundingBox.size.width > normalBoundingBox.size.width, @"Unbolded and bolded spaces should have different kerning");
}
- (void)testWordKerningDoesNotAlterGlyphOrigin
{
CGSize size = CGSizeMake(200, 200);
NSDictionary *attributes = @{ASTextNodeWordKerningAttributeName: @".5"};
NSString *seedString = @" ";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:seedString attributes:attributes];
UIFont *normalFont = [UIFont systemFontOfSize:12];
[attributedString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, 1)];
_components = [ASTextKitComponents componentsWithAttributedSeedString:attributedString textContainerSize:size];
CGPoint glyphPosition = CGPointMake(42, 54);
CGRect boundingBox = [_layoutManagerDelegate layoutManager:_components.layoutManager boundingBoxForControlGlyphAtIndex:0 forTextContainer:_components.textContainer proposedLineFragment:CGRectZero glyphPosition:glyphPosition characterIndex:0];
XCTAssertTrue(CGPointEqualToPoint(glyphPosition, boundingBox.origin), @"Word kerning shouldn't alter the origin point of a glyph");
}
@end