-
Notifications
You must be signed in to change notification settings - Fork 67
fix(ios): apply obliqueness if italics are not supported #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
74e63d3
fix(ios): apply obliqueness if italics are not supported
hejsztynx bb2e588
feat: drop the neutral kind behavior
hejsztynx e443c55
chore: update e2e baseline screenshots
hejsztynx bd9ec44
refactor: neutral character set optimalization
hejsztynx e063bd5
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx 43e32ea
refactor: stylePriority rename
hejsztynx 01b3185
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx 23961d2
refactor: less merge conflicts with customstyle
hejsztynx f412b2e
refactor: move logic to ItalicStyle.mm
hejsztynx e5705ad
refactor: extract priorityStyling sorting logic
hejsztynx e459c79
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx 71baac6
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx 799ee36
fix: grammar
hejsztynx f68c7cc
refactor: italicstyle file
hejsztynx b20c423
refactor: sorting array util extraction
hejsztynx b3f78f5
refactor: introduce ItalicUtils file
hejsztynx b66fb35
fix: remove unnecessary console warning
hejsztynx ee170d2
refactor: privatization of a constant
hejsztynx 6c23d92
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx 0a64ac7
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx c34015d
Merge branch 'main' into @ksienkiewicz/fix-ios-cjk-italics
hejsztynx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Binary file modified
BIN
+8 Bytes
(100%)
.maestro/enrichedInput/screenshots/ios/inline_styles_merge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+21 Bytes
(100%)
.maestro/enrichedInput/screenshots/ios/inline_styles_removal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #import <Foundation/Foundation.h> | ||
| #pragma once | ||
|
|
||
| @interface NSArray (ArrayExtension) | ||
| - (NSArray *)sortedArrayBySortKey:(NSInteger (^)(id item))sortKeyForItem; | ||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #import "ArrayExtension.h" | ||
|
|
||
| @implementation NSArray (ArrayExtension) | ||
|
|
||
| - (NSArray *)sortedArrayBySortKey:(NSInteger (^)(id item))sortKeyForItem { | ||
| return [self sortedArrayWithOptions:NSSortStable | ||
| usingComparator:^NSComparisonResult(id a, id b) { | ||
| NSInteger aKey = sortKeyForItem(a); | ||
| NSInteger bKey = sortKeyForItem(b); | ||
| if (aKey == bKey) { | ||
| return NSOrderedSame; | ||
| } | ||
| return aKey < bKey ? NSOrderedAscending | ||
| : NSOrderedDescending; | ||
| }]; | ||
| } | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #import <UIKit/UIKit.h> | ||
| #pragma once | ||
|
|
||
| typedef NS_ENUM(NSInteger, ItalicKind) { | ||
| // character must not be slanted at all (whitespace, control characters, | ||
| // text attachments) | ||
| ItalicKindNone, | ||
| // font has a real italic glyph for the character | ||
| ItalicKindFont, | ||
| // no italic glyph available, the slant has to be used | ||
| ItalicKindOblique, | ||
| }; | ||
|
|
||
| @interface ItalicUtils : NSObject | ||
|
|
||
| + (void)applyItalicForFont:(UIFont *)font | ||
| inTextStorage:(NSTextStorage *)textStorage | ||
| inRange:(NSRange)range; | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| #import "ItalicUtils.h" | ||
| #import "FontExtension.h" | ||
|
|
||
| // slant used when a font has no italic face | ||
| static const CGFloat kObliquenessFallback = 0.2; | ||
|
|
||
| @interface ItalicUtils () | ||
|
|
||
| + (NSCharacterSet *)nonNeutralCharacters; | ||
|
|
||
| + (ItalicKind)kindForCluster:(NSString *)cluster | ||
| font:(UIFont *)font | ||
| italicFont:(UIFont *)italicFont | ||
| hasItalicFace:(BOOL)hasItalicFace; | ||
|
|
||
| + (void)applyKind:(ItalicKind)kind | ||
| toSegment:(NSRange)segment | ||
| inTextStorage:(NSTextStorage *)textStorage | ||
| withItalicFont:(UIFont *)italicFont; | ||
|
|
||
| @end | ||
|
|
||
| @implementation ItalicUtils | ||
|
|
||
| + (void)applyItalicForFont:(UIFont *)font | ||
| inTextStorage:(NSTextStorage *)textStorage | ||
| inRange:(NSRange)range { | ||
| UIFont *italicFont = [font setItalic]; | ||
| BOOL hasItalicFace = [italicFont isItalic]; | ||
|
|
||
| NSMutableArray<NSValue *> *clusterRanges = [NSMutableArray array]; | ||
| NSMutableArray<NSNumber *> *clusterKinds = [NSMutableArray array]; | ||
|
|
||
| // we process each composed character sequence and classify it to a specific | ||
| // ItalicKind | ||
| [textStorage.string | ||
| enumerateSubstringsInRange:range | ||
| options:NSStringEnumerationByComposedCharacterSequences | ||
| usingBlock:^(NSString *_Nullable cluster, | ||
| NSRange clusterRange, NSRange _, | ||
| BOOL *_Nonnull stop) { | ||
| if (cluster.length == 0) { | ||
| return; | ||
| } | ||
| [clusterRanges | ||
| addObject:[NSValue valueWithRange:clusterRange]]; | ||
| [clusterKinds | ||
| addObject:@([self kindForCluster:cluster | ||
| font:font | ||
| italicFont:italicFont | ||
| hasItalicFace:hasItalicFace])]; | ||
| }]; | ||
|
|
||
| // merge neighbouring clusters of the same kind and apply the style | ||
| NSUInteger index = 0; | ||
| while (index < clusterKinds.count) { | ||
| NSUInteger endIndex = index + 1; | ||
| ItalicKind kind = (ItalicKind)[clusterKinds[index] integerValue]; | ||
| while (endIndex < clusterKinds.count && | ||
| (ItalicKind)[clusterKinds[endIndex] integerValue] == kind) { | ||
| endIndex += 1; | ||
| } | ||
|
|
||
| NSRange startRange = [clusterRanges[index] rangeValue]; | ||
| NSRange endRange = [clusterRanges[endIndex - 1] rangeValue]; | ||
| NSRange segment = NSMakeRange(startRange.location, | ||
| NSMaxRange(endRange) - startRange.location); | ||
|
|
||
| [self applyKind:kind | ||
| toSegment:segment | ||
| inTextStorage:textStorage | ||
| withItalicFont:italicFont]; | ||
|
|
||
| index = endIndex; | ||
| } | ||
| } | ||
|
|
||
| // contains all characters except whitespaces, newlines, | ||
| // control characters and ZWS | ||
| + (NSCharacterSet *)nonNeutralCharacters { | ||
| static NSCharacterSet *nonNeutral = nil; | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
| NSMutableCharacterSet *set = | ||
| [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy]; | ||
| [set formUnionWithCharacterSet:[NSCharacterSet controlCharacterSet]]; | ||
| [set addCharactersInString:[NSString | ||
| stringWithFormat:@"%C", (unichar)0x200B]]; | ||
| nonNeutral = [[set invertedSet] copy]; | ||
| }); | ||
| return nonNeutral; | ||
| } | ||
|
|
||
| + (ItalicKind)kindForCluster:(NSString *)cluster | ||
| font:(UIFont *)font | ||
| italicFont:(UIFont *)italicFont | ||
| hasItalicFace:(BOOL)hasItalicFace { | ||
| if ([cluster rangeOfCharacterFromSet:[self nonNeutralCharacters]].location == | ||
| NSNotFound) { | ||
| return ItalicKindNone; | ||
| } | ||
|
|
||
| // we just need to analyze the first unicode character to classify the whole | ||
| // cluster | ||
| unichar chars[2] = {0, 0}; | ||
| CFIndex count = 1; | ||
| chars[0] = [cluster characterAtIndex:0]; | ||
| if (CFStringIsSurrogateHighCharacter(chars[0]) && cluster.length > 1) { | ||
| chars[1] = [cluster characterAtIndex:1]; | ||
| count = 2; | ||
| } | ||
|
|
||
| if (chars[0] == (unichar)NSAttachmentCharacter) { | ||
| return ItalicKindNone; | ||
| } | ||
|
|
||
| BOOL coveredByFont = [font coversCharacters:chars count:count]; | ||
|
|
||
| // italic style is supported - we use it | ||
| if (coveredByFont && hasItalicFace && italicFont != nullptr && | ||
| [italicFont coversCharacters:chars count:count]) { | ||
| return ItalicKindFont; | ||
| } | ||
|
|
||
| // italic is not supported, we use the slant instead | ||
| return ItalicKindOblique; | ||
| } | ||
|
|
||
| + (void)applyKind:(ItalicKind)kind | ||
| toSegment:(NSRange)segment | ||
| inTextStorage:(NSTextStorage *)textStorage | ||
| withItalicFont:(UIFont *)italicFont { | ||
| switch (kind) { | ||
| case ItalicKindFont: | ||
| [textStorage addAttribute:NSFontAttributeName | ||
| value:italicFont | ||
| range:segment]; | ||
| [textStorage removeAttribute:NSObliquenessAttributeName range:segment]; | ||
| break; | ||
| case ItalicKindOblique: | ||
| [textStorage addAttribute:NSObliquenessAttributeName | ||
| value:@(kObliquenessFallback) | ||
| range:segment]; | ||
| break; | ||
| case ItalicKindNone: | ||
| [textStorage removeAttribute:NSObliquenessAttributeName range:segment]; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| @end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.