packages feed

balkon-0.3.0.0: test/Data/Text/ParagraphLayout/FontLoader.hs

module Data.Text.ParagraphLayout.FontLoader
    ( arabicFont
    , devanagariFont
    , latinFont
    , loadFont
    , writeFontInfo
    )
where

import Data.ByteString (readFile)
import Data.List (concat, (++))
import Data.Text.Glyphize
    ( Font
    , FontOptions
    , createFace
    , createFontWithOptions
    , faceIndex
    , fontFace
    , fontPPEm
    , fontScale
    )
import Data.Word (Word)
import Prelude (return, show, ($))
import System.FilePath (FilePath, (</>))
import System.IO (IO, writeFile)

arabicFont :: FilePath
arabicFont = "assets" </> "fonts" </> "plex" </> "IBMPlexSansArabic-Regular.ttf"

devanagariFont :: FilePath
devanagariFont = "assets" </> "fonts" </> "sarala" </> "Sarala-Regular.ttf"

latinFont :: FilePath
latinFont = "assets" </> "fonts" </> "ubuntu" </> "Ubuntu-R.ttf"

loadFont :: FilePath -> Word -> FontOptions -> IO Font
loadFont path index opts = do
    ttf <- readFile path
    let face = createFace ttf index
    let font = createFontWithOptions opts face
    return font

-- | Write a file containing information about the font and options used in a
-- test, to be passed to Typograffiti.
--
-- Note: The font path will not be escaped.
writeFontInfo :: FilePath -> FilePath -> Font -> IO ()
writeFontInfo outputPath fontPath font = writeFile outputPath info
    where
        info = concat [fontPath, " ", index, " ", pixelSize, " ", scale, "\n"]
        index = show $ faceIndex face
        pixelSize = let (x, y) = fontPPEm font in (show x) ++ "," ++ (show y)
        scale = let (x, y) = fontScale font in (show x) ++ "," ++ (show y)
        face = fontFace font