packages feed

WXDiffCtrl-0.0.1: src/Graphics/UI/WXContrib/WXFontMetrics.hs

{-
   Copyright (C) Jeremy O'Donoghue (jeremy.odonoghue@gmail.com) 2007-2010.
   All Rights Reserved.

   Redistribution and use in source and binary forms, with or without 
   modification, are permitted provided that the following conditions 
   are met:

   Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
   Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

   The name of Jeremy O'Donoghue may not be used to endorse or promote
   products derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
-}

module Graphics.UI.WXContrib.WXFontMetrics 
    ( FontMetrics(..)
    , getFontMetrics 
    )
    where

import Graphics.UI.WXCore
import Graphics.UI.WX

-- | FontMetrics for a given device context.
data FontMetrics = FontMetrics { fm_x    :: !Int  -- | Largest X value
                               , fm_y    :: !Int  -- | Largest Y value
                               , fm_desc :: !Int  -- | Largest value of descender
                               , fm_el   :: !Int  -- | Largest external leader
                               } deriving Show


-- | Return font metrics (based on several selected characters) for 
--   a single character in a given window based on the supplied text 
--   attributes. An attempt is made to determine the maximum likely
--   space needed for any character in the font, although the reality
--   is that this is inevitably somewhat font dependent.
getFontMetrics :: Window a -> FontStyle -> IO FontMetrics
getFontMetrics win font =
    do
    dc <- windowDCCreate win
    dcSetFontStyle dc font
    (sz1, d1, el1) <- getFullTextExtent dc "W"  -- A wide character
    (sz2, d2, el2) <- getFullTextExtent dc "y"  -- A character with descender
    (sz3, d3, el3) <- getFullTextExtent dc "l"  -- May have external leading
    let szx1 = sizeW sz1
        szy1 = sizeH sz1
        szx2 = sizeW sz2
        szy2 = sizeH sz2
        szx3 = sizeW sz3
        szy3 = sizeH sz3
        fm   = FontMetrics (maximum [szx1, szx2, szx3]) -- Largest X value for size
                           (maximum [szy1, szy2, szy3]) -- Largest Y value for size
                           (maximum [d1, d2, d3])       -- Largest value of descender
                           (maximum [el1, el2, el3])    -- Largest value of external leading
    windowDCDelete dc 
    return fm