packages feed

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

module Build.CapHeight
  ( CapHeight(..)
  , Capped(..)
  , capped
  ) where

import Codec.TTF.Reader qualified as TTF
import Control.DeepSeq (NFData, force)
import Control.Exception (evaluate, try)
import Control.Monad (unless, when)
import Data.Binary (Binary)
import Data.Bits (shiftL, shiftR, (.&.), (.|.))
import Data.ByteString (ByteString)
import Data.ByteString qualified as ByteString
import Data.Hashable (Hashable)
import GHC.Generics (Generic)

data CapHeight
  = Declared
  | Measured
  | Units Int
  deriving (Show, Eq, Generic)

instance Hashable CapHeight
instance Binary CapHeight
instance NFData CapHeight

data Capped = Capped
  { declared :: Int
  , applied :: Int
  , font :: ByteString
  }

capped :: FilePath -> CapHeight -> ByteString -> IO Capped
capped path wanted bytes = do
  os2 <-
    case TTF.lookupTableRecord tables TTF.TableTag_OS_2 of
      TTF.NoTableRecord -> failWith "no OS/2 table"
      found -> pure found
  let os2Data = TTF.getTableData bytes os2
  when (ByteString.length os2Data < sCapHeight + 2 || word16 os2Data 0 < 2) $
    failWith "the OS/2 table is too old to declare a cap height"

  let declared = word16 os2Data sCapHeight
  applied <-
    case wanted of
      Declared -> pure declared
      Units units -> pure units
      Measured -> measuredH

  unless (applied > 0 && applied < 0x8000) $
    failWith $ "cap height out of range: " <> show applied

  pure Capped
    { declared
    , applied
    , font = putWord16 (os2.offset + sCapHeight) applied bytes
    }
  where
    failWith message = fail $ path <> ": " <> message

    tables = TTF.readTableDirectory bytes

    measuredH = do
      measured <- try @TTF.TTFException . evaluate . force $ capTop tables bytes
      case measured of
        Left err -> failWith $ show err <> " while measuring H"
        Right Nothing -> failWith "no outline for H"
        Right (Just top) -> pure top

sCapHeight :: Int
sCapHeight = 88

capTop :: TTF.TableRecords -> ByteString -> Maybe Int
capTop tables bytes =
  case TTF.lookupGlyphId (TTF.readCMap $ table TTF.TableTag_cmap) 'H' of
    TTF.NoGlyphId ->
      Nothing
    glyphId -> do
      contours <- TTF.readGlyphOutline loca (table TTF.TableTag_glyf) glyphId
      case [point.y | contour <- contours, segment <- TTF.contourSegments contour, point <- points segment] of
        [] -> Nothing
        ys -> Just . round $ maximum ys
  where
    table tag = TTF.getTableData bytes (TTF.lookupTableRecord tables tag)
    numGlyphs = TTF.readNumGlyphs (table TTF.TableTag_maxp)
    loca = TTF.readLoca (TTF.readIndexToLocFormat (table TTF.TableTag_head)) numGlyphs (table TTF.TableTag_loca)

    points = \case
      TTF.LineSegment a b -> [a, b]
      TTF.QuadraticSegment a c b -> [a, c, b]

word16 :: ByteString -> Int -> Int
word16 bytes at = fromIntegral (ByteString.index bytes at) `shiftL` 8 .|. fromIntegral (ByteString.index bytes (at + 1))

putWord16 :: Int -> Int -> ByteString -> ByteString
putWord16 at value bytes =
  ByteString.take at bytes
    <> ByteString.pack [fromIntegral $ value `shiftR` 8 .&. 0xFF, fromIntegral $ value .&. 0xFF]
    <> ByteString.drop (at + 2) bytes