Skip to content

Commit

Permalink
Add Code That Fix the bug and Final Screenshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
Athipat Nampetch committed Oct 21, 2021
1 parent 1d012ab commit 9ca2aa2
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion Source/TextExperiment/Component/ASTextLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,53 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri
return [self layoutWithContainer:container text:text range:NSMakeRange(0, text.length)];
}

+ (BOOL)_isBeginningLineIndexOfParagraph:(NSAttributedString *)text index:(NSUInteger)index
{
NSString *string = [text string];
NSUInteger length = [string length];

NSRange paragraphRange = [self _rangeOfParagraphsContainingRange:string range:NSMakeRange(0, 0) parBegIndex:NULL parEndIndex:NULL];

while (paragraphRange.length)
{
if (index == paragraphRange.location) {
return true;
}
if (index < paragraphRange.location) {
return false;
}
NSUInteger nextParagraphBegin = NSMaxRange(paragraphRange);
if (nextParagraphBegin>=length)
{
break;
}
// next paragraph
paragraphRange = [self _rangeOfParagraphsContainingRange:string range:NSMakeRange(nextParagraphBegin, 0) parBegIndex:NULL parEndIndex:NULL];
}
return false;
}

+ (NSRange)_rangeOfParagraphsContainingRange:(NSString *)text range:(NSRange)range parBegIndex:(NSUInteger *)parBegIndex parEndIndex:(NSUInteger *)parEndIndex
{
// get beginning and end of paragraph containing the replaced range
CFIndex beginIndex;
CFIndex endIndex;

CFStringGetParagraphBounds((__bridge CFStringRef)text, CFRangeMake(range.location, range.length), &beginIndex, &endIndex, NULL);

if (parBegIndex)
{
*parBegIndex = beginIndex;
}

if (parEndIndex)
{
*parEndIndex = endIndex;
}
// endIndex is the first character of the following paragraph, so we don't need to add 1
return NSMakeRange(beginIndex, endIndex - beginIndex);
}

+ (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttributedString *)text range:(NSRange)range {
ASTextLayout *layout = NULL;
CGPathRef cgPath = nil;
Expand Down Expand Up @@ -864,10 +911,25 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri
[lastLineText insertAttributedString:truncationToken atIndex:0];
}
}

CGFloat headIndent = 0;

BOOL isAtBeginOfParagraph = [self _isBeginningLineIndexOfParagraph:text index:lastLine.range.location];
// get the paragraph style at this index
CTParagraphStyleRef paragraphStyle = (__bridge CTParagraphStyleRef)[text attribute:(id)kCTParagraphStyleAttributeName atIndex:lastRange.location effectiveRange:NULL];

if (isAtBeginOfParagraph)
{
CTParagraphStyleGetValueForSpecifier(paragraphStyle, kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(headIndent), &headIndent);
}
else
{
CTParagraphStyleGetValueForSpecifier(paragraphStyle, kCTParagraphStyleSpecifierHeadIndent, sizeof(headIndent), &headIndent);
}

CTLineRef ctLastLineExtend = CTLineCreateWithAttributedString((CFAttributedStringRef) lastLineText);
if (ctLastLineExtend) {
CTLineRef ctTruncatedLine = CTLineCreateTruncatedLine(ctLastLineExtend, truncatedWidth, type, truncationTokenLine);
CTLineRef ctTruncatedLine = CTLineCreateTruncatedLine(ctLastLineExtend, truncatedWidth - headIndent, type, truncationTokenLine);
CFRelease(ctLastLineExtend);
if (ctTruncatedLine) {
truncatedLine = [ASTextLine lineWithCTLine:ctTruncatedLine position:lastLine.position vertical:isVerticalForm];
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 9ca2aa2

Please sign in to comment.