fountainhead/app/Main.hs

72 lines
2.4 KiB
Haskell
Raw Normal View History

2023-03-14 09:52:02 +01:00
module Main
( main
) where
2023-11-10 16:45:45 +01:00
import Control.Monad (foldM_)
import Data.Int (Int64)
2023-03-14 09:52:02 +01:00
import qualified Data.ByteString as ByteString
2023-11-10 16:45:45 +01:00
import qualified Data.ByteString.Char8 as Char8
2023-03-14 09:52:02 +01:00
import Data.ByteString (ByteString)
import qualified Text.Megaparsec as Megaparsec
import Data.Foldable (find)
2023-11-10 16:45:45 +01:00
import qualified Data.Text.Lazy.Builder as Text.Builder
import qualified Data.Text.Lazy.Builder.Int as Text.Builder
import qualified Data.Text.Lazy as Text.Lazy
import qualified Data.Text.Lazy.IO as Text.Lazy
import qualified Data.Text.Encoding as Text
import GHC.Records (HasField(..))
2023-11-11 10:57:43 +01:00
import Graphics.Fountainhead.Dumper (dumpTrueType)
2023-03-14 09:52:02 +01:00
-- TODO: kern table since format 1.
-- For details on subtable format see examples in TrueType reference.
import Graphics.Fountainhead.Parser
( fontDirectoryP
, os2TableP
, parseTable
, shortLocaTableP
)
2023-11-10 16:45:45 +01:00
import Graphics.Fountainhead.TrueType
( FontDirectory(..)
, OffsetSubtable(..)
, TableDirectory(..)
)
2023-03-14 09:52:02 +01:00
import System.Environment (getArgs)
2023-11-10 11:57:08 +01:00
import System.Exit (exitWith)
import GHC.IO.Exception (ExitCode(..))
2023-03-14 09:52:02 +01:00
2023-11-10 11:57:08 +01:00
fontMain :: FilePath -> IO ()
fontMain fontFile = do
2023-11-10 16:45:45 +01:00
putStrLn $ "Dumping File:" <> fontFile <> "\n\n"
2023-03-14 09:52:02 +01:00
ttfContents <- ByteString.readFile fontFile
let initialState = Megaparsec.State
{ stateInput = ttfContents
, stateOffset = 0
, statePosState = Megaparsec.PosState
{ pstateInput = ttfContents
, pstateOffset = 0
, pstateSourcePos = Megaparsec.initialPos fontFile
, pstateTabWidth = Megaparsec.defaultTabWidth
, pstateLinePrefix = ""
}
, stateParseErrors = []
}
2023-11-11 10:57:43 +01:00
(processedState, initialResult) = Megaparsec.runParser' fontDirectoryP initialState
case initialResult >>= dumpTrueType processedState of
(Right fontDump) -> Text.Lazy.putStrLn $ Text.Builder.toLazyText fontDump
Left e -> putStr (Megaparsec.errorBundlePretty e)
{-
2023-03-14 09:52:02 +01:00
let Just tableDirectory' = find (("OS/2" ==) . tag) $ tableDirectory directory
tableResult = parseTable tableDirectory' os2TableP processedState
case tableResult of
2023-11-10 16:45:45 +01:00
Right x -> print x -}
2023-03-14 09:52:02 +01:00
main :: IO ()
2023-11-10 11:57:08 +01:00
main = do
programArguments <- getArgs
case programArguments of
[fontFile] -> fontMain fontFile
_ -> putStrLn "The program expects exactly one argument, the font file path."
>> exitWith (ExitFailure 2)