packages feed

kb-text-shape-0.2.1.0: app/ttf2kbts/Main.hs

module Main (main) where

import Data.ByteString qualified as ByteString
import Options.Applicative
import System.FilePath (replaceExtension)

import KB.Text.Shape.Font qualified as Font

main :: IO ()
main = do
  Options{..} <- execParser optionsInfo
  ttfData <- ByteString.readFile inputPath
  blobData <- Font.extractBlob ttfData fontIndex
  ByteString.writeFile (resolveOutput inputPath outputPath) blobData

data Options = Options
  { inputPath :: FilePath
  , outputPath :: Maybe FilePath
  , fontIndex :: Int
  }

resolveOutput :: FilePath -> Maybe FilePath -> FilePath
resolveOutput inputPath = \case
  Just explicit -> explicit
  Nothing -> replaceExtension inputPath "kbts"

optionsInfo :: ParserInfo Options
optionsInfo = info (helper <*> optionsParser) $ mconcat
  [ fullDesc
  , progDesc "Extract a pre-processed .kbts blob from a font file"
  ]

optionsParser :: Parser Options
optionsParser = do
  inputPath <- strArgument $ mconcat
    [ metavar "FONT"
    , help "Input font file (TTF/OTF)"
    ]
  outputPath <- optional . strOption $ mconcat
    [ long "output"
    , short 'o'
    , metavar "FILE"
    , help "Output .kbts file (default: FONT with the extension replaced by .kbts)"
    ]
  fontIndex <- option auto $ mconcat
    [ long "index"
    , short 'i'
    , metavar "N"
    , value 0
    , showDefault
    , help "Font index inside a collection"
    ]
  pure Options{..}