Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist
dist*
.cabal-sandbox
*.swp
*.swo
20 changes: 11 additions & 9 deletions src/Graphics/Text/TrueType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
-- | Module in charge of loading fonts.
module Graphics.Text.TrueType
( -- * Functions
Expand Down Expand Up @@ -83,6 +84,7 @@ import Graphics.Text.TrueType.Kerning (getKerningValue)
import Graphics.Text.TrueType.Name()
import Graphics.Text.TrueType.FontType
import Graphics.Text.TrueType.FontFolders
import System.IO (hClose, IOMode (..), openFile)

{-import Debug.Trace-}

Expand Down Expand Up @@ -228,10 +230,15 @@ getFontNameAndStyle =
-- | This function will search in the system for truetype
-- files and index them in a cache for further fast search.
buildCache :: IO FontCache
buildCache = buildFontCache loader
buildCache = buildFontCache fontLoader

fontLoader :: FilePath -> IO (Maybe Font)
fontLoader n = do
h <- (openFile n ReadMode)
!x <- toMayb . getOrFail getFontNameAndStyle <$> LB.hGetContents h
hClose h
pure x
where
loader n =
toMayb . getOrFail getFontNameAndStyle <$> LB.readFile n
toMayb (Left _) = Nothing
toMayb (Right v) = Just v

Expand All @@ -244,12 +251,7 @@ findFontInCache (FontCache cache) descr = M.lookup descr cache
-- find a font with the desired properties. Favor using
-- a FontCache to speed up the lookup process.
findFontOfFamily :: String -> FontStyle -> IO (Maybe FilePath)
findFontOfFamily = findFont loader
where
loader n =
toMayb . getOrFail getFontNameAndStyle <$> LB.readFile n
toMayb (Left _) = Nothing
toMayb (Right v) = Just v
findFontOfFamily = findFont fontLoader

-- | Express device resolution in dot per inch.
type Dpi = Int
Expand Down