ktx-build-0.1.0.0: app/Main.hs
{-# LANGUAGE TypeFamilies #-}
import Control.Monad (void)
import Data.Foldable
import Data.Map.Strict qualified as Map
import Development.Shake
import Development.Shake.Classes
import Development.Shake.FilePath
import GHC.Generics (Generic)
import Build.Atlas (msdfAtlasGen, AtlasJob (..))
import Build.CapHeight (CapHeight(..), Capped(..))
import Build.CapHeight qualified as CapHeight
import Build.Job (Job(..), JobType(..), parseJobsFile)
import Build.Ktx2 (ktxCreate, ktxCreateRaw, ktxTranscode)
import Build.Slug qualified as Slug
import Codec.Ktx2.Font (bundleFont, bundleSlug, packSlugFont, slugVersion)
import Codec.Ktx2.Font.Slug qualified as Slug
import Data.Aeson qualified as Aeson
import Data.ByteString qualified as ByteString
import Data.Vector qualified as Vector
import System.Directory (getFileSize)
{- | The job options a build stage ran with.
Most of them (format, target, aemrange and, depending on the output template,
even size and type) don't show up in the output path. Without an oracle to
depend on, editing them in the config leaves the stale artifacts in place.
Each stage asks only for the options it consumes, so retargeting the
transcode doesn't regenerate the atlas.
-}
data JobParams = JobParams Stage FilePath
deriving (Show, Eq, Generic)
instance Hashable JobParams
instance Binary JobParams
instance NFData JobParams
data Stage
= AtlasStage -- ^ Source and atlas generator options.
| CreateStage -- ^ Source and texture format.
| TranscodeStage -- ^ Transcode target.
| OutlineStage
| CapStage
deriving (Show, Eq, Generic)
instance Hashable Stage
instance Binary Stage
instance NFData Stage
type instance RuleResult JobParams = String
main :: IO ()
main = shakeArgs shakeOptions $ do
let buildDir = shakeFiles shakeOptions </> "build"
phony "clean" $
removeFilesAfter buildDir ["//*"]
want ["assets.yaml", "all"]
jobs <- parseJobsFile "assets.yaml"
let jobIndex = Map.fromList [(jobOutput, job) | job@Job{jobOutput} <- jobs]
askJob <- addOracle \key@(JobParams stage output) ->
case Map.lookup output jobIndex of
Nothing ->
fail $ "No job for " <> show key
Just Job{jobSource, jobType, jobFormat, jobTarget, jobCapHeight} ->
pure case stage of
AtlasStage -> withCapHeight $ show (jobSource, jobType)
OutlineStage -> withCapHeight $ show (jobSource, jobType, slugVersion)
CreateStage -> show (jobSource, jobFormat)
TranscodeStage -> jobTarget
CapStage -> show (jobSource, jobCapHeight)
where
withCapHeight =
case jobCapHeight of
Declared -> id
capHeight -> (<> " " <> show capHeight)
"all" ~>
need [jobOutput | Job{jobOutput} <- jobs]
for_ jobs \job@Job{jobType, jobOutput} -> do
let needStage stage = void . askJob $ JobParams stage jobOutput
case jobType of
Atlas args -> fontSource buildDir needStage job >>= \source -> atlasJob buildDir needStage source job args
Slug -> fontSource buildDir needStage job >>= \source -> slugJob buildDir needStage source job
Texture -> textureJob buildDir needStage job
fontSource :: FilePath -> (Stage -> Action ()) -> Job -> Rules FilePath
fontSource buildDir needStage Job{..} =
case jobCapHeight of
Declared ->
pure jobSource
capHeight -> do
capped %> \out -> do
needStage CapStage
need [jobSource]
result <- traced "cap-height" $
ByteString.readFile jobSource >>= CapHeight.capped jobSource capHeight
putInfo $ jobSource <> ": cap height " <> show result.declared <> " -> " <> show result.applied
liftIO $ ByteString.writeFile out result.font
pure capped
where
capped = buildDir </> jobOutput -<.> "ttf"
atlasJob :: FilePath -> (Stage -> Action ()) -> FilePath -> Job -> AtlasJob -> Rules ()
atlasJob buildDir needStage source Job{..} args = do
jobOutput %> \ktxf -> do
needStage AtlasStage
need [json, ktx2]
traced "ktxf-bundle" $
bundleFont source json ktx2 ktxf
ktx2 %> \_ -> do
needStage TranscodeStage
need [uastc]
ktxTranscode uastc jobTarget ktx2
uastc %> \_ -> do
needStage CreateStage
need [png]
ktxCreate png jobFormat uastc
[json, png] &%> \_ -> do
needStage AtlasStage
need [source]
msdfAtlasGen args source json png
where
ktx2 = buildDir </> jobOutput -<.> "ktx2"
uastc = buildDir </> jobOutput -<.> "uastc"
png = buildDir </> jobOutput -<.> "png"
json = buildDir </> jobOutput -<.> "json"
slugJob :: FilePath -> (Stage -> Action ()) -> FilePath -> Job -> Rules ()
slugJob buildDir needStage source Job{..} = do
jobOutput %> \ktxf -> do
needStage OutlineStage
need [glyphs, bands, ktx2]
traced "ktxf-bundle" do
glyphTable <- Aeson.eitherDecodeFileStrict glyphs >>= either fail pure
bandValues <- ByteString.readFile bands >>= either fail pure . Slug.decodeBands
bundleSlug source Slug.Tables{bands = bandValues, glyphs = Vector.fromList glyphTable} ktx2 ktxf
ktx2 %> \_ -> do
needStage CreateStage
need [curves]
bytes <- liftIO $ getFileSize curves
ktxCreateRaw curves jobFormat Slug.dataWidth (fromIntegral bytes `div` (8 * Slug.dataWidth)) ktx2
[curves, bands, glyphs] &%> \_ -> do
needStage OutlineStage
need [source]
traced "slug-pack" do
packed <- Slug.outlines source >>= packSlugFont source
ByteString.writeFile curves $ Slug.encodeCurves packed.curves
ByteString.writeFile bands $ Slug.encodeBands packed.tables.bands
Aeson.encodeFile glyphs $ Vector.toList packed.tables.glyphs
where
ktx2 = buildDir </> jobOutput -<.> "ktx2"
curves = buildDir </> jobOutput -<.> "curves"
bands = buildDir </> jobOutput -<.> "bands"
glyphs = buildDir </> jobOutput -<.> "glyphs.json"
textureJob :: FilePath -> (Stage -> Action ()) -> Job -> Rules ()
textureJob buildDir needStage Job{..} = do
jobOutput %> \out -> do
needStage TranscodeStage
need [uastc]
ktxTranscode uastc jobTarget out
uastc %> \_ -> do
needStage CreateStage
need [jobSource]
ktxCreate jobSource jobFormat uastc
where
uastc = buildDir </> jobOutput -<.> "uastc"