packages feed

ktx-build-0.1.0.0: app/Build/Atlas.hs

{-# OPTIONS_GHC -Wno-orphans #-}
module Build.Atlas where

import Data.Aeson
import Data.Char (toLower)
import Data.Maybe
import Development.Shake
import Development.Shake.Classes

import Graphics.MSDF.Atlas.Layout (AtlasType(..))
import GHC.Generics (Generic)

instance Hashable AtlasType
instance Binary AtlasType
instance NFData AtlasType

data AtlasJob = AtlasJob
  { atlasType :: AtlasType
  , size :: Float
  , aemRange :: Maybe (Float, Float)
  } deriving (Show, Eq, Generic)

instance FromJSON AtlasJob where
  parseJSON = withObject "AtlasJob" \job -> do
    atlasType <- job .: "type"
    size <- job .: "size"
    aemRange <- job .:? "aemrange"
    pure AtlasJob{..}

instance Hashable AtlasJob
instance Binary AtlasJob
instance NFData AtlasJob

msdfAtlasGen :: AtlasJob -> FilePath -> FilePath -> FilePath -> Action ()
msdfAtlasGen AtlasJob{..} ttf json png = do
  gen_ <- getEnv "MSDF_ATLAS_GEN"
  cmd_
    (fromMaybe "msdf-atlas-gen.caps" gen_)
    [ "-caps" -- XXX: requires caps fork
    , "-allglyphs"
    , "-font", ttf
    , "-json", json
    , "-imageout", png
    , "-type", map toLower $ show atlasType
    , "-size", show size
    ]
    aemRangeArgs
    -- XXX: TODO: pull from job options
    [ "-potr" :: String
    , "-yorigin", "top"
    , "-pxpadding", "4"
    , "-pxalign", "on"
    , "-seed", "42"
    ]
  where
    aemRangeArgs =
      case aemRange of
        Nothing ->
          []
        Just (lower, upper) ->
          ["-aemrange", show lower, show upper]