2021-04-28 06:29:21 -04:00
|
|
|
import Prelude hiding (getContents, readFile, writeFile, putStrLn)
|
2020-11-10 09:39:45 -05:00
|
|
|
import Svg
|
2021-04-28 06:29:21 -04:00
|
|
|
import Glyphs (doGlyphs, lineHeight')
|
|
|
|
import Split
|
2020-11-10 09:39:45 -05:00
|
|
|
import Options.Applicative
|
|
|
|
import Data.Functor
|
2021-04-28 06:29:21 -04:00
|
|
|
import Data.Text.IO (readFile, getContents)
|
|
|
|
import Data.Text.Lazy.IO (writeFile, putStrLn)
|
2020-11-10 09:39:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
data Options =
|
2021-04-28 06:29:21 -04:00
|
|
|
Opts {
|
|
|
|
width, size, stroke :: {-# UNPACK #-} !Double,
|
2021-04-29 05:55:54 -04:00
|
|
|
inFile, outFile :: Maybe FilePath,
|
|
|
|
text :: Maybe Text,
|
|
|
|
color :: Text
|
2021-04-28 06:29:21 -04:00
|
|
|
}
|
2020-11-10 09:39:45 -05:00
|
|
|
deriving Show
|
|
|
|
|
|
|
|
options :: IO Options
|
|
|
|
options = execParser desc where
|
|
|
|
desc = info (opts <**> helper) $
|
|
|
|
fullDesc <> header "render lántas text as svg"
|
|
|
|
opts =
|
2021-04-28 06:29:21 -04:00
|
|
|
Opts <$> dimOpt 'W' "width" 1000
|
|
|
|
<*> (dimOpt' 'S' "size" "text size" 60 <&> (/ lineHeight'))
|
|
|
|
<*> dimOpt' 'K' "stroke" "line thickness" 2
|
|
|
|
<*> filePath 'i' "input"
|
|
|
|
<*> filePath 'o' "output"
|
|
|
|
<*> text
|
2021-04-29 05:55:54 -04:00
|
|
|
<*> color
|
2020-11-10 09:39:45 -05:00
|
|
|
dimOpt s l d = dimOpt' s l l d
|
2021-04-28 06:29:21 -04:00
|
|
|
dimOpt' s l n d = option auto $ mconcat
|
|
|
|
[short s, long l, help $ n <> " in pixels", metavar "SIZE", value d]
|
|
|
|
filePath s n = optional $ option str $ mconcat
|
|
|
|
[short s, long n, help $ n <> " file", metavar "FILE"]
|
|
|
|
text = optional $ option str $ mconcat
|
|
|
|
[short 't', long "text", help $ "use given text instead of a file",
|
|
|
|
metavar "TEXT"]
|
2021-04-29 05:55:54 -04:00
|
|
|
color = option str $ mconcat
|
|
|
|
[short 'C', long "color", help $ "set stroke color (any css syntax)",
|
|
|
|
metavar "COLOR", value "black"]
|
2020-11-10 09:39:45 -05:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
Opts {..} <- options
|
2021-04-28 06:29:21 -04:00
|
|
|
txt <- split <$> if
|
|
|
|
| Just t <- text -> pure t
|
|
|
|
| Just "-" <- inFile -> getContents
|
|
|
|
| Just i <- inFile -> readFile i
|
|
|
|
| otherwise -> fail "no input given"
|
|
|
|
let res = prettyText $ doGlyphs txt (E {..})
|
|
|
|
case outFile of
|
|
|
|
Just o | o /= "-" -> writeFile o res
|
|
|
|
_ -> putStrLn res
|