{-# LINE 2 "./Graphics/Rendering/Pango/Font.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) - text layout functions: Font
--
-- Author : Axel Simon
--
-- Created: 16 October 2005
--
-- Copyright (C) 1999-2005 Axel Simon
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- Fonts. The selection of an appropriate font to render text becomes a
-- substantial task in the presence of Unicode where a single font does not
-- cover the whole range of possible characters. Pango provides several
-- concepts to find appropriate fonts and to query information about them:
--
-- * 'FontDescription': Font descriptions provide a way to query and state
-- requirements on
-- fonts. This data structure has several fields describing different
-- characteristics of a font. Each of these fields can be set of left
-- unspecified.
--
-- * 'FontMap' : A font map represents the set of fonts available for a
-- particular rendering system. In particular this map defines the
-- relation between font size and pixel size in terms of the output medium.
--
-- * 'FontFamily' : A font family represents a set of fonts that have
-- related faces, that is, their faces share a common design, but differ
-- in slant, weight, width and other aspects.
--
-- * 'FontFace': A face is a specific font where all characteristics are
-- fixed except for the size.
--
-- * 'Font': A font in the underlying rendering system.
--
-- * 'FontMetrics': Information about the font that will be used to render
-- a specific 'Context' or 'PangoItem'.
--
module Graphics.Rendering.Pango.Font (
  -- Functions to manage font descriptions.
  module Graphics.Rendering.Pango.Description,
  -- Font metrics.
  FontMap,
  FontMapClass,
  pangoFontMapListFamilies,
  FontFamily,
  FontFamilyClass,

  pangoFontFamilyIsMonospace,

  pangoFontFamilyListFaces,
  FontFace,
  FontFaceClass,

  pangoFontFaceListSizes,

  pangoFontFaceDescribe,
  Font,
  FontClass,
  ) where

import Control.Monad (liftM)
import qualified Data.Text as T (unpack)

import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.GObject (makeNewGObject)
import Graphics.Rendering.Pango.BasicTypes
{-# LINE 83 "./Graphics/Rendering/Pango/Font.chs" #-}
import Graphics.Rendering.Pango.Types
{-# LINE 84 "./Graphics/Rendering/Pango/Font.chs" #-}
import Graphics.Rendering.Pango.Enums (FontMetrics)
import Graphics.Rendering.Pango.Description
import Graphics.Rendering.Pango.Structs


{-# LINE 89 "./Graphics/Rendering/Pango/Font.chs" #-}


-- | Ask for the different font families that a particular back-end supports.
--
-- * The 'FontMap' can be acquired by calling
-- 'Graphics.Rendering.Pango.Cairo.cairoFontMapGetDefault'.
--
pangoFontMapListFamilies :: FontMap -> IO [FontFamily]
pangoFontMapListFamilies :: FontMap -> IO [FontFamily]
pangoFontMapListFamilies FontMap
fm = (Ptr (Ptr FontFamily) -> IO [FontFamily]) -> IO [FontFamily]
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr FontFamily) -> IO [FontFamily]) -> IO [FontFamily])
-> (Ptr (Ptr FontFamily) -> IO [FontFamily]) -> IO [FontFamily]
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr FontFamily)
arrPtrPtr -> (Ptr CInt -> IO [FontFamily]) -> IO [FontFamily]
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO [FontFamily]) -> IO [FontFamily])
-> (Ptr CInt -> IO [FontFamily]) -> IO [FontFamily]
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
sizePtr -> do
  (\(FontMap ForeignPtr FontMap
arg1) Ptr (Ptr FontFamily)
arg2 Ptr CInt
arg3 -> ForeignPtr FontMap -> (Ptr FontMap -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontMap
arg1 ((Ptr FontMap -> IO ()) -> IO ())
-> (Ptr FontMap -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr FontMap
argPtr1 ->Ptr FontMap -> Ptr (Ptr FontFamily) -> Ptr CInt -> IO ()
pango_font_map_list_families Ptr FontMap
argPtr1 Ptr (Ptr FontFamily)
arg2 Ptr CInt
arg3) FontMap
fm Ptr (Ptr FontFamily)
arrPtrPtr Ptr CInt
sizePtr
  Ptr FontFamily
arrPtr <- Ptr (Ptr FontFamily) -> IO (Ptr FontFamily)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr FontFamily)
arrPtrPtr
  CInt
size <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
sizePtr
  [Ptr FontFamily]
ffsPtr <- Int -> Ptr (Ptr FontFamily) -> IO [Ptr FontFamily]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size)
            (Ptr FontFamily -> Ptr (Ptr FontFamily)
forall a b. Ptr a -> Ptr b
castPtr Ptr FontFamily
arrPtr::Ptr (Ptr FontFamily)) -- c2hs is wrong here
  Ptr () -> IO ()
