-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import CoreFoundation | ||
|
||
func knuthPlassLineBreakingAlgorithm( | ||
proposedBreadth: CGFloat, | ||
sizes: [Size], | ||
spacings: [CGFloat] | ||
) -> [Int] { | ||
let breaks = calculateOptimalBreaks( | ||
proposedBreadth: proposedBreadth, | ||
sizes: sizes, | ||
spacings: spacings | ||
) | ||
|
||
var breakpoints: [Int] = [] | ||
var i = sizes.count | ||
while let breakPoint = breaks[i] { | ||
breakpoints.insert(i, at: 0) | ||
i = breakPoint | ||
} | ||
breakpoints.insert(0, at: 0) | ||
return breakpoints | ||
} | ||
|
||
private func calculateOptimalBreaks( | ||
proposedBreadth: CGFloat, | ||
sizes: [Size], | ||
spacings: [CGFloat] | ||
) -> [Int?] { | ||
let count = sizes.count | ||
var costs: [CGFloat] = Array(repeating: .infinity, count: count + 1) | ||
var breaks: [Int?] = Array(repeating: nil, count: count + 1) | ||
|
||
costs[0] = 0 | ||
|
||
for end in 1 ... count { | ||
var totalBreadth: CGFloat = 0 | ||
for start in (0 ..< end).reversed() { | ||
let size = sizes[start].breadth | ||
let spacing = (end - start) == 1 ? 0 : spacings[start + 1] | ||
totalBreadth += size + spacing | ||
if totalBreadth > proposedBreadth { | ||
break | ||
} | ||
let remainingSpace = proposedBreadth - totalBreadth | ||
let bias = CGFloat(count - end) * 0.5 // Introduce a small bias to prefer breaks that fill earlier lines more | ||
let cost = costs[start] + remainingSpace * remainingSpace + bias | ||
if cost < costs[end] { | ||
costs[end] = cost | ||
breaks[end] = start | ||
} | ||
} | ||
} | ||
|
||
return breaks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extension Sequence { | ||
func adjacentPairs() -> some Sequence<(Element, Element)> { | ||
zip(self, self.dropFirst()) | ||
} | ||
|
||
func sum<Result: Numeric>(of block: (Element) -> Result) -> Result { | ||
reduce(into: .zero) { $0 += block($1) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters