Skip to content

Commit b2fc2a7

Browse files
Allocate some stave space to grace notes
(heuristic)
1 parent 0548b33 commit b2fc2a7

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/VexFlow/Abc/TickableContext.purs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Data.Abc (Bar, Music(..), KeySignature, NoteDuration, RestOrNote)
99
import Data.Abc.KeySignature (keySet)
1010
import Data.Either (Either(..))
1111
import Data.Foldable (foldl, foldMap)
12-
import Data.List (length)
12+
import Data.List (List, length)
1313
import Data.List.NonEmpty (head, toUnfoldable) as Nel
1414
import Data.Maybe (Maybe, maybe)
1515
import Data.Int (round, toNumber)
@@ -32,6 +32,7 @@ instance tickableMonoidCtx :: Monoid TickableContext where
3232
mempty = TickableContext 0 (fromInt 0)
3333

3434
-- | get the tickable content of any Music item
35+
-- | Grace notes don't count here for the VexFlow API
3536
getTickableContext :: Music -> TickableContext
3637
getTickableContext m =
3738
case m of
@@ -61,7 +62,6 @@ getTickableContext m =
6162
_ ->
6263
mempty
6364

64-
6565
getRorNsDuration :: Array RestOrNote -> NoteDuration
6666
getRorNsDuration rOrNs =
6767
let
@@ -76,15 +76,21 @@ getRorNsDuration rOrNs =
7676
estimateBarWidth :: Boolean -> Boolean -> Maybe KeySignature -> Bar -> Int
7777
estimateBarWidth hasClef hasTimeSig maybeKeySig abcBar =
7878
let
79-
(TickableContext noteCount duration) = foldMap getTickableContext abcBar.music
79+
(TickableContext noteCount duration) =
80+
foldMap getTickableContext abcBar.music
8081
clefCount =
8182
if hasClef then 1.0 else 0.0
8283
timeSigCount =
8384
if hasTimeSig then 1.0 else 0.0
8485
keySigCount =
8586
maybe 0.0 keySignatureWidth maybeKeySig
87+
graceCount = countGraceNoteGroups abcBar.music
8688
in
87-
round $ (clefCount + timeSigCount + keySigCount + (tickableCountWidth noteCount)) * pixelsPerItem
89+
round $ ( clefCount
90+
+ timeSigCount
91+
+ keySigCount
92+
+ (tickableCountWidth noteCount)
93+
+ (0.5 * graceCount)) * pixelsPerItem
8894

8995
-- heuristic to decide how much width to dedicate to a key signature
9096
-- by counting the number of sharps and flats
@@ -107,3 +113,16 @@ tickableCountWidth n =
107113
1 -> 1.5 -- just 1.0 is too small
108114
2 -> 2.5 -- just 2.0 is too small
109115
_ -> 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

Comments
 (0)