module Graphics.SVGFonts.CharReference (charsFromFullName, characterStrings) where
import Control.Applicative ((<|>), many)
import Data.Attoparsec.Text
import qualified Data.Text as T
import Data.List (sortBy)
charRef :: Parser Int
charRef
= do
_ <- try (string (T.pack "&#x"))
d <- hexadecimal
_ <- char ';'
return d
<|> do
_ <- try (string (T.pack "&#"))
d <- decimal
_ <- char ';'
return d
<|> do
c <- anyChar
return (fromEnum c)
<?> "character reference"
charRefs :: Parser [Int]
charRefs = do l <- many1 charRef
return l
fromCharRefs :: T.Text -> [Int]
fromCharRefs str
= case (parseOnly charRefs str) of
Right x -> x
Left _ -> []
charsFromFullName :: String -> String
charsFromFullName str = map toEnum ( fromCharRefs (T.pack str) )
characterStrings :: String -> [String] -> [T.Text]
characterStrings str ligs | null ligs = map ((T.pack).(\x->[x])) str
| otherwise = case parseOnly myParser (T.pack str)
of Right x -> x
Left _ -> []
where myParser = many (try ligatures <|> charToText)
ligatures = buildChain $ sortBy
(\x y -> compare (length y) (length x)) $ ligs
buildChain [] = string (T.pack "")
buildChain [x] = parseLigature x
buildChain (x:xs) = try (parseLigature x) <|> buildChain xs
parseLigature x = string (T.pack x)
charToText = do c <- anyChar
return (T.singleton c)