Add an option for dumping a single table

This commit is contained in:
2024-02-06 12:14:07 +01:00
parent 3160ceab08
commit 23271d6f6c
6 changed files with 164 additions and 87 deletions

View File

@ -1,3 +1,4 @@
{-# LANGUAGE RecordWildCards #-}
module Main
( main
) where
@ -5,49 +6,49 @@ module Main
import qualified Data.Text.Lazy.Builder as Text.Builder
import qualified Data.Text.Lazy.IO as Text.Lazy
import Graphics.Fountainhead (dumpFontFile)
import System.Exit (exitWith)
import GHC.IO.Exception (ExitCode(..))
import Options.Applicative
( Parser
, ParserInfo(..)
( ParserInfo(..)
, (<**>)
, argument
, command
, execParser
, header
, help
, helper
, info
, long
, fullDesc
, metavar
, optional
, progDesc
, short
, str
, subparser
, strOption
)
data Operation
= Dump
| Afm
deriving (Eq, Show)
data Options = Options Operation FilePath
deriving (Eq, Show)
data Options = Options
{ tableName :: Maybe String
, fontFile :: FilePath
} deriving (Eq, Show)
operationOptions :: ParserInfo Options
operationOptions = info (options <**> helper) fullDesc
operationOptions = info (options <**> helper)
$ fullDesc
<> progDesc "Dumping the contents of a TrueType Font file."
<> header "fountainhead - font parser"
where
options = Options
<$> commands
<$> tableNameArgument
<*> argument str (metavar "FONTFILE")
commands = subparser
$ command "dump" (info (pure Dump) (progDesc "Dumping the contents of a TrueType Font file."))
<> command "afm" (info (pure Afm) (progDesc "Generating Adobe Font Metrics files for TrueType fonts."))
tableNameArgument = optional $ strOption
$ long "table"
<> short 't'
<> metavar "tablename"
<> help "Dump only the specified table. Otherwise dump all tables"
main :: IO ()
main = execParser operationOptions >>= handleArguments
where
handleArguments (Options Dump fontFile)
handleArguments Options{..}
= putStrLn ("Dumping File:" <> fontFile <> "\n\n")
>> dumpFontFile fontFile
>> dumpFontFile fontFile tableName
>>= either print (Text.Lazy.putStrLn . Text.Builder.toLazyText)
handleArguments (Options Afm _)
= putStrLn "The program expects exactly one argument, the font file path."
>> exitWith (ExitFailure 2)