~jaro/balkon

ref: 2eb2cd0081f77470ddf664bb242c24f2173b8a1d balkon/test/Data/Text/ParagraphLayout/FontLoader.hs -rw-r--r-- 1.7 KiB
2eb2cd00Jaro Bump version to 0.2.0.1. 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Data.Text.ParagraphLayout.FontLoader
    (arabicFont
    ,devanagariFont
    ,latinFont
    ,loadFont
    ,writeFontInfo
    )
where

import Data.ByteString (readFile)
import Data.Int (Int32)
import Data.Text.Glyphize
    (Font
    ,FontOptions
    ,createFace
    ,createFontWithOptions
    ,faceIndex
    ,fontFace
    ,fontPPEm
    ,fontScale
    )
import Data.Word (Word)
import Prelude (concat, fromIntegral, 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 (show32 x) ++ "," ++ (show32 y)
        -- Workaround for:
        -- https://todo.argonaut-constellation.org/~alcinnz/harfbuzz-pure/3
        show32 x = show (fromIntegral x :: Int32)
        face = fontFace font