Skip to content

Commit

Permalink
Remove double scaling of lineHeightMultiple & paragraphSpacing attrib…
Browse files Browse the repository at this point in the history
…utes in ASTextKitFontSizeAdjuster (TextureGroup#1056)
  • Loading branch information
ejensen authored and nguyenhuy committed Aug 15, 2018
1 parent 03e6ce0 commit 5f912d1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
8 changes: 6 additions & 2 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
ACF6ED611B178DC700DA7C62 /* ASOverlayLayoutSpecSnapshotTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = ACF6ED591B178DC700DA7C62 /* ASOverlayLayoutSpecSnapshotTests.mm */; };
ACF6ED621B178DC700DA7C62 /* ASRatioLayoutSpecSnapshotTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = ACF6ED5A1B178DC700DA7C62 /* ASRatioLayoutSpecSnapshotTests.mm */; };
ACF6ED631B178DC700DA7C62 /* ASStackLayoutSpecSnapshotTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = ACF6ED5B1B178DC700DA7C62 /* ASStackLayoutSpecSnapshotTests.mm */; };
AE440175210FB7CF00B36DA2 /* ASTextKitFontSizeAdjusterTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = AE440174210FB7CF00B36DA2 /* ASTextKitFontSizeAdjusterTests.mm */; };
AE6987C11DD04E1000B9E458 /* ASPagerNodeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AE6987C01DD04E1000B9E458 /* ASPagerNodeTests.m */; };
AEEC47E41C21D3D200EC1693 /* ASVideoNodeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AEEC47E31C21D3D200EC1693 /* ASVideoNodeTests.m */; };
B13CA0F81C519EBA00E031AB /* ASCollectionViewLayoutFacilitatorProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = B13CA0F61C519E9400E031AB /* ASCollectionViewLayoutFacilitatorProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -814,6 +815,7 @@
ACF6ED591B178DC700DA7C62 /* ASOverlayLayoutSpecSnapshotTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASOverlayLayoutSpecSnapshotTests.mm; sourceTree = "<group>"; };
ACF6ED5A1B178DC700DA7C62 /* ASRatioLayoutSpecSnapshotTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASRatioLayoutSpecSnapshotTests.mm; sourceTree = "<group>"; };
ACF6ED5B1B178DC700DA7C62 /* ASStackLayoutSpecSnapshotTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASStackLayoutSpecSnapshotTests.mm; sourceTree = "<group>"; };
AE440174210FB7CF00B36DA2 /* ASTextKitFontSizeAdjusterTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ASTextKitFontSizeAdjusterTests.mm; sourceTree = "<group>"; };
AE6987C01DD04E1000B9E458 /* ASPagerNodeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASPagerNodeTests.m; sourceTree = "<group>"; };
AEB7B0181C5962EA00662EF4 /* ASDefaultPlayButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASDefaultPlayButton.h; sourceTree = "<group>"; };
AEB7B0191C5962EA00662EF4 /* ASDefaultPlayButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASDefaultPlayButton.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1253,7 +1255,6 @@
058D09C5195D04C000B7D73C /* Tests */ = {
isa = PBXGroup;
children = (
CC35CEC520DD87280006448D /* ASCollectionsTests.m */,
DBC452DD1C5C6A6A00B16017 /* ArrayDiffingTests.m */,
AC026B571BD3F61800BBC17E /* ASAbsoluteLayoutSpecSnapshotTests.m */,
696FCB301D6E46050093471E /* ASBackgroundLayoutSpecSnapshotTests.mm */,
Expand All @@ -1264,6 +1265,7 @@
CC051F1E1D7A286A006434CB /* ASCALayerTests.m */,
ACF6ED531B178DC700DA7C62 /* ASCenterLayoutSpecSnapshotTests.mm */,
CCDD148A1EEDCD9D0020834E /* ASCollectionModernDataSourceTests.m */,
CC35CEC520DD87280006448D /* ASCollectionsTests.m */,
2538B6F21BC5D2A2003CA0B4 /* ASCollectionViewFlowLayoutInspectorTests.m */,
9F06E5CC1B4CAF4200F015D8 /* ASCollectionViewTests.mm */,
CCEDDDD8200C518800FFCD0A /* ASConfigurationTests.m */,
Expand Down Expand Up @@ -1314,8 +1316,10 @@
3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */,
CC4981B21D1A02BE004E13CC /* ASTableViewThrashTests.m */,
058D0A33195D057000B7D73C /* ASTextKitCoreTextAdditionsTests.m */,
AE440174210FB7CF00B36DA2 /* ASTextKitFontSizeAdjusterTests.mm */,
254C6B531BF8FF2A003EC431 /* ASTextKitTests.mm */,
254C6B511BF8FE6D003EC431 /* ASTextKitTruncationTests.mm */,
C057D9BC20B5453D00FC9112 /* ASTextNode2SnapshotTests.m */,
CC8B05D71D73979700F54286 /* ASTextNodePerformanceTests.m */,
81E95C131D62639600336598 /* ASTextNodeSnapshotTests.m */,
058D0A36195D057000B7D73C /* ASTextNodeTests.m */,
Expand All @@ -1325,7 +1329,6 @@
4496D0721FA9EA6B001CC8D5 /* ASTraitCollectionTests.m */,
CC0AEEA31D66316E005D1C78 /* ASUICollectionViewTests.m */,
AEEC47E31C21D3D200EC1693 /* ASVideoNodeTests.m */,
C057D9BC20B5453D00FC9112 /* ASTextNode2SnapshotTests.m */,
CCA221D21D6FA7EF00AF6A0F /* ASViewControllerTests.m */,
83A7D95D1D446A6E00BF333E /* ASWeakMapTests.m */,
CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.m */,
Expand Down Expand Up @@ -2265,6 +2268,7 @@
buildActionMask = 2147483647;
files = (
CCEDDDD9200C518800FFCD0A /* ASConfigurationTests.m in Sources */,
AE440175210FB7CF00B36DA2 /* ASTextKitFontSizeAdjusterTests.mm in Sources */,
E51B78BF1F028ABF00E32604 /* ASLayoutFlatteningTests.m in Sources */,
4496D0731FA9EA6B001CC8D5 /* ASTraitCollectionTests.m in Sources */,
29CDC2E21AAE70D000833CA4 /* ASBasicImageDownloaderContextTests.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Add an experimental feature that reuses CTFramesetter objects in ASTextNode2 to improve performance. [Adlai Holler](https://github.com/Adlai-Holler)
- Add NS_DESIGNATED_INITIALIZER to ASViewController initWithNode: [Michael Schneider](https://github.com/maicki) [#1054](https://github.com/TextureGroup/Texture/pull/1054)
- Optimize text stack by removing unneeded copying. [Adlai Holler](https://github.com/Adlai-Holler)
- Remove double scaling of lineHeightMultiple & paragraphSpacing attributes in ASTextKitFontSizeAdjuster. [Eric Jensen](https://github.com/ejensen)

## 2.7
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)
Expand Down
4 changes: 1 addition & 3 deletions Source/TextKit/ASTextKitFontSizeAdjuster.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ + (void)adjustFontSizeForAttributeString:(NSMutableAttributedString *)attrString
paragraphStyle.tailIndent = (paragraphStyle.tailIndent * scaleFactor);
paragraphStyle.minimumLineHeight = (paragraphStyle.minimumLineHeight * scaleFactor);
paragraphStyle.maximumLineHeight = (paragraphStyle.maximumLineHeight * scaleFactor);
paragraphStyle.lineHeightMultiple = (paragraphStyle.lineHeightMultiple * scaleFactor);
paragraphStyle.paragraphSpacing = (paragraphStyle.paragraphSpacing * scaleFactor);

[attrString removeAttribute:NSParagraphStyleAttributeName range:range];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
Expand Down Expand Up @@ -214,7 +212,7 @@ - (CGFloat)scaleFactor

if (longestWordFits == NO) {
// we need to check the longest word to make sure it fits
longestWordFits = std::ceil((longestWordSize.width * adjustedScale) <= _constrainedSize.width);
longestWordFits = std::ceil((longestWordSize.width * adjustedScale) <= _constrainedSize.width);
}

// if the longest word fits, go ahead and check max line and height. If it didn't fit continue to the next scale factor
Expand Down
51 changes: 51 additions & 0 deletions Tests/ASTextKitFontSizeAdjusterTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// ASTextKitFontSizeAdjusterTests.mm
// Texture
//
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//

#import <AsyncDisplayKit/ASTextKitFontSizeAdjuster.h>
#import <XCTest/XCTest.h>

@interface ASFontSizeAdjusterTests : XCTestCase

@end

@implementation ASFontSizeAdjusterTests

- (void)testFontSizeAdjusterAttributes
{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineHeightMultiple = 2.0;
paragraphStyle.lineSpacing = 2.0;
paragraphStyle.paragraphSpacing = 4.0;
paragraphStyle.firstLineHeadIndent = 6.0;
paragraphStyle.headIndent = 8.0;
paragraphStyle.tailIndent = 10.0;
paragraphStyle.minimumLineHeight = 12.0;
paragraphStyle.maximumLineHeight = 14.0;

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet"
attributes:@{ NSParagraphStyleAttributeName: paragraphStyle }];

[ASTextKitFontSizeAdjuster adjustFontSizeForAttributeString:string withScaleFactor:0.5];

NSParagraphStyle *adjustedParagraphStyle = [string attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:nil];

XCTAssertEqual(adjustedParagraphStyle.lineHeightMultiple, 2.0);
XCTAssertEqual(adjustedParagraphStyle.lineSpacing, 1.0);
XCTAssertEqual(adjustedParagraphStyle.paragraphSpacing, 2.0);
XCTAssertEqual(adjustedParagraphStyle.firstLineHeadIndent, 3.0);
XCTAssertEqual(adjustedParagraphStyle.headIndent, 4.0);
XCTAssertEqual(adjustedParagraphStyle.tailIndent, 5.0);
XCTAssertEqual(adjustedParagraphStyle.minimumLineHeight, 6.0);
XCTAssertEqual(adjustedParagraphStyle.maximumLineHeight, 7.0);
}

@end
16 changes: 16 additions & 0 deletions Tests/ASTextNodeSnapshotTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,20 @@ - (void)DISABLED_testThatTruncationTokenAttributesPrecedeThoseInheritedFromTextW
ASSnapshotVerifyNode(textNode, nil);
}

- (void)testFontPointSizeScaling
{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineHeightMultiple = 0.5;
paragraphStyle.lineSpacing = 2.0;

ASTextNode *textNode = [[ASTextNode alloc] init];
textNode.style.maxSize = CGSizeMake(60, 80);
textNode.pointSizeScaleFactors = @[@0.5];
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"Quality is an important thing"
attributes:@{ NSParagraphStyleAttributeName: paragraphStyle }];

ASDisplayNodeSizeToFitSizeRange(textNode, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
ASSnapshotVerifyNode(textNode, nil);
}

@end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5f912d1

Please sign in to comment.