@@ -9,7 +9,7 @@ import Data.Abc (Bar, Music(..), KeySignature, NoteDuration, RestOrNote)
9
9
import Data.Abc.KeySignature (keySet )
10
10
import Data.Either (Either (..))
11
11
import Data.Foldable (foldl , foldMap )
12
- import Data.List (length )
12
+ import Data.List (List , length )
13
13
import Data.List.NonEmpty (head , toUnfoldable ) as Nel
14
14
import Data.Maybe (Maybe , maybe )
15
15
import Data.Int (round , toNumber )
@@ -32,6 +32,7 @@ instance tickableMonoidCtx :: Monoid TickableContext where
32
32
mempty = TickableContext 0 (fromInt 0 )
33
33
34
34
-- | get the tickable content of any Music item
35
+ -- | Grace notes don't count here for the VexFlow API
35
36
getTickableContext :: Music -> TickableContext
36
37
getTickableContext m =
37
38
case m of
@@ -61,7 +62,6 @@ getTickableContext m =
61
62
_ ->
62
63
mempty
63
64
64
-
65
65
getRorNsDuration :: Array RestOrNote -> NoteDuration
66
66
getRorNsDuration rOrNs =
67
67
let
@@ -76,15 +76,21 @@ getRorNsDuration rOrNs =
76
76
estimateBarWidth :: Boolean -> Boolean -> Maybe KeySignature -> Bar -> Int
77
77
estimateBarWidth hasClef hasTimeSig maybeKeySig abcBar =
78
78
let
79
- (TickableContext noteCount duration) = foldMap getTickableContext abcBar.music
79
+ (TickableContext noteCount duration) =
80
+ foldMap getTickableContext abcBar.music
80
81
clefCount =
81
82
if hasClef then 1.0 else 0.0
82
83
timeSigCount =
83
84
if hasTimeSig then 1.0 else 0.0
84
85
keySigCount =
85
86
maybe 0.0 keySignatureWidth maybeKeySig
87
+ graceCount = countGraceNoteGroups abcBar.music
86
88
in
87
- round $ (clefCount + timeSigCount + keySigCount + (tickableCountWidth noteCount)) * pixelsPerItem
89
+ round $ ( clefCount
90
+ + timeSigCount
91
+ + keySigCount
92
+ + (tickableCountWidth noteCount)
93
+ + (0.5 * graceCount)) * pixelsPerItem
88
94
89
95
-- heuristic to decide how much width to dedicate to a key signature
90
96
-- by counting the number of sharps and flats
@@ -107,3 +113,16 @@ tickableCountWidth n =
107
113
1 -> 1.5 -- just 1.0 is too small
108
114
2 -> 2.5 -- just 2.0 is too small
109
115
_ -> toNumber n
116
+
117
+ -- count the grace note groups, allocating a unit to each
118
+ countGraceNoteGroups :: List Music -> Number
119
+ countGraceNoteGroups ms =
120
+ let
121
+ sumGrace acc m =
122
+ case m of
123
+ GraceNote _ _ ->
124
+ acc + 1.0
125
+ _ ->
126
+ acc
127
+ in
128
+ foldl sumGrace 0.0 ms
0 commit comments