diagrams / SVGFonts

Fonts from the SVG-Font format
http://hackage.haskell.org/package/SVGFonts
Other
20 stars 12 forks source link

"Prelude.read: no parse" when accessing ascent and descent in FontData #2

Closed jbracker closed 11 years ago

jbracker commented 11 years ago

This code leads to a Prelude.read: no parse:

import qualified Graphics.SVGFonts.ReadFont as F

main = do
  let font@(fontData,_) = F.lin
      (_,_,_,_,_,(_,_,weight,_,_,panose,ascent,descent,xHeight,capHeight,stemh,stemv,_)) = fontData
  putStrLn $ "ascent:"++ascent++"\n"

as does this code:

import qualified Graphics.SVGFonts.ReadFont as F

main = do
  let font@(fontData,_) = F.lin
      (_,_,_,_,_,(_,_,weight,_,_,panose,ascent,descent,xHeight,capHeight,stemh,stemv,_)) = fontData
  putStrLn $ "descent:"++descent++"\n"
jbracker commented 11 years ago

Ok, I looked at the place where the font file is read and I think I have spotted the problem here: https://github.com/diagrams/SVGFonts/blob/master/src/Graphics/SVGFonts/ReadFont.hs#L169

ascent    = read $ fromMaybe "" $ findAttr (unqual "ascent") fontface
descent   = read $ fromMaybe "" $ findAttr (unqual "descent") fontface

I think the problem is that ascent and descent are passed on as Strings, though the intention here is to parse a number. Parsing a String fails, because the quotes are missing. I checked all of the provided fonts and each has the ascent and descent attribute set. So I would suggest to change that code like this:

ascent    = fromJust $ fmap read $ findAttr (unqual "ascent") fontface
descent   = fromJust $ fmap read $ findAttr (unqual "descent") fontface

Proper error handling is missing either way...

I think the same error will occur with the values for xHeight, capHeight, stemh, stemv, fontWeight and unitsPerEm.

I will open a pull request with the corrected code soon.