module Graphics.PDF.Hyphenate.LowLevel (
HyphenationDatabase(..)
, mkExceptions
, mkPatterns
, mkCustomLanguage
)
where
import qualified Graphics.PDF.Data.Trie as T
import Graphics.PDF.Data.Trie(MapString)
import Data.Char(isDigit)
import Data.List(unfoldr)
data HyphenationDatabase = English (Maybe (MapString [Int]))
| CustomLanguage (MapString [Int]) (MapString [Int])
mkExceptions :: [String] -> T.MapString [Int]
mkExceptions = T.fromList . map createException
where
createException x = (removeHyphen x,exceptionPoints x)
mkPatterns :: [String] -> T.MapString [Int]
mkPatterns = T.fromList . map convertPattern
mkCustomLanguage :: [String]
-> [String]
-> HyphenationDatabase
mkCustomLanguage e p = CustomLanguage (mkExceptions e) (mkPatterns p)
isChar :: Char -> Bool
isChar = not . isDigit
fromDigit :: Char -> Int
fromDigit c = fromEnum c fromEnum '0'
toNumber :: Char -> Int
toNumber x = if isChar x then 0 else fromDigit x
simplify :: [Int] -> [Int]
simplify (a:b:c:l) | a /= 0 && b == 0 && c /= 0 = a:simplify (c:l)
| otherwise = a:simplify (b:c:l)
simplify a = a
split :: (Char -> Bool) -> String -> [Int]
split f = simplify . map toNumber . unfoldr (split' f)
split' :: (Char -> Bool) -> String -> Maybe (Char, String)
split' f l | null l = Nothing
| otherwise = if null h then Just (' ', drop 1 t) else Just (head h, t)
where (h, t) = span f l
convertPattern :: String -> (String,[Int])
convertPattern s =
let s' = filter isChar s
p = split isDigit s
in
(s',p)
removeHyphen :: String -> String
removeHyphen = filter ((/=) '-')
exceptionPoints :: String -> [Int]
exceptionPoints s = 0 : map onlyHyphen s
where
onlyHyphen '-' = 1
onlyHyphen _ = 0