Skip to content

Commit 081b78e

Browse files
amend scaleConfigToDesiredWidth function
to scale to only 98% of the desired width. This should prevent margin errors when we eventually render the final score where there would be the possibilty otherwise of slightly overextending the width because of rounding issues etc.
1 parent 835bcbf commit 081b78e

File tree

3 files changed

+24
-53
lines changed

3 files changed

+24
-53
lines changed

examples/scaling/src/Main.purs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import VexFlow.Types (Config, RenderingError, defaultConfig)
99
import Data.Abc.Parser (parse)
1010
import Data.Abc (AbcTune)
1111
import Examples.Scaling.Texts (arepolskan, augustsson, cig, eklund3, ewa, fastan, smalandPolska,
12-
chordSymbolExample, gustavPersson, voltaContinuationSample,
13-
keyChangeSample, meterChangeSample,
14-
continuationSample, emptyBarSample)
12+
gustavPersson, bakmes)
1513

1614
config :: Config
1715
config =
@@ -23,11 +21,11 @@ config =
2321

2422
main :: Effect (Maybe RenderingError)
2523
main =
26-
case (parse cig) of
24+
case (parse bakmes) of
2725
Right abcTune -> do
2826
renderer <- initialiseCanvas config
2927
let
30-
desiredWidth = 500
28+
desiredWidth = 580
3129
renderFinalTuneAtWidth config desiredWidth renderer abcTune
3230
_ ->
3331
pure $ Just "ABC failed to parse"

examples/scaling/src/Texts.purs

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -121,48 +121,15 @@ arepolskan =
121121
<> "|: g | f2a g3 d2B | c2d ege dcB | A2G FG2 A2B | cdc BAG A2F |\r\n"
122122
<> "D2g fg2 d2B | c2d ege dcB | A2G FG2 ABc |1 cB2 G3-G2 :|2 cB2 G6 |\r\n"
123123

124-
-- example with chord symbols
125-
chordSymbolExample :: String
126-
chordSymbolExample =
127-
"X:1\r\n"
128-
<> "T:Polska från Fjällnäs\r\n"
129-
<> "R:Polska\r\n"
130-
<> "L:1/8\r\n"
131-
<> "M:3/4\r\n"
132-
<> "K:G\r\n"
133-
<> " \"G\"G2G>B \"D7\"A>F | \"G\"G2B>dg2 | \"Am\"a2a>g \"D7\"fe/f/ |1 \"G\"g2b>g \"D7\"d>B :|2 \"G\"g2g4 |]\r\n"
134-
<> "|: \"G\"d2B>d g>f | \"C\"e2e>dc2 | \"D7\"a2a>g fe/f/ |1 \"G\"g2b>g \"D7\"d>B :|2 \"G\"g2g4 |]\r\n"
135-
136-
137-
keyChangeSample :: String
138-
keyChangeSample =
139-
"K: D\r\n"
140-
<> "M: 4/4\r\n"
141-
<> "| A B c |[K: A] F |\r\n"
142-
<> "| f g a |\r\n"
143-
144-
meterChangeSample :: String
145-
meterChangeSample =
146-
"K: D\r\n"
147-
<> "M: 4/4\r\n"
148-
<> "| A B c |\r\n"
124+
bakmes :: String
125+
bakmes =
126+
"X: 1\r\n"
127+
<> "T: Bakmes av Åker Erland Jonsson\r\n"
128+
<> "R: polska\r\n"
149129
<> "M: 3/4\r\n"
150-
<> "| f g a |\r\n"
151-
152-
continuationSample :: String
153-
continuationSample =
154-
"| ABc |\\\r\n| def |\r\n"
155-
156-
emptyBarSample :: String
157-
emptyBarSample =
158-
"| ABc | | def |\r\n"
159-
160-
voltaContinuationSample :: String
161-
voltaContinuationSample =
162-
"X:1\r\n"
163-
<> "T: volta continued across staves\r\n"
164-
<> "M: 3/4\r\n"
165-
<> "L: 1/8\r\n"
166-
<> "K: D\r\n"
167-
<> "| AB cd ef |1 bb cc dd :|2 dd cc bb | AA BB cc |\r\n"
168-
<> "| dd ee ff || g6 |\r\n"
130+
<> "L: 1/8\r\n"
131+
<> "K:Gmin\r\n"
132+
<> "|: D | G>G-GA Bc | d>d-dc Ac | B>B-BA G^F | G2 D3 D | \r\n"
133+
<> "G>G-GA Bc | d>d-dc Ac | B>B-BA G^F | G2 G3 :|\r\n"
134+
<> "|: d>d-d=e fg | d>d-dc Ac | B>B-BA Bc | (3d2c2B2 AB/2A/2 |\r\n"
135+
<> "G>G-GA Bc | d2 B4 | (3ABc A^FDF | G2 G4 :|\r\n"

src/VexFlow/Score.purs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Data.Maybe (Maybe(..), fromMaybe, maybe)
2727
import Data.String (length) as String
2828
import Data.Traversable (traverse_)
2929
import Effect (Effect)
30-
import Prelude ((<>), (*), (==), (/), (/=), (<=), (&&), ($), (+), (>), Unit, bind, discard, not, pure, when)
30+
import Prelude ((<>), (*), (==), (/), (/=), (<=), (&&), ($), (+), (>), Unit, bind, discard, not, pure, show, when)
3131
import VexFlow.Abc.Alignment (centeredTitleXPos, justifiedScoreConfig, rightJustifiedOriginXPos)
3232
import VexFlow.Abc.Alignment (rightJustify) as Exports
3333
import VexFlow.Abc.TranslateStateful (runTuneBody)
@@ -89,7 +89,11 @@ renderFinalTune config renderer abcTune =
8989
in
9090
-- don't render if the tune width exceded the requested canvas width
9191
if (config'.width > config.width) then
92-
pure $ Just "Canvas width exceded"
92+
pure $ Just ("Canvas width exceded actual: "
93+
<> show config'.width
94+
<> " requested: "
95+
<> show config.width
96+
)
9397
else do
9498
_ <- resizeCanvas renderer config'
9599
case config'.titling of
@@ -144,9 +148,11 @@ scaleConfigToDesiredWidth :: AbcTune -> Config -> Int -> Config
144148
scaleConfigToDesiredWidth abcTune config desiredWidth =
145149
let
146150
justifiedConfig = justifiedScoreConfig (createScore config abcTune) config
147-
scaleFactor = (toNumber desiredWidth) / (toNumber justifiedConfig.width)
151+
-- fit the score to within 98% of the desired width to avoid margin errors
152+
scaleFactor = (0.98 * toNumber desiredWidth) / (toNumber justifiedConfig.width)
148153
newScale = justifiedConfig.scale * scaleFactor
149-
newWidth = floor $ (toNumber justifiedConfig.width) * scaleFactor
154+
newWidth = desiredWidth
155+
-- floor $ (toNumber justifiedConfig.width) * scaleFactor
150156
newHeight = floor $ (toNumber justifiedConfig.width) * scaleFactor
151157
in
152158
justifiedConfig

0 commit comments

Comments
 (0)