CTTypesetterCreateLine returns line with incorrect advance for last glyph

Originator:st
Number:rdar://38258815 Date Originated:March 8 2018
Status: Resolved:
Product:iOS + SDK Product Version:Xcode Version 9.3 beta 4 (9Q127n)
Classification:Other bug Reproducible:Always
 
Area:
Core Graphics

Summary:
When CTTypesetterCreateLine creates a line for a range in the typesetter's string that ends in the middle of a word, it determines the advance for the last glyph in the returned line as if the glyph was still followed by the remaining glyphs of the word. It does not adjust the last glyph's advance for the fact that it is no longer followed by another glyph. This often results in a typographic width of the returned line that is too small.

Steps to Reproduce:

Run the following Swift code e.g. in an iOS Swift playground:

let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: "HelveticaNeue", size:18)!]
let tLine = CTLineCreateWithAttributedString(NSAttributedString(string: "T", attributes: attrs))
let tWidth = CTLineGetTypographicBounds(tLine, nil, nil, nil)
print("expected line width: ", tWidth)

let typesetter = CTTypesetterCreateWithAttributedString(NSAttributedString(string: "Te", attributes: attrs))
let tLine2 = CTTypesetterCreateLine(typesetter, CFRange(location: 0, length: 1))
let tWidth2 = CTLineGetTypographicBounds(tLine2, nil, nil, nil)
print("actual line width: ", tWidth2)

Expected Results:

The code should print:
expected line width:  10.332
actual line width:  10.332

Actual Results:
The code prints:
expected line width:  10.332
actual line width:  8.334

The typographic width of tLine2 is too short by 2 pts.

Version/Build:
Version 9.3 beta 4 (9Q127n)

Configuration:
NA

Comments
No Comments

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!