Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Remove Foundation from CTLine - #2586

Merged
Jeyaraj (msft-Jeyaram) merged 9 commits into
microsoft:developfrom
msft-Jeyaram:CTLine_Develop
Apr 24, 2017
Merged

Jeyaraj (msft-Jeyaram) merged 9 commits into
microsoft:developfrom
msft-Jeyaram:CTLine_Develop

Conversation

@msft-Jeyaram

@msft-Jeyaram Jeyaraj (msft-Jeyaram) commented Apr 20, 2017 •

Copy link
Copy Markdown
  • Implementation of CTLine via runtime based
  • Reduced/Removed Attributed string creation in loops
  • Implemented CFAttribute functions to append and insert attributed string
  • Implemented NSAttributes in terms of CFAttributes
  • Fixed memory leaks
  • Code improvements
  • Increased perf

Fixes #2357,#2451

image

@DHowett-MSFT

Dustin L. Howett (MSFT) (DHowett-MSFT) commented Apr 21, 2017 •

Copy link
Copy Markdown

Nit: please reword to be in imperative mood and not end in a period. #Resolved

@aballway Allen Ballway (aballway) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the perf differences from these changes? I don't see anything that should significantly affect memory or cpu time.

Comment thread Frameworks/CoreText/CTLine.mm Outdated
return CFRangeMake(start, end - start);
}

