Extract some convenience parsing functions
This commit is contained in:
@@ -7,11 +7,22 @@
|
||||
module Graphics.Fountainhead.Metrics
|
||||
( FontBBox(..)
|
||||
, FontDescriptor(..)
|
||||
, MetricsError(..)
|
||||
, Number
|
||||
, FontDescriptorFlag(..)
|
||||
, collectMetrics
|
||||
) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.Text (Text)
|
||||
import Graphics.Fountainhead.TrueType (findTableByTag)
|
||||
import Graphics.Fountainhead.Parser
|
||||
( ParseErrorBundle
|
||||
, nameTableP
|
||||
, parseFontDirectory
|
||||
, parseTable
|
||||
)
|
||||
import qualified Text.Megaparsec as Megaparsec
|
||||
|
||||
type Number = Double
|
||||
|
||||
@@ -66,3 +77,27 @@ data FontDescriptor = FontDescriptor
|
||||
, ascender :: Number
|
||||
, descender :: Number
|
||||
} deriving (Eq, Show)
|
||||
|
||||
data MetricsError
|
||||
= MetricsParseError ParseErrorBundle
|
||||
| MetricsRequiredTableMissingError String
|
||||
deriving Eq
|
||||
|
||||
instance Show MetricsError
|
||||
where
|
||||
show (MetricsParseError errorBundle) = Megaparsec.errorBundlePretty errorBundle
|
||||
show (MetricsRequiredTableMissingError tableName) =
|
||||
"Required table " <> tableName <> " is missing."
|
||||
|
||||
collectMetrics :: FilePath -> ByteString -> Either MetricsError FontDescriptor
|
||||
collectMetrics fontFile ttfContents =
|
||||
case parseFontDirectory fontFile ttfContents of
|
||||
(_processedState, Left initialResult) -> Left
|
||||
$ MetricsParseError initialResult
|
||||
(processedState, Right initialResult)
|
||||
| Just tableEntry <- findTableByTag "name" initialResult
|
||||
, Right parsedNameTable <- parseTable tableEntry nameTableP processedState ->
|
||||
pure $ FontDescriptor
|
||||
{
|
||||
}
|
||||
| otherwise -> Left $ MetricsRequiredTableMissingError "name"
|
||||
|
Reference in New Issue
Block a user