module Data.Text.ParagraphLayout.FontLoader
( arabicFont
, devanagariFont
, latinFont
, loadFont
, writeFontInfo
, testingOptions
, testingOptionsSmall
, demoOptions
)
where
import Data.ByteString (readFile)
import Data.List (concat, (++))
import Data.Maybe (Maybe (Just))
import Data.Text.Glyphize
( Font
, FontOptions (optionPPEm, optionScale)
, createFace
, createFontWithOptions
, defaultFontOptions
, 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
-- | Font options to be used for high precision testing (1000 units per EM).
-- Hinting should behave as if the font size were 24px.
-- TODO: Test hinting.
testingOptions :: FontOptions
testingOptions = defaultFontOptions {
optionPPEm = Just (24, 24),
optionScale = Just (1000, 1000)
}
-- | Like `testingOptions` with font size proportionally changed to 18px.
testingOptionsSmall :: FontOptions
testingOptionsSmall = defaultFontOptions {
optionPPEm = Just (18, 18),
optionScale = Just (750, 750)
}
-- | Font options to be used to be used for easy visual testing in a demo app
-- (32 units per EM, 1 unit per pixel).
-- This makes 20em equal 640px, which is the width of the demo window.
demoOptions :: FontOptions
demoOptions = defaultFontOptions {
optionPPEm = Just (32, 32),
optionScale = Just (32, 32)
}