fountainhead/lib/Graphics/Fountainhead.hs

37 lines
1.7 KiB
Haskell
Raw Normal View History

2023-12-27 16:19:21 +01:00
{- This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -}
2024-02-03 11:58:47 +01:00
-- | Convenience wrappers for working with font files.
2023-12-27 16:19:21 +01:00
module Graphics.Fountainhead
2024-02-03 11:58:47 +01:00
( dumpFontFile
, parseFontDirectoryFromFile
2023-12-27 16:19:21 +01:00
) where
import Data.ByteString (ByteString)
import Data.Void (Void)
import Graphics.Fountainhead.Dumper (dumpTable, dumpTables, DumpError(..))
import Graphics.Fountainhead.Parser (ParseErrorBundle, parseFontDirectory)
2023-12-27 16:19:21 +01:00
import Graphics.Fountainhead.TrueType (FontDirectory(..))
import Text.Megaparsec (State(..))
2024-02-03 11:58:47 +01:00
import System.IO (IOMode(..), withBinaryFile)
import Data.Bifunctor (Bifunctor(..))
import qualified Data.Text.Lazy.Builder as Text.Builder
import Graphics.Fountainhead.Compression (hDecompress)
2023-12-27 16:19:21 +01:00
-- | Does initial parsing of the font at the given path and returns the font
-- directory and parsing state that can be used to parse other tables in the
-- font.
2024-02-03 11:58:47 +01:00
parseFontDirectoryFromFile :: FilePath
2023-12-27 16:19:21 +01:00
-> IO (State ByteString Void, Either ParseErrorBundle FontDirectory)
parseFontDirectoryFromFile fontFile = withBinaryFile fontFile ReadMode
$ fmap (parseFontDirectory fontFile) . hDecompress
2024-02-03 11:58:47 +01:00
-- | Dumps the contents of the font in the file. If the table name is given,
-- dumps only this one table.
dumpFontFile :: FilePath -> Maybe String -> IO (Either DumpError Text.Builder.Builder)
dumpFontFile fontFile tableName = do
let dumpRequest = maybe dumpTables dumpTable tableName
2024-02-03 11:58:47 +01:00
(processedState, initialResult) <- parseFontDirectoryFromFile fontFile
pure $ first DumpParseError initialResult >>= dumpRequest processedState