-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix chords in {textblock} environment
- Loading branch information
Showing
13 changed files
with
111 additions
and
85 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
62 changes: 0 additions & 62 deletions
62
Chord Provider/PDFBuild/Core/Elements/PDFBuild+TextblockSection.swift
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
Chord Provider/PDFBuild/SongElements/PDFBuild+TextblockSection.swift
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,78 @@ | ||
// | ||
// PDFBuild+TextblockSection.swift | ||
// Chord Provider | ||
// | ||
// © 2025 Nick Berendsen | ||
// | ||
|
||
import AppKit | ||
|
||
extension PDFBuild { | ||
|
||
// MARK: A PDF **textblock section** element | ||
|
||
/// A PDF **textblock section** element | ||
class TextblockSection: PDFElement { | ||
|
||
/// The section with textblock | ||
let section: Song.Section | ||
/// All the chords of the song | ||
let chords: [ChordDefinition] | ||
|
||
/// Init the **textblock section** element | ||
/// - Parameters: | ||
/// - section: The section with textblock | ||
/// - chords: All the chords of the song | ||
init(_ section: Song.Section, chords: [ChordDefinition]) { | ||
self.section = section | ||
self.chords = chords | ||
} | ||
|
||
/// Draw the **textblock section** element | ||
/// - Parameters: | ||
/// - rect: The available rectangle | ||
/// - calculationOnly: Bool if only the Bounding Rect should be calculated | ||
/// - pageRect: The page size of the PDF document | ||
func draw(rect: inout CGRect, calculationOnly: Bool, pageRect: CGRect) { | ||
for line in section.lines { | ||
if let parts = line.parts { | ||
let text = NSMutableAttributedString() | ||
for part in parts { | ||
if let chord = chords.first(where: { $0.id == part.chord }) { | ||
text.append( | ||
NSAttributedString( | ||
/// Add a space behind the chord-name so two chords will never 'stick' together | ||
string: "\(chord.display)", | ||
attributes: .partChord(chord.status) | ||
) | ||
) | ||
} | ||
text.append(NSAttributedString( | ||
string: "\(part.text)", | ||
attributes: .textblockLine) | ||
) | ||
} | ||
let textBounds = text.boundingRect(with: rect.size, options: .usesLineFragmentOrigin) | ||
if !calculationOnly { | ||
text.draw(with: rect, options: textDrawingOptions, context: nil) | ||
} | ||
let height = textBounds.height + 2 * textPadding | ||
rect.origin.y += height | ||
rect.size.height -= height | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension PDFStringAttribute { | ||
|
||
// MARK: Textblock string styling | ||
|
||
/// String attributes for a textblock line | ||
static var textblockLine: PDFStringAttribute { | ||
[ | ||
.foregroundColor: NSColor.gray | ||
] | ||
} | ||
} |
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
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
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
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
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