{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module Diagrams.Backend.PGF.Hbox
( Hbox (..)
, hboxOnline
, hboxSurf
, hboxSurfIO
, hboxPoint
) where
import Data.ByteString.Lazy (toStrict)
import Data.ByteString.Builder (stringUtf8, toLazyByteString)
import Data.Monoid
import Data.Typeable
import System.IO.Unsafe
import System.Texrunner.Online hiding (hbox)
import qualified System.Texrunner.Online as Online
import System.Texrunner.Parse
import Diagrams.Core.Envelope (pointEnvelope)
import Diagrams.Prelude hiding (Box, (<>))
import Diagrams.Backend.PGF.Surface
data Hbox n = Hbox (Transformation V2 n) String
deriving Typeable
type instance V (Hbox n) = V2
type instance N (Hbox n) = n
instance Fractional n => Transformable (Hbox n) where
transform t (Hbox tt str) = Hbox (t <> tt) str
instance Fractional n => Renderable (Hbox n) NullBackend where
render _ _ = mempty
hboxPoint :: (OrderedField n, Typeable n, Renderable (Hbox n) b)
=> String -> QDiagram b V2 n Any
hboxPoint raw = mkQD (Prim (Hbox mempty raw))
(pointEnvelope origin)
mempty
mempty
mempty
hboxSurf :: (TypeableFloat n, Renderable (Hbox n) b)
=> Surface -> String -> QDiagram b V2 n Any
hboxSurf surf txt = unsafePerformIO (hboxSurfIO surf txt)
{-# NOINLINE hboxSurf #-}
hboxSurfIO :: (TypeableFloat n, Renderable (Hbox n) b)
=> Surface -> String -> IO (QDiagram b V2 n Any)
hboxSurfIO surf txt = surfOnlineTexIO surf (hboxOnline txt)
hboxOnline :: (TypeableFloat n, Renderable (Hbox n) b)
=> String -> OnlineTex (QDiagram b V2 n Any)
hboxOnline txt = do
Box h d w <- Online.hbox (toStrict . toLazyByteString $ stringUtf8 txt)
let bb = fromCorners (P $ V2 0 (-d))
(P $ V2 w h)
return $ mkQD (Prim (Hbox mempty txt))
(getEnvelope bb)
(getTrace bb)
mempty
(getQuery bb)