Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Frameworks/CoreText/CTLine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,17 @@ CFRange CTLineGetStringRange(CTLineRef line) {
}

/**
@Status Stub
@Status Interoperable
@Notes
*/
CGRect CTLineGetImageBounds(CTLineRef line, CGContextRef context) {
UNIMPLEMENTED();
return StubReturn();
if (!line || !context) {
return CGRectNull;
}

CGFloat ascent, descent;
double width = CTLineGetTypographicBounds(line, &ascent, &descent, nullptr);
return { CGContextGetTextPosition(context), { width, ascent - descent } };
}

/**
Expand Down
35 changes: 23 additions & 12 deletions Frameworks/CoreText/CTRun.mm
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,22 @@ CFRange CTRunGetStringRange(CTRunRef run) {
*/
double CTRunGetTypographicBounds(CTRunRef run, CFRange range, CGFloat* ascent, CGFloat* descent, CGFloat* leading) {
if (run == nullptr || range.length < 0L) {
return 0;
return 0.0;
}

_CTRun* curRun = static_cast<_CTRun*>(run);

if (range.length == 0) {
if (range.length == 0L) {
range = curRun->_range;
}

if (range.location < 0) {
if (range.location < 0L) {
range.length += range.location;
range.location = 0;
range.location = 0L;
}

if ((range.location + range.length) > (curRun->_range.location + curRun->_range.length) || range.length <= 0) {
return 0;
if ((range.location + range.length) > (curRun->_range.location + curRun->_range.length) || range.length <= 0L) {
return 0.0;
}

if (ascent || descent || leading) {
Expand All @@ -252,13 +252,14 @@ CFRange CTRunGetStringRange(CTRunRef run) {

CGFloat newAscent = -FLT_MAX;
CGFloat newDescent = -FLT_MAX;
for (size_t i = range.location - curRun->_range.location; i < range.location + range.length - curRun->_range.location; ++i) {
for (size_t i = 0; i < range.length; ++i) {
// CoreText ascent is equivalent of DWrite verticalOriginY, and descent the value of bottomSideBearing
// which are in designUnits, so they need to be converted to points for CoreText
// The ascent and descent of the run is the max and min of the respective values per glyph
// The ascent and descent of the run are the max of the respective values per glyph
newAscent = std::max(newAscent, glyphMetrics[i].verticalOriginY * scalingFactor);
newDescent = std::max(newDescent, glyphMetrics[i].bottomSideBearing * scalingFactor);
}

if (ascent) {
*ascent = newAscent;
}
Expand All @@ -279,12 +280,22 @@ CFRange CTRunGetStringRange(CTRunRef run) {
}

/**
@Status Stub
@Status Interoperable
@Notes
*/
CGRect CTRunGetImageBounds(CTRunRef run, CGContextRef context, CFRange range) {
UNIMPLEMENTED();
return StubReturn();
CGRect CTRunGetImageBounds(CTRunRef runRef, CGContextRef context, CFRange range) {
_CTRun* run = static_cast<_CTRun*>(runRef);
if (!run || !context || range.location < 0L || range.length < 0L || range.location + range.length > run->_dwriteGlyphRun.glyphCount) {
return CGRectNull;
}

if (range.location == 0L) {
range.location = run->_dwriteGlyphRun.glyphCount - range.location;
}

CGFloat ascent, descent;
double width = CTRunGetTypographicBounds(runRef, range, &ascent, &descent, nullptr);
return { CGContextGetTextPosition(context), { width, ascent - descent } };
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Frameworks/include/CppUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::basic_ostream<T>& operator<<(std::basic_ostream<T>& os, const CFRange& rang

#pragma region CGPoint
inline bool operator==(const CGPoint& lhs, const CGPoint& rhs) {
return ((std::abs(lhs.x - rhs.x) < 0.00001) && (std::abs(lhs.y - rhs.y) < 0.00001));
return lhs.x == rhs.x && lhs.y == rhs.y;

Choose a reason for hiding this comment

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

This will almost certainly make tests fail on ARM. We had these set up for a reason. What's up?

Choose a reason for hiding this comment

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

Oh, I see. Interesting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tests passed on ARM

}

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/CoreText/CTLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ CORETEXT_EXPORT CFIndex CTLineGetGlyphCount(CTLineRef line);
CORETEXT_EXPORT CFArrayRef CTLineGetGlyphRuns(CTLineRef line);
CORETEXT_EXPORT CFRange CTLineGetStringRange(CTLineRef line);
CORETEXT_EXPORT double CTLineGetPenOffsetForFlush(CTLineRef line, CGFloat flushFactor, double flushWidth);
CORETEXT_EXPORT CGRect CTLineGetImageBounds(CTLineRef line, CGContextRef context) STUB_METHOD;
CORETEXT_EXPORT CGRect CTLineGetImageBounds(CTLineRef line, CGContextRef context);
CORETEXT_EXPORT CGRect CTLineGetBoundsWithOptions(CTLineRef line, CTLineBoundsOptions options) NOTINPLAN_METHOD;
CORETEXT_EXPORT double CTLineGetTypographicBounds(CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading);
CORETEXT_EXPORT double CTLineGetTrailingWhitespaceWidth(CTLineRef line) STUB_METHOD;
Expand Down
2 changes: 1 addition & 1 deletion include/CoreText/CTRun.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CORETEXT_EXPORT const CFIndex* CTRunGetStringIndicesPtr(CTRunRef run);
CORETEXT_EXPORT void CTRunGetStringIndices(CTRunRef run, CFRange range, CFIndex buffer[]);
CORETEXT_EXPORT CFRange CTRunGetStringRange(CTRunRef run);
CORETEXT_EXPORT double CTRunGetTypographicBounds(CTRunRef run, CFRange range, CGFloat* ascent, CGFloat* descent, CGFloat* leading);
CORETEXT_EXPORT CGRect CTRunGetImageBounds(CTRunRef run, CGContextRef context, CFRange range) STUB_METHOD;
CORETEXT_EXPORT CGRect CTRunGetImageBounds(CTRunRef run, CGContextRef context, CFRange range);
CORETEXT_EXPORT void CTRunDraw(CTRunRef run, CGContextRef context, CFRange range);
CORETEXT_EXPORT CGAffineTransform CTRunGetTextMatrix(CTRunRef run);
CORETEXT_EXPORT CFTypeID CTRunGetTypeID() STUB_METHOD;
Expand Down
27 changes: 21 additions & 6 deletions tests/unittests/CoreGraphics/CGContextTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,16 @@ virtual void SetUp() {
/* static */
ComPtr<ID2D1RenderTarget> ContextCoordinateTest::renderTarget;

static constexpr double c_errorDelta = 0.0001;
TEST_P(ContextCoordinateTest, ConvertToDeviceSpace) {
const std::vector<CGPoint>& userSpacePoints = ::testing::get<1>(GetParam());
const std::vector<CGPoint>& deviceSpacePoints = ::testing::get<2>(GetParam());
for (unsigned int i = 0; i < userSpacePoints.size(); ++i) {
const CGPoint& userSpacePoint = userSpacePoints[i];
const CGPoint& deviceSpacePoint = deviceSpacePoints[i];
EXPECT_EQ(deviceSpacePoint, CGContextConvertPointToDeviceSpace(context.get(), userSpacePoint));
CGPoint result = CGContextConvertPointToDeviceSpace(context.get(), userSpacePoint);
EXPECT_NEAR(deviceSpacePoint.x, result.x, c_errorDelta);
EXPECT_NEAR(deviceSpacePoint.y, result.y, c_errorDelta);
}
}

Expand All @@ -343,7 +346,9 @@ virtual void SetUp() {
for (unsigned int i = 0; i < userSpacePoints.size(); ++i) {
const CGPoint& userSpacePoint = userSpacePoints[i];
const CGPoint& deviceSpacePoint = deviceSpacePoints[i];
EXPECT_EQ(userSpacePoint, CGContextConvertPointToUserSpace(context.get(), deviceSpacePoint));
CGPoint result = CGContextConvertPointToUserSpace(context.get(), deviceSpacePoint);
EXPECT_NEAR(userSpacePoint.x, result.x, c_errorDelta);
EXPECT_NEAR(userSpacePoint.y, result.y, c_errorDelta);
}
}

Expand Down Expand Up @@ -406,8 +411,13 @@ virtual void SetUp() {
woc::unique_cf<CGColorSpaceRef> rgbColorSpace(CGColorSpaceCreateDeviceRGB());

// Create a canvas context
woc::unique_cf<CGContextRef> context(CGBitmapContextCreate(
nullptr, 512, 256, 8, 4 * 512 /* bytesPerRow = bytesPerPixel*width*/, rgbColorSpace.get(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));
woc::unique_cf<CGContextRef> context(CGBitmapContextCreate(nullptr,
512,
256,
8,
4 * 512 /* bytesPerRow = bytesPerPixel*width*/,
rgbColorSpace.get(),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));

// Load an image from file
CFDataRef data = (CFDataRef)[NSData dataWithContentsOfFile:getPathToFile(@"data/jpg1.jpg")];
Expand Down Expand Up @@ -452,8 +462,13 @@ virtual void SetUp() {
ASSERT_NE(image, nullptr);

// This will be the pseudo canvas context which we will draw into
woc::unique_cf<CGContextRef> context(CGBitmapContextCreate(
nullptr, 512, 256, 8, 4 * 512 /* bytesPerRow = bytesPerPixel*width*/, rgbColorSpace.get(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));
woc::unique_cf<CGContextRef> context(CGBitmapContextCreate(nullptr,
512,
256,
8,
4 * 512 /* bytesPerRow = bytesPerPixel*width*/,
rgbColorSpace.get(),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));

CGRect bounds = { 0, 0, 512, 256 };

Expand Down
36 changes: 36 additions & 0 deletions tests/unittests/CoreText/CTLineTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import <UIKit/UIKit.h>
#include <TestFramework.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CppUtils.h>

NSMutableAttributedString* getAttributedString(NSString* str) {
UIFontDescriptor* fontDescriptor = [UIFontDescriptor fontDescriptorWithName:@"Segoe UI" size:40];
Expand Down Expand Up @@ -335,4 +336,39 @@

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(group);
}

TEST(CTLine, GetImageBounds) {
// Should return CGRectNull for null line or context
EXPECT_EQ(CGRectNull, CTLineGetImageBounds(nullptr, nullptr));

auto rgbColorSpace = woc::MakeAutoCF<CGColorSpaceRef>(CGColorSpaceCreateDeviceRGB());
auto context = woc::MakeAutoCF<CGContextRef>(CGBitmapContextCreate(nullptr,
512,
256,
8,
4 * 512 /* bytesPerRow = bytesPerPixel*width */,
rgbColorSpace.get(),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));
EXPECT_EQ(CGRectNull, CTLineGetImageBounds(nullptr, context));

CFAttributedStringRef string = (__bridge CFAttributedStringRef)getAttributedString(@"foobar");
auto line = woc::MakeAutoCF<CTLineRef>(CTLineCreateWithAttributedString(string));
EXPECT_EQ(CGRectNull, CTLineGetImageBounds(line, nullptr));

// Size of the rect is exactly the size of the line
CGFloat ascent, descent;
double width = CTLineGetTypographicBounds(line, &ascent, &descent, nullptr);
CGSize lineSize{ width, ascent - descent };
CGRect lineRect{ CGPointZero, lineSize };
EXPECT_EQ(lineRect, CTLineGetImageBounds(line, context));

// Size should be independent of CTM or Text Matrix, but origin is the text position
CGContextScaleCTM(context, 2, 3);
CGContextTranslateCTM(context, 35, 75);
CGContextSetTextMatrix(context, CGAffineTransform{ 1, 2, 3, 4, 0, 0 });
CGPoint origin{ 17, 23 };
CGContextSetTextPosition(context, origin.x, origin.y);
lineRect = { origin, lineSize };
EXPECT_EQ(lineRect, CTLineGetImageBounds(line, context));
}
65 changes: 65 additions & 0 deletions tests/unittests/CoreText/CTRunTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,68 @@ virtual void TearDown() {
run = (CTRunRef)CFArrayGetValueAtIndex(runsArray, 0);
EXPECT_EQ(kCTRunStatusRightToLeft | kCTRunStatusNonMonotonic, CTRunGetStatus(run));
}

TEST(CTRun, GetImageBounds) {
// Should return CGRectNull for null run or context
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(nullptr, nullptr, {}));

auto rgbColorSpace = woc::MakeAutoCF<CGColorSpaceRef>(CGColorSpaceCreateDeviceRGB());
auto context = woc::MakeAutoCF<CGContextRef>(CGBitmapContextCreate(nullptr,
512,
256,
8,
4 * 512 /* bytesPerRow = bytesPerPixel*width */,
rgbColorSpace.get(),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(nullptr, context, {}));

CFAttributedStringRef string = (__bridge CFAttributedStringRef)getString(@"foobar");
auto line = woc::MakeAutoCF<CTLineRef>(CTLineCreateWithAttributedString(string));
CFArrayRef runsArray = CTLineGetGlyphRuns(line);
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runsArray, 0);
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(run, nullptr, {}));

// Size of the rect is exactly the size of the run
CGFloat ascent, descent;
double width = CTRunGetTypographicBounds(run, {}, &ascent, &descent, nullptr);
CGSize runSize{ width, ascent - descent };
CGRect runRect{ CGPointZero, runSize };
EXPECT_EQ(runRect, CTRunGetImageBounds(run, context, {}));

// Size should be independent of CTM or Text Matrix, but origin is the text position
CGContextScaleCTM(context, 2, 3);
CGContextTranslateCTM(context, 35, 75);
CGContextSetTextMatrix(context, CGAffineTransform{ 1, 2, 3, 4, 0, 0 });
CGPoint origin{ 17, 23 };
CGContextSetTextPosition(context, origin.x, origin.y);
runRect = { origin, runSize };
EXPECT_EQ(runRect, CTRunGetImageBounds(run, context, {}));

// Range works the same for ImageBounds as TypographicBounds
CFRange range{ 1, 3 };
width = CTRunGetTypographicBounds(run, range, &ascent, &descent, nullptr);
runRect.size = { width, ascent - descent };
EXPECT_EQ(runRect, CTRunGetImageBounds(run, context, range));

// And returns CGRectNull for invalid ranges
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(run, context, { 7, 0 }));
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(run, context, { 7, 1 }));
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(run, context, { 1, -1 }));
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(run, context, { -1, 1 }));
}

TEST(CTRun, GetTypographicBoundsShouldHandleNonzeroLocation) {
CFAttributedStringRef string = (__bridge CFAttributedStringRef)getString(@"foobar");
auto line = woc::MakeAutoCF<CTLineRef>(CTLineCreateWithAttributedString(string));
CFArrayRef runsArray = CTLineGetGlyphRuns(line);
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runsArray, 0);
EXPECT_EQ(CGRectNull, CTRunGetImageBounds(run, nullptr, {}));

// Size of the rect is exactly the size of the run
CGFloat ascent, descent, leading;
double width = CTRunGetTypographicBounds(run, { 1, 2 }, &ascent, &descent, &leading);
EXPECT_NEAR(ascent, 29.1211, c_errorDelta);
EXPECT_NEAR(descent, 7.9492, c_errorDelta);
EXPECT_NEAR(leading, 0, c_errorDelta);
EXPECT_NEAR(width, 46.875, c_errorDelta);
}