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)
|
2024-02-06 12:14:07 +01:00
|
|
|
import Graphics.Fountainhead.Dumper (dumpTable, dumpTables, DumpError(..))
|
2024-02-07 10:40:00 +01:00
|
|
|
import Graphics.Fountainhead.Parser (ParseErrorBundle, parseFontDirectory)
|
2023-12-27 16:19:21 +01:00
|
|
|
import Graphics.Fountainhead.TrueType (FontDirectory(..))
|
2024-02-07 10:40:00 +01:00
|
|
|
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
|
|
|
|
2024-02-07 10:40:00 +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)
|
2024-02-07 10:40:00 +01:00
|
|
|
parseFontDirectoryFromFile fontFile = withBinaryFile fontFile ReadMode
|
|
|
|
$ fmap (parseFontDirectory fontFile) . hDecompress
|
2024-02-03 11:58:47 +01:00
|
|
|
|
2024-02-07 10:40:00 +01:00
|
|
|
-- | Dumps the contents of the font in the file. If the table name is given,
|
|
|
|
-- dumps only this one table.
|
2024-02-06 12:14:07 +01:00
|
|
|
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
|
2024-02-06 12:14:07 +01:00
|
|
|
pure $ first DumpParseError initialResult >>= dumpRequest processedState
|