module Waterfall.TwoD.Text
( text
, Font
, FontAspect (..)
, fontFromPath
, fontFromSystem
) where
import qualified Waterfall.TwoD.Internal.Shape as Shape
import qualified OpenCascade.GP.Ax3 as GP.Ax3
import qualified OpenCascade.Font.BRepFont as BRepFont
import qualified OpenCascade.Font.BRepTextBuilder as BRepTextBuilder
import qualified OpenCascade.Graphic3D.VerticalTextAlignment as VTA
import qualified OpenCascade.Graphic3D.HorizontalTextAlignment as HTA
import Foreign.Ptr
import Control.Monad (unless)
import Control.Monad.IO.Class (liftIO)
import OpenCascade.Font.FontAspect (FontAspect (..))
newtype Font = Font {Font -> Ptr BRepFont -> IO ()
initFont :: Ptr BRepFont.BRepFont -> IO ()}
fontFromPath :: FilePath -> Double -> Font
fontFromPath :: [Char] -> Double -> Font
fontFromPath [Char]
fontpath Double
size = (Ptr BRepFont -> IO ()) -> Font
Font ((Ptr BRepFont -> IO ()) -> Font)
-> (Ptr BRepFont -> IO ()) -> Font
forall a b. (a -> b) -> a -> b
$ \Ptr BRepFont
font -> do
Bool
fontOk <- Ptr BRepFont -> [Char] -> Double -> IO Bool
BRepFont.initFromPathAndSize Ptr BRepFont
font [Char]
fontpath Double
size
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
fontOk) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char]
"Unable to initialize font from filepath: " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
fontpath)
fontFromSystem :: String -> FontAspect -> Double -> Font
fontFromSystem :: [Char] -> FontAspect -> Double -> Font
fontFromSystem [Char]
name FontAspect
aspect Double
size = (Ptr BRepFont -> IO ()) -> Font
Font ((Ptr BRepFont -> IO ()) -> Font)
-> (Ptr BRepFont -> IO ()) -> Font
forall a b. (a -> b) -> a -> b
$ \Ptr BRepFont
font -> do
Bool
fontOk <- Ptr BRepFont -> [Char] -> FontAspect -> Double -> IO Bool
BRepFont.initFromNameAspectAndSize Ptr BRepFont
font [Char]
name FontAspect
aspect Double
size
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
fontOk) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char]
"Unable to initialize system font with name: " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
name [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
", and aspect" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> FontAspect -> [Char]
forall a. Show a => a -> [Char]
show FontAspect
aspect)
text :: Font -> String -> Shape.Shape
text :: Font -> [Char] -> Shape
text Font
font [Char]
content = Acquire (Ptr Shape) -> Shape
Shape.Shape (Acquire (Ptr Shape) -> Shape) -> Acquire (Ptr Shape) -> Shape
forall a b. (a -> b) -> a -> b
$ do
Ptr Ax3
axis <- Acquire (Ptr Ax3)
GP.Ax3.new
Ptr BRepFont
brepfont <- Acquire (Ptr BRepFont)
BRepFont.new
IO () -> Acquire ()
forall a. IO a -> Acquire a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Acquire ()) -> IO () -> Acquire ()
forall a b. (a -> b) -> a -> b
$ Font -> Ptr BRepFont -> IO ()
initFont Font
font Ptr BRepFont
brepfont
Ptr BRepTextBuilder
builder <- Acquire (Ptr BRepTextBuilder)
BRepTextBuilder.new
Ptr BRepTextBuilder
-> Ptr BRepFont
-> [Char]
-> Ptr Ax3
-> HorizontalTextAlignment
-> VerticalTextAlignment
-> Acquire (Ptr Shape)
BRepTextBuilder.perform Ptr BRepTextBuilder
builder Ptr BRepFont
brepfont [Char]
content Ptr Ax3
axis HorizontalTextAlignment
HTA.Center VerticalTextAlignment
VTA.Center