g_free (Ptr FontFamily -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr FontFamily
arrPtr)
  (Ptr FontFamily -> IO FontFamily)
-> [Ptr FontFamily] -> IO [FontFamily]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((ForeignPtr FontFamily -> FontFamily, FinalizerPtr FontFamily)
-> IO (Ptr FontFamily) -> IO FontFamily
forall obj.
GObjectClass obj =>
(ForeignPtr obj -> obj, FinalizerPtr obj) -> IO (Ptr obj) -> IO obj
makeNewGObject (ForeignPtr FontFamily -> FontFamily, FinalizerPtr FontFamily)
forall {a}. (ForeignPtr FontFamily -> FontFamily, FinalizerPtr a)
mkFontFamily (IO (Ptr FontFamily) -> IO FontFamily)
-> (Ptr FontFamily -> IO (Ptr FontFamily))
-> Ptr FontFamily
-> IO FontFamily
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr FontFamily -> IO (Ptr FontFamily)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr FontFamily -> IO (Ptr FontFamily))
-> (Ptr FontFamily -> Ptr FontFamily)
-> Ptr FontFamily
-> IO (Ptr FontFamily)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr FontFamily -> Ptr FontFamily
forall a b. Ptr a -> Ptr b
castPtr) [Ptr FontFamily]
ffsPtr

instance Show FontFamily where
  show :: FontFamily -> String
show FontFamily
ff = Text -> String
T.unpack (Text -> String) -> (IO Text -> Text) -> IO Text -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO Text -> Text
forall a. IO a -> a
unsafePerformIO (IO Text -> String) -> IO Text -> String
forall a b. (a -> b) -> a -> b
$ do
    Ptr CChar
strPtr <- (\(FontFamily ForeignPtr FontFamily
arg1) -> ForeignPtr FontFamily
-> (Ptr FontFamily -> IO (Ptr CChar)) -> IO (Ptr CChar)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontFamily
arg1 ((Ptr FontFamily -> IO (Ptr CChar)) -> IO (Ptr CChar))
-> (Ptr FontFamily -> IO (Ptr CChar)) -> IO (Ptr CChar)
forall a b. (a -> b) -> a -> b
$ \Ptr FontFamily
argPtr1 ->Ptr FontFamily -> IO (Ptr CChar)
pango_font_family_get_name Ptr FontFamily
argPtr1) FontFamily
ff
    Ptr CChar -> IO Text
forall s. GlibString s => Ptr CChar -> IO s
peekUTFString Ptr CChar
strPtr


-- | Ask if the given family contains monospace fonts.
--
-- * A monospace font is a font designed for text display where the
-- characters form a regular grid. For Western languages this would
-- mean that the advance width of all characters are the same, but
-- this categorization also includes Asian fonts which include
-- double-width characters: characters that occupy two grid cells.
--
-- * The best way to find out the grid-cell size is to query the members
-- of the according 'FontMetrics' structure.
--
pangoFontFamilyIsMonospace :: FontFamily -> Bool
pangoFontFamilyIsMonospace :: FontFamily -> Bool
pangoFontFamilyIsMonospace FontFamily
ff = IO Bool -> Bool
forall a. IO a -> a
unsafePerformIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$
  (CInt -> Bool) -> IO CInt -> IO Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ (\(FontFamily ForeignPtr FontFamily
arg1) -> ForeignPtr FontFamily -> (Ptr FontFamily -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontFamily
arg1 ((Ptr FontFamily -> IO CInt) -> IO CInt)
-> (Ptr FontFamily -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr FontFamily
argPtr1 ->Ptr FontFamily -> IO CInt
pango_font_family_is_monospace Ptr FontFamily
argPtr1) FontFamily
ff


-- | Ask for the faces contained in a particular family.
--
-- * Asks for all font faces in the given family. The faces in a family
-- share a common design, but differ in slant, weight, width and other
-- aspects. For example, the font family "Sans" contains several fonts
-- such as Helvetica and Arial.
--
pangoFontFamilyListFaces :: FontFamily -> IO [FontFace]
pangoFontFamilyListFaces :: FontFamily -> IO [FontFace]
pangoFontFamilyListFaces FontFamily
ff = (Ptr (Ptr FontFace) -> IO [FontFace]) -> IO [FontFace]
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr FontFace) -> IO [FontFace]) -> IO [FontFace])
-> (Ptr (Ptr FontFace) -> IO [FontFace]) -> IO [FontFace]
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr FontFace)
arrPtrPtr -> (Ptr CInt -> IO [FontFace]) -> IO [FontFace]
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO [FontFace]) -> IO [FontFace])
-> (Ptr CInt -> IO [FontFace]) -> IO [FontFace]
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
sizePtr -> do
  (\(FontFamily ForeignPtr FontFamily
arg1) Ptr (Ptr FontFace)
arg2 Ptr CInt
arg3 -> ForeignPtr FontFamily -> (Ptr FontFamily -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontFamily
arg1 ((Ptr FontFamily -> IO ()) -> IO ())
-> (Ptr FontFamily -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr FontFamily
argPtr1 ->Ptr FontFamily -> Ptr (Ptr FontFace) -> Ptr CInt -> IO ()
pango_font_family_list_faces Ptr FontFamily
argPtr1 Ptr (Ptr FontFace)
arg2 Ptr CInt
arg3) FontFamily
ff Ptr (Ptr FontFace)
arrPtrPtr Ptr CInt
sizePtr
  Ptr FontFace
arrPtr <- Ptr (Ptr FontFace) -> IO (Ptr FontFace)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr FontFace)
arrPtrPtr
  CInt
size <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
sizePtr
  [Ptr FontFace]
ffsPtr <- Int -> Ptr (Ptr FontFace) -> IO [Ptr FontFace]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size)
            (Ptr FontFace -> Ptr (Ptr FontFace)
forall a b. Ptr a -> Ptr b
castPtr Ptr FontFace
arrPtr::Ptr (Ptr FontFace)) -- c2hs is wrong here
  Ptr () -> IO ()