static void __CFAtributedStringAppendAttributedString(CFMutableAttributedStringRef attString, CFAttributedStringRef appendString) {

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attributed* #Resolved

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename first param to attrString #Resolved

Comment thread Frameworks/CoreText/CTLine.mm Outdated
ret->_glyphCount = _glyphCount;
ret->_runs.attach(CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, _runs));
ret->_relativeXOffset = _relativeXOffset;
static CFMutableAttributedStringRef __getTruncatedStringFromSourceLine(CTLineRef sourceLine,

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: follow naming convention - __CTLineGet... #Resolved

Comment thread Frameworks/CoreText/CTLine.mm Outdated
#import <cwchar>
#import <vector>

static inline CFRange __CFRangeIntersectionRange(CFRange a, CFRange b) {

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be made a more public helper function? #WontFix

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CFIndex can be negative, so you need to make sure that the lengths are >= 0L #WontFix

@msft-Jeyaram Jeyaraj (msft-Jeyaram) Apr 21, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly this guy has to go :(.
Also the CF version of this in CFStroage doesn't seem to account for it.


In reply to: 112723252 [](ancestors = 112723252)

Comment thread Frameworks/CoreText/CTLine.mm Outdated
CFIndex lenDiff = 0;
CFRange currentRange;

while ((index >= enumerationRange.location) && index < (enumerationRange.location + enumerationRange.length)) {

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add some comments about what's going on in this #Resolved

Comment thread Frameworks/CoreText/CTLine.mm Outdated
index += (currentRange.length + lenDiff);
enumerationRange.length += lenDiff;

if (currentRange.length == 0) {

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 0L. #Resolved

CFIndex CTLineGetGlyphCount(CTLineRef line) {
return line ? static_cast<_CTLine*>(line)->_glyphCount : 0;
RETURN_RESULT_IF_NULL(line, 0);
return line->_glyphCount;

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all be getter methods #WontFix

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be ideal, but the earlier code violated the getter/setter pattern and exposed private vars outside the file.
so it doesn't help us much here.


In reply to: 112724807 [](ancestors = 112724807)

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean in the paradigm this was done in CTLineGetGlyphCount was the getter, but now since we're updating this to use CppBase and touching everything we should use better style 🛩 #ByDesign

@msft-Jeyaram Jeyaraj (msft-Jeyaram) Apr 21, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline functions in structs are pretty awesome ;P Also it troubles me to write a getter for a public field.


In reply to: 112742078 [](ancestors = 112742078)

Comment thread Frameworks/CoreText/CTLine.mm Outdated
double CTLineGetTypographicBounds(CTLineRef lineRef, CGFloat* ascent, CGFloat* descent, CGFloat* leading) {
_CTLine* line = static_cast<_CTLine*>(lineRef);

__CTLine* line = const_cast<__CTLine*>(lineRef);

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just move this into a CTLine::GetTypographicBounds(...) so it doesn't need to be mutated like this. Since the values are computed lazily just make them mutable so it can stay const. #ByDesign

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not going to make logic changes in this CR.
That's out of scope, it should of been done initially to avoid modifying private vars.


In reply to: 112725004 [](ancestors = 112725004)

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't be a logic change, just moving it into __CTLine like we do with other CppBase classes #ByDesign

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay , you want to move the whole function into __CTLine.
uh, this is where it's going to get weird. __CTline is already violated elsewhere, so it has to go public strut.
Then this moves into it and expose a function. it would be ideal to clean everything up, so please open a bug for it.
The main purpose of this is to get rid of foundation.


In reply to: 112742309 [](ancestors = 112742309)

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't require the rest of __CTLine be moved into a private struct (which would be better but would be some more churn). We're already adding extra abstraction with _CTLineCreate so we might as well do this right 😸 #Resolved

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL yes it would not require for that to occur, uh but this just get's uglier.


In reply to: 112746377 [](ancestors = 112746377)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we need the cast anyways, since this method is modifying lineRef. so we can't do lineRef->GetTypographics...(), given GetTypographics can't be a const method.


In reply to: 112746739 [](ancestors = 112746739,112746377)

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values being modified are for all intents and purposes constant for this method, they're just being lazily calculated, which is why they should be marked mutable. I did something similar w/ CGDataProvider to keep things const even though the values were being lazily modified #Resolved

* @return Unmutable array of _CTLine objects created with the requested parameters.
*/
static _CTLine* _DWriteGetLine(CFAttributedStringRef string) {
static CTLineRef _DWriteGetLine(CFAttributedStringRef string) {

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this one is Get since it returns a +1, but can you change this to Create* #Resolved

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why it was named like that too ;P


In reply to: 112725508 [](ancestors = 112725508)

@ms-jihua ms-jihua Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allen Ballway (@aballway) didn't you name this #ByDesign

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this one is 100% my fault. No clue why I named it that 🤷‍♂️ #ByDesign

//******************************************************************************
//
// Copyright (c) 2016 Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are nice but again unrelated to the rest of these changes. Pull these into a separate commit? #ByDesign

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The can easily be separated into a commit.


In reply to: 112725826 [](ancestors = 112725826)

Comment thread Frameworks/CoreText/CTLine.mm Outdated
CFArrayRef glyphRuns = CTLineGetGlyphRuns(sourceLine);
CFIndex numberOfRuns = CFArrayGetCount(glyphRuns);
double extractedWidth = 0;
bool done = 0;

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: false #Resolved

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL this is from earlier code.
I'll update it.


In reply to: 112725988 [](ancestors = 112725988)

Comment thread Frameworks/CoreText/CTLine.mm Outdated
CFIndex lenDiff = 0;
CFRange currentRange;

while ((index >= enumerationRange.location) && index < (enumerationRange.location + enumerationRange.length)) {

@ms-jihua ms-jihua Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use CFAttributedStringReplaceAttributedString instead #ByDesign

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reflects what NSAttributedString is doing.
Given i don't have the full range of context on the CoreText dancing here, I decide to do the same.
which is also pretty trivial. If you are 100% this is sufficient, i can move forward.


In reply to: 112734332 [](ancestors = 112734332)

@ms-jihua ms-jihua Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely just use CFAttributedStringReplaceAttributedString - I am 100% sure it is sufficient. The reason we don't use it for [NSMutableAttributedString appendAttributedString:] is to reduce the number of abstract functions. Before we made that class more properly abstract, the initial implementation of it DID use ReplaceAttributedString. #Resolved

CFRangeMake(index, CFAttributedStringGetLength(typesetter->AttributedString()) - index),
CGRectMake(offset, 0, width, FLT_MAX));
CTFrameRef frame = _DWriteGetFrame(typesetter->AttributedString(),
CFRangeMake(index, CFAttributedStringGetLength(typesetter->AttributedString()) - index),

@ms-jihua ms-jihua Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL #ByDesign

@msft-Jeyaram

Copy link
Copy Markdown
Author

Allen Ballway (@aballway) there is, #1 you aren't doing message dispatches anymore, second there some redundant code that was reduced + reduced allocations.
Updating this PR with run time perfs with benchmark.

@DHowett-MSFT

Dustin L. Howett (MSFT) (DHowett-MSFT) commented Apr 21, 2017 •

Copy link
Copy Markdown

Nit: please reword to be in imperative mood and not end in a period. #Resolved

@msft-Jeyaram Jeyaraj (msft-Jeyaram) changed the title Removal of Foundation from CTLine. Removing Foundation from CTLine Apr 21, 2017
@msft-Jeyaram Jeyaraj (msft-Jeyaram) changed the title Removing Foundation from CTLine Remove Foundation from CTLine Apr 21, 2017
@msft-Jeyaram

Jeyaraj (msft-Jeyaram) commented Apr 21, 2017 •

Copy link
Copy Markdown
Author

Comment thread Frameworks/CoreText/CTLine.mm Outdated

CFRange range = { 0, CFStringGetLength(string) };
CFAttributedStringSetAttributes(attString, { range.location + replaceRange.location, range.length }, attribs, true);
CFAttributedStringSetAttributes(attrString, { range.location + replaceRange.location, range.length }, attribs, true);

@DHowett-MSFT Dustin L. Howett (MSFT) (DHowett-MSFT) Apr 21, 2017 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range.location + replaceRange.location

soooo...

replaceRange.location?

wait, sooo

index ? #Resolved

Comment thread Frameworks/include/CoreTextInternal.h Outdated
CGFloat _ascent, _descent, _leading;
}
@end
CGFloat _ascent;

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so then if these are marked mutable you'll be able to keep GetTypographicBounds() const 😄 #Resolved

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh mutable 😄


In reply to: 112780387 [](ancestors = 112780387)

Comment thread Frameworks/CoreText/CTLine.mm Outdated
_CTLine* line = static_cast<_CTLine*>(lineRef);

RETURN_RESULT_IF((!line) || (CFArrayGetCount(line->_runs) == 0), 0);
double __CTLine::GetTypographicBounds(CGFloat* ascent, CGFloat* descent, CGFloat* leading) {

@aballway Allen Ballway (aballway) Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3 #Resolved

@aballway Allen Ballway (aballway) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approval pending mutable

}

// Inserts the characters and attribute of the given string into the receiver at the given index.
static inline void __CFAttributedStringInsertAttributedStringOneAttribute(CFMutableAttributedStringRef attrString,

@ms-jihua ms-jihua Apr 21, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these would provide much perf improvement over your other non-OneAttribute helper, would they? I would like to just get rid of them. #ByDesign

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we keep on creating AttributedStrings and trying to insert them in the for-loop for based on the glyph count, it can be a bottleneck.
If glyph count is usually low, then this isn't needed.


In reply to: 112783999 [](ancestors = 112783999)

@ms-jihua ms-jihua left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to not have the OneAttribute helpers (possibly the non-OneAttribute helper would even be faster...), otherwise fine.

@msft-Jeyaram

Copy link
Copy Markdown
Author

image

Jeyaram Jeyaraj added 8 commits April 21, 2017 15:50
- Implementation of CTLine via runtime based
- Reduced/Removed Attributed string creation in loops
- Implemented CFAttribute functions to append and insert attributed string
- Implemented NSAttributes in terms of CFAttributes
- Code improvements
- Increased Perf
Fixes microsoft#2357
NSMutableAttributedString stringFromToken is created, but the function can return upon an invalid truncationType before stringFromToken is released.
Fixes microsoft#2451
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants