-
Notifications
You must be signed in to change notification settings - Fork 30
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
3 changed files
with
82 additions
and
83 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 |
---|---|---|
@@ -1,57 +1,77 @@ | ||
import CoreFoundation | ||
|
||
@inlinable | ||
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 | ||
@usableFromInline | ||
protocol LineBreaking { | ||
@inlinable | ||
func wrapItemsToLines(sizes: [CGFloat], spacings: [CGFloat], in availableSpace: CGFloat) -> [Int] | ||
} | ||
|
||
@usableFromInline | ||
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 | ||
struct FlowLineBreaker: LineBreaking { | ||
@inlinable | ||
func wrapItemsToLines(sizes: [CGFloat], spacings: [CGFloat], in availableSpace: CGFloat) -> [Int] { | ||
var breakpoints: [Int] = [] | ||
var currentLineSize: CGFloat = 0 | ||
|
||
for (index, size) in sizes.enumerated() { | ||
let requiredSpace = spacings[index] + size | ||
if currentLineSize + requiredSpace > availableSpace { | ||
breakpoints.append(index) | ||
currentLineSize = size | ||
} else { | ||
currentLineSize += requiredSpace | ||
} | ||
} | ||
|
||
if breakpoints.first != 0 { | ||
breakpoints.insert(0, at: 0) | ||
} | ||
breakpoints.append(sizes.endIndex) | ||
|
||
return breakpoints | ||
} | ||
} | ||
|
||
@usableFromInline | ||
struct KnuthPlassLineBreaker: LineBreaking { | ||
@inlinable | ||
func wrapItemsToLines(sizes: [CGFloat], spacings: [CGFloat], in availableSpace: 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] | ||
let spacing = (end - start) == 1 ? 0 : spacings[start + 1] | ||
totalBreadth += size + spacing | ||
if totalBreadth > availableSpace { | ||
break | ||
} | ||
let remainingSpace = availableSpace - 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 | ||
if breaks.compactMap({ $0 }).isEmpty { | ||
return Array(0 ... sizes.endIndex) | ||
} | ||
|
||
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 | ||
} | ||
} |
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