Skip to content

Commit

Permalink
Stop using record syntax for FontDef
Browse files Browse the repository at this point in the history
  • Loading branch information
klausweiss committed Sep 11, 2022
1 parent 7e15883 commit 03d16ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/Monomer/Graphics/FontManager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import Monomer.Common.BasicTypes
import Monomer.Graphics.FFI
import Monomer.Graphics.Types
import Monomer.Helper (putStrLnErr)
import Monomer.Graphics.Lens (fontName, fontPath, fontBytes)

import qualified Monomer.Graphics.Lens as L

-- | Creates a font manager instance.
makeFontManager
Expand Down Expand Up @@ -109,9 +110,9 @@ loadFont ctx fonts fontDef = do
then return $ name : fonts
else putStrLnErr ("Failed to load font: " ++ T.unpack name) >> return fonts
where
name = fontDef ^. fontName
createFont FontDefFile{} = fmCreateFont ctx name (fontDef ^. fontPath)
createFont FontDefMem{} = fmCreateFontMem ctx name (fontDef ^. fontBytes)
name = fontDef ^. L.name
createFont (FontDefFile name path) = fmCreateFont ctx name path
createFont (FontDefMem name bytes) = fmCreateFontMem ctx name bytes

setFont :: FMContext -> Double -> Font -> FontSize -> FontSpace -> IO ()
setFont ctx scale (Font name) (FontSize size) (FontSpace spaceH) = do
Expand Down
10 changes: 10 additions & 0 deletions src/Monomer/Graphics/Lens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Lenses for the Graphics types.

module Monomer.Graphics.Lens where

import Control.Lens (lens)
import Control.Lens.TH (abbreviatedFields, makeLensesWith)
import Data.Text (Text)

import Monomer.Common.Lens
import Monomer.Core.Lens
Expand All @@ -26,3 +28,11 @@ makeLensesWith abbreviatedFields ''GlyphPos
makeLensesWith abbreviatedFields ''ImageDef
makeLensesWith abbreviatedFields ''TextMetrics
makeLensesWith abbreviatedFields ''TextLine

instance HasName FontDef Text where
name = lens getName setName
where
getName (FontDefFile name _) = name
getName (FontDefMem name _) = name
setName (FontDefFile _ path) name = FontDefFile name path
setName (FontDefMem _ bytes) name = FontDefMem name bytes
6 changes: 3 additions & 3 deletions src/Monomer/Graphics/NanoVGRenderer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ loadFont c fonts fontDef = do
Just{} -> return $ Set.insert name fonts
_ -> putStrLnErr ("Failed to load font: " ++ T.unpack name) >> return fonts
where
name = fontDef ^. L.fontName
createFont FontDefFile{} = VG.createFont c name (VG.FileName $ fontDef ^. L.fontPath)
createFont FontDefMem{} = VG.createFontMem c name (fontDef ^. L.fontBytes)
name = fontDef ^. L.name
createFont (FontDefFile name path) = VG.createFont c name (VG.FileName path)
createFont (FontDefMem name bytes) = VG.createFontMem c name bytes

setFont
:: VG.Context
Expand Down
10 changes: 2 additions & 8 deletions src/Monomer/Graphics/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ instance Default Color where
def = Color 255 255 255 1.0

data FontDef
= FontDefFile
{ _fntFontName :: !Text -- ^ The logic name. Will be used when defining styles.
, _fntFontPath :: !Text -- ^ The path in the filesystem.
}
| FontDefMem
{ _fntFontName :: !Text -- ^ The logic name. Will be used when defining styles.
, _fntFontBytes :: !ByteString -- ^ The bytes of the loaded font.
}
= FontDefFile !Text !Text -- ^ Associates a font name with a filesystem path.
| FontDefMem !Text !ByteString -- ^ Associates a font name with a font loaded in memory.
deriving (Eq, Show, Generic)

-- | The name of a loaded font.
Expand Down

0 comments on commit 03d16ce

Please sign in to comment.