Skip to content
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

Fix CRLF Line Ending Typesetting #20

Merged
merged 1 commit into from
Feb 15, 2024
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
29 changes: 26 additions & 3 deletions Sources/CodeEditTextView/TextLine/Typesetter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,18 @@ final class Typesetter {
startingOffset: Int,
constrainingWidth: CGFloat
) -> Int {
let breakIndex = startingOffset + CTTypesetterSuggestClusterBreak(typesetter, startingOffset, constrainingWidth)
if breakIndex >= string.length || (breakIndex - 1 > 0 && ensureCharacterCanBreakLine(at: breakIndex - 1)) {
var breakIndex = startingOffset + CTTypesetterSuggestClusterBreak(typesetter, startingOffset, constrainingWidth)

let isBreakAtEndOfString = breakIndex >= string.length

let isNextCharacterCarriageReturn = checkIfLineBreakOnCRLF(breakIndex)
if isNextCharacterCarriageReturn {
breakIndex += 1
}

let canLastCharacterBreak = (breakIndex - 1 > 0 && ensureCharacterCanBreakLine(at: breakIndex - 1))

if isBreakAtEndOfString || canLastCharacterBreak {
// Breaking either at the end of the string, or on a whitespace.
return breakIndex
} else if breakIndex - 1 > 0 {
Expand All @@ -208,7 +218,20 @@ final class Typesetter {
let set = CharacterSet(
charactersIn: string.attributedSubstring(from: NSRange(location: index, length: 1)).string
)
return set.isSubset(of: .whitespaces) || set.isSubset(of: .punctuationCharacters)
return set.isSubset(of: .whitespacesAndNewlines) || set.isSubset(of: .punctuationCharacters)
}

/// Check if the break index is on a CRLF (`\r\n`) character, indicating a valid break position.
/// - Parameter breakIndex: The index to check in the string.
/// - Returns: True, if the break index lies after the `\n` character in a `\r\n` sequence.
private func checkIfLineBreakOnCRLF(_ breakIndex: Int) -> Bool {
guard breakIndex - 1 > 0 && breakIndex + 1 <= string.length else {
return false
}
let substringRange = NSRange(location: breakIndex - 1, length: 2)
let substring = string.attributedSubstring(from: substringRange).string

return substring == LineEnding.carriageReturnLineFeed.rawValue
}

deinit {
Expand Down
74 changes: 74 additions & 0 deletions Tests/CodeEditTextViewTests/TypesetterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import XCTest
@testable import CodeEditTextView

// swiftlint:disable all

class TypesetterTests: XCTestCase {
let limitedLineWidthDisplayData = TextLine.DisplayData(maxWidth: 150, lineHeightMultiplier: 1.0, estimatedLineHeight: 20.0)
let unlimitedLineWidthDisplayData = TextLine.DisplayData(maxWidth: .infinity, lineHeightMultiplier: 1.0, estimatedLineHeight: 20.0)

func test_LineFeedBreak() {
let typesetter = Typesetter()
typesetter.typeset(
NSAttributedString(string: "testline\n"),
displayData: unlimitedLineWidthDisplayData,
breakStrategy: .word,
markedRanges: nil
)

XCTAssertEqual(typesetter.lineFragments.count, 1, "Typesetter typeset incorrect number of lines.")

typesetter.typeset(
NSAttributedString(string: "testline\n"),
displayData: unlimitedLineWidthDisplayData,
breakStrategy: .character,
markedRanges: nil
)

XCTAssertEqual(typesetter.lineFragments.count, 1, "Typesetter typeset incorrect number of lines.")
}

func test_carriageReturnBreak() {
let typesetter = Typesetter()
typesetter.typeset(
NSAttributedString(string: "testline\r"),
displayData: unlimitedLineWidthDisplayData,
breakStrategy: .word,
markedRanges: nil
)

XCTAssertEqual(typesetter.lineFragments.count, 1, "Typesetter typeset incorrect number of lines.")

typesetter.typeset(
NSAttributedString(string: "testline\r"),
displayData: unlimitedLineWidthDisplayData,
breakStrategy: .character,
markedRanges: nil
)

XCTAssertEqual(typesetter.lineFragments.count, 1, "Typesetter typeset incorrect number of lines.")
}

func test_carriageReturnLineFeedBreak() {
let typesetter = Typesetter()
typesetter.typeset(
NSAttributedString(string: "testline\r\n"),
displayData: unlimitedLineWidthDisplayData,
breakStrategy: .word,
markedRanges: nil
)

XCTAssertEqual(typesetter.lineFragments.count, 1, "Typesetter typeset incorrect number of lines.")

typesetter.typeset(
NSAttributedString(string: "testline\r\n"),
displayData: unlimitedLineWidthDisplayData,
breakStrategy: .character,
markedRanges: nil
)

XCTAssertEqual(typesetter.lineFragments.count, 1, "Typesetter typeset incorrect number of lines.")
}
}

// swiftlint:enable all
Loading