| 1 |
-- | |
| 2 |
-- Module : Diagrams.Backend.Cairo.Text |
| 3 |
-- Copyright : (c) 2015 Diagrams-cairo team (see LICENSE) |
| 4 |
-- License : BSD-style (see LICENSE) |
| 5 |
-- Maintainer : [email protected] |
| 6 |
-- |
| 7 |
-- This module provides convenience functions for querying information |
| 8 |
-- from cairo. In particular, this provides utilities for information |
| 9 |
-- about fonts, and creating text primitives with bounds based on the |
| 10 |
-- font being used. To render text with automatically determined |
| 11 |
-- envelopes, use 'textLineBounded', 'textLineBoundedIO', |
| 12 |
-- 'textVisualBounded', or 'textVisualBoundedIO'. |
| 13 |
-- |
| 14 |
-- Many of these functions take a 'Style' 'V2' 'Double' parameter, |
| 15 |
-- determining the style to apply to the text before rendering / |
| 16 |
-- querying information about the text. These 'Style' 'V2' 'Double' |
| 17 |
-- parameters can be created a variety of ways, but the most direct |
| 18 |
-- will likely be by applying style-transforming functions such as |
| 19 |
-- 'font', 'fontSize', 'fontSlant', and 'fontWeight' to 'mempty'. |
| 20 |
-- This works because there are instances of 'HasStyle' and 'Monoid' |
| 21 |
-- for @'Style' v@. |
| 22 |
|
| 23 |
module Diagrams.Backend.Cairo.Text |
| 24 |
( |
| 25 |
-- | These create diagrams instantiated with extent-based envelopes. |
| 26 |
textLineBoundedIO |
| 27 |
, textVisualBoundedIO |
| 28 |
|
| 29 |
-- * Utilities |
| 30 |
, queryCairo, unsafeCairo |
| 31 |
) where |
| 32 |
|
| 33 |
import Diagrams.Backend.Cairo.Internal |
| 34 |
import qualified Diagrams.BoundingBox as BB |
| 35 |
import Diagrams.Prelude hiding (height, view) |
| 36 |
import Diagrams.TwoD.Text hiding (font) |
| 37 |
|
| 38 |
import qualified Graphics.Rendering.Cairo as C |
| 39 |
import qualified Graphics.Rendering.Pango as P |
| 40 |
|
| 41 |
import System.IO.Unsafe |
| 42 |
|
| 43 |
-- | Executes a cairo action on a dummy, zero-size image surface, in order to |
| 44 |
-- query things like font information. |
| 45 |
queryCairo :: C.Render a -> IO a |
| 46 |
queryCairo c = C.withImageSurface C.FormatA1 0 0 (`C.renderWith` c) |
| 47 |
|
| 48 |
-- | Unsafely invokes 'queryCairo' using 'unsafePerformIO'. |
| 49 |
unsafeCairo :: C.Render a -> a |
| 50 |
unsafeCairo = unsafePerformIO . queryCairo |
| 51 |
|
| 52 |
-- | Creates text diagrams with their envelopes set such that using |
| 53 |
-- @'vcat' . map ('textLineBounded' style)@ stacks them in the way that |
| 54 |
-- the font designer intended. Pango refers to this as logical extents. |
| 55 |
textLineBoundedIO :: Style V2 Double -> Text Double -> IO (Diagram Cairo) |
| 56 |
textLineBoundedIO = textLineIO fst |
| 57 |
|
| 58 |
-- | Creates a text diagram with its envelope set to enclose the glyphs of the text, |
| 59 |
-- including leading (though not trailing) whitespace. |
| 60 |
textVisualBoundedIO :: Style V2 Double -> Text Double -> IO (Diagram Cairo) |
| 61 |
textVisualBoundedIO = textLineIO snd |
| 62 |
|
| 63 |
-- | Abstract common code from @textLineBoundedIO@ and @textVisualBoundedIO@ |
| 64 |
-- textLineIO :: ((a,a) -> a) -> Style V2 Double -> Text Double -> IO (Diagram Cairo) |
| 65 |
textLineIO :: ((P.PangoRectangle,P.PangoRectangle) -> P.PangoRectangle) -> Style V2 Double -> Text Double -> IO (Diagram Cairo) |
| 66 |
textLineIO pick sty txt = do |
| 67 |
layout <- queryCairo $ layoutStyledText sty txt |
| 68 |
P.PangoRectangle x y w h <- pick <$> P.layoutGetExtents layout |
| 69 |
let bb = BB.fromCorners (mkP2 x y) (mkP2 (x + w) (y + h)) |
| 70 |
return $ mkQD (Prim txt) (getEnvelope bb) mempty mempty mempty |