{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
module Text.TeXMath.Shared
( getMMLType
, getTextType
, getLaTeXTextCommand
, getScalerCommand
, getScalerValue
, scalers
, getSpaceWidth
, getSpaceChars
, getDiacriticalCommand
, diacriticals
, getOperator
, readLength
, fixTree
, isEmpty
, empty
, handleDownup
) where
import Text.TeXMath.Types
import Text.TeXMath.TeX
import qualified Data.Map as M
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Maybe (fromMaybe)
import Data.Ratio ((%))
import Data.List (sort)
import Control.Monad (guard)
import Text.Parsec (Parsec, parse, getInput, digit, char, many1, option)
import Data.Generics (everywhere, mkT)
removeNesting :: Exp -> Exp
removeNesting :: Exp -> Exp
removeNesting (EDelimited Text
o Text
c [Right (EDelimited Text
"" Text
"" [InEDelimited]
xs)]) = Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
o Text
c [InEDelimited]
xs
removeNesting (EDelimited Text
"" Text
"" [InEDelimited
x]) = (Text -> Exp) -> (Exp -> Exp) -> InEDelimited -> Exp
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Ord) Exp -> Exp
forall a. a -> a
id InEDelimited
x
removeNesting (EGrouped [Exp
x]) = Exp
x
removeNesting Exp
x = Exp
x
removeEmpty :: [Exp] -> [Exp]
removeEmpty :: [Exp] -> [Exp]
removeEmpty [Exp]
xs = (Exp -> Bool) -> [Exp] -> [Exp]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Exp -> Bool) -> Exp -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exp -> Bool
isEmpty) [Exp]
xs
empty :: Exp
empty :: Exp
empty = [Exp] -> Exp
EGrouped []
isEmpty :: Exp -> Bool
isEmpty :: Exp -> Bool
isEmpty (EGrouped []) = Bool
True
isEmpty Exp
_ = Bool
False
fixTree :: Exp -> Exp
fixTree :: Exp -> Exp
fixTree = (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((Exp -> Exp) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT Exp -> Exp
removeNesting) (Exp -> Exp) -> (Exp -> Exp) -> Exp -> Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere (([Exp] -> [Exp]) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT [Exp] -> [Exp]
removeEmpty)
getMMLType :: TextType -> T.Text
getMMLType :: TextType -> Text
getMMLType TextType
t = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"normal" ((Text, Text) -> Text
forall a b. (a, b) -> a
fst ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TextType -> Map TextType (Text, Text) -> Maybe (Text, Text)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup TextType
t Map TextType (Text, Text)
textTypesMap)
getLaTeXTextCommand :: Env -> TextType -> T.Text
getLaTeXTextCommand :: Env -> TextType -> Text
getLaTeXTextCommand Env
e TextType
t =
let textCmd :: Text
textCmd = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"\\mathrm"
((Text, Text) -> Text
forall a b. (a, b) -> b
snd ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TextType -> Map TextType (Text, Text) -> Maybe (Text, Text)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup TextType
t Map TextType (Text, Text)
textTypesMap) in
if Text -> Env -> Bool
textPackage Text
textCmd Env
e
then Text
textCmd
else Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"\\mathrm" (Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
textCmd Map Text Text
alts)
getTextType :: T.Text -> TextType
getTextType :: Text -> TextType
getTextType Text
s = TextType -> Maybe TextType -> TextType
forall a. a -> Maybe a -> a
fromMaybe TextType
TextNormal (Text -> Map Text TextType -> Maybe TextType
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
s Map Text TextType
revTextTypesMap)
getScalerCommand :: Rational -> Maybe T.Text
getScalerCommand :: Rational -> Maybe Text
getScalerCommand Rational
width =
case [(Rational, Text)] -> [(Rational, Text)]
forall a. Ord a => [a] -> [a]
sort [ (Rational
w, Text
cmd) | (Text
cmd, Rational
w) <- [(Text, Rational)]
scalers, Rational
w Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
>= Rational
width ] of
((Rational
_,Text
cmd):[(Rational, Text)]
_) -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
cmd
[(Rational, Text)]
_ -> Maybe Text
forall a. Maybe a
Nothing
getScalerValue :: T.Text -> Maybe Rational
getScalerValue :: Text -> Maybe Rational
getScalerValue Text
command = Text -> [(Text, Rational)] -> Maybe Rational
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
command [(Text, Rational)]
scalers
getDiacriticalCommand :: Position -> T.Text -> Maybe T.Text
getDiacriticalCommand :: Position -> Text -> Maybe Text
getDiacriticalCommand Position
pos Text
symbol = do
Text
command <- Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
symbol Map Text Text
diaMap
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b