~jaro/balkon

ref: v1.3.0.0 balkon/test/Data/Text/ParagraphLayout/FontLoader.hs -rw-r--r-- 2.5 KiB
9e3b0ec7Jaro Set release date for v1.3.0.0. 1 year, 5 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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)
}