Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing shorthand arc #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Graphics/Svg/PathParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ num = realToFrac <$> (skipSpace *> plusMinus <* skipSpace)
shorthand = process' <$> (string "." *> many1 digit)
process' = either (const 0) id . parseOnly doubleNumber . T.pack . (++) "0."

bool :: Parser Bool
bool = ("1" ==) <$> (skipSpace *> zeroOrOne <* skipSpace)
where zeroOrOne = string "0" <|> string "1"


viewBoxParser :: Parser (Double, Double, Double, Double)
viewBoxParser = (,,,)
<$> iParse <*> iParse <*> iParse <*> iParse
Expand Down Expand Up @@ -102,11 +107,12 @@ command = (MoveTo OriginAbsolute <$ string "M" <*> pointList)
manyComma a = a `sepBy1` commaWsp

numComma = num <* commaWsp
boolComma = bool <* commaWsp
ellipticalArgs = (,,,,,) <$> numComma
<*> numComma
<*> numComma
<*> (fmap (/= 0) numComma)
<*> (fmap (/= 0) numComma)
<*> boolComma
<*> boolComma
<*> point

serializePoint :: RPoint -> String
Expand Down
2 changes: 1 addition & 1 deletion svg-tree.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: svg-tree
version: 0.6.2.4
version: 0.6.2.4.999
synopsis: SVG file loader and serializer
description:
svg-tree provides types representing a SVG document,
Expand Down
17 changes: 14 additions & 3 deletions test/PathParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ import Test.Hspec

spec :: Spec
spec = do
describe "num" $ do
describe "num" $
it "support shorthand number" $ do
parseOnly command d `shouldBe` Right p
where
let
d = "M-.10 .10z"
p = MoveTo OriginAbsolute [V2 (-0.1) 0.10]
parseOnly command d `shouldBe` Right p
describe "arc" $ do
it "support arc" $ do
let
d = "a.5 .5 .5 0 0 -.5 .5"
p = EllipticalArc OriginRelative [(0.5, 0.5, 0.5, False, False, V2 (-0.5) 0.5)]
parseOnly command d `shouldBe` Right p
it "support shorthand arc" $ do
let
d = "a.5 .5 .5 00-.5 .5"
p = EllipticalArc OriginRelative [(0.5, 0.5, 0.5, False, False, V2 (-0.5) 0.5)]
parseOnly command d `shouldBe` Right p