{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}
module DearImGui.Raw.Font
(
Font(..)
, GlyphRanges(..)
, addFontDefault
, addFontFromFileTTF
, addFontFromMemoryTTF
, pushFont
, popFont
, clearFontAtlas
, buildFontAtlas
)
where
import Control.Monad.IO.Class
( MonadIO, liftIO )
import Foreign ( Ptr, castPtr )
import Foreign.C
import DearImGui.Raw.Context
( imguiContext )
import DearImGui.Structs
import DearImGui.Raw.Font.Config
( FontConfig(..) )
import DearImGui.Raw.Font.GlyphRanges
( GlyphRanges(..) )
import qualified Language.C.Inline as C
import qualified Language.C.Inline.Cpp as Cpp
C.context (Cpp.cppCtx <> C.bsCtx <> imguiContext)
C.include "imgui.h"
Cpp.using "namespace ImGui"
newtype Font = Font (Ptr ImFont)
addFontDefault :: MonadIO m
=> m Font
addFontDefault :: forall (m :: * -> *). MonadIO m => m Font
addFontDefault = IO Font -> m Font
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
Ptr ImFont -> Font
Font (Ptr ImFont -> Font) -> IO (Ptr ImFont) -> IO Font
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Ptr ImFont)
[C.block|
ImFont* {
return GetIO().Fonts->AddFontDefault();
}
|]
addFontFromFileTTF :: MonadIO m
=> CString
-> CFloat
-> FontConfig
-> GlyphRanges
-> m Font
addFontFromFileTTF :: forall (m :: * -> *).
MonadIO m =>
CString -> CFloat -> FontConfig -> GlyphRanges -> m Font
addFontFromFileTTF CString
filenamePtr CFloat
sizePixels (FontConfig Ptr ImFontConfig
fontConfig) (GlyphRanges Ptr ImWchar
glyphRanges) = IO Font -> m Font
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
Ptr ImFont -> Font
Font (Ptr ImFont -> Font) -> IO (Ptr ImFont) -> IO Font
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [C.block|
ImFont* {
return GetIO().Fonts->AddFontFromFileTTF(
$(char* filenamePtr),
$(float sizePixels),
$(ImFontConfig* fontConfig),
$(ImWchar* glyphRanges));
}
|]
addFontFromMemoryTTF :: MonadIO m => CStringLen -> CFloat -> FontConfig -> GlyphRanges -> m Font
addFontFromMemoryTTF :: forall (m :: * -> *).
MonadIO m =>
CStringLen -> CFloat -> FontConfig -> GlyphRanges -> m Font
addFontFromMemoryTTF (CString -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr -> Ptr ()
fontDataPtr, Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral -> CInt
fontSize) CFloat
sizePixels (FontConfig Ptr ImFontConfig
fontConfig) (GlyphRanges Ptr ImWchar
glyphRanges) = IO Font -> m Font
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
Ptr ImFont -> Font
Font (Ptr ImFont -> Font) -> IO (Ptr ImFont) -> IO Font
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [C.block|
ImFont* {
return GetIO().Fonts->AddFontFromMemoryTTF(
$(void* fontDataPtr),
$(int fontSize),
$(float sizePixels),
$(ImFontConfig* fontConfig),
$(ImWchar* glyphRanges)
);
}
|]
pushFont :: MonadIO m => Font -> m ()
pushFont :: forall (m :: * -> *). MonadIO m => Font -> m ()
pushFont (Font Ptr ImFont
font) = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
[C.exp| void { PushFont($(ImFont* font)); } |]
popFont :: MonadIO m => m ()
popFont :: forall (m :: * -> *). MonadIO m => m ()
popFont = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
IO ()
[C.exp| void { PopFont(); } |]
buildFontAtlas :: MonadIO m => m ()
buildFontAtlas :: forall (m :: * -> *). MonadIO m => m ()
buildFontAtlas = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
IO ()
[C.block|
void {
GetIO().Fonts->Build();
}
|]
clearFontAtlas :: MonadIO m => m ()
clearFontAtlas :: forall (m :: * -> *). MonadIO m => m ()
clearFontAtlas = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
IO ()
[C.block|
void {
GetIO().Fonts->Clear();
}
|]