module Diagrams.Backend.Rasterific.Text
( texterific'
, texterific
, fromFontStyle
, textBoundingBox
) where
import Graphics.Text.TrueType hiding (BoundingBox)
import Diagrams.Prelude
import Diagrams.TwoD.Text hiding (Font)
import Paths_diagrams_rasterific (getDataFileName)
import System.IO.Unsafe (unsafePerformIO)
textBoundingBox :: RealFloat n => Font -> PointSize -> String -> BoundingBox V2 n
textBoundingBox f p s = fromCorners
(mkP2 (2*r2f _xMin bb) (r2f _yMin bb))
(mkP2 (r2f _xMax bb + r2f _xMin bb) (r2f _yMax bb))
where
r2f = fmap realToFrac
bb = stringBoundingBox f 96 p s
texterific' :: (TypeableFloat n, Renderable (Text n) b)
=> FontSlant -> FontWeight -> String -> QDiagram b V2 n Any
texterific' fs fw s = recommendFillColor black . fontSizeL 1
. fontSlant fs . fontWeight fw
$ mkQD (Prim $ Text mempty BaselineText s)
(getEnvelope bb)
(getTrace bb)
mempty
(boundingBoxQuery bb)
where
bb = textBoundingBox fnt (PointSize 1) s
fnt = fromFontStyle fs fw
texterific :: (TypeableFloat n, Renderable (Text n) b) => String -> QDiagram b V2 n Any
texterific s = texterific' FontSlantNormal FontWeightNormal s
fromFontStyle :: FontSlant -> FontWeight -> Font
fromFontStyle FontSlantItalic FontWeightBold = openSansBoldItalic
fromFontStyle FontSlantOblique FontWeightBold = openSansBoldItalic
fromFontStyle FontSlantNormal FontWeightBold = openSansBold
fromFontStyle FontSlantItalic FontWeightNormal = openSansItalic
fromFontStyle FontSlantOblique FontWeightNormal = openSansItalic
fromFontStyle _ _ = openSansRegular
staticFont :: String -> Font
staticFont nm =
case unsafePerformIO $ getDataFileName nm >>= loadFontFile of
Right f -> f
Left e -> error e
openSansRegular :: Font
openSansRegular = staticFont "fonts/OpenSans-Regular.ttf"
openSansBold :: Font
openSansBold = staticFont "fonts/OpenSans-Bold.ttf"
openSansItalic :: Font
openSansItalic = staticFont "fonts/OpenSans-Italic.ttf"
openSansBoldItalic :: Font
openSansBoldItalic = staticFont "fonts/OpenSans-BoldItalic.ttf"