Skip to content

Commit

Permalink
Fix chords in {textblock} environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Desbeers committed Jan 1, 2025
1 parent 260efe7 commit a9a55b2
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 85 deletions.
22 changes: 11 additions & 11 deletions Chord Provider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@
D71173812BD2FB0D0075325C /* PDFBuild+Section.swift */,
D706FD7B2BCECC9F005DEDF6 /* PDFBuild+Spacer.swift */,
D706FD7C2BCECC9F005DEDF6 /* PDFBuild+Text.swift */,
D7B319DE2BD564D9006A173A /* PDFBuild+TextblockSection.swift */,
D7AB50622BD958FF00AA797B /* PDFBuild+TOCItem.swift */,
);
path = Elements;
Expand Down Expand Up @@ -850,6 +849,7 @@
D70B72852BD50EC6003C89B4 /* PDFBuild+TabSection.swift */,
D70B72782BD4D6A4003C89B4 /* PDFBuild+TabSection+Line.swift */,
D70B72872BD5111D003C89B4 /* PDFBuild+GridSection.swift */,
D7B319DE2BD564D9006A173A /* PDFBuild+TextblockSection.swift */,
);
path = SongElements;
sourceTree = "<group>";
Expand Down Expand Up @@ -994,16 +994,16 @@
D77DF8BD2D2406BD000A0557 /* MainScene */ = {
isa = PBXGroup;
children = (
D7F13FAE2A6460B7002D875D /* EditorView */,
D7F13FB72A6460B7002D875D /* ChordsView.swift */,
D7F13FB52A6460B7002D875D /* MainView.swift */,
D7F13FB42A6460B7002D875D /* HeaderView.swift */,
D7F13FAD2A6460B7002D875D /* ToolbarView.swift */,
D7F13FAE2A6460B7002D875D /* EditorView */,
D7F13FB82A6460B7002D875D /* SongView.swift */,
D7F13FB72A6460B7002D875D /* ChordsView.swift */,
D7F13F882A6460B7002D875D /* RenderView.swift */,
D73A03E82AC85B6100D6CD0E /* RenderView+ChordView.swift */,
D7781B5E2D1062B000EC0CCF /* RenderView+ImageView.swift */,
D7F13FAD2A6460B7002D875D /* ToolbarView.swift */,
D76C24E22C3AA2AA00C93D82 /* PreviewPaneView.swift */,
D7F13FB42A6460B7002D875D /* HeaderView.swift */,
);
path = MainScene;
sourceTree = "<group>";
Expand Down Expand Up @@ -1860,7 +1860,7 @@
);
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 15.0;
MARKETING_VERSION = 6.0.0;
MARKETING_VERSION = 6.0;
PRODUCT_BUNDLE_IDENTIFIER = nl.desbeers.chordprovider.qlthumbnail;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
Expand Down Expand Up @@ -1894,7 +1894,7 @@
);
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 15.0;
MARKETING_VERSION = 6.0.0;
MARKETING_VERSION = 6.0;
PRODUCT_BUNDLE_IDENTIFIER = nl.desbeers.chordprovider.qlthumbnail;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
Expand Down Expand Up @@ -2056,7 +2056,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 15.0;
MARKETING_VERSION = 6.0.0;
MARKETING_VERSION = 6.0;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = nl.desbeers.chordprovider;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -2106,7 +2106,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 15.0;
MARKETING_VERSION = 6.0.0;
MARKETING_VERSION = 6.0;
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = nl.desbeers.chordprovider;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -2143,7 +2143,7 @@
"@executable_path/../../../../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 15.0;
MARKETING_VERSION = 6.0.0;
MARKETING_VERSION = 6.0;
PRODUCT_BUNDLE_IDENTIFIER = nl.desbeers.chordprovider.qlpreview;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
Expand Down Expand Up @@ -2174,7 +2174,7 @@
"@executable_path/../../../../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 15.0;
MARKETING_VERSION = 6.0.0;
MARKETING_VERSION = 6.0;
PRODUCT_BUNDLE_IDENTIFIER = nl.desbeers.chordprovider.qlpreview;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
Expand Down

This file was deleted.

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
]
}
}
2 changes: 1 addition & 1 deletion Chord Provider/PDFBuild/SongExport/SongExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ extension SongExport {
alignment: .right
),
labelDivider(section: section),
PDFBuild.TextblockSection(section)
PDFBuild.TextblockSection(section, chords: song.chords)
]
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

extension ChordsDatabaseView {

/// Show a chord diagram
/// - Parameter chord: The ``ChordDefinition``
/// - Returns: A `View` with the chord diagram
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

extension ChordsDatabaseView {

/// View the chords in a grid
var grid: some View {
VStack(spacing: 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

extension ChordsDatabaseView {

/// A `View` with options for the grid
var options: some View {
Grid(alignment: .leading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct ChordsDatabaseView: View {
.environment(appState)
.environment(chordsDatabaseState)
}

/// Filter the chords
func filterChords() {
var chords = chordsDatabaseState.allChords
Expand Down
12 changes: 9 additions & 3 deletions Chord Provider/Views/MainScene/RenderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct RenderView: View {
) {
sections
}
.padding(song.settings.display.scale * 20)
}
.frame(maxHeight: .infinity)
.font(.system(size: 14 * song.settings.display.scale))
Expand Down Expand Up @@ -329,9 +330,14 @@ extension RenderView {
VStack(alignment: getFlush(section.arguments)) {
ForEach(section.lines) { line in
if let parts = line.parts {
ForEach(parts) { part in
/// Init the text like this to enable markdown formatting
Text(.init(part.text.trimmingCharacters(in: .whitespacesAndNewlines)))
HStack(spacing: 0) {
ForEach(parts) { part in
if let chord = song.chords.first(where: { $0.id == part.chord }) {
ChordView(settings: song.settings, sectionID: section.id, partID: part.id, chord: chord)
}
/// Init the text like this to enable markdown formatting
Text(.init(part.text))
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Chord Provider/Views/MainScene/SongView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct SongView: View {
labelStyle: .inline
)
}
.padding(sceneState.settings.song.display.scale * 20)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
case .asColumns:
Expand All @@ -43,7 +44,6 @@ struct SongView: View {
)
}
}
.contentMargins(.all, sceneState.settings.song.display.scale * 20, for: .scrollContent)
.contentShape(Rectangle())
.scaleModifier
}
Expand Down
4 changes: 2 additions & 2 deletions Chord Provider/Views/SettingsScene/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ struct SettingsView: View {
}

extension SettingsView {

/// Check if the **ChordPro** cli is found in the $PATH
/// - Returns: True or false
func checkChordProCLI() async -> Bool {
Expand All @@ -237,7 +237,7 @@ extension SettingsView {


extension SettingsView {

/// Wrap the settings section
struct WrapSettingsSection: ViewModifier {
/// The title of the section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension WelcomeView {
}

extension WelcomeView {

/// A `View` with a single song
/// - Parameters:
/// - song: The song
Expand Down
4 changes: 4 additions & 0 deletions Resources/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ <h2>Enjoy!</h2>

<p>-- Nick</p>

<hr>

<p><small><strong>Chord Provider</strong> is &copy; 2025 Nick Berendsen.</small></p>
<p><small>The source code is released under the <strong>GPL3 licence</strong> and is available on <a href="https://github.com/Desbeers/Chord-Provider">GitHub</a>.</small></p>
</div>
</body>
</html>

0 comments on commit a9a55b2

Please sign in to comment.