module Graphics.PDF.Typesetting.WritingSystem(
WritingSystem(..)
, mapToSpecialGlyphs
) where
import qualified Data.Text as T
import Graphics.PDF.LowLevel.Types
import qualified Text.Hyphenation as H
import Data.List(intersperse)
import Data.Char
import Data.List(unfoldr)
data WritingSystem = Latin H.Hyphenator
| UnknownWritingSystem
myWords' :: T.Text -> Maybe (T.Text, T.Text)
myWords' l | T.null l = Nothing
| otherwise = if T.null h then Just (h', t') else Just (T.singleton ' ', t)
where
(h, t) = T.span isSpace l
(h', t') = T.span (not . isSpace) l
myWords :: T.Text -> [T.Text]
myWords l = concatMap onlyWord . unfoldr myWords' $ l
where
onlyWord s =
let (w,p) = T.span isAlpha s in
case (T.null w,T.null p) of
(True,True) -> []
(False,True) -> [w]
(True,False) -> [p]
(False,False) -> [w,p]
addHyphens :: H.Hyphenator -> T.Text -> T.Text
addHyphens hn f =
T.concat . map (T.concat . intersperse (T.pack "/-") . hyphenate) . myWords $ f
where
hyphenate = fmap T.pack . H.hyphenate hn . T.unpack
mapToSpecialGlyphs :: WritingSystem -> T.Text -> [SpecialChar]
mapToSpecialGlyphs UnknownWritingSystem theText =
let getBreakingGlyphs (' ':l) = NormalSpace:getBreakingGlyphs l
getBreakingGlyphs (a:l) = NormalChar a:getBreakingGlyphs l
getBreakingGlyphs [] = []
in getBreakingGlyphs (T.unpack theText)
mapToSpecialGlyphs (Latin hn) theText =
let getBreakingGlyphs [] = []
getBreakingGlyphs (a:'/':'-':d:l) = (NormalChar a):BreakingHyphen:getBreakingGlyphs (d:l)
getBreakingGlyphs (',':' ':l) = NormalChar ',':BiggerSpace:getBreakingGlyphs l
getBreakingGlyphs (';':' ':l) = NormalChar ';':BiggerSpace:getBreakingGlyphs l
getBreakingGlyphs ('.':' ':l) = NormalChar '.':BiggerSpace:getBreakingGlyphs l
getBreakingGlyphs (':':' ':l) = NormalChar ':':BiggerSpace:getBreakingGlyphs l
getBreakingGlyphs ('!':' ':l) = NormalChar '!':BiggerSpace:getBreakingGlyphs l
getBreakingGlyphs ('?':' ':l) = NormalChar '?':BiggerSpace:getBreakingGlyphs l
getBreakingGlyphs (' ':l) = NormalSpace:getBreakingGlyphs l
getBreakingGlyphs (a:l) = NormalChar a:getBreakingGlyphs l
in getBreakingGlyphs (T.unpack . addHyphens hn $ theText)