-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading fonts from memory #199
Changes from 6 commits
32e067a
fac20ed
13f55dd
5cbbe4b
06d061a
ec4bf11
e439230
1e4bd8b
d6f11a0
cf7839c
9e3bac9
0ee513d
85ce069
cf68aea
ced04e2
7112d1e
5403752
7f1db12
5cd391b
375c5cb
7e15883
03d16ce
7880379
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,7 @@ executables: | |
- -threaded | ||
dependencies: | ||
- monomer | ||
- file-embed | ||
|
||
books: | ||
main: Main.hs | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ import Monomer.Common.BasicTypes | |
import Monomer.Graphics.FFI | ||
import Monomer.Graphics.Types | ||
import Monomer.Helper (putStrLnErr) | ||
import Control.Lens ((^.)) | ||
import Monomer.Graphics.Lens (fontName, fontPath, fontBytes) | ||
|
||
-- | Creates a font manager instance. | ||
makeFontManager | ||
|
@@ -101,11 +103,15 @@ newManager ctx = FontManager {..} where | |
} | ||
|
||
loadFont :: FMContext -> [Text] -> FontDef -> IO [Text] | ||
loadFont ctx fonts (FontDef name path) = do | ||
res <- fmCreateFont ctx name path | ||
loadFont ctx fonts fontDef = do | ||
res <- createFont fontDef | ||
if res >= 0 | ||
then return $ path : fonts | ||
then return $ name : fonts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the values inside the resulting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! 👍 |
||
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) | ||
|
||
setFont :: FMContext -> Double -> Font -> FontSize -> FontSpace -> IO () | ||
setFont ctx scale (Font name) (FontSize size) (FontSpace spaceH) = do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,10 @@ Basic types for Graphics. | |
|
||
Angles are always expressed in degrees, not radians. | ||
-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE Strict #-} | ||
|
||
module Monomer.Graphics.Types where | ||
|
@@ -45,11 +48,16 @@ data Color = Color { | |
instance Default Color where | ||
def = Color 255 255 255 1.0 | ||
|
||
-- | The definition of a font. | ||
data FontDef = FontDef { | ||
_fntName :: !Text, -- ^ The logic name. Will be used when defining styles. | ||
_fntPath :: !Text -- ^ The path in the filesystem. | ||
} deriving (Eq, Show, Generic) | ||
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. | ||
} | ||
deriving (Eq, Show, Generic) | ||
Comment on lines
+48
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd love to use this data FontDef
= FontDefFile
{ _fntName :: !Text -- ^ The logic name. Will be used when defining styles.
, _fntPath :: !Text -- ^ The path in the filesystem.
}
| FontDefMem
{ _fntName :: !Text -- ^ The logic name. Will be used when defining styles.
, _fntBytes :: !ByteString -- ^ The bytes of the loaded font.
}
deriving (Eq, Show, Generic) but for reasons I don't understand (some lens stuff) I can't:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also note this change creates traversals, not lenses, which is something to watch out for, see GHCI: λ: FontDefMem "name" "Bytes" ^. fontPath
"" I'm open to alternatives, but given how small these constructors are, I'm fine with leaving as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with the naming you chose. I guess the error is caused by a previous Lens/Prism generated in https://github.com/fjvallarino/monomer/blob/main/src/Monomer/Lens.hs. Alternatively, since this constructors are only used in a couple of places, you can use: data FontDef
= 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) Then, in ...
createFont (FontDefFile name path) = VG.createFont c name (VG.FileName path)
createFont (FontDefMem name bytes) = VG.createFontMem c name bytes
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I went with what you suggested. |
||
|
||
-- | The name of a loaded font. | ||
newtype Font | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ import Monomer.Core.ThemeTypes | |
import Monomer.Core.WidgetTypes | ||
import Monomer.Event.Types | ||
import Monomer.Graphics.Types | ||
import Data.ByteString (ByteString) | ||
|
||
-- | Main Monomer monad. | ||
type MonomerM s e m = (Eq s, MonadState (MonomerCtx s e) m, MonadCatch m, MonadIO m) | ||
|
@@ -385,9 +386,24 @@ appDisableAutoScale disable = def { | |
Available fonts to the application. An empty list will make it impossible to | ||
render text. | ||
-} | ||
appFontDef :: Text -> Text -> AppConfig e | ||
appFontDef name path = def { | ||
_apcFonts = [ FontDef name path ] | ||
appFontDefFile :: Text -> Text -> AppConfig e | ||
appFontDefFile name path = def { | ||
_apcFonts = [ FontDefFile name path ] | ||
} | ||
|
||
{- | | ||
Alias for 'appFontDefFile' for backwards compatibility. | ||
-} | ||
appFontDef = appFontDefFile | ||
{-# DEPRECATED appFontDef "Use appFontDefFile directly" #-} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the changes here get through, I propose deprecating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we go down this route, we'll also need to update the examples and docs code to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @klausweiss I'd prefer to keep the existing If you prefer to keep it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sounds good (re deprecation). Renamed |
||
|
||
{-| | ||
Available fonts to the application. An empty list will make it impossible to | ||
render text. | ||
-} | ||
appFontDefMem :: Text -> ByteString -> AppConfig e | ||
appFontDefMem name bytes = def { | ||
_apcFonts = [ FontDefMem name bytes ] | ||
} | ||
|
||
-- | Initial theme. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@klausweiss I'm thinking maybe something like this could work (I have not tested it, and it may not compile as it stands):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the C API requires it to use
Ptr CUChar
instead ofPtr CChar
, whichCString
uses. I've made some progress (see this comment and the code I pushed), but that's still not it.