fountainhead/lib/Graphics/Fountainhead.hs

50 lines
2.1 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(..))
2023-12-27 16:19:21 +01:00
import Graphics.Fountainhead.Parser (ParseErrorBundle, fontDirectoryP)
import Graphics.Fountainhead.TrueType (FontDirectory(..))
import qualified Text.Megaparsec as Megaparsec
import Text.Megaparsec (PosState(..), 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-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 withFontHandle
where
withFontHandle fontHandle = doParsing
2024-02-03 11:58:47 +01:00
<$> hDecompress fontHandle
2023-12-27 16:19:21 +01:00
doParsing ttfContents =
let initialState = Megaparsec.State
{ stateInput = ttfContents
, stateOffset = 0
, statePosState = Megaparsec.PosState
{ pstateInput = ttfContents
, pstateOffset = 0
, pstateSourcePos = Megaparsec.initialPos fontFile
, pstateTabWidth = Megaparsec.defaultTabWidth
, pstateLinePrefix = ""
}
, stateParseErrors = []
}
in Megaparsec.runParser' fontDirectoryP initialState
2024-02-03 11:58:47 +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
pure $ first DumpParseError initialResult >>= dumpRequest processedState