g_free (Ptr FontFace -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr FontFace
arrPtr)
  (Ptr FontFace -> IO FontFace) -> [Ptr FontFace] -> IO [FontFace]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((ForeignPtr FontFace -> FontFace, FinalizerPtr FontFace)
-> IO (Ptr FontFace) -> IO FontFace
forall obj.
GObjectClass obj =>
(ForeignPtr obj -> obj, FinalizerPtr obj) -> IO (Ptr obj) -> IO obj
makeNewGObject (ForeignPtr FontFace -> FontFace, FinalizerPtr FontFace)
forall {a}. (ForeignPtr FontFace -> FontFace, FinalizerPtr a)
mkFontFace (IO (Ptr FontFace) -> IO FontFace)
-> (Ptr FontFace -> IO (Ptr FontFace))
-> Ptr FontFace
-> IO FontFace
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr FontFace -> IO (Ptr FontFace)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr FontFace -> IO (Ptr FontFace))
-> (Ptr FontFace -> Ptr FontFace)
-> Ptr FontFace
-> IO (Ptr FontFace)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr FontFace -> Ptr FontFace
forall a b. Ptr a -> Ptr b
castPtr) [Ptr FontFace]
ffsPtr

instance Show FontFace where
  show :: FontFace -> String
show FontFace
ff = Text -> String
T.unpack (Text -> String) -> (IO Text -> Text) -> IO Text -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO Text -> Text
forall a. IO a -> a
unsafePerformIO (IO Text -> String) -> IO Text -> String
forall a b. (a -> b) -> a -> b
$ do
    Ptr CChar
strPtr <- (\(FontFace ForeignPtr FontFace
arg1) -> ForeignPtr FontFace
-> (Ptr FontFace -> IO (Ptr CChar)) -> IO (Ptr CChar)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontFace
arg1 ((Ptr FontFace -> IO (Ptr CChar)) -> IO (Ptr CChar))
-> (Ptr FontFace -> IO (Ptr CChar)) -> IO (Ptr CChar)
forall a b. (a -> b) -> a -> b
$ \Ptr FontFace
argPtr1 ->Ptr FontFace -> IO (Ptr CChar)
pango_font_face_get_face_name Ptr FontFace
argPtr1) FontFace
ff
    Ptr CChar -> IO Text
forall s. GlibString s => Ptr CChar -> IO s
peekUTFString Ptr CChar
strPtr


-- | Ask for available sizes of this font face.
--
-- * List the available sizes for a font. This is only applicable to bitmap
-- fonts since all other fonts can be scaled arbitrarily. For scalable
-- fonts, this function returns @Nothing@. The sizes returned are in
-- ascending order, their unit is points (1\/72 inch).
--
pangoFontFaceListSizes :: FontFace -> IO (Maybe [Double])
pangoFontFaceListSizes :: FontFace -> IO (Maybe [Double])
pangoFontFaceListSizes FontFace
ff = (Ptr (Ptr CInt) -> IO (Maybe [Double])) -> IO (Maybe [Double])
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr CInt) -> IO (Maybe [Double])) -> IO (Maybe [Double]))
-> (Ptr (Ptr CInt) -> IO (Maybe [Double])) -> IO (Maybe [Double])
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CInt)
arrPtrPtr -> (Ptr CInt -> IO (Maybe [Double])) -> IO (Maybe [Double])
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO (Maybe [Double])) -> IO (Maybe [Double]))
-> (Ptr CInt -> IO (Maybe [Double])) -> IO (Maybe [Double])
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
sizePtr -> do
  (\(FontFace ForeignPtr FontFace
arg1) Ptr (Ptr CInt)
arg2 Ptr CInt
arg3 -> ForeignPtr FontFace -> (Ptr FontFace -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontFace
arg1 ((Ptr FontFace -> IO ()) -> IO ())
-> (Ptr FontFace -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr FontFace
argPtr1 ->Ptr FontFace -> Ptr (Ptr CInt) -> Ptr CInt -> IO ()
pango_font_face_list_sizes Ptr FontFace
argPtr1 Ptr (Ptr CInt)
arg2 Ptr CInt
arg3) FontFace
ff Ptr (Ptr CInt)
arrPtrPtr Ptr CInt
sizePtr
  Ptr CInt
arrPtr <- Ptr (Ptr CInt) -> IO (Ptr CInt)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CInt)
arrPtrPtr
  CInt
size <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
sizePtr
  if Ptr CInt
arrPtrPtr CInt -> Ptr CInt -> Bool
forall a. Eq a => a -> a -> Bool
==Ptr CInt
forall a. Ptr a
nullPtr then Maybe [Double] -> IO (Maybe [Double])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [Double]
forall a. Maybe a
Nothing else do
    [CInt]
sizes <- Int -> Ptr CInt -> IO [CInt]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size) Ptr CInt
arrPtr
    Ptr () -> IO ()
g_free (Ptr CInt -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr CInt
arrPtr)
    Maybe [Double] -> IO (Maybe [Double])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Double] -> Maybe [Double]
forall a. a -> Maybe a
Just ((CInt -> Double) -> [CInt] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map CInt -> Double
intToPu [CInt]
sizes))


-- | Ask for a description of this face.
--
-- * Returns the family, style, variant, weight and stretch of a 'FontFace'.
-- The size field of the resulting font description will be unset.
--
pangoFontFaceDescribe :: FontFace -> IO FontDescription
pangoFontFaceDescribe :: FontFace -> IO FontDescription
pangoFontFaceDescribe FontFace
ff = do
  Ptr FontDescription
fdPtr <- (\(FontFace ForeignPtr FontFace
arg1) -> ForeignPtr FontFace
-> (Ptr FontFace -> IO (Ptr FontDescription))
-> IO (Ptr FontDescription)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr FontFace
arg1 ((Ptr FontFace -> IO (Ptr FontDescription))
 -> IO (Ptr FontDescription))
-> (Ptr FontFace -> IO (Ptr FontDescription))
-> IO (Ptr FontDescription)
forall a b. (a -> b) -> a -> b
$ \Ptr FontFace
argPtr1 ->Ptr FontFace -> IO (Ptr FontDescription)
pango_font_face_describe Ptr FontFace
argPtr1) FontFace
ff
  Ptr FontDescription -> IO FontDescription
makeNewFontDescription Ptr FontDescription
fdPtr

foreign import ccall unsafe "pango_font_map_list_families"
  pango_font_map_list_families :: ((Ptr FontMap) -> ((Ptr (Ptr FontFamily)) -> ((Ptr CInt) -> (IO ()))))

foreign import ccall unsafe "g_free"
  g_free :: ((Ptr ()) -> (IO ()))

foreign import ccall unsafe "pango_font_family_get_name"
  pango_font_family_get_name :: ((Ptr FontFamily) -> (IO (Ptr CChar)))

foreign import ccall unsafe "pango_font_family_is_monospace"
  pango_font_family_is_monospace :: ((Ptr FontFamily) -> (IO CInt))

foreign import ccall unsafe "pango_font_family_list_faces"
  pango_font_family_list_faces :: ((Ptr FontFamily) -> ((Ptr (Ptr FontFace)) -> ((Ptr CInt) -> (IO ()))))

foreign import ccall unsafe "pango_font_face_get_face_name"
  pango_font_face_get_face_name :: ((Ptr FontFace) -> (IO (Ptr CChar)))

foreign import ccall unsafe "pango_font_face_list_sizes"
  pango_font_face_list_sizes :: ((Ptr FontFace) -> ((Ptr (Ptr CInt)) -> ((Ptr CInt) -> (IO ()))))

foreign import ccall unsafe "pango_font_face_describe"
  pango_font_face_describe :: ((Ptr FontFace) -> (IO (Ptr FontDescription